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: Moved intilization code into module Created June 4, 2015, 9:35 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 let {startIconAnimation, stopIconAnimation} = require("icon");
19 let {Utils} = require("utils");
20 let {Notification: NotificationStorage} = require("notification");
21 let {stringifyURL} = require("url");
22 let {initAntiAdblockNotification} = require("antiadblockInit");
23
24 let activeNotification = null;
25
26 // Chrome on Linux does not fully support chrome.notifications until version 35
27 // https://code.google.com/p/chromium/issues/detail?id=291485
28 let canUseChromeNotifications = (function()
29 {
30 let info = require("info");
31 if (info.platform == "chromium" && "notifications" in chrome)
32 {
33 if (navigator.platform.indexOf("Linux") == -1)
34 return true;
35 if (Services.vc.compare(info.applicationVersion, "35") >= 0)
36 return true;
37 }
38 return false;
39 })();
40
41 function prepareNotificationIconAndPopup()
42 {
43 let animateIcon = (activeNotification.type != "question");
44 activeNotification.onClicked = function()
45 {
46 if (animateIcon)
47 stopIconAnimation();
48 notificationClosed();
49 };
50 if (animateIcon)
51 startIconAnimation(activeNotification.type);
52 }
53
54 function openNotificationLinks()
55 {
56 if (activeNotification.links)
57 {
58 activeNotification.links.forEach(function(link)
59 {
60 ext.windows.getLastFocused(function(win)
61 {
62 win.openTab(Utils.getDocLink(link));
63 });
64 });
65 }
66 }
67
68 function notificationButtonClick(buttonIndex)
69 {
70 if (activeNotification.type == "question")
71 {
72 NotificationStorage.triggerQuestionListeners(activeNotification.id, buttonIn dex == 0);
73 NotificationStorage.markAsShown(activeNotification.id);
74 activeNotification.onClicked();
75 }
76 else if (activeNotification.links && activeNotification.links[buttonIndex])
77 {
78 ext.windows.getLastFocused(function(win)
79 {
80 win.openTab(Utils.getDocLink(activeNotification.links[buttonIndex]));
81 });
82 }
83 }
84
85 function notificationClosed()
86 {
87 activeNotification = null;
88 }
89
90 function imgToBase64(url, callback)
91 {
92 let canvas = document.createElement("canvas"),
93 ctx = canvas.getContext("2d"),
94 img = new Image;
95 img.src = url;
96 img.onload = function()
97 {
98 canvas.height = img.height;
99 canvas.width = img.width;
100 ctx.drawImage(img, 0, 0);
101 callback(canvas.toDataURL("image/png"));
102 canvas = null;
103 };
104 }
105
106 function initChromeNotifications()
107 {
108 // Chrome hides notifications in notification center when clicked so we need t o clear them
109 function clearActiveNotification(notificationId)
110 {
111 if (activeNotification && activeNotification.type != "question" && !("links" in activeNotification))
112 return;
113
114 chrome.notifications.clear(notificationId, function(wasCleared)
115 {
116 if (wasCleared)
117 notificationClosed();
118 });
119 }
120
121 chrome.notifications.onButtonClicked.addListener(function(notificationId, butt onIndex)
122 {
123 notificationButtonClick(buttonIndex);
124 clearActiveNotification(notificationId);
125 });
126 chrome.notifications.onClicked.addListener(clearActiveNotification);
127 chrome.notifications.onClosed.addListener(notificationClosed);
128 }
129
130 /**
131 * Initilizes the notification system.
Thomas Greiner 2015/06/05 13:50:52 Replace "Initilizes" with "Initializes"
Sebastian Noack 2015/06/05 14:06:13 Done.
132 */
133 exports.initNotifications = function()
134 {
135 if (canUseChromeNotifications)
136 initChromeNotifications();
137 initAntiAdblockNotification();
138 };
139
140 let showNextNotification =
141 /**
142 * Shows the next notification from the queue if any.
143 *
144 * @param {URL} [url] URL to match notifications to
145 */
146 exports.showNextNotification = function(url)
147 {
148 let notification = NotificationStorage.getNextToShow(url && stringifyURL(url)) ;
149 if (!notification || activeNotification && activeNotification.id == notificati on.id)
150 return;
151
152 activeNotification = notification;
153 if (activeNotification.type == "critical" || activeNotification.type == "quest ion")
154 {
155 let texts = NotificationStorage.getLocalizedTexts(notification);
156 let title = texts.title || "";
157 let message = texts.message ? texts.message.replace(/<\/?(a|strong)>/g, "") : "";
158 let iconUrl = ext.getURL("icons/detailed/abp-128.png");
159 let hasLinks = activeNotification.links && activeNotification.links.length > 0;
160
161 if (canUseChromeNotifications)
162 {
163 let opts = {
164 type: "basic",
165 title: title,
166 message: message,
167 buttons: [],
168 priority: 2 // We use the highest priority to prevent the notification f rom closing automatically
169 };
170 if (activeNotification.type == "question")
171 {
172 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_yes")});
173 opts.buttons.push({title: ext.i18n.getMessage("overlay_notification_butt on_no")});
174 }
175 else
176 {
177 let regex = /<a>(.*?)<\/a>/g;
178 let plainMessage = texts.message || "";
179 let match;
180 while (match = regex.exec(plainMessage))
181 opts.buttons.push({title: match[1]});
182 }
183
184 imgToBase64(iconUrl, function(iconData)
185 {
186 opts["iconUrl"] = iconData;
187 chrome.notifications.create("", opts, function() {});
188 });
189 }
190 else if ("Notification" in window && activeNotification.type != "question")
191 {
192 if (hasLinks)
193 message += " " + ext.i18n.getMessage("notification_without_buttons");
194
195 imgToBase64(iconUrl, function(iconData)
196 {
197 let notification = new Notification(
198 title,
199 {
200 lang: Utils.appLocale,
201 dir: ext.i18n.getMessage("@@bidi_dir"),
202 body: message,
203 icon: iconData
204 }
205 );
206
207 notification.addEventListener("click", openNotificationLinks);
208 notification.addEventListener("close", notificationClosed);
209 });
210 }
211 else
212 {
213 let message = title + "\n" + message;
214 if (hasLinks)
215 message += "\n\n" + ext.i18n.getMessage("notification_with_buttons");
216
217 let approved = confirm(message);
218 if (activeNotification.type == "question")
219 notificationButtonClick(approved ? 0 : 1);
220 else if (approved)
221 openNotificationLinks();
222 }
223 }
224 prepareNotificationIconAndPopup();
225 };
226
227 setTimeout(showNextNotification, 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