Create browsers directory and browsers/common for common navigator.gssEap.js, move...
[gssweb.git] / browsers / chrome / app / gssweb_background.js
1 console.log("gssweb_background.js loading: #4");
2
3
4
5 var gssNativePort = null;
6 var applicationPorts = {};
7
8 /* What to do with the output of the GSS command line */
9 function onGSSResponse(msg) {
10   var nonce;
11   var appPort;
12   var appTag;
13
14   // Read the cookies out of the response  
15   if ( typeof(msg.cookies) == 'undefined' ||
16        typeof(msg.cookies.gssweb_bg_tag) == 'undefined' )
17   {
18     console.error(
19       "gssweb_background.js received a response from the command-line NativeHost with no gssweb_bg_tag cookie."
20     );
21     return;    
22   }
23   appTag = msg.cookies.app_tag;
24   nonce = msg.cookies.gssweb_bg_tag;
25   msg.cookies.gssweb_bg_tag = undefined;
26
27   // Informationally log
28   console.info('[' + appTag + '] Response from GSS command line: [' + 
29                JSON.stringify(msg) + ']'
30               );
31
32   // Find the content script's port that should receive this message  
33   appPort = applicationPorts[nonce]
34   applicationPorts[nonce] = undefined;
35   if ( typeof(appPort) == "undefined")
36   {
37     console.error(
38       "[" + appTag + "] gssweb_background.js received a response from the command-line NativeHost with no associated application port."
39     );
40     return;    
41   }
42   // appPort is now guaranteed to exist.
43
44
45   // Send the message on to the content script
46   appPort.postMessage(msg);
47   
48   console.info('[' + appTag + '] Response sent to the content script.');
49 }
50
51 function connectToNativeHost() {
52   console.info('Connecting to json_gssapi command line.');
53   // var host = 'com.google.chrome.example.echo';
54   var host = 'com.painlesssecurity.jsongss';
55   gssNativePort = chrome.runtime.connectNative( host );
56   gssNativePort.onMessage.addListener( onGSSResponse );
57 }
58
59
60 connectToNativeHost();
61
62 // When we receive a connection from a page through the content script...
63 chrome.runtime.onConnect.addListener(
64   function(appPort) 
65   {
66     // ... First, make sure that we're talking to the right people
67     console.assert(appPort.name == "com.painlesssecurity.gssweb");
68     
69     appPort.onMessage.addListener(
70       // Now, when we receive a message
71       function(msg)
72       {
73         var nonce;
74         var appTag;
75
76         // Deal with the cookies in the message
77         if ( typeof(msg.cookies) == 'undefined')
78         {
79           msg.cookies = {};
80         }
81         appTag = msg.cookies.app_tag;
82
83         // Save out the port
84         nonce = navigator.generateNonce();
85         applicationPorts[nonce] = appPort;
86         msg.cookies.gssweb_bg_tag = nonce;
87         
88         // Send the message to the NativePort / command line
89         console.info(
90           '[' + appTag + '] About to send message to Native Port: [' +
91           JSON.stringify(msg) + ']'
92         );
93         gssNativePort.postMessage(msg);
94         console.info('[' + appTag + '] ... message sent to Native Port.')
95         
96       }
97     );
98   }
99 );
100