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

Side by Side Diff: background.js

Issue 11161031: Show notifications on startup (Chrome) (Closed)
Patch Set: Addressed issues (except for the animation) Created July 22, 2013, 12:27 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
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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 15 matching lines...) Expand all
26 { 26 {
27 this.Subscription = Subscription; 27 this.Subscription = Subscription;
28 this.DownloadableSubscription = DownloadableSubscription; 28 this.DownloadableSubscription = DownloadableSubscription;
29 } 29 }
30 var FilterStorage = require("filterStorage").FilterStorage; 30 var FilterStorage = require("filterStorage").FilterStorage;
31 var ElemHide = require("elemHide").ElemHide; 31 var ElemHide = require("elemHide").ElemHide;
32 var defaultMatcher = require("matcher").defaultMatcher; 32 var defaultMatcher = require("matcher").defaultMatcher;
33 var Prefs = require("prefs").Prefs; 33 var Prefs = require("prefs").Prefs;
34 var Synchronizer = require("synchronizer").Synchronizer; 34 var Synchronizer = require("synchronizer").Synchronizer;
35 var Utils = require("utils").Utils; 35 var Utils = require("utils").Utils;
36 var Notification = require("notification").Notification;
36 37
37 // Some types cannot be distinguished 38 // Some types cannot be distinguished
38 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT; 39 RegExpFilter.typeMap.OBJECT_SUBREQUEST = RegExpFilter.typeMap.OBJECT;
39 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER; 40 RegExpFilter.typeMap.MEDIA = RegExpFilter.typeMap.FONT = RegExpFilter.typeMap.OT HER;
40 41
41 var isFirstRun = false; 42 var isFirstRun = false;
42 var seenDataCorruption = false; 43 var seenDataCorruption = false;
43 require("filterNotifier").FilterNotifier.addListener(function(action) 44 require("filterNotifier").FilterNotifier.addListener(function(action)
44 { 45 {
45 if (action == "load") 46 if (action == "load")
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 { 101 {
101 // Ignore fragment identifier 102 // Ignore fragment identifier
102 var index = url.indexOf("#"); 103 var index = url.indexOf("#");
103 if (index >= 0) 104 if (index >= 0)
104 url = url.substring(0, index); 105 url = url.substring(0, index);
105 106
106 var result = defaultMatcher.matchesAny(url, type || "DOCUMENT", extractHostFro mURL(parentUrl || url), false); 107 var result = defaultMatcher.matchesAny(url, type || "DOCUMENT", extractHostFro mURL(parentUrl || url), false);
107 return (result instanceof WhitelistFilter ? result : null); 108 return (result instanceof WhitelistFilter ? result : null);
108 } 109 }
109 110
111 var activeNotification = null;
112
110 // Adds or removes page action icon according to options. 113 // Adds or removes page action icon according to options.
111 function refreshIconAndContextMenu(tab) 114 function refreshIconAndContextMenu(tab)
112 { 115 {
113 // The tab could have been closed by the time this function is called 116 // The tab could have been closed by the time this function is called
114 if(!tab) 117 if(!tab)
115 return; 118 return;
116 119
117 var excluded = isWhitelisted(tab.url); 120 var excluded = isWhitelisted(tab.url);
118 var iconFilename = excluded ? "icons/abp-19-whitelisted.png" : "icons/abp-19.p ng"; 121 var iconFilename = excluded ? "icons/abp-19-whitelisted.png" : "icons/abp-19.p ng";
119 chrome.pageAction.setIcon({tabId: tab.id, path: iconFilename}); 122
123 if (activeNotification)
124 setNotificationPageAction(tab, iconFilename);
Wladimir Palant 2013/07/22 14:18:36 setNotificationPageAction() won't do anything for
Felix Dahlke 2013/07/22 15:10:22 Yes, it was like that. In patch set 4, all notific
125 else
126 chrome.pageAction.setIcon({tabId: tab.id, path: iconFilename});
120 127
121 // Only show icon for pages we can influence (http: and https:) 128 // Only show icon for pages we can influence (http: and https:)
122 if(/^https?:/.test(tab.url)) 129 if(/^https?:/.test(tab.url))
123 { 130 {
124 chrome.pageAction.setTitle({tabId: tab.id, title: "Adblock Plus"}); 131 chrome.pageAction.setTitle({tabId: tab.id, title: "Adblock Plus"});
125 if ("shouldShowIcon" in localStorage && localStorage["shouldShowIcon"] == "f alse") 132 if ("shouldShowIcon" in localStorage && localStorage["shouldShowIcon"] == "f alse")
126 chrome.pageAction.hide(tab.id); 133 chrome.pageAction.hide(tab.id);
127 else 134 else
128 chrome.pageAction.show(tab.id); 135 chrome.pageAction.show(tab.id);
129 136
130 // Set context menu status according to whether current tab has whitelisted domain 137 // Set context menu status according to whether current tab has whitelisted domain
131 if (excluded) 138 if (excluded)
132 chrome.contextMenus.removeAll(); 139 chrome.contextMenus.removeAll();
133 else 140 else
134 showContextMenu(); 141 showContextMenu();
135 } 142 }
136 } 143 }
137 144
145 function loadImages(imageFiles, callback)
146 {
147 var images = {};
148 var imagesLoaded = 0;
149 imageFiles.forEach(function(imageFile)
150 {
151 var image = new Image();
152 image.src = imageFile;
153 image.addEventListener("load", function()
154 {
155 images[imageFile] = image;
156 if (++imagesLoaded === imageFiles.length)
157 callback(images);
158 });
159 });
160 }
161
162 var iconAnimationTimer = null;
163
164 function stopIconAnimation()
165 {
166 if (!iconAnimationTimer)
167 return;
168
169 clearTimeout(iconAnimationTimer);
170 iconAnimationTimer = null;
171 chrome.tabs.getAllInWindow(null, function(tabs)
172 {
173 tabs.forEach(refreshIconAndContextMenu);
174 activeNotification = null;
Wladimir Palant 2013/07/22 14:18:36 Shouldn't activeNotification be reset before calli
Felix Dahlke 2013/07/22 15:10:22 Yes, I fixed that in patch set 4.
175 });
176 }
177
178 function setNotificationPageAction(tab, iconFileName)
179 {
180 if (activeNotification.severity !== "critical")
181 return;
182
183 stopIconAnimation();
184
185 var notificationIconFile = "icons/notification-critical.png";
186 var iconFiles = [iconFileName, notificationIconFile];
187 loadImages(iconFiles, function(images)
188 {
189 var abpIcon = images[iconFileName];
190 var notificationIcon = images[notificationIconFile];
191 var canvas = document.createElement("canvas");
192 canvas.width = abpIcon.width;
193 canvas.height = abpIcon.height;
194 var context = canvas.getContext("2d");
195 context.clearRect(0, 0, canvas.width, canvas.height);
196 context.drawImage(abpIcon, 0, 0);
197 context.drawImage(notificationIcon, 0, 0);
198 var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
Wladimir Palant 2013/07/22 14:18:36 The code to combine two images with some opacity a
Felix Dahlke 2013/07/22 15:10:22 Not necessary anymore, as of patch set 4.
199 chrome.pageAction.setIcon({tabId: tab.id, imageData: imageData});
200 });
201 }
202
138 /** 203 /**
139 * Old versions stored filter data in the localStorage object, this will import 204 * Old versions stored filter data in the localStorage object, this will import
140 * it into FilterStorage properly. 205 * it into FilterStorage properly.
141 */ 206 */
142 function importOldData() 207 function importOldData()
143 { 208 {
144 function addSubscription(url, title) 209 function addSubscription(url, title)
145 { 210 {
146 try 211 try
147 { 212 {
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 chrome.tabs.onUpdated.removeListener(listener); 531 chrome.tabs.onUpdated.removeListener(listener);
467 onLoad(); 532 onLoad();
468 } 533 }
469 }; 534 };
470 chrome.tabs.onUpdated.addListener(listener); 535 chrome.tabs.onUpdated.addListener(listener);
471 } 536 }
472 }); 537 });
473 } 538 }
474 } 539 }
475 540
541 function startInformationNotificationAnimation()
542 {
543 stopIconAnimation();
544
545 var abpIconFile = "icons/abp-19.png";
Wladimir Palant 2013/07/22 14:18:36 This still hardcodes the icon and fails to conside
Felix Dahlke 2013/07/22 15:10:22 Has been fixed in patch set 4. But yeah, second co
Wladimir Palant 2013/07/23 07:25:04 No, we can still work with a single global timer -
Felix Dahlke 2013/07/23 10:38:08 Done. I do prefer this, just wasn't able to come u
546 var notificationIconFile = "icons/notification-information.png";
547 var iconFiles = [abpIconFile, notificationIconFile];
548 loadImages(iconFiles, function(images)
549 {
550 var abpIcon = images[abpIconFile];
551 var notificationIcon = images[notificationIconFile];
552 var canvas = document.createElement("canvas");
553 canvas.width = abpIcon.width;
554 canvas.height = abpIcon.height;
555 var context = canvas.getContext("2d");
556
557 var animationStartTime = Date.now();
558 var animationInterval = 1000 / 10;
559 function animationStep()
560 {
561 var timeElapsed = Date.now() - animationStartTime;
562 var duration = 3000;
563 var animationTime = timeElapsed % duration;
564 var delay = 1000;
565 var fadeInEndTime = duration / 2 - delay / 2;
566 var fadeOutStartTime = fadeInEndTime + delay;
567
568 var opacity;
569 if (animationTime < fadeInEndTime)
570 opacity = animationTime / fadeInEndTime;
571 else if (animationTime > fadeOutStartTime)
572 opacity = 1 - (animationTime - fadeOutStartTime)
573 / (duration - fadeOutStartTime);
Wladimir Palant 2013/07/22 14:18:36 Pretty complicated and IMHO not really necessary c
Felix Dahlke 2013/07/22 15:10:22 It's not really possible to get smooth animations
Felix Dahlke 2013/07/22 15:16:45 But I definitely prefer how you approached the del
Wladimir Palant 2013/07/23 07:25:04 You don't have a smooth animation right now, exact
Felix Dahlke 2013/07/23 10:38:08 Implemented this as you prefer it, since it's not
574
575 context.clearRect(0, 0, canvas.width, canvas.height);
576 context.globalAlpha = 1;
577 context.drawImage(abpIcon, 0, 0);
578 context.globalAlpha = opacity;
579 context.drawImage(notificationIcon, 0, 0);
580 var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
581
582 chrome.tabs.getSelected(null, function(tab)
583 {
584 chrome.pageAction.setIcon({tabId: tab.id, imageData: imageData});
585 iconAnimationTimer = setTimeout(animationStep, animationInterval);
586 });
587 }
588 animationStep();
589 });
590 }
591
592 function showInformationNotification()
593 {
594 activeNotification.onClicked = function()
595 {
596 stopIconAnimation();
597 activeNotification = null;
598 };
599
600 startInformationNotificationAnimation();
601 }
602
603 function showCriticalNotification()
604 {
605 var notification = webkitNotifications.createHTMLNotification("notification.ht ml");
606 notification.show();
607 }
608
609 function showNotification(notification)
610 {
611 activeNotification = notification;
612
613 if (notification.severity === "critical")
614 showCriticalNotification();
615 else
616 showInformationNotification();
617
618 chrome.tabs.getAllInWindow(null, function(tabs)
619 {
620 tabs.forEach(refreshIconAndContextMenu);
621 });
622 }
623
476 /** 624 /**
477 * This function is a hack - we only know the tabId and document URL for a 625 * This function is a hack - we only know the tabId and document URL for a
478 * message but we need to know the frame ID. Try to find it in webRequest's 626 * message but we need to know the frame ID. Try to find it in webRequest's
479 * frame data. 627 * frame data.
480 */ 628 */
481 function getFrameId(tabId, url) 629 function getFrameId(tabId, url)
482 { 630 {
483 if (tabId in frames) 631 if (tabId in frames)
484 { 632 {
485 for (var f in frames[tabId]) 633 for (var f in frames[tabId])
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 refreshIconAndContextMenu(windows[i].tabs[j]); 750 refreshIconAndContextMenu(windows[i].tabs[j]);
603 }); 751 });
604 752
605 // Update icon if a tab changes location 753 // Update icon if a tab changes location
606 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 754 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
607 { 755 {
608 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"}) 756 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"})
609 if(changeInfo.status == "loading") 757 if(changeInfo.status == "loading")
610 refreshIconAndContextMenu(tab); 758 refreshIconAndContextMenu(tab);
611 }); 759 });
760
761 setTimeout(function()
762 {
763 var notificationToShow = Notification.getNextToShow();
764 if (notificationToShow)
765 showNotification(notificationToShow);
766 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « .hgsubstate ('k') | icons/notification-critical.png » ('j') | notification.js » ('J')

Powered by Google App Engine
This is Rietveld