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

Delta Between Two Patch Sets: lib/notificationHelper.js

Issue 29334223: Issue 3532 - Generate animation images at runtime (Closed)
Left Patch Set: Addressed further feedback Created Jan. 22, 2016, 4:47 p.m.
Right Patch Set: Addressed final nits Created Jan. 26, 2016, 10:57 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 | « lib/icon.js ('k') | metadata.chrome » ('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-2016 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 {loadImage, 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 activeButtons = null;
28 let defaultDisplayMethods = ["popup"]; 28 let defaultDisplayMethods = ["popup"];
29 let displayMethods = Object.create(null); 29 let displayMethods = Object.create(null);
30 displayMethods.critical = ["icon", "notification", "popup"]; 30 displayMethods.critical = ["icon", "notification", "popup"];
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 activeNotification.onClicked(); 145 activeNotification.onClicked();
146 break; 146 break;
147 } 147 }
148 } 148 }
149 149
150 function notificationClosed() 150 function notificationClosed()
151 { 151 {
152 activeNotification = null; 152 activeNotification = null;
153 } 153 }
154 154
155 function imgToBase64(url, callback)
156 {
157 return loadImage(url).then(function(canvas)
158 {
159 return canvas.toDataURL("image/png");
Sebastian Noack 2016/01/23 14:04:10 Perhaps, we can simply get rid of this helper func
kzar 2016/01/23 14:43:14 Done.
160 });
161 }
162
163 function initChromeNotifications() 155 function initChromeNotifications()
164 { 156 {
165 // Chrome hides notifications in notification center when clicked so we need t o clear them 157 // Chrome hides notifications in notification center when clicked so we need t o clear them
166 function clearActiveNotification(notificationId) 158 function clearActiveNotification(notificationId)
167 { 159 {
168 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification)) 160 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification))
169 return; 161 return;
170 162
171 chrome.notifications.clear(notificationId, function(wasCleared) 163 chrome.notifications.clear(notificationId, function(wasCleared)
172 { 164 {
(...skipping 21 matching lines...) Expand all
194 { 186 {
195 let texts = NotificationStorage.getLocalizedTexts(notification); 187 let texts = NotificationStorage.getLocalizedTexts(notification);
196 let title = texts.title || ""; 188 let title = texts.title || "";
197 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 189 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
198 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); 190 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
199 let linkCount = (activeNotification.links || []).length; 191 let linkCount = (activeNotification.links || []).length;
200 192
201 if (canUseChromeNotifications) 193 if (canUseChromeNotifications)
202 { 194 {
203 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age); 195 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age);
204 let opts = { 196 chrome.notifications.create("", {
205 type: "basic", 197 type: "basic",
206 title: title, 198 title: title,
199 iconUrl: iconUrl,
207 message: message, 200 message: message,
208 buttons: activeButtons.map(button => ({title: button.title})), 201 buttons: activeButtons.map(button => ({title: button.title})),
209 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically 202 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
210 };
211
212 imgToBase64(iconUrl).then(function(iconData)
213 {
214 opts.iconUrl = iconData;
215 chrome.notifications.create("", opts, function() {});
216 }); 203 });
217 } 204 }
218 else if ("Notification" in window && activeNotification.type != "question") 205 else if ("Notification" in window && activeNotification.type != "question")
219 { 206 {
220 if (linkCount > 0) 207 if (linkCount > 0)
221 message += " " + ext.i18n.getMessage("notification_without_buttons"); 208 message += " " + ext.i18n.getMessage("notification_without_buttons");
222 209
223 imgToBase64(iconUrl).then(function(iconData) 210 let notification = new Notification(
224 { 211 title,
225 let notification = new Notification( 212 {
226 title, 213 lang: Utils.appLocale,
227 { 214 dir: ext.i18n.getMessage("@@bidi_dir"),
228 lang: Utils.appLocale, 215 body: message,
229 dir: ext.i18n.getMessage("@@bidi_dir"), 216 icon: iconUrl
230 body: message, 217 }
231 icon: iconData 218 );
232 } 219
233 ); 220 notification.addEventListener("click", openNotificationLinks);
234 221 notification.addEventListener("close", notificationClosed);
235 notification.addEventListener("click", openNotificationLinks);
236 notification.addEventListener("close", notificationClosed);
237 });
238 } 222 }
239 else 223 else
240 { 224 {
241 let message = title + "\n" + message; 225 let message = title + "\n" + message;
242 if (linkCount > 0) 226 if (linkCount > 0)
243 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 227 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
244 228
245 let approved = confirm(message); 229 let approved = confirm(message);
246 if (activeNotification.type == "question") 230 if (activeNotification.type == "question")
247 notificationButtonClick(approved ? 0 : 1); 231 notificationButtonClick(approved ? 0 : 1);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 * specified notification type. 272 * specified notification type.
289 * 273 *
290 * @param {string} method Display method: icon, notification or popup 274 * @param {string} method Display method: icon, notification or popup
291 * @param {string} notificationType 275 * @param {string} notificationType
292 * @return {boolean} 276 * @return {boolean}
293 */ 277 */
294 exports.shouldDisplay = function(method, notificationType) 278 exports.shouldDisplay = function(method, notificationType)
295 { 279 {
296 let methods = displayMethods[notificationType] || defaultDisplayMethods; 280 let methods = displayMethods[notificationType] || defaultDisplayMethods;
297 return methods.indexOf(method) > -1; 281 return methods.indexOf(method) > -1;
298 } 282 };
299 283
300 NotificationStorage.addShowListener(showNotification); 284 NotificationStorage.addShowListener(showNotification);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld