From: Mark Donnelly Date: Wed, 3 Dec 2014 21:26:56 +0000 (-0500) Subject: Requests originating in content scripts now generate responses to the content scripts X-Git-Url: http://www.project-moonshot.org/gitweb/?p=gssweb.git;a=commitdiff_plain;h=086d59db41dc1608c92b98409f2beb1df2f41909 Requests originating in content scripts now generate responses to the content scripts --- diff --git a/firefox/data/navigator.gss.js b/firefox/data/navigator.gss.js index 7f3b4df..35c8985 100644 --- a/firefox/data/navigator.gss.js +++ b/firefox/data/navigator.gss.js @@ -15,4 +15,18 @@ self.port.on('alert', function(message) { }); +var msg = { + 'method': 'gss_import_name', + 'arguments': { + 'input_name': 'HTTP@localhost', + 'input_name_type': "{1 2 840 113554 1 2 1 4 }" + }, + 'cookies': { + 'app_tag': 'Test request' + } +}; +self.port.on('gss_response', function(message) { + console.log("Content script received a reply from gss_import_name: " + JSON.stringify(message) ); +}); +self.port.emit("gss_request", msg); diff --git a/firefox/lib/main.js b/firefox/lib/main.js index 4fa60b0..f38786e 100644 --- a/firefox/lib/main.js +++ b/firefox/lib/main.js @@ -29,16 +29,18 @@ catch (e) { console.log(" ... loaded."); console.log("Declaring the call_gss function:"); -const call_gss = json_gssapi.declare("gss_request", +const gss_request = json_gssapi.declare("gss_request", ctypes.default_abi, ctypes.char.ptr, ctypes.char.ptr); console.log(" ... declared."); +/* console.log("Calling import_name"); var reply; reply = call_gss("{\"method\":\"gss_import_name\",\"arguments\":{\"input_name\":\"HTTP@localhost\",\"input_name_type\":\"{1 2 840 113554 1 2 1 4 }\"}}"); console.log(" ... Reply: " + reply.readString()); +*/ var self = require("sdk/self"); @@ -51,22 +53,38 @@ pageMod.PageMod({ contentScriptWhen: "ready" }); -tabs.on("ready", function(tab) { - worker = tab.attach({ contentScriptFile: data.url("navigator.gss.js") }); - worker.port.on("gss_request", function(message) { - console.log("main.js received message: " + message); - }); - worker.port.emit("alert", "Message from the add-on"); -}); -/*var gssweb = { - myListener: function(evt) { - alert("Received from web page: " + - evt.target.getAttribute("attribute1") + "/" + - evt.target.getAttribute("attribute2")); +function invokeNativeGSS(msg) +{ + var appTag; + var reply; + var response; + + // Deal with the cookies in the message + if ( typeof(msg.cookies) == 'undefined') + { + msg.cookies = {}; } -}*/ -//document.addEventListener("GsswebEvent", function(e) { gssweb.myListener(e); }, false, true); -// The last value is a Mozilla-specific value to indicate untrusted content is allowed to trigger the event. + appTag = msg.cookies.app_tag; + + // Send the message to the NativePort / command line + console.info( + '[' + appTag + '] About to invoke native function: [' + + JSON.stringify(msg) + ']' + ); + reply = gss_request( JSON.stringify(msg) ); + response = JSON.parse(reply.readString() ); + console.info('[' + appTag + '] ... native function invoked.'); + console.info('[' + appTag + '] ... returned: ' + response); + + return(response); +} +tabs.on("ready", function(tab) { + app = tab.attach({ contentScriptFile: data.url("navigator.gss.js") }); + app.port.on("gss_request", function(message) { + var response = invokeNativeGSS(message); + app.port.emit("gss_response", response); + }); +});