2 * Copyright (c) 2015, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 console.log("gssweb_background.js loading: #4");
36 var gssNativePort = null;
37 var applicationPorts = {};
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 )) ;
45 /* What to do with the output of the GSS command line */
46 function onGSSResponse(msg) {
51 // Read the cookies out of the response
52 if ( typeof(msg.cookies) == 'undefined' ||
53 typeof(msg.cookies.gssweb_bg_tag) == 'undefined' )
56 "gssweb_background.js received a response from the command-line NativeHost with no gssweb_bg_tag cookie."
60 appTag = msg.cookies.app_tag;
61 nonce = msg.cookies.gssweb_bg_tag;
62 msg.cookies.gssweb_bg_tag = undefined;
64 // Informationally log
65 console.info('[' + appTag + '] Response from GSS command line: [' +
66 JSON.stringify(msg) + ']'
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")
75 "[" + appTag + "] gssweb_background.js received a response from the command-line NativeHost with no associated application port."
79 // appPort is now guaranteed to exist.
82 // Send the message on to the content script
83 appPort.postMessage(msg);
85 console.info('[' + appTag + '] Response sent to the content script.');
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 );
97 connectToNativeHost();
99 // When we receive a connection from a page through the content script...
100 chrome.runtime.onConnect.addListener(
103 // ... First, make sure that we're talking to the right people
104 console.assert(appPort.name == "com.painlesssecurity.gssweb");
106 appPort.onMessage.addListener(
107 // Now, when we receive a message
113 // Deal with the cookies in the message
114 if ( typeof(msg.cookies) == 'undefined')
118 appTag = msg.cookies.app_tag;
121 nonce = navigator.generateNonce();
122 applicationPorts[nonce] = appPort;
123 msg.cookies.gssweb_bg_tag = nonce;
125 // Send the message to the NativePort / command line
127 '[' + appTag + '] About to send message to Native Port: [' +
128 JSON.stringify(msg) + ']'
130 gssNativePort.postMessage(msg);
131 console.info('[' + appTag + '] ... message sent to Native Port.')