More coherent wrapper around the webex messaging API.

This commit is contained in:
hackademix 2018-08-22 16:34:16 +02:00
parent 48c04726b8
commit 075a5ad0e0
8 changed files with 35 additions and 46 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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

View File

@ -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) {

View File

@ -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);
}
}
}

View File

@ -62,6 +62,7 @@
"all_frames": true,
"js": [
"lib/log.js",
"lib/Messages.js",
"content/onScriptDisabled.js",
"content/content.js",
"content/webglHook.js",

View File

@ -4,9 +4,7 @@ var sitesUI;
addEventListener("unload", e => {
if (!UI.initialized) {
browser.runtime.sendMessage({
type: "openStandalonePopup"
});
Messages.send("openStandalonePopup");
}
});

View File

@ -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() {