Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 |
(...skipping 25 matching lines...) Expand all Loading... | |
36 "tabs.query", | 36 "tabs.query", |
37 "tabs.reload", | 37 "tabs.reload", |
38 "tabs.sendMessage", | 38 "tabs.sendMessage", |
39 "tabs.update", | 39 "tabs.update", |
40 "webNavigation.getAllFrames", | 40 "webNavigation.getAllFrames", |
41 "webRequest.handlerBehaviorChanged", | 41 "webRequest.handlerBehaviorChanged", |
42 "windows.create", | 42 "windows.create", |
43 "windows.update" | 43 "windows.update" |
44 ]; | 44 ]; |
45 | 45 |
46 // Errors that occur only when we show an interest in the response from an | 46 // Since we add a callback for all messaging API calls in our wrappers, |
47 // API call. | 47 // Chrome assumes we're interested in the response; when there's no response, |
48 const noFulfillmentErrors = new Set([ | 48 // it sets runtime.lastError |
49 "The message port closed before a response was received.", | 49 const portClosedBeforeResponseError = |
50 // Older versions of Chrome have a typo: | 50 // Older versions of Chrome have a typo: |
51 // https://crrev.com/c33f51726eacdcc1a487b21a13611f7eab580d6d | 51 // https://crrev.com/c33f51726eacdcc1a487b21a13611f7eab580d6d |
52 "The message port closed before a reponse was received." | 52 /^The message port closed before a res?ponse was received\.$/; |
53 ]); | |
Sebastian Noack
2017/10/20 01:26:30
Perhaps, we can just use a regexp to account for t
Manish Jethani
2017/10/20 01:45:32
Yes, using a regex now.
The commit was on June 30
| |
54 | 53 |
55 function wrapAPI(api) | 54 function wrapAPI(api) |
56 { | 55 { |
57 let object = browser; | 56 let object = browser; |
58 let path = api.split("."); | 57 let path = api.split("."); |
59 let name = path.pop(); | 58 let name = path.pop(); |
60 | 59 |
61 for (let node of path) | 60 for (let node of path) |
62 { | 61 { |
63 object = object[node]; | 62 object = object[node]; |
64 | 63 |
65 if (!object) | 64 if (!object) |
66 return; | 65 return; |
67 } | 66 } |
68 | 67 |
69 let func = object[name]; | 68 let func = object[name]; |
70 object[name] = function(...args) | 69 object[name] = function(...args) |
71 { | 70 { |
71 let callStack = new Error().stack; | |
72 | |
72 if (typeof args[args.length - 1] == "function") | 73 if (typeof args[args.length - 1] == "function") |
73 return func.apply(object, args); | 74 return func.apply(object, args); |
74 | 75 |
75 // If the last argument is undefined, we drop it from the list assuming | 76 // If the last argument is undefined, we drop it from the list assuming |
76 // it stands for the optional callback. We must do this, because we have | 77 // it stands for the optional callback. We must do this, because we have |
77 // to replace it with our own callback. If we simply append our own | 78 // to replace it with our own callback. If we simply append our own |
78 // callback to the list, it won't match the signature of the function and | 79 // callback to the list, it won't match the signature of the function and |
79 // will cause an exception. | 80 // will cause an exception. |
80 if (typeof args[args.length - 1] == "undefined") | 81 if (typeof args[args.length - 1] == "undefined") |
81 args.pop(); | 82 args.pop(); |
82 | 83 |
83 return new Promise((resolve, reject) => | 84 return new Promise((resolve, reject) => |
84 { | 85 { |
85 func.call(object, ...args, result => | 86 func.call(object, ...args, result => |
86 { | 87 { |
87 let error = browser.runtime.lastError; | 88 let error = browser.runtime.lastError; |
88 if (error && !noFulfillmentErrors.has(error.message)) | 89 if (error && !portClosedBeforeResponseError.test(error.message)) |
90 { | |
91 // runtime.lastError is already an Error instance on Edge, while on | |
92 // Chrome it is a plain object with only a message property. | |
93 if (!(error instanceof Error)) | |
94 { | |
95 error = new Error(error.message); | |
96 | |
97 // Add a more helpful stack trace. | |
98 error.stack = callStack; | |
99 } | |
100 | |
89 reject(error); | 101 reject(error); |
102 } | |
90 else | 103 else |
104 { | |
91 resolve(result); | 105 resolve(result); |
106 } | |
92 }); | 107 }); |
93 }); | 108 }); |
94 }; | 109 }; |
95 } | 110 } |
96 | 111 |
97 function shouldWrapAPIs() | 112 function shouldWrapAPIs() |
98 { | 113 { |
99 try | 114 try |
100 { | 115 { |
101 return !(browser.storage.local.get([]) instanceof Promise); | 116 return !(browser.storage.local.get([]) instanceof Promise); |
(...skipping 19 matching lines...) Expand all Loading... | |
121 | 136 |
122 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList | 137 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList |
123 // didn't have iterator support before Chrome 51. | 138 // didn't have iterator support before Chrome 51. |
124 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 | 139 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 |
125 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) | 140 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) |
126 { | 141 { |
127 if (!(Symbol.iterator in object.prototype)) | 142 if (!(Symbol.iterator in object.prototype)) |
128 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; | 143 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; |
129 } | 144 } |
130 } | 145 } |
LEFT | RIGHT |