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

Delta Between Two Patch Sets: lib/downloader.js

Issue 29375915: Issue 4878 - Start using ESLint for adblockpluscore (Closed)
Left Patch Set: Disable no-console for the tests Created Feb. 21, 2017, 11:25 a.m.
Right Patch Set: Removed unused imports Created March 15, 2017, 3:11 a.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 | « lib/coreUtils.js ('k') | lib/elemHide.js » ('j') | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
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 let MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000; 26 const MILLIS_IN_SECOND = exports.MILLIS_IN_SECOND = 1000;
27 let 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 let 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 let 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 let Downloader =
32 /** 32 /**
33 * Creates a new downloader instance. 33 * Creates a new downloader instance.
34 * @param {Function} dataSource Function that will yield downloadable objects 34 * @param {Function} dataSource
35 * on each check 35 * Function that will yield downloadable objects on each check
36 * @param {number} initialDelay Number of milliseconds to wait before the 36 * @param {number} initialDelay
37 * first check 37 * Number of milliseconds to wait before the first check
38 * @param {number} checkInterval Interval between the checks 38 * @param {number} checkInterval
39 * Interval between the checks
39 * @constructor 40 * @constructor
40 */ 41 */
41 exports.Downloader = function(dataSource, initialDelay, checkInterval) 42 exports.Downloader = function(dataSource, initialDelay, checkInterval)
42 { 43 {
43 this.dataSource = dataSource; 44 this.dataSource = dataSource;
44 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); 45 this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
45 this._timer.initWithCallback(() => 46 this._timer.initWithCallback(() =>
46 { 47 {
47 this._timer.delay = checkInterval; 48 this._timer.delay = checkInterval;
48 this._doCheck(); 49 this._doCheck();
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 * Checks whether anything needs downloading. 124 * Checks whether anything needs downloading.
124 */ 125 */
125 _doCheck() 126 _doCheck()
126 { 127 {
127 let now = Date.now(); 128 let now = Date.now();
128 for (let downloadable of this.dataSource()) 129 for (let downloadable of this.dataSource())
129 { 130 {
130 if (downloadable.lastCheck && 131 if (downloadable.lastCheck &&
131 now - downloadable.lastCheck > this.maxAbsenceInterval) 132 now - downloadable.lastCheck > this.maxAbsenceInterval)
132 { 133 {
133 // No checks for a long time interval - user must have been 134 // No checks for a long time interval - user must have been offline,
134 // offline, e.g. during a weekend. Increase soft expiration 135 // e.g. during a weekend. Increase soft expiration to prevent load
135 // to prevent load peaks on the server. 136 // peaks on the server.
136 downloadable.softExpiration += now - downloadable.lastCheck; 137 downloadable.softExpiration += now - downloadable.lastCheck;
137 } 138 }
138 downloadable.lastCheck = now; 139 downloadable.lastCheck = now;
139 140
140 // Sanity check: do expiration times make sense? Make sure people changing 141 // Sanity check: do expiration times make sense? Make sure people changing
141 // system clock don't get stuck with outdated subscriptions. 142 // system clock don't get stuck with outdated subscriptions.
142 if (downloadable.hardExpiration - now > this.maxExpirationInterval) 143 if (downloadable.hardExpiration - now > this.maxExpirationInterval)
143 downloadable.hardExpiration = now + this.maxExpirationInterval; 144 downloadable.hardExpiration = now + this.maxExpirationInterval;
144 if (downloadable.softExpiration - now > this.maxExpirationInterval) 145 if (downloadable.softExpiration - now > this.maxExpirationInterval)
145 downloadable.softExpiration = now + this.maxExpirationInterval; 146 downloadable.softExpiration = now + this.maxExpirationInterval;
146 147
147 // Notify the caller about changes to expiration parameters 148 // Notify the caller about changes to expiration parameters
148 if (this.onExpirationChange) 149 if (this.onExpirationChange)
149 this.onExpirationChange(downloadable); 150 this.onExpirationChange(downloadable);
150 151
151 // Does that object need downloading? 152 // Does that object need downloading?
152 if (downloadable.softExpiration > now && 153 if (downloadable.softExpiration > now &&
153 downloadable.hardExpiration > now) 154 downloadable.hardExpiration > now)
155 {
154 continue; 156 continue;
157 }
155 158
156 // Do not retry downloads too often 159 // Do not retry downloads too often
157 if (downloadable.lastError && 160 if (downloadable.lastError &&
158 now - downloadable.lastError < this.minRetryInterval) 161 now - downloadable.lastError < this.minRetryInterval)
162 {
159 continue; 163 continue;
164 }
160 165
161 this._download(downloadable, 0); 166 this._download(downloadable, 0);
162 } 167 }
163 }, 168 },
164 169
165 /** 170 /**
166 * Stops the periodic checks. 171 * Stops the periodic checks.
167 */ 172 */
168 cancel() 173 cancel()
169 { 174 {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 "Download address: " + downloadUrl + "\n" + 249 "Download address: " + downloadUrl + "\n" +
245 "Channel status: " + channelStatus + "\n" + 250 "Channel status: " + channelStatus + "\n" +
246 "Server response: " + responseStatus); 251 "Server response: " + responseStatus);
247 252
248 if (this.onDownloadError) 253 if (this.onDownloadError)
249 { 254 {
250 // Allow one extra redirect if the error handler gives us a redirect URL 255 // Allow one extra redirect if the error handler gives us a redirect URL
251 let redirectCallback = null; 256 let redirectCallback = null;
252 if (redirects <= this.maxRedirects) 257 if (redirects <= this.maxRedirects)
253 { 258 {
254 redirectCallback = (url) => 259 redirectCallback = url =>
255 { 260 {
256 downloadable.redirectURL = url; 261 downloadable.redirectURL = url;
257 this._download(downloadable, redirects + 1); 262 this._download(downloadable, redirects + 1);
258 }; 263 };
259 } 264 }
260 265
261 this.onDownloadError(downloadable, downloadUrl, error, channelStatus, 266 this.onDownloadError(downloadable, downloadUrl, error, channelStatus,
262 responseStatus, redirectCallback); 267 responseStatus, redirectCallback);
263 } 268 }
264 }.bind(this); 269 }.bind(this);
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 * @type {number} 412 * @type {number}
408 */ 413 */
409 hardExpiration: 0, 414 hardExpiration: 0,
410 415
411 /** 416 /**
412 * Number indicating how often the object was downloaded. 417 * Number indicating how often the object was downloaded.
413 * @type {number} 418 * @type {number}
414 */ 419 */
415 downloadCount: 0 420 downloadCount: 0
416 }; 421 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld