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 |
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 Manages synchronization of filter subscriptions. | 21 * @fileOverview Manages synchronization of filter subscriptions. |
22 */ | 22 */ |
23 | 23 |
24 const {Downloader, Downloadable, | 24 const {Downloader, Downloadable, |
25 MILLIS_IN_SECOND, MILLIS_IN_MINUTE, | 25 MILLIS_IN_SECOND, MILLIS_IN_MINUTE, |
26 MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("./downloader"); | 26 MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("./downloader"); |
27 const {Filter} = require("./filterClasses"); | 27 const {Filter} = require("./filterClasses"); |
28 const {FilterStorage} = require("./filterStorage"); | 28 const {filterStorage} = require("./filterStorage"); |
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 /** |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 /** | 88 /** |
89 * Yields {@link Downloadable} instances for all subscriptions that can be | 89 * Yields {@link Downloadable} instances for all subscriptions that can be |
90 * downloaded. | 90 * downloaded. |
91 * @yields {Downloadable} | 91 * @yields {Downloadable} |
92 */ | 92 */ |
93 *_getDownloadables() | 93 *_getDownloadables() |
94 { | 94 { |
95 if (!Prefs.subscriptions_autoupdate) | 95 if (!Prefs.subscriptions_autoupdate) |
96 return; | 96 return; |
97 | 97 |
98 for (let subscription of FilterStorage.subscriptions()) | 98 for (let subscription of filterStorage.subscriptions()) |
99 { | 99 { |
100 if (subscription instanceof DownloadableSubscription) | 100 if (subscription instanceof DownloadableSubscription) |
101 yield this._getDownloadable(subscription, false); | 101 yield this._getDownloadable(subscription, false); |
102 } | 102 } |
103 } | 103 } |
104 | 104 |
105 /** | 105 /** |
106 * Creates a {@link Downloadable} instance for a subscription. | 106 * Creates a {@link Downloadable} instance for a subscription. |
107 * @param {Subscription} subscription | 107 * @param {Subscription} subscription |
108 * @param {boolean} manual | 108 * @param {boolean} manual |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 let subscription = Subscription.fromURL(downloadable.redirectURL || | 179 let subscription = Subscription.fromURL(downloadable.redirectURL || |
180 downloadable.url); | 180 downloadable.url); |
181 if (downloadable.redirectURL && | 181 if (downloadable.redirectURL && |
182 downloadable.redirectURL != downloadable.url) | 182 downloadable.redirectURL != downloadable.url) |
183 { | 183 { |
184 let oldSubscription = Subscription.fromURL(downloadable.url); | 184 let oldSubscription = Subscription.fromURL(downloadable.url); |
185 subscription.title = oldSubscription.title; | 185 subscription.title = oldSubscription.title; |
186 subscription.disabled = oldSubscription.disabled; | 186 subscription.disabled = oldSubscription.disabled; |
187 subscription.lastCheck = oldSubscription.lastCheck; | 187 subscription.lastCheck = oldSubscription.lastCheck; |
188 | 188 |
189 let listed = FilterStorage.knownSubscriptions.has(oldSubscription.url); | 189 let listed = filterStorage.knownSubscriptions.has(oldSubscription.url); |
190 if (listed) | 190 if (listed) |
191 FilterStorage.removeSubscription(oldSubscription); | 191 filterStorage.removeSubscription(oldSubscription); |
192 | 192 |
193 Subscription.knownSubscriptions.delete(oldSubscription.url); | 193 Subscription.knownSubscriptions.delete(oldSubscription.url); |
194 | 194 |
195 if (listed) | 195 if (listed) |
196 FilterStorage.addSubscription(subscription); | 196 filterStorage.addSubscription(subscription); |
197 } | 197 } |
198 | 198 |
199 // The download actually succeeded | 199 // The download actually succeeded |
200 subscription.lastSuccess = subscription.lastDownload = Math.round( | 200 subscription.lastSuccess = subscription.lastDownload = Math.round( |
201 Date.now() / MILLIS_IN_SECOND | 201 Date.now() / MILLIS_IN_SECOND |
202 ); | 202 ); |
203 subscription.downloadStatus = "synchronize_ok"; | 203 subscription.downloadStatus = "synchronize_ok"; |
204 subscription.downloadCount = downloadable.downloadCount; | 204 subscription.downloadCount = downloadable.downloadCount; |
205 subscription.errors = 0; | 205 subscription.errors = 0; |
206 | 206 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 // Process filters | 260 // Process filters |
261 lines.shift(); | 261 lines.shift(); |
262 let filters = []; | 262 let filters = []; |
263 for (let line of lines) | 263 for (let line of lines) |
264 { | 264 { |
265 line = Filter.normalize(line); | 265 line = Filter.normalize(line); |
266 if (line) | 266 if (line) |
267 filters.push(Filter.fromText(line)); | 267 filters.push(Filter.fromText(line)); |
268 } | 268 } |
269 | 269 |
270 FilterStorage.updateSubscriptionFilters(subscription, filters); | 270 filterStorage.updateSubscriptionFilters(subscription, filters); |
271 } | 271 } |
272 | 272 |
273 _onDownloadError(downloadable, downloadURL, error, channelStatus, | 273 _onDownloadError(downloadable, downloadURL, error, channelStatus, |
274 responseStatus, redirectCallback) | 274 responseStatus, redirectCallback) |
275 { | 275 { |
276 let subscription = Subscription.fromURL(downloadable.url); | 276 let subscription = Subscription.fromURL(downloadable.url); |
277 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); | 277 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); |
278 subscription.downloadStatus = error; | 278 subscription.downloadStatus = error; |
279 | 279 |
280 // Request fallback URL if necessary - for automatic updates only | 280 // Request fallback URL if necessary - for automatic updates only |
(...skipping 27 matching lines...) Expand all Loading... |
308 request.open("GET", fallbackURL); | 308 request.open("GET", fallbackURL); |
309 request.overrideMimeType("text/plain"); | 309 request.overrideMimeType("text/plain"); |
310 request.channel.loadFlags = request.channel.loadFlags | | 310 request.channel.loadFlags = request.channel.loadFlags | |
311 request.channel.INHIBIT_CACHING | | 311 request.channel.INHIBIT_CACHING | |
312 request.channel.VALIDATE_ALWAYS; | 312 request.channel.VALIDATE_ALWAYS; |
313 request.addEventListener("load", ev => | 313 request.addEventListener("load", ev => |
314 { | 314 { |
315 if (onShutdown.done) | 315 if (onShutdown.done) |
316 return; | 316 return; |
317 | 317 |
318 if (!FilterStorage.knownSubscriptions.has(subscription.url)) | 318 if (!filterStorage.knownSubscriptions.has(subscription.url)) |
319 return; | 319 return; |
320 | 320 |
321 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText); | 321 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText); |
322 if (match && match[1] == "301" && // Moved permanently | 322 if (match && match[1] == "301" && // Moved permanently |
323 match[2] && /^https?:\/\//i.test(match[2])) | 323 match[2] && /^https?:\/\//i.test(match[2])) |
324 { | 324 { |
325 redirectCallback(match[2]); | 325 redirectCallback(match[2]); |
326 } | 326 } |
327 else if (match && match[1] == "410") // Gone | 327 else if (match && match[1] == "410") // Gone |
328 { | 328 { |
329 let data = "[Adblock]\n" + | 329 let data = "[Adblock]\n" + |
330 subscription.filters.map(f => f.text).join("\n"); | 330 subscription.filters.map(f => f.text).join("\n"); |
331 redirectCallback("data:text/plain," + encodeURIComponent(data)); | 331 redirectCallback("data:text/plain," + encodeURIComponent(data)); |
332 } | 332 } |
333 }, false); | 333 }, false); |
334 request.send(null); | 334 request.send(null); |
335 } | 335 } |
336 } | 336 } |
337 } | 337 } |
338 } | 338 } |
339 | 339 |
340 /** | 340 /** |
341 * This object is responsible for downloading filter subscriptions whenever | 341 * This object is responsible for downloading filter subscriptions whenever |
342 * necessary. | 342 * necessary. |
343 * @type {Synchronizer} | 343 * @type {Synchronizer} |
344 */ | 344 */ |
345 let synchronizer = new Synchronizer(); | 345 let synchronizer = new Synchronizer(); |
346 | 346 |
347 exports.Synchronizer = synchronizer; | 347 exports.Synchronizer = synchronizer; |
OLD | NEW |