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, |
Manish Jethani
2017/10/19 10:13:23
We should keep this array because there are more s
kzar
2017/10/19 10:17:49
Maybe a Set would be a better data structure to us
Manish Jethani
2017/10/19 10:24:46
Regarding the error message itself, I should go ba
Manish Jethani
2017/10/19 10:30:32
Good point, done.
Sebastian Noack
2017/10/19 18:30:18
We can easily turn it into an array, set or regexp
kzar
2017/10/19 18:37:39
Yea I'm pretty sure we all agree there, if we can
Sebastian Noack
2017/10/19 22:08:31
Ollie just confirmed, that on Microsoft Edge no er
| |
47 // API call. | 47 // Chrome assumes we're interested in the response; when there's no response, |
48 const noFulfillmentErrors = [ | 48 // it sets runtime.lastError |
49 "The message port closed before a response was received." | 49 const portClosedBeforeResponseError = |
50 ]; | 50 // Older versions of Chrome have a typo: |
51 // https://crrev.com/c33f51726eacdcc1a487b21a13611f7eab580d6d | |
52 /^The message port closed before a res?ponse was received\.$/; | |
51 | 53 |
52 function wrapAPI(api) | 54 function wrapAPI(api) |
53 { | 55 { |
54 let object = browser; | 56 let object = browser; |
55 let path = api.split("."); | 57 let path = api.split("."); |
56 let name = path.pop(); | 58 let name = path.pop(); |
57 | 59 |
58 for (let node of path) | 60 for (let node of path) |
59 { | 61 { |
60 object = object[node]; | 62 object = object[node]; |
61 | 63 |
62 if (!object) | 64 if (!object) |
63 return; | 65 return; |
64 } | 66 } |
65 | 67 |
66 let func = object[name]; | 68 let func = object[name]; |
67 object[name] = function(...args) | 69 object[name] = function(...args) |
68 { | 70 { |
71 let callStack = new Error().stack; | |
72 | |
69 if (typeof args[args.length - 1] == "function") | 73 if (typeof args[args.length - 1] == "function") |
70 return func.apply(object, args); | 74 return func.apply(object, args); |
71 | 75 |
72 // 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 |
73 // 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 |
74 // 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 |
75 // 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 |
76 // will cause an exception. | 80 // will cause an exception. |
77 if (typeof args[args.length - 1] == "undefined") | 81 if (typeof args[args.length - 1] == "undefined") |
78 args.pop(); | 82 args.pop(); |
79 | 83 |
80 return new Promise((resolve, reject) => | 84 return new Promise((resolve, reject) => |
81 { | 85 { |
82 func.call(object, ...args, result => | 86 func.call(object, ...args, result => |
83 { | 87 { |
84 let error = browser.runtime.lastError; | 88 let error = browser.runtime.lastError; |
85 if (error && !noFulfillmentErrors.includes(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 | |
86 reject(error); | 101 reject(error); |
102 } | |
87 else | 103 else |
104 { | |
88 resolve(result); | 105 resolve(result); |
106 } | |
89 }); | 107 }); |
90 }); | 108 }); |
91 }; | 109 }; |
92 } | 110 } |
93 | 111 |
94 function shouldWrapAPIs() | 112 function shouldWrapAPIs() |
95 { | 113 { |
96 try | 114 try |
97 { | 115 { |
98 return !(browser.storage.local.get([]) instanceof Promise); | 116 return !(browser.storage.local.get([]) instanceof Promise); |
(...skipping 19 matching lines...) Expand all Loading... | |
118 | 136 |
119 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList | 137 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList |
120 // didn't have iterator support before Chrome 51. | 138 // didn't have iterator support before Chrome 51. |
121 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 | 139 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 |
122 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) | 140 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) |
123 { | 141 { |
124 if (!(Symbol.iterator in object.prototype)) | 142 if (!(Symbol.iterator in object.prototype)) |
125 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; | 143 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; |
126 } | 144 } |
127 } | 145 } |
LEFT | RIGHT |