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-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 30 matching lines...) Expand all Loading... |
41 */ | 41 */ |
42 exports.Downloader = function(dataSource, initialDelay, checkInterval) | 42 exports.Downloader = function(dataSource, initialDelay, checkInterval) |
43 { | 43 { |
44 this.dataSource = dataSource; | 44 this.dataSource = dataSource; |
45 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); | 45 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
46 this._timer.initWithCallback(() => | 46 this._timer.initWithCallback(() => |
47 { | 47 { |
48 this._timer.delay = checkInterval; | 48 this._timer.delay = checkInterval; |
49 this._doCheck(); | 49 this._doCheck(); |
50 }, initialDelay, Ci.nsITimer.TYPE_REPEATING_SLACK); | 50 }, initialDelay, Ci.nsITimer.TYPE_REPEATING_SLACK); |
51 this._downloading = Object.create(null); | 51 this._downloading = new Set(); |
52 }; | 52 }; |
53 Downloader.prototype = | 53 Downloader.prototype = |
54 { | 54 { |
55 /** | 55 /** |
56 * Timer triggering the downloads. | 56 * Timer triggering the downloads. |
57 * @type {nsITimer} | 57 * @type {nsITimer} |
58 */ | 58 */ |
59 _timer: null, | 59 _timer: null, |
60 | 60 |
61 /** | 61 /** |
62 * Map containing the URLs of objects currently being downloaded as its keys. | 62 * Set containing the URLs of objects currently being downloaded. |
| 63 * @type {Set.<string>} |
63 */ | 64 */ |
64 _downloading: null, | 65 _downloading: null, |
65 | 66 |
66 /** | 67 /** |
67 * Function that will yield downloadable objects on each check. | 68 * Function that will yield downloadable objects on each check. |
68 * @type {Function} | 69 * @type {Function} |
69 */ | 70 */ |
70 dataSource: null, | 71 dataSource: null, |
71 | 72 |
72 /** | 73 /** |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 this._timer.cancel(); | 176 this._timer.cancel(); |
176 }, | 177 }, |
177 | 178 |
178 /** | 179 /** |
179 * Checks whether an address is currently being downloaded. | 180 * Checks whether an address is currently being downloaded. |
180 * @param {string} url | 181 * @param {string} url |
181 * @return {boolean} | 182 * @return {boolean} |
182 */ | 183 */ |
183 isDownloading(url) | 184 isDownloading(url) |
184 { | 185 { |
185 return url in this._downloading; | 186 return this._downloading.has(url); |
186 }, | 187 }, |
187 | 188 |
188 /** | 189 /** |
189 * Starts downloading for an object. | 190 * Starts downloading for an object. |
190 * @param {Downloadable} downloadable | 191 * @param {Downloadable} downloadable |
191 */ | 192 */ |
192 download(downloadable) | 193 download(downloadable) |
193 { | 194 { |
194 // Make sure to detach download from the current execution context | 195 // Make sure to detach download from the current execution context |
195 Utils.runAsync(this._download.bind(this, downloadable, 0)); | 196 Utils.runAsync(this._download.bind(this, downloadable, 0)); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 catch (e) | 295 catch (e) |
295 { | 296 { |
296 Cu.reportError(e); | 297 Cu.reportError(e); |
297 } | 298 } |
298 | 299 |
299 request.addEventListener("error", event => | 300 request.addEventListener("error", event => |
300 { | 301 { |
301 if (onShutdown.done) | 302 if (onShutdown.done) |
302 return; | 303 return; |
303 | 304 |
304 delete this._downloading[downloadable.url]; | 305 this._downloading.delete(downloadable.url); |
305 errorCallback("synchronize_connection_error"); | 306 errorCallback("synchronize_connection_error"); |
306 }, false); | 307 }, false); |
307 | 308 |
308 request.addEventListener("load", event => | 309 request.addEventListener("load", event => |
309 { | 310 { |
310 if (onShutdown.done) | 311 if (onShutdown.done) |
311 return; | 312 return; |
312 | 313 |
313 delete this._downloading[downloadable.url]; | 314 this._downloading.delete(downloadable.url); |
314 | 315 |
315 // Status will be 0 for non-HTTP requests | 316 // Status will be 0 for non-HTTP requests |
316 if (request.status && request.status != 200) | 317 if (request.status && request.status != 200) |
317 { | 318 { |
318 errorCallback("synchronize_connection_error"); | 319 errorCallback("synchronize_connection_error"); |
319 return; | 320 return; |
320 } | 321 } |
321 | 322 |
322 downloadable.downloadCount++; | 323 downloadable.downloadCount++; |
323 | 324 |
324 this.onDownloadSuccess( | 325 this.onDownloadSuccess( |
325 downloadable, request.responseText, errorCallback, | 326 downloadable, request.responseText, errorCallback, |
326 url => | 327 url => |
327 { | 328 { |
328 if (redirects >= this.maxRedirects) | 329 if (redirects >= this.maxRedirects) |
329 errorCallback("synchronize_connection_error"); | 330 errorCallback("synchronize_connection_error"); |
330 else | 331 else |
331 { | 332 { |
332 downloadable.redirectURL = url; | 333 downloadable.redirectURL = url; |
333 this._download(downloadable, redirects + 1); | 334 this._download(downloadable, redirects + 1); |
334 } | 335 } |
335 } | 336 } |
336 ); | 337 ); |
337 }); | 338 }); |
338 | 339 |
339 request.send(null); | 340 request.send(null); |
340 | 341 |
341 this._downloading[downloadable.url] = true; | 342 this._downloading.add(downloadable.url); |
342 if (this.onDownloadStarted) | 343 if (this.onDownloadStarted) |
343 this.onDownloadStarted(downloadable); | 344 this.onDownloadStarted(downloadable); |
344 }, | 345 }, |
345 | 346 |
346 /** | 347 /** |
347 * Produces a soft and a hard expiration interval for a given supplied | 348 * Produces a soft and a hard expiration interval for a given supplied |
348 * expiration interval. | 349 * expiration interval. |
349 * @param {number} interval | 350 * @param {number} interval |
350 * @return {Array} soft and hard expiration interval | 351 * @return {Array} soft and hard expiration interval |
351 */ | 352 */ |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 * @type {number} | 413 * @type {number} |
413 */ | 414 */ |
414 hardExpiration: 0, | 415 hardExpiration: 0, |
415 | 416 |
416 /** | 417 /** |
417 * Number indicating how often the object was downloaded. | 418 * Number indicating how often the object was downloaded. |
418 * @type {number} | 419 * @type {number} |
419 */ | 420 */ |
420 downloadCount: 0 | 421 downloadCount: 0 |
421 }; | 422 }; |
OLD | NEW |