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

Side by Side Diff: polyfill.js

Issue 29573892: Issue 4579 - Promisify APIs (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Rebase Created Oct. 17, 2017, 1:30 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 // Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser" object, 20 (function()
Manish Jethani 2017/10/17 13:33:24 It's best to move this inside, after the call to s
Manish Jethani 2017/10/17 13:33:24 This all needs to be wrapped inside an anonymous f
Manish Jethani 2017/10/17 17:35:45 Actually this can just be an anonymous block. Don
kzar 2017/10/17 18:13:42 IIRC Firefox got upset when declaring functions in
Manish Jethani 2017/10/17 20:48:45 You're right, the function declarations would be "
Sebastian Noack 2017/10/17 23:31:03 Both of you are wrong. In strict mode, as of ES201
Manish Jethani 2017/10/18 00:05:33 Ah, of course. Reverted.
21 // but provides the extension API through the "chrome" namespace 21 {
22 // (non-standard). 22 const asyncAPIs = [
23 if (typeof browser == "undefined") 23 "contextMenus.removeAll",
24 window.browser = chrome; 24 "devtools.panels.create",
25 "notifications.clear",
26 "notifications.create",
27 "runtime.openOptionsPage",
28 "runtime.sendMessage",
29 "runtime.setUninstallURL",
30 "storage.local.get",
31 "storage.local.remove",
32 "storage.local.set",
33 "storage.managed.get",
34 "tabs.create",
35 "tabs.get",
36 "tabs.insertCSS",
37 "tabs.query",
38 "tabs.reload",
39 "tabs.sendMessage",
40 "tabs.update",
41 "webNavigation.getAllFrames",
42 "webRequest.handlerBehaviorChanged",
43 "windows.create",
44 "windows.update"
45 ];
25 46
26 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList 47 function wrapAPI(api)
27 // didn't have iterator support before Chrome 51. 48 {
28 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 49 let object = browser;
Manish Jethani 2017/10/17 13:33:23 Note: Using browser now instead of chrome
29 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) 50 let path = api.split(".");
30 { 51 let name = path.pop();
31 if (!(Symbol.iterator in object.prototype)) 52
32 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; 53 for (let node of path)
33 } 54 {
55 object = object[node];
56
57 if (!object)
58 return;
59 }
60
61 let func = object[name];
62 object[name] = function(...args)
63 {
64 if (typeof args[args.length - 1] == "function")
65 return func.apply(object, args);
66
67 // If the last argument is undefined, we drop it from the list assuming
68 // it stands for the optional callback. We must do this, because we have
69 // to replace it with our own callback. If we simply append our own
70 // callback to the list, it won't match the signature of the function and
71 // will cause an exception.
72 if (typeof args[args.length - 1] == "undefined")
73 args.pop();
74
75 return new Promise((resolve, reject) =>
76 {
77 func.call(object, ...args, result =>
78 {
79 let error = browser.runtime.lastError;
80 if (error)
81 reject(error);
82 else
83 resolve(result);
84 });
85 });
86 };
87 }
88
89 function shouldWrapAPIs()
90 {
91 try
92 {
93 return !(browser.storage.local.get([]) instanceof Promise);
94 }
95 catch (error)
96 {
97 }
98
99 return true;
100 }
101
102 if (shouldWrapAPIs())
103 {
104 // Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser" object ,
Manish Jethani 2017/10/17 13:33:23 After calling shouldWrapAPIs we can safely overwri
105 // but provides the extension API through the "chrome" namespace
106 // (non-standard).
107 if (typeof browser == "undefined")
108 window.browser = chrome;
109
110 for (let api of asyncAPIs)
111 wrapAPI(api);
112 }
113
114 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList
115 // didn't have iterator support before Chrome 51.
116 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699
117 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList])
118 {
119 if (!(Symbol.iterator in object.prototype))
120 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
121 }
122 }());
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