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

Side by Side Diff: lib/notificationHelper.js

Issue 6063199216467968: Issue 2642 - Moved notifications code to separate module (Closed)
Patch Set: Created June 4, 2015, 9:31 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 | « background.js ('k') | metadata.common » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 var {startIconAnimation, stopIconAnimation} = require("icon");
19 var {Utils} = require("utils");
20 var {Notification: NotificationStorage} = require("notification");
Sebastian Noack 2015/06/04 21:38:15 Except for the imports here, the code below has be
21
22 var activeNotification = null;
23
24 // Chrome on Linux does not fully support chrome.notifications until version 35
25 // https://code.google.com/p/chromium/issues/detail?id=291485
26 var canUseChromeNotifications = (function()
27 {
28 var info = require("info");
29 if (info.platform == "chromium" && "notifications" in chrome)
30 {
31 if (navigator.platform.indexOf("Linux") == -1)
32 return true;
33 if (Services.vc.compare(info.applicationVersion, "35") >= 0)
34 return true;
35 }
36 return false;
37 })();
38
39 function prepareNotificationIconAndPopup()
40 {
41 var animateIcon = (activeNotification.type !== "question");
42 activeNotification.onClicked = function()
43 {
44 if (animateIcon)
45 stopIconAnimation();
46 notificationClosed();
47 };
48 if (animateIcon)
49 startIconAnimation(activeNotification.type);
50 }
51
52 function openNotificationLinks()
53 {
54 if (activeNotification.links)
55 {
56 activeNotification.links.forEach(function(link)
57 {
58 ext.windows.getLastFocused(function(win)
59 {
60 win.openTab(Utils.getDocLink(link));
61 });
62 });
63 }
64 }
65
66 function notificationButtonClick(buttonIndex)
67 {
68 if (activeNotification.type === "question")
69 {
70 NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIn dex === 0);
71 NotificationStorage.markAsShown(activeNotification.id);
72 activeNotification.onClicked();
73 }
74 else if (activeNotification.links && activeNotification.links[buttonIndex])
75 {
76 ext.windows.getLastFocused(function(win)
77 {
78 win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex]));
79 });
80 }
81 }
82
83 function notificationClosed()
84 {
85 activeNotification = null;
86 }
87
88 function imgToBase64(url, callback)
89 {
90 var canvas = document.createElement("canvas"),
91 ctx = canvas.getContext("2d"),
92 img = new Image;
93 img.src = url;
94 img.onload = function()
95 {
96 canvas.height = img.height;
97 canvas.width = img.width;
98 ctx.drawImage(img, 0, 0);
99 callback(canvas.toDataURL("image/png"));
100 canvas = null;
101 };
102 }
103
104 function initChromeNotifications()
105 {
106 // Chrome hides notifications in notification center when clicked so we need t o clear them
107 function clearActiveNotification(notificationId)
108 {
109 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification))
110 return;
111
112 chrome.notifications.clear(notificationId, function(wasCleared)
113 {
114 if (wasCleared)
115 notificationClosed();
116 });
117 }
118
119 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt onIndex)
120 {
121 notificationButtonClick(buttonIndex);
122 clearActiveNotification(notificationId);
123 });
124 chrome.notifications.onClicked.addListener(clearActiveNotification);
125 chrome.notifications.onClosed.addListener(notificationClosed);
126 }
127
128 function showNotification(notification)
129 {
130 if (activeNotification && activeNotification.id === notification.id)
131 return;
132
133 activeNotification = notification;
134 if (activeNotification.type === "critical" || activeNotification.type === "que stion")
135 {
136 var texts = NotificationStorage.getLocalizedTexts(notification);
137 var title = texts.title || "";
138 var message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
139 var iconUrl = ext.getURL("icons/detailed/abp-128.png");
140 var hasLinks = activeNotification.links && activeNotification.links.length > 0;
141
142 if (canUseChromeNotifications)
143 {
144 var opts = {
145 type: "basic",
146 title: title,
147 message: message,
148 buttons: [],
149 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
150 };
151 if (activeNotification.type === "question")
152 {
153 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")});
154 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")});
155 }
156 else
157 {
158 var regex = /<a>(.*?)<\/a>/g;
159 var plainMessage = texts.message || "";
160 var match;
161 while (match = regex.exec(plainMessage))
162 opts.buttons.push({title: match[1]});
163 }
164
165 imgToBase64(iconUrl, function(iconData)
166 {
167 opts["iconUrl"] = iconData;
168 chrome.notifications.create("", opts, function() {});
169 });
170 }
171 else if ("Notification" in window && activeNotification.type !== "question")
172 {
173 if (hasLinks)
174 message += " " + ext.i18n.getMessage("notification_without_buttons");
175
176 imgToBase64(iconUrl, function(iconData)
177 {
178 var notification = new Notification(
179 title,
180 {
181 lang: Utils.appLocale,
182 dir: ext.i18n.getMessage("@@bidi_dir"),
183 body: message,
184 icon: iconData
185 }
186 );
187
188 notification.addEventListener("click", openNotificationLinks);
189 notification.addEventListener("close", notificationClosed);
190 });
191 }
192 else
193 {
194 var message = title + "\n" + message;
195 if (hasLinks)
196 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
197
198 var approved = confirm(message);
199 if (activeNotification.type === "question")
200 notificationButtonClick(approved ? 0 : 1);
201 else if (approved)
202 openNotificationLinks();
203 }
204 }
205 prepareNotificationIconAndPopup();
206 }
207
208 setTimeout(function()
209 {
210 var notificationToShow = NotificationStorage.getNextToShow();
211 if (notificationToShow)
212 showNotification(notificationToShow);
213 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « background.js ('k') | metadata.common » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld