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

Delta Between Two Patch Sets: lib/notificationHelper.js

Issue 29338190: Issue 3697 - Fall back to i18n.getUILanguage if @ui_locale isn't supported (Closed)
Left Patch Set: Remove Safari redundancy and Firefox specific codepath Created March 17, 2016, 5:38 a.m.
Right Patch Set: Address the nits Created Aug. 30, 2017, 10:59 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 | « dependencies ('k') | lib/utils.js » ('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-2016 Eyeo GmbH 3 * Copyright (C) 2006-present 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"];
36 displayMethods.relentless = ["notification"];
33 displayMethods.information = ["icon", "popup"]; 37 displayMethods.information = ["icon", "popup"];
34 38
35 // Chrome on Linux does not fully support chrome.notifications until version 35
36 // https://code.google.com/p/chromium/issues/detail?id=291485
37 let canUseChromeNotifications = (function()
38 {
39 let info = require("info");
40 if (info.platform == "chromium" && "notifications" in chrome)
41 {
42 if (navigator.platform.indexOf("Linux") == -1)
43 return true;
44 if (Services.vc.compare(info.applicationVersion, "35") >= 0)
45 return true;
46 }
47 return false;
48 })();
49
50 function prepareNotificationIconAndPopup() 39 function prepareNotificationIconAndPopup()
51 { 40 {
52 let animateIcon = shouldDisplay("icon", activeNotification.type); 41 let animateIcon = shouldDisplay("icon", activeNotification.type);
53 activeNotification.onClicked = function() 42 activeNotification.onClicked = () =>
54 { 43 {
55 if (animateIcon) 44 if (animateIcon)
56 stopIconAnimation(); 45 stopIconAnimation();
57 notificationClosed(); 46 notificationClosed();
58 }; 47 };
59 if (animateIcon) 48 if (animateIcon)
60 startIconAnimation(activeNotification.type); 49 startIconAnimation(activeNotification.type);
61 } 50 }
62 51
63 function getNotificationButtons(notificationType, message) 52 function getNotificationButtons(notificationType, message)
(...skipping 27 matching lines...) Expand all
91 let maxButtons = (notificationType == "critical") ? 2 : 1; 80 let maxButtons = (notificationType == "critical") ? 2 : 1;
92 if (buttons.length > maxButtons) 81 if (buttons.length > maxButtons)
93 { 82 {
94 buttons = [ 83 buttons = [
95 { 84 {
96 type: "open-all", 85 type: "open-all",
97 title: ext.i18n.getMessage("notification_open_all") 86 title: ext.i18n.getMessage("notification_open_all")
98 } 87 }
99 ]; 88 ];
100 } 89 }
101 if (notificationType != "critical") 90 if (!["critical", "relentless"].includes(notificationType))
102 { 91 {
103 buttons.push({ 92 buttons.push({
104 type: "configure", 93 type: "configure",
105 title: ext.i18n.getMessage("notification_configure") 94 title: ext.i18n.getMessage("notification_configure")
106 }); 95 });
107 } 96 }
108 } 97 }
109 98
110 return buttons; 99 return buttons;
111 } 100 }
112 101
113 function openNotificationLinks() 102 function openNotificationLinks()
114 { 103 {
115 if (activeNotification.links) 104 if (activeNotification.links)
116 { 105 {
117 for (let link of activeNotification.links) 106 for (let link of activeNotification.links)
118 ext.pages.open(Utils.getDocLink(link)); 107 ext.pages.open(Utils.getDocLink(link));
119 } 108 }
120 } 109 }
121 110
122 function notificationButtonClick(buttonIndex) 111 function notificationButtonClick(buttonIndex)
123 { 112 {
113 if (!(activeButtons && buttonIndex in activeButtons))
114 return;
115
124 switch (activeButtons[buttonIndex].type) 116 switch (activeButtons[buttonIndex].type)
125 { 117 {
126 case "link": 118 case "link":
127 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex])); 119 ext.pages.open(Utils.getDocLink(activeNotification.links[buttonIndex]));
128 break; 120 break;
129 case "open-all": 121 case "open-all":
130 openNotificationLinks(); 122 openNotificationLinks();
131 break; 123 break;
132 case "configure": 124 case "configure":
133 Prefs.notifications_showui = true; 125 Prefs.notifications_showui = true;
134 ext.showOptions(function(page) 126 ext.showOptions(page =>
135 { 127 {
136 page.sendMessage({ 128 page.sendMessage({
137 type: "focus-section", 129 type: "app.respond",
138 section: "notifications" 130 action: "focusSection",
131 args: ["notifications"]
139 }); 132 });
140 }); 133 });
141 break; 134 break;
142 case "question": 135 case "question":
143 NotificationStorage.triggerQuestionListeners(activeNotification.id, button Index == 0); 136 NotificationStorage.triggerQuestionListeners(activeNotification.id,
137 buttonIndex == 0);
144 NotificationStorage.markAsShown(activeNotification.id); 138 NotificationStorage.markAsShown(activeNotification.id);
145 activeNotification.onClicked(); 139 activeNotification.onClicked();
146 break; 140 break;
147 } 141 }
148 } 142 }
149 143
150 function notificationClosed() 144 function notificationClosed()
151 { 145 {
152 activeNotification = null; 146 activeNotification = null;
153 } 147 }
154 148
155 function initChromeNotifications() 149 function initChromeNotifications()
156 { 150 {
157 // Chrome hides notifications in notification center when clicked so we need t o clear them 151 // Chrome hides notifications in notification center when clicked so
152 // we need to clear them.
158 function clearActiveNotification(notificationId) 153 function clearActiveNotification(notificationId)
159 { 154 {
160 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification)) 155 if (activeNotification &&
156 activeNotification.type != "question" &&
157 !("links" in activeNotification))
161 return; 158 return;
162 159
163 chrome.notifications.clear(notificationId, function(wasCleared) 160 chrome.notifications.clear(notificationId, wasCleared =>
164 { 161 {
165 if (wasCleared) 162 if (wasCleared)
166 notificationClosed(); 163 notificationClosed();
167 }); 164 });
168 } 165 }
169 166
170 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt onIndex) 167 chrome.notifications.onButtonClicked.addListener(
171 { 168 (notificationId, buttonIndex) =>
172 notificationButtonClick(buttonIndex); 169 {
173 clearActiveNotification(notificationId); 170 notificationButtonClick(buttonIndex);
174 }); 171 clearActiveNotification(notificationId);
172 }
173 );
175 chrome.notifications.onClicked.addListener(clearActiveNotification); 174 chrome.notifications.onClicked.addListener(clearActiveNotification);
176 chrome.notifications.onClosed.addListener(notificationClosed); 175 chrome.notifications.onClosed.addListener(notificationClosed);
177 } 176 }
178 177
179 function showNotification(notification) 178 function showNotification(notification)
180 { 179 {
181 if (activeNotification && activeNotification.id == notification.id) 180 if (activeNotification && activeNotification.id == notification.id)
182 return; 181 return;
183 182
184 activeNotification = notification; 183 activeNotification = notification;
185 if (shouldDisplay("notification", activeNotification.type)) 184 if (shouldDisplay("notification", activeNotification.type))
186 { 185 {
187 let texts = NotificationStorage.getLocalizedTexts(notification); 186 let texts = NotificationStorage.getLocalizedTexts(notification);
188 let title = texts.title || ""; 187 let title = texts.title || "";
189 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 188 let message = (texts.message || "").replace(/<\/?(a|strong)>/g, "");
190 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); 189 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
191 let linkCount = (activeNotification.links || []).length; 190 let linkCount = (activeNotification.links || []).length;
192 191
193 if (canUseChromeNotifications) 192 if ("notifications" in chrome)
194 { 193 {
195 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age); 194 activeButtons = getNotificationButtons(activeNotification.type,
195 texts.message);
196 chrome.notifications.create("", { 196 chrome.notifications.create("", {
197 type: "basic", 197 type: "basic",
198 title: title, 198 title,
199 iconUrl: iconUrl, 199 iconUrl,
200 message: message, 200 message,
201 buttons: activeButtons.map(button => ({title: button.title})), 201 buttons: activeButtons.map(button => ({title: button.title})),
202 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically 202 // We use the highest priority to prevent the notification
203 // from closing automatically.
204 priority: 2
203 }); 205 });
204 } 206 }
205 else if ("Notification" in window && activeNotification.type != "question") 207 else if ("Notification" in window && activeNotification.type != "question")
206 { 208 {
207 if (linkCount > 0) 209 if (linkCount > 0)
208 message += " " + ext.i18n.getMessage("notification_without_buttons"); 210 message += " " + ext.i18n.getMessage("notification_without_buttons");
209 211
210 let notification = new Notification( 212 let widget = new Notification(
211 title, 213 title,
212 { 214 {
213 lang: Utils.appLocale, 215 lang: Utils.appLocale,
214 dir: Utils.readingDirection, 216 dir: Utils.readingDirection,
215 body: message, 217 body: message,
216 icon: iconUrl 218 icon: iconUrl
217 } 219 }
218 ); 220 );
219 221
220 notification.addEventListener("click", openNotificationLinks); 222 widget.addEventListener("click", openNotificationLinks);
221 notification.addEventListener("close", notificationClosed); 223 widget.addEventListener("close", notificationClosed);
222 } 224 }
223 else 225 else
224 { 226 {
225 let message = title + "\n" + message; 227 message = title + "\n" + message;
226 if (linkCount > 0) 228 if (linkCount > 0)
227 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 229 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
228 230
229 let approved = confirm(message); 231 let approved = confirm(message);
230 if (activeNotification.type == "question") 232 if (activeNotification.type == "question")
231 notificationButtonClick(approved ? 0 : 1); 233 notificationButtonClick(approved ? 0 : 1);
232 else if (approved) 234 else if (approved)
233 openNotificationLinks(); 235 openNotificationLinks();
234 } 236 }
235 } 237 }
236 prepareNotificationIconAndPopup(); 238 prepareNotificationIconAndPopup();
237 }; 239
240 if (notification.type !== "question")
241 NotificationStorage.markAsShown(notification.id);
242 }
238 243
239 /** 244 /**
240 * Initializes the notification system. 245 * Initializes the notification system.
241 */ 246 */
242 exports.initNotifications = function() 247 exports.initNotifications = () =>
243 { 248 {
244 if (canUseChromeNotifications) 249 if ("notifications" in chrome)
245 initChromeNotifications(); 250 initChromeNotifications();
246 initAntiAdblockNotification(); 251 initAntiAdblockNotification();
247 }; 252 };
248 253
249 /** 254 /**
250 * Shows the next notification (if any) for the supplied URL.
251 *
252 * @param {URL} url URL to match notifications to
253 */
254 exports.showNextNotificationForUrl = function(url)
255 {
256 NotificationStorage.showNext(stringifyURL(url));
257 };
258
259 /**
260 * Gets the active notification to be shown if any. 255 * Gets the active notification to be shown if any.
261 * 256 *
262 * @return {?object} 257 * @return {?object}
263 */ 258 */
264 exports.getActiveNotification = function() 259 exports.getActiveNotification = () => activeNotification;
265 {
266 return activeNotification;
267 };
268 260
269 let shouldDisplay = 261 let shouldDisplay =
270 /** 262 /**
271 * Determines whether a given display method should be used for a 263 * Determines whether a given display method should be used for a
272 * specified notification type. 264 * specified notification type.
273 * 265 *
274 * @param {string} method Display method: icon, notification or popup 266 * @param {string} method Display method: icon, notification or popup
275 * @param {string} notificationType 267 * @param {string} notificationType
276 * @return {boolean} 268 * @return {boolean}
277 */ 269 */
278 exports.shouldDisplay = function(method, notificationType) 270 exports.shouldDisplay = (method, notificationType) =>
279 { 271 {
280 let methods = displayMethods[notificationType] || defaultDisplayMethods; 272 let methods = displayMethods[notificationType] || defaultDisplayMethods;
281 return methods.indexOf(method) > -1; 273 return methods.includes(method);
282 }; 274 };
283 275
276 ext.pages.onLoading.addListener(page =>
277 {
278 NotificationStorage.showNext(stringifyURL(page.url));
279 });
280
284 NotificationStorage.addShowListener(showNotification); 281 NotificationStorage.addShowListener(showNotification);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld