Add copyright to browser plug-in file, finish code reorg and cleanup.
[gssweb.git] / browsers / chrome / app / background.js
1 /*
2  * Copyright (c) 2015, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 console.log("gssweb_background.js loading: #4");
35
36 var gssNativePort = null;
37 var applicationPorts = {};
38
39 navigator.generateNonce = function() { 
40   // TODO: Make sure that we don't have a collision! 
41   // Random integer in the range [0..(2^32-1)] 
42     return Math.floor(Math.random() * ( 4294967295 )) ; 
43
44
45 /* What to do with the output of the GSS command line */
46 function onGSSResponse(msg) {
47   var nonce;
48   var appPort;
49   var appTag;
50
51   // Read the cookies out of the response  
52   if ( typeof(msg.cookies) == 'undefined' ||
53        typeof(msg.cookies.gssweb_bg_tag) == 'undefined' )
54   {
55     console.error(
56       "gssweb_background.js received a response from the command-line NativeHost with no gssweb_bg_tag cookie."
57     );
58     return;    
59   }
60   appTag = msg.cookies.app_tag;
61   nonce = msg.cookies.gssweb_bg_tag;
62   msg.cookies.gssweb_bg_tag = undefined;
63
64   // Informationally log
65   console.info('[' + appTag + '] Response from GSS command line: [' + 
66                JSON.stringify(msg) + ']'
67               );
68
69   // Find the content script's port that should receive this message  
70   appPort = applicationPorts[nonce]
71   applicationPorts[nonce] = undefined;
72   if ( typeof(appPort) == "undefined")
73   {
74     console.error(
75       "[" + appTag + "] gssweb_background.js received a response from the command-line NativeHost with no associated application port."
76     );
77     return;    
78   }
79   // appPort is now guaranteed to exist.
80
81
82   // Send the message on to the content script
83   appPort.postMessage(msg);
84   
85   console.info('[' + appTag + '] Response sent to the content script.');
86 }
87
88 function connectToNativeHost() {
89   console.info('Connecting to json_gssapi command line.');
90   // var host = 'com.google.chrome.example.echo';
91   var host = 'com.painlesssecurity.jsongss';
92   gssNativePort = chrome.runtime.connectNative( host );
93   gssNativePort.onMessage.addListener( onGSSResponse );
94 }
95
96
97 connectToNativeHost();
98
99 // When we receive a connection from a page through the content script...
100 chrome.runtime.onConnect.addListener(
101   function(appPort) 
102   {
103     // ... First, make sure that we're talking to the right people
104     console.assert(appPort.name == "com.painlesssecurity.gssweb");
105     
106     appPort.onMessage.addListener(
107       // Now, when we receive a message
108       function(msg)
109       {
110         var nonce;
111         var appTag;
112
113         // Deal with the cookies in the message
114         if ( typeof(msg.cookies) == 'undefined')
115         {
116           msg.cookies = {};
117         }
118         appTag = msg.cookies.app_tag;
119
120         // Save out the port
121         nonce = navigator.generateNonce();
122         applicationPorts[nonce] = appPort;
123         msg.cookies.gssweb_bg_tag = nonce;
124         
125         // Send the message to the NativePort / command line
126         console.info(
127           '[' + appTag + '] About to send message to Native Port: [' +
128           JSON.stringify(msg) + ']'
129         );
130         gssNativePort.postMessage(msg);
131         console.info('[' + appTag + '] ... message sent to Native Port.')
132         
133       }
134     );
135   }
136 );
137