| Index: lib/notification.js |
| diff --git a/lib/notification.js b/lib/notification.js |
| index 7800469d02d74bb86bf6b17843204cfefc39c6f2..cb1956382fa814d34e6a3e6f6e241d396533992e 100644 |
| --- a/lib/notification.js |
| +++ b/lib/notification.js |
| @@ -15,34 +15,39 @@ |
| * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| +"use strict"; |
| + |
| /** |
| * @fileOverview Handles notifications. |
| */ |
| -Cu.import("resource://gre/modules/Services.jsm"); |
| +const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| -var {Prefs} = require("prefs"); |
| -var {Downloader, Downloadable, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); |
| -var {Utils} = require("utils"); |
| -var {Matcher, defaultMatcher} = require("matcher"); |
| -var {Filter, RegExpFilter, WhitelistFilter} = require("filterClasses"); |
| +const {Prefs} = require("prefs"); |
| +const {Downloader, Downloadable, |
| + MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); |
| +const {Utils} = require("utils"); |
| +const {Matcher, defaultMatcher} = require("matcher"); |
| +const {Filter, RegExpFilter, WhitelistFilter} = require("filterClasses"); |
| -var INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; |
| -var CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; |
| -var EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; |
| -var TYPE = { |
| +const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; |
| +const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; |
| +const EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; |
| +const TYPE = { |
| information: 0, |
| question: 1, |
| relentless: 2, |
| critical: 3 |
| }; |
| -var showListeners = []; |
| -var questionListeners = {}; |
| +let showListeners = []; |
| +let questionListeners = {}; |
| function getNumericalSeverity(notification) |
| { |
| - return (notification.type in TYPE ? TYPE[notification.type] : TYPE.information); |
| + if (notification.type in TYPE) |
| + return TYPE[notification.type]; |
| + return TYPE.information; |
| } |
| function saveNotificationData() |
| @@ -66,23 +71,24 @@ function localize(translations, locale) |
| /** |
| * The object providing actual downloading functionality. |
| - * @type Downloader |
| + * @type {Downloader} |
| */ |
| -var downloader = null; |
| -var localData = []; |
| +let downloader = null; |
| +let localData = []; |
| /** |
| * Regularly fetches notifications and decides which to show. |
| * @class |
| */ |
| -var Notification = exports.Notification = |
| +let Notification = exports.Notification = |
| { |
| /** |
| * Called on module startup. |
| */ |
| - init: function() |
| + init() |
| { |
| - downloader = new Downloader(this._getDownloadables.bind(this), INITIAL_DELAY, CHECK_INTERVAL); |
| + downloader = new Downloader(this._getDownloadables.bind(this), |
| + INITIAL_DELAY, CHECK_INTERVAL); |
| downloader.onExpirationChange = this._onExpirationChange.bind(this); |
| downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this); |
| downloader.onDownloadError = this._onDownloadError.bind(this); |
| @@ -92,15 +98,18 @@ var Notification = exports.Notification = |
| /** |
| * Yields a Downloadable instances for the notifications download. |
| */ |
| - _getDownloadables: function*() |
| + *_getDownloadables() |
| { |
| let downloadable = new Downloadable(Prefs.notificationurl); |
| if (typeof Prefs.notificationdata.lastError === "number") |
| downloadable.lastError = Prefs.notificationdata.lastError; |
| if (typeof Prefs.notificationdata.lastCheck === "number") |
| downloadable.lastCheck = Prefs.notificationdata.lastCheck; |
| - if (typeof Prefs.notificationdata.data === "object" && "version" in Prefs.notificationdata.data) |
| + if (typeof Prefs.notificationdata.data === "object" && |
| + "version" in Prefs.notificationdata.data) |
| + { |
| downloadable.lastVersion = Prefs.notificationdata.data.version; |
| + } |
| if (typeof Prefs.notificationdata.softExpiration === "number") |
| downloadable.softExpiration = Prefs.notificationdata.softExpiration; |
| if (typeof Prefs.notificationdata.hardExpiration === "number") |
| @@ -110,7 +119,7 @@ var Notification = exports.Notification = |
| yield downloadable; |
| }, |
| - _onExpirationChange: function(downloadable) |
| + _onExpirationChange(downloadable) |
| { |
| Prefs.notificationdata.lastCheck = downloadable.lastCheck; |
| Prefs.notificationdata.softExpiration = downloadable.softExpiration; |
| @@ -118,7 +127,8 @@ var Notification = exports.Notification = |
| saveNotificationData(); |
| }, |
| - _onDownloadSuccess: function(downloadable, responseText, errorCallback, redirectCallback) |
| + _onDownloadSuccess(downloadable, responseText, errorCallback, |
| + redirectCallback) |
| { |
| try |
| { |
| @@ -143,14 +153,18 @@ var Notification = exports.Notification = |
| Prefs.notificationdata.lastError = 0; |
| Prefs.notificationdata.downloadStatus = "synchronize_ok"; |
| - [Prefs.notificationdata.softExpiration, Prefs.notificationdata.hardExpiration] = downloader.processExpirationInterval(EXPIRATION_INTERVAL); |
| + [ |
| + Prefs.notificationdata.softExpiration, |
| + Prefs.notificationdata.hardExpiration |
| + ] = downloader.processExpirationInterval(EXPIRATION_INTERVAL); |
| Prefs.notificationdata.downloadCount = downloadable.downloadCount; |
| saveNotificationData(); |
| Notification.showNext(); |
| }, |
| - _onDownloadError: function(downloadable, downloadURL, error, channelStatus, responseStatus, redirectCallback) |
| + _onDownloadError(downloadable, downloadURL, error, channelStatus, |
| + responseStatus, redirectCallback) |
| { |
| Prefs.notificationdata.lastError = Date.now(); |
| Prefs.notificationdata.downloadStatus = error; |
| @@ -162,7 +176,7 @@ var Notification = exports.Notification = |
| * @param {Function} listener Listener to be invoked when a notification is |
| * to be shown |
| */ |
| - addShowListener: function(listener) |
| + addShowListener(listener) |
| { |
| if (showListeners.indexOf(listener) == -1) |
| showListeners.push(listener); |
| @@ -172,7 +186,7 @@ var Notification = exports.Notification = |
| * Removes the supplied listener. |
| * @param {Function} listener Listener that was added via addShowListener() |
| */ |
| - removeShowListener: function(listener) |
| + removeShowListener(listener) |
| { |
| let index = showListeners.indexOf(listener); |
| if (index != -1) |
| @@ -181,33 +195,40 @@ var Notification = exports.Notification = |
| /** |
| * Determines which notification is to be shown next. |
| - * @param {String} url URL to match notifications to (optional) |
| + * @param {string} url URL to match notifications to (optional) |
| * @return {Object} notification to be shown, or null if there is none |
| */ |
| - _getNextToShow: function(url) |
| + _getNextToShow(url) |
| { |
| function checkTarget(target, parameter, name, version) |
| { |
| let minVersionKey = parameter + "MinVersion"; |
| let maxVersionKey = parameter + "MaxVersion"; |
| return !((parameter in target && target[parameter] != name) || |
| - (minVersionKey in target && Services.vc.compare(version, target[minVersionKey]) < 0) || |
| - (maxVersionKey in target && Services.vc.compare(version, target[maxVersionKey]) > 0)); |
| + (minVersionKey in target && |
| + Services.vc.compare(version, target[minVersionKey]) < 0) || |
| + (maxVersionKey in target && |
| + Services.vc.compare(version, target[maxVersionKey]) > 0)); |
| } |
| let remoteData = []; |
| - if (typeof Prefs.notificationdata.data == "object" && Prefs.notificationdata.data.notifications instanceof Array) |
| + if (typeof Prefs.notificationdata.data == "object" && |
| + Prefs.notificationdata.data.notifications instanceof Array) |
| + { |
| remoteData = Prefs.notificationdata.data.notifications; |
| + } |
| let notifications = localData.concat(remoteData); |
| if (notifications.length === 0) |
| return null; |
| - let {addonName, addonVersion, application, applicationVersion, platform, platformVersion} = require("info"); |
| + const {addonName, addonVersion, application, |
| + applicationVersion, platform, platformVersion} = require("info"); |
| let notificationToShow = null; |
| for (let notification of notifications) |
| { |
| - if (typeof notification.type === "undefined" || notification.type !== "critical") |
| + if (typeof notification.type === "undefined" || |
| + notification.type !== "critical") |
| { |
| let shown; |
| if (typeof Prefs.notificationdata.shown == "object") |
| @@ -224,13 +245,17 @@ var Notification = exports.Notification = |
| continue; |
| } |
| - if (notification.type !== "relentless" && Prefs.notifications_ignoredcategories.indexOf("*") != -1) |
| + if (notification.type !== "relentless" && |
| + Prefs.notifications_ignoredcategories.indexOf("*") != -1) |
| + { |
| continue; |
| + } |
| } |
| if (typeof url === "string" || notification.urlFilters instanceof Array) |
| { |
| - if (Prefs.enabled && typeof url === "string" && notification.urlFilters instanceof Array) |
| + if (Prefs.enabled && typeof url === "string" && |
| + notification.urlFilters instanceof Array) |
| { |
| let host; |
| try |
| @@ -242,15 +267,20 @@ var Notification = exports.Notification = |
| host = ""; |
| } |
| - let exception = defaultMatcher.matchesAny(url, RegExpFilter.typeMap.DOCUMENT, host, false, null); |
| + let exception = defaultMatcher.matchesAny( |
| + url, RegExpFilter.typeMap.DOCUMENT, host, false, null |
| + ); |
| if (exception instanceof WhitelistFilter) |
| continue; |
| let matcher = new Matcher(); |
| for (let urlFilter of notification.urlFilters) |
| matcher.add(Filter.fromText(urlFilter)); |
| - if (!matcher.matchesAny(url, RegExpFilter.typeMap.DOCUMENT, host, false, null)) |
| + if (!matcher.matchesAny(url, RegExpFilter.typeMap.DOCUMENT, host, |
| + false, null)) |
| + { |
| continue; |
| + } |
| } |
| else |
| continue; |
| @@ -262,7 +292,8 @@ var Notification = exports.Notification = |
| for (let target of notification.targets) |
| { |
| if (checkTarget(target, "extension", addonName, addonVersion) && |
| - checkTarget(target, "application", application, applicationVersion) && |
| + checkTarget(target, "application", application, |
| + applicationVersion) && |
| checkTarget(target, "platform", platform, platformVersion)) |
| { |
| match = true; |
| @@ -273,8 +304,9 @@ var Notification = exports.Notification = |
| continue; |
| } |
| - if (!notificationToShow |
| - || getNumericalSeverity(notification) > getNumericalSeverity(notificationToShow)) |
| + if (!notificationToShow || |
| + getNumericalSeverity(notification) > |
| + getNumericalSeverity(notificationToShow)) |
| notificationToShow = notification; |
| } |
| @@ -284,21 +316,23 @@ var Notification = exports.Notification = |
| /** |
| * Invokes the listeners added via addShowListener() with the next |
| * notification to be shown. |
| - * @param {String} url URL to match notifications to (optional) |
| + * @param {string} url URL to match notifications to (optional) |
| */ |
| - showNext: function(url) |
| + showNext(url) |
| { |
| let notification = Notification._getNextToShow(url); |
| if (notification) |
| + { |
| for (let showListener of showListeners) |
| showListener(notification); |
| + } |
| }, |
| /** |
| * Marks a notification as shown. |
| - * @param {String} id ID of the notification to be marked as shown |
| + * @param {string} id ID of the notification to be marked as shown |
| */ |
| - markAsShown: function(id) |
| + markAsShown(id) |
| { |
| let now = Date.now(); |
| let data = Prefs.notificationdata; |
| @@ -322,11 +356,11 @@ var Notification = exports.Notification = |
| /** |
| * Localizes the texts of the supplied notification. |
| * @param {Object} notification notification to translate |
| - * @param {String} locale the target locale (optional, defaults to the |
| + * @param {string} locale the target locale (optional, defaults to the |
| * application locale) |
| * @return {Object} the translated texts |
| */ |
| - getLocalizedTexts: function(notification, locale) |
| + getLocalizedTexts(notification, locale) |
| { |
| locale = locale || Utils.appLocale; |
| let textKeys = ["title", "message"]; |
| @@ -348,7 +382,7 @@ var Notification = exports.Notification = |
| * Adds a local notification. |
| * @param {Object} notification notification to add |
| */ |
| - addNotification: function(notification) |
| + addNotification(notification) |
| { |
| if (localData.indexOf(notification) == -1) |
| localData.push(notification); |
| @@ -358,7 +392,7 @@ var Notification = exports.Notification = |
| * Removes an existing local notification. |
| * @param {Object} notification notification to remove |
| */ |
| - removeNotification: function(notification) |
| + removeNotification(notification) |
| { |
| let index = localData.indexOf(notification); |
| if (index > -1) |
| @@ -366,9 +400,18 @@ var Notification = exports.Notification = |
| }, |
| /** |
| + * A callback function which listens to see if notifications were approved. |
| + * |
| + * @callback QuestionListener |
| + * @param {boolean} approved |
| + */ |
| + |
| + /** |
| * Adds a listener for question-type notifications |
| + * @param {string} id |
| + * @param {QuestionListener} listener |
| */ |
| - addQuestionListener: function(/**string*/ id, /**function(approved)*/ listener) |
| + addQuestionListener(id, listener) |
| { |
| if (!(id in questionListeners)) |
| questionListeners[id] = []; |
| @@ -378,8 +421,10 @@ var Notification = exports.Notification = |
| /** |
| * Removes a listener that was previously added via addQuestionListener |
| + * @param {string} id |
| + * @param {QuestionListener} listener |
| */ |
| - removeQuestionListener: function(/**string*/ id, /**function(approved)*/ listener) |
| + removeQuestionListener(id, listener) |
| { |
| if (!(id in questionListeners)) |
| return; |
| @@ -392,10 +437,10 @@ var Notification = exports.Notification = |
| /** |
| * Notifies question listeners about interactions with a notification |
| - * @param {String} id notification ID |
| - * @param {Boolean} approved indicator whether notification has been approved or not |
| + * @param {string} id notification ID |
| + * @param {boolean} approved indicator whether notification has been approved |
| */ |
| - triggerQuestionListeners: function(id, approved) |
| + triggerQuestionListeners(id, approved) |
| { |
| if (!(id in questionListeners)) |
| return; |
| @@ -406,10 +451,10 @@ var Notification = exports.Notification = |
| /** |
| * Toggles whether notifications of a specific category should be ignored |
| - * @param {String} category notification category identifier |
| - * @param {Boolean} [forceValue] force specified value |
| + * @param {string} category notification category identifier |
| + * @param {boolean} [forceValue] force specified value |
| */ |
| - toggleIgnoreCategory: function(category, forceValue) |
| + toggleIgnoreCategory(category, forceValue) |
| { |
| let categories = Prefs.notifications_ignoredcategories; |
| let index = categories.indexOf(category); |
| @@ -421,8 +466,10 @@ var Notification = exports.Notification = |
| else if (index != -1 && forceValue !== true) |
| categories.splice(index, 1); |
| - // HACK: JSON values aren't saved unless they are assigned a different object. |
| - Prefs.notifications_ignoredcategories = JSON.parse(JSON.stringify(categories)); |
| + // HACK: JSON values aren't saved unless they are assigned a |
| + // different object. |
| + Prefs.notifications_ignoredcategories = |
| + JSON.parse(JSON.stringify(categories)); |
| } |
| }; |
| Notification.init(); |