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