| 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 20 matching lines...) Expand all Loading... |
| 31 | 31 |
| 32 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; | 32 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; |
| 33 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; | 33 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; |
| 34 const EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; | 34 const EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; |
| 35 const TYPE = { | 35 const TYPE = { |
| 36 information: 0, | 36 information: 0, |
| 37 question: 1, | 37 question: 1, |
| 38 relentless: 2, | 38 relentless: 2, |
| 39 critical: 3 | 39 critical: 3 |
| 40 }; | 40 }; |
| 41 const MATCHER = Symbol("Notification matcher"); |
| 41 | 42 |
| 42 let showListeners = []; | 43 let showListeners = []; |
| 43 let questionListeners = {}; | 44 let questionListeners = {}; |
| 44 | 45 |
| 45 function getNumericalSeverity(notification) | 46 function getNumericalSeverity(notification) |
| 46 { | 47 { |
| 47 if (notification.type in TYPE) | 48 if (notification.type in TYPE) |
| 48 return TYPE[notification.type]; | 49 return TYPE[notification.type]; |
| 49 return TYPE.information; | 50 return TYPE.information; |
| 50 } | 51 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // Compare version suffix (e.g. 0.1alpha < 0.1b1 < 01.b2 < 0.1). | 97 // 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 | 98 // However, note that this is a simple string comparision, meaning: b10 < b2 |
| 98 if (tail1 == tail2) | 99 if (tail1 == tail2) |
| 99 return 0; | 100 return 0; |
| 100 if (!tail1 || tail2 && tail1 > tail2) | 101 if (!tail1 || tail2 && tail1 > tail2) |
| 101 return 1; | 102 return 1; |
| 102 return -1; | 103 return -1; |
| 103 } | 104 } |
| 104 | 105 |
| 105 /** | 106 /** |
| 107 * Initializes notification's matcher based on notification's URL filters |
| 108 * @param {Object} notification |
| 109 */ |
| 110 function initNotificationMatcher(notification) |
| 111 { |
| 112 if (MATCHER in notification || !(notification.urlFilters instanceof Array)) |
| 113 return; |
| 114 |
| 115 let matcher = new Matcher(); |
| 116 for (let urlFilter of notification.urlFilters) |
| 117 { |
| 118 matcher.add(Filter.fromText(urlFilter)); |
| 119 } |
| 120 notification[MATCHER] = matcher; |
| 121 } |
| 122 |
| 123 /** |
| 124 * Matches URL against notification's URL filters |
| 125 * @param {Object} notification |
| 126 * @param {string} [url] |
| 127 * @return {boolean} whether notification and URL match |
| 128 */ |
| 129 function matchesUrl(notification, url) |
| 130 { |
| 131 // No matching necessary if there's nothing to match |
| 132 if (typeof url !== "string" && !(MATCHER in notification)) |
| 133 return true; |
| 134 |
| 135 // Notification shouldn't match if extension is disabled |
| 136 if (!Prefs.enabled) |
| 137 return false; |
| 138 |
| 139 // Notification shouldn't match if matching cannot be done |
| 140 if (typeof url !== "string" || !(MATCHER in notification)) |
| 141 return false; |
| 142 |
| 143 let host; |
| 144 try |
| 145 { |
| 146 host = new URL(url).hostname; |
| 147 } |
| 148 catch (e) |
| 149 { |
| 150 host = ""; |
| 151 } |
| 152 |
| 153 // Notification shouldn't match if extension is disabled on provided domain |
| 154 let exception = defaultMatcher.matchesAny( |
| 155 url, RegExpFilter.typeMap.DOCUMENT, host, false, null |
| 156 ); |
| 157 if (exception instanceof WhitelistFilter) |
| 158 return false; |
| 159 |
| 160 // Notification should match if one of its filters matches |
| 161 let filter = notification[MATCHER].matchesAny( |
| 162 url, RegExpFilter.typeMap.DOCUMENT, host, false, null |
| 163 ); |
| 164 return !!filter; |
| 165 } |
| 166 |
| 167 /** |
| 106 * The object providing actual downloading functionality. | 168 * The object providing actual downloading functionality. |
| 107 * @type {Downloader} | 169 * @type {Downloader} |
| 108 */ | 170 */ |
| 109 let downloader = null; | 171 let downloader = null; |
| 172 |
| 173 /** |
| 174 * List of notifications provided by the extension |
| 175 * @type {Object[]} |
| 176 */ |
| 110 let localData = []; | 177 let localData = []; |
| 111 | 178 |
| 112 /** | 179 /** |
| 113 * Regularly fetches notifications and decides which to show. | 180 * Regularly fetches notifications and decides which to show. |
| 114 * @class | 181 * @class |
| 115 */ | 182 */ |
| 116 let Notification = exports.Notification = | 183 let Notification = exports.Notification = |
| 117 { | 184 { |
| 118 /** | 185 /** |
| 119 * Called on module startup. | 186 * Called on module startup. |
| 120 */ | 187 */ |
| 121 init() | 188 init() |
| 122 { | 189 { |
| 190 let {data} = Prefs.notificationdata; |
| 191 if (data) |
| 192 { |
| 193 for (let notification of data.notifications) |
| 194 { |
| 195 initNotificationMatcher(notification); |
| 196 } |
| 197 } |
| 198 |
| 123 downloader = new Downloader(this._getDownloadables.bind(this), | 199 downloader = new Downloader(this._getDownloadables.bind(this), |
| 124 INITIAL_DELAY, CHECK_INTERVAL); | 200 INITIAL_DELAY, CHECK_INTERVAL); |
| 125 downloader.onExpirationChange = this._onExpirationChange.bind(this); | 201 downloader.onExpirationChange = this._onExpirationChange.bind(this); |
| 126 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this); | 202 downloader.onDownloadSuccess = this._onDownloadSuccess.bind(this); |
| 127 downloader.onDownloadError = this._onDownloadError.bind(this); | 203 downloader.onDownloadError = this._onDownloadError.bind(this); |
| 128 onShutdown.add(() => downloader.cancel()); | 204 onShutdown.add(() => downloader.cancel()); |
| 129 }, | 205 }, |
| 130 | 206 |
| 131 /** | 207 /** |
| 132 * Yields a Downloadable instances for the notifications download. | 208 * Yields a Downloadable instances for the notifications download. |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 { | 243 { |
| 168 let data = JSON.parse(responseText); | 244 let data = JSON.parse(responseText); |
| 169 for (let notification of data.notifications) | 245 for (let notification of data.notifications) |
| 170 { | 246 { |
| 171 if ("severity" in notification) | 247 if ("severity" in notification) |
| 172 { | 248 { |
| 173 if (!("type" in notification)) | 249 if (!("type" in notification)) |
| 174 notification.type = notification.severity; | 250 notification.type = notification.severity; |
| 175 delete notification.severity; | 251 delete notification.severity; |
| 176 } | 252 } |
| 253 initNotificationMatcher(notification); |
| 177 } | 254 } |
| 178 Prefs.notificationdata.data = data; | 255 Prefs.notificationdata.data = data; |
| 179 } | 256 } |
| 180 catch (e) | 257 catch (e) |
| 181 { | 258 { |
| 182 Cu.reportError(e); | 259 Cu.reportError(e); |
| 183 errorCallback("synchronize_invalid_data"); | 260 errorCallback("synchronize_invalid_data"); |
| 184 return; | 261 return; |
| 185 } | 262 } |
| 186 | 263 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 showListeners.splice(index, 1); | 303 showListeners.splice(index, 1); |
| 227 }, | 304 }, |
| 228 | 305 |
| 229 /** | 306 /** |
| 230 * Determines which notification is to be shown next. | 307 * Determines which notification is to be shown next. |
| 231 * @param {string} url URL to match notifications to (optional) | 308 * @param {string} url URL to match notifications to (optional) |
| 232 * @return {Object} notification to be shown, or null if there is none | 309 * @return {Object} notification to be shown, or null if there is none |
| 233 */ | 310 */ |
| 234 _getNextToShow(url) | 311 _getNextToShow(url) |
| 235 { | 312 { |
| 236 let remoteData = []; | 313 let remoteData = Prefs.notificationdata.data.notifications; |
| 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); | 314 let notifications = localData.concat(remoteData); |
| 244 if (notifications.length === 0) | 315 if (notifications.length === 0) |
| 245 return null; | 316 return null; |
| 246 | 317 |
| 247 const {addonName, addonVersion, application, | 318 const {addonName, addonVersion, application, |
| 248 applicationVersion, platform, platformVersion} = require("info"); | 319 applicationVersion, platform, platformVersion} = require("info"); |
| 249 | 320 |
| 250 let targetChecks = { | 321 let targetChecks = { |
| 251 extension: v => v == addonName, | 322 extension: v => v == addonName, |
| 252 extensionMinVersion: | 323 extensionMinVersion: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 continue; | 362 continue; |
| 292 } | 363 } |
| 293 | 364 |
| 294 if (notification.type !== "relentless" && | 365 if (notification.type !== "relentless" && |
| 295 Prefs.notifications_ignoredcategories.indexOf("*") != -1) | 366 Prefs.notifications_ignoredcategories.indexOf("*") != -1) |
| 296 { | 367 { |
| 297 continue; | 368 continue; |
| 298 } | 369 } |
| 299 } | 370 } |
| 300 | 371 |
| 301 if (typeof url === "string" || notification.urlFilters instanceof Array) | 372 if (!matchesUrl(notification, url)) |
| 302 { | 373 continue; |
| 303 if (Prefs.enabled && typeof url === "string" && | |
| 304 notification.urlFilters instanceof Array) | |
| 305 { | |
| 306 let host; | |
| 307 try | |
| 308 { | |
| 309 host = new URL(url).hostname; | |
| 310 } | |
| 311 catch (e) | |
| 312 { | |
| 313 host = ""; | |
| 314 } | |
| 315 | |
| 316 let exception = defaultMatcher.matchesAny( | |
| 317 url, RegExpFilter.typeMap.DOCUMENT, host, false, null | |
| 318 ); | |
| 319 if (exception instanceof WhitelistFilter) | |
| 320 continue; | |
| 321 | |
| 322 let matcher = new Matcher(); | |
| 323 for (let urlFilter of notification.urlFilters) | |
| 324 matcher.add(Filter.fromText(urlFilter)); | |
| 325 if (!matcher.matchesAny(url, RegExpFilter.typeMap.DOCUMENT, host, | |
| 326 false, null)) | |
| 327 { | |
| 328 continue; | |
| 329 } | |
| 330 } | |
| 331 else | |
| 332 continue; | |
| 333 } | |
| 334 | 374 |
| 335 if (notification.targets instanceof Array) | 375 if (notification.targets instanceof Array) |
| 336 { | 376 { |
| 337 let match = false; | 377 let match = false; |
| 338 | 378 |
| 339 for (let target of notification.targets) | 379 for (let target of notification.targets) |
| 340 { | 380 { |
| 341 if (Object.keys(target).every(key => | 381 if (Object.keys(target).every(key => |
| 342 targetChecks.hasOwnProperty(key) && | 382 targetChecks.hasOwnProperty(key) && |
| 343 targetChecks[key](target[key]))) | 383 targetChecks[key](target[key]))) |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 return localizedTexts; | 466 return localizedTexts; |
| 427 }, | 467 }, |
| 428 | 468 |
| 429 /** | 469 /** |
| 430 * Adds a local notification. | 470 * Adds a local notification. |
| 431 * @param {Object} notification notification to add | 471 * @param {Object} notification notification to add |
| 432 */ | 472 */ |
| 433 addNotification(notification) | 473 addNotification(notification) |
| 434 { | 474 { |
| 435 if (localData.indexOf(notification) == -1) | 475 if (localData.indexOf(notification) == -1) |
| 476 { |
| 477 initNotificationMatcher(notification); |
| 436 localData.push(notification); | 478 localData.push(notification); |
| 479 } |
| 437 }, | 480 }, |
| 438 | 481 |
| 439 /** | 482 /** |
| 440 * Removes an existing local notification. | 483 * Removes an existing local notification. |
| 441 * @param {Object} notification notification to remove | 484 * @param {Object} notification notification to remove |
| 442 */ | 485 */ |
| 443 removeNotification(notification) | 486 removeNotification(notification) |
| 444 { | 487 { |
| 445 let index = localData.indexOf(notification); | 488 let index = localData.indexOf(notification); |
| 446 if (index > -1) | 489 if (index > -1) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 else if (index != -1 && forceValue !== true) | 557 else if (index != -1 && forceValue !== true) |
| 515 categories.splice(index, 1); | 558 categories.splice(index, 1); |
| 516 | 559 |
| 517 // HACK: JSON values aren't saved unless they are assigned a | 560 // HACK: JSON values aren't saved unless they are assigned a |
| 518 // different object. | 561 // different object. |
| 519 Prefs.notifications_ignoredcategories = | 562 Prefs.notifications_ignoredcategories = |
| 520 JSON.parse(JSON.stringify(categories)); | 563 JSON.parse(JSON.stringify(categories)); |
| 521 } | 564 } |
| 522 }; | 565 }; |
| 523 Notification.init(); | 566 Notification.init(); |
| OLD | NEW |