| 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 10 matching lines...) Expand all Loading... |
| 21 * @fileOverview Downloads a set of URLs in regular time intervals. | 21 * @fileOverview Downloads a set of URLs in regular time intervals. |
| 22 */ | 22 */ |
| 23 | 23 |
| 24 const {Utils} = require("utils"); | 24 const {Utils} = require("utils"); |
| 25 | 25 |
| 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 let Downloader = | |
| 32 /** | 31 /** |
| 33 * Creates a new downloader instance. | 32 * Creates a new downloader instance. |
| 34 * @param {Function} dataSource | 33 * @param {Function} dataSource |
| 35 * Function that will yield downloadable objects on each check | 34 * Function that will yield downloadable objects on each check |
| 36 * @param {number} initialDelay | 35 * @param {number} initialDelay |
| 37 * Number of milliseconds to wait before the first check | 36 * Number of milliseconds to wait before the first check |
| 38 * @param {number} checkInterval | 37 * @param {number} checkInterval |
| 39 * Interval between the checks | 38 * Interval between the checks |
| 40 * @constructor | |
| 41 */ | 39 */ |
| 42 exports.Downloader = function(dataSource, initialDelay, checkInterval) | 40 class Downloader |
| 43 { | 41 { |
| 44 this.dataSource = dataSource; | 42 constructor(dataSource, initialDelay, checkInterval) |
| 45 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); | |
| 46 this._timer.initWithCallback(() => | |
| 47 { | 43 { |
| 48 this._timer.delay = checkInterval; | 44 /** |
| 49 this._doCheck(); | 45 * Timer triggering the downloads. |
| 50 }, initialDelay, Ci.nsITimer.TYPE_REPEATING_SLACK); | 46 * @type {nsITimer} |
| 51 this._downloading = new Set(); | 47 */ |
| 52 }; | 48 this._timer = null; |
| 53 Downloader.prototype = | |
| 54 { | |
| 55 /** | |
| 56 * Timer triggering the downloads. | |
| 57 * @type {nsITimer} | |
| 58 */ | |
| 59 _timer: null, | |
| 60 | 49 |
| 61 /** | 50 /** |
| 62 * Set containing the URLs of objects currently being downloaded. | 51 * Set containing the URLs of objects currently being downloaded. |
| 63 * @type {Set.<string>} | 52 * @type {Set.<string>} |
| 64 */ | 53 */ |
| 65 _downloading: null, | 54 this._downloading = null; |
| 66 | 55 |
| 67 /** | 56 /** |
| 68 * Function that will yield downloadable objects on each check. | 57 * Function that will yield downloadable objects on each check. |
| 69 * @type {Function} | 58 * @type {Function} |
| 70 */ | 59 */ |
| 71 dataSource: null, | 60 this.dataSource = null; |
| 72 | 61 |
| 73 /** | 62 /** |
| 74 * Maximal time interval that the checks can be left out until the soft | 63 * Maximal time interval that the checks can be left out until the soft |
| 75 * expiration interval increases. | 64 * expiration interval increases. |
| 76 * @type {number} | 65 * @type {number} |
| 77 */ | 66 */ |
| 78 maxAbsenceInterval: 1 * MILLIS_IN_DAY, | 67 this.maxAbsenceInterval = 1 * MILLIS_IN_DAY; |
| 79 | 68 |
| 80 /** | 69 /** |
| 81 * Minimal time interval before retrying a download after an error. | 70 * Minimal time interval before retrying a download after an error. |
| 82 * @type {number} | 71 * @type {number} |
| 83 */ | 72 */ |
| 84 minRetryInterval: 1 * MILLIS_IN_DAY, | 73 this.minRetryInterval = 1 * MILLIS_IN_DAY; |
| 85 | 74 |
| 86 /** | 75 /** |
| 87 * Maximal allowed expiration interval, larger expiration intervals will be | 76 * Maximal allowed expiration interval; larger expiration intervals will be |
| 88 * corrected. | 77 * corrected. |
| 89 * @type {number} | 78 * @type {number} |
| 90 */ | 79 */ |
| 91 maxExpirationInterval: 14 * MILLIS_IN_DAY, | 80 this.maxExpirationInterval = 14 * MILLIS_IN_DAY; |
| 92 | 81 |
| 93 /** | 82 /** |
| 94 * Maximal number of redirects before the download is considered as failed. | 83 * Maximal number of redirects before the download is considered as failed. |
| 95 * @type {number} | 84 * @type {number} |
| 96 */ | 85 */ |
| 97 maxRedirects: 5, | 86 this.maxRedirects = 5; |
| 98 | 87 |
| 99 /** | 88 /** |
| 100 * Called whenever expiration intervals for an object need to be adapted. | 89 * Called whenever expiration intervals for an object need to be adapted. |
| 101 * @type {Function} | 90 * @type {Function} |
| 102 */ | 91 */ |
| 103 onExpirationChange: null, | 92 this.onExpirationChange = null; |
| 104 | 93 |
| 105 /** | 94 /** |
| 106 * Callback to be triggered whenever a download starts. | 95 * Callback to be triggered whenever a download starts. |
| 107 * @type {Function} | 96 * @type {Function} |
| 108 */ | 97 */ |
| 109 onDownloadStarted: null, | 98 this.onDownloadStarted = null; |
| 110 | 99 |
| 111 /** | 100 /** |
| 112 * Callback to be triggered whenever a download finishes successfully. The | 101 * Callback to be triggered whenever a download finishes successfully. The |
| 113 * callback can return an error code to indicate that the data is wrong. | 102 * callback can return an error code to indicate that the data is wrong. |
| 114 * @type {Function} | 103 * @type {Function} |
| 115 */ | 104 */ |
| 116 onDownloadSuccess: null, | 105 this.onDownloadSuccess = null; |
| 117 | 106 |
| 118 /** | 107 /** |
| 119 * Callback to be triggered whenever a download fails. | 108 * Callback to be triggered whenever a download fails. |
| 120 * @type {Function} | 109 * @type {Function} |
| 121 */ | 110 */ |
| 122 onDownloadError: null, | 111 this.onDownloadError = null; |
| 112 |
| 113 |
| 114 this.dataSource = dataSource; |
| 115 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
| 116 this._timer.initWithCallback(() => |
| 117 { |
| 118 this._timer.delay = checkInterval; |
| 119 this._doCheck(); |
| 120 }, initialDelay, Ci.nsITimer.TYPE_REPEATING_SLACK); |
| 121 this._downloading = new Set(); |
| 122 } |
| 123 | 123 |
| 124 /** | 124 /** |
| 125 * Checks whether anything needs downloading. | 125 * Checks whether anything needs downloading. |
| 126 */ | 126 */ |
| 127 _doCheck() | 127 _doCheck() |
| 128 { | 128 { |
| 129 let now = Date.now(); | 129 let now = Date.now(); |
| 130 for (let downloadable of this.dataSource()) | 130 for (let downloadable of this.dataSource()) |
| 131 { | 131 { |
| 132 if (downloadable.lastCheck && | 132 if (downloadable.lastCheck && |
| (...skipping 26 matching lines...) Expand all Loading... |
| 159 | 159 |
| 160 // Do not retry downloads too often | 160 // Do not retry downloads too often |
| 161 if (downloadable.lastError && | 161 if (downloadable.lastError && |
| 162 now - downloadable.lastError < this.minRetryInterval) | 162 now - downloadable.lastError < this.minRetryInterval) |
| 163 { | 163 { |
| 164 continue; | 164 continue; |
| 165 } | 165 } |
| 166 | 166 |
| 167 this._download(downloadable, 0); | 167 this._download(downloadable, 0); |
| 168 } | 168 } |
| 169 }, | 169 } |
| 170 | 170 |
| 171 /** | 171 /** |
| 172 * Stops the periodic checks. | 172 * Stops the periodic checks. |
| 173 */ | 173 */ |
| 174 cancel() | 174 cancel() |
| 175 { | 175 { |
| 176 this._timer.cancel(); | 176 this._timer.cancel(); |
| 177 }, | 177 } |
| 178 | 178 |
| 179 /** | 179 /** |
| 180 * Checks whether an address is currently being downloaded. | 180 * Checks whether an address is currently being downloaded. |
| 181 * @param {string} url | 181 * @param {string} url |
| 182 * @return {boolean} | 182 * @return {boolean} |
| 183 */ | 183 */ |
| 184 isDownloading(url) | 184 isDownloading(url) |
| 185 { | 185 { |
| 186 return this._downloading.has(url); | 186 return this._downloading.has(url); |
| 187 }, | 187 } |
| 188 | 188 |
| 189 /** | 189 /** |
| 190 * Starts downloading for an object. | 190 * Starts downloading for an object. |
| 191 * @param {Downloadable} downloadable | 191 * @param {Downloadable} downloadable |
| 192 */ | 192 */ |
| 193 download(downloadable) | 193 download(downloadable) |
| 194 { | 194 { |
| 195 // Make sure to detach download from the current execution context | 195 // Make sure to detach download from the current execution context |
| 196 Utils.runAsync(this._download.bind(this, downloadable, 0)); | 196 Utils.runAsync(this._download.bind(this, downloadable, 0)); |
| 197 }, | 197 } |
| 198 | 198 |
| 199 /** | 199 /** |
| 200 * Generates the real download URL for an object by appending various | 200 * Generates the real download URL for an object by appending various |
| 201 * parameters. | 201 * parameters. |
| 202 * @param {Downloadable} downloadable | 202 * @param {Downloadable} downloadable |
| 203 * @return {string} | 203 * @return {string} |
| 204 */ | 204 */ |
| 205 getDownloadUrl(downloadable) | 205 getDownloadUrl(downloadable) |
| 206 { | 206 { |
| 207 const {addonName, addonVersion, application, applicationVersion, | 207 const {addonName, addonVersion, application, applicationVersion, |
| 208 platform, platformVersion} = require("info"); | 208 platform, platformVersion} = require("info"); |
| 209 let url = downloadable.redirectURL || downloadable.url; | 209 let url = downloadable.redirectURL || downloadable.url; |
| 210 if (url.includes("?")) | 210 if (url.includes("?")) |
| 211 url += "&"; | 211 url += "&"; |
| 212 else | 212 else |
| 213 url += "?"; | 213 url += "?"; |
| 214 // We limit the download count to 4+ to keep the request anonymized | 214 // We limit the download count to 4+ to keep the request anonymized |
| 215 let {downloadCount} = downloadable; | 215 let {downloadCount} = downloadable; |
| 216 if (downloadCount > 4) | 216 if (downloadCount > 4) |
| 217 downloadCount = "4+"; | 217 downloadCount = "4+"; |
| 218 url += "addonName=" + encodeURIComponent(addonName) + | 218 url += "addonName=" + encodeURIComponent(addonName) + |
| 219 "&addonVersion=" + encodeURIComponent(addonVersion) + | 219 "&addonVersion=" + encodeURIComponent(addonVersion) + |
| 220 "&application=" + encodeURIComponent(application) + | 220 "&application=" + encodeURIComponent(application) + |
| 221 "&applicationVersion=" + encodeURIComponent(applicationVersion) + | 221 "&applicationVersion=" + encodeURIComponent(applicationVersion) + |
| 222 "&platform=" + encodeURIComponent(platform) + | 222 "&platform=" + encodeURIComponent(platform) + |
| 223 "&platformVersion=" + encodeURIComponent(platformVersion) + | 223 "&platformVersion=" + encodeURIComponent(platformVersion) + |
| 224 "&lastVersion=" + encodeURIComponent(downloadable.lastVersion) + | 224 "&lastVersion=" + encodeURIComponent(downloadable.lastVersion) + |
| 225 "&downloadCount=" + encodeURIComponent(downloadCount); | 225 "&downloadCount=" + encodeURIComponent(downloadCount); |
| 226 return url; | 226 return url; |
| 227 }, | 227 } |
| 228 | 228 |
| 229 _download(downloadable, redirects) | 229 _download(downloadable, redirects) |
| 230 { | 230 { |
| 231 if (this.isDownloading(downloadable.url)) | 231 if (this.isDownloading(downloadable.url)) |
| 232 return; | 232 return; |
| 233 | 233 |
| 234 let downloadUrl = this.getDownloadUrl(downloadable); | 234 let downloadUrl = this.getDownloadUrl(downloadable); |
| 235 let request = null; | 235 let request = null; |
| 236 | 236 |
| 237 let errorCallback = function errorCallback(error) | 237 let errorCallback = function errorCallback(error) |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 } | 335 } |
| 336 } | 336 } |
| 337 ); | 337 ); |
| 338 }); | 338 }); |
| 339 | 339 |
| 340 request.send(null); | 340 request.send(null); |
| 341 | 341 |
| 342 this._downloading.add(downloadable.url); | 342 this._downloading.add(downloadable.url); |
| 343 if (this.onDownloadStarted) | 343 if (this.onDownloadStarted) |
| 344 this.onDownloadStarted(downloadable); | 344 this.onDownloadStarted(downloadable); |
| 345 }, | 345 } |
| 346 | 346 |
| 347 /** | 347 /** |
| 348 * 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 |
| 349 * expiration interval. | 349 * expiration interval. |
| 350 * @param {number} interval | 350 * @param {number} interval |
| 351 * @return {Array} soft and hard expiration interval | 351 * @return {Array} soft and hard expiration interval |
| 352 */ | 352 */ |
| 353 processExpirationInterval(interval) | 353 processExpirationInterval(interval) |
| 354 { | 354 { |
| 355 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval); | 355 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval); |
| 356 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8)); | 356 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8)); |
| 357 let hard = interval * 2; | 357 let hard = interval * 2; |
| 358 let now = Date.now(); | 358 let now = Date.now(); |
| 359 return [now + soft, now + hard]; | 359 return [now + soft, now + hard]; |
| 360 } | 360 } |
| 361 }; | 361 } |
| 362 exports.Downloader = Downloader; |
| 362 | 363 |
| 363 /** | 364 /** |
| 364 * An object that can be downloaded by the downloadable | 365 * An object that can be downloaded by the downloadable |
| 365 * @param {string} url URL that has to be requested for the object | 366 * @param {string} url URL that has to be requested for the object |
| 366 * @constructor | |
| 367 */ | 367 */ |
| 368 let Downloadable = exports.Downloadable = function Downloadable(url) | 368 class Downloadable |
| 369 { | 369 { |
| 370 this.url = url; | 370 constructor(url) |
| 371 }; | 371 { |
| 372 Downloadable.prototype = | 372 /** |
| 373 { | 373 * URL that has to be requested for the object. |
| 374 /** | 374 * @type {string} |
| 375 * URL that has to be requested for the object. | 375 */ |
| 376 * @type {string} | 376 this.url = null; |
| 377 */ | |
| 378 url: null, | |
| 379 | 377 |
| 380 /** | 378 /** |
| 381 * URL that the download was redirected to if any. | 379 * URL that the download was redirected to if any. |
| 382 * @type {string} | 380 * @type {string} |
| 383 */ | 381 */ |
| 384 redirectURL: null, | 382 this.redirectURL = null; |
| 385 | 383 |
| 386 /** | 384 /** |
| 387 * Time of last download error or 0 if the last download was successful. | 385 * Time of last download error or 0 if the last download was successful. |
| 388 * @type {number} | 386 * @type {number} |
| 389 */ | 387 */ |
| 390 lastError: 0, | 388 this.lastError = 0; |
| 391 | 389 |
| 392 /** | 390 /** |
| 393 * Time of last check whether the object needs downloading. | 391 * Time of last check whether the object needs downloading. |
| 394 * @type {number} | 392 * @type {number} |
| 395 */ | 393 */ |
| 396 lastCheck: 0, | 394 this.lastCheck = 0; |
| 397 | 395 |
| 398 /** | 396 /** |
| 399 * Object version corresponding to the last successful download. | 397 * Object version corresponding to the last successful download. |
| 400 * @type {number} | 398 * @type {number} |
| 401 */ | 399 */ |
| 402 lastVersion: 0, | 400 this.lastVersion = 0; |
| 403 | 401 |
| 404 /** | 402 /** |
| 405 * Soft expiration interval, will increase if no checks are performed for a | 403 * Soft expiration interval; will increase if no checks are performed for a |
| 406 * while. | 404 * while. |
| 407 * @type {number} | 405 * @type {number} |
| 408 */ | 406 */ |
| 409 softExpiration: 0, | 407 this.softExpiration = 0; |
| 410 | 408 |
| 411 /** | 409 /** |
| 412 * Hard expiration interval, this is fixed. | 410 * Hard expiration interval; this is fixed. |
| 413 * @type {number} | 411 * @type {number} |
| 414 */ | 412 */ |
| 415 hardExpiration: 0, | 413 this.hardExpiration = 0; |
| 416 | 414 |
| 417 /** | 415 /** |
| 418 * Number indicating how often the object was downloaded. | 416 * Number indicating how often the object was downloaded. |
| 419 * @type {number} | 417 * @type {number} |
| 420 */ | 418 */ |
| 421 downloadCount: 0 | 419 this.downloadCount = 0; |
| 422 }; | 420 this.url = url; |
| 421 } |
| 422 |
| 423 |
| 424 } |
| 425 exports.Downloadable = Downloadable; |
| OLD | NEW |