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

Side by Side Diff: lib/notificationHelper.js

Issue 29327244: Issue 3024 - Added opt-out to Chrome notifications (Closed)
Patch Set: Centralized button management Created Sept. 11, 2015, 12:36 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 | « _locales/en_US/messages.json ('k') | options.html » ('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-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 19 matching lines...) Expand all
56 notificationClosed(); 57 notificationClosed();
57 }; 58 };
58 if (animateIcon) 59 if (animateIcon)
59 startIconAnimation(activeNotification.type); 60 startIconAnimation(activeNotification.type);
60 } 61 }
61 62
62 function openNotificationLinks() 63 function openNotificationLinks()
63 { 64 {
64 if (activeNotification.links) 65 if (activeNotification.links)
65 { 66 {
66 activeNotification.links.forEach(function(link) 67 for (let link of activeNotification.links)
67 { 68 ext.pages.open(Utils.getDocLink(link));
68 ext.windows.getLastFocused(function(win)
69 {
70 win.openTab(Utils.getDocLink(link));
71 });
72 });
73 } 69 }
74 } 70 }
75 71
76 function notificationButtonClick(buttonIndex) 72 function notificationButtonClick(buttonIndex)
77 { 73 {
78 if (activeNotification.type == "question") 74 switch (activeButtons[buttonIndex].type)
79 { 75 {
80 NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIn dex == 0); 76 case "link":
81 NotificationStorage.markAsShown(activeNotification.id); 77 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex]));
82 activeNotification.onClicked(); 78 break;
83 } 79 case "open-all":
84 else if (activeNotification.links && activeNotification.links[buttonIndex]) 80 openNotificationLinks();
85 { 81 break;
86 ext.windows.getLastFocused(function(win) 82 case "configure":
87 { 83 Prefs.notifications_showui = true;
88 win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex])); 84 ext.showOptions(function(page)
89 }); 85 {
86 page.sendMessage({
87 type: "focus-section",
88 section: "notifications"
89 });
90 });
91 break;
92 case "question":
93 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0);
94 NotificationStorage.markAsShown(activeNotification.id);
95 activeNotification.onClicked();
96 break;
90 } 97 }
91 } 98 }
92 99
93 function notificationClosed() 100 function notificationClosed()
94 { 101 {
95 activeNotification = null; 102 activeNotification = null;
96 } 103 }
97 104
98 function imgToBase64(url, callback) 105 function imgToBase64(url, callback)
99 { 106 {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 if (activeNotification && activeNotification.id == notification.id) 147 if (activeNotification && activeNotification.id == notification.id)
141 return; 148 return;
142 149
143 activeNotification = notification; 150 activeNotification = notification;
144 if (shouldDisplay("notification", activeNotification.type)) 151 if (shouldDisplay("notification", activeNotification.type))
145 { 152 {
146 let texts = NotificationStorage.getLocalizedTexts(notification); 153 let texts = NotificationStorage.getLocalizedTexts(notification);
147 let title = texts.title || ""; 154 let title = texts.title || "";
148 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 155 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
149 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); 156 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
150 let hasLinks = activeNotification.links && activeNotification.links.length > 0; 157 let hasLinks = activeNotification.links && activeNotification.links.length > 0;
Sebastian Noack 2015/09/11 13:09:55 I actually preferred the way you did it before usi
Thomas Greiner 2015/09/11 14:03:44 Done. Note that I merely reverted the changes from
151 158
152 if (canUseChromeNotifications) 159 if (canUseChromeNotifications)
153 { 160 {
161 activeButtons = [];
Sebastian Noack 2015/09/11 13:09:55 This function is becoming quite long. How about mo
Thomas Greiner 2015/09/11 14:03:44 Done. I agree, should make it easier to comprehend
154 let opts = { 162 let opts = {
155 type: "basic", 163 type: "basic",
156 title: title, 164 title: title,
157 message: message, 165 message: message,
158 buttons: [], 166 buttons: [],
159 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically 167 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
160 }; 168 };
161 if (activeNotification.type == "question") 169 if (activeNotification.type == "question")
162 { 170 {
163 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")}); 171 activeButtons.push({
164 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")}); 172 type: "question",
173 title: ext.i18n.getMessage("overlay_notification_button_yes")
174 });
175 activeButtons.push({
176 type: "question",
177 title: ext.i18n.getMessage("overlay_notification_button_no")
178 });
165 } 179 }
166 else 180 else
167 { 181 {
168 let regex = /<a>(.*?)<\/a>/g; 182 let regex = /<a>(.*?)<\/a>/g;
169 let plainMessage = texts.message || "";
170 let match; 183 let match;
171 while (match = regex.exec(plainMessage)) 184 while (match = regex.exec(texts.message))
172 opts.buttons.push({title: match[1]}); 185 {
186 activeButtons.push({
187 type: "link",
188 title: match[1]
189 });
190 }
191
192 // Chrome only allows two notification buttons so we need to fall back
193 // to a single button to open all links if there are more than two.
194 let maxButtons = (activeNotification.type == "critical") ? 2 : 1;
195 if (activeButtons.length > maxButtons)
196 {
197 activeButtons = [
198 {
199 type: "open-all",
200 title: ext.i18n.getMessage("notification_open_all")
201 }
202 ];
203 }
204 if (activeNotification.type != "critical")
205 {
206 activeButtons.push({
207 type: "configure",
208 title: ext.i18n.getMessage("notification_configure")
209 });
210 }
173 } 211 }
174 212
175 imgToBase64(iconUrl, function(iconData) 213 imgToBase64(iconUrl, function(iconData)
176 { 214 {
177 opts["iconUrl"] = iconData; 215 opts.buttons = activeButtons.map(function(button)
Sebastian Noack 2015/09/11 13:09:55 Why not a for-of loop?
Thomas Greiner 2015/09/11 14:03:44 No particular reason. The way I use it now in the
Sebastian Noack 2015/09/11 14:15:39 I see. However, much better with an arrow function
216 {
217 return {title: button.title};
218 });
219 opts.iconUrl = iconData;
178 chrome.notifications.create("", opts, function() {}); 220 chrome.notifications.create("", opts, function() {});
179 }); 221 });
180 } 222 }
181 else if ("Notification" in window && activeNotification.type != "question") 223 else if ("Notification" in window && activeNotification.type != "question")
182 { 224 {
183 if (hasLinks) 225 if (hasLinks)
184 message += " " + ext.i18n.getMessage("notification_without_buttons"); 226 message += " " + ext.i18n.getMessage("notification_without_buttons");
185 227
186 imgToBase64(iconUrl, function(iconData) 228 imgToBase64(iconUrl, function(iconData)
187 { 229 {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 * @return {boolean} 296 * @return {boolean}
255 */ 297 */
256 exports.shouldDisplay = shouldDisplay; 298 exports.shouldDisplay = shouldDisplay;
257 function shouldDisplay(method, notificationType) 299 function shouldDisplay(method, notificationType)
258 { 300 {
259 let methods = displayMethods[notificationType] || defaultDisplayMethods; 301 let methods = displayMethods[notificationType] || defaultDisplayMethods;
260 return methods.indexOf(method) > -1; 302 return methods.indexOf(method) > -1;
261 } 303 }
262 304
263 NotificationStorage.addShowListener(showNotification); 305 NotificationStorage.addShowListener(showNotification);
OLDNEW
« no previous file with comments | « _locales/en_US/messages.json ('k') | options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld