From 075a5ad0e0f9b4f9af614194d9c8d21d0ed45184 Mon Sep 17 00:00:00 2001 From: hackademix Date: Wed, 22 Aug 2018 16:34:16 +0200 Subject: More coherent wrapper around the webex messaging API. --- src/bg/RequestGuard.js | 16 +++++++--------- src/bg/main.js | 9 ++------- src/content/PlaceHolder.js | 3 +-- src/content/content.js | 15 +++------------ src/lib/Messages.js | 15 ++++++++++++--- src/manifest.json | 1 + src/ui/popup.js | 4 +--- src/ui/ui.js | 18 ++++++++---------- 8 files changed, 35 insertions(+), 46 deletions(-) diff --git a/src/bg/RequestGuard.js b/src/bg/RequestGuard.js index ccf3c70..e6be19e 100644 --- a/src/bg/RequestGuard.js +++ b/src/bg/RequestGuard.js @@ -263,7 +263,7 @@ var RequestGuard = (() => { return true; }, - async docStatus(message, sender) { + async queryDocStatus(message, sender) { let {frameId, tab} = sender; let {url} = message; let tabId = tab.id; @@ -291,10 +291,9 @@ var RequestGuard = (() => { if (pending) request.initialUrl = pending.initialUrl; if (type !== "sub_frame") { // we couldn't deliver it to frameId, since it's generally not loaded yet try { - await browser.tabs.sendMessage( - tabId, - {type: "seen", request, allowed, policyType, ownFrame: true}, - {frameId} + await Messages.send("seen", + {request, allowed, policyType, ownFrame: true}, + {tabId, frameId} ); } catch (e) { debug(`Couldn't deliver "seen" message for ${type}@${url} ${allowed ? "A" : "F" } to document ${documentUrl} (${frameId}/${tabId})`, e); @@ -302,10 +301,9 @@ var RequestGuard = (() => { } if (frameId === 0) return; try { - await browser.tabs.sendMessage( - tabId, - {type: "seen", request, allowed, policyType}, - {frameId: 0} + await Message.send("seen", + {request, allowed, policyType}, + {tabId, frameId: 0} ); } catch (e) { debug(`Couldn't deliver "seen" message to top frame containing ${documentUrl} (${frameId}/${tabId}`, e); diff --git a/src/bg/main.js b/src/bg/main.js index 3882191..47d28a3 100644 --- a/src/bg/main.js +++ b/src/bg/main.js @@ -118,8 +118,7 @@ let policy = ns.policy.dry(true); let seen = tabId !== -1 ? await ns.collectSeen(tabId) : null; let xssUserChoices = await XSS.getUserChoices(); - browser.runtime.sendMessage({ - type: "settings", + await Messages.send("settings", { policy, seen, xssUserChoices, @@ -228,11 +227,7 @@ async collectSeen(tabId) { try { - let seen = Array.from(await browser.tabs.sendMessage(tabId, { - type: "collect" - }, { - frameId: 0 - })); + let seen = Array.from(await Messages.send("collect", {}, {tabId, frameId: 0})); debug("Collected seen", seen); return seen; } catch (e) { diff --git a/src/content/PlaceHolder.js b/src/content/PlaceHolder.js index 764b7bc..ec2ab5b 100644 --- a/src/content/PlaceHolder.js +++ b/src/content/PlaceHolder.js @@ -116,8 +116,7 @@ var PlaceHolder = (() => { async enable(replacement) { debug("Enabling %o", this.request, this.policyType); - let ok = await browser.runtime.sendMessage({ - action: "enable", + let ok = await Messages.send("enable", { url: this.request.url, policyType: this.policyType, documentUrl: document.URL diff --git a/src/content/content.js b/src/content/content.js index d5adc94..22dbf1e 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -114,8 +114,7 @@ var seen = { } } -var handlers = { - +Messages.addHandler({ seen(event) { let {allowed, policyType, request, ownFrame} = event; if (window.top === window) { @@ -129,19 +128,11 @@ var handlers = { } } }, - collect(event) { let list = seen.list; debug("COLLECT", list); return list; } -}; - -browser.runtime.onMessage.addListener(async event => { - if (event.type in handlers) { - debug("Received message", event); - return handlers[event.type](event); - } }); if (document.readyState !== "complete") { @@ -157,7 +148,7 @@ let notifyPage = async () => { debug("Page %s shown, %s", document.URL, document.readyState); if (document.readyState === "complete") { try { - await browser.runtime.sendMessage({action: "pageshow", seen: seen.list, canScript}); + await Messages.send("pageshow", {seen: seen.list, canScript}); return true; } catch (e) { debug(e); @@ -184,7 +175,7 @@ async function init(oldPage = false) { document.URL, document.contentType, document.readyState, window.frameElement && frameElement.data); try { - ({canScript, shouldScript} = await browser.runtime.sendMessage({action: "docStatus", url: document.URL})); + ({canScript, shouldScript} = await Messages.send("queryDocStatus", {url: document.URL})); debug(`document %s, canScript=%s, shouldScript=%s, readyState %s`, document.URL, canScript, shouldScript, document.readyState); if (canScript) { if (oldPage) { diff --git a/src/lib/Messages.js b/src/lib/Messages.js index c87a8a8..fc7df6a 100644 --- a/src/lib/Messages.js +++ b/src/lib/Messages.js @@ -3,9 +3,9 @@ let handlers = new Set(); let dispatch = async (msg, sender) => { - let {action} = msg; + let {_messageName} = msg; for (let h of handlers) { - let f = h[action]; + let f = h[_messageName]; if (typeof f === "function") { return await f(msg, sender); } @@ -24,8 +24,17 @@ let originalSize = handlers.size; handlers.delete(handler); if (originalSize === 1 && handlers.size === 0) { - browser.runtime.onMessage.remveListener(dispatch); + browser.runtime.onMessage.removeListener(dispatch); } + }, + async send(name, args = {}, toContent = null) { + args._messageName = name; + if (toContent && "tabId" in toContent) { + let opts; + if ("frameId" in toContent) opts = {frameId: toContent.frameId}; + return await browser.tabs.sendMessage(toContent.tabId, args, opts); + } + return await browser.runtime.sendMessage(args); } } } diff --git a/src/manifest.json b/src/manifest.json index d28027d..4f42461 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -62,6 +62,7 @@ "all_frames": true, "js": [ "lib/log.js", + "lib/Messages.js", "content/onScriptDisabled.js", "content/content.js", "content/webglHook.js", diff --git a/src/ui/popup.js b/src/ui/popup.js index 55eff03..bc054d1 100644 --- a/src/ui/popup.js +++ b/src/ui/popup.js @@ -4,9 +4,7 @@ var sitesUI; addEventListener("unload", e => { if (!UI.initialized) { - browser.runtime.sendMessage({ - type: "openStandalonePopup" - }); + Messages.send("openStandalonePopup"); } }); diff --git a/src/ui/ui.js b/src/ui/ui.js index be9063b..8cf20ce 100644 --- a/src/ui/ui.js +++ b/src/ui/ui.js @@ -16,6 +16,7 @@ var UI = (() => { UI.tabId = tabId; let scripts = [ "/ui/ui.css", + "/lib/Messages.js", "/lib/punycode.js", "/lib/tld.js", "/common/Policy.js", @@ -27,11 +28,9 @@ var UI = (() => { } await include(scripts); - - let inited = new Promise(resolve => { - let listener = async m => { - if (m.type === "settings") { + Messages.addHandler({ + async settings(m) { UI.policy = new Policy(m.policy); UI.snapshot = UI.policy.snapshot; UI.seen = m.seen; @@ -46,8 +45,7 @@ var UI = (() => { if (UI.onSettings) UI.onSettings(); await HighContrast.init(); } - }; - browser.runtime.onMessage.addListener(listener); + }); if (this.mobile) FastClick.attach(document.body); UI.pullSettings(); @@ -59,11 +57,11 @@ var UI = (() => { debug("Imported", Policy); }, async pullSettings() { - browser.runtime.sendMessage({action: "broadcastSettings", tabId: UI.tabId}); + Messages.send("broadcastSettings", {tabId: UI.tabId}); }, async updateSettings({policy, xssUserChoices, unrestrictedTab, local, sync, reloadAffected}) { if (policy) policy = policy.dry(true); - return await browser.runtime.sendMessage({action: "updateSettings", + return await Messages.send("updateSettings", { policy, xssUserChoices, unrestrictedTab, @@ -75,10 +73,10 @@ var UI = (() => { }, async exportSettings() { - return await browser.runtime.sendMessage({action: "exportSettings"}); + return await Messages.send("exportSettings"); }, async importSettings(data) { - return await browser.runtime.sendMessage({action: "importSettings", data}); + return await Messages.send("importSettings", {data}); }, async revokeTemp() { -- cgit v1.2.3