| 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 15 matching lines...) Expand all Loading... | |
| 26 const MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; | 26 const MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; |
| 27 const MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; | 27 const MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; |
| 28 const MILLIS_IN_HOUR = exports.MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; | 28 const MILLIS_IN_HOUR = exports.MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; |
| 29 const MILLIS_IN_DAY = exports.MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; | 29 const MILLIS_IN_DAY = exports.MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; |
| 30 | 30 |
| 31 class Downloader | 31 class Downloader |
| 32 { | 32 { |
| 33 /** | 33 /** |
| 34 * Creates a new downloader instance. | 34 * Creates a new downloader instance. |
| 35 * @param {function} dataSource | 35 * @param {function} dataSource |
| 36 * function that will yield downloadable objects on each check | 36 * Function that will yield downloadable objects on each check |
|
Manish Jethani
2018/08/23 15:03:39
This should still be capital F? It's not the type
Jon Sonesen
2018/08/23 16:41:46
Done. I used sed replacement carelessly to do the
| |
| 37 * @param {number} initialDelay | 37 * @param {number} initialDelay |
| 38 * Number of milliseconds to wait before the first check | 38 * Number of milliseconds to wait before the first check |
| 39 * @param {number} checkInterval | 39 * @param {number} checkInterval |
| 40 * Interval between the checks | 40 * Interval between the checks |
| 41 */ | 41 */ |
| 42 constructor(dataSource, initialDelay, checkInterval) | 42 constructor(dataSource, initialDelay, checkInterval) |
| 43 { | 43 { |
| 44 /** | 44 /** |
| 45 * Maximal time interval that the checks can be left out until the soft | 45 * Maximal time interval that the checks can be left out until the soft |
| 46 * expiration interval increases. | 46 * expiration interval increases. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 62 this.maxExpirationInterval = 14 * MILLIS_IN_DAY; | 62 this.maxExpirationInterval = 14 * MILLIS_IN_DAY; |
| 63 | 63 |
| 64 /** | 64 /** |
| 65 * Maximal number of redirects before the download is considered as failed. | 65 * Maximal number of redirects before the download is considered as failed. |
| 66 * @type {number} | 66 * @type {number} |
| 67 */ | 67 */ |
| 68 this.maxRedirects = 5; | 68 this.maxRedirects = 5; |
| 69 | 69 |
| 70 /** | 70 /** |
| 71 * Called whenever expiration intervals for an object need to be adapted. | 71 * Called whenever expiration intervals for an object need to be adapted. |
| 72 * @type {function} | 72 * @type {function?} |
|
Manish Jethani
2018/08/23 15:03:39
Let's change the type here to `function?` (also be
Jon Sonesen
2018/08/23 16:41:46
Done.
| |
| 73 */ | 73 */ |
| 74 this.onExpirationChange = null; | 74 this.onExpirationChange = null; |
| 75 | 75 |
| 76 /** | 76 /** |
| 77 * Callback to be triggered whenever a download starts. | 77 * Callback to be triggered whenever a download starts. |
| 78 * @type {function} | 78 * @type {function?} |
| 79 */ | 79 */ |
| 80 this.onDownloadStarted = null; | 80 this.onDownloadStarted = null; |
| 81 | 81 |
| 82 /** | 82 /** |
| 83 * Callback to be triggered whenever a download finishes successfully. The | 83 * Callback to be triggered whenever a download finishes successfully. The |
| 84 * callback can return an error code to indicate that the data is wrong. | 84 * callback can return an error code to indicate that the data is wrong. |
| 85 * @type {function} | 85 * @type {function?} |
| 86 */ | 86 */ |
| 87 this.onDownloadSuccess = null; | 87 this.onDownloadSuccess = null; |
| 88 | 88 |
| 89 /** | 89 /** |
| 90 * Callback to be triggered whenever a download fails. | 90 * Callback to be triggered whenever a download fails. |
| 91 * @type {function} | 91 * @type {function?} |
| 92 */ | 92 */ |
| 93 this.onDownloadError = null; | 93 this.onDownloadError = null; |
| 94 | 94 |
| 95 /** | 95 /** |
| 96 * function that will yield downloadable objects on each check. | 96 * Function that will yield downloadable objects on each check. |
|
Manish Jethani
2018/08/23 15:03:39
This too.
Jon Sonesen
2018/08/23 16:41:46
Done.
| |
| 97 * @type {function} | 97 * @type {function} |
| 98 */ | 98 */ |
| 99 this.dataSource = dataSource; | 99 this.dataSource = dataSource; |
| 100 | 100 |
| 101 /** | 101 /** |
| 102 * Timer triggering the downloads. | 102 * Timer triggering the downloads. |
| 103 * @type {nsITimer} | 103 * @type {nsITimer} |
| 104 */ | 104 */ |
| 105 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); | 105 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
| 106 this._timer.initWithCallback(() => | 106 this._timer.initWithCallback(() => |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 * Stops the periodic checks. | 167 * Stops the periodic checks. |
| 168 */ | 168 */ |
| 169 cancel() | 169 cancel() |
| 170 { | 170 { |
| 171 this._timer.cancel(); | 171 this._timer.cancel(); |
| 172 } | 172 } |
| 173 | 173 |
| 174 /** | 174 /** |
| 175 * Checks whether an address is currently being downloaded. | 175 * Checks whether an address is currently being downloaded. |
| 176 * @param {string} url | 176 * @param {string} url |
| 177 * @return {boolean} | 177 * @returns {boolean} |
| 178 */ | 178 */ |
| 179 isDownloading(url) | 179 isDownloading(url) |
| 180 { | 180 { |
| 181 return this._downloading.has(url); | 181 return this._downloading.has(url); |
| 182 } | 182 } |
| 183 | 183 |
| 184 /** | 184 /** |
| 185 * Starts downloading for an object. | 185 * Starts downloading for an object. |
| 186 * @param {Downloadable} downloadable | 186 * @param {Downloadable} downloadable |
| 187 */ | 187 */ |
| 188 download(downloadable) | 188 download(downloadable) |
| 189 { | 189 { |
| 190 // Make sure to detach download from the current execution context | 190 // Make sure to detach download from the current execution context |
| 191 Utils.runAsync(this._download.bind(this, downloadable, 0)); | 191 Utils.runAsync(this._download.bind(this, downloadable, 0)); |
| 192 } | 192 } |
| 193 | 193 |
| 194 /** | 194 /** |
| 195 * Generates the real download URL for an object by appending various | 195 * Generates the real download URL for an object by appending various |
| 196 * parameters. | 196 * parameters. |
| 197 * @param {Downloadable} downloadable | 197 * @param {Downloadable} downloadable |
| 198 * @return {string} | 198 * @returns {string} |
| 199 */ | 199 */ |
| 200 getDownloadUrl(downloadable) | 200 getDownloadUrl(downloadable) |
| 201 { | 201 { |
| 202 const {addonName, addonVersion, application, applicationVersion, | 202 const {addonName, addonVersion, application, applicationVersion, |
| 203 platform, platformVersion} = require("info"); | 203 platform, platformVersion} = require("info"); |
| 204 let url = downloadable.redirectURL || downloadable.url; | 204 let url = downloadable.redirectURL || downloadable.url; |
| 205 if (url.includes("?")) | 205 if (url.includes("?")) |
| 206 url += "&"; | 206 url += "&"; |
| 207 else | 207 else |
| 208 url += "?"; | 208 url += "?"; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 336 | 336 |
| 337 this._downloading.add(downloadable.url); | 337 this._downloading.add(downloadable.url); |
| 338 if (this.onDownloadStarted) | 338 if (this.onDownloadStarted) |
| 339 this.onDownloadStarted(downloadable); | 339 this.onDownloadStarted(downloadable); |
| 340 } | 340 } |
| 341 | 341 |
| 342 /** | 342 /** |
| 343 * Produces a soft and a hard expiration interval for a given supplied | 343 * Produces a soft and a hard expiration interval for a given supplied |
| 344 * expiration interval. | 344 * expiration interval. |
| 345 * @param {number} interval | 345 * @param {number} interval |
| 346 * @return {Array} soft and hard expiration interval | 346 * @returns {Array.<number>} soft and hard expiration interval |
| 347 */ | 347 */ |
| 348 processExpirationInterval(interval) | 348 processExpirationInterval(interval) |
| 349 { | 349 { |
| 350 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval); | 350 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval); |
| 351 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8)); | 351 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8)); |
| 352 let hard = interval * 2; | 352 let hard = interval * 2; |
| 353 let now = Date.now(); | 353 let now = Date.now(); |
| 354 return [now + soft, now + hard]; | 354 return [now + soft, now + hard]; |
| 355 } | 355 } |
| 356 } | 356 } |
| 357 | 357 |
| 358 exports.Downloader = Downloader; | |
| 359 | |
| 358 class Downloadable | 360 class Downloadable |
| 359 { | 361 { |
| 360 /** | 362 /** |
| 361 * Creates an object that can be downloaded by the downloader. | 363 * Creates an object that can be downloaded by the downloader. |
| 362 * @param {string} url URL that has to be requested for the object | 364 * @param {string} url URL that has to be requested for the object |
| 363 */ | 365 */ |
| 364 constructor(url) | 366 constructor(url) |
| 365 { | 367 { |
| 366 /** | 368 /** |
| 367 * URL that the download was redirected to if any. | 369 * URL that the download was redirected to if any. |
| 368 * @type {string} | 370 * @type {string?} |
|
Manish Jethani
2018/08/23 15:03:39
Since redirectURL can be null (starts out as null
Jon Sonesen
2018/08/23 16:41:46
Done.
| |
| 369 */ | 371 */ |
| 370 this.redirectURL = null; | 372 this.redirectURL = null; |
| 371 | 373 |
| 372 /** | 374 /** |
| 373 * Time of last download error or 0 if the last download was successful. | 375 * Time of last download error or 0 if the last download was successful. |
| 374 * @type {number} | 376 * @type {number} |
| 375 */ | 377 */ |
| 376 this.lastError = 0; | 378 this.lastError = 0; |
| 377 | 379 |
| 378 /** | 380 /** |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 407 this.downloadCount = 0; | 409 this.downloadCount = 0; |
| 408 | 410 |
| 409 /** | 411 /** |
| 410 * URL that has to be requested for the object. | 412 * URL that has to be requested for the object. |
| 411 * @type {string} | 413 * @type {string} |
| 412 */ | 414 */ |
| 413 this.url = url; | 415 this.url = url; |
| 414 } | 416 } |
| 415 } | 417 } |
| 416 | 418 |
| 417 exports.Downloader = Downloader; | |
| 418 exports.Downloadable = Downloadable; | 419 exports.Downloadable = Downloadable; |
|
Jon Sonesen
2018/08/22 18:18:51
I thought having a blank line, the addition to exp
Manish Jethani
2018/08/23 15:03:39
Sure, this is how I do it too for my personal proj
Jon Sonesen
2018/08/23 16:41:46
Done.
| |
| LEFT | RIGHT |