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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 (function() 20 (function()
21 { 21 {
22 // Both Edge and Mozilla Web Extensions use the namespace 22 // Both Edge and Mozilla Web Extensions use the namespace
23 // 'browser' instead of 'chrome'. Edge has chrome namespace defined, 23 // 'browser' instead of 'chrome'. Edge has chrome namespace defined,
24 // in some cases, but only with one property: 'app'. 24 // in some cases, but only with one property: 'app'.
25 if (typeof chrome == "undefined" || typeof chrome.extension == "undefined") 25 if (typeof chrome == "undefined" || typeof chrome.extension == "undefined")
26 window.chrome = window.browser; 26 window.chrome = window.browser;
27 27
28 const asyncAPIs = [
29 "contextMenus.removeAll",
30 "devtools.panels.create",
31 "notifications.clear",
32 "notifications.create",
33 "runtime.openOptionsPage",
34 "runtime.sendMessage",
35 "runtime.setUninstallURL",
36 "storage.local.get",
37 "storage.local.remove",
38 "storage.local.set",
39 "storage.managed.get",
40 "tabs.create",
41 "tabs.get",
42 "tabs.insertCSS",
43 "tabs.query",
44 "tabs.reload",
45 "tabs.sendMessage",
46 "tabs.update",
47 "webNavigation.getAllFrames",
48 "webRequest.handlerBehaviorChanged",
49 "windows.create",
50 "windows.update"
51 ];
52
53 function wrapAPI(api, object)
54 {
55 if (!object)
56 return;
57
58 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
59
60 if (subPath.length > 0)
61 {
62 wrapAPI(subPath.join("."), object[root]);
63 return;
64 }
65
66 let func = object[root];
67 object[root] = function(...args)
68 {
69 if (typeof args[args.length - 1] == "function")
70 return func.apply(object, args);
71
72 // If the last argument is undefined, we drop it from the list assuming
73 // it stands for the optional callback. We must do this, because we have
74 // to replace it with our own callback. If we simply append our own
75 // callback to the list, it won't match the signature of the function and
76 // will cause an exception.
77 if (typeof args[args.length - 1] == "undefined")
78 args.pop();
79
80 return new Promise((resolve, reject) =>
81 {
82 func.call(object, ...args, result =>
83 {
84 let error = chrome.runtime.lastError;
85 if (error)
86 reject(error);
87 else
88 resolve(result);
89 });
90 });
91 };
92 }
93
94 if (typeof browser == "undefined" ||
Manish Jethani 2017/10/11 21:33:55 I was going for runtime.getPlatformInfo, but it's
95 !(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
96 {
97 for (let api of asyncAPIs)
98 wrapAPI(api, chrome);
99 }
100
28 window.ext = {}; 101 window.ext = {};
29 102
30 let EventTarget = ext._EventTarget = function() 103 let EventTarget = ext._EventTarget = function()
31 { 104 {
32 this._listeners = new Set(); 105 this._listeners = new Set();
33 }; 106 };
34 EventTarget.prototype = { 107 EventTarget.prototype = {
35 addListener(listener) 108 addListener(listener)
36 { 109 {
37 this._listeners.add(listener); 110 this._listeners.add(listener);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 { 150 {
78 return chrome.extension.getBackgroundPage(); 151 return chrome.extension.getBackgroundPage();
79 } 152 }
80 }; 153 };
81 154
82 155
83 /* Utils */ 156 /* Utils */
84 157
85 ext.getURL = chrome.extension.getURL; 158 ext.getURL = chrome.extension.getURL;
86 }()); 159 }());
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld