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

Side by Side Diff: lib/ui.js

Issue 5538776168267776: Implemented anti-adblock message notification (Closed)
Patch Set: Created Feb. 11, 2014, 4:51 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 | « lib/notification.js ('k') | no next file » | 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 18 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
19 Cu.import("resource://gre/modules/Services.jsm"); 19 Cu.import("resource://gre/modules/Services.jsm");
20 20
21 let {Utils} = require("utils"); 21 let {Utils} = require("utils");
22 let {Prefs} = require("prefs"); 22 let {Prefs} = require("prefs");
23 let {Policy} = require("contentPolicy"); 23 let {Policy} = require("contentPolicy");
24 let {FilterListener} = require("filterListener"); 24 let {FilterListener} = require("filterListener");
25 let {FilterStorage} = require("filterStorage"); 25 let {FilterStorage} = require("filterStorage");
26 let {FilterNotifier} = require("filterNotifier"); 26 let {FilterNotifier} = require("filterNotifier");
27 let {RequestNotifier} = require("requestNotifier"); 27 let {RequestNotifier} = require("requestNotifier");
28 let {Filter} = require("filterClasses"); 28 let {Filter, ActiveFilter} = require("filterClasses");
29 let {Subscription, SpecialSubscription, DownloadableSubscription} = require("sub scriptionClasses"); 29 let {Subscription, SpecialSubscription, DownloadableSubscription} = require("sub scriptionClasses");
30 let {Synchronizer} = require("synchronizer"); 30 let {Synchronizer} = require("synchronizer");
31 let {KeySelector} = require("keySelector"); 31 let {KeySelector} = require("keySelector");
32 let {Notification} = require("notification"); 32 let {Notification} = require("notification");
33 33
34 let CustomizableUI; 34 let CustomizableUI;
35 try 35 try
36 { 36 {
37 ({CustomizableUI}) = Cu.import("resource:///modules/CustomizableUI.jsm", null) ; 37 ({CustomizableUI}) = Cu.import("resource:///modules/CustomizableUI.jsm", null) ;
38 } 38 }
(...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 window.addEventListener("popupshowing", this.onPopupShowing, false); 518 window.addEventListener("popupshowing", this.onPopupShowing, false);
519 window.addEventListener("keypress", this.onKeyPress, false); 519 window.addEventListener("keypress", this.onKeyPress, false);
520 520
521 addBrowserLocationListener(window, function() 521 addBrowserLocationListener(window, function()
522 { 522 {
523 this.updateIconState(window, window.document.getElementById("abp-status")) ; 523 this.updateIconState(window, window.document.getElementById("abp-status")) ;
524 this.updateIconState(window, window.document.getElementById("abp-toolbarbu tton")); 524 this.updateIconState(window, window.document.getElementById("abp-toolbarbu tton"));
525 }.bind(this)); 525 }.bind(this));
526 addBrowserClickListener(window, this.onBrowserClick.bind(this, window)); 526 addBrowserClickListener(window, this.onBrowserClick.bind(this, window));
527 527
528 window.document.getElementById("abp-notification-close").addEventListener("c ommand", function(event)
529 {
530 window.document.getElementById("abp-notification").hidePopup();
531 }, false);
532
533 // Add "anti-adblock messages" notification
Felix Dahlke 2014/02/11 17:57:17 I think all the anti adblock message specific code
Thomas Greiner 2014/02/12 11:58:49 Done.
534 function getURLFiltersFromSubscription(subscription)
535 {
536 let urlFilters = [];
537 for each (let filter in subscription.filters)
538 if (filter instanceof ActiveFilter)
539 for (let domain in filter.domains)
540 if (domain && urlFilters.indexOf(domain) == -1)
541 urlFilters.push(domain);
542 return urlFilters;
543 }
544
545 let antiadblockNotification = {
546 id: "antiadblock",
547 severity: "question",
548 title: Utils.getString("notification_antiadblock_title"),
549 message: Utils.getString("notification_antiadblock_message"),
550 urlFilters: [],
551 onApproved: function()
Felix Dahlke 2014/02/11 17:57:17 I really don't think we should be able to put code
Thomas Greiner 2014/02/12 11:58:49 You cannot include functions in JSON so they can o
Felix Dahlke 2014/02/12 15:09:27 Oh that's right of course, wouldn't work anyway.
552 {
553 let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblocku rl);
554 if (subscription.url in FilterStorage.knownSubscriptions)
555 subscription.disabled = false;
556 },
557 onDeclined: function()
558 {
559 let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblocku rl);
560 if (subscription.url in FilterStorage.knownSubscriptions)
561 subscription.disabled = true;
562 }
563 };
564 let antiadblockSubscription = Subscription.fromURL(Prefs.subscriptions_antia dblockurl);
565 if (antiadblockSubscription.lastDownload && antiadblockSubscription.disabled )
566 {
567 antiadblockNotification.urlFilters = getURLFiltersFromSubscription(antiadb lockSubscription);
568 Notification.addNotification(antiadblockNotification);
569 }
570 FilterNotifier.addListener(function(action, value, newItem, oldItem)
571 {
572 if (!/^subscription\.(updated|removed|disabled)$/.test(action) || value.ur l != Prefs.subscriptions_antiadblockurl)
573 return;
574
575 if (action == "subscription.updated")
576 {
577 antiadblockNotification.urlFilters = getURLFiltersFromSubscription(value );
Felix Dahlke 2014/02/11 17:57:17 This is repeated above. How about having a createA
Thomas Greiner 2014/02/12 11:58:49 Done.
578 Notification.addNotification(antiadblockNotification);
579 }
580 else if (action == "subscription.removed" || (action == "subscription.disa bled" && !value.disabled))
581 {
582 Notification.removeNotification(antiadblockNotification);
583 }
584 });
585
586 var httpRequestObserver = {
Felix Dahlke 2014/02/11 17:57:17 Shouldn't it be called documentCreatingObserver or
Thomas Greiner 2014/02/12 11:58:49 Done.
587 observe: function(subject, topic, data)
588 {
589 if (!(subject instanceof Ci.nsIDOMWindow))
590 return;
591
592 this._showNextNotification(subject.location.href);
593 }.bind(this)
594 };
595 Services.obs.addObserver(httpRequestObserver, "content-document-global-creat ed", false);
596 onShutdown.add(function()
597 {
598 Services.obs.removeObserver(httpRequestObserver, "content-document-global- created", false);
599 });
600
528 // First-run actions? 601 // First-run actions?
529 if (!this.firstRunDone) 602 if (!this.firstRunDone)
530 { 603 {
531 this.firstRunDone = true; 604 this.firstRunDone = true;
532 605
533 let {addonVersion} = require("info"); 606 let {addonVersion} = require("info");
534 let prevVersion = Prefs.currentVersion; 607 let prevVersion = Prefs.currentVersion;
535 if (prevVersion != addonVersion) 608 if (prevVersion != addonVersion)
536 { 609 {
537 Prefs.currentVersion = addonVersion; 610 Prefs.currentVersion = addonVersion;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
763 { 836 {
764 subscription.title = "Allow non-intrusive advertising"; 837 subscription.title = "Allow non-intrusive advertising";
765 FilterStorage.addSubscription(subscription); 838 FilterStorage.addSubscription(subscription);
766 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload) 839 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload)
767 Synchronizer.execute(subscription); 840 Synchronizer.execute(subscription);
768 } 841 }
769 else 842 else
770 addAcceptable = false; 843 addAcceptable = false;
771 } 844 }
772 845
846 // Add "anti-adblock messages" subscription for new users and users updating from old ABP versions.
847 if (Services.vc.compare(prevVersion, "2.5") < 0)
Felix Dahlke 2014/02/11 17:57:17 I think we should do this for everyone, no matter
Thomas Greiner 2014/02/12 11:58:49 Done.
848 {
849 let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl );
850 if (subscription)
851 {
852 subscription.disabled = true;
853 FilterStorage.addSubscription(subscription);
854 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload)
855 Synchronizer.execute(subscription);
856 }
857 }
858
773 if (!addSubscription && !addAcceptable) 859 if (!addSubscription && !addAcceptable)
774 return; 860 return;
775 861
776 function notifyUser() 862 function notifyUser()
777 { 863 {
778 let {addTab} = require("appSupport"); 864 let {addTab} = require("appSupport");
779 if (addTab) 865 if (addTab)
780 { 866 {
781 addTab(window, "chrome://adblockplus/content/ui/firstRun.html"); 867 addTab(window, "chrome://adblockplus/content/ui/firstRun.html");
782 } 868 }
(...skipping 975 matching lines...) Expand 10 before | Expand all | Expand 10 after
1758 Prefs.hideContributeButton = true; 1844 Prefs.hideContributeButton = true;
1759 1845
1760 for each (let id in ["abp-status-contributebutton", "abp-toolbar-contributeb utton", "abp-menuitem-contributebutton"]) 1846 for each (let id in ["abp-status-contributebutton", "abp-toolbar-contributeb utton", "abp-menuitem-contributebutton"])
1761 { 1847 {
1762 let button = window.document.getElementById(id); 1848 let button = window.document.getElementById(id);
1763 if (button) 1849 if (button)
1764 button.hidden = true; 1850 button.hidden = true;
1765 } 1851 }
1766 }, 1852 },
1767 1853
1768 _showNextNotification: function() 1854 _showNextNotification: function(url)
1769 { 1855 {
1770 let window = this.currentWindow; 1856 let window = this.currentWindow;
1771 if (!window) 1857 if (!window)
1772 return; 1858 return;
1773 1859
1774 let button = window.document.getElementById("abp-toolbarbutton") 1860 let button = window.document.getElementById("abp-toolbarbutton")
1775 || window.document.getElementById("abp-status"); 1861 || window.document.getElementById("abp-status");
1776 if (!button) 1862 if (!button)
1777 return; 1863 return;
1778 1864
1779 let notification = Notification.getNextToShow(); 1865 let notification = Notification.getNextToShow(url);
1780 if (!notification) 1866 if (!notification)
1781 return; 1867 return;
1782 1868
1783 this._showNotification(window, button, notification); 1869 this._showNotification(window, button, notification);
1784 }, 1870 },
1785 1871
1786 _showNotification: function(window, button, notification) 1872 _showNotification: function(window, button, notification)
1787 { 1873 {
1874 let panel = window.document.getElementById("abp-notification");
1875 if (panel.state !== "closed")
1876 return;
1877
1788 function insertMessage(element, text, links) 1878 function insertMessage(element, text, links)
1789 { 1879 {
1790 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text); 1880 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text);
1791 if (!match) 1881 if (!match)
1792 { 1882 {
1793 element.appendChild(window.document.createTextNode(text)); 1883 element.appendChild(window.document.createTextNode(text));
1794 return; 1884 return;
1795 } 1885 }
1796 1886
1797 let [_, before, tagName, value, after] = match; 1887 let [_, before, tagName, value, after] = match;
1798 1888
1799 insertMessage(element, before, links); 1889 insertMessage(element, before, links);
1800 1890
1801 let newElement = window.document.createElementNS("http://www.w3.org/1999/x html", tagName); 1891 let newElement = window.document.createElementNS("http://www.w3.org/1999/x html", tagName);
1802 if (tagName === "a" && links && links.length) 1892 if (tagName === "a" && links && links.length)
1803 newElement.setAttribute("href", links.shift()); 1893 newElement.setAttribute("href", links.shift());
1804 insertMessage(newElement, value, links); 1894 insertMessage(newElement, value, links);
1805 element.appendChild(newElement); 1895 element.appendChild(newElement);
1806 1896
1807 insertMessage(element, after, links); 1897 insertMessage(element, after, links);
1808 } 1898 }
1809 1899
1810 let texts = Notification.getLocalizedTexts(notification); 1900 let texts = Notification.getLocalizedTexts(notification);
1811 let titleElement = window.document.getElementById("abp-notification-title"); 1901 let titleElement = window.document.getElementById("abp-notification-title");
1812 titleElement.setAttribute("value", texts.title); 1902 titleElement.setAttribute("value", texts.title);
1813 let messageElement = window.document.getElementById("abp-notification-messag e"); 1903 let messageElement = window.document.getElementById("abp-notification-messag e");
1904 messageElement.innerHTML = "";
1814 let docLinks = []; 1905 let docLinks = [];
1815 for each (let link in notification.links) 1906 for each (let link in notification.links)
1816 docLinks.push(Utils.getDocLink(link)); 1907 docLinks.push(Utils.getDocLink(link));
1817 insertMessage(messageElement, texts.message, docLinks); 1908 insertMessage(messageElement, texts.message, docLinks);
1818 1909
1819 messageElement.addEventListener("click", function(event) 1910 messageElement.addEventListener("click", function(event)
1820 { 1911 {
1821 let link = event.target; 1912 let link = event.target;
1822 while (link && link !== messageElement && link.localName !== "a") 1913 while (!link || link === messageElement || link.localName !== "a")
Felix Dahlke 2014/02/11 17:57:17 Shouldn't this be an "if"? Anyway, why aren't we
Thomas Greiner 2014/02/12 11:58:49 Done. The bubbling lead to the link opening no ma
1823 link = link.parentNode;
1824 if (!link)
1825 return; 1914 return;
1826 event.preventDefault(); 1915 event.preventDefault();
1827 event.stopPropagation(); 1916 event.stopPropagation();
1828 this.loadInBrowser(link.href, window); 1917 this.loadInBrowser(link.href, window);
1829 }.bind(this)); 1918 }.bind(this));
1830 1919
1831 let panel = window.document.getElementById("abp-notification"); 1920 if (notification.severity === "question")
1921 {
1922 function buttonHandler(event)
1923 {
1924 event.preventDefault();
1925 event.stopPropagation();
1926 panel.hidePopup();
1927
1928 let action = notification[event.target.getAttribute("action")];
1929 if (typeof action === "function")
1930 action();
1931 Notification.markAsShown(notification.id);
1932 }
1933 window.document.getElementById("abp-notification-yes").oncommand = buttonH andler;
1934 window.document.getElementById("abp-notification-no").oncommand = buttonHa ndler;
1935 }
1936
1937 panel.setAttribute("class", "abp-" + notification.severity);
1938 panel.setAttribute("noautohide", notification.severity === "question");
1832 panel.openPopup(button, "bottomcenter topcenter", 0, 0, false, false, null); 1939 panel.openPopup(button, "bottomcenter topcenter", 0, 0, false, false, null);
1833 } 1940 }
1834 }; 1941 };
1835 UI.onPopupShowing = UI.onPopupShowing.bind(UI); 1942 UI.onPopupShowing = UI.onPopupShowing.bind(UI);
1836 UI.onKeyPress = UI.onKeyPress.bind(UI); 1943 UI.onKeyPress = UI.onKeyPress.bind(UI);
1837 UI.onIconClick = UI.onIconClick.bind(UI); 1944 UI.onIconClick = UI.onIconClick.bind(UI);
1838 UI.init(); 1945 UI.init();
1839 1946
1840 /** 1947 /**
1841 * List of event handers to be registered for each window. For each event 1948 * List of event handers to be registered for each window. For each event
(...skipping 16 matching lines...) Expand all
1858 ["abp-command-contribute", "command", UI.openContributePage.bind(UI)], 1965 ["abp-command-contribute", "command", UI.openContributePage.bind(UI)],
1859 ["abp-command-contribute-hide", "command", UI.hideContributeButton.bind(UI)] 1966 ["abp-command-contribute-hide", "command", UI.hideContributeButton.bind(UI)]
1860 ]; 1967 ];
1861 1968
1862 onShutdown.add(function() 1969 onShutdown.add(function()
1863 { 1970 {
1864 for (let window in UI.applicationWindows) 1971 for (let window in UI.applicationWindows)
1865 if (UI.isBottombarOpen(window)) 1972 if (UI.isBottombarOpen(window))
1866 UI.toggleBottombar(window); 1973 UI.toggleBottombar(window);
1867 }); 1974 });
OLDNEW
« no previous file with comments | « lib/notification.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld