Left: | ||
Right: |
OLD | NEW |
---|---|
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 let fs = require("fs"); | 20 const fs = require("fs"); |
21 let path = require("path"); | 21 const path = require("path"); |
22 let SandboxedModule = require("sandboxed-module"); | 22 const SandboxedModule = require("sandboxed-module"); |
23 | 23 |
24 const Cr = exports.Cr = { | 24 const Cr = exports.Cr = { |
25 NS_OK: 0, | 25 NS_OK: 0, |
26 NS_BINDING_ABORTED: 0x804B0002, | 26 NS_BINDING_ABORTED: 0x804B0002, |
27 NS_ERROR_FAILURE: 0x80004005 | 27 NS_ERROR_FAILURE: 0x80004005 |
28 }; | 28 }; |
29 | 29 |
30 const MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; | 30 const MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; |
31 const MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; | 31 const MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; |
32 const MILLIS_IN_HOUR = exports.MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; | 32 const MILLIS_IN_HOUR = exports.MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; |
33 const MILLIS_IN_DAY = exports.MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; | 33 |
34 function URL(urlString) | |
35 { | |
36 return require("url").parse(urlString); | |
37 } | |
38 | |
39 let Services = { | |
40 obs: { | |
41 addObserver() {} | |
42 }, | |
43 vc: { | |
44 compare(v1, v2) | |
45 { | |
46 function comparePart(p1, p2) | |
47 { | |
48 if (p1 != "*" && p2 == "*") | |
49 return -1; | |
50 else if (p1 == "*" && p2 != "*") | |
51 return 1; | |
52 else if (p1 == p2) | |
53 return 0; | |
54 return parseInt(p1, 10) - parseInt(p2, 10); | |
55 } | |
56 | |
57 let parts1 = v1.split("."); | |
58 let parts2 = v2.split("."); | |
59 for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) | |
60 { | |
61 let result = comparePart(parts1[i] || "0", parts2[i] || "0"); | |
62 if (result != 0) | |
63 return result; | |
64 } | |
65 return 0; | |
66 } | |
67 } | |
68 }; | |
69 let XPCOMUtils = { | |
70 generateQI() {} | |
71 }; | |
72 let FileUtils = {}; | |
73 let resources = {Services, XPCOMUtils, FileUtils}; | |
34 | 74 |
35 let globals = { | 75 let globals = { |
36 atob: data => new Buffer(data, "base64").toString("binary"), | 76 atob: data => new Buffer(data, "base64").toString("binary"), |
37 btoa: data => new Buffer(data, "binary").toString("base64"), | 77 btoa: data => new Buffer(data, "binary").toString("base64"), |
38 Ci: { | 78 Ci: { |
39 }, | 79 }, |
40 Cu: { | 80 Cu: { |
41 import: () => {}, | 81 import(resource) |
42 reportError: e => undefined | 82 { |
83 let match = /^resource:\/\/gre\/modules\/(.+)\.jsm$/.exec(resource); | |
84 let resourceName = match && match[1]; | |
85 if (resourceName && resources.hasOwnProperty(resourceName)) | |
86 return {[resourceName]: resources[resourceName]}; | |
Wladimir Palant
2017/03/09 14:37:14
This is a good case for the consistent-return rule
kzar
2017/03/10 06:56:14
Done.
| |
87 }, | |
88 reportError(e) {} | |
43 }, | 89 }, |
44 console: { | 90 console: { |
45 log: () => undefined, | 91 log() {}, |
46 error: () => undefined, | 92 error() {} |
47 }, | 93 }, |
48 navigator: { | 94 navigator: { |
49 }, | 95 }, |
50 onShutdown: { | 96 onShutdown: { |
51 add: () => | 97 add() {} |
52 { | |
53 } | |
54 }, | 98 }, |
55 Services: { | 99 URL |
56 obs: { | |
57 addObserver: () => | |
58 { | |
59 } | |
60 }, | |
61 vc: { | |
62 compare: (v1, v2) => | |
63 { | |
64 function comparePart(p1, p2) | |
65 { | |
66 if (p1 != "*" && p2 == "*") | |
67 return -1; | |
68 else if (p1 == "*" && p2 != "*") | |
69 return 1; | |
70 else if (p1 == p2) | |
71 return 0; | |
72 else | |
73 return parseInt(p1, 10) - parseInt(p2, 10); | |
74 } | |
75 | |
76 let parts1 = v1.split("."); | |
77 let parts2 = v2.split("."); | |
78 for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) | |
79 { | |
80 let result = comparePart(parts1[i] || "0", parts2[i] || "0"); | |
81 if (result != 0) | |
82 return result; | |
83 } | |
84 return 0; | |
85 } | |
86 } | |
87 }, | |
88 XPCOMUtils: { | |
89 generateQI: () => | |
90 { | |
91 } | |
92 }, | |
93 URL: function(urlString) | |
94 { | |
95 return require("url").parse(urlString); | |
96 } | |
97 }; | 100 }; |
98 | 101 |
99 let knownModules = new Map(); | 102 let knownModules = new Map(); |
100 for (let dir of [path.join(__dirname, "stub-modules"), | 103 for (let dir of [path.join(__dirname, "stub-modules"), |
101 path.join(__dirname, "..", "lib")]) | 104 path.join(__dirname, "..", "lib")]) |
105 { | |
102 for (let file of fs.readdirSync(path.resolve(dir))) | 106 for (let file of fs.readdirSync(path.resolve(dir))) |
107 { | |
103 if (path.extname(file) == ".js") | 108 if (path.extname(file) == ".js") |
104 knownModules[path.basename(file, ".js")] = path.resolve(dir, file); | 109 knownModules[path.basename(file, ".js")] = path.resolve(dir, file); |
110 } | |
111 } | |
105 | 112 |
106 function addExports(exports) | 113 function addExports(exports) |
107 { | 114 { |
108 return function(source) | 115 return function(source) |
109 { | 116 { |
110 let extraExports = exports[path.basename(this.filename, ".js")]; | 117 let extraExports = exports[path.basename(this.filename, ".js")]; |
111 if (extraExports) | 118 if (extraExports) |
119 { | |
112 for (let name of extraExports) | 120 for (let name of extraExports) |
121 { | |
113 source += ` | 122 source += ` |
114 Object.defineProperty(exports, "${name}", {get: () => ${name}});`; | 123 Object.defineProperty(exports, "${name}", {get: () => ${name}});`; |
124 } | |
125 } | |
115 return source; | 126 return source; |
116 }; | 127 }; |
117 } | 128 } |
118 | 129 |
119 function rewriteRequires(source) | 130 function rewriteRequires(source) |
120 { | 131 { |
121 function escapeString(str) | 132 function escapeString(str) |
122 { | 133 { |
123 return str.replace(/(["'\\])/g, "\\$1"); | 134 return str.replace(/(["'\\])/g, "\\$1"); |
124 } | 135 } |
(...skipping 12 matching lines...) Expand all Loading... | |
137 options = {}; | 148 options = {}; |
138 | 149 |
139 let sourceTransformers = [rewriteRequires]; | 150 let sourceTransformers = [rewriteRequires]; |
140 if (options.extraExports) | 151 if (options.extraExports) |
141 sourceTransformers.push(addExports(options.extraExports)); | 152 sourceTransformers.push(addExports(options.extraExports)); |
142 | 153 |
143 // This module loads itself into a sandbox, keeping track of the require | 154 // This module loads itself into a sandbox, keeping track of the require |
144 // function which can be used to load further modules into the sandbox. | 155 // function which can be used to load further modules into the sandbox. |
145 return SandboxedModule.require("./_common", { | 156 return SandboxedModule.require("./_common", { |
146 globals: Object.assign({}, globals, options.globals), | 157 globals: Object.assign({}, globals, options.globals), |
147 sourceTransformers: sourceTransformers | 158 sourceTransformers |
148 }).require; | 159 }).require; |
149 }; | 160 }; |
150 | 161 |
151 exports.require = require; | 162 exports.require = require; |
152 | 163 |
153 exports.setupTimerAndXMLHttp = function() | 164 exports.setupTimerAndXMLHttp = function() |
154 { | 165 { |
155 let currentTime = 100000 * MILLIS_IN_HOUR; | 166 let currentTime = 100000 * MILLIS_IN_HOUR; |
156 let startTime = currentTime; | 167 let startTime = currentTime; |
157 | 168 |
158 let fakeTimer = { | 169 let fakeTimer = { |
159 callback: null, | 170 callback: null, |
160 delay: -1, | 171 delay: -1, |
161 nextExecution: 0, | 172 nextExecution: 0, |
162 | 173 |
163 initWithCallback: function(callback, delay, type) | 174 initWithCallback(callback, delay, type) |
164 { | 175 { |
165 if (this.callback) | 176 if (this.callback) |
166 throw new Error("Only one timer instance supported"); | 177 throw new Error("Only one timer instance supported"); |
167 if (type != 1) | 178 if (type != 1) |
168 throw new Error("Only TYPE_REPEATING_SLACK timers supported"); | 179 throw new Error("Only TYPE_REPEATING_SLACK timers supported"); |
169 | 180 |
170 this.callback = callback; | 181 this.callback = callback; |
171 this.delay = delay; | 182 this.delay = delay; |
172 this.nextExecution = currentTime + delay; | 183 this.nextExecution = currentTime + delay; |
173 }, | 184 }, |
174 | 185 |
175 trigger: function() | 186 trigger() |
176 { | 187 { |
177 if (currentTime < this.nextExecution) | 188 if (currentTime < this.nextExecution) |
178 currentTime = this.nextExecution; | 189 currentTime = this.nextExecution; |
179 try | 190 try |
180 { | 191 { |
181 this.callback(); | 192 this.callback(); |
182 } | 193 } |
183 finally | 194 finally |
184 { | 195 { |
185 this.nextExecution = currentTime + this.delay; | 196 this.nextExecution = currentTime + this.delay; |
186 } | 197 } |
187 }, | 198 }, |
188 | 199 |
189 cancel: function() | 200 cancel() |
190 { | 201 { |
191 this.nextExecution = -1; | 202 this.nextExecution = -1; |
192 } | 203 } |
193 }; | 204 }; |
194 | 205 |
195 let requests = []; | 206 let requests = []; |
196 function XMLHttpRequest() | 207 function XMLHttpRequest() |
197 { | 208 { |
198 this._host = "http://example.com"; | 209 this._host = "http://example.com"; |
199 this._loadHandlers = []; | 210 this._loadHandlers = []; |
200 this._errorHandlers = []; | 211 this._errorHandlers = []; |
201 }; | 212 } |
202 XMLHttpRequest.prototype = | 213 XMLHttpRequest.prototype = { |
203 { | |
204 _path: null, | 214 _path: null, |
205 _data: null, | 215 _data: null, |
206 _queryString: null, | 216 _queryString: null, |
207 _loadHandlers: null, | 217 _loadHandlers: null, |
208 _errorHandlers: null, | 218 _errorHandlers: null, |
209 status: 0, | 219 status: 0, |
210 readyState: 0, | 220 readyState: 0, |
211 responseText: null, | 221 responseText: null, |
212 | 222 |
213 addEventListener: function(eventName, handler, capture) | 223 addEventListener(eventName, handler, capture) |
214 { | 224 { |
215 let list; | 225 let list; |
216 if (eventName == "load") | 226 if (eventName == "load") |
217 list = this._loadHandlers; | 227 list = this._loadHandlers; |
218 else if (eventName == "error") | 228 else if (eventName == "error") |
219 list = this._errorHandlers; | 229 list = this._errorHandlers; |
220 else | 230 else |
221 throw new Error("Event type " + eventName + " not supported"); | 231 throw new Error("Event type " + eventName + " not supported"); |
222 | 232 |
223 if (list.indexOf(handler) < 0) | 233 if (list.indexOf(handler) < 0) |
224 list.push(handler); | 234 list.push(handler); |
225 }, | 235 }, |
226 | 236 |
227 removeEventListener: function(eventName, handler, capture) | 237 removeEventListener(eventName, handler, capture) |
228 { | 238 { |
229 let list; | 239 let list; |
230 if (eventName == "load") | 240 if (eventName == "load") |
231 list = this._loadHandlers; | 241 list = this._loadHandlers; |
232 else if (eventName == "error") | 242 else if (eventName == "error") |
233 list = this._errorHandlers; | 243 list = this._errorHandlers; |
234 else | 244 else |
235 throw new Error("Event type " + eventName + " not supported"); | 245 throw new Error("Event type " + eventName + " not supported"); |
236 | 246 |
237 let index = list.indexOf(handler); | 247 let index = list.indexOf(handler); |
238 if (index >= 0) | 248 if (index >= 0) |
239 list.splice(index, 1); | 249 list.splice(index, 1); |
240 }, | 250 }, |
241 | 251 |
242 open: function(method, url, async, user, password) | 252 open(method, url, async, user, password) |
243 { | 253 { |
244 if (method != "GET") | 254 if (method != "GET") |
245 throw new Error("Only GET requests are supported"); | 255 throw new Error("Only GET requests are supported"); |
246 if (typeof async != "undefined" && !async) | 256 if (typeof async != "undefined" && !async) |
247 throw new Error("Sync requests are not supported"); | 257 throw new Error("Sync requests are not supported"); |
248 if (typeof user != "undefined" || typeof password != "undefined") | 258 if (typeof user != "undefined" || typeof password != "undefined") |
249 throw new Error("User authentification is not supported"); | 259 throw new Error("User authentification is not supported"); |
250 | 260 |
251 let match = /^data:[^,]+,/.exec(url); | 261 let match = /^data:[^,]+,/.exec(url); |
252 if (match) | 262 if (match) |
253 { | 263 { |
254 this._data = decodeURIComponent(url.substr(match[0].length)); | 264 this._data = decodeURIComponent(url.substr(match[0].length)); |
255 return; | 265 return; |
256 } | 266 } |
257 | 267 |
258 if (url.substr(0, this._host.length + 1) != this._host + "/") | 268 if (url.substr(0, this._host.length + 1) != this._host + "/") |
259 throw new Error("Unexpected URL: " + url + " (URL starting with " + this ._host + "expected)"); | 269 throw new Error("Unexpected URL: " + url + " (URL starting with " + this ._host + "expected)"); |
260 | 270 |
261 this._path = url.substr(this._host.length); | 271 this._path = url.substr(this._host.length); |
262 | 272 |
263 let queryIndex = this._path.indexOf("?"); | 273 let queryIndex = this._path.indexOf("?"); |
264 this._queryString = ""; | 274 this._queryString = ""; |
265 if (queryIndex >= 0) | 275 if (queryIndex >= 0) |
266 { | 276 { |
267 this._queryString = this._path.substr(queryIndex + 1); | 277 this._queryString = this._path.substr(queryIndex + 1); |
268 this._path = this._path.substr(0, queryIndex); | 278 this._path = this._path.substr(0, queryIndex); |
269 } | 279 } |
270 }, | 280 }, |
271 | 281 |
272 send: function(data) | 282 send(data) |
273 { | 283 { |
274 if (!this._data && !this._path) | 284 if (!this._data && !this._path) |
275 throw new Error("No request path set"); | 285 throw new Error("No request path set"); |
276 if (typeof data != "undefined" && data) | 286 if (typeof data != "undefined" && data) |
277 throw new Error("Sending data to server is not supported"); | 287 throw new Error("Sending data to server is not supported"); |
278 | 288 |
279 requests.push(Promise.resolve().then(() => | 289 requests.push(Promise.resolve().then(() => |
280 { | 290 { |
281 let result = [Cr.NS_OK, 404, ""]; | 291 let result = [Cr.NS_OK, 404, ""]; |
282 if (this._data) | 292 if (this._data) |
(...skipping 10 matching lines...) Expand all Loading... | |
293 [this.channel.status, this.channel.responseStatus, this.responseText] = result; | 303 [this.channel.status, this.channel.responseStatus, this.responseText] = result; |
294 this.status = this.channel.responseStatus; | 304 this.status = this.channel.responseStatus; |
295 | 305 |
296 let eventName = (this.channel.status == Cr.NS_OK ? "load" : "error"); | 306 let eventName = (this.channel.status == Cr.NS_OK ? "load" : "error"); |
297 let event = {type: eventName}; | 307 let event = {type: eventName}; |
298 for (let handler of this["_" + eventName + "Handlers"]) | 308 for (let handler of this["_" + eventName + "Handlers"]) |
299 handler.call(this, event); | 309 handler.call(this, event); |
300 })); | 310 })); |
301 }, | 311 }, |
302 | 312 |
303 overrideMimeType: function(mime) | 313 overrideMimeType(mime) |
304 { | 314 { |
305 }, | 315 }, |
306 | 316 |
307 channel: | 317 channel: |
308 { | 318 { |
309 status: -1, | 319 status: -1, |
310 responseStatus: 0, | 320 responseStatus: 0, |
311 loadFlags: 0, | 321 loadFlags: 0, |
312 INHIBIT_CACHING: 0, | 322 INHIBIT_CACHING: 0, |
313 VALIDATE_ALWAYS: 0, | 323 VALIDATE_ALWAYS: 0, |
314 QueryInterface: () => this | 324 QueryInterface: () => this |
315 } | 325 } |
316 }; | 326 }; |
317 | 327 |
318 XMLHttpRequest.requestHandlers = {}; | 328 XMLHttpRequest.requestHandlers = {}; |
319 this.registerHandler = (path, handler) => | 329 this.registerHandler = (requestPath, handler) => |
320 { | 330 { |
321 XMLHttpRequest.requestHandlers[path] = handler; | 331 XMLHttpRequest.requestHandlers[requestPath] = handler; |
322 }; | 332 }; |
323 | 333 |
324 function waitForRequests() | 334 function waitForRequests() |
325 { | 335 { |
326 if (requests.length) | 336 if (requests.length) |
327 { | 337 { |
328 let result = Promise.all(requests); | 338 let result = Promise.all(requests); |
329 requests = []; | 339 requests = []; |
330 return result.catch(e => | 340 return result.catch(e => |
331 { | 341 { |
332 console.error(e); | 342 console.error(e); |
333 }).then(() => waitForRequests()); | 343 }).then(() => waitForRequests()); |
334 } | 344 } |
335 else | 345 return Promise.resolve(); |
336 return Promise.resolve(); | |
337 } | 346 } |
338 | 347 |
339 function runScheduledTasks(maxMillis) | 348 function runScheduledTasks(maxMillis) |
340 { | 349 { |
341 let endTime = currentTime + maxMillis; | 350 let endTime = currentTime + maxMillis; |
342 if (fakeTimer.nextExecution < 0 || fakeTimer.nextExecution > endTime) | 351 if (fakeTimer.nextExecution < 0 || fakeTimer.nextExecution > endTime) |
343 { | 352 { |
344 currentTime = endTime; | 353 currentTime = endTime; |
345 return Promise.resolve(); | 354 return Promise.resolve(); |
346 } | 355 } |
347 else | 356 fakeTimer.trigger(); |
348 { | 357 return waitForRequests().then(() => runScheduledTasks(endTime - currentTime) ); |
349 fakeTimer.trigger(); | |
350 return waitForRequests().then(() => runScheduledTasks(endTime - currentTim e)); | |
351 } | |
352 | |
353 currentTime = endTime; | |
354 } | 358 } |
355 | 359 |
356 this.runScheduledTasks = (maxHours, initial, skip) => | 360 this.runScheduledTasks = (maxHours, initial, skip) => |
357 { | 361 { |
358 if (typeof maxHours != "number") | 362 if (typeof maxHours != "number") |
359 throw new Error("Numerical parameter expected"); | 363 throw new Error("Numerical parameter expected"); |
360 if (typeof initial != "number") | 364 if (typeof initial != "number") |
361 initial = 0; | 365 initial = 0; |
362 if (typeof skip != "number") | 366 if (typeof skip != "number") |
363 skip = 0; | 367 skip = 0; |
(...skipping 28 matching lines...) Expand all Loading... | |
392 createInstance: () => fakeTimer | 396 createInstance: () => fakeTimer |
393 } | 397 } |
394 }, | 398 }, |
395 Ci: { | 399 Ci: { |
396 nsITimer: | 400 nsITimer: |
397 { | 401 { |
398 TYPE_ONE_SHOT: 0, | 402 TYPE_ONE_SHOT: 0, |
399 TYPE_REPEATING_SLACK: 1, | 403 TYPE_REPEATING_SLACK: 1, |
400 TYPE_REPEATING_PRECISE: 2 | 404 TYPE_REPEATING_PRECISE: 2 |
401 }, | 405 }, |
402 nsIHttpChannel: () => null, | 406 nsIHttpChannel: () => null |
403 }, | 407 }, |
404 Cr, | 408 Cr, |
405 XMLHttpRequest, | 409 XMLHttpRequest, |
406 Date: { | 410 Date: { |
407 now: () => currentTime | 411 now: () => currentTime |
408 } | 412 } |
409 }; | 413 }; |
410 }; | 414 }; |
411 | 415 |
416 console.warn = console.log; | |
417 | |
412 exports.setupRandomResult = function() | 418 exports.setupRandomResult = function() |
413 { | 419 { |
414 let randomResult = 0.5; | 420 let randomResult = 0.5; |
415 Object.defineProperty(this, "randomResult", { | 421 Object.defineProperty(this, "randomResult", { |
416 get: () => randomResult, | 422 get: () => randomResult, |
417 set: value => randomResult = value | 423 set: value => randomResult = value |
418 }); | 424 }); |
419 | 425 |
420 return { | 426 return { |
421 Math: { | 427 Math: { |
422 random: () => randomResult, | 428 random: () => randomResult, |
423 min: Math.min, | 429 min: Math.min, |
424 max: Math.max, | 430 max: Math.max, |
425 round: Math.round | 431 round: Math.round |
426 } | 432 } |
427 }; | 433 }; |
428 }; | 434 }; |
429 | 435 |
430 exports.unexpectedError = function(error) | 436 exports.unexpectedError = function(error) |
431 { | 437 { |
432 console.error(error); | 438 console.error(error); |
433 this.ok(false, "Unexpected error: " + error); | 439 this.ok(false, "Unexpected error: " + error); |
434 }; | 440 }; |
OLD | NEW |