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

Side by Side Diff: lib/synchronizer.js

Issue 29375915: Issue 4878 - Start using ESLint for adblockpluscore (Closed)
Patch Set: Addressed Wladimir's comments Created March 8, 2017, 12:28 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/subscriptionClasses.js ('k') | test/.eslintrc.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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";
19
18 /** 20 /**
19 * @fileOverview Manages synchronization of filter subscriptions. 21 * @fileOverview Manages synchronization of filter subscriptions.
20 */ 22 */
21 23
22 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 24 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
23 Cu.import("resource://gre/modules/Services.jsm"); 25 Cu.import("resource://gre/modules/Services.jsm");
24 26
25 var {Downloader, Downloadable, 27 const {Downloader, Downloadable,
26 MILLIS_IN_SECOND, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require ("downloader"); 28 MILLIS_IN_SECOND, MILLIS_IN_MINUTE,
27 var {Filter, CommentFilter} = require("filterClasses"); 29 MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader");
28 var {FilterStorage} = require("filterStorage"); 30 const {Filter} = require("filterClasses");
29 var {FilterNotifier} = require("filterNotifier"); 31 const {FilterStorage} = require("filterStorage");
30 var {Prefs} = require("prefs"); 32 const {FilterNotifier} = require("filterNotifier");
31 var {Subscription, DownloadableSubscription} = require("subscriptionClasses"); 33 const {Prefs} = require("prefs");
32 var {Utils} = require("utils"); 34 const {Subscription, DownloadableSubscription} = require("subscriptionClasses");
35 const {Utils} = require("utils");
33 36
34 var INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; 37 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE;
35 var CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; 38 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
36 var DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY; 39 const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY;
37 40
38 /** 41 /**
39 * The object providing actual downloading functionality. 42 * The object providing actual downloading functionality.
40 * @type Downloader 43 * @type {Downloader}
41 */ 44 */
42 var downloader = null; 45 let downloader = null;
43 46
44 /** 47 /**
45 * This object is responsible for downloading filter subscriptions whenever 48 * This object is responsible for downloading filter subscriptions whenever
46 * necessary. 49 * necessary.
47 * @class 50 * @class
48 */ 51 */
49 var Synchronizer = exports.Synchronizer = 52 let Synchronizer = exports.Synchronizer =
50 { 53 {
51 /** 54 /**
52 * Called on module startup. 55 * Called on module startup.
53 */ 56 */
54 init: function() 57 init()
55 { 58 {
56 downloader = new Downloader(this._getDownloadables.bind(this), INITIAL_DELAY , CHECK_INTERVAL); 59 downloader = new Downloader(this._getDownloadables.bind(this),
57 onShutdown.add(function() 60 INITIAL_DELAY, CHECK_INTERVAL);
61 onShutdown.add(() =>
58 { 62 {
59 downloader.cancel(); 63 downloader.cancel();
60 }); 64 });
61 65
62 downloader.onExpirationChange = this._onExpirationChange.bind(this); 66 downloader.onExpirationChange = this._onExpirationChange.bind(this);
63 downloader.onDownloadStarted = this._onDownloadStarted.bind(this); 67 downloader.onDownloadStarted = this._onDownloadStarted.bind(this);
64 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this); 68 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this);
65 downloader.onDownloadError = this._onDownloadError.bind(this); 69 downloader.onDownloadError = this._onDownloadError.bind(this);
66 }, 70 },
67 71
68 /** 72 /**
69 * Checks whether a subscription is currently being downloaded. 73 * Checks whether a subscription is currently being downloaded.
70 * @param {String} url URL of the subscription 74 * @param {string} url URL of the subscription
71 * @return {Boolean} 75 * @return {boolean}
72 */ 76 */
73 isExecuting: function(url) 77 isExecuting(url)
74 { 78 {
75 return downloader.isDownloading(url); 79 return downloader.isDownloading(url);
76 }, 80 },
77 81
78 /** 82 /**
79 * Starts the download of a subscription. 83 * Starts the download of a subscription.
80 * @param {DownloadableSubscription} subscription Subscription to be download ed 84 * @param {DownloadableSubscription} subscription
81 * @param {Boolean} manual true for a manually started download (should not t rigger fallback requests) 85 * Subscription to be downloaded
86 * @param {boolean} manual
87 * true for a manually started download (should not trigger fallback
88 * requests)
82 */ 89 */
83 execute: function(subscription, manual) 90 execute(subscription, manual)
84 { 91 {
85 downloader.download(this._getDownloadable(subscription, manual)); 92 downloader.download(this._getDownloadable(subscription, manual));
86 }, 93 },
87 94
88 /** 95 /**
89 * Yields Downloadable instances for all subscriptions that can be downloaded. 96 * Yields Downloadable instances for all subscriptions that can be downloaded.
90 */ 97 */
91 _getDownloadables: function*() 98 *_getDownloadables()
92 { 99 {
93 if (!Prefs.subscriptions_autoupdate) 100 if (!Prefs.subscriptions_autoupdate)
94 return; 101 return;
95 102
96 for (let subscription of FilterStorage.subscriptions) 103 for (let subscription of FilterStorage.subscriptions)
97 { 104 {
98 if (subscription instanceof DownloadableSubscription) 105 if (subscription instanceof DownloadableSubscription)
99 yield this._getDownloadable(subscription, false); 106 yield this._getDownloadable(subscription, false);
100 } 107 }
101 }, 108 },
102 109
103 /** 110 /**
104 * Creates a Downloadable instance for a subscription. 111 * Creates a Downloadable instance for a subscription.
112 * @param {Subscription} subscription
113 * @param {boolean} manual
114 * @return {Downloadable}
105 */ 115 */
106 _getDownloadable: function(/**Subscription*/ subscription, /**Boolean*/ manual ) /**Downloadable*/ 116 _getDownloadable(subscription, manual)
107 { 117 {
108 let result = new Downloadable(subscription.url); 118 let result = new Downloadable(subscription.url);
109 if (subscription.lastDownload != subscription.lastSuccess) 119 if (subscription.lastDownload != subscription.lastSuccess)
110 result.lastError = subscription.lastDownload * MILLIS_IN_SECOND; 120 result.lastError = subscription.lastDownload * MILLIS_IN_SECOND;
111 result.lastCheck = subscription.lastCheck * MILLIS_IN_SECOND; 121 result.lastCheck = subscription.lastCheck * MILLIS_IN_SECOND;
112 result.lastVersion = subscription.version; 122 result.lastVersion = subscription.version;
113 result.softExpiration = subscription.softExpiration * MILLIS_IN_SECOND; 123 result.softExpiration = subscription.softExpiration * MILLIS_IN_SECOND;
114 result.hardExpiration = subscription.expires * MILLIS_IN_SECOND; 124 result.hardExpiration = subscription.expires * MILLIS_IN_SECOND;
115 result.manual = manual; 125 result.manual = manual;
116 result.downloadCount = subscription.downloadCount; 126 result.downloadCount = subscription.downloadCount;
117 return result; 127 return result;
118 }, 128 },
119 129
120 _onExpirationChange: function(downloadable) 130 _onExpirationChange(downloadable)
121 { 131 {
122 let subscription = Subscription.fromURL(downloadable.url); 132 let subscription = Subscription.fromURL(downloadable.url);
123 subscription.lastCheck = Math.round(downloadable.lastCheck / MILLIS_IN_SECON D); 133 subscription.lastCheck = Math.round(
124 subscription.softExpiration = Math.round(downloadable.softExpiration / MILLI S_IN_SECOND); 134 downloadable.lastCheck / MILLIS_IN_SECOND
125 subscription.expires = Math.round(downloadable.hardExpiration / MILLIS_IN_SE COND); 135 );
136 subscription.softExpiration = Math.round(
137 downloadable.softExpiration / MILLIS_IN_SECOND
138 );
139 subscription.expires = Math.round(
140 downloadable.hardExpiration / MILLIS_IN_SECOND
141 );
126 }, 142 },
127 143
128 _onDownloadStarted: function(downloadable) 144 _onDownloadStarted(downloadable)
129 { 145 {
130 let subscription = Subscription.fromURL(downloadable.url); 146 let subscription = Subscription.fromURL(downloadable.url);
131 FilterNotifier.triggerListeners("subscription.downloading", subscription); 147 FilterNotifier.triggerListeners("subscription.downloading", subscription);
132 }, 148 },
133 149
134 _onDownloadSuccess: function(downloadable, responseText, errorCallback, redire ctCallback) 150 _onDownloadSuccess(downloadable, responseText, errorCallback,
151 redirectCallback)
135 { 152 {
136 let lines = responseText.split(/[\r\n]+/); 153 let lines = responseText.split(/[\r\n]+/);
137 let match = /\[Adblock(?:\s*Plus\s*([\d\.]+)?)?\]/i.exec(lines[0]); 154 let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]);
138 if (!match) 155 if (!headerMatch)
139 return errorCallback("synchronize_invalid_data"); 156 return errorCallback("synchronize_invalid_data");
140 let minVersion = match[1]; 157 let minVersion = headerMatch[1];
141 158
142 // Don't remove parameter comments immediately but add them to a list first, 159 // Don't remove parameter comments immediately but add them to a list first,
143 // they need to be considered in the checksum calculation. 160 // they need to be considered in the checksum calculation.
144 let remove = []; 161 let remove = [];
145 let params = { 162 let params = {
146 redirect: null, 163 redirect: null,
147 homepage: null, 164 homepage: null,
148 title: null, 165 title: null,
149 version: null, 166 version: null,
150 expires: null 167 expires: null
(...skipping 17 matching lines...) Expand all
168 if (checksum && checksum != value.replace(/=+$/, "")) 185 if (checksum && checksum != value.replace(/=+$/, ""))
169 return errorCallback("synchronize_checksum_mismatch"); 186 return errorCallback("synchronize_checksum_mismatch");
170 } 187 }
171 } 188 }
172 } 189 }
173 190
174 if (params.redirect) 191 if (params.redirect)
175 return redirectCallback(params.redirect); 192 return redirectCallback(params.redirect);
176 193
177 // Handle redirects 194 // Handle redirects
178 let subscription = Subscription.fromURL(downloadable.redirectURL || download able.url); 195 let subscription = Subscription.fromURL(downloadable.redirectURL ||
179 if (downloadable.redirectURL && downloadable.redirectURL != downloadable.url ) 196 downloadable.url);
197 if (downloadable.redirectURL &&
198 downloadable.redirectURL != downloadable.url)
180 { 199 {
181 let oldSubscription = Subscription.fromURL(downloadable.url); 200 let oldSubscription = Subscription.fromURL(downloadable.url);
182 subscription.title = oldSubscription.title; 201 subscription.title = oldSubscription.title;
183 subscription.disabled = oldSubscription.disabled; 202 subscription.disabled = oldSubscription.disabled;
184 subscription.lastCheck = oldSubscription.lastCheck; 203 subscription.lastCheck = oldSubscription.lastCheck;
185 204
186 let listed = (oldSubscription.url in FilterStorage.knownSubscriptions); 205 let listed = (oldSubscription.url in FilterStorage.knownSubscriptions);
187 if (listed) 206 if (listed)
188 FilterStorage.removeSubscription(oldSubscription); 207 FilterStorage.removeSubscription(oldSubscription);
189 208
190 delete Subscription.knownSubscriptions[oldSubscription.url]; 209 delete Subscription.knownSubscriptions[oldSubscription.url];
191 210
192 if (listed) 211 if (listed)
193 FilterStorage.addSubscription(subscription); 212 FilterStorage.addSubscription(subscription);
194 } 213 }
195 214
196 // The download actually succeeded 215 // The download actually succeeded
197 subscription.lastSuccess = subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); 216 subscription.lastSuccess = subscription.lastDownload = Math.round(
217 Date.now() / MILLIS_IN_SECOND
218 );
198 subscription.downloadStatus = "synchronize_ok"; 219 subscription.downloadStatus = "synchronize_ok";
199 subscription.downloadCount = downloadable.downloadCount; 220 subscription.downloadCount = downloadable.downloadCount;
200 subscription.errors = 0; 221 subscription.errors = 0;
201 222
202 // Remove lines containing parameters 223 // Remove lines containing parameters
203 for (let i = remove.length - 1; i >= 0; i--) 224 for (let i = remove.length - 1; i >= 0; i--)
204 lines.splice(remove[i], 1); 225 lines.splice(remove[i], 1);
205 226
206 // Process parameters 227 // Process parameters
207 if (params.homepage) 228 if (params.homepage)
(...skipping 29 matching lines...) Expand all
237 if (match) 258 if (match)
238 { 259 {
239 let interval = parseInt(match[1], 10); 260 let interval = parseInt(match[1], 10);
240 if (match[2]) 261 if (match[2])
241 expirationInterval = interval * MILLIS_IN_HOUR; 262 expirationInterval = interval * MILLIS_IN_HOUR;
242 else 263 else
243 expirationInterval = interval * MILLIS_IN_DAY; 264 expirationInterval = interval * MILLIS_IN_DAY;
244 } 265 }
245 } 266 }
246 267
247 let [softExpiration, hardExpiration] = downloader.processExpirationInterval( expirationInterval); 268 let [
269 softExpiration,
270 hardExpiration
271 ] = downloader.processExpirationInterval(expirationInterval);
248 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); 272 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND);
249 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); 273 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND);
250 274
251 if (minVersion) 275 if (minVersion)
252 subscription.requiredVersion = minVersion; 276 subscription.requiredVersion = minVersion;
253 else 277 else
254 delete subscription.requiredVersion; 278 delete subscription.requiredVersion;
255 279
256 // Process filters 280 // Process filters
257 lines.shift(); 281 lines.shift();
258 let filters = []; 282 let filters = [];
259 for (let line of lines) 283 for (let line of lines)
260 { 284 {
261 line = Filter.normalize(line); 285 line = Filter.normalize(line);
262 if (line) 286 if (line)
263 filters.push(Filter.fromText(line)); 287 filters.push(Filter.fromText(line));
264 } 288 }
265 289
266 FilterStorage.updateSubscriptionFilters(subscription, filters); 290 FilterStorage.updateSubscriptionFilters(subscription, filters);
267 291
268 return undefined; 292 return undefined;
269 }, 293 },
270 294
271 _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re sponseStatus, redirectCallback) 295 _onDownloadError(downloadable, downloadURL, error, channelStatus,
296 responseStatus, redirectCallback)
272 { 297 {
273 let subscription = Subscription.fromURL(downloadable.url); 298 let subscription = Subscription.fromURL(downloadable.url);
274 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); 299 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND);
275 subscription.downloadStatus = error; 300 subscription.downloadStatus = error;
276 301
277 // Request fallback URL if necessary - for automatic updates only 302 // Request fallback URL if necessary - for automatic updates only
278 if (!downloadable.manual) 303 if (!downloadable.manual)
279 { 304 {
280 subscription.errors++; 305 subscription.errors++;
281 306
282 if (redirectCallback && subscription.errors >= Prefs.subscriptions_fallbac kerrors && /^https?:\/\//i.test(subscription.url)) 307 if (redirectCallback &&
308 subscription.errors >= Prefs.subscriptions_fallbackerrors &&
309 /^https?:\/\//i.test(subscription.url))
283 { 310 {
284 subscription.errors = 0; 311 subscription.errors = 0;
285 312
286 let fallbackURL = Prefs.subscriptions_fallbackurl; 313 let fallbackURL = Prefs.subscriptions_fallbackurl;
287 let {addonVersion} = require("info"); 314 const {addonVersion} = require("info");
288 fallbackURL = fallbackURL.replace(/%VERSION%/g, encodeURIComponent(addon Version)); 315 fallbackURL = fallbackURL.replace(/%VERSION%/g,
289 fallbackURL = fallbackURL.replace(/%SUBSCRIPTION%/g, encodeURIComponent( subscription.url)); 316 encodeURIComponent(addonVersion));
290 fallbackURL = fallbackURL.replace(/%URL%/g, encodeURIComponent(downloadU RL)); 317 fallbackURL = fallbackURL.replace(/%SUBSCRIPTION%/g,
291 fallbackURL = fallbackURL.replace(/%ERROR%/g, encodeURIComponent(error)) ; 318 encodeURIComponent(subscription.url));
292 fallbackURL = fallbackURL.replace(/%CHANNELSTATUS%/g, encodeURIComponent (channelStatus)); 319 fallbackURL = fallbackURL.replace(/%URL%/g,
293 fallbackURL = fallbackURL.replace(/%RESPONSESTATUS%/g, encodeURIComponen t(responseStatus)); 320 encodeURIComponent(downloadURL));
321 fallbackURL = fallbackURL.replace(/%ERROR%/g,
322 encodeURIComponent(error));
323 fallbackURL = fallbackURL.replace(/%CHANNELSTATUS%/g,
324 encodeURIComponent(channelStatus));
325 fallbackURL = fallbackURL.replace(/%RESPONSESTATUS%/g,
326 encodeURIComponent(responseStatus));
294 327
295 let request = new XMLHttpRequest(); 328 let request = new XMLHttpRequest();
296 request.mozBackgroundRequest = true; 329 request.mozBackgroundRequest = true;
297 request.open("GET", fallbackURL); 330 request.open("GET", fallbackURL);
298 request.overrideMimeType("text/plain"); 331 request.overrideMimeType("text/plain");
299 request.channel.loadFlags = request.channel.loadFlags | 332 request.channel.loadFlags = request.channel.loadFlags |
300 request.channel.INHIBIT_CACHING | 333 request.channel.INHIBIT_CACHING |
301 request.channel.VALIDATE_ALWAYS; 334 request.channel.VALIDATE_ALWAYS;
302 request.addEventListener("load", function(ev) 335 request.addEventListener("load", ev =>
303 { 336 {
304 if (onShutdown.done) 337 if (onShutdown.done)
305 return; 338 return;
306 339
307 if (!(subscription.url in FilterStorage.knownSubscriptions)) 340 if (!(subscription.url in FilterStorage.knownSubscriptions))
308 return; 341 return;
309 342
310 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText); 343 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText);
311 if (match && match[1] == "301" && match[2] && /^https?:\/\//i.test(mat ch[2])) // Moved permanently 344 if (match && match[1] == "301" && // Moved permanently
345 match[2] && /^https?:\/\//i.test(match[2]))
346 {
312 redirectCallback(match[2]); 347 redirectCallback(match[2]);
313 else if (match && match[1] == "410") // Gone 348 }
349 else if (match && match[1] == "410") // Gone
314 { 350 {
315 let data = "[Adblock]\n" + subscription.filters.map((f) => f.text).j oin("\n"); 351 let data = "[Adblock]\n" +
352 subscription.filters.map(f => f.text).join("\n");
316 redirectCallback("data:text/plain," + encodeURIComponent(data)); 353 redirectCallback("data:text/plain," + encodeURIComponent(data));
317 } 354 }
318 }, false); 355 }, false);
319 request.send(null); 356 request.send(null);
320 } 357 }
321 } 358 }
322 }, 359 }
323 }; 360 };
324 Synchronizer.init(); 361 Synchronizer.init();
OLDNEW
« no previous file with comments | « lib/subscriptionClasses.js ('k') | test/.eslintrc.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld