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

Delta Between Two Patch Sets: lib/notificationHelper.js

Issue 29327244: Issue 3024 - Added opt-out to Chrome notifications (Closed)
Left Patch Set: Created Sept. 10, 2015, 6:46 p.m.
Right Patch Set: Made `getNotificationButtons` independent of external variables Created Sept. 11, 2015, 2:37 p.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 | « _locales/en_US/messages.json ('k') | options.html » ('j') | 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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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 let {startIconAnimation, stopIconAnimation} = require("icon");
21 let {Utils} = require("utils"); 21 let {Utils} = require("utils");
22 let {Notification: NotificationStorage} = require("notification"); 22 let {Notification: NotificationStorage} = require("notification");
23 let {stringifyURL} = require("url"); 23 let {stringifyURL} = require("url");
24 let {initAntiAdblockNotification} = require("antiadblockInit"); 24 let {initAntiAdblockNotification} = require("antiadblockInit");
25 25
26 let activeNotification = null; 26 let activeNotification = null;
27 let activeButtons = null;
27 let defaultDisplayMethods = ["popup"]; 28 let defaultDisplayMethods = ["popup"];
28 let displayMethods = Object.create(null); 29 let displayMethods = Object.create(null);
29 displayMethods.critical = ["icon", "notification", "popup"]; 30 displayMethods.critical = ["icon", "notification", "popup"];
30 displayMethods.question = ["notification"]; 31 displayMethods.question = ["notification"];
31 displayMethods.normal = ["notification"]; 32 displayMethods.normal = ["notification"];
32 displayMethods.information = ["icon", "popup"]; 33 displayMethods.information = ["icon", "popup"];
33 34
34 // Chrome on Linux does not fully support chrome.notifications until version 35 35 // Chrome on Linux does not fully support chrome.notifications until version 35
35 // https://code.google.com/p/chromium/issues/detail?id=291485 36 // https://code.google.com/p/chromium/issues/detail?id=291485
36 let canUseChromeNotifications = (function() 37 let canUseChromeNotifications = (function()
(...skipping 15 matching lines...) Expand all
52 activeNotification.onClicked = function() 53 activeNotification.onClicked = function()
53 { 54 {
54 if (animateIcon) 55 if (animateIcon)
55 stopIconAnimation(); 56 stopIconAnimation();
56 notificationClosed(); 57 notificationClosed();
57 }; 58 };
58 if (animateIcon) 59 if (animateIcon)
59 startIconAnimation(activeNotification.type); 60 startIconAnimation(activeNotification.type);
60 } 61 }
61 62
63 function getNotificationButtons(notificationType, message)
64 {
65 let buttons = [];
66 if (notificationType == "question")
67 {
68 buttons.push({
69 type: "question",
70 title: ext.i18n.getMessage("overlay_notification_button_yes")
71 });
72 buttons.push({
73 type: "question",
74 title: ext.i18n.getMessage("overlay_notification_button_no")
75 });
76 }
77 else
78 {
79 let regex = /<a>(.*?)<\/a>/g;
80 let match;
81 while (match = regex.exec(message))
82 {
83 buttons.push({
84 type: "link",
85 title: match[1]
86 });
87 }
88
89 // Chrome only allows two notification buttons so we need to fall back
90 // to a single button to open all links if there are more than two.
91 let maxButtons = (notificationType == "critical") ? 2 : 1;
92 if (buttons.length > maxButtons)
93 {
94 buttons = [
95 {
96 type: "open-all",
97 title: ext.i18n.getMessage("notification_open_all")
98 }
99 ];
100 }
101 if (notificationType != "critical")
102 {
103 buttons.push({
104 type: "configure",
105 title: ext.i18n.getMessage("notification_configure")
106 });
107 }
108 }
109
110 return buttons;
111 }
112
62 function openNotificationLinks() 113 function openNotificationLinks()
63 { 114 {
64 if (activeNotification.links) 115 if (activeNotification.links)
65 { 116 {
66 for (let link of activeNotification.links) 117 for (let link of activeNotification.links)
Sebastian Noack 2015/09/11 14:51:00 This one is unrelated.
67 ext.pages.open(Utils.getDocLink(link)); 118 ext.pages.open(Utils.getDocLink(link));
68 } 119 }
69 } 120 }
70 121
71 function notificationButtonClick(buttonIndex) 122 function notificationButtonClick(buttonIndex)
72 { 123 {
73 if (activeNotification.type == "question") 124 switch (activeButtons[buttonIndex].type)
74 { 125 {
75 NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIn dex == 0); 126 case "link":
76 NotificationStorage.markAsShown(activeNotification.id); 127 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex]));
77 activeNotification.onClicked(); 128 break;
78 } 129 case "open-all":
79 else 130 openNotificationLinks();
80 { 131 break;
81 let links = activeNotification.links || []; 132 case "configure":
82 if (buttonIndex == 1 || links.length == 0)
83 {
84 Prefs.notifications_showui = true; 133 Prefs.notifications_showui = true;
85 ext.showOptions(function(page) 134 ext.showOptions(function(page)
86 { 135 {
87 page.sendMessage({ 136 page.sendMessage({
88 type: "focus-section", 137 type: "focus-section",
89 section: "notifications" 138 section: "notifications"
90 }); 139 });
91 }); 140 });
92 } 141 break;
93 else if (links.length == 1) 142 case "question":
94 ext.pages.open(Utils.getDocLink(activeNotification.links[0])); 143 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0);
95 else 144 NotificationStorage.markAsShown(activeNotification.id);
96 openNotificationLinks(); 145 activeNotification.onClicked();
146 break;
97 } 147 }
98 } 148 }
99 149
100 function notificationClosed() 150 function notificationClosed()
101 { 151 {
102 activeNotification = null; 152 activeNotification = null;
103 } 153 }
104 154
105 function imgToBase64(url, callback) 155 function imgToBase64(url, callback)
106 { 156 {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 if (shouldDisplay("notification", activeNotification.type)) 201 if (shouldDisplay("notification", activeNotification.type))
152 { 202 {
153 let texts = NotificationStorage.getLocalizedTexts(notification); 203 let texts = NotificationStorage.getLocalizedTexts(notification);
154 let title = texts.title || ""; 204 let title = texts.title || "";
155 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 205 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
156 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); 206 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
157 let linkCount = (activeNotification.links || []).length; 207 let linkCount = (activeNotification.links || []).length;
158 208
159 if (canUseChromeNotifications) 209 if (canUseChromeNotifications)
160 { 210 {
211 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age);
161 let opts = { 212 let opts = {
162 type: "basic", 213 type: "basic",
163 title: title, 214 title: title,
164 message: message, 215 message: message,
165 buttons: [], 216 buttons: activeButtons.map(button => ({title: button.title})),
166 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically 217 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
167 }; 218 };
168 if (activeNotification.type == "question")
169 {
170 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")});
171 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")});
172 }
173 else
174 {
175 if (linkCount == 1)
176 {
177 let match = /<a>(.*?)<\/a>/.exec(texts.message || "");
Sebastian Noack 2015/09/11 10:58:26 Nit: .exec() simply returns null if you pass null
Thomas Greiner 2015/09/11 12:51:48 Done.
178 if (match)
179 opts.buttons.push({title: match[1]});
180 }
181 else if (linkCount > 1)
182 opts.buttons.push({title: ext.i18n.getMessage("notification_open_all") });
183 opts.buttons.push({title: ext.i18n.getMessage("notification_configure")} );
184 }
185 219
186 imgToBase64(iconUrl, function(iconData) 220 imgToBase64(iconUrl, function(iconData)
187 { 221 {
188 opts["iconUrl"] = iconData; 222 opts.iconUrl = iconData;
189 chrome.notifications.create("", opts, function() {}); 223 chrome.notifications.create("", opts, function() {});
190 }); 224 });
191 } 225 }
192 else if ("Notification" in window && activeNotification.type != "question") 226 else if ("Notification" in window && activeNotification.type != "question")
193 { 227 {
194 if (linkCount == 0) 228 if (linkCount > 0)
195 message += " " + ext.i18n.getMessage("notification_without_buttons"); 229 message += " " + ext.i18n.getMessage("notification_without_buttons");
196 230
197 imgToBase64(iconUrl, function(iconData) 231 imgToBase64(iconUrl, function(iconData)
198 { 232 {
199 let notification = new Notification( 233 let notification = new Notification(
200 title, 234 title,
201 { 235 {
202 lang: Utils.appLocale, 236 lang: Utils.appLocale,
203 dir: ext.i18n.getMessage("@@bidi_dir"), 237 dir: ext.i18n.getMessage("@@bidi_dir"),
204 body: message, 238 body: message,
205 icon: iconData 239 icon: iconData
206 } 240 }
207 ); 241 );
208 242
209 notification.addEventListener("click", openNotificationLinks); 243 notification.addEventListener("click", openNotificationLinks);
210 notification.addEventListener("close", notificationClosed); 244 notification.addEventListener("close", notificationClosed);
211 }); 245 });
212 } 246 }
213 else 247 else
214 { 248 {
215 let message = title + "\n" + message; 249 let message = title + "\n" + message;
216 if (linkCount == 0) 250 if (linkCount > 0)
217 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 251 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
218 252
219 let approved = confirm(message); 253 let approved = confirm(message);
220 if (activeNotification.type == "question") 254 if (activeNotification.type == "question")
221 notificationButtonClick(approved ? 0 : 1); 255 notificationButtonClick(approved ? 0 : 1);
222 else if (approved) 256 else if (approved)
223 openNotificationLinks(); 257 openNotificationLinks();
224 } 258 }
225 } 259 }
226 prepareNotificationIconAndPopup(); 260 prepareNotificationIconAndPopup();
(...skipping 22 matching lines...) Expand all
249 /** 283 /**
250 * Gets the active notification to be shown if any. 284 * Gets the active notification to be shown if any.
251 * 285 *
252 * @return {?object} 286 * @return {?object}
253 */ 287 */
254 exports.getActiveNotification = function() 288 exports.getActiveNotification = function()
255 { 289 {
256 return activeNotification; 290 return activeNotification;
257 }; 291 };
258 292
293 let shouldDisplay =
Sebastian Noack 2015/09/11 14:51:00 This one is unrelated.
259 /** 294 /**
260 * Determines whether a given display method should be used for a 295 * Determines whether a given display method should be used for a
261 * specified notification type. 296 * specified notification type.
262 * 297 *
263 * @param {string} method Display method: icon, notification or popup 298 * @param {string} method Display method: icon, notification or popup
264 * @param {string} notificationType 299 * @param {string} notificationType
265 * @return {boolean} 300 * @return {boolean}
266 */ 301 */
267 exports.shouldDisplay = shouldDisplay; 302 exports.shouldDisplay = function(method, notificationType)
268 function shouldDisplay(method, notificationType)
269 { 303 {
270 let methods = displayMethods[notificationType] || defaultDisplayMethods; 304 let methods = displayMethods[notificationType] || defaultDisplayMethods;
271 return methods.indexOf(method) > -1; 305 return methods.indexOf(method) > -1;
272 } 306 }
273 307
274 NotificationStorage.addShowListener(showNotification); 308 NotificationStorage.addShowListener(showNotification);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld