Native Messaging WIP - browser extension bits
authorBill Erickson <berickxx@gmail.com>
Thu, 10 Nov 2016 21:05:16 +0000 (16:05 -0500)
committerBill Erickson <berickxx@gmail.com>
Thu, 10 Nov 2016 21:05:16 +0000 (16:05 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
extension/app/README [new file with mode: 0644]
extension/app/main.html [new file with mode: 0644]
extension/app/main.js [new file with mode: 0644]
extension/app/manifest.json [new file with mode: 0644]
extension/host/README [new file with mode: 0644]
extension/host/org.evergreen_ils.hatch.json [new file with mode: 0644]
start-hatch [new file with mode: 0755]

diff --git a/extension/app/README b/extension/app/README
new file mode 100644 (file)
index 0000000..4a326ca
--- /dev/null
@@ -0,0 +1,5 @@
+
+See "Load unpacked extension" from:
+
+https://developer.chrome.com/extensions/getstarted
+
diff --git a/extension/app/main.html b/extension/app/main.html
new file mode 100644 (file)
index 0000000..8472010
--- /dev/null
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<!-- This file will eventually go away. -->
+<html>
+  <head>
+    <script src='./main.js'></script>
+  </head>
+  <body>
+    <h1>Hatch Native Messaging Host Extension</h2>
+    <p>This page will not be visible under normal circumstances</p>
+  </body>
+</html>
diff --git a/extension/app/main.js b/extension/app/main.js
new file mode 100644 (file)
index 0000000..f13ed35
--- /dev/null
@@ -0,0 +1,85 @@
+/* -----------------------------------------------------------------------     
+ * Copyright 2016 King County Library System                                   
+ * Bill Erickson <berickxx@gmail.com>                                          
+ *                                                                             
+ * This program is free software; you can redistribute it and/or               
+ * modify it under the terms of the GNU General Public License                 
+ * as published by the Free Software Foundation; either version 2              
+ * of the License, or (at your option) any later version.                      
+ *                                                                             
+ * This program is distributed in the hope that it will be useful,             
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of              
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               
+ * GNU General Public License for more details.                                
+ * -----------------------------------------------------------------------     
+ */ 
+
+// Singleton connection to Hatch
+var hatchPort = null;
+
+// Map of tab identifers to tab-specific connection ports.
+var browserPorts = {};
+
+
+/**
+ * Handle response messages received from Hatch.
+ */
+function onNativeMessage(message) {
+    var tabId = message.clientid;
+
+    if (tabId) {
+        if (browserPorts[tabId]) {
+            browserPorts[tabId].postMessage(JSON.stringify(message));
+
+        } else {
+            console.warn(
+                "Hatch message contains port ID " + tabId +
+                " which was not found in the browser tab map. " +
+                "Unable to deliver response to browser");
+        }
+
+    } else {
+        console.warn("Hatch response does not contain a 'clientid' value. " +
+            "Unable to deliver response to browser");
+    }
+}
+
+/**
+ * Called when the connection to Hatch goes away.
+ */
+function onDisconnected() {
+  console.warn("Failed to connect: " + chrome.runtime.lastError.message);
+  hatchPort = null;
+  browserPorts = {};
+}
+
+/**
+ * Called when a browser tab opens a connection to this extension.
+ */
+chrome.runtime.onConnectExternal.addListener(function(port) {
+    var tabId = port.sender.tab.id;
+
+    browserPorts[tabId] = port;
+    console.debug('new port connected with id ' + tabId);
+
+    port.onMessage.addListener(function(msg) {
+        console.log("Received message from browser on port " + tabId);
+        hatchPort.postMessage(msg);
+    });
+
+    port.onDisconnect.addListener(function() {
+        console.log("Removing port " + tabId + " on tab disconnect");
+        delete browserPorts[tabId];
+    });
+});
+    
+
+/**
+ * Connect to Hatch on startup.
+ */
+var hostName = "org.evergreen_ils.hatch";
+console.debug("Connecting to native messaging host: " + hostName);
+hatchPort = chrome.runtime.connectNative(hostName);
+hatchPort.onMessage.addListener(onNativeMessage);
+hatchPort.onDisconnect.addListener(onDisconnected);
+
diff --git a/extension/app/manifest.json b/extension/app/manifest.json
new file mode 100644 (file)
index 0000000..d0f0d17
--- /dev/null
@@ -0,0 +1,19 @@
+{
+  // Extension ID: knldjmfmopnpolahpmmgbagdohdnhkik
+  "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
+  "name": "Hatch Native Messaging Extension",
+  "version": "1.0",
+  "manifest_version": 2,
+  "description": "Relays messages to/from Hatch.",
+  "app": {
+    "launch": {
+      "local_path": "main.html"
+    }
+  },
+  "permissions": [
+    "nativeMessaging"
+  ],
+  "externally_connectable": {
+    "matches": ["*://eg-dev-local/*"]
+  }
+}
diff --git a/extension/host/README b/extension/host/README
new file mode 100644 (file)
index 0000000..fea5b31
--- /dev/null
@@ -0,0 +1,13 @@
+https://developer.chrome.com/extensions/nativeMessaging
+
+1. Edit org.evergreen_ils.hatch.json "path" variable
+2. Copy org.evergreen_ils.hatch.json to:
+
+Windows:
+ TODO
+
+Linux: 
+
+ Google Chrome: ~/.config/google-chrome/NativeMessagingHosts/
+
+ Chromium: ~/.config/chromium/NativeMessagingHosts/
diff --git a/extension/host/org.evergreen_ils.hatch.json b/extension/host/org.evergreen_ils.hatch.json
new file mode 100644 (file)
index 0000000..49640a5
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "name": "org.evergreen_ils.hatch",
+  "description": "Hatch Native Messaging Host",
+  "path": "/home/berick/code/Hatch/start-hatch",
+  "type": "stdio",
+  "allowed_origins": [
+    "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/"
+  ]
+}
diff --git a/start-hatch b/start-hatch
new file mode 100755 (executable)
index 0000000..f0995ad
--- /dev/null
@@ -0,0 +1,3 @@
+#!/bin/bash
+
+./run.sh run