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