Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: ext/common.js

Issue 29573892: Issue 4579 - Promisify APIs (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Do not wrap APIs on Firefox Created Oct. 11, 2017, 9:28 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ext/common.js
===================================================================
--- a/ext/common.js
+++ b/ext/common.js
@@ -20,16 +20,89 @@
(function()
{
// Both Edge and Mozilla Web Extensions use the namespace
// 'browser' instead of 'chrome'. Edge has chrome namespace defined,
// in some cases, but only with one property: 'app'.
if (typeof chrome == "undefined" || typeof chrome.extension == "undefined")
window.chrome = window.browser;
+ const asyncAPIs = [
+ "contextMenus.removeAll",
+ "devtools.panels.create",
+ "notifications.clear",
+ "notifications.create",
+ "runtime.openOptionsPage",
+ "runtime.sendMessage",
+ "runtime.setUninstallURL",
+ "storage.local.get",
+ "storage.local.remove",
+ "storage.local.set",
+ "storage.managed.get",
+ "tabs.create",
+ "tabs.get",
+ "tabs.insertCSS",
+ "tabs.query",
+ "tabs.reload",
+ "tabs.sendMessage",
+ "tabs.update",
+ "webNavigation.getAllFrames",
+ "webRequest.handlerBehaviorChanged",
+ "windows.create",
+ "windows.update"
+ ];
+
+ function wrapAPI(api, object)
+ {
+ if (!object)
+ return;
+
+ let [root, ...subPath] = api.split(".");
Sebastian Noack 2017/10/13 04:04:19 Would this logic here potentially somewhat more li
Manish Jethani 2017/10/13 07:17:46 It would be more efficient if it were were an arra
+
+ if (subPath.length > 0)
+ {
+ wrapAPI(subPath.join("."), object[root]);
+ return;
+ }
+
+ let func = object[root];
+ object[root] = function(...args)
+ {
+ if (typeof args[args.length - 1] == "function")
+ return func.apply(object, args);
+
+ // If the last argument is undefined, we drop it from the list assuming
+ // it stands for the optional callback. We must do this, because we have
+ // to replace it with our own callback. If we simply append our own
+ // callback to the list, it won't match the signature of the function and
+ // will cause an exception.
+ if (typeof args[args.length - 1] == "undefined")
+ args.pop();
+
+ return new Promise((resolve, reject) =>
+ {
+ func.call(object, ...args, result =>
+ {
+ let error = chrome.runtime.lastError;
+ if (error)
+ reject(error);
+ else
+ resolve(result);
+ });
+ });
+ };
+ }
+
+ if (typeof browser == "undefined" ||
Manish Jethani 2017/10/11 21:33:55 I was going for runtime.getPlatformInfo, but it's
+ !(browser.storage.local.get([]) instanceof Promise))
Sebastian Noack 2017/10/13 04:04:19 Wouldn't you get an error here on Chrome, because
Manish Jethani 2017/10/13 07:17:46 It would be Edge on which the error might be throw
+ {
+ for (let api of asyncAPIs)
+ wrapAPI(api, chrome);
+ }
+
window.ext = {};
let EventTarget = ext._EventTarget = function()
{
this._listeners = new Set();
};
EventTarget.prototype = {
addListener(listener)
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld