Left: | ||
Right: |
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. |
Manish Jethani
2018/08/21 03:56:02
This JSDoc comment is in fact for the constructor;
Jon Sonesen
2018/08/21 19:10:01
Acknowledged. I wasn't sure about that so thanks f
| |
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} |
Manish Jethani
2018/08/21 03:56:02
So you see there was always an inconsistency betwe
Jon Sonesen
2018/08/21 19:10:01
Yeah, I also wasn't sure about that either and I l
| |
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 this.dataSource = dataSource; | |
114 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); | |
115 this._timer.initWithCallback(() => | |
116 { | |
117 this._timer.delay = checkInterval; | |
118 this._doCheck(); | |
119 }, initialDelay, Ci.nsITimer.TYPE_REPEATING_SLACK); | |
120 this._downloading = new Set(); | |
121 } | |
123 | 122 |
124 /** | 123 /** |
125 * Checks whether anything needs downloading. | 124 * Checks whether anything needs downloading. |
126 */ | 125 */ |
127 _doCheck() | 126 _doCheck() |
128 { | 127 { |
129 let now = Date.now(); | 128 let now = Date.now(); |
130 for (let downloadable of this.dataSource()) | 129 for (let downloadable of this.dataSource()) |
131 { | 130 { |
132 if (downloadable.lastCheck && | 131 if (downloadable.lastCheck && |
(...skipping 26 matching lines...) Expand all Loading... | |
159 | 158 |
160 // Do not retry downloads too often | 159 // Do not retry downloads too often |
161 if (downloadable.lastError && | 160 if (downloadable.lastError && |
162 now - downloadable.lastError < this.minRetryInterval) | 161 now - downloadable.lastError < this.minRetryInterval) |
163 { | 162 { |
164 continue; | 163 continue; |
165 } | 164 } |
166 | 165 |
167 this._download(downloadable, 0); | 166 this._download(downloadable, 0); |
168 } | 167 } |
169 }, | 168 } |
170 | 169 |
171 /** | 170 /** |
172 * Stops the periodic checks. | 171 * Stops the periodic checks. |
173 */ | 172 */ |
174 cancel() | 173 cancel() |
175 { | 174 { |
176 this._timer.cancel(); | 175 this._timer.cancel(); |
177 }, | 176 } |
178 | 177 |
179 /** | 178 /** |
180 * Checks whether an address is currently being downloaded. | 179 * Checks whether an address is currently being downloaded. |
181 * @param {string} url | 180 * @param {string} url |
182 * @return {boolean} | 181 * @return {boolean} |
183 */ | 182 */ |
184 isDownloading(url) | 183 isDownloading(url) |
185 { | 184 { |
186 return this._downloading.has(url); | 185 return this._downloading.has(url); |
187 }, | 186 } |
188 | 187 |
189 /** | 188 /** |
190 * Starts downloading for an object. | 189 * Starts downloading for an object. |
191 * @param {Downloadable} downloadable | 190 * @param {Downloadable} downloadable |
192 */ | 191 */ |
193 download(downloadable) | 192 download(downloadable) |
194 { | 193 { |
195 // Make sure to detach download from the current execution context | 194 // Make sure to detach download from the current execution context |
196 Utils.runAsync(this._download.bind(this, downloadable, 0)); | 195 Utils.runAsync(this._download.bind(this, downloadable, 0)); |
197 }, | 196 } |
198 | 197 |
199 /** | 198 /** |
200 * Generates the real download URL for an object by appending various | 199 * Generates the real download URL for an object by appending various |
201 * parameters. | 200 * parameters. |
202 * @param {Downloadable} downloadable | 201 * @param {Downloadable} downloadable |
203 * @return {string} | 202 * @return {string} |
204 */ | 203 */ |
205 getDownloadUrl(downloadable) | 204 getDownloadUrl(downloadable) |
206 { | 205 { |
207 const {addonName, addonVersion, application, applicationVersion, | 206 const {addonName, addonVersion, application, applicationVersion, |
208 platform, platformVersion} = require("info"); | 207 platform, platformVersion} = require("info"); |
209 let url = downloadable.redirectURL || downloadable.url; | 208 let url = downloadable.redirectURL || downloadable.url; |
210 if (url.includes("?")) | 209 if (url.includes("?")) |
211 url += "&"; | 210 url += "&"; |
212 else | 211 else |
213 url += "?"; | 212 url += "?"; |
214 // We limit the download count to 4+ to keep the request anonymized | 213 // We limit the download count to 4+ to keep the request anonymized |
215 let {downloadCount} = downloadable; | 214 let {downloadCount} = downloadable; |
216 if (downloadCount > 4) | 215 if (downloadCount > 4) |
217 downloadCount = "4+"; | 216 downloadCount = "4+"; |
218 url += "addonName=" + encodeURIComponent(addonName) + | 217 url += "addonName=" + encodeURIComponent(addonName) + |
219 "&addonVersion=" + encodeURIComponent(addonVersion) + | 218 "&addonVersion=" + encodeURIComponent(addonVersion) + |
220 "&application=" + encodeURIComponent(application) + | 219 "&application=" + encodeURIComponent(application) + |
221 "&applicationVersion=" + encodeURIComponent(applicationVersion) + | 220 "&applicationVersion=" + encodeURIComponent(applicationVersion) + |
222 "&platform=" + encodeURIComponent(platform) + | 221 "&platform=" + encodeURIComponent(platform) + |
223 "&platformVersion=" + encodeURIComponent(platformVersion) + | 222 "&platformVersion=" + encodeURIComponent(platformVersion) + |
224 "&lastVersion=" + encodeURIComponent(downloadable.lastVersion) + | 223 "&lastVersion=" + encodeURIComponent(downloadable.lastVersion) + |
225 "&downloadCount=" + encodeURIComponent(downloadCount); | 224 "&downloadCount=" + encodeURIComponent(downloadCount); |
226 return url; | 225 return url; |
227 }, | 226 } |
228 | 227 |
229 _download(downloadable, redirects) | 228 _download(downloadable, redirects) |
230 { | 229 { |
231 if (this.isDownloading(downloadable.url)) | 230 if (this.isDownloading(downloadable.url)) |
232 return; | 231 return; |
233 | 232 |
234 let downloadUrl = this.getDownloadUrl(downloadable); | 233 let downloadUrl = this.getDownloadUrl(downloadable); |
235 let request = null; | 234 let request = null; |
236 | 235 |
237 let errorCallback = function errorCallback(error) | 236 let errorCallback = function errorCallback(error) |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 } | 334 } |
336 } | 335 } |
337 ); | 336 ); |
338 }); | 337 }); |
339 | 338 |
340 request.send(null); | 339 request.send(null); |
341 | 340 |
342 this._downloading.add(downloadable.url); | 341 this._downloading.add(downloadable.url); |
343 if (this.onDownloadStarted) | 342 if (this.onDownloadStarted) |
344 this.onDownloadStarted(downloadable); | 343 this.onDownloadStarted(downloadable); |
345 }, | 344 } |
346 | 345 |
347 /** | 346 /** |
348 * Produces a soft and a hard expiration interval for a given supplied | 347 * Produces a soft and a hard expiration interval for a given supplied |
349 * expiration interval. | 348 * expiration interval. |
350 * @param {number} interval | 349 * @param {number} interval |
351 * @return {Array} soft and hard expiration interval | 350 * @return {Array} soft and hard expiration interval |
352 */ | 351 */ |
353 processExpirationInterval(interval) | 352 processExpirationInterval(interval) |
354 { | 353 { |
355 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval); | 354 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval); |
356 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8)); | 355 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8)); |
357 let hard = interval * 2; | 356 let hard = interval * 2; |
358 let now = Date.now(); | 357 let now = Date.now(); |
359 return [now + soft, now + hard]; | 358 return [now + soft, now + hard]; |
360 } | 359 } |
361 }; | 360 } |
Manish Jethani
2018/08/21 03:56:02
Blank line after `}`
Jon Sonesen
2018/08/21 19:10:01
Do you mean insert a blank line here?
Manish Jethani
2018/08/22 06:13:34
Yes, I meant we should have a blank line after `cl
Jon Sonesen
2018/08/22 18:18:50
Done.
| |
361 exports.Downloader = Downloader; | |
362 | 362 |
363 /** | 363 /** |
364 * An object that can be downloaded by the downloadable | 364 * An object that can be downloaded by the downloadable |
Manish Jethani
2018/08/21 03:56:02
Same as above, this is JSDoc for the constructor.
Jon Sonesen
2018/08/21 19:10:00
Acknowledged.
| |
365 * @param {string} url URL that has to be requested for the object | 365 * @param {string} url URL that has to be requested for the object |
366 * @constructor | |
367 */ | 366 */ |
368 let Downloadable = exports.Downloadable = function Downloadable(url) | 367 class Downloadable |
369 { | 368 { |
370 this.url = url; | 369 constructor(url) |
371 }; | 370 { |
372 Downloadable.prototype = | 371 /** |
373 { | 372 * URL that has to be requested for the object. |
374 /** | 373 * @type {string} |
375 * URL that has to be requested for the object. | 374 */ |
376 * @type {string} | 375 this.url = null; |
377 */ | |
378 url: null, | |
379 | 376 |
380 /** | 377 /** |
381 * URL that the download was redirected to if any. | 378 * URL that the download was redirected to if any. |
382 * @type {string} | 379 * @type {string} |
383 */ | 380 */ |
384 redirectURL: null, | 381 this.redirectURL = null; |
385 | 382 |
386 /** | 383 /** |
387 * Time of last download error or 0 if the last download was successful. | 384 * Time of last download error or 0 if the last download was successful. |
388 * @type {number} | 385 * @type {number} |
389 */ | 386 */ |
390 lastError: 0, | 387 this.lastError = 0; |
391 | 388 |
392 /** | 389 /** |
393 * Time of last check whether the object needs downloading. | 390 * Time of last check whether the object needs downloading. |
394 * @type {number} | 391 * @type {number} |
395 */ | 392 */ |
396 lastCheck: 0, | 393 this.lastCheck = 0; |
397 | 394 |
398 /** | 395 /** |
399 * Object version corresponding to the last successful download. | 396 * Object version corresponding to the last successful download. |
400 * @type {number} | 397 * @type {number} |
401 */ | 398 */ |
402 lastVersion: 0, | 399 this.lastVersion = 0; |
403 | 400 |
404 /** | 401 /** |
405 * Soft expiration interval, will increase if no checks are performed for a | 402 * Soft expiration interval; will increase if no checks are performed for a |
406 * while. | 403 * while. |
407 * @type {number} | 404 * @type {number} |
408 */ | 405 */ |
409 softExpiration: 0, | 406 this.softExpiration = 0; |
410 | 407 |
411 /** | 408 /** |
412 * Hard expiration interval, this is fixed. | 409 * Hard expiration interval; this is fixed. |
413 * @type {number} | 410 * @type {number} |
414 */ | 411 */ |
415 hardExpiration: 0, | 412 this.hardExpiration = 0; |
416 | 413 |
417 /** | 414 /** |
418 * Number indicating how often the object was downloaded. | 415 * Number indicating how often the object was downloaded. |
419 * @type {number} | 416 * @type {number} |
420 */ | 417 */ |
421 downloadCount: 0 | 418 this.downloadCount = 0; |
422 }; | 419 this.url = url; |
420 } | |
421 } | |
422 exports.Downloadable = Downloadable; | |
OLD | NEW |