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. 12, 2014, 11:55 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
« lib/notification.js ('K') | « 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 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 391
392 if (this.overlay.all.length) 392 if (this.overlay.all.length)
393 this.overlay.all[0].appendChild(menuSource); 393 this.overlay.all[0].appendChild(menuSource);
394 if ("abp-toolbarbutton" in this.overlay) 394 if ("abp-toolbarbutton" in this.overlay)
395 this.overlay["abp-toolbarbutton"].appendChild(fixId(menuSource.cloneNode (true), "abp-toolbar")); 395 this.overlay["abp-toolbarbutton"].appendChild(fixId(menuSource.cloneNode (true), "abp-toolbar"));
396 if ("abp-menuitem" in this.overlay) 396 if ("abp-menuitem" in this.overlay)
397 this.overlay["abp-menuitem"].appendChild(fixId(menuSource.cloneNode(true ), "abp-menuitem")); 397 this.overlay["abp-menuitem"].appendChild(fixId(menuSource.cloneNode(true ), "abp-menuitem"));
398 } 398 }
399 }, 399 },
400 400
401 initAntiAdblockNotification: function()
Felix Dahlke 2014/02/12 15:09:27 Shouldn't this be "private"?
Thomas Greiner 2014/02/12 18:28:02 Done.
402 {
403 let notification = {
Felix Dahlke 2014/02/12 15:09:27 I think we should move this into addAntiAdblockNot
Thomas Greiner 2014/02/12 18:28:02 No, it's also used to remove the notification (see
Felix Dahlke 2014/02/13 10:22:04 Oh, right.
404 id: "antiadblock",
405 severity: "question",
406 title: Utils.getString("notification_antiadblock_title"),
407 message: Utils.getString("notification_antiadblock_message"),
408 urlFilters: []
409 };
410
411 function addAntiAdblockNotification(subscription)
412 {
413 let urlFilters = [];
414 for each (let filter in subscription.filters)
415 if (filter instanceof ActiveFilter)
416 for (let domain in filter.domains)
417 if (domain && urlFilters.indexOf(domain) == -1)
418 urlFilters.push(domain);
419 notification.urlFilters = urlFilters;
420 Notification.addNotification(notification);
421 }
422
423 let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
424 if (subscription.lastDownload && subscription.disabled)
425 {
426 addAntiAdblockNotification(subscription);
427 }
428
429 function notificationListener(id, approved)
430 {
431 if (id !== notification.id)
432 return;
433
434 let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl );
435 if (subscription.url in FilterStorage.knownSubscriptions)
436 subscription.disabled = !approved;
437 }
438 Notification.addListener(notificationListener);
439
440 FilterNotifier.addListener(function(action, value, newItem, oldItem)
441 {
442 if (!/^subscription\.(updated|removed|disabled)$/.test(action) || value.ur l != Prefs.subscriptions_antiadblockurl)
443 return;
444
445 if (action == "subscription.updated")
446 addAntiAdblockNotification(value);
447 else if (action == "subscription.removed" || (action == "subscription.disa bled" && !value.disabled))
448 {
449 Notification.removeNotification(notification);
450 }
451 });
452
453 var documentCreationObserver = {
454 observe: function(subject, topic, data)
455 {
456 if (!(subject instanceof Ci.nsIDOMWindow))
457 return;
458
459 this._showNextNotification(subject.location.href);
460 }.bind(this)
461 };
462 Services.obs.addObserver(documentCreationObserver, "content-document-global- created", false);
463 onShutdown.add(function()
464 {
465 Services.obs.removeObserver(documentCreationObserver, "content-document-gl obal-created", false);
466 });
467 },
468
401 /** 469 /**
402 * Gets called once the initialization is finished and Adblock Plus elements 470 * Gets called once the initialization is finished and Adblock Plus elements
403 * can be added to the UI. 471 * can be added to the UI.
404 */ 472 */
405 initDone: function() 473 initDone: function()
406 { 474 {
407 let {WindowObserver} = require("windowObserver"); 475 let {WindowObserver} = require("windowObserver");
408 new WindowObserver(this); 476 new WindowObserver(this);
409 477
410 // Add toolbar icon 478 // Add toolbar icon
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 window.addEventListener("popupshowing", this.onPopupShowing, false); 586 window.addEventListener("popupshowing", this.onPopupShowing, false);
519 window.addEventListener("keypress", this.onKeyPress, false); 587 window.addEventListener("keypress", this.onKeyPress, false);
520 588
521 addBrowserLocationListener(window, function() 589 addBrowserLocationListener(window, function()
522 { 590 {
523 this.updateIconState(window, window.document.getElementById("abp-status")) ; 591 this.updateIconState(window, window.document.getElementById("abp-status")) ;
524 this.updateIconState(window, window.document.getElementById("abp-toolbarbu tton")); 592 this.updateIconState(window, window.document.getElementById("abp-toolbarbu tton"));
525 }.bind(this)); 593 }.bind(this));
526 addBrowserClickListener(window, this.onBrowserClick.bind(this, window)); 594 addBrowserClickListener(window, this.onBrowserClick.bind(this, window));
527 595
596 window.document.getElementById("abp-notification-close").addEventListener("c ommand", function(event)
597 {
598 window.document.getElementById("abp-notification").hidePopup();
599 }, false);
600
601 // Add "anti-adblock messages" notification
602 this.initAntiAdblockNotification();
603
528 // First-run actions? 604 // First-run actions?
529 if (!this.firstRunDone) 605 if (!this.firstRunDone)
530 { 606 {
531 this.firstRunDone = true; 607 this.firstRunDone = true;
532 608
533 let {addonVersion} = require("info"); 609 let {addonVersion} = require("info");
534 let prevVersion = Prefs.currentVersion; 610 let prevVersion = Prefs.currentVersion;
535 if (prevVersion != addonVersion) 611 if (prevVersion != addonVersion)
536 { 612 {
537 Prefs.currentVersion = addonVersion; 613 Prefs.currentVersion = addonVersion;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
763 { 839 {
764 subscription.title = "Allow non-intrusive advertising"; 840 subscription.title = "Allow non-intrusive advertising";
765 FilterStorage.addSubscription(subscription); 841 FilterStorage.addSubscription(subscription);
766 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload) 842 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload)
767 Synchronizer.execute(subscription); 843 Synchronizer.execute(subscription);
768 } 844 }
769 else 845 else
770 addAcceptable = false; 846 addAcceptable = false;
771 } 847 }
772 848
849 // Add "anti-adblock messages" subscription
850 let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
851 if (subscription)
852 {
853 subscription.disabled = true;
854 FilterStorage.addSubscription(subscription);
855 if (subscription instanceof DownloadableSubscription && !subscription.last Download)
856 Synchronizer.execute(subscription);
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")
1823 link = link.parentNode; 1914 link = link.parentNode;
1824 if (!link) 1915 if (!link || link.localName !== "a")
1825 return; 1916 return;
1826 event.preventDefault(); 1917 event.preventDefault();
1827 event.stopPropagation(); 1918 event.stopPropagation();
1828 this.loadInBrowser(link.href, window); 1919 this.loadInBrowser(link.href, window);
1829 }.bind(this)); 1920 }.bind(this));
1830 1921
1831 let panel = window.document.getElementById("abp-notification"); 1922 if (notification.severity === "question")
1923 {
1924 function buttonHandler(approved, event)
1925 {
1926 event.preventDefault();
1927 event.stopPropagation();
1928 panel.hidePopup();
1929 Notification.triggerListeners(notification.id, approved)
1930 Notification.markAsShown(notification.id);
1931 }
1932 window.document.getElementById("abp-notification-yes").onclick = buttonHan dler.bind(null, true);
1933 window.document.getElementById("abp-notification-no").onclick = buttonHand ler.bind(null, false);
1934 }
1935
1936 panel.setAttribute("class", "abp-" + notification.severity);
1937 panel.setAttribute("noautohide", notification.severity === "question");
1832 panel.openPopup(button, "bottomcenter topcenter", 0, 0, false, false, null); 1938 panel.openPopup(button, "bottomcenter topcenter", 0, 0, false, false, null);
1833 } 1939 }
1834 }; 1940 };
1835 UI.onPopupShowing = UI.onPopupShowing.bind(UI); 1941 UI.onPopupShowing = UI.onPopupShowing.bind(UI);
1836 UI.onKeyPress = UI.onKeyPress.bind(UI); 1942 UI.onKeyPress = UI.onKeyPress.bind(UI);
1837 UI.onIconClick = UI.onIconClick.bind(UI); 1943 UI.onIconClick = UI.onIconClick.bind(UI);
1838 UI.init(); 1944 UI.init();
1839 1945
1840 /** 1946 /**
1841 * List of event handers to be registered for each window. For each event 1947 * 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)], 1964 ["abp-command-contribute", "command", UI.openContributePage.bind(UI)],
1859 ["abp-command-contribute-hide", "command", UI.hideContributeButton.bind(UI)] 1965 ["abp-command-contribute-hide", "command", UI.hideContributeButton.bind(UI)]
1860 ]; 1966 ];
1861 1967
1862 onShutdown.add(function() 1968 onShutdown.add(function()
1863 { 1969 {
1864 for (let window in UI.applicationWindows) 1970 for (let window in UI.applicationWindows)
1865 if (UI.isBottombarOpen(window)) 1971 if (UI.isBottombarOpen(window))
1866 UI.toggleBottombar(window); 1972 UI.toggleBottombar(window);
1867 }); 1973 });
OLDNEW
« lib/notification.js ('K') | « lib/notification.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld