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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 { | 62 { |
63 object = object[node]; | 63 object = object[node]; |
64 | 64 |
65 if (!object) | 65 if (!object) |
66 return; | 66 return; |
67 } | 67 } |
68 | 68 |
69 let func = object[name]; | 69 let func = object[name]; |
70 if (!func) | 70 if (!func) |
71 return; | 71 return; |
72 let descriptor = Reflect.getOwnPropertyDescriptor(object, name); | 72 let descriptor = Object.getOwnPropertyDescriptor(object, name); |
Oleksandr
2017/10/27 13:12:37
We cannot use Object.getOwnPropertyDescriptor beca
Manish Jethani
2017/10/27 14:25:16
We can use Object.getOwnPropertyDescriptor here si
| |
73 delete descriptor["get"]; | |
74 delete descriptor["set"]; | |
75 descriptor.value = function(...args) | |
76 { | |
77 let callStack = new Error().stack; | |
73 | 78 |
74 if (descriptor && descriptor.configurable) | 79 if (typeof args[args.length - 1] == "function") |
Oleksandr
2017/10/27 13:12:38
There isn't really an object in our list that does
Manish Jethani
2017/10/27 14:25:16
Yes, we should remove it.
If it's not configurabl
| |
75 { | 80 return func.apply(object, args); |
76 Object.defineProperty(object, name, { | 81 |
Manish Jethani
2017/10/27 14:25:16
You could just do:
descriptor.value = (...args)
| |
77 enumerable: true, | 82 // If the last argument is undefined, we drop it from the list assuming |
78 configurable: true, | 83 // it stands for the optional callback. We must do this, because we have |
79 writable: true, | 84 // to replace it with our own callback. If we simply append our own |
80 value: (...args) => | 85 // callback to the list, it won't match the signature of the function and |
86 // will cause an exception. | |
87 if (typeof args[args.length - 1] == "undefined") | |
88 args.pop(); | |
89 | |
90 return new Promise((resolve, reject) => | |
91 { | |
92 func.call(object, ...args, result => | |
81 { | 93 { |
82 let callStack = new Error().stack; | 94 let error = browser.runtime.lastError; |
83 if (typeof args[args.length - 1] == "function") | 95 if (error && !portClosedBeforeResponseError.test(error.message)) |
84 return func.apply(object, args); | 96 { |
97 // runtime.lastError is already an Error instance on Edge, while on | |
98 // Chrome it is a plain object with only a message property. | |
99 if (!(error instanceof Error)) | |
100 { | |
101 error = new Error(error.message); | |
85 | 102 |
86 // If the last argument is undefined, we drop it from the list | 103 // Add a more helpful stack trace. |
87 // assuming it stands for the optional callback. We must do this, | 104 error.stack = callStack; |
88 // because we have to replace it with our own callback. If we simply | 105 } |
89 // append our own callback to the list, it won't match the signature | |
90 // of the function and will cause an exception. | |
91 if (typeof args[args.length - 1] == "undefined") | |
92 args.pop(); | |
93 | 106 |
94 return new Promise((resolve, reject) => | 107 reject(error); |
108 } | |
109 else | |
95 { | 110 { |
96 func.call(object, ...args, result => | 111 resolve(result); |
97 { | 112 } |
98 let error = browser.runtime.lastError; | 113 }); |
99 if (error && !portClosedBeforeResponseError.test(error.message)) | 114 }); |
100 { | 115 }; |
101 // runtime.lastError is already an Error instance on Edge, while | 116 Object.defineProperty(object, name, descriptor); |
102 // on Chrome it is a plain object with only a message property. | |
103 if (!(error instanceof Error)) | |
104 { | |
105 error = new Error(error.message); | |
106 | |
107 // Add a more helpful stack trace. | |
108 error.stack = callStack; | |
109 } | |
110 | |
111 reject(error); | |
112 } | |
113 else | |
114 { | |
115 resolve(result); | |
116 } | |
117 }); | |
118 }); | |
119 }}); | |
120 } | |
121 } | 117 } |
122 | 118 |
123 function shouldWrapAPIs() | 119 function shouldWrapAPIs() |
124 { | 120 { |
125 try | 121 try |
126 { | 122 { |
127 return !(browser.storage.local.get([]) instanceof Promise); | 123 return !(browser.storage.local.get([]) instanceof Promise); |
128 } | 124 } |
129 catch (error) | 125 catch (error) |
130 { | 126 { |
131 } | 127 } |
132 | 128 |
133 return true; | 129 return true; |
134 } | 130 } |
135 | 131 |
136 if (shouldWrapAPIs()) | 132 if (shouldWrapAPIs()) |
137 { | 133 { |
138 // Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser" object , | 134 // Unlike Firefox and Microsoft Edge, Chrome doesn't have a "browser" |
139 // but provides the extension API through the "chrome" namespace | 135 // object, but provides the extension API through the "chrome" namespace |
140 // (non-standard). | 136 // (non-standard). |
141 if (typeof browser == "undefined") | 137 if (typeof browser == "undefined") |
142 window.browser = chrome; | 138 window.browser = chrome; |
143 | 139 |
144 for (let api of asyncAPIs) | 140 for (let api of asyncAPIs) |
145 wrapAPI(api); | 141 wrapAPI(api); |
146 } | 142 } |
147 | 143 |
148 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList | 144 // Workaround since HTMLCollection, NodeList, StyleSheetList, and CSSRuleList |
149 // didn't have iterator support before Chrome 51. | 145 // didn't have iterator support before Chrome 51. |
150 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 | 146 // https://bugs.chromium.org/p/chromium/issues/detail?id=401699 |
151 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) | 147 for (let object of [HTMLCollection, NodeList, StyleSheetList, CSSRuleList]) |
152 { | 148 { |
153 if (!(Symbol.iterator in object.prototype)) | 149 if (!(Symbol.iterator in object.prototype)) |
154 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; | 150 object.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator]; |
155 } | 151 } |
156 } | 152 } |
LEFT | RIGHT |