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