--- /dev/null
+/* -----------------------------------------------------------------------
+ * 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);
+
--- /dev/null
+{
+ // 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/*"]
+ }
+}