| OLD | NEW | 
|    1 /* |    1 /* | 
|    2  * This file is part of Adblock Plus <http://adblockplus.org/>, |    2  * This file is part of Adblock Plus <http://adblockplus.org/>, | 
|    3  * Copyright (C) 2006-2013 Eyeo GmbH |    3  * Copyright (C) 2006-2013 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 /** |   18 /** | 
|   19  * @fileOverview Handles notifications. |   19  * @fileOverview Handles notifications. | 
|   20  */ |   20  */ | 
|   21  |   21  | 
|   22 Cu.import("resource://gre/modules/Services.jsm"); |   22 Cu.import("resource://gre/modules/Services.jsm"); | 
|   23  |   23  | 
|   24 let {TimeLine} = require("timeline"); |   24 let {TimeLine} = require("timeline"); | 
|   25 let {Prefs} = require("prefs"); |   25 let {Prefs} = require("prefs"); | 
|   26 let {Downloader, Downloadable, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} 
     = require("downloader"); |   26 let {Downloader, Downloadable, MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} 
     = require("downloader"); | 
|   27 let {Utils} = require("utils"); |   27 let {Utils} = require("utils"); | 
 |   28 let {Matcher} = require("matcher"); | 
 |   29 let {Filter} = require("filterClasses"); | 
|   28  |   30  | 
|   29 let INITIAL_DELAY = 12 * MILLIS_IN_MINUTE; |   31 let INITIAL_DELAY = 12 * MILLIS_IN_MINUTE; | 
|   30 let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; |   32 let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; | 
|   31 let EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; |   33 let EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; | 
 |   34 let SEVERITY = { | 
 |   35   information: 0, | 
 |   36   question: 1, | 
 |   37   critical: 2 | 
 |   38 }; | 
|   32  |   39  | 
|   33 function getNumericalSeverity(notification) |   40 function getNumericalSeverity(notification) | 
|   34 { |   41 { | 
|   35   let levels = {information: 0, critical: 1}; |   42   return (notification.severity in SEVERITY ? SEVERITY[notification.severity] : 
     SEVERITY.information); | 
|   36   return (notification.severity in levels ? levels[notification.severity] : leve
     ls.information); |  | 
|   37 } |   43 } | 
|   38  |   44  | 
|   39 function saveNotificationData() |   45 function saveNotificationData() | 
|   40 { |   46 { | 
|   41   // HACK: JSON values aren't saved unless they are assigned a different object. |   47   // HACK: JSON values aren't saved unless they are assigned a different object. | 
|   42   Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); |   48   Prefs.notificationdata = JSON.parse(JSON.stringify(Prefs.notificationdata)); | 
|   43 } |   49 } | 
|   44  |   50  | 
|   45 function localize(translations, locale) |   51 function localize(translations, locale) | 
|   46 { |   52 { | 
|   47   if (locale in translations) |   53   if (locale in translations) | 
|   48     return translations[locale]; |   54     return translations[locale]; | 
|   49  |   55  | 
|   50   let languagePart = locale.substring(0, locale.indexOf("-")); |   56   let languagePart = locale.substring(0, locale.indexOf("-")); | 
|   51   if (languagePart && languagePart in translations) |   57   if (languagePart && languagePart in translations) | 
|   52     return translations[languagePart]; |   58     return translations[languagePart]; | 
|   53  |   59  | 
|   54   let defaultLocale = "en-US"; |   60   let defaultLocale = "en-US"; | 
|   55   return translations[defaultLocale]; |   61   return translations[defaultLocale]; | 
|   56 } |   62 } | 
|   57  |   63  | 
|   58 /** |   64 /** | 
|   59  * The object providing actual downloading functionality. |   65  * The object providing actual downloading functionality. | 
|   60  * @type Downloader |   66  * @type Downloader | 
|   61  */ |   67  */ | 
|   62 let downloader = null; |   68 let downloader = null; | 
 |   69 let localData = []; | 
|   63  |   70  | 
|   64 /** |   71 /** | 
|   65  * Regularly fetches notifications and decides which to show. |   72  * Regularly fetches notifications and decides which to show. | 
|   66  * @class |   73  * @class | 
|   67  */ |   74  */ | 
|   68 let Notification = exports.Notification = |   75 let Notification = exports.Notification = | 
|   69 { |   76 { | 
|   70   /** |   77   /** | 
|   71    * Called on module startup. |   78    * Called on module startup. | 
|   72    */ |   79    */ | 
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  135  |  142  | 
|  136   _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re
     sponseStatus, redirectCallback) |  143   _onDownloadError: function(downloadable, downloadURL, error, channelStatus, re
     sponseStatus, redirectCallback) | 
|  137   { |  144   { | 
|  138     Prefs.notificationdata.lastError = Date.now(); |  145     Prefs.notificationdata.lastError = Date.now(); | 
|  139     Prefs.notificationdata.downloadStatus = error; |  146     Prefs.notificationdata.downloadStatus = error; | 
|  140     saveNotificationData(); |  147     saveNotificationData(); | 
|  141   }, |  148   }, | 
|  142  |  149  | 
|  143   /** |  150   /** | 
|  144    * Determines which notification is to be shown next. |  151    * Determines which notification is to be shown next. | 
|  145    * @param {Array of Object} notifications active notifications |  152    * @param {String} url URL to match notifications to | 
|  146    * @return {Object} notification to be shown, or null if there is none |  153    * @return {Object} notification to be shown, or null if there is none | 
|  147    */ |  154    */ | 
|  148   getNextToShow: function() |  155   getNextToShow: function(url) | 
|  149   { |  156   { | 
|  150     function checkTarget(target, parameter, name, version) |  157     function checkTarget(target, parameter, name, version) | 
|  151     { |  158     { | 
|  152       let minVersionKey = parameter + "MinVersion"; |  159       let minVersionKey = parameter + "MinVersion"; | 
|  153       let maxVersionKey = parameter + "MaxVersion"; |  160       let maxVersionKey = parameter + "MaxVersion"; | 
|  154       return !((parameter in target && target[parameter] != name) || |  161       return !((parameter in target && target[parameter] != name) || | 
|  155                (minVersionKey in target && Services.vc.compare(version, target[m
     inVersionKey]) < 0) || |  162                (minVersionKey in target && Services.vc.compare(version, target[m
     inVersionKey]) < 0) || | 
|  156                (maxVersionKey in target && Services.vc.compare(version, target[m
     axVersionKey]) > 0)); |  163                (maxVersionKey in target && Services.vc.compare(version, target[m
     axVersionKey]) > 0)); | 
|  157  |  | 
|  158     } |  164     } | 
|  159  |  165  | 
|  160     if (typeof Prefs.notificationdata.data != "object" || !(Prefs.notificationda
     ta.data.notifications instanceof Array)) |  166     let remoteData = []; | 
|  161       return null; |  167     if (typeof Prefs.notificationdata.data == "object" && Prefs.notificationdata
     .data.notifications instanceof Array) | 
 |  168       remoteData = Prefs.notificationdata.data.notifications; | 
|  162  |  169  | 
|  163     if (!(Prefs.notificationdata.shown instanceof Array)) |  170     if (!(Prefs.notificationdata.shown instanceof Array)) | 
|  164     { |  171     { | 
|  165       Prefs.notificationdata.shown = []; |  172       Prefs.notificationdata.shown = []; | 
|  166       saveNotificationData(); |  173       saveNotificationData(); | 
|  167     } |  174     } | 
|  168  |  175  | 
 |  176     let notifications = localData.concat(remoteData); | 
 |  177     if (notifications.length === 0) | 
 |  178       return null; | 
 |  179  | 
|  169     let {addonName, addonVersion, application, applicationVersion, platform, pla
     tformVersion} = require("info"); |  180     let {addonName, addonVersion, application, applicationVersion, platform, pla
     tformVersion} = require("info"); | 
|  170     let notifications = Prefs.notificationdata.data.notifications; |  | 
|  171     let notificationToShow = null; |  181     let notificationToShow = null; | 
|  172     for each (let notification in notifications) |  182     for each (let notification in notifications) | 
|  173     { |  183     { | 
|  174       if ((typeof notification.severity === "undefined" || notification.severity
      === "information") |  184       if ((typeof notification.severity === "undefined" || notification.severity
      !== "critical") | 
|  175           && Prefs.notificationdata.shown.indexOf(notification.id) !== -1) |  185           && Prefs.notificationdata.shown.indexOf(notification.id) !== -1) | 
|  176         continue; |  186         continue; | 
|  177  |  187  | 
 |  188       if (typeof url === "string" || notification.urlFilters instanceof Array) | 
 |  189       { | 
 |  190         if (typeof url === "string" && notification.urlFilters instanceof Array) | 
 |  191         { | 
 |  192           let matcher = new Matcher(); | 
 |  193           for each (let urlFilter in notification.urlFilters) | 
 |  194             matcher.add(Filter.fromText(urlFilter)); | 
 |  195           if (!matcher.matchesAny(url, "DOCUMENT", url)) | 
 |  196             continue; | 
 |  197         } | 
 |  198         else | 
 |  199           continue; | 
 |  200       } | 
 |  201  | 
|  178       if (notification.targets instanceof Array) |  202       if (notification.targets instanceof Array) | 
|  179       { |  203       { | 
|  180         let match = false; |  204         let match = false; | 
|  181         for each (let target in notification.targets) |  205         for each (let target in notification.targets) | 
|  182         { |  206         { | 
|  183           if (checkTarget(target, "extension", addonName, addonVersion) && |  207           if (checkTarget(target, "extension", addonName, addonVersion) && | 
|  184               checkTarget(target, "application", application, applicationVersion
     ) && |  208               checkTarget(target, "application", application, applicationVersion
     ) && | 
|  185               checkTarget(target, "platform", platform, platformVersion)) |  209               checkTarget(target, "platform", platform, platformVersion)) | 
|  186           { |  210           { | 
|  187             match = true; |  211             match = true; | 
|  188             break; |  212             break; | 
|  189           } |  213           } | 
|  190         } |  214         } | 
|  191         if (!match) |  215         if (!match) | 
|  192           continue; |  216           continue; | 
|  193       } |  217       } | 
|  194  |  218  | 
|  195       if (!notificationToShow |  219       if (!notificationToShow || getNumericalSeverity(notification) > getNumeric
     alSeverity(notificationToShow)) | 
|  196           || getNumericalSeverity(notification) > getNumericalSeverity(notificat
     ionToShow)) |  | 
|  197         notificationToShow = notification; |  220         notificationToShow = notification; | 
|  198     } |  221     } | 
|  199  |  222  | 
|  200     if (notificationToShow && "id" in notificationToShow) |  223     if (notificationToShow && "id" in notificationToShow) | 
|  201     { |  224     { | 
|  202       Prefs.notificationdata.shown.push(notificationToShow.id); |  225       if (notificationToShow.severity !== "question") | 
|  203       saveNotificationData(); |  226         this.markAsShown(notificationToShow.id); | 
|  204     } |  227     } | 
|  205  |  228  | 
|  206     return notificationToShow; |  229     return notificationToShow; | 
|  207   }, |  230   }, | 
|  208  |  231  | 
 |  232   markAsShown: function(id) | 
 |  233   { | 
 |  234     if (Prefs.notificationdata.shown.indexOf(id) > -1) | 
 |  235       return; | 
 |  236  | 
 |  237     Prefs.notificationdata.shown.push(id); | 
 |  238     saveNotificationData(); | 
 |  239   }, | 
 |  240  | 
|  209   /** |  241   /** | 
|  210    * Localizes the texts of the supplied notification. |  242    * Localizes the texts of the supplied notification. | 
|  211    * @param {Object} notification notification to translate |  243    * @param {Object} notification notification to translate | 
|  212    * @param {String} locale the target locale (optional, defaults to the |  244    * @param {String} locale the target locale (optional, defaults to the | 
|  213    *                        application locale) |  245    *                        application locale) | 
|  214    * @return {Object} the translated texts |  246    * @return {Object} the translated texts | 
|  215    */ |  247    */ | 
|  216   getLocalizedTexts: function(notification, locale) |  248   getLocalizedTexts: function(notification, locale) | 
|  217   { |  249   { | 
|  218     locale = locale || Utils.appLocale; |  250     locale = locale || Utils.appLocale; | 
|  219     let textKeys = ["title", "message"]; |  251     let textKeys = ["title", "message"]; | 
|  220     let localizedTexts = []; |  252     let localizedTexts = []; | 
|  221     for each (let key in textKeys) |  253     for each (let key in textKeys) | 
|  222     { |  254     { | 
|  223       if (key in notification) |  255       if (key in notification) | 
|  224         localizedTexts[key] = localize(notification[key], locale); |  256       { | 
 |  257         if (typeof notification[key] == "string") | 
 |  258           localizedTexts[key] = notification[key]; | 
 |  259         else | 
 |  260           localizedTexts[key] = localize(notification[key], locale); | 
 |  261       } | 
|  225     } |  262     } | 
|  226     return localizedTexts; |  263     return localizedTexts; | 
 |  264   }, | 
 |  265  | 
 |  266   /** | 
 |  267    * Adds a local notification. | 
 |  268    * @param {Object} notification notification to add | 
 |  269    */ | 
 |  270   addNotification: function(notification) | 
 |  271   { | 
 |  272     if (localData.indexOf(notification) == -1) | 
 |  273       localData.push(notification); | 
 |  274   }, | 
 |  275  | 
 |  276   /** | 
 |  277    * Removes an existing local notification. | 
 |  278    * @param {Object} notification notification to remove | 
 |  279    */ | 
 |  280   removeNotification: function(notification) | 
 |  281   { | 
 |  282     let index = localData.indexOf(notification); | 
 |  283     if (index > -1) | 
 |  284       localData.splice(index, 1); | 
|  227   } |  285   } | 
|  228 }; |  286 }; | 
|  229 Notification.init(); |  287 Notification.init(); | 
| OLD | NEW |