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"); | |
28 const {FilterStorage} = require("filterStorage"); | 27 const {FilterStorage} = require("filterStorage"); |
29 const {FilterNotifier} = require("filterNotifier"); | 28 const {FilterNotifier} = require("filterNotifier"); |
30 const {Prefs} = require("prefs"); | 29 const {Prefs} = require("prefs"); |
31 const {Subscription, DownloadableSubscription} = require("subscriptionClasses"); | 30 const {Subscription, DownloadableSubscription} = require("subscriptionClasses"); |
32 const {Utils} = require("utils"); | |
33 | 31 |
34 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; | 32 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; |
35 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; | 33 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; |
36 const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY; | 34 const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY; |
37 | 35 |
38 /** | 36 /** |
39 * The object providing actual downloading functionality. | 37 * The object providing actual downloading functionality. |
40 * @type {Downloader} | 38 * @type {Downloader} |
41 */ | 39 */ |
42 let downloader = null; | 40 let downloader = null; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |
156 // Don't remove parameter comments immediately but add them to a list first, | 154 let parser = DownloadableSubscription.parseDownload(); |
157 // they need to be considered in the checksum calculation. | 155 |
158 let remove = []; | 156 // Process filters |
159 let params = { | 157 for (let line of lines) |
160 redirect: null, | 158 parser.process(line); |
161 homepage: null, | 159 |
162 title: null, | 160 if (parser.redirect) |
163 version: null, | |
164 expires: null | |
165 }; | |
166 for (let i = 0; i < lines.length; i++) | |
167 { | 161 { |
168 let match = /^\s*!\s*(\w+)\s*:\s*(.*)/.exec(lines[i]); | 162 let {redirect} = parser; |
169 if (match) | 163 parser.delete(); |
170 { | 164 return redirectCallback(redirect); |
171 let keyword = match[1].toLowerCase(); | |
172 let value = match[2]; | |
173 if (keyword in params) | |
174 { | |
175 params[keyword] = value; | |
176 remove.push(i); | |
177 } | |
178 else if (keyword == "checksum") | |
179 { | |
180 lines.splice(i--, 1); | |
181 let checksum = Utils.generateChecksum(lines); | |
182 if (checksum && checksum != value.replace(/=+$/, "")) | |
183 return errorCallback("synchronize_checksum_mismatch"); | |
184 } | |
185 } | |
186 } | 165 } |
187 | 166 |
188 if (params.redirect) | |
189 return redirectCallback(params.redirect); | |
190 | |
191 // Handle redirects | 167 // Handle redirects |
192 let subscription = Subscription.fromURL(downloadable.redirectURL || | 168 let subscription = Subscription.fromURL(downloadable.redirectURL || |
193 downloadable.url); | 169 downloadable.url); |
194 if (downloadable.redirectURL && | 170 if (downloadable.redirectURL && |
195 downloadable.redirectURL != downloadable.url) | 171 downloadable.redirectURL != downloadable.url) |
196 { | 172 { |
197 let oldSubscription = Subscription.fromURL(downloadable.url); | 173 let oldSubscription = Subscription.fromURL(downloadable.url); |
198 subscription.title = oldSubscription.title; | 174 subscription.title = oldSubscription.title; |
199 subscription.disabled = oldSubscription.disabled; | 175 subscription.disabled = oldSubscription.disabled; |
200 subscription.lastCheck = oldSubscription.lastCheck; | 176 subscription.lastCheck = oldSubscription.lastCheck; |
201 | 177 |
202 let listed = (oldSubscription.url in FilterStorage.knownSubscriptions); | 178 let {listed} = oldSubscription; |
203 if (listed) | 179 if (listed) |
204 FilterStorage.removeSubscription(oldSubscription); | 180 FilterStorage.removeSubscription(oldSubscription); |
205 | 181 |
206 delete Subscription.knownSubscriptions[oldSubscription.url]; | |
207 | |
208 if (listed) | 182 if (listed) |
209 FilterStorage.addSubscription(subscription); | 183 FilterStorage.addSubscription(subscription); |
210 } | 184 } |
211 | 185 |
212 // The download actually succeeded | 186 // The download actually succeeded |
213 subscription.lastSuccess = subscription.lastDownload = Math.round( | 187 subscription.lastSuccess = subscription.lastDownload = Math.round( |
214 Date.now() / MILLIS_IN_SECOND | 188 Date.now() / MILLIS_IN_SECOND |
215 ); | 189 ); |
216 subscription.downloadStatus = "synchronize_ok"; | 190 subscription.downloadStatus = "synchronize_ok"; |
217 subscription.downloadCount = downloadable.downloadCount; | 191 subscription.downloadCount = downloadable.downloadCount; |
218 subscription.errors = 0; | 192 subscription.errors = 0; |
219 | 193 |
220 // Remove lines containing parameters | |
221 for (let i = remove.length - 1; i >= 0; i--) | |
222 lines.splice(remove[i], 1); | |
223 | |
224 // Process parameters | 194 // Process parameters |
225 if (params.homepage) | 195 if (parser.homepage) |
226 { | 196 { |
227 let url; | 197 let url; |
228 try | 198 try |
229 { | 199 { |
230 url = new URL(params.homepage); | 200 url = new URL(parser.homepage); |
231 } | 201 } |
232 catch (e) | 202 catch (e) |
233 { | 203 { |
234 url = null; | 204 url = null; |
235 } | 205 } |
236 | 206 |
237 if (url && (url.protocol == "http:" || url.protocol == "https:")) | 207 if (url && (url.protocol == "http:" || url.protocol == "https:")) |
238 subscription.homepage = url.href; | 208 subscription.homepage = url.href; |
239 } | 209 } |
240 | 210 |
241 if (params.title) | 211 if (minVersion) |
242 { | 212 subscription.requiredVersion = minVersion; |
243 subscription.title = params.title; | |
244 subscription.fixedTitle = true; | |
245 } | |
246 else | 213 else |
247 subscription.fixedTitle = false; | 214 delete subscription.requiredVersion; |
248 | |
249 subscription.version = (params.version ? parseInt(params.version, 10) : 0); | |
250 | 215 |
251 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL; | 216 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL; |
252 if (params.expires) | 217 let expiration = parser.finalize(subscription); |
253 { | 218 if (expiration != 0) |
254 let match = /^(\d+)\s*(h)?/.exec(params.expires); | 219 expirationInterval = expiration; |
255 if (match) | |
256 { | |
257 let interval = parseInt(match[1], 10); | |
258 if (match[2]) | |
259 expirationInterval = interval * MILLIS_IN_HOUR; | |
260 else | |
261 expirationInterval = interval * MILLIS_IN_DAY; | |
262 } | |
263 } | |
264 | 220 |
265 let [ | 221 let [ |
266 softExpiration, | 222 softExpiration, |
267 hardExpiration | 223 hardExpiration |
268 ] = downloader.processExpirationInterval(expirationInterval); | 224 ] = downloader.processExpirationInterval(expirationInterval); |
269 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); | 225 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); |
270 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); | 226 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); |
271 | 227 |
272 if (minVersion) | 228 parser.delete(); |
273 subscription.requiredVersion = minVersion; | |
274 else | |
275 delete subscription.requiredVersion; | |
276 | |
277 // Process filters | |
278 lines.shift(); | |
279 let filters = []; | |
280 for (let line of lines) | |
281 { | |
282 line = Filter.normalize(line); | |
283 if (line) | |
284 filters.push(Filter.fromText(line)); | |
285 } | |
286 | |
287 FilterStorage.updateSubscriptionFilters(subscription, filters); | |
288 | 229 |
289 return undefined; | 230 return undefined; |
290 }, | 231 }, |
291 | 232 |
292 _onDownloadError(downloadable, downloadURL, error, channelStatus, | 233 _onDownloadError(downloadable, downloadURL, error, channelStatus, |
293 responseStatus, redirectCallback) | 234 responseStatus, redirectCallback) |
294 { | 235 { |
295 let subscription = Subscription.fromURL(downloadable.url); | 236 let subscription = Subscription.fromURL(downloadable.url); |
296 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); | 237 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); |
297 subscription.downloadStatus = error; | 238 subscription.downloadStatus = error; |
(...skipping 29 matching lines...) Expand all Loading... |
327 request.open("GET", fallbackURL); | 268 request.open("GET", fallbackURL); |
328 request.overrideMimeType("text/plain"); | 269 request.overrideMimeType("text/plain"); |
329 request.channel.loadFlags = request.channel.loadFlags | | 270 request.channel.loadFlags = request.channel.loadFlags | |
330 request.channel.INHIBIT_CACHING | | 271 request.channel.INHIBIT_CACHING | |
331 request.channel.VALIDATE_ALWAYS; | 272 request.channel.VALIDATE_ALWAYS; |
332 request.addEventListener("load", ev => | 273 request.addEventListener("load", ev => |
333 { | 274 { |
334 if (onShutdown.done) | 275 if (onShutdown.done) |
335 return; | 276 return; |
336 | 277 |
337 if (!(subscription.url in FilterStorage.knownSubscriptions)) | 278 if (!subscription.listed) |
338 return; | 279 return; |
339 | 280 |
340 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText); | 281 let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText); |
341 if (match && match[1] == "301" && // Moved permanently | 282 if (match && match[1] == "301" && // Moved permanently |
342 match[2] && /^https?:\/\//i.test(match[2])) | 283 match[2] && /^https?:\/\//i.test(match[2])) |
343 { | 284 { |
344 redirectCallback(match[2]); | 285 redirectCallback(match[2]); |
345 } | 286 } |
346 else if (match && match[1] == "410") // Gone | 287 else if (match && match[1] == "410") // Gone |
347 { | 288 { |
348 let data = "[Adblock]\n" + | 289 let data = "[Adblock]\n" + |
349 subscription.filters.map(f => f.text).join("\n"); | 290 Array.from(subscription.filters, f => f.text).join("\n"); |
350 redirectCallback("data:text/plain," + encodeURIComponent(data)); | 291 redirectCallback("data:text/plain," + encodeURIComponent(data)); |
351 } | 292 } |
352 }, false); | 293 }, false); |
353 request.send(null); | 294 request.send(null); |
354 } | 295 } |
355 } | 296 } |
356 } | 297 } |
357 }; | 298 }; |
358 Synchronizer.init(); | 299 Synchronizer.init(); |
OLD | NEW |