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: 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:
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 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 activeNotification.links.forEach(function(link) 117 for (let link of activeNotification.links)
Sebastian Noack 2015/09/11 14:51:00 This one is unrelated.
67 { 118 ext.pages.open(Utils.getDocLink(link));
68 ext.windows.getLastFocused(function(win)
69 {
70 win.openTab(Utils.getDocLink(link));
71 });
72 });
73 } 119 }
74 } 120 }
75 121
76 function notificationButtonClick(buttonIndex) 122 function notificationButtonClick(buttonIndex)
77 { 123 {
78 if (activeNotification.type == "question") 124 switch (activeButtons[buttonIndex].type)
79 { 125 {
80 NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIn dex == 0); 126 case "link":
81 NotificationStorage.markAsShown(activeNotification.id); 127 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex]));
82 activeNotification.onClicked(); 128 break;
83 } 129 case "open-all":
84 else if (activeNotification.links && activeNotification.links[buttonIndex]) 130 openNotificationLinks();
85 { 131 break;
86 ext.windows.getLastFocused(function(win) 132 case "configure":
87 { 133 Prefs.notifications_showui = true;
88 win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex])); 134 ext.showOptions(function(page)
89 }); 135 {
136 page.sendMessage({
137 type: "focus-section",
138 section: "notifications"
139 });
140 });
141 break;
142 case "question":
143 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0);
144 NotificationStorage.markAsShown(activeNotification.id);
145 activeNotification.onClicked();
146 break;
90 } 147 }
91 } 148 }
92 149
93 function notificationClosed() 150 function notificationClosed()
94 { 151 {
95 activeNotification = null; 152 activeNotification = null;
96 } 153 }
97 154
98 function imgToBase64(url, callback) 155 function imgToBase64(url, callback)
99 { 156 {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 if (activeNotification && activeNotification.id == notification.id) 197 if (activeNotification && activeNotification.id == notification.id)
141 return; 198 return;
142 199
143 activeNotification = notification; 200 activeNotification = notification;
144 if (shouldDisplay("notification", activeNotification.type)) 201 if (shouldDisplay("notification", activeNotification.type))
145 { 202 {
146 let texts = NotificationStorage.getLocalizedTexts(notification); 203 let texts = NotificationStorage.getLocalizedTexts(notification);
147 let title = texts.title || ""; 204 let title = texts.title || "";
148 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 205 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
149 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); 206 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
150 let hasLinks = activeNotification.links && activeNotification.links.length > 0; 207 let linkCount = (activeNotification.links || []).length;
151 208
152 if (canUseChromeNotifications) 209 if (canUseChromeNotifications)
153 { 210 {
211 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age);
154 let opts = { 212 let opts = {
155 type: "basic", 213 type: "basic",
156 title: title, 214 title: title,
157 message: message, 215 message: message,
158 buttons: [], 216 buttons: activeButtons.map(button => ({title: button.title})),
159 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
160 }; 218 };
161 if (activeNotification.type == "question")
162 {
163 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")});
164 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")});
165 }
166 else
167 {
168 let regex = /<a>(.*?)<\/a>/g;
169 let plainMessage = texts.message || "";
170 let match;
171 while (match = regex.exec(plainMessage))
172 opts.buttons.push({title: match[1]});
173 }
174 219
175 imgToBase64(iconUrl, function(iconData) 220 imgToBase64(iconUrl, function(iconData)
176 { 221 {
177 opts["iconUrl"] = iconData; 222 opts.iconUrl = iconData;
178 chrome.notifications.create("", opts, function() {}); 223 chrome.notifications.create("", opts, function() {});
179 }); 224 });
180 } 225 }
181 else if ("Notification" in window && activeNotification.type != "question") 226 else if ("Notification" in window && activeNotification.type != "question")
182 { 227 {
183 if (hasLinks) 228 if (linkCount > 0)
184 message += " " + ext.i18n.getMessage("notification_without_buttons"); 229 message += " " + ext.i18n.getMessage("notification_without_buttons");
185 230
186 imgToBase64(iconUrl, function(iconData) 231 imgToBase64(iconUrl, function(iconData)
187 { 232 {
188 let notification = new Notification( 233 let notification = new Notification(
189 title, 234 title,
190 { 235 {
191 lang: Utils.appLocale, 236 lang: Utils.appLocale,
192 dir: ext.i18n.getMessage("@@bidi_dir"), 237 dir: ext.i18n.getMessage("@@bidi_dir"),
193 body: message, 238 body: message,
194 icon: iconData 239 icon: iconData
195 } 240 }
196 ); 241 );
197 242
198 notification.addEventListener("click", openNotificationLinks); 243 notification.addEventListener("click", openNotificationLinks);
199 notification.addEventListener("close", notificationClosed); 244 notification.addEventListener("close", notificationClosed);
200 }); 245 });
201 } 246 }
202 else 247 else
203 { 248 {
204 let message = title + "\n" + message; 249 let message = title + "\n" + message;
205 if (hasLinks) 250 if (linkCount > 0)
206 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 251 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
207 252
208 let approved = confirm(message); 253 let approved = confirm(message);
209 if (activeNotification.type == "question") 254 if (activeNotification.type == "question")
210 notificationButtonClick(approved ? 0 : 1); 255 notificationButtonClick(approved ? 0 : 1);
211 else if (approved) 256 else if (approved)
212 openNotificationLinks(); 257 openNotificationLinks();
213 } 258 }
214 } 259 }
215 prepareNotificationIconAndPopup(); 260 prepareNotificationIconAndPopup();
(...skipping 22 matching lines...) Expand all
238 /** 283 /**
239 * Gets the active notification to be shown if any. 284 * Gets the active notification to be shown if any.
240 * 285 *
241 * @return {?object} 286 * @return {?object}
242 */ 287 */
243 exports.getActiveNotification = function() 288 exports.getActiveNotification = function()
244 { 289 {
245 return activeNotification; 290 return activeNotification;
246 }; 291 };
247 292
293 let shouldDisplay =
Sebastian Noack 2015/09/11 14:51:00 This one is unrelated.
248 /** 294 /**
249 * Determines whether a given display method should be used for a 295 * Determines whether a given display method should be used for a
250 * specified notification type. 296 * specified notification type.
251 * 297 *
252 * @param {string} method Display method: icon, notification or popup 298 * @param {string} method Display method: icon, notification or popup
253 * @param {string} notificationType 299 * @param {string} notificationType
254 * @return {boolean} 300 * @return {boolean}
255 */ 301 */
256 exports.shouldDisplay = shouldDisplay; 302 exports.shouldDisplay = function(method, notificationType)
257 function shouldDisplay(method, notificationType)
258 { 303 {
259 let methods = displayMethods[notificationType] || defaultDisplayMethods; 304 let methods = displayMethods[notificationType] || defaultDisplayMethods;
260 return methods.indexOf(method) > -1; 305 return methods.indexOf(method) > -1;
261 } 306 }
262 307
263 NotificationStorage.addShowListener(showNotification); 308 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