Add copyright to browser plug-in file, finish code reorg and cleanup.
[gssweb.git] / browsers / firefox / lib / main.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
35 var tabs = require("sdk/tabs");
36 const {Cu} = require("chrome");
37
38 Cu.import("resource://gre/modules/ctypes.jsm");
39
40
41 // TODO: detect which OS we're running, and proactively search for the
42 //       correct library name.
43 var json_gssapi;
44 try {
45     json_gssapi = ctypes.open("libjsongssapi.so");
46 }
47 catch (e) {
48     try {
49         json_gssapi = ctypes.open("jsongssapi.dll");
50     }
51     catch (e) {
52         console.log("Error loading the JSON GSS-API library: " + e);
53     }
54 }
55
56 var gss_request = function(msg) { return "{\"error_message\":\"JSON GSS-API library was not loaded properly\"}"; }
57 try {
58 gss_request = json_gssapi.declare("gss_request",
59     ctypes.default_abi,
60     ctypes.char.ptr,
61     ctypes.char.ptr);
62 }
63 catch (e) {
64   console.log("Could not find the function 'gss_request' in the JSON GSS-API library: " + e);
65 }
66
67 var self = require("sdk/self");
68 var data = require("sdk/self").data;
69 var pageMod = require("sdk/page-mod");
70
71 pageMod.PageMod({
72   include: "*",
73   contentScriptFile: [data.url("contentscript.js")],
74   contentScriptWhen: "ready"
75 });
76
77
78
79 function invokeNativeGSS(msg)
80 {
81   var appTag;
82   var reply;
83   var response;
84   
85   // Deal with the cookies in the message
86   if ( typeof(msg.cookies) == 'undefined')
87   {
88     msg.cookies = {};
89   }
90   appTag = msg.cookies.app_tag;
91
92   // Send the message to the NativePort / command line
93   console.info(
94     '[' + appTag + '] About to invoke native function: [' +
95     JSON.stringify(msg) + ']'
96   );
97   reply = gss_request( JSON.stringify(msg) );
98   response = JSON.parse(reply.readString() );
99   console.info('[' + appTag + '] ... native function invoked.');
100   console.info('[' + appTag + '] ... returned: ' + response);
101   
102   return(response);
103 }
104
105 tabs.on("ready", function(tab) {
106   app = tab.attach({ contentScriptFile: data.url("contentscript.js") });
107   app.port.on("gss_request", function(message) {
108     var response = invokeNativeGSS(message);
109     app.port.emit("gss_response", response);
110   });
111 });