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

Side by Side Diff: lib/notificationHelper.js

Issue 29334223: Issue 3532 - Generate animation images at runtime (Closed)
Patch Set: Addressed further feedback Created Jan. 22, 2016, 4:47 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
« lib/icon.js ('K') | « lib/icon.js ('k') | metadata.chrome » ('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-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 {startIconAnimation, stopIconAnimation} = require("icon"); 20 let {loadImage, 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) 155 function imgToBase64(url, callback)
Sebastian Noack 2016/01/23 14:04:10 Since you return a promise now, the callback param
kzar 2016/01/23 14:43:14 Acknowledged.
156 { 156 {
157 let canvas = document.createElement("canvas"), 157 return loadImage(url).then(function(canvas)
158 ctx = canvas.getContext("2d"),
159 img = new Image;
160 img.src = url;
161 img.onload = function()
162 { 158 {
163 canvas.height = img.height; 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.
164 canvas.width = img.width; 160 });
165 ctx.drawImage(img, 0, 0);
166 callback(canvas.toDataURL("image/png"));
167 canvas = null;
168 };
169 } 161 }
170 162
171 function initChromeNotifications() 163 function initChromeNotifications()
172 { 164 {
173 // Chrome hides notifications in notification center when clicked so we need t o clear them 165 // Chrome hides notifications in notification center when clicked so we need t o clear them
174 function clearActiveNotification(notificationId) 166 function clearActiveNotification(notificationId)
175 { 167 {
176 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification)) 168 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification))
177 return; 169 return;
178 170
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 { 202 {
211 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age); 203 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age);
212 let opts = { 204 let opts = {
213 type: "basic", 205 type: "basic",
214 title: title, 206 title: title,
215 message: message, 207 message: message,
216 buttons: activeButtons.map(button => ({title: button.title})), 208 buttons: activeButtons.map(button => ({title: button.title})),
217 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically 209 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
218 }; 210 };
219 211
220 imgToBase64(iconUrl, function(iconData) 212 imgToBase64(iconUrl).then(function(iconData)
221 { 213 {
222 opts.iconUrl = iconData; 214 opts.iconUrl = iconData;
223 chrome.notifications.create("", opts, function() {}); 215 chrome.notifications.create("", opts, function() {});
224 }); 216 });
225 } 217 }
226 else if ("Notification" in window && activeNotification.type != "question") 218 else if ("Notification" in window && activeNotification.type != "question")
227 { 219 {
228 if (linkCount > 0) 220 if (linkCount > 0)
229 message += " " + ext.i18n.getMessage("notification_without_buttons"); 221 message += " " + ext.i18n.getMessage("notification_without_buttons");
230 222
231 imgToBase64(iconUrl, function(iconData) 223 imgToBase64(iconUrl).then(function(iconData)
232 { 224 {
233 let notification = new Notification( 225 let notification = new Notification(
234 title, 226 title,
235 { 227 {
236 lang: Utils.appLocale, 228 lang: Utils.appLocale,
237 dir: ext.i18n.getMessage("@@bidi_dir"), 229 dir: ext.i18n.getMessage("@@bidi_dir"),
238 body: message, 230 body: message,
239 icon: iconData 231 icon: iconData
240 } 232 }
241 ); 233 );
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 * @param {string} notificationType 291 * @param {string} notificationType
300 * @return {boolean} 292 * @return {boolean}
301 */ 293 */
302 exports.shouldDisplay = function(method, notificationType) 294 exports.shouldDisplay = function(method, notificationType)
303 { 295 {
304 let methods = displayMethods[notificationType] || defaultDisplayMethods; 296 let methods = displayMethods[notificationType] || defaultDisplayMethods;
305 return methods.indexOf(method) > -1; 297 return methods.indexOf(method) > -1;
306 } 298 }
307 299
308 NotificationStorage.addShowListener(showNotification); 300 NotificationStorage.addShowListener(showNotification);
OLDNEW
« lib/icon.js ('K') | « lib/icon.js ('k') | metadata.chrome » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld