Typo in a comment
[gssweb.git] / chrome / app / navigator.gss.js
1 console.log('Loading navigator.gss.js - #7');
2
3 /* This file gets injected into the web page verbatim */
4
5
6
7 var GSSEap = (function () 
8   {
9     function GSSEap(config)
10     {
11         // Public attributes
12         this.version = "0.0.1";
13         this.implemented_methods = ["gss_import_name"];
14         this.callbacks = {};
15         this.methods = {};
16         this.errors = {};
17         this.appTag = config.appTag || "";
18         this.default_error = config.error || 
19             function (major, minor, error, appTag) {
20                 console.warn(error);
21             };
22         window.addEventListener(
23             "message",
24             this.dispatch_responses.bind(this)
25         );
26     }
27     GSSEap.prototype.dispatch_responses = function (event) 
28     {
29         var method;
30         var nonce;
31         var callback;
32         var app_tag;
33         var error;
34
35         /* This message is destined for us only if all the following apply:
36         * - The data.method_name is one of the methods implemented by this
37         *   object
38         * - data.return_values exists
39         * - data.cookies exists
40         * - One of my callbacks matches the nonce in
41         *   data.cookies.navigator_gss_tag
42         * - One of my methods matches the nonce in 
43         *   data.cookies.navigator_gss_tag and that method matches
44         *   data.method
45         */
46         method = event.data.method;
47         if (
48              ( -1 == this.implemented_methods.indexOf(method) ) ||
49              ("undefined" == typeof (event.data.return_values)) ||
50              ("undefined" == typeof (event.data.cookies)))
51         {
52             return;
53         }
54
55         nonce = event.data.cookies.navigator_gss_tag;
56         event.data.cookies.navigator_gss_tag = undefined;
57         callback = this.callbacks[nonce];
58         if ("undefined" == typeof (callback) || 
59             this.methods[nonce] != method) {
60             return;
61         }
62
63         // We now know that this message is for us!
64         this.callbacks[nonce] = undefined;
65         app_tag = event.data.cookies.app_tag;
66         error = this.errors[nonce] || this.default_error;
67
68         if (this.gss_error(event.data.return_values.major_status))
69         {
70             var errMsg = "Error during " + method + ": " + 
71               "Major status message: " + 
72               event.data.return_values.errors.major_status_message + 
73               "; Minor status message: " + 
74               event.data.return_values.errors.minor_status_message;
75             error(
76               event.data.return_values.major_status,
77               event.data.return_values.minor_status, 
78               errMsg,
79               app_tag);
80         } else {
81             callback(event.data.return_values, app_tag);
82         }
83     };
84
85     GSSEap.prototype.import_name = function (params) 
86     {
87         /* variables */
88         var nonce;
89         var name = params.name;
90         var name_type = params.name_type || "{1 2 840 113554 1 2 1 4 }";
91         var callback = params.success;
92         var error = params.error || this.default_error; 
93         var app_tag = params.app_tag || this.appTag;
94
95         /* Error checking */
96         // Call an error if we don't have the required parameters.
97         // - name
98         // - success()
99         if ( "undefined" == typeof(name) ||
100              "undefined" == typeof(callback) )
101         {
102           error(-1, -1, 
103             "import_name called missing either name or success callback"
104           );
105           return;
106         }
107         
108         nonce = navigator.generateNonce();
109         this.callbacks[nonce] = callback;
110         this.methods[nonce] = "gss_import_name";
111         this.errors[nonce] = error;
112         window.postMessage({
113             "method":"gss_import_name",
114             "arguments":
115             {
116                 "input_name": name,
117                 "input_name_type": name_type
118             },
119             "cookies":
120             {
121                 "navigator_gss_tag": nonce,
122                 "app_tag": app_tag
123             }
124         }, "*");
125         
126     };
127
128     GSSEap.prototype.gss_error = function (major) 
129     {
130         var callingMask;
131         var routineMask;
132         var mask;
133
134         callingMask = 255 << 24;
135         routineMask = 255 << 16;
136         mask = callingMask | routineMask;
137
138         return (0 != (major & mask));
139     };
140     return GSSEap;
141 })();
142
143 navigator.gss_eap = GSSEap;