| 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 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 let MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; | 24 let MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; |
| 25 let MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; | 25 let MILLIS_IN_MINUTE = exports.MILLIS_IN_MINUTE = 60 * MILLIS_IN_SECOND; |
| 26 let MILLIS_IN_HOUR = exports.MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; | 26 let MILLIS_IN_HOUR = exports.MILLIS_IN_HOUR = 60 * MILLIS_IN_MINUTE; |
| 27 let MILLIS_IN_DAY = exports.MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; | 27 let MILLIS_IN_DAY = exports.MILLIS_IN_DAY = 24 * MILLIS_IN_HOUR; |
| 28 | 28 |
| 29 /** | 29 /** |
| 30 * Creates a new downloader instance. | 30 * Creates a new downloader instance. |
| 31 * @param {Function} dataSource Function that will yield downloadable objects o
n each check | 31 * @param {Function} dataSource Function that will yield downloadable objects o
n each check |
| 32 * @param {Integer} initialDelay Number of milliseconds to wait before the firs
t check | 32 * @param {Integer} initialDelay Number of milliseconds to wait before the firs
t check |
| 33 * @param {Integer} checkInterval Interval between the checks | 33 * @param {Integer} checkInterval Interval between the checks |
| 34 * @param {function} downloadCallback optional function that's being called when
something needs to be downloaded |
| 34 * @constructor | 35 * @constructor |
| 35 */ | 36 */ |
| 36 let Downloader = exports.Downloader = function Downloader(dataSource, initialDel
ay, checkInterval) | 37 let Downloader = exports.Downloader = function Downloader(dataSource, initialDel
ay, checkInterval, downloadCallback) |
| 37 { | 38 { |
| 38 this.dataSource = dataSource; | 39 this.dataSource = dataSource; |
| 40 this.downloadCallback = downloadCallback || this._download; |
| 39 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); | 41 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
| 40 this._timer.initWithCallback(function() | 42 this._timer.initWithCallback(function() |
| 41 { | 43 { |
| 42 this._timer.delay = checkInterval; | 44 this._timer.delay = checkInterval; |
| 43 this._doCheck(); | 45 this._doCheck(); |
| 44 }.bind(this), initialDelay, Ci.nsITimer.TYPE_REPEATING_SLACK); | 46 }.bind(this), initialDelay, Ci.nsITimer.TYPE_REPEATING_SLACK); |
| 45 this._downloading = Object.create(null); | 47 this._downloading = Object.create(null); |
| 46 } | 48 } |
| 47 Downloader.prototype = | 49 Downloader.prototype = |
| 48 { | 50 { |
| 49 /** | 51 /** |
| 50 * Timer triggering the downloads. | 52 * Timer triggering the downloads. |
| 51 * @type nsITimer | 53 * @type nsITimer |
| 52 */ | 54 */ |
| 53 _timer: null, | 55 _timer: null, |
| 54 | 56 |
| 55 /** | 57 /** |
| 56 * Map containing the URLs of objects currently being downloaded as its keys. | 58 * Map containing the URLs of objects currently being downloaded as its keys. |
| 57 */ | 59 */ |
| 58 _downloading: null, | 60 _downloading: null, |
| 59 | 61 |
| 60 /** | 62 /** |
| 61 * Function that will yield downloadable objects on each check. | 63 * Function that will yield downloadable objects on each check. |
| 62 * @type Function | 64 * @type Function |
| 63 */ | 65 */ |
| 64 dataSource: null, | 66 dataSource: null, |
| 65 | 67 |
| 66 /** | 68 /** |
| 69 * Function that's being called when something needs to be downloaded. |
| 70 * @type Function |
| 71 */ |
| 72 downloadCallback: null, |
| 73 |
| 74 /** |
| 67 * Maximal time interval that the checks can be left out until the soft | 75 * Maximal time interval that the checks can be left out until the soft |
| 68 * expiration interval increases. | 76 * expiration interval increases. |
| 69 * @type Integer | 77 * @type Integer |
| 70 */ | 78 */ |
| 71 maxAbsenceInterval: 1 * MILLIS_IN_DAY, | 79 maxAbsenceInterval: 1 * MILLIS_IN_DAY, |
| 72 | 80 |
| 73 /** | 81 /** |
| 74 * Minimal time interval before retrying a download after an error. | 82 * Minimal time interval before retrying a download after an error. |
| 75 * @type Integer | 83 * @type Integer |
| 76 */ | 84 */ |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 this.onExpirationChange(downloadable); | 151 this.onExpirationChange(downloadable); |
| 144 | 152 |
| 145 // Does that object need downloading? | 153 // Does that object need downloading? |
| 146 if (downloadable.softExpiration > now && downloadable.hardExpiration > now
) | 154 if (downloadable.softExpiration > now && downloadable.hardExpiration > now
) |
| 147 continue; | 155 continue; |
| 148 | 156 |
| 149 // Do not retry downloads too often | 157 // Do not retry downloads too often |
| 150 if (downloadable.lastError && now - downloadable.lastError < this.minRetry
Interval) | 158 if (downloadable.lastError && now - downloadable.lastError < this.minRetry
Interval) |
| 151 continue; | 159 continue; |
| 152 | 160 |
| 153 this._download(downloadable, 0); | 161 this.downloadCallback(downloadable, 0); |
| 154 } | 162 } |
| 155 }, | 163 }, |
| 156 | 164 |
| 157 /** | 165 /** |
| 158 * Stops the periodic checks. | 166 * Stops the periodic checks. |
| 159 */ | 167 */ |
| 160 cancel: function() | 168 cancel: function() |
| 161 { | 169 { |
| 162 this._timer.cancel(); | 170 this._timer.cancel(); |
| 163 }, | 171 }, |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 * @type Integer | 393 * @type Integer |
| 386 */ | 394 */ |
| 387 hardExpiration: 0, | 395 hardExpiration: 0, |
| 388 | 396 |
| 389 /** | 397 /** |
| 390 * Number indicating how often the object was downloaded. | 398 * Number indicating how often the object was downloaded. |
| 391 * @type Integer | 399 * @type Integer |
| 392 */ | 400 */ |
| 393 downloadCount: 0, | 401 downloadCount: 0, |
| 394 }; | 402 }; |
| OLD | NEW |