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