Index: lib/synchronizer.js |
diff --git a/lib/synchronizer.js b/lib/synchronizer.js |
index acca4711108822976a213d59febd5f5ebf72d196..675acd325174b7cb70391cf48d294ec2482a9eae 100644 |
--- a/lib/synchronizer.js |
+++ b/lib/synchronizer.js |
@@ -15,6 +15,8 @@ |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
+"use strict"; |
+ |
/** |
* @fileOverview Manages synchronization of filter subscriptions. |
*/ |
@@ -22,39 +24,41 @@ |
Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
Cu.import("resource://gre/modules/Services.jsm"); |
-var {Downloader, Downloadable, |
- MILLIS_IN_SECOND, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); |
-var {Filter, CommentFilter} = require("filterClasses"); |
-var {FilterStorage} = require("filterStorage"); |
-var {FilterNotifier} = require("filterNotifier"); |
-var {Prefs} = require("prefs"); |
-var {Subscription, DownloadableSubscription} = require("subscriptionClasses"); |
-var {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"); |
-var INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; |
-var CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; |
-var 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} |
*/ |
-var downloader = null; |
+let downloader = null; |
/** |
* This object is responsible for downloading filter subscriptions whenever |
* necessary. |
* @class |
*/ |
-var Synchronizer = exports.Synchronizer = |
+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 +71,23 @@ var 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 +95,7 @@ var Synchronizer = exports.Synchronizer = |
/** |
* Yields Downloadable instances for all subscriptions that can be downloaded. |
*/ |
- _getDownloadables: function*() |
+ *_getDownloadables() |
{ |
if (!Prefs.subscriptions_autoupdate) |
return; |
@@ -102,8 +109,11 @@ var 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) |
@@ -117,27 +127,34 @@ var 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.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. |
@@ -175,8 +192,10 @@ var 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; |
@@ -194,7 +213,9 @@ var 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; |
@@ -244,7 +265,10 @@ var 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); |
@@ -268,7 +292,8 @@ var 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); |
@@ -279,18 +304,26 @@ var 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; |
@@ -299,7 +332,7 @@ var 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; |
@@ -308,17 +341,21 @@ var 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(); |