diff options
Diffstat (limited to 'data/extensions/spyblock@gnu.org/lib/synchronizer.js')
-rw-r--r-- | data/extensions/spyblock@gnu.org/lib/synchronizer.js | 168 |
1 files changed, 103 insertions, 65 deletions
diff --git a/data/extensions/spyblock@gnu.org/lib/synchronizer.js b/data/extensions/spyblock@gnu.org/lib/synchronizer.js index 2304895..b8d14a2 100644 --- a/data/extensions/spyblock@gnu.org/lib/synchronizer.js +++ b/data/extensions/spyblock@gnu.org/lib/synchronizer.js @@ -1,6 +1,6 @@ /* * This file is part of Adblock Plus <https://adblockplus.org/>, - * Copyright (C) 2006-2015 Eyeo GmbH + * Copyright (C) 2006-2017 eyeo GmbH * * Adblock Plus is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as @@ -15,29 +15,29 @@ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. */ +"use strict"; + /** * @fileOverview Manages synchronization of filter subscriptions. */ -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); -Cu.import("resource://gre/modules/Services.jsm"); - -let {Downloader, Downloadable, - MILLIS_IN_SECOND, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); -let {Filter, CommentFilter} = require("filterClasses"); -let {FilterStorage} = require("filterStorage"); -let {FilterNotifier} = require("filterNotifier"); -let {Prefs} = require("prefs"); -let {Subscription, DownloadableSubscription} = require("subscriptionClasses"); -let {Utils} = require("utils"); +const {Downloader, Downloadable, + MILLIS_IN_SECOND, MILLIS_IN_MINUTE, + MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); +const {Filter} = require("filterClasses"); +const {FilterStorage} = require("filterStorage"); +const {FilterNotifier} = require("filterNotifier"); +const {Prefs} = require("prefs"); +const {Subscription, DownloadableSubscription} = require("subscriptionClasses"); +const {Utils} = require("utils"); -let INITIAL_DELAY = 6 * MILLIS_IN_MINUTE; -let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; -let DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY; +const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; +const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; +const DEFAULT_EXPIRATION_INTERVAL = 5 * MILLIS_IN_DAY; /** * The object providing actual downloading functionality. - * @type Downloader + * @type {Downloader} */ let downloader = null; @@ -51,10 +51,11 @@ let Synchronizer = exports.Synchronizer = /** * Called on module startup. */ - init: function() + init() { - downloader = new Downloader(this._getDownloadables.bind(this), INITIAL_DELAY, CHECK_INTERVAL); - onShutdown.add(function() + downloader = new Downloader(this._getDownloadables.bind(this), + INITIAL_DELAY, CHECK_INTERVAL); + onShutdown.add(() => { downloader.cancel(); }); @@ -67,20 +68,23 @@ let Synchronizer = exports.Synchronizer = /** * Checks whether a subscription is currently being downloaded. - * @param {String} url URL of the subscription - * @return {Boolean} + * @param {string} url URL of the subscription + * @return {boolean} */ - isExecuting: function(url) + isExecuting(url) { return downloader.isDownloading(url); }, /** * Starts the download of a subscription. - * @param {DownloadableSubscription} subscription Subscription to be downloaded - * @param {Boolean} manual true for a manually started download (should not trigger fallback requests) + * @param {DownloadableSubscription} subscription + * Subscription to be downloaded + * @param {boolean} manual + * true for a manually started download (should not trigger fallback + * requests) */ - execute: function(subscription, manual) + execute(subscription, manual) { downloader.download(this._getDownloadable(subscription, manual)); }, @@ -88,7 +92,7 @@ let Synchronizer = exports.Synchronizer = /** * Yields Downloadable instances for all subscriptions that can be downloaded. */ - _getDownloadables: function() + *_getDownloadables() { if (!Prefs.subscriptions_autoupdate) return; @@ -102,8 +106,11 @@ let Synchronizer = exports.Synchronizer = /** * Creates a Downloadable instance for a subscription. + * @param {Subscription} subscription + * @param {boolean} manual + * @return {Downloadable} */ - _getDownloadable: function(/**Subscription*/ subscription, /**Boolean*/ manual) /**Downloadable*/ + _getDownloadable(subscription, manual) { let result = new Downloadable(subscription.url); if (subscription.lastDownload != subscription.lastSuccess) @@ -118,27 +125,34 @@ let Synchronizer = exports.Synchronizer = return result; }, - _onExpirationChange: function(downloadable) + _onExpirationChange(downloadable) { let subscription = Subscription.fromURL(downloadable.url); - subscription.lastCheck = Math.round(downloadable.lastCheck / MILLIS_IN_SECOND); - subscription.softExpiration = Math.round(downloadable.softExpiration / MILLIS_IN_SECOND); - subscription.expires = Math.round(downloadable.hardExpiration / MILLIS_IN_SECOND); + subscription.lastCheck = Math.round( + downloadable.lastCheck / MILLIS_IN_SECOND + ); + subscription.softExpiration = Math.round( + downloadable.softExpiration / MILLIS_IN_SECOND + ); + subscription.expires = Math.round( + downloadable.hardExpiration / MILLIS_IN_SECOND + ); }, - _onDownloadStarted: function(downloadable) + _onDownloadStarted(downloadable) { let subscription = Subscription.fromURL(downloadable.url); - FilterNotifier.triggerListeners("subscription.downloadStatus", subscription); + FilterNotifier.triggerListeners("subscription.downloading", subscription); }, - _onDownloadSuccess: function(downloadable, responseText, errorCallback, redirectCallback) + _onDownloadSuccess(downloadable, responseText, errorCallback, + redirectCallback) { let lines = responseText.split(/[\r\n]+/); - let match = /\[Adblock(?:\s*Plus\s*([\d\.]+)?)?\]/i.exec(lines[0]); - if (!match) + let headerMatch = /\[Adblock(?:\s*Plus\s*([\d.]+)?)?\]/i.exec(lines[0]); + if (!headerMatch) return errorCallback("synchronize_invalid_data"); - let minVersion = match[1]; + let minVersion = headerMatch[1]; // Don't remove parameter comments immediately but add them to a list first, // they need to be considered in the checksum calculation. @@ -177,8 +191,10 @@ let Synchronizer = exports.Synchronizer = return redirectCallback(params.redirect); // Handle redirects - let subscription = Subscription.fromURL(downloadable.redirectURL || downloadable.url); - if (downloadable.redirectURL && downloadable.redirectURL != downloadable.url) + let subscription = Subscription.fromURL(downloadable.redirectURL || + downloadable.url); + if (downloadable.redirectURL && + downloadable.redirectURL != downloadable.url) { let oldSubscription = Subscription.fromURL(downloadable.url); subscription.title = oldSubscription.title; @@ -196,7 +212,9 @@ let Synchronizer = exports.Synchronizer = } // The download actually succeeded - subscription.lastSuccess = subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); + subscription.lastSuccess = subscription.lastDownload = Math.round( + Date.now() / MILLIS_IN_SECOND + ); subscription.downloadStatus = "synchronize_ok"; subscription.downloadCount = downloadable.downloadCount; subscription.errors = 0; @@ -208,9 +226,18 @@ let Synchronizer = exports.Synchronizer = // Process parameters if (params.homepage) { - let uri = Utils.makeURI(params.homepage); - if (uri && (uri.scheme == "http" || uri.scheme == "https")) - subscription.homepage = uri.spec; + let url; + try + { + url = new URL(params.homepage); + } + catch (e) + { + url = null; + } + + if (url && (url.protocol == "http:" || url.protocol == "https:")) + subscription.homepage = url.href; } if (params.privatemode) @@ -242,19 +269,17 @@ let Synchronizer = exports.Synchronizer = } } - let [softExpiration, hardExpiration] = downloader.processExpirationInterval(expirationInterval); + let [ + softExpiration, + hardExpiration + ] = downloader.processExpirationInterval(expirationInterval); subscription.softExpiration = Math.round(softExpiration / MILLIS_IN_SECOND); subscription.expires = Math.round(hardExpiration / MILLIS_IN_SECOND); - delete subscription.requiredVersion; - delete subscription.upgradeRequired; if (minVersion) - { - let {addonVersion} = require("info"); subscription.requiredVersion = minVersion; - if (Services.vc.compare(minVersion, addonVersion) > 0) - subscription.upgradeRequired = true; - } + else + delete subscription.requiredVersion; // Process filters lines.shift(); @@ -271,7 +296,8 @@ let Synchronizer = exports.Synchronizer = return undefined; }, - _onDownloadError: function(downloadable, downloadURL, error, channelStatus, responseStatus, redirectCallback) + _onDownloadError(downloadable, downloadURL, error, channelStatus, + responseStatus, redirectCallback) { let subscription = Subscription.fromURL(downloadable.url); subscription.lastDownload = Math.round(Date.now() / MILLIS_IN_SECOND); @@ -282,18 +308,26 @@ let Synchronizer = exports.Synchronizer = { subscription.errors++; - if (redirectCallback && subscription.errors >= Prefs.subscriptions_fallbackerrors && /^https?:\/\//i.test(subscription.url)) + if (redirectCallback && + subscription.errors >= Prefs.subscriptions_fallbackerrors && + /^https?:\/\//i.test(subscription.url)) { subscription.errors = 0; let fallbackURL = Prefs.subscriptions_fallbackurl; - let {addonVersion} = require("info"); - fallbackURL = fallbackURL.replace(/%VERSION%/g, encodeURIComponent(addonVersion)); - fallbackURL = fallbackURL.replace(/%SUBSCRIPTION%/g, encodeURIComponent(subscription.url)); - fallbackURL = fallbackURL.replace(/%URL%/g, encodeURIComponent(downloadURL)); - fallbackURL = fallbackURL.replace(/%ERROR%/g, encodeURIComponent(error)); - fallbackURL = fallbackURL.replace(/%CHANNELSTATUS%/g, encodeURIComponent(channelStatus)); - fallbackURL = fallbackURL.replace(/%RESPONSESTATUS%/g, encodeURIComponent(responseStatus)); + const {addonVersion} = require("info"); + fallbackURL = fallbackURL.replace(/%VERSION%/g, + encodeURIComponent(addonVersion)); + fallbackURL = fallbackURL.replace(/%SUBSCRIPTION%/g, + encodeURIComponent(subscription.url)); + fallbackURL = fallbackURL.replace(/%URL%/g, + encodeURIComponent(downloadURL)); + fallbackURL = fallbackURL.replace(/%ERROR%/g, + encodeURIComponent(error)); + fallbackURL = fallbackURL.replace(/%CHANNELSTATUS%/g, + encodeURIComponent(channelStatus)); + fallbackURL = fallbackURL.replace(/%RESPONSESTATUS%/g, + encodeURIComponent(responseStatus)); let request = new XMLHttpRequest(); request.mozBackgroundRequest = true; @@ -302,7 +336,7 @@ let Synchronizer = exports.Synchronizer = request.channel.loadFlags = request.channel.loadFlags | request.channel.INHIBIT_CACHING | request.channel.VALIDATE_ALWAYS; - request.addEventListener("load", function(ev) + request.addEventListener("load", ev => { if (onShutdown.done) return; @@ -311,17 +345,21 @@ let Synchronizer = exports.Synchronizer = return; let match = /^(\d+)(?:\s+(\S+))?$/.exec(request.responseText); - if (match && match[1] == "301" && match[2] && /^https?:\/\//i.test(match[2])) // Moved permanently + if (match && match[1] == "301" && // Moved permanently + match[2] && /^https?:\/\//i.test(match[2])) + { redirectCallback(match[2]); - else if (match && match[1] == "410") // Gone + } + else if (match && match[1] == "410") // Gone { - let data = "[Adblock]\n" + subscription.filters.map((f) => f.text).join("\n"); + let data = "[Adblock]\n" + + subscription.filters.map(f => f.text).join("\n"); redirectCallback("data:text/plain," + encodeURIComponent(data)); } }, false); request.send(null); } } - }, + } }; Synchronizer.init(); |