| 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 | 
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   95  |   95  | 
|   96   // Compare version suffix (e.g. 0.1alpha < 0.1b1 < 01.b2 < 0.1). |   96   // Compare version suffix (e.g. 0.1alpha < 0.1b1 < 01.b2 < 0.1). | 
|   97   // However, note that this is a simple string comparision, meaning: b10 < b2 |   97   // However, note that this is a simple string comparision, meaning: b10 < b2 | 
|   98   if (tail1 == tail2) |   98   if (tail1 == tail2) | 
|   99     return 0; |   99     return 0; | 
|  100   if (!tail1 || tail2 && tail1 > tail2) |  100   if (!tail1 || tail2 && tail1 > tail2) | 
|  101     return 1; |  101     return 1; | 
|  102   return -1; |  102   return -1; | 
|  103 } |  103 } | 
|  104  |  104  | 
 |  105 function initNotificationMatcher(notification) | 
 |  106 { | 
 |  107   if ("_matcher" in notification || !(notification.urlFilters instanceof Array)) | 
 |  108     return; | 
 |  109  | 
 |  110   let matcher = new Matcher(); | 
 |  111   for (let urlFilter of notification.urlFilters) | 
 |  112     matcher.add(Filter.fromText(urlFilter)); | 
 |  113   matcher.toJSON = () => {}; | 
 |  114   notification._matcher = matcher; | 
 |  115 } | 
 |  116  | 
|  105 /** |  117 /** | 
|  106  * The object providing actual downloading functionality. |  118  * The object providing actual downloading functionality. | 
|  107  * @type {Downloader} |  119  * @type {Downloader} | 
|  108  */ |  120  */ | 
|  109 let downloader = null; |  121 let downloader = null; | 
|  110 let localData = []; |  122 let localData = []; | 
 |  123 let remoteData = []; | 
|  111  |  124  | 
|  112 /** |  125 /** | 
|  113  * Regularly fetches notifications and decides which to show. |  126  * Regularly fetches notifications and decides which to show. | 
|  114  * @class |  127  * @class | 
|  115  */ |  128  */ | 
|  116 let Notification = exports.Notification = |  129 let Notification = exports.Notification = | 
|  117 { |  130 { | 
|  118   /** |  131   /** | 
|  119    * Called on module startup. |  132    * Called on module startup. | 
|  120    */ |  133    */ | 
|  121   init() |  134   init() | 
|  122   { |  135   { | 
 |  136     let notificationdata = Prefs.notificationdata.data; | 
 |  137     if (notificationdata) | 
 |  138     { | 
 |  139       for (let notification of notificationdata.notifications) | 
 |  140         initNotificationMatcher(notification); | 
 |  141       remoteData = notificationdata.notifications; | 
 |  142     } | 
 |  143  | 
|  123     downloader = new Downloader(this._getDownloadables.bind(this), |  144     downloader = new Downloader(this._getDownloadables.bind(this), | 
|  124                                 INITIAL_DELAY, CHECK_INTERVAL); |  145                                 INITIAL_DELAY, CHECK_INTERVAL); | 
|  125     downloader.onExpirationChange = this._onExpirationChange.bind(this); |  146     downloader.onExpirationChange = this._onExpirationChange.bind(this); | 
|  126     downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this); |  147     downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this); | 
|  127     downloader.onDownloadError = this._onDownloadError.bind(this); |  148     downloader.onDownloadError = this._onDownloadError.bind(this); | 
|  128     onShutdown.add(() => downloader.cancel()); |  149     onShutdown.add(() => downloader.cancel()); | 
|  129   }, |  150   }, | 
|  130  |  151  | 
|  131   /** |  152   /** | 
|  132    * Yields a Downloadable instances for the notifications download. |  153    * Yields a Downloadable instances for the notifications download. | 
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  167     { |  188     { | 
|  168       let data = JSON.parse(responseText); |  189       let data = JSON.parse(responseText); | 
|  169       for (let notification of data.notifications) |  190       for (let notification of data.notifications) | 
|  170       { |  191       { | 
|  171         if ("severity" in notification) |  192         if ("severity" in notification) | 
|  172         { |  193         { | 
|  173           if (!("type" in notification)) |  194           if (!("type" in notification)) | 
|  174             notification.type = notification.severity; |  195             notification.type = notification.severity; | 
|  175           delete notification.severity; |  196           delete notification.severity; | 
|  176         } |  197         } | 
 |  198         initNotificationMatcher(notification); | 
|  177       } |  199       } | 
 |  200       remoteData = data.notifications; | 
|  178       Prefs.notificationdata.data = data; |  201       Prefs.notificationdata.data = data; | 
|  179     } |  202     } | 
|  180     catch (e) |  203     catch (e) | 
|  181     { |  204     { | 
|  182       Cu.reportError(e); |  205       Cu.reportError(e); | 
|  183       errorCallback("synchronize_invalid_data"); |  206       errorCallback("synchronize_invalid_data"); | 
|  184       return; |  207       return; | 
|  185     } |  208     } | 
|  186  |  209  | 
|  187     Prefs.notificationdata.lastError = 0; |  210     Prefs.notificationdata.lastError = 0; | 
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  226       showListeners.splice(index, 1); |  249       showListeners.splice(index, 1); | 
|  227   }, |  250   }, | 
|  228  |  251  | 
|  229   /** |  252   /** | 
|  230    * Determines which notification is to be shown next. |  253    * Determines which notification is to be shown next. | 
|  231    * @param {string} url URL to match notifications to (optional) |  254    * @param {string} url URL to match notifications to (optional) | 
|  232    * @return {Object} notification to be shown, or null if there is none |  255    * @return {Object} notification to be shown, or null if there is none | 
|  233    */ |  256    */ | 
|  234   _getNextToShow(url) |  257   _getNextToShow(url) | 
|  235   { |  258   { | 
|  236     let remoteData = []; |  | 
|  237     if (typeof Prefs.notificationdata.data == "object" && |  | 
|  238         Prefs.notificationdata.data.notifications instanceof Array) |  | 
|  239     { |  | 
|  240       remoteData = Prefs.notificationdata.data.notifications; |  | 
|  241     } |  | 
|  242  |  | 
|  243     let notifications = localData.concat(remoteData); |  259     let notifications = localData.concat(remoteData); | 
|  244     if (notifications.length === 0) |  260     if (notifications.length === 0) | 
|  245       return null; |  261       return null; | 
|  246  |  262  | 
|  247     const {addonName, addonVersion, application, |  263     const {addonName, addonVersion, application, | 
|  248            applicationVersion, platform, platformVersion} = require("info"); |  264            applicationVersion, platform, platformVersion} = require("info"); | 
|  249  |  265  | 
|  250     let targetChecks = { |  266     let targetChecks = { | 
|  251       extension: v => v == addonName, |  267       extension: v => v == addonName, | 
|  252       extensionMinVersion: |  268       extensionMinVersion: | 
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  291             continue; |  307             continue; | 
|  292         } |  308         } | 
|  293  |  309  | 
|  294         if (notification.type !== "relentless" && |  310         if (notification.type !== "relentless" && | 
|  295             Prefs.notifications_ignoredcategories.indexOf("*") != -1) |  311             Prefs.notifications_ignoredcategories.indexOf("*") != -1) | 
|  296         { |  312         { | 
|  297           continue; |  313           continue; | 
|  298         } |  314         } | 
|  299       } |  315       } | 
|  300  |  316  | 
|  301       if (typeof url === "string" || notification.urlFilters instanceof Array) |  317       if (typeof url === "string" || "_matcher" in notification) | 
|  302       { |  318       { | 
|  303         if (Prefs.enabled && typeof url === "string" && |  319         if (Prefs.enabled && typeof url === "string" && | 
|  304             notification.urlFilters instanceof Array) |  320             "_matcher" in notification) | 
|  305         { |  321         { | 
|  306           let host; |  322           let host; | 
|  307           try |  323           try | 
|  308           { |  324           { | 
|  309             host = new URL(url).hostname; |  325             host = new URL(url).hostname; | 
|  310           } |  326           } | 
|  311           catch (e) |  327           catch (e) | 
|  312           { |  328           { | 
|  313             host = ""; |  329             host = ""; | 
|  314           } |  330           } | 
|  315  |  331  | 
|  316           let exception = defaultMatcher.matchesAny( |  332           let exception = defaultMatcher.matchesAny( | 
|  317             url, RegExpFilter.typeMap.DOCUMENT, host, false, null |  333             url, RegExpFilter.typeMap.DOCUMENT, host, false, null | 
|  318           ); |  334           ); | 
|  319           if (exception instanceof WhitelistFilter) |  335           if (exception instanceof WhitelistFilter) | 
|  320             continue; |  336             continue; | 
|  321  |  337  | 
|  322           let matcher = new Matcher(); |  338           if (!notification._matcher.matchesAny(url, | 
|  323           for (let urlFilter of notification.urlFilters) |  339               RegExpFilter.typeMap.DOCUMENT, host, false, null)) | 
|  324             matcher.add(Filter.fromText(urlFilter)); |  | 
|  325           if (!matcher.matchesAny(url, RegExpFilter.typeMap.DOCUMENT, host, |  | 
|  326               false, null)) |  | 
|  327           { |  | 
|  328             continue; |  340             continue; | 
|  329           } |  | 
|  330         } |  341         } | 
|  331         else |  342         else | 
|  332           continue; |  343           continue; | 
|  333       } |  344       } | 
|  334  |  345  | 
|  335       if (notification.targets instanceof Array) |  346       if (notification.targets instanceof Array) | 
|  336       { |  347       { | 
|  337         let match = false; |  348         let match = false; | 
|  338  |  349  | 
|  339         for (let target of notification.targets) |  350         for (let target of notification.targets) | 
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  426     return localizedTexts; |  437     return localizedTexts; | 
|  427   }, |  438   }, | 
|  428  |  439  | 
|  429   /** |  440   /** | 
|  430    * Adds a local notification. |  441    * Adds a local notification. | 
|  431    * @param {Object} notification notification to add |  442    * @param {Object} notification notification to add | 
|  432    */ |  443    */ | 
|  433   addNotification(notification) |  444   addNotification(notification) | 
|  434   { |  445   { | 
|  435     if (localData.indexOf(notification) == -1) |  446     if (localData.indexOf(notification) == -1) | 
 |  447     { | 
 |  448       initNotificationMatcher(notification); | 
|  436       localData.push(notification); |  449       localData.push(notification); | 
 |  450     } | 
|  437   }, |  451   }, | 
|  438  |  452  | 
|  439   /** |  453   /** | 
|  440    * Removes an existing local notification. |  454    * Removes an existing local notification. | 
|  441    * @param {Object} notification notification to remove |  455    * @param {Object} notification notification to remove | 
|  442    */ |  456    */ | 
|  443   removeNotification(notification) |  457   removeNotification(notification) | 
|  444   { |  458   { | 
|  445     let index = localData.indexOf(notification); |  459     let index = localData.indexOf(notification); | 
|  446     if (index > -1) |  460     if (index > -1) | 
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  514     else if (index != -1 && forceValue !== true) |  528     else if (index != -1 && forceValue !== true) | 
|  515       categories.splice(index, 1); |  529       categories.splice(index, 1); | 
|  516  |  530  | 
|  517     // HACK: JSON values aren't saved unless they are assigned a |  531     // HACK: JSON values aren't saved unless they are assigned a | 
|  518     // different object. |  532     // different object. | 
|  519     Prefs.notifications_ignoredcategories = |  533     Prefs.notifications_ignoredcategories = | 
|  520       JSON.parse(JSON.stringify(categories)); |  534       JSON.parse(JSON.stringify(categories)); | 
|  521   } |  535   } | 
|  522 }; |  536 }; | 
|  523 Notification.init(); |  537 Notification.init(); | 
| OLD | NEW |