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"); | 31 const {Utils} = require("utils"); |
33 | 32 |
34 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; | 33 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; |
35 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; | 34 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; |
36 const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY; | 35 const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY; |
37 | 36 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 | 145 |
147 _onDownloadSuccess(downloadable, responseText, errorCallback, | 146 _onDownloadSuccess(downloadable, responseText, errorCallback, |
148 redirectCallback) | 147 redirectCallback) |
149 { | 148 { |
150 let lines = responseText.split(/[\r\n]+/); | 149 let lines = responseText.split(/[\r\n]+/); |
151 let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]); | 150 let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]); |
152 if (!headerMatch) | 151 if (!headerMatch) |
153 return errorCallback("synchronize_invalid_data"); | 152 return errorCallback("synchronize_invalid_data"); |
154 let minVersion = headerMatch[1]; | 153 let minVersion = headerMatch[1]; |
155 | 154 |
| 155 let parser = subscription.parseDownload(); |
| 156 |
156 // Don't remove parameter comments immediately but add them to a list first, | 157 // Don't remove parameter comments immediately but add them to a list first, |
157 // they need to be considered in the checksum calculation. | 158 // they need to be considered in the checksum calculation. |
158 let remove = []; | |
159 let params = { | |
160 redirect: null, | |
161 homepage: null, | |
162 title: null, | |
163 version: null, | |
164 expires: null | |
165 }; | |
166 for (let i = 0; i < lines.length; i++) | 159 for (let i = 0; i < lines.length; i++) |
167 { | 160 { |
168 let match = /^\s*!\s*(\w+)\s*:\s*(.*)/.exec(lines[i]); | 161 let match = /^\s*!\s*(\w+)\s*:\s*(.*)/.exec(lines[i]); |
169 if (match) | 162 if (match) |
170 { | 163 { |
171 let keyword = match[1].toLowerCase(); | 164 let keyword = match[1].toLowerCase(); |
172 let value = match[2]; | 165 let value = match[2]; |
173 if (keyword in params) | 166 if (keyword == "checksum") |
174 { | |
175 params[keyword] = value; | |
176 remove.push(i); | |
177 } | |
178 else if (keyword == "checksum") | |
179 { | 167 { |
180 lines.splice(i--, 1); | 168 lines.splice(i--, 1); |
181 let checksum = Utils.generateChecksum(lines); | 169 let checksum = Utils.generateChecksum(lines); |
182 if (checksum && checksum != value.replace(/=+$/, "")) | 170 if (checksum && checksum != value.replace(/=+$/, "")) |
183 return errorCallback("synchronize_checksum_mismatch"); | 171 return errorCallback("synchronize_checksum_mismatch"); |
184 } | 172 } |
185 } | 173 } |
186 } | 174 } |
187 | 175 |
188 if (params.redirect) | 176 // Process filters |
189 return redirectCallback(params.redirect); | 177 lines.shift(); |
| 178 for (let line of lines) |
| 179 parser.process(line); |
| 180 |
| 181 if (parser.redirect) |
| 182 { |
| 183 let {redirect} = parser; |
| 184 parser.delete(); |
| 185 return redirectCallback(redirect); |
| 186 } |
190 | 187 |
191 // Handle redirects | 188 // Handle redirects |
192 let subscription = Subscription.fromURL(downloadable.redirectURL || | 189 let subscription = Subscription.fromURL(downloadable.redirectURL || |
193 downloadable.url); | 190 downloadable.url); |
194 if (downloadable.redirectURL && | 191 if (downloadable.redirectURL && |
195 downloadable.redirectURL != downloadable.url) | 192 downloadable.redirectURL != downloadable.url) |
196 { | 193 { |
197 let oldSubscription = Subscription.fromURL(downloadable.url); | 194 let oldSubscription = Subscription.fromURL(downloadable.url); |
198 subscription.title = oldSubscription.title; | 195 subscription.title = oldSubscription.title; |
199 subscription.disabled = oldSubscription.disabled; | 196 subscription.disabled = oldSubscription.disabled; |
(...skipping 10 matching lines...) Expand all Loading... |
210 } | 207 } |
211 | 208 |
212 // The download actually succeeded | 209 // The download actually succeeded |
213 subscription.lastSuccess = subscription.lastDownload = Math.round( | 210 subscription.lastSuccess = subscription.lastDownload = Math.round( |
214 Date.now() / MILLIS_IN_SECOND | 211 Date.now() / MILLIS_IN_SECOND |
215 ); | 212 ); |
216 subscription.downloadStatus = "synchronize_ok"; | 213 subscription.downloadStatus = "synchronize_ok"; |
217 subscription.downloadCount = downloadable.downloadCount; | 214 subscription.downloadCount = downloadable.downloadCount; |
218 subscription.errors = 0; | 215 subscription.errors = 0; |
219 | 216 |
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 | 217 // Process parameters |
225 if (params.homepage) | 218 if (parser.homepage) |
226 { | 219 { |
227 let url; | 220 let url; |
228 try | 221 try |
229 { | 222 { |
230 url = new URL(params.homepage); | 223 url = new URL(parser.homepage); |
231 } | 224 } |
232 catch (e) | 225 catch (e) |
233 { | 226 { |
234 url = null; | 227 url = null; |
235 } | 228 } |
236 | 229 |
237 if (url && (url.protocol == "http:" || url.protocol == "https:")) | 230 if (url && (url.protocol == "http:" || url.protocol == "https:")) |
238 subscription.homepage = url.href; | 231 subscription.homepage = url.href; |
239 } | 232 } |
240 | 233 |
241 if (params.title) | 234 if (minVersion) |
242 { | 235 subscription.requiredVersion = minVersion; |
243 subscription.title = params.title; | |
244 subscription.fixedTitle = true; | |
245 } | |
246 else | 236 else |
247 subscription.fixedTitle = false; | 237 delete subscription.requiredVersion; |
248 | |
249 subscription.version = (params.version ? parseInt(params.version, 10) : 0); | |
250 | 238 |
251 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL; | 239 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL; |
252 if (params.expires) | 240 let expiration = parser.finalize(); |
253 { | 241 if (expiration != 0) |
254 let match = /^(\d+)\s*(h)?/.exec(params.expires); | 242 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 | 243 |
265 let [ | 244 let [ |
266 softExpiration, | 245 softExpiration, |
267 hardExpiration | 246 hardExpiration |
268 ] = downloader.processExpirationInterval(expirationInterval); | 247 ] = downloader.processExpirationInterval(expirationInterval); |
269 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); | 248 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); |
270 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); | 249 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); |
271 | 250 |
272 if (minVersion) | 251 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 | 252 |
289 return undefined; | 253 return undefined; |
290 }, | 254 }, |
291 | 255 |
292 _onDownloadError(downloadable, downloadURL, error, channelStatus, | 256 _onDownloadError(downloadable, downloadURL, error, channelStatus, |
293 responseStatus, redirectCallback) | 257 responseStatus, redirectCallback) |
294 { | 258 { |
295 let subscription = Subscription.fromURL(downloadable.url); | 259 let subscription = Subscription.fromURL(downloadable.url); |
296 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); | 260 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); |
297 subscription.downloadStatus = error; | 261 subscription.downloadStatus = error; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 subscription.filters.map(f => f.text).join("\n"); | 313 subscription.filters.map(f => f.text).join("\n"); |
350 redirectCallback("data:text/plain," + encodeURIComponent(data)); | 314 redirectCallback("data:text/plain," + encodeURIComponent(data)); |
351 } | 315 } |
352 }, false); | 316 }, false); |
353 request.send(null); | 317 request.send(null); |
354 } | 318 } |
355 } | 319 } |
356 } | 320 } |
357 }; | 321 }; |
358 Synchronizer.init(); | 322 Synchronizer.init(); |
OLD | NEW |