Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/downloader.js

Issue 29859565: Issue 6741 - Use ES2015 classes in lib/downloader.js (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Remove excess whitespace Created Aug. 20, 2018, 11:15 p.m.
Right Patch Set: Address PS7 comments Created Aug. 27, 2018, 4:16 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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.
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
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}
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
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 this.dataSource = dataSource;
114 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); 105 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
115 this._timer.initWithCallback(() => 106 this._timer.initWithCallback(() =>
116 { 107 {
117 this._timer.delay = checkInterval; 108 this._timer.delay = checkInterval;
118 this._doCheck(); 109 this._doCheck();
119 }, 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 */
120 this._downloading = new Set(); 116 this._downloading = new Set();
121 } 117 }
122 118
123 /** 119 /**
124 * Checks whether anything needs downloading. 120 * Checks whether anything needs downloading.
125 */ 121 */
126 _doCheck() 122 _doCheck()
127 { 123 {
128 let now = Date.now(); 124 let now = Date.now();
129 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
171 * Stops the periodic checks. 167 * Stops the periodic checks.
172 */ 168 */
173 cancel() 169 cancel()
174 { 170 {
175 this._timer.cancel(); 171 this._timer.cancel();
176 } 172 }
177 173
178 /** 174 /**
179 * Checks whether an address is currently being downloaded. 175 * Checks whether an address is currently being downloaded.
180 * @param {string} url 176 * @param {string} url
181 * @return {boolean} 177 * @returns {boolean}
182 */ 178 */
183 isDownloading(url) 179 isDownloading(url)
184 { 180 {
185 return this._downloading.has(url); 181 return this._downloading.has(url);
186 } 182 }
187 183
188 /** 184 /**
189 * Starts downloading for an object. 185 * Starts downloading for an object.
190 * @param {Downloadable} downloadable 186 * @param {Downloadable} downloadable
191 */ 187 */
192 download(downloadable) 188 download(downloadable)
193 { 189 {
194 // Make sure to detach download from the current execution context 190 // Make sure to detach download from the current execution context
195 Utils.runAsync(this._download.bind(this, downloadable, 0)); 191 Utils.runAsync(this._download.bind(this, downloadable, 0));
196 } 192 }
197 193
198 /** 194 /**
199 * Generates the real download URL for an object by appending various 195 * Generates the real download URL for an object by appending various
200 * parameters. 196 * parameters.
201 * @param {Downloadable} downloadable 197 * @param {Downloadable} downloadable
202 * @return {string} 198 * @returns {string}
203 */ 199 */
204 getDownloadUrl(downloadable) 200 getDownloadUrl(downloadable)
205 { 201 {
206 const {addonName, addonVersion, application, applicationVersion, 202 const {addonName, addonVersion, application, applicationVersion,
207 platform, platformVersion} = require("info"); 203 platform, platformVersion} = require("info");
208 let url = downloadable.redirectURL || downloadable.url; 204 let url = downloadable.redirectURL || downloadable.url;
209 if (url.includes("?")) 205 if (url.includes("?"))
210 url += "&"; 206 url += "&";
211 else 207 else
212 url += "?"; 208 url += "?";
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
340 336
341 this._downloading.add(downloadable.url); 337 this._downloading.add(downloadable.url);
342 if (this.onDownloadStarted) 338 if (this.onDownloadStarted)
343 this.onDownloadStarted(downloadable); 339 this.onDownloadStarted(downloadable);
344 } 340 }
345 341
346 /** 342 /**
347 * 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
348 * expiration interval. 344 * expiration interval.
349 * @param {number} interval 345 * @param {number} interval
350 * @return {Array} soft and hard expiration interval 346 * @returns {Array.<number>} soft and hard expiration interval
351 */ 347 */
352 processExpirationInterval(interval) 348 processExpirationInterval(interval)
353 { 349 {
354 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval); 350 interval = Math.min(Math.max(interval, 0), this.maxExpirationInterval);
355 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8)); 351 let soft = Math.round(interval * (Math.random() * 0.4 + 0.8));
356 let hard = interval * 2; 352 let hard = interval * 2;
357 let now = Date.now(); 353 let now = Date.now();
358 return [now + soft, now + hard]; 354 return [now + soft, now + hard];
359 } 355 }
360 } 356 }
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.
357
361 exports.Downloader = Downloader; 358 exports.Downloader = Downloader;
362 359
363 /**
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
366 */
367 class Downloadable 360 class Downloadable
368 { 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 */
369 constructor(url) 366 constructor(url)
370 { 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
371 /** 411 /**
372 * URL that has to be requested for the object. 412 * URL that has to be requested for the object.
373 * @type {string} 413 * @type {string}
374 */ 414 */
375 this.url = null;
376
377 /**
378 * URL that the download was redirected to if any.
379 * @type {string}
380 */
381 this.redirectURL = null;
382
383 /**
384 * Time of last download error or 0 if the last download was successful.
385 * @type {number}
386 */
387 this.lastError = 0;
388
389 /**
390 * Time of last check whether the object needs downloading.
391 * @type {number}
392 */
393 this.lastCheck = 0;
394
395 /**
396 * Object version corresponding to the last successful download.
397 * @type {number}
398 */
399 this.lastVersion = 0;
400
401 /**
402 * Soft expiration interval; will increase if no checks are performed for a
403 * while.
404 * @type {number}
405 */
406 this.softExpiration = 0;
407
408 /**
409 * Hard expiration interval; this is fixed.
410 * @type {number}
411 */
412 this.hardExpiration = 0;
413
414 /**
415 * Number indicating how often the object was downloaded.
416 * @type {number}
417 */
418 this.downloadCount = 0;
419 this.url = url; 415 this.url = url;
420 } 416 }
421 } 417 }
418
422 exports.Downloadable = Downloadable; 419 exports.Downloadable = Downloadable;
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld