Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/notificationHelper.js

Issue 29370565: [adblockpluschrome] Issue 4764 - Added "relentless" notification that shows up in intervals (Closed)
Left Patch Set: Implemented review comments Created Jan. 5, 2017, 3:16 p.m.
Right Patch Set: Added dependency update Created Jan. 20, 2017, 10:51 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « dependencies ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 let {startIconAnimation, stopIconAnimation} = require("icon"); 20 "use strict";
21 let {Utils} = require("utils"); 21
22 let {Notification: NotificationStorage} = require("notification"); 22 const {startIconAnimation, stopIconAnimation} = require("icon");
23 let {stringifyURL} = require("url"); 23 const {Utils} = require("utils");
24 let {initAntiAdblockNotification} = require("antiadblockInit"); 24 const {Notification: NotificationStorage} = require("notification");
25 const {stringifyURL} = require("url");
26 const {initAntiAdblockNotification} = require("antiadblockInit");
27 const {Prefs} = require("prefs");
25 28
26 let activeNotification = null; 29 let activeNotification = null;
27 let activeButtons = null; 30 let activeButtons = null;
28 let defaultDisplayMethods = ["popup"]; 31 let defaultDisplayMethods = ["popup"];
29 let displayMethods = Object.create(null); 32 let displayMethods = Object.create(null);
30 displayMethods.critical = ["icon", "notification", "popup"]; 33 displayMethods.critical = ["icon", "notification", "popup"];
31 displayMethods.question = ["notification"]; 34 displayMethods.question = ["notification"];
32 displayMethods.normal = ["notification"]; 35 displayMethods.normal = ["notification"];
33 displayMethods.relentless = ["notification"]; 36 displayMethods.relentless = ["notification"];
34 displayMethods.information = ["icon", "popup"]; 37 displayMethods.information = ["icon", "popup"];
35 38
36 // Chrome on Linux does not fully support chrome.notifications until version 35
37 // https://code.google.com/p/chromium/issues/detail?id=291485
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
51 function prepareNotificationIconAndPopup() 39 function prepareNotificationIconAndPopup()
52 { 40 {
53 let animateIcon = shouldDisplay("icon", activeNotification.type); 41 let animateIcon = shouldDisplay("icon", activeNotification.type);
54 activeNotification.onClicked = function() 42 activeNotification.onClicked = () =>
55 { 43 {
56 if (animateIcon) 44 if (animateIcon)
57 stopIconAnimation(); 45 stopIconAnimation();
58 notificationClosed(); 46 notificationClosed();
59 }; 47 };
60 if (animateIcon) 48 if (animateIcon)
61 startIconAnimation(activeNotification.type); 49 startIconAnimation(activeNotification.type);
62 } 50 }
63 51
64 function getNotificationButtons(notificationType, message) 52 function getNotificationButtons(notificationType, message)
(...skipping 27 matching lines...) Expand all
92 let maxButtons = (notificationType == "critical") ? 2 : 1; 80 let maxButtons = (notificationType == "critical") ? 2 : 1;
93 if (buttons.length > maxButtons) 81 if (buttons.length > maxButtons)
94 { 82 {
95 buttons = [ 83 buttons = [
96 { 84 {
97 type: "open-all", 85 type: "open-all",
98 title: ext.i18n.getMessage("notification_open_all") 86 title: ext.i18n.getMessage("notification_open_all")
99 } 87 }
100 ]; 88 ];
101 } 89 }
102 if (notificationType != "critical" && notificationType != "relentless") 90 if (["critical", "relentless"].indexOf(notificationType) == -1)
103 { 91 {
104 buttons.push({ 92 buttons.push({
105 type: "configure", 93 type: "configure",
106 title: ext.i18n.getMessage("notification_configure") 94 title: ext.i18n.getMessage("notification_configure")
107 }); 95 });
108 } 96 }
109 } 97 }
110 98
111 return buttons; 99 return buttons;
112 } 100 }
(...skipping 15 matching lines...) Expand all
128 switch (activeButtons[buttonIndex].type) 116 switch (activeButtons[buttonIndex].type)
129 { 117 {
130 case "link": 118 case "link":
131 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); 119 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex]));
132 break; 120 break;
133 case "open-all": 121 case "open-all":
134 openNotificationLinks(); 122 openNotificationLinks();
135 break; 123 break;
136 case "configure": 124 case "configure":
137 Prefs.notifications_showui = true; 125 Prefs.notifications_showui = true;
138 ext.showOptions(function(page) 126 ext.showOptions(page =>
139 { 127 {
140 page.sendMessage({ 128 page.sendMessage({
141 type: "app.respond", 129 type: "app.respond",
142 action: "focusSection", 130 action: "focusSection",
143 args: ["notifications"] 131 args: ["notifications"]
144 }); 132 });
145 }); 133 });
146 break; 134 break;
147 case "question": 135 case "question":
148 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0); 136 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0);
149 NotificationStorage.markAsShown(activeNotification.id); 137 NotificationStorage.markAsShown(activeNotification.id);
150 activeNotification.onClicked(); 138 activeNotification.onClicked();
151 break; 139 break;
152 } 140 }
153 } 141 }
154 142
155 function notificationClosed() 143 function notificationClosed()
156 { 144 {
157 activeNotification = null; 145 activeNotification = null;
158 } 146 }
159 147
160 function initChromeNotifications() 148 function initChromeNotifications()
161 { 149 {
162 // Chrome hides notifications in notification center when clicked so we need t o clear them 150 // Chrome hides notifications in notification center when clicked so we need t o clear them
163 function clearActiveNotification(notificationId) 151 function clearActiveNotification(notificationId)
164 { 152 {
165 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification)) 153 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification))
166 return; 154 return;
167 155
168 chrome.notifications.clear(notificationId, function(wasCleared) 156 chrome.notifications.clear(notificationId, wasCleared =>
169 { 157 {
170 if (wasCleared) 158 if (wasCleared)
171 notificationClosed(); 159 notificationClosed();
172 }); 160 });
173 } 161 }
174 162
175 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt onIndex) 163 chrome.notifications.onButtonClicked.addListener((notificationId, buttonIndex) =>
176 { 164 {
177 notificationButtonClick(buttonIndex); 165 notificationButtonClick(buttonIndex);
178 clearActiveNotification(notificationId); 166 clearActiveNotification(notificationId);
179 }); 167 });
180 chrome.notifications.onClicked.addListener(clearActiveNotification); 168 chrome.notifications.onClicked.addListener(clearActiveNotification);
181 chrome.notifications.onClosed.addListener(notificationClosed); 169 chrome.notifications.onClosed.addListener(notificationClosed);
182 } 170 }
183 171
184 function showNotification(notification) 172 function showNotification(notification)
185 { 173 {
186 if (activeNotification && activeNotification.id == notification.id) 174 if (activeNotification && activeNotification.id == notification.id)
187 return; 175 return;
188 176
189 activeNotification = notification; 177 activeNotification = notification;
190 if (shouldDisplay("notification", activeNotification.type)) 178 if (shouldDisplay("notification", activeNotification.type))
191 { 179 {
192 let texts = NotificationStorage.getLocalizedTexts(notification); 180 let texts = NotificationStorage.getLocalizedTexts(notification);
193 let title = texts.title || ""; 181 let title = texts.title || "";
194 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 182 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
195 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); 183 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
196 let linkCount = (activeNotification.links || []).length; 184 let linkCount = (activeNotification.links || []).length;
197 185
198 if (canUseChromeNotifications) 186 if ("notifications" in chrome)
199 { 187 {
200 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age); 188 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age);
201 chrome.notifications.create("", { 189 chrome.notifications.create("", {
202 type: "basic", 190 type: "basic",
203 title: title, 191 title: title,
204 iconUrl: iconUrl, 192 iconUrl: iconUrl,
205 message: message, 193 message: message,
206 buttons: activeButtons.map(button => ({title: button.title})), 194 buttons: activeButtons.map(button => ({title: button.title})),
207 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically 195 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
208 }); 196 });
(...skipping 24 matching lines...) Expand all
233 221
234 let approved = confirm(message); 222 let approved = confirm(message);
235 if (activeNotification.type == "question") 223 if (activeNotification.type == "question")
236 notificationButtonClick(approved ? 0 : 1); 224 notificationButtonClick(approved ? 0 : 1);
237 else if (approved) 225 else if (approved)
238 openNotificationLinks(); 226 openNotificationLinks();
239 } 227 }
240 } 228 }
241 prepareNotificationIconAndPopup(); 229 prepareNotificationIconAndPopup();
242 230
243 if (notification.type != "question") 231 if (notification.type !== "question")
244 NotificationStorage.markAsShown(notification.id); 232 NotificationStorage.markAsShown(notification.id);
245 }; 233 };
246 234
247 /** 235 /**
248 * Initializes the notification system. 236 * Initializes the notification system.
249 */ 237 */
250 exports.initNotifications = function() 238 exports.initNotifications = () =>
251 { 239 {
252 if (canUseChromeNotifications) 240 if ("notifications" in chrome)
253 initChromeNotifications(); 241 initChromeNotifications();
254 initAntiAdblockNotification(); 242 initAntiAdblockNotification();
255 }; 243 };
256 244
257 /** 245 /**
258 * Gets the active notification to be shown if any. 246 * Gets the active notification to be shown if any.
259 * 247 *
260 * @return {?object} 248 * @return {?object}
261 */ 249 */
262 exports.getActiveNotification = function() 250 exports.getActiveNotification = () => activeNotification;
263 {
264 return activeNotification;
265 };
266 251
267 let shouldDisplay = 252 let shouldDisplay =
268 /** 253 /**
269 * Determines whether a given display method should be used for a 254 * Determines whether a given display method should be used for a
270 * specified notification type. 255 * specified notification type.
271 * 256 *
272 * @param {string} method Display method: icon, notification or popup 257 * @param {string} method Display method: icon, notification or popup
273 * @param {string} notificationType 258 * @param {string} notificationType
274 * @return {boolean} 259 * @return {boolean}
275 */ 260 */
276 exports.shouldDisplay = function(method, notificationType) 261 exports.shouldDisplay = (method, notificationType) =>
277 { 262 {
278 let methods = displayMethods[notificationType] || defaultDisplayMethods; 263 let methods = displayMethods[notificationType] || defaultDisplayMethods;
279 return methods.indexOf(method) > -1; 264 return methods.indexOf(method) > -1;
280 }; 265 };
281 266
282 ext.pages.onLoading.addListener(page => 267 ext.pages.onLoading.addListener(page =>
283 { 268 {
284 NotificationStorage.showNext(stringifyURL(page.url)); 269 NotificationStorage.showNext(stringifyURL(page.url));
285 }); 270 });
286 271
287 NotificationStorage.addShowListener(showNotification); 272 NotificationStorage.addShowListener(showNotification);
LEFTRIGHT
« dependencies ('k') | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld