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: Address remaining issue Created July 23, 2013, 11:24 a.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 | « .hgsubstate ('k') | icons/notification-critical.png » ('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 <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 startIconAnimation(tab, iconFilename);
Wladimir Palant 2013/07/23 11:53:17 Don't we need to check whether this is actually th
Felix Dahlke 2013/07/23 12:52:02 I don't think it's necessary, since we only refres
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
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
466 chrome.tabs.onUpdated.removeListener(listener); 473 chrome.tabs.onUpdated.removeListener(listener);
467 onLoad(); 474 onLoad();
468 } 475 }
469 }; 476 };
470 chrome.tabs.onUpdated.addListener(listener); 477 chrome.tabs.onUpdated.addListener(listener);
471 } 478 }
472 }); 479 });
473 } 480 }
474 } 481 }
475 482
483 var iconAnimationTimer = null;
484 var animatedIconTab = null;
485
486 function stopIconAnimation()
487 {
488 if (!iconAnimationTimer)
489 return;
490
491 clearTimeout(iconAnimationTimer);
492 iconAnimationTimer = null;
493 animatedIconTab = null;
Wladimir Palant 2013/07/23 11:53:17 refreshIconAndContextMenu(animatedIconTab)? Well,
Felix Dahlke 2013/07/23 12:52:02 Not necessary, no. I used to do that here, but rem
494 }
495
496 function loadImages(imageFiles, callback)
497 {
498 var images = {};
499 var imagesLoaded = 0;
500 imageFiles.forEach(function(imageFile)
501 {
502 var image = new Image();
503 image.src = imageFile;
504 image.addEventListener("load", function()
505 {
506 images[imageFile] = image;
507 if (++imagesLoaded === imageFiles.length)
508 callback(images);
509 });
510 });
511 }
512
513 function startIconAnimation(tab, iconPath)
514 {
515 stopIconAnimation();
516 animatedIconTab = tab;
517
518 var severitySuffix = activeNotification.severity === "critical"
519 ? "critical" : "information";
520 var notificationIconPath = "icons/notification-" + severitySuffix + ".png";
521 var iconFiles = [iconPath, notificationIconPath];
522 loadImages(iconFiles, function(images)
523 {
524 var icon = images[iconPath];
525 var notificationIcon = images[notificationIconPath];
526
527 var canvas = document.createElement("canvas");
528 canvas.width = icon.width;
529 canvas.height = icon.height;
530 var context = canvas.getContext("2d");
531
532 var currentFrame = 0;
533 var frameOpacities = [0, 0.2, 0.4, 0.6, 0.8,
534 1, 1, 1, 1, 1,
535 0.8, 0.6, 0.4, 0.2, 0];
536
537 function animationStep()
538 {
539 var opacity = frameOpacities[currentFrame];
540 context.clearRect(0, 0, canvas.width, canvas.height);
541 context.globalAlpha = 1;
542 context.drawImage(icon, 0, 0);
543 context.globalAlpha = opacity;
544 context.drawImage(notificationIcon, 0, 0);
545 var imageData = context.getImageData(0, 0, canvas.width, canvas.height);
546 chrome.pageAction.setIcon({tabId: tab.id, imageData: imageData});
547
548 var interval;
549 currentFrame++;
550 if (currentFrame < frameOpacities.length)
551 {
552 var duration = 3000;
553 interval = duration / frameOpacities.length;
554 }
555 else
556 {
557 currentFrame = 0;
558 interval = 10000;
559 }
560 iconAnimationTimer = setTimeout(animationStep, interval);
561 }
562 animationStep();
563 });
564 }
565
566 function prepareNotificationIconAndPopup()
567 {
568 function refreshAll(tabs)
569 {
570 tabs.forEach(refreshIconAndContextMenu);
571 }
572
573 activeNotification.onClicked = function()
574 {
575 stopIconAnimation();
576 activeNotification = null;
577 chrome.tabs.query(null, refreshAll);
Wladimir Palant 2013/07/23 11:53:17 Do we still need to refresh all icons here? if (a
Felix Dahlke 2013/07/23 12:52:02 Done. You're right, it's not necessary anymore now
578 };
579
580 chrome.windows.getLastFocused({populate: true}, function(window)
581 {
582 chrome.tabs.query({active: true, windowId: window.id}, refreshAll);
583 });
584 }
585
586 function showNotification(notification)
587 {
588 activeNotification = notification;
589
590 if (activeNotification.severity === "critical")
591 {
592 var notification = webkitNotifications.createHTMLNotification("notification. html");
593 notification.show();
594 notification.addEventListener("close", prepareNotificationIconAndPopup);
595 }
596 else
597 prepareNotificationIconAndPopup();
598 }
599
476 /** 600 /**
477 * This function is a hack - we only know the tabId and document URL for a 601 * 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 602 * message but we need to know the frame ID. Try to find it in webRequest's
479 * frame data. 603 * frame data.
480 */ 604 */
481 function getFrameId(tabId, url) 605 function getFrameId(tabId, url)
482 { 606 {
483 if (tabId in frames) 607 if (tabId in frames)
484 { 608 {
485 for (var f in frames[tabId]) 609 for (var f in frames[tabId])
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
602 refreshIconAndContextMenu(windows[i].tabs[j]); 726 refreshIconAndContextMenu(windows[i].tabs[j]);
603 }); 727 });
604 728
605 // Update icon if a tab changes location 729 // Update icon if a tab changes location
606 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) 730 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
607 { 731 {
608 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"}) 732 chrome.tabs.sendRequest(tabId, {reqtype: "clickhide-deactivate"})
609 if(changeInfo.status == "loading") 733 if(changeInfo.status == "loading")
610 refreshIconAndContextMenu(tab); 734 refreshIconAndContextMenu(tab);
611 }); 735 });
736
737 // Refresh icon when switching tabs or windows
738 chrome.tabs.onActivated.addListener(function(activeInfo)
739 {
740 refreshIconAndContextMenu(animatedIconTab);
741 chrome.tabs.get(activeInfo.tabId, function(tab)
742 {
743 refreshIconAndContextMenu(tab);
744 });
Wladimir Palant 2013/07/23 11:53:17 How about: chrome.tabs.get(activeInfo.tabId, refr
Felix Dahlke 2013/07/23 12:52:02 Done.
745 });
746 chrome.windows.onFocusChanged.addListener(function(windowId)
747 {
748 refreshIconAndContextMenu(animatedIconTab);
749 chrome.tabs.query({active: true, windowId: windowId}, function(tabs)
750 {
751 refreshIconAndContextMenu(tabs[0]);
Wladimir Palant 2013/07/23 11:53:17 How about dropping the assumption that there is on
Felix Dahlke 2013/07/23 12:52:02 This should really only be one, but yeah, forEach
752 });
753 });
754
755 setTimeout(function()
756 {
757 var notificationToShow = Notification.getNextToShow();
758 if (notificationToShow)
759 showNotification(notificationToShow);
760 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « .hgsubstate ('k') | icons/notification-critical.png » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld