From: Bill Erickson Date: Thu, 10 Nov 2016 21:05:16 +0000 (-0500) Subject: Native Messaging WIP - browser extension bits X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=de2993190d398df4f216ec6f99701db7fb8b9abc;p=working%2FHatch.git Native Messaging WIP - browser extension bits Signed-off-by: Bill Erickson --- diff --git a/extension/app/README b/extension/app/README new file mode 100644 index 0000000000..4a326caaa8 --- /dev/null +++ b/extension/app/README @@ -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 index 0000000000..8472010ed6 --- /dev/null +++ b/extension/app/main.html @@ -0,0 +1,11 @@ + + + + + + + +

Hatch Native Messaging Host Extension

+

This page will not be visible under normal circumstances

+ + diff --git a/extension/app/main.js b/extension/app/main.js new file mode 100644 index 0000000000..f13ed351a7 --- /dev/null +++ b/extension/app/main.js @@ -0,0 +1,85 @@ +/* ----------------------------------------------------------------------- + * Copyright 2016 King County Library System + * Bill Erickson + * + * 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 index 0000000000..d0f0d17065 --- /dev/null +++ b/extension/app/manifest.json @@ -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 index 0000000000..fea5b3162f --- /dev/null +++ b/extension/host/README @@ -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 index 0000000000..49640a5055 --- /dev/null +++ b/extension/host/org.evergreen_ils.hatch.json @@ -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 index 0000000000..f0995ad292 --- /dev/null +++ b/start-hatch @@ -0,0 +1,3 @@ +#!/bin/bash + +./run.sh run