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

Delta Between Two Patch Sets: polyfill.js

Issue 29573892: Issue 4579 - Promisify APIs (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Left Patch Set: Do not return promise if last argument is undefined Created Oct. 14, 2017, 1:04 a.m.
Right Patch Set: Revert patch set 9 Created Oct. 18, 2017, 12:04 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
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 // Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser" object, 20 {
21 // but provides the extension API through the "chrome" namespace 21 const asyncAPIs = [
22 // (non-standard). 22 "contextMenus.removeAll",
23 if (typeof browser == "undefined") 23 "devtools.panels.create",
24 window.browser = chrome; 24 "notifications.clear",
25 "notifications.create",
26 "runtime.openOptionsPage",
27 "runtime.sendMessage",
28 "runtime.setUninstallURL",
29 "storage.local.get",
30 "storage.local.remove",
31 "storage.local.set",
32 "storage.managed.get",
33 "tabs.create",
34 "tabs.get",
35 "tabs.insertCSS",
36 "tabs.query",
37 "tabs.reload",
38 "tabs.sendMessage",
39 "tabs.update",
40 "webNavigation.getAllFrames",
41 "webRequest.handlerBehaviorChanged",
42 "windows.create",
43 "windows.update"
44 ];
25 45
26 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList 46 function wrapAPI(api)
27 // didn't have iterator support before Chrome 51. 47 {
28 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 48 let object = browser;
29 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) 49 let path = api.split(".");
30 { 50 let name = path.pop();
31 if (!(Symbol.iterator in object.prototype)) 51
32 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; 52 for (let node of path)
53 {
54 object = object[node];
55
56 if (!object)
57 return;
58 }
59
60 let func = object[name];
61 object[name] = function(...args)
62 {
63 if (typeof args[args.length - 1] == "function")
64 return func.apply(object, args);
65
66 // If the last argument is undefined, we drop it from the list assuming
67 // it stands for the optional callback. We must do this, because we have
68 // to replace it with our own callback. If we simply append our own
69 // callback to the list, it won't match the signature of the function and
70 // will cause an exception.
71 if (typeof args[args.length - 1] == "undefined")
72 args.pop();
73
74 return new Promise((resolve, reject) =>
75 {
76 func.call(object, ...args, result =>
77 {
78 let error = browser.runtime.lastError;
79 if (error)
80 reject(error);
81 else
82 resolve(result);
83 });
84 });
85 };
86 }
87
88 function shouldWrapAPIs()
89 {
90 try
91 {
92 return !(browser.storage.local.get([]) instanceof Promise);
93 }
94 catch (error)
95 {
96 }
97
98 return true;
Wladimir Palant 2017/10/18 08:59:11 Nit: I would put that return inside the catch bloc
Manish Jethani 2017/10/18 11:00:26 Acknowledged.
99 }
100
101 if (shouldWrapAPIs())
102 {
103 // Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser" object ,
104 // but provides the extension API through the "chrome" namespace
105 // (non-standard).
106 if (typeof browser == "undefined")
107 window.browser = chrome;
108
109 for (let api of asyncAPIs)
110 wrapAPI(api);
111 }
112
113 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList
114 // didn't have iterator support before Chrome 51.
115 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699
116 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList])
117 {
118 if (!(Symbol.iterator in object.prototype))
119 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
120 }
33 } 121 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld