| 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-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 /** @module notificationHelper */ | 18 /** @module notificationHelper */ |
| 19 | 19 |
| 20 "use strict"; |
| 21 |
| 20 let {startIconAnimation, stopIconAnimation} = require("icon"); | 22 let {startIconAnimation, stopIconAnimation} = require("icon"); |
| 21 let {Utils} = require("utils"); | 23 let {Utils} = require("utils"); |
| 22 let {Notification: NotificationStorage} = require("notification"); | 24 let {Notification: NotificationStorage} = require("notification"); |
| 23 let {stringifyURL} = require("url"); | 25 let {stringifyURL} = require("url"); |
| 24 let {initAntiAdblockNotification} = require("antiadblockInit"); | 26 let {initAntiAdblockNotification} = require("antiadblockInit"); |
| 25 let {Prefs} = require("prefs"); | 27 let {Prefs} = require("prefs"); |
| 26 | 28 |
| 27 let activeNotification = null; | 29 let activeNotification = null; |
| 28 let activeButtons = null; | 30 let activeButtons = null; |
| 29 let defaultDisplayMethods = ["popup"]; | 31 let defaultDisplayMethods = ["popup"]; |
| 30 let displayMethods = Object.create(null); | 32 let displayMethods = Object.create(null); |
| 31 displayMethods.critical = ["icon", "notification", "popup"]; | 33 displayMethods.critical = ["icon", "notification", "popup"]; |
| 32 displayMethods.question = ["notification"]; | 34 displayMethods.question = ["notification"]; |
| 33 displayMethods.normal = ["notification"]; | 35 displayMethods.normal = ["notification"]; |
| 34 displayMethods.information = ["icon", "popup"]; | 36 displayMethods.information = ["icon", "popup"]; |
| 35 | 37 |
| 36 // Chrome on Linux does not fully support chrome.notifications until version 35 | 38 let platform = require("info").platform; |
| 37 // https://code.google.com/p/chromium/issues/detail?id=291485 | 39 let canUseChromeNotifications = platform == "chromium" && "notifications" in chr
ome; |
| 38 let canUseChromeNotifications = (function() | |
| 39 { | |
| 40 let info = require("info"); | |
| 41 if (info.platform == "chromium" && "notifications" in chrome) | |
| 42 { | |
| 43 if (navigator.platform.indexOf("Linux") == -1) | |
| 44 return true; | |
| 45 if (Services.vc.compare(info.applicationVersion, "35") >= 0) | |
| 46 return true; | |
| 47 } | |
| 48 return false; | |
| 49 })(); | |
| 50 | 40 |
| 51 function prepareNotificationIconAndPopup() | 41 function prepareNotificationIconAndPopup() |
| 52 { | 42 { |
| 53 let animateIcon = shouldDisplay("icon", activeNotification.type); | 43 let animateIcon = shouldDisplay("icon", activeNotification.type); |
| 54 activeNotification.onClicked = function() | 44 activeNotification.onClicked = () => |
| 55 { | 45 { |
| 56 if (animateIcon) | 46 if (animateIcon) |
| 57 stopIconAnimation(); | 47 stopIconAnimation(); |
| 58 notificationClosed(); | 48 notificationClosed(); |
| 59 }; | 49 }; |
| 60 if (animateIcon) | 50 if (animateIcon) |
| 61 startIconAnimation(activeNotification.type); | 51 startIconAnimation(activeNotification.type); |
| 62 } | 52 } |
| 63 | 53 |
| 64 function getNotificationButtons(notificationType, message) | 54 function getNotificationButtons(notificationType, message) |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 switch (activeButtons[buttonIndex].type) | 118 switch (activeButtons[buttonIndex].type) |
| 129 { | 119 { |
| 130 case "link": | 120 case "link": |
| 131 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); | 121 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); |
| 132 break; | 122 break; |
| 133 case "open-all": | 123 case "open-all": |
| 134 openNotificationLinks(); | 124 openNotificationLinks(); |
| 135 break; | 125 break; |
| 136 case "configure": | 126 case "configure": |
| 137 Prefs.notifications_showui = true; | 127 Prefs.notifications_showui = true; |
| 138 ext.showOptions(function(page) | 128 ext.showOptions(page => |
| 139 { | 129 { |
| 140 page.sendMessage({ | 130 page.sendMessage({ |
| 141 type: "app.respond", | 131 type: "app.respond", |
| 142 action: "focusSection", | 132 action: "focusSection", |
| 143 args: ["notifications"] | 133 args: ["notifications"] |
| 144 }); | 134 }); |
| 145 }); | 135 }); |
| 146 break; | 136 break; |
| 147 case "question": | 137 case "question": |
| 148 NotificationStorage.triggerQuestionListeners(activeNotification.id, button
Index == 0); | 138 NotificationStorage.triggerQuestionListeners(activeNotification.id, button
Index == 0); |
| 149 NotificationStorage.markAsShown(activeNotification.id); | 139 NotificationStorage.markAsShown(activeNotification.id); |
| 150 activeNotification.onClicked(); | 140 activeNotification.onClicked(); |
| 151 break; | 141 break; |
| 152 } | 142 } |
| 153 } | 143 } |
| 154 | 144 |
| 155 function notificationClosed() | 145 function notificationClosed() |
| 156 { | 146 { |
| 157 activeNotification = null; | 147 activeNotification = null; |
| 158 } | 148 } |
| 159 | 149 |
| 160 function initChromeNotifications() | 150 function initChromeNotifications() |
| 161 { | 151 { |
| 162 // Chrome hides notifications in notification center when clicked so we need t
o clear them | 152 // Chrome hides notifications in notification center when clicked so we need t
o clear them |
| 163 function clearActiveNotification(notificationId) | 153 function clearActiveNotification(notificationId) |
| 164 { | 154 { |
| 165 if (activeNotification && activeNotification.type != "question" && !("links"
in activeNotification)) | 155 if (activeNotification && activeNotification.type != "question" && !("links"
in activeNotification)) |
| 166 return; | 156 return; |
| 167 | 157 |
| 168 chrome.notifications.clear(notificationId, function(wasCleared) | 158 chrome.notifications.clear(notificationId, wasCleared => |
| 169 { | 159 { |
| 170 if (wasCleared) | 160 if (wasCleared) |
| 171 notificationClosed(); | 161 notificationClosed(); |
| 172 }); | 162 }); |
| 173 } | 163 } |
| 174 | 164 |
| 175 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt
onIndex) | 165 chrome.notifications.onButtonClicked.addListener((notificationId, buttonIndex)
=> |
| 176 { | 166 { |
| 177 notificationButtonClick(buttonIndex); | 167 notificationButtonClick(buttonIndex); |
| 178 clearActiveNotification(notificationId); | 168 clearActiveNotification(notificationId); |
| 179 }); | 169 }); |
| 180 chrome.notifications.onClicked.addListener(clearActiveNotification); | 170 chrome.notifications.onClicked.addListener(clearActiveNotification); |
| 181 chrome.notifications.onClosed.addListener(notificationClosed); | 171 chrome.notifications.onClosed.addListener(notificationClosed); |
| 182 } | 172 } |
| 183 | 173 |
| 184 function showNotification(notification) | 174 function showNotification(notification) |
| 185 { | 175 { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 else if (approved) | 227 else if (approved) |
| 238 openNotificationLinks(); | 228 openNotificationLinks(); |
| 239 } | 229 } |
| 240 } | 230 } |
| 241 prepareNotificationIconAndPopup(); | 231 prepareNotificationIconAndPopup(); |
| 242 }; | 232 }; |
| 243 | 233 |
| 244 /** | 234 /** |
| 245 * Initializes the notification system. | 235 * Initializes the notification system. |
| 246 */ | 236 */ |
| 247 exports.initNotifications = function() | 237 exports.initNotifications = () => |
| 248 { | 238 { |
| 249 if (canUseChromeNotifications) | 239 if (canUseChromeNotifications) |
| 250 initChromeNotifications(); | 240 initChromeNotifications(); |
| 251 initAntiAdblockNotification(); | 241 initAntiAdblockNotification(); |
| 252 }; | 242 }; |
| 253 | 243 |
| 254 /** | 244 /** |
| 255 * Gets the active notification to be shown if any. | 245 * Gets the active notification to be shown if any. |
| 256 * | 246 * |
| 257 * @return {?object} | 247 * @return {?object} |
| 258 */ | 248 */ |
| 259 exports.getActiveNotification = function() | 249 exports.getActiveNotification = () => activeNotification; |
| 260 { | |
| 261 return activeNotification; | |
| 262 }; | |
| 263 | 250 |
| 264 let shouldDisplay = | 251 let shouldDisplay = |
| 265 /** | 252 /** |
| 266 * Determines whether a given display method should be used for a | 253 * Determines whether a given display method should be used for a |
| 267 * specified notification type. | 254 * specified notification type. |
| 268 * | 255 * |
| 269 * @param {string} method Display method: icon, notification or popup | 256 * @param {string} method Display method: icon, notification or popup |
| 270 * @param {string} notificationType | 257 * @param {string} notificationType |
| 271 * @return {boolean} | 258 * @return {boolean} |
| 272 */ | 259 */ |
| 273 exports.shouldDisplay = function(method, notificationType) | 260 exports.shouldDisplay = (method, notificationType) => |
| 274 { | 261 { |
| 275 let methods = displayMethods[notificationType] || defaultDisplayMethods; | 262 let methods = displayMethods[notificationType] || defaultDisplayMethods; |
| 276 return methods.indexOf(method) > -1; | 263 return methods.indexOf(method) > -1; |
| 277 }; | 264 }; |
| 278 | 265 |
| 279 ext.pages.onLoading.addListener(page => | 266 ext.pages.onLoading.addListener(page => |
| 280 { | 267 { |
| 281 NotificationStorage.showNext(stringifyURL(page.url)); | 268 NotificationStorage.showNext(stringifyURL(page.url)); |
| 282 }); | 269 }); |
| 283 | 270 |
| 284 NotificationStorage.addShowListener(showNotification); | 271 NotificationStorage.addShowListener(showNotification); |
| OLD | NEW |