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