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 initial feedback Created Jan. 21, 2016, 8:58 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
(...skipping 134 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 Utils.loadImage(url).then(function(image)
158 {
159 let canvas = document.createElement("canvas");
160 let ctx = canvas.getContext("2d");
161 canvas.height = image.height;
162 canvas.width = image.width;
163 ctx.drawImage(image, 0, 0);
164 return canvas.toDataURL("image/png");
165 });
166 }
167
168 function initChromeNotifications() 155 function initChromeNotifications()
169 { 156 {
170 // 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
171 function clearActiveNotification(notificationId) 158 function clearActiveNotification(notificationId)
172 { 159 {
173 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification)) 160 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification))
174 return; 161 return;
175 162
176 chrome.notifications.clear(notificationId, function(wasCleared) 163 chrome.notifications.clear(notificationId, function(wasCleared)
177 { 164 {
(...skipping 21 matching lines...) Expand all
199 { 186 {
200 let texts = NotificationStorage.getLocalizedTexts(notification); 187 let texts = NotificationStorage.getLocalizedTexts(notification);
201 let title = texts.title || ""; 188 let title = texts.title || "";
202 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : ""; 189 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
203 let iconUrl = ext.getURL("icons/detailed/abp-128.png"); 190 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
204 let linkCount = (activeNotification.links || []).length; 191 let linkCount = (activeNotification.links || []).length;
205 192
206 if (canUseChromeNotifications) 193 if (canUseChromeNotifications)
207 { 194 {
208 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age); 195 activeButtons = getNotificationButtons(activeNotification.type, texts.mess age);
209 let opts = { 196 chrome.notifications.create("", {
210 type: "basic", 197 type: "basic",
211 title: title, 198 title: title,
199 iconUrl: iconUrl,
212 message: message, 200 message: message,
213 buttons: activeButtons.map(button => ({title: button.title})), 201 buttons: activeButtons.map(button => ({title: button.title})),
214 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
215 };
216
217 imgToBase64(iconUrl).then(function(iconData)
218 {
219 opts.iconUrl = iconData;
220 chrome.notifications.create("", opts, function() {});
221 }); 203 });
222 } 204 }
223 else if ("Notification" in window && activeNotification.type != "question") 205 else if ("Notification" in window && activeNotification.type != "question")
224 { 206 {
225 if (linkCount > 0) 207 if (linkCount > 0)
226 message += " " + ext.i18n.getMessage("notification_without_buttons"); 208 message += " " + ext.i18n.getMessage("notification_without_buttons");
227 209
228 imgToBase64(iconUrl).then(function(iconData) 210 let notification = new Notification(
229 { 211 title,
230 let notification = new Notification( 212 {
231 title, 213 lang: Utils.appLocale,
232 { 214 dir: ext.i18n.getMessage("@@bidi_dir"),
233 lang: Utils.appLocale, 215 body: message,
234 dir: ext.i18n.getMessage("@@bidi_dir"), 216 icon: iconUrl
235 body: message, 217 }
236 icon: iconData 218 );
237 } 219
238 ); 220 notification.addEventListener("click", openNotificationLinks);
239 221 notification.addEventListener("close", notificationClosed);
240 notification.addEventListener("click", openNotificationLinks);
241 notification.addEventListener("close", notificationClosed);
242 });
243 } 222 }
244 else 223 else
245 { 224 {
246 let message = title + "\n" + message; 225 let message = title + "\n" + message;
247 if (linkCount > 0) 226 if (linkCount > 0)
248 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons"); 227 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
249 228
250 let approved = confirm(message); 229 let approved = confirm(message);
251 if (activeNotification.type == "question") 230 if (activeNotification.type == "question")
252 notificationButtonClick(approved ? 0 : 1); 231 notificationButtonClick(approved ? 0 : 1);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 * specified notification type. 272 * specified notification type.
294 * 273 *
295 * @param {string} method Display method: icon, notification or popup 274 * @param {string} method Display method: icon, notification or popup
296 * @param {string} notificationType 275 * @param {string} notificationType
297 * @return {boolean} 276 * @return {boolean}
298 */ 277 */
299 exports.shouldDisplay = function(method, notificationType) 278 exports.shouldDisplay = function(method, notificationType)
300 { 279 {
301 let methods = displayMethods[notificationType] || defaultDisplayMethods; 280 let methods = displayMethods[notificationType] || defaultDisplayMethods;
302 return methods.indexOf(method) > -1; 281 return methods.indexOf(method) > -1;
303 } 282 };
304 283
305 NotificationStorage.addShowListener(showNotification); 284 NotificationStorage.addShowListener(showNotification);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld