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

Side by Side Diff: lib/synchronizer.js

Issue 29869558: Issue 6741 - Use ES2015 classes in lib/synchronizer.js (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Aug. 30, 2018, 11:36 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 | « no previous file | no next file » | 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-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 18 matching lines...) Expand all
29 const {filterNotifier} = require("./filterNotifier"); 29 const {filterNotifier} = require("./filterNotifier");
30 const {Prefs} = require("prefs"); 30 const {Prefs} = require("prefs");
31 const {Subscription, 31 const {Subscription,
32 DownloadableSubscription} = require("./subscriptionClasses"); 32 DownloadableSubscription} = require("./subscriptionClasses");
33 33
34 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; 34 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE;
35 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; 35 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR;
36 const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY; 36 const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY;
37 37
38 /** 38 /**
39 * The object providing actual downloading functionality. 39 * Downloads filter subscriptions whenever necessary.
40 * @type {Downloader}
41 */ 40 */
42 let downloader = null; 41 class Synchronizer
43
44 /**
45 * This object is responsible for downloading filter subscriptions whenever
46 * necessary.
47 * @class
48 */
49 let Synchronizer = exports.Synchronizer =
50 { 42 {
51 /** 43 constructor()
52 * Called on module startup.
53 */
54 init()
55 { 44 {
56 downloader = new Downloader(this._getDownloadables.bind(this), 45 /**
57 INITIAL_DELAY, CHECK_INTERVAL); 46 * The object providing actual downloading functionality.
47 * @type {Downloader}
48 */
49 this._downloader = new Downloader(this._getDownloadables.bind(this),
50 INITIAL_DELAY, CHECK_INTERVAL);
58 onShutdown.add(() => 51 onShutdown.add(() =>
59 { 52 {
60 downloader.cancel(); 53 this._downloader.cancel();
61 }); 54 });
62 55
63 downloader.onExpirationChange = this._onExpirationChange.bind(this); 56 this._downloader.onExpirationChange = this._onExpirationChange.bind(this);
64 downloader.onDownloadStarted = this._onDownloadStarted.bind(this); 57 this._downloader.onDownloadStarted = this._onDownloadStarted.bind(this);
65 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this); 58 this._downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this);
66 downloader.onDownloadError = this._onDownloadError.bind(this); 59 this._downloader.onDownloadError = this._onDownloadError.bind(this);
67 }, 60 }
68 61
69 /** 62 /**
70 * Checks whether a subscription is currently being downloaded. 63 * Checks whether a subscription is currently being downloaded.
71 * @param {string} url URL of the subscription 64 * @param {string} url URL of the subscription
72 * @return {boolean} 65 * @returns {boolean}
73 */ 66 */
74 isExecuting(url) 67 isExecuting(url)
75 { 68 {
76 return downloader.isDownloading(url); 69 return this._downloader.isDownloading(url);
77 }, 70 }
78 71
79 /** 72 /**
80 * Starts the download of a subscription. 73 * Starts the download of a subscription.
81 * @param {DownloadableSubscription} subscription 74 * @param {DownloadableSubscription} subscription
82 * Subscription to be downloaded 75 * Subscription to be downloaded
83 * @param {boolean} manual 76 * @param {boolean} manual
84 * true for a manually started download (should not trigger fallback 77 * <code>true</code> for a manually started download (should not trigger
85 * requests) 78 * fallback requests)
86 */ 79 */
87 execute(subscription, manual) 80 execute(subscription, manual)
88 { 81 {
89 downloader.download(this._getDownloadable(subscription, manual)); 82 this._downloader.download(this._getDownloadable(subscription, manual));
90 }, 83 }
91 84
92 /** 85 /**
93 * Yields Downloadable instances for all subscriptions that can be downloaded. 86 * Yields {@link Downloadable} instances for all subscriptions that can be
87 * downloaded.
88 * @yields {Downloadable}
94 */ 89 */
95 *_getDownloadables() 90 *_getDownloadables()
96 { 91 {
97 if (!Prefs.subscriptions_autoupdate) 92 if (!Prefs.subscriptions_autoupdate)
98 return; 93 return;
99 94
100 for (let subscription of FilterStorage.subscriptions) 95 for (let subscription of FilterStorage.subscriptions)
101 { 96 {
102 if (subscription instanceof DownloadableSubscription) 97 if (subscription instanceof DownloadableSubscription)
103 yield this._getDownloadable(subscription, false); 98 yield this._getDownloadable(subscription, false);
104 } 99 }
105 }, 100 }
106 101
107 /** 102 /**
108 * Creates a Downloadable instance for a subscription. 103 * Creates a {@link Downloadable} instance for a subscription.
109 * @param {Subscription} subscription 104 * @param {Subscription} subscription
110 * @param {boolean} manual 105 * @param {boolean} manual
111 * @return {Downloadable} 106 * @returns {Downloadable}
112 */ 107 */
113 _getDownloadable(subscription, manual) 108 _getDownloadable(subscription, manual)
114 { 109 {
115 let result = new Downloadable(subscription.url); 110 let result = new Downloadable(subscription.url);
116 if (subscription.lastDownload != subscription.lastSuccess) 111 if (subscription.lastDownload != subscription.lastSuccess)
117 result.lastError = subscription.lastDownload * MILLIS_IN_SECOND; 112 result.lastError = subscription.lastDownload * MILLIS_IN_SECOND;
118 result.lastCheck = subscription.lastCheck * MILLIS_IN_SECOND; 113 result.lastCheck = subscription.lastCheck * MILLIS_IN_SECOND;
119 result.lastVersion = subscription.version; 114 result.lastVersion = subscription.version;
120 result.softExpiration = subscription.softExpiration * MILLIS_IN_SECOND; 115 result.softExpiration = subscription.softExpiration * MILLIS_IN_SECOND;
121 result.hardExpiration = subscription.expires * MILLIS_IN_SECOND; 116 result.hardExpiration = subscription.expires * MILLIS_IN_SECOND;
122 result.manual = manual; 117 result.manual = manual;
123 result.downloadCount = subscription.downloadCount; 118 result.downloadCount = subscription.downloadCount;
124 return result; 119 return result;
125 }, 120 }
126 121
127 _onExpirationChange(downloadable) 122 _onExpirationChange(downloadable)
128 { 123 {
129 let subscription = Subscription.fromURL(downloadable.url); 124 let subscription = Subscription.fromURL(downloadable.url);
130 subscription.lastCheck = Math.round( 125 subscription.lastCheck = Math.round(
131 downloadable.lastCheck / MILLIS_IN_SECOND 126 downloadable.lastCheck / MILLIS_IN_SECOND
132 ); 127 );
133 subscription.softExpiration = Math.round( 128 subscription.softExpiration = Math.round(
134 downloadable.softExpiration / MILLIS_IN_SECOND 129 downloadable.softExpiration / MILLIS_IN_SECOND
135 ); 130 );
136 subscription.expires = Math.round( 131 subscription.expires = Math.round(
137 downloadable.hardExpiration / MILLIS_IN_SECOND 132 downloadable.hardExpiration / MILLIS_IN_SECOND
138 ); 133 );
139 }, 134 }
140 135
141 _onDownloadStarted(downloadable) 136 _onDownloadStarted(downloadable)
142 { 137 {
143 let subscription = Subscription.fromURL(downloadable.url); 138 let subscription = Subscription.fromURL(downloadable.url);
144 filterNotifier.emit("subscription.downloading", subscription); 139 filterNotifier.emit("subscription.downloading", subscription);
145 }, 140 }
146 141
147 _onDownloadSuccess(downloadable, responseText, errorCallback, 142 _onDownloadSuccess(downloadable, responseText, errorCallback,
148 redirectCallback) 143 redirectCallback)
149 { 144 {
150 let lines = responseText.split(/[\r\n]+/); 145 let lines = responseText.split(/[\r\n]+/);
151 let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]); 146 let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]);
152 if (!headerMatch) 147 if (!headerMatch)
153 return errorCallback("synchronize_invalid_data"); 148 return errorCallback("synchronize_invalid_data");
154 let minVersion = headerMatch[1]; 149 let minVersion = headerMatch[1];
155 150
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 if (match[2]) 239 if (match[2])
245 expirationInterval = interval * MILLIS_IN_HOUR; 240 expirationInterval = interval * MILLIS_IN_HOUR;
246 else 241 else
247 expirationInterval = interval * MILLIS_IN_DAY; 242 expirationInterval = interval * MILLIS_IN_DAY;
248 } 243 }
249 } 244 }
250 245
251 let [ 246 let [
252 softExpiration, 247 softExpiration,
253 hardExpiration 248 hardExpiration
254 ] = downloader.processExpirationInterval(expirationInterval); 249 ] = this._downloader.processExpirationInterval(expirationInterval);
255 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); 250 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND);
256 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); 251 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND);
257 252
258 if (minVersion) 253 if (minVersion)
259 subscription.requiredVersion = minVersion; 254 subscription.requiredVersion = minVersion;
260 else 255 else
261 delete subscription.requiredVersion; 256 delete subscription.requiredVersion;
262 257
263 // Process filters 258 // Process filters
264 lines.shift(); 259 lines.shift();
265 let filters = []; 260 let filters = [];
266 for (let line of lines) 261 for (let line of lines)
267 { 262 {
268 line = Filter.normalize(line); 263 line = Filter.normalize(line);
269 if (line) 264 if (line)
270 filters.push(Filter.fromText(line)); 265 filters.push(Filter.fromText(line));
271 } 266 }
272 267
273 FilterStorage.updateSubscriptionFilters(subscription, filters); 268 FilterStorage.updateSubscriptionFilters(subscription, filters);
274 }, 269 }
275 270
276 _onDownloadError(downloadable, downloadURL, error, channelStatus, 271 _onDownloadError(downloadable, downloadURL, error, channelStatus,
277 responseStatus, redirectCallback) 272 responseStatus, redirectCallback)
278 { 273 {
279 let subscription = Subscription.fromURL(downloadable.url); 274 let subscription = Subscription.fromURL(downloadable.url);
280 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); 275 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND);
281 subscription.downloadStatus = error; 276 subscription.downloadStatus = error;
282 277
283 // Request fallback URL if necessary - for automatic updates only 278 // Request fallback URL if necessary - for automatic updates only
284 if (!downloadable.manual) 279 if (!downloadable.manual)
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 { 326 {
332 let data = "[Adblock]\n" + 327 let data = "[Adblock]\n" +
333 subscription.filters.map(f => f.text).join("\n"); 328 subscription.filters.map(f => f.text).join("\n");
334 redirectCallback("data:text/plain," + encodeURIComponent(data)); 329 redirectCallback("data:text/plain," + encodeURIComponent(data));
335 } 330 }
336 }, false); 331 }, false);
337 request.send(null); 332 request.send(null);
338 } 333 }
339 } 334 }
340 } 335 }
341 }; 336 }
342 Synchronizer.init(); 337
338 /**
339 * This object is responsible for downloading filter subscriptions whenever
340 * necessary.
341 * @type {Synchronizer}
342 */
343 let synchronizer = new Synchronizer();
344
345 exports.Synchronizer = synchronizer;
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld