async'ify msg, don't throw for flow control (#1078)

This commit is contained in:
tophf 2020-10-26 17:39:07 +03:00 committed by GitHub
parent 1a7b51be6b
commit bf40fa81e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 151 additions and 251 deletions

View File

@ -258,7 +258,7 @@ if (FIREFOX && browser.commands && browser.commands.update) {
}); });
} }
msg.broadcastTab({method: 'backgroundReady'}); msg.broadcast({method: 'backgroundReady'});
function webNavIframeHelperFF({tabId, frameId}) { function webNavIframeHelperFF({tabId, frameId}) {
if (!frameId) return; if (!frameId) return;

View File

@ -155,13 +155,10 @@ self.INJECTED !== 1 && (() => {
break; break;
case 'backgroundReady': case 'backgroundReady':
initializing initializing.catch(err =>
.catch(err => { msg.isIgnorableError(err)
if (msg.RX_NO_RECEIVER.test(err.message)) { ? init()
return init(); : console.error(err));
}
})
.catch(console.error);
break; break;
case 'updateCount': case 'updateCount':

363
js/msg.js
View File

@ -1,258 +1,161 @@
/* global promisifyChrome deepCopy */ /* global promisifyChrome */
// deepCopy is only used if the script is executed in extension pages. /* global deepCopy getOwnTab URLS */ // not used in content scripts
'use strict'; 'use strict';
self.msg = self.INJECTED === 1 ? self.msg : (() => { // eslint-disable-next-line no-unused-expressions
window.INJECTED !== 1 && (() => {
promisifyChrome({ promisifyChrome({
runtime: ['sendMessage'], runtime: ['sendMessage', 'getBackgroundPage'],
tabs: ['sendMessage', 'query'], tabs: ['sendMessage', 'query'],
}); });
const isBg = chrome.extension.getBackgroundPage && chrome.extension.getBackgroundPage() === window; const TARGETS = Object.assign(Object.create(null), {
if (isBg) { all: ['both', 'tab', 'extension'],
window._msg = { extension: ['both', 'extension'],
handler: null, tab: ['both', 'tab'],
clone: deepCopy });
const NEEDS_TAB_IN_SENDER = [
'getTabUrlPrefix',
'updateIconBadge',
'styleViaAPI',
];
const ERR_NO_RECEIVER = 'Receiving end does not exist';
const ERR_PORT_CLOSED = 'The message port closed before';
const handler = {
both: new Set(),
tab: new Set(),
extension: new Set(),
}; };
let bg = chrome.extension.getBackgroundPage && chrome.extension.getBackgroundPage();
const isBg = bg === window;
if (!isBg && (!bg || !bg.document || bg.document.readyState === 'loading')) {
bg = null;
} }
const bgReady = getBg();
const EXTENSION_URL = chrome.runtime.getURL(''); // TODO: maybe move into polyfill.js and hook addListener + sendMessage so they wrap/unwrap automatically
let handler; const wrapData = data => ({
const RX_NO_RECEIVER = /Receiving end does not exist/; data,
// typo in Chrome 49 });
const RX_PORT_CLOSED = /The message port closed before a res?ponse was received/; const wrapError = error => ({
return { error: Object.assign({
send, message: error.message || `${error}`,
sendTab, stack: error.stack,
sendBg, }, error), // passing custom properties e.g. `error.index`
broadcast, });
broadcastTab, const unwrapResponse = ({data, error} = {error: {message: ERR_NO_RECEIVER}}) =>
broadcastExtension, error
ignoreError, ? Promise.reject(Object.assign(new Error(error.message), error))
on, : data;
onTab, chrome.runtime.onMessage.addListener(({data, target}, sender, sendResponse) => {
onExtension, const res = window.msg._execute(TARGETS[target] || TARGETS.all, data, sender);
off, if (res instanceof Promise) {
RX_NO_RECEIVER, res.then(wrapData, wrapError).then(sendResponse);
RX_PORT_CLOSED, return true;
}
if (res !== undefined) sendResponse(wrapData(res));
});
// This direct assignment allows IDEs to provide autocomplete for msg methods automatically
const msg = window.msg = {
isBg, isBg,
};
function getBg() { async broadcast(data) {
if (isBg) { const requests = [msg.send(data, 'both').catch(msg.ignoreError)];
return Promise.resolve(window); for (const tab of await browser.tabs.query({})) {
} const url = tab.pendingUrl || tab.url;
// try using extension.getBackgroundPage because runtime.getBackgroundPage is too slow if (!tab.discarded &&
// https://github.com/openstyles/stylus/issues/771 !url.startsWith(URLS.ownOrigin) &&
if (chrome.extension.getBackgroundPage) { URLS.supported(url)) {
const bg = chrome.extension.getBackgroundPage(); requests[tab.active ? 'unshift' : 'push'](
if (bg && bg.document && bg.document.readyState !== 'loading') { msg.sendTab(tab.id, data, null, 'both').catch(msg.ignoreError));
return Promise.resolve(bg);
}
}
if (chrome.runtime.getBackgroundPage) {
promisifyChrome({
runtime: ['getBackgroundPage'],
});
return browser.runtime.getBackgroundPage().catch(() => null);
}
return Promise.resolve(null);
}
function send(data, target = 'extension') {
const message = {data, target};
return browser.runtime.sendMessage(message).then(unwrapData);
}
function sendTab(tabId, data, options, target = 'tab') {
return browser.tabs.sendMessage(tabId, {data, target}, options)
.then(unwrapData);
}
function sendBg(data) {
return bgReady.then(bg => {
if (bg) {
if (!bg._msg.handler) {
throw new Error('there is no bg handler');
}
const handlers = bg._msg.handler.extension.concat(bg._msg.handler.both);
// in FF, the object would become a dead object when the window
// is closed, so we have to clone the object into background.
return Promise.resolve(executeCallbacks(handlers, bg._msg.clone(data), {url: location.href}))
.then(deepCopy);
}
return send(data);
});
}
function ignoreError(err) {
if (err.message && (
RX_NO_RECEIVER.test(err.message) ||
RX_PORT_CLOSED.test(err.message)
)) {
return;
}
console.warn(err);
}
function broadcast(data, filter) {
return Promise.all([
send(data, 'both').catch(ignoreError),
broadcastTab(data, filter, null, true, 'both')
]);
}
function broadcastTab(data, filter, options, ignoreExtension = false, target = 'tab') {
return browser.tabs.query({})
// TODO: send to activated tabs first?
.then(tabs => {
const requests = [];
for (const tab of tabs) {
const tabUrl = tab.pendingUrl || tab.url;
const isExtension = tabUrl.startsWith(EXTENSION_URL);
if (
tab.discarded ||
// FIXME: use `URLS.supported`?
!/^(http|ftp|file)/.test(tabUrl) &&
!tabUrl.startsWith('chrome://newtab/') &&
!isExtension ||
isExtension && ignoreExtension ||
filter && !filter(tab)
) {
continue;
}
const dataObj = typeof data === 'function' ? data(tab) : data;
if (!dataObj) {
continue;
}
const message = {data: dataObj, target};
if (tab && tab.id) {
requests.push(
browser.tabs.sendMessage(tab.id, message, options)
.then(unwrapData)
.catch(ignoreError)
);
} }
} }
return Promise.all(requests); return Promise.all(requests);
}); },
}
function broadcastExtension(...args) { broadcastExtension(...args) {
return send(...args).catch(ignoreError); return msg.send(...args).catch(msg.ignoreError);
} },
function on(fn) { isIgnorableError(err) {
initHandler(); const msg = `${err && err.message || err}`;
handler.both.push(fn); return msg.includes(ERR_NO_RECEIVER) || msg.includes(ERR_PORT_CLOSED);
} },
function onTab(fn) { ignoreError(err) {
initHandler(); if (!msg.isIgnorableError(err)) {
handler.tab.push(fn); console.warn(err);
} }
},
function onExtension(fn) { on(fn) {
initHandler(); handler.both.add(fn);
handler.extension.push(fn); },
}
function off(fn) { onTab(fn) {
for (const type of ['both', 'tab', 'extension']) { handler.tab.add(fn);
const index = handler[type].indexOf(fn); },
if (index >= 0) {
handler[type].splice(index, 1);
}
}
}
function initHandler() { onExtension(fn) {
if (handler) { handler.extension.add(fn);
return; },
}
handler = {
both: [],
tab: [],
extension: []
};
if (isBg) {
window._msg.handler = handler;
}
chrome.runtime.onMessage.addListener(handleMessage);
}
function executeCallbacks(callbacks, ...args) { off(fn) {
for (const type of TARGETS.all) {
handler[type].delete(fn);
}
},
send(data, target = 'extension') {
return browser.runtime.sendMessage({data, target})
.then(unwrapResponse);
},
sendTab(tabId, data, options, target = 'tab') {
return browser.tabs.sendMessage(tabId, {data, target}, options)
.then(unwrapResponse);
},
_execute(types, ...args) {
let result; let result;
for (const fn of callbacks) { for (const type of types) {
const data = withPromiseError(fn, ...args); for (const fn of handler[type]) {
if (data !== undefined && result === undefined) { let res;
result = data; try {
res = fn(...args);
} catch (err) {
res = Promise.reject(err);
}
if (res !== undefined && result === undefined) {
result = res;
}
} }
} }
return result; return result;
} },
};
function handleMessage(message, sender, sendResponse) { window.API = new Proxy({}, {
const handlers = message.target === 'tab' ? get(target, name) {
handler.tab.concat(handler.both) : message.target === 'extension' ? // using a named function for convenience when debugging
handler.extension.concat(handler.both) : return async function invokeAPI(...args) {
handler.both.concat(handler.extension, handler.tab); if (!bg && chrome.tabs) {
if (!handlers.length) { bg = await browser.runtime.getBackgroundPage().catch(() => {});
return;
} }
const result = executeCallbacks(handlers, message.data, sender); const message = {method: 'invokeAPI', name, args};
if (result === undefined) { // content scripts, frames and probably private tabs
return; if (!bg || window !== parent) {
return msg.send(message);
} }
Promise.resolve(result) // in FF, the object would become a dead object when the window
.then( // is closed, so we have to clone the object into background.
data => ({ const res = bg.msg._execute(TARGETS.extension, bg.deepCopy(message), {
error: false, frameId: 0,
data tab: NEEDS_TAB_IN_SENDER.includes(name) && await getOwnTab(),
}), url: location.href,
err => ({
error: true,
data: Object.assign({
message: err.message || String(err),
// FIXME: do we want to pass the entire stack?
stack: err.stack
}, err) // this allows us to pass custom properties e.g. `err.index`
})
)
.then(sendResponse);
return true;
}
function withPromiseError(fn, ...args) {
try {
return fn(...args);
} catch (err) {
return Promise.reject(err);
}
}
// {type, error, data, id}
function unwrapData(result) {
if (result === undefined) {
throw new Error('Receiving end does not exist');
}
if (result.error) {
throw Object.assign(new Error(result.data.message), result.data);
}
return result.data;
}
})();
self.API = self.INJECTED === 1 ? self.API : new Proxy({
// Handlers for these methods need sender.tab.id which is set by `send` as it uses messaging,
// unlike `sendBg` which invokes the background page directly in our own extension tabs
getTabUrlPrefix: true,
updateIconBadge: true,
styleViaAPI: true,
}, {
get: (target, name) =>
(...args) => Promise.resolve(self.msg[target[name] ? 'send' : 'sendBg']({
method: 'invokeAPI',
name,
args
}))
}); });
return deepCopy(await res);
};
},
});
})();