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