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

Side by Side Diff: lib/notificationHelper.js

Issue 29513555: Issue 5512 - Manage multiple notifications displayed at the same time Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Aug. 12, 2017, 4:49 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | notification.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 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"; 20 "use strict";
21 21
22 const {startIconAnimation, stopIconAnimation} = require("icon"); 22 const {startIconAnimation, stopIconAnimation} = require("icon");
23 const {Utils} = require("utils"); 23 const {Utils} = require("utils");
24 const {Notification: NotificationStorage} = require("notification"); 24 const {Notification: NotificationStorage} = require("notification");
25 const {stringifyURL} = require("url"); 25 const {stringifyURL} = require("url");
26 const {initAntiAdblockNotification} = require("antiadblockInit"); 26 const {initAntiAdblockNotification} = require("antiadblockInit");
27 const {Prefs} = require("prefs"); 27 const {Prefs} = require("prefs");
28 28
29 // "Open" notifications are any notifications currently being displayed in the
Manish Jethani 2017/08/12 16:57:48 Comments explaining the conceptual difference betw
kzar 2017/08/21 13:59:25 Thanks for adding those, I think it helps quite a
30 // platform's notification center. There can be multiple notifications open at
31 // the same time.
32 let openNotificationsInfo = new Map();
33 let openNotificationIds = new Set();
34
35 // The "active" notification is the one that is displayed in the extension's
36 // popup. It may be the same as one of the open notifications, or it may be a
37 // different one.
29 let activeNotification = null; 38 let activeNotification = null;
30 let activeButtons = null; 39
31 let defaultDisplayMethods = ["popup"]; 40 let defaultDisplayMethods = ["popup"];
32 let displayMethods = Object.create(null); 41 let displayMethods = Object.create(null);
42
33 displayMethods.critical = ["icon", "notification", "popup"]; 43 displayMethods.critical = ["icon", "notification", "popup"];
34 displayMethods.question = ["notification"]; 44 displayMethods.question = ["notification"];
35 displayMethods.normal = ["notification"]; 45 displayMethods.normal = ["notification"];
36 displayMethods.relentless = ["notification"]; 46 displayMethods.relentless = ["notification"];
37 displayMethods.information = ["icon", "popup"]; 47 displayMethods.information = ["icon", "popup"];
38 48
39 function prepareNotificationIconAndPopup() 49 function prepareNotificationIconAndPopup(notification)
kzar 2017/08/21 13:59:24 Since we're no longer always making the given noti
Manish Jethani 2017/08/24 09:54:12 It seems appropriate to me, since the function wil
40 { 50 {
41 let animateIcon = shouldDisplay("icon", activeNotification.type); 51 let animateIcon = shouldDisplay("icon", notification.type);
42 activeNotification.onClicked = () => 52 let showInPopup = shouldDisplay("popup", notification.type);
53
54 if (animateIcon || showInPopup)
43 { 55 {
56 // If another notification is active, close it first. This will stop any
57 // existing icon animation.
58 if (activeNotification)
59 activeNotification.onClosed();
60
61 activeNotification = notification;
62 }
63
64 notification.onClosed = () =>
65 {
66 if (notification != activeNotification)
67 return;
68
44 if (animateIcon) 69 if (animateIcon)
45 stopIconAnimation(); 70 stopIconAnimation();
46 notificationClosed(); 71
72 activeNotification = null;
47 }; 73 };
74
48 if (animateIcon) 75 if (animateIcon)
49 startIconAnimation(activeNotification.type); 76 startIconAnimation(notification.type);
50 } 77 }
51 78
52 function getNotificationButtons(notificationType, message) 79 function getNotificationButtons(notificationType, message)
53 { 80 {
54 let buttons = []; 81 let buttons = [];
55 if (notificationType == "question") 82 if (notificationType == "question")
56 { 83 {
57 buttons.push({ 84 buttons.push({
58 type: "question", 85 type: "question",
59 title: ext.i18n.getMessage("overlay_notification_button_yes") 86 title: ext.i18n.getMessage("overlay_notification_button_yes")
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 buttons.push({ 119 buttons.push({
93 type: "configure", 120 type: "configure",
94 title: ext.i18n.getMessage("notification_configure") 121 title: ext.i18n.getMessage("notification_configure")
95 }); 122 });
96 } 123 }
97 } 124 }
98 125
99 return buttons; 126 return buttons;
100 } 127 }
101 128
102 function openNotificationLinks() 129 function openNotificationLinks(notification)
103 { 130 {
104 if (activeNotification.links) 131 if (notification.links)
105 { 132 {
106 for (let link of activeNotification.links) 133 for (let link of notification.links)
107 ext.pages.open(Utils.getDocLink(link)); 134 ext.pages.open(Utils.getDocLink(link));
108 } 135 }
109 } 136 }
110 137
111 function notificationButtonClick(buttonIndex) 138 function notificationButtonClick(notificationInfo, buttonIndex)
112 { 139 {
113 if (!(activeButtons && buttonIndex in activeButtons)) 140 let {notification, buttons} = notificationInfo || {};
141
142 if (!(buttons && buttonIndex in buttons))
114 return; 143 return;
115 144
116 switch (activeButtons[buttonIndex].type) 145 switch (buttons[buttonIndex].type)
117 { 146 {
118 case "link": 147 case "link":
119 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); 148 ext.pages.open(Utils.getDocLink(notification.links[buttonIndex]));
120 break; 149 break;
121 case "open-all": 150 case "open-all":
122 openNotificationLinks(); 151 openNotificationLinks(notification);
123 break; 152 break;
124 case "configure": 153 case "configure":
125 Prefs.notifications_showui = true; 154 Prefs.notifications_showui = true;
126 ext.showOptions(page => 155 ext.showOptions(page =>
127 { 156 {
128 page.sendMessage({ 157 page.sendMessage({
129 type: "app.respond", 158 type: "app.respond",
130 action: "focusSection", 159 action: "focusSection",
131 args: ["notifications"] 160 args: ["notifications"]
132 }); 161 });
133 }); 162 });
134 break; 163 break;
135 case "question": 164 case "question":
136 NotificationStorage.triggerQuestionListeners(activeNotification.id, 165 NotificationStorage.triggerQuestionListeners(notification.id,
137 buttonIndex == 0); 166 buttonIndex == 0);
138 NotificationStorage.markAsShown(activeNotification.id); 167 NotificationStorage.markAsShown(notification.id);
139 activeNotification.onClicked(); 168 break;
Manish Jethani 2017/08/12 16:57:48 Calling onClicked (now onClosed) is no longer need
140 break; 169 }
141 } 170 }
142 } 171
143 172 function notificationOpened(key, notificationInfo)
144 function notificationClosed() 173 {
145 { 174 let {notification} = notificationInfo;
146 activeNotification = null; 175
176 openNotificationsInfo.set(key, notificationInfo);
177 openNotificationIds.add(notification.id);
178 }
179
180 function notificationClosed(key)
181 {
182 let {notification} = openNotificationsInfo.get(key) || {};
183 if (notification)
184 {
185 if (notification.onClosed)
186 notification.onClosed();
187
188 openNotificationIds.delete(notification.id);
189 }
190
191 openNotificationsInfo.delete(key);
147 } 192 }
148 193
149 function initChromeNotifications() 194 function initChromeNotifications()
150 { 195 {
151 // Chrome hides notifications in notification center when clicked so 196 // Chrome hides notifications in notification center when clicked so
152 // we need to clear them. 197 // we need to clear them.
153 function clearActiveNotification(notificationId) 198 function clearNotification(key)
154 { 199 {
155 if (activeNotification && 200 let {notification} = openNotificationsInfo.get(key) || {};
156 activeNotification.type != "question" && 201 if (notification &&
157 !("links" in activeNotification)) 202 notification.type != "question" &&
203 !("links" in notification))
158 return; 204 return;
159 205
160 chrome.notifications.clear(notificationId, wasCleared => 206 chrome.notifications.clear(key, wasCleared =>
Manish Jethani 2017/08/12 16:57:48 Renamed "notificationId" to "key" here, because th
161 { 207 {
162 if (wasCleared) 208 if (wasCleared)
163 notificationClosed(); 209 notificationClosed(key);
164 }); 210 });
165 } 211 }
166 212
167 chrome.notifications.onButtonClicked.addListener( 213 chrome.notifications.onButtonClicked.addListener(
168 (notificationId, buttonIndex) => 214 (key, buttonIndex) =>
169 { 215 {
170 notificationButtonClick(buttonIndex); 216 notificationButtonClick(openNotificationsInfo.get(key), buttonIndex);
171 clearActiveNotification(notificationId); 217 clearNotification(key);
172 } 218 }
173 ); 219 );
174 chrome.notifications.onClicked.addListener(clearActiveNotification); 220
221 chrome.notifications.onClicked.addListener(clearNotification);
222
223 // Known issue on macOS: When the user closes a notification by clicking on
Manish Jethani 2017/08/12 16:57:48 Maybe it's just me, but macOS doesn't seem to trig
224 // the close button in the notification widget's title bar, it does not
225 // trigger the onClosed handler.
175 chrome.notifications.onClosed.addListener(notificationClosed); 226 chrome.notifications.onClosed.addListener(notificationClosed);
176 } 227 }
177 228
178 function showNotification(notification) 229 function showNotification(notification)
179 { 230 {
180 if (activeNotification && activeNotification.id == notification.id) 231 if ((activeNotification && activeNotification.id == notification.id) ||
232 openNotificationIds.has(notification.id))
181 return; 233 return;
182 234
183 activeNotification = notification; 235 if (shouldDisplay("notification", notification.type))
184 if (shouldDisplay("notification", activeNotification.type)) 236 {
185 { 237 let notificationInfo = {notification};
238
186 let texts = NotificationStorage.getLocalizedTexts(notification); 239 let texts = NotificationStorage.getLocalizedTexts(notification);
187 let title = texts.title || ""; 240 let title = texts.title || "";
188 let message = (texts.message || "").replace(/<\/?(a|strong)>/g, ""); 241 let message = (texts.message || "").replace(/<\/?(a|strong)>/g, "");
189 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); 242 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
190 let linkCount = (activeNotification.links || []).length; 243 let linkCount = (notification.links || []).length;
244
245 notificationInfo.buttons = getNotificationButtons(notification.type,
246 texts.message);
191 247
192 if ("notifications" in chrome) 248 if ("notifications" in chrome)
193 { 249 {
194 activeButtons = getNotificationButtons(activeNotification.type,
195 texts.message);
196 chrome.notifications.create("", { 250 chrome.notifications.create("", {
197 type: "basic", 251 type: "basic",
198 title, 252 title,
199 iconUrl, 253 iconUrl,
200 message, 254 message,
201 buttons: activeButtons.map(button => ({title: button.title})), 255 buttons: notificationInfo.buttons.map(button =>
256 ({title: button.title})),
202 // We use the highest priority to prevent the notification 257 // We use the highest priority to prevent the notification
203 // from closing automatically. 258 // from closing automatically.
204 priority: 2 259 priority: 2,
205 }); 260 // Critical notifications should not go away in a few seconds, as
Manish Jethani 2017/08/12 16:57:48 I took the liberty to add this here. If it's a cri
kzar 2017/08/21 13:59:24 Sounds sensible to me but you've not mentioned thi
Thomas Greiner 2017/08/22 18:16:59 I can confirm that critical notifications are not
Manish Jethani 2017/08/24 09:54:12 It's true that the notification doesn't close auto
Manish Jethani 2017/08/24 09:54:12 I've updated the issue now: https://issues.adbloc
kzar 2017/08/29 12:16:30 Thanks
206 } 261 // happens on some platforms (e.g. macOS).
207 else if ("Notification" in window && activeNotification.type != "question") 262 requireInteraction: notification.type == "critical"
263 },
264 key =>
265 {
266 notificationOpened(key, notificationInfo);
267 });
268 }
269 else if ("Notification" in window && notification.type != "question")
208 { 270 {
209 if (linkCount > 0) 271 if (linkCount > 0)
210 message += " " + ext.i18n.getMessage("notification_without_buttons"); 272 message += " " + ext.i18n.getMessage("notification_without_buttons");
211 273
274 // Since the Notification API doesn't given us a "notification ID", and
275 // we don't want to use the Notification object itself as the key
276 // (because memory leaks), we generate a random key here.
kzar 2017/08/21 13:59:24 Couldn't we rather use a WeakMap?
277 let key = String(Date.now() + Math.random());
278
212 let widget = new Notification( 279 let widget = new Notification(
213 title, 280 title,
214 { 281 {
215 lang: Utils.appLocale, 282 lang: Utils.appLocale,
216 dir: ext.i18n.getMessage("@@bidi_dir"), 283 dir: ext.i18n.getMessage("@@bidi_dir"),
217 body: message, 284 body: message,
218 icon: iconUrl 285 icon: iconUrl
219 } 286 }
220 ); 287 );
221 288
222 widget.addEventListener("click", openNotificationLinks); 289 notificationOpened(key, notificationInfo);
223 widget.addEventListener("close", notificationClosed); 290
291 widget.addEventListener("click", () =>
292 {
293 openNotificationLinks(notification);
294 });
295
296 widget.addEventListener("close", () => notificationClosed(key));
297 widget.addEventListener("error", () => notificationClosed(key));
224 } 298 }
225 else 299 else
226 { 300 {
227 message = title + "\n" + message; 301 message = title + "\n" + message;
228 if (linkCount > 0) 302 if (linkCount > 0)
229 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 303 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
230 304
231 let approved = confirm(message); 305 let approved = confirm(message);
232 if (activeNotification.type == "question") 306 if (notification.type == "question")
233 notificationButtonClick(approved ? 0 : 1); 307 notificationButtonClick(notificationInfo, approved ? 0 : 1);
234 else if (approved) 308 else if (approved)
235 openNotificationLinks(); 309 openNotificationLinks(notification);
236 } 310 }
237 } 311 }
238 prepareNotificationIconAndPopup(); 312
239 313 prepareNotificationIconAndPopup(notification);
240 if (notification.type !== "question") 314
315 if (notification.type != "question")
241 NotificationStorage.markAsShown(notification.id); 316 NotificationStorage.markAsShown(notification.id);
242 } 317 }
243 318
244 /** 319 /**
245 * Initializes the notification system. 320 * Initializes the notification system.
246 */ 321 */
247 exports.initNotifications = () => 322 exports.initNotifications = () =>
248 { 323 {
249 if ("notifications" in chrome) 324 if ("notifications" in chrome)
250 initChromeNotifications(); 325 initChromeNotifications();
326
251 initAntiAdblockNotification(); 327 initAntiAdblockNotification();
252 }; 328 };
253 329
254 /** 330 /**
255 * Gets the active notification to be shown if any. 331 * Gets the active notification to be shown if any.
256 * 332 *
257 * @return {?object} 333 * @return {?object}
258 */ 334 */
259 exports.getActiveNotification = () => activeNotification; 335 exports.getActiveNotification = () => activeNotification;
260 336
(...skipping 11 matching lines...) Expand all
272 let methods = displayMethods[notificationType] || defaultDisplayMethods; 348 let methods = displayMethods[notificationType] || defaultDisplayMethods;
273 return methods.indexOf(method) > -1; 349 return methods.indexOf(method) > -1;
274 }; 350 };
275 351
276 ext.pages.onLoading.addListener(page => 352 ext.pages.onLoading.addListener(page =>
277 { 353 {
278 NotificationStorage.showNext(stringifyURL(page.url)); 354 NotificationStorage.showNext(stringifyURL(page.url));
279 }); 355 });
280 356
281 NotificationStorage.addShowListener(showNotification); 357 NotificationStorage.addShowListener(showNotification);
OLDNEW
« no previous file with comments | « no previous file | notification.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld