| Left: | ||
| Right: |
| 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); | |
|
hub
2017/11/13 22:35:28
I intend to parse the whole buffer, but for now we
| |
| 180 | |
| 181 if (parser.redirect) | |
| 182 return redirectCallback(parser.redirect); | |
|
hub
2017/11/13 22:35:28
This can be better as we should catch the redirect
| |
| 190 | 183 |
| 191 // Handle redirects | 184 // Handle redirects |
| 192 let subscription = Subscription.fromURL(downloadable.redirectURL || | 185 let subscription = Subscription.fromURL(downloadable.redirectURL || |
| 193 downloadable.url); | 186 downloadable.url); |
| 194 if (downloadable.redirectURL && | 187 if (downloadable.redirectURL && |
| 195 downloadable.redirectURL != downloadable.url) | 188 downloadable.redirectURL != downloadable.url) |
| 196 { | 189 { |
| 197 let oldSubscription = Subscription.fromURL(downloadable.url); | 190 let oldSubscription = Subscription.fromURL(downloadable.url); |
| 198 subscription.title = oldSubscription.title; | 191 subscription.title = oldSubscription.title; |
| 199 subscription.disabled = oldSubscription.disabled; | 192 subscription.disabled = oldSubscription.disabled; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 210 } | 203 } |
| 211 | 204 |
| 212 // The download actually succeeded | 205 // The download actually succeeded |
| 213 subscription.lastSuccess = subscription.lastDownload = Math.round( | 206 subscription.lastSuccess = subscription.lastDownload = Math.round( |
| 214 Date.now() / MILLIS_IN_SECOND | 207 Date.now() / MILLIS_IN_SECOND |
| 215 ); | 208 ); |
| 216 subscription.downloadStatus = "synchronize_ok"; | 209 subscription.downloadStatus = "synchronize_ok"; |
| 217 subscription.downloadCount = downloadable.downloadCount; | 210 subscription.downloadCount = downloadable.downloadCount; |
| 218 subscription.errors = 0; | 211 subscription.errors = 0; |
| 219 | 212 |
| 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 | 213 // Process parameters |
| 225 if (params.homepage) | 214 if (parser.homepage) |
| 226 { | 215 { |
| 227 let url; | 216 let url; |
| 228 try | 217 try |
| 229 { | 218 { |
| 230 url = new URL(params.homepage); | 219 url = new URL(parser.homepage); |
| 231 } | 220 } |
| 232 catch (e) | 221 catch (e) |
| 233 { | 222 { |
| 234 url = null; | 223 url = null; |
| 235 } | 224 } |
| 236 | 225 |
| 237 if (url && (url.protocol == "http:" || url.protocol == "https:")) | 226 if (url && (url.protocol == "http:" || url.protocol == "https:")) |
| 238 subscription.homepage = url.href; | 227 subscription.homepage = url.href; |
| 239 } | 228 } |
| 240 | 229 |
| 241 if (params.title) | 230 if (minVersion) |
| 242 { | 231 subscription.requiredVersion = minVersion; |
| 243 subscription.title = params.title; | |
| 244 subscription.fixedTitle = true; | |
| 245 } | |
| 246 else | 232 else |
| 247 subscription.fixedTitle = false; | 233 delete subscription.requiredVersion; |
| 248 | |
| 249 subscription.version = (params.version ? parseInt(params.version, 10) : 0); | |
| 250 | 234 |
| 251 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL; | 235 let expirationInterval = DEFAULT_EXPIRATION_INTERVAL; |
| 252 if (params.expires) | 236 let expiration = parser.finalize(); |
| 253 { | 237 if (expiration != 0) |
| 254 let match = /^(\d+)\s*(h)?/.exec(params.expires); | 238 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 | 239 |
| 265 let [ | 240 let [ |
| 266 softExpiration, | 241 softExpiration, |
| 267 hardExpiration | 242 hardExpiration |
| 268 ] = downloader.processExpirationInterval(expirationInterval); | 243 ] = downloader.processExpirationInterval(expirationInterval); |
| 269 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); | 244 subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); |
| 270 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); | 245 subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); |
| 271 | 246 |
| 272 if (minVersion) | 247 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 | 248 |
| 289 return undefined; | 249 return undefined; |
| 290 }, | 250 }, |
| 291 | 251 |
| 292 _onDownloadError(downloadable, downloadURL, error, channelStatus, | 252 _onDownloadError(downloadable, downloadURL, error, channelStatus, |
| 293 responseStatus, redirectCallback) | 253 responseStatus, redirectCallback) |
| 294 { | 254 { |
| 295 let subscription = Subscription.fromURL(downloadable.url); | 255 let subscription = Subscription.fromURL(downloadable.url); |
| 296 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); | 256 subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); |
| 297 subscription.downloadStatus = error; | 257 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"); | 309 subscription.filters.map(f => f.text).join("\n"); |
| 350 redirectCallback("data:text/plain," + encodeURIComponent(data)); | 310 redirectCallback("data:text/plain," + encodeURIComponent(data)); |
| 351 } | 311 } |
| 352 }, false); | 312 }, false); |
| 353 request.send(null); | 313 request.send(null); |
| 354 } | 314 } |
| 355 } | 315 } |
| 356 } | 316 } |
| 357 }; | 317 }; |
| 358 Synchronizer.init(); | 318 Synchronizer.init(); |
| OLD | NEW |