Native Messaging WIP - init origin; api fixes
authorBill Erickson <berickxx@gmail.com>
Mon, 14 Nov 2016 18:04:56 +0000 (13:04 -0500)
committerBill Erickson <berickxx@gmail.com>
Mon, 14 Nov 2016 18:04:56 +0000 (13:04 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
extension/app/main.js
src/org/evergreen_ils/hatch/RequestHandler.java

index f13ed35..6eefbdf 100644 (file)
@@ -29,7 +29,7 @@ function onNativeMessage(message) {
 
     if (tabId) {
         if (browserPorts[tabId]) {
-            browserPorts[tabId].postMessage(JSON.stringify(message));
+            browserPorts[tabId].postMessage(message);
 
         } else {
             console.warn(
@@ -63,7 +63,23 @@ chrome.runtime.onConnectExternal.addListener(function(port) {
     console.debug('new port connected with id ' + tabId);
 
     port.onMessage.addListener(function(msg) {
-        console.log("Received message from browser on port " + tabId);
+        console.debug("Received message from browser on port " + tabId);
+
+        if (!msg) { // belt+suspenders
+            console.warn("Received NULL message");
+            return;
+        }
+
+        // tag the message with the browser tab ID for response routing.
+        msg.clientid = tabId; 
+
+        if (msg.action == 'init') {
+            // "init" messages require origin info.
+            // Extract just the protocol + host
+            msg.origin = port.sender.url.match(/https?:\/\/[^\/]+/)[0];
+            console.debug("Init'ing message with origin: " + msg.origin);
+        }
+
         hatchPort.postMessage(msg);
     });
 
index eeace1b..d4c0441 100644 (file)
@@ -64,7 +64,8 @@ public class RequestHandler extends Thread {
         logger.info("Received message id=" + 
             response.get("msgid") + " action=" + action);
 
-        // init must be called first to set the origin info
+        // init must be called first to set the origin info. Init may be
+        // called multiple times.  Each call will update the origin info.
         if (action.equals("init")) {
             origin = request.getString("origin");
             return false;
@@ -83,7 +84,7 @@ public class RequestHandler extends Thread {
         switch (action) {
 
             case "printers":
-                response.put("printers",
+                response.put("content",
                     new PrintManager().getPrintersAsMaps());
                 break;
 
@@ -105,7 +106,7 @@ public class RequestHandler extends Thread {
 
             case "keys": // Return stored keys
                 String pfxKey = request.optString("key");
-                response.put("keys", fileIO.keys(pfxKey));
+                response.put("content", fileIO.keys(pfxKey));
                 break;
             
             case "get":