LEFT | RIGHT |
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-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 "use strict"; | 18 "use strict"; |
19 | 19 |
20 /** | 20 /** |
21 * @fileOverview Handles notifications. | 21 * @fileOverview Handles notifications. |
22 */ | 22 */ |
23 | 23 |
24 Cu.import("resource://gre/modules/Services.jsm"); | 24 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
25 | 25 |
26 const {Prefs} = require("prefs"); | 26 const {Prefs} = require("prefs"); |
27 const {Downloader, Downloadable, | 27 const {Downloader, Downloadable, |
28 MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); | 28 MILLIS_IN_MINUTE, MILLIS_IN_HOUR, MILLIS_IN_DAY} = require("downloader"); |
29 const {Utils} = require("utils"); | 29 const {Utils} = require("utils"); |
30 const {Matcher, defaultMatcher} = require("matcher"); | 30 const {Matcher, defaultMatcher} = require("matcher"); |
31 const {Filter, RegExpFilter, WhitelistFilter} = require("filterClasses"); | 31 const {Filter, RegExpFilter, WhitelistFilter} = require("filterClasses"); |
32 | 32 |
33 let INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; | 33 const INITIAL_DELAY = 1 * MILLIS_IN_MINUTE; |
34 let CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; | 34 const CHECK_INTERVAL = 1 * MILLIS_IN_HOUR; |
35 let EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; | 35 const EXPIRATION_INTERVAL = 1 * MILLIS_IN_DAY; |
36 let TYPE = { | 36 const TYPE = { |
37 information: 0, | 37 information: 0, |
38 question: 1, | 38 question: 1, |
39 relentless: 2, | 39 relentless: 2, |
40 critical: 3 | 40 critical: 3 |
41 }; | 41 }; |
42 | 42 |
43 let showListeners = []; | 43 let showListeners = []; |
44 let questionListeners = {}; | 44 let questionListeners = {}; |
45 | 45 |
46 function getNumericalSeverity(notification) | 46 function getNumericalSeverity(notification) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 */ | 100 */ |
101 *_getDownloadables() | 101 *_getDownloadables() |
102 { | 102 { |
103 let downloadable = new Downloadable(Prefs.notificationurl); | 103 let downloadable = new Downloadable(Prefs.notificationurl); |
104 if (typeof Prefs.notificationdata.lastError === "number") | 104 if (typeof Prefs.notificationdata.lastError === "number") |
105 downloadable.lastError = Prefs.notificationdata.lastError; | 105 downloadable.lastError = Prefs.notificationdata.lastError; |
106 if (typeof Prefs.notificationdata.lastCheck === "number") | 106 if (typeof Prefs.notificationdata.lastCheck === "number") |
107 downloadable.lastCheck = Prefs.notificationdata.lastCheck; | 107 downloadable.lastCheck = Prefs.notificationdata.lastCheck; |
108 if (typeof Prefs.notificationdata.data === "object" && | 108 if (typeof Prefs.notificationdata.data === "object" && |
109 "version" in Prefs.notificationdata.data) | 109 "version" in Prefs.notificationdata.data) |
| 110 { |
110 downloadable.lastVersion = Prefs.notificationdata.data.version; | 111 downloadable.lastVersion = Prefs.notificationdata.data.version; |
| 112 } |
111 if (typeof Prefs.notificationdata.softExpiration === "number") | 113 if (typeof Prefs.notificationdata.softExpiration === "number") |
112 downloadable.softExpiration = Prefs.notificationdata.softExpiration; | 114 downloadable.softExpiration = Prefs.notificationdata.softExpiration; |
113 if (typeof Prefs.notificationdata.hardExpiration === "number") | 115 if (typeof Prefs.notificationdata.hardExpiration === "number") |
114 downloadable.hardExpiration = Prefs.notificationdata.hardExpiration; | 116 downloadable.hardExpiration = Prefs.notificationdata.hardExpiration; |
115 if (typeof Prefs.notificationdata.downloadCount === "number") | 117 if (typeof Prefs.notificationdata.downloadCount === "number") |
116 downloadable.downloadCount = Prefs.notificationdata.downloadCount; | 118 downloadable.downloadCount = Prefs.notificationdata.downloadCount; |
117 yield downloadable; | 119 yield downloadable; |
118 }, | 120 }, |
119 | 121 |
120 _onExpirationChange(downloadable) | 122 _onExpirationChange(downloadable) |
(...skipping 23 matching lines...) Expand all Loading... |
144 } | 146 } |
145 catch (e) | 147 catch (e) |
146 { | 148 { |
147 Cu.reportError(e); | 149 Cu.reportError(e); |
148 errorCallback("synchronize_invalid_data"); | 150 errorCallback("synchronize_invalid_data"); |
149 return; | 151 return; |
150 } | 152 } |
151 | 153 |
152 Prefs.notificationdata.lastError = 0; | 154 Prefs.notificationdata.lastError = 0; |
153 Prefs.notificationdata.downloadStatus = "synchronize_ok"; | 155 Prefs.notificationdata.downloadStatus = "synchronize_ok"; |
154 [Prefs.notificationdata.softExpiration, | 156 [ |
155 Prefs.notificationdata.hardExpiration] = | 157 Prefs.notificationdata.softExpiration, |
156 downloader.processExpirationInterval(EXPIRATION_INTERVAL); | 158 Prefs.notificationdata.hardExpiration |
| 159 ] = downloader.processExpirationInterval(EXPIRATION_INTERVAL); |
157 Prefs.notificationdata.downloadCount = downloadable.downloadCount; | 160 Prefs.notificationdata.downloadCount = downloadable.downloadCount; |
158 saveNotificationData(); | 161 saveNotificationData(); |
159 | 162 |
160 Notification.showNext(); | 163 Notification.showNext(); |
161 }, | 164 }, |
162 | 165 |
163 _onDownloadError(downloadable, downloadURL, error, channelStatus, | 166 _onDownloadError(downloadable, downloadURL, error, channelStatus, |
164 responseStatus, redirectCallback) | 167 responseStatus, redirectCallback) |
165 { | 168 { |
166 Prefs.notificationdata.lastError = Date.now(); | 169 Prefs.notificationdata.lastError = Date.now(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 return !((parameter in target && target[parameter] != name) || | 207 return !((parameter in target && target[parameter] != name) || |
205 (minVersionKey in target && | 208 (minVersionKey in target && |
206 Services.vc.compare(version, target[minVersionKey]) < 0) || | 209 Services.vc.compare(version, target[minVersionKey]) < 0) || |
207 (maxVersionKey in target && | 210 (maxVersionKey in target && |
208 Services.vc.compare(version, target[maxVersionKey]) > 0)); | 211 Services.vc.compare(version, target[maxVersionKey]) > 0)); |
209 } | 212 } |
210 | 213 |
211 let remoteData = []; | 214 let remoteData = []; |
212 if (typeof Prefs.notificationdata.data == "object" && | 215 if (typeof Prefs.notificationdata.data == "object" && |
213 Prefs.notificationdata.data.notifications instanceof Array) | 216 Prefs.notificationdata.data.notifications instanceof Array) |
| 217 { |
214 remoteData = Prefs.notificationdata.data.notifications; | 218 remoteData = Prefs.notificationdata.data.notifications; |
| 219 } |
215 | 220 |
216 let notifications = localData.concat(remoteData); | 221 let notifications = localData.concat(remoteData); |
217 if (notifications.length === 0) | 222 if (notifications.length === 0) |
218 return null; | 223 return null; |
219 | 224 |
220 const {addonName, addonVersion, application, | 225 const {addonName, addonVersion, application, |
221 applicationVersion, platform, platformVersion} = require("info"); | 226 applicationVersion, platform, platformVersion} = require("info"); |
222 let notificationToShow = null; | 227 let notificationToShow = null; |
223 for (let notification of notifications) | 228 for (let notification of notifications) |
224 { | 229 { |
(...skipping 10 matching lines...) Expand all Loading... |
235 { | 240 { |
236 if (shown + notification.interval > Date.now()) | 241 if (shown + notification.interval > Date.now()) |
237 continue; | 242 continue; |
238 } | 243 } |
239 else if (shown) | 244 else if (shown) |
240 continue; | 245 continue; |
241 } | 246 } |
242 | 247 |
243 if (notification.type !== "relentless" && | 248 if (notification.type !== "relentless" && |
244 Prefs.notifications_ignoredcategories.indexOf("*") != -1) | 249 Prefs.notifications_ignoredcategories.indexOf("*") != -1) |
| 250 { |
245 continue; | 251 continue; |
| 252 } |
246 } | 253 } |
247 | 254 |
248 if (typeof url === "string" || notification.urlFilters instanceof Array) | 255 if (typeof url === "string" || notification.urlFilters instanceof Array) |
249 { | 256 { |
250 if (Prefs.enabled && typeof url === "string" && | 257 if (Prefs.enabled && typeof url === "string" && |
251 notification.urlFilters instanceof Array) | 258 notification.urlFilters instanceof Array) |
252 { | 259 { |
253 let host; | 260 let host; |
254 try | 261 try |
255 { | 262 { |
256 host = new URL(url).hostname; | 263 host = new URL(url).hostname; |
257 } | 264 } |
258 catch (e) | 265 catch (e) |
259 { | 266 { |
260 host = ""; | 267 host = ""; |
261 } | 268 } |
262 | 269 |
263 let exception = defaultMatcher.matchesAny( | 270 let exception = defaultMatcher.matchesAny( |
264 url, RegExpFilter.typeMap.DOCUMENT, host, false, null | 271 url, RegExpFilter.typeMap.DOCUMENT, host, false, null |
265 ); | 272 ); |
266 if (exception instanceof WhitelistFilter) | 273 if (exception instanceof WhitelistFilter) |
267 continue; | 274 continue; |
268 | 275 |
269 let matcher = new Matcher(); | 276 let matcher = new Matcher(); |
270 for (let urlFilter of notification.urlFilters) | 277 for (let urlFilter of notification.urlFilters) |
271 matcher.add(Filter.fromText(urlFilter)); | 278 matcher.add(Filter.fromText(urlFilter)); |
272 if (!matcher.matchesAny(url, RegExpFilter.typeMap.DOCUMENT, host, | 279 if (!matcher.matchesAny(url, RegExpFilter.typeMap.DOCUMENT, host, |
273 false, null)) | 280 false, null)) |
| 281 { |
274 continue; | 282 continue; |
| 283 } |
275 } | 284 } |
276 else | 285 else |
277 continue; | 286 continue; |
278 } | 287 } |
279 | 288 |
280 if (notification.targets instanceof Array) | 289 if (notification.targets instanceof Array) |
281 { | 290 { |
282 let match = false; | 291 let match = false; |
283 for (let target of notification.targets) | 292 for (let target of notification.targets) |
284 { | 293 { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 removeNotification(notification) | 395 removeNotification(notification) |
387 { | 396 { |
388 let index = localData.indexOf(notification); | 397 let index = localData.indexOf(notification); |
389 if (index > -1) | 398 if (index > -1) |
390 localData.splice(index, 1); | 399 localData.splice(index, 1); |
391 }, | 400 }, |
392 | 401 |
393 /** | 402 /** |
394 * A callback function which listens to see if notifications were approved. | 403 * A callback function which listens to see if notifications were approved. |
395 * | 404 * |
396 * @callback approvedListener | 405 * @callback QuestionListener |
397 * @param {boolean} approved | 406 * @param {boolean} approved |
398 */ | 407 */ |
399 | 408 |
400 /** | 409 /** |
401 * Adds a listener for question-type notifications | 410 * Adds a listener for question-type notifications |
402 * @param {string} id | 411 * @param {string} id |
403 * @param {approvedListener} listener | 412 * @param {QuestionListener} listener |
404 */ | 413 */ |
405 addQuestionListener(id, listener) | 414 addQuestionListener(id, listener) |
406 { | 415 { |
407 if (!(id in questionListeners)) | 416 if (!(id in questionListeners)) |
408 questionListeners[id] = []; | 417 questionListeners[id] = []; |
409 if (questionListeners[id].indexOf(listener) === -1) | 418 if (questionListeners[id].indexOf(listener) === -1) |
410 questionListeners[id].push(listener); | 419 questionListeners[id].push(listener); |
411 }, | 420 }, |
412 | 421 |
413 /** | 422 /** |
414 * Removes a listener that was previously added via addQuestionListener | 423 * Removes a listener that was previously added via addQuestionListener |
415 * @param {string} id | 424 * @param {string} id |
416 * @param {approvedListener} listener | 425 * @param {QuestionListener} listener |
417 */ | 426 */ |
418 removeQuestionListener(id, listener) | 427 removeQuestionListener(id, listener) |
419 { | 428 { |
420 if (!(id in questionListeners)) | 429 if (!(id in questionListeners)) |
421 return; | 430 return; |
422 let index = questionListeners[id].indexOf(listener); | 431 let index = questionListeners[id].indexOf(listener); |
423 if (index > -1) | 432 if (index > -1) |
424 questionListeners[id].splice(index, 1); | 433 questionListeners[id].splice(index, 1); |
425 if (questionListeners[id].length === 0) | 434 if (questionListeners[id].length === 0) |
426 delete questionListeners[id]; | 435 delete questionListeners[id]; |
(...skipping 30 matching lines...) Expand all Loading... |
457 else if (index != -1 && forceValue !== true) | 466 else if (index != -1 && forceValue !== true) |
458 categories.splice(index, 1); | 467 categories.splice(index, 1); |
459 | 468 |
460 // HACK: JSON values aren't saved unless they are assigned a | 469 // HACK: JSON values aren't saved unless they are assigned a |
461 // different object. | 470 // different object. |
462 Prefs.notifications_ignoredcategories = | 471 Prefs.notifications_ignoredcategories = |
463 JSON.parse(JSON.stringify(categories)); | 472 JSON.parse(JSON.stringify(categories)); |
464 } | 473 } |
465 }; | 474 }; |
466 Notification.init(); | 475 Notification.init(); |
LEFT | RIGHT |