[Pkg-mozext-commits] [adblock-plus] 17/52: Issue 1663 - Made first-run page use an asynchronous messaging protocol

David Prévot taffit at moszumanska.debian.org
Thu Jan 22 21:43:45 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus.

commit 2bbe49083ed110492f08f5004f671f1454edb6f2
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Thu Dec 18 20:30:22 2014 +0100

    Issue 1663 - Made first-run page use an asynchronous messaging protocol
    
    --HG--
    extra : amend_source : 5f9c16b676c664acd1d93d5d0004d811ce8d96e5
    extra : histedit_source : 25421fad111ffeb4f0794380c584ca3f897f1d4b
---
 README.md      |  56 +++++++++++++++++++
 ext/content.js | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 firstRun.js    | 167 +++++++++++++++++++++++++++++++++------------------------
 3 files changed, 307 insertions(+), 70 deletions(-)

diff --git a/README.md b/README.md
new file mode 100644
index 0000000..18716d4
--- /dev/null
+++ b/README.md
@@ -0,0 +1,56 @@
+Shared Adblock Plus UI code
+===========================
+
+The user interface elements defined in this repository will be used by various
+Adblock Plus products like Adblock Plus for Firefox. Their functionality can be
+tested within this repository, even though they might not work exactly the same
+as they will do in the final product.
+
+Directory structure
+-------------------
+
+* Top-level files: HTML pages and scripts meant to be imported into the
+  respective products.
+* `locale` directory: Localized strings, with one directory per locale. The
+  Firefox format for locale identifiers is used (xx-YY where xx is the language
+  code and YY the optional region code). The localization strings themselves are
+  stored in the JSON format, like the one used by Chrome extensions. There is
+  one JSON file per HTML page, file names of HTML page and JSON file should
+  match.
+* `skin` directory: CSS files and any additional resources (images and fonts)
+  required for these.
+* `ext` directory: Test implementation of the abstraction layer. This one should
+  *not to be imported*, these files should rather be replaced by
+  product-specific versions providing the same interface.
+
+Testing
+-------
+
+In Firefox and Safari the HTML pages can be opened directly from the file system
+and should be fully functional. Due to security restrictions in Chrome, there
+you need to pass in the `--allow-file-access-from-files` command line flag when
+starting the application. Alternatively, you can run `test_server.py` (requires
+Python 2.7) and open the HTML pages under URLs like
+`http://127.0.0.1:5000/firstRun.html`.
+
+Various aspects of the pages can be tested by setting parameters in the URL. The
+only universal parameter is `locale`, e.g. `?locale=es-AR`. This parameter
+overrides browser's locale which will be used by default.
+
+firstRun.html
+-------------
+
+This is the implementation of the Adblock Plus first-run page that will show up
+whenever changes are applied automatically to user's Adblock Plus configuration.
+This will usually happen when the user first installs Adblock Plus (initial
+setup), but it can also happen in case the user's settings get lost.
+
+The behavior of this page is affected by a number of URL parameters:
+
+* `platform`, `platformVersion`, `application`, `applicationVersion`: sets
+  application parameters that are normally determined by Adblock Plus. Using
+  `?platform=safari&platformVersion=5.0` should trigger a warning.
+* `seenDataCorruption`, `filterlistsReinitialized`: setting these parameters to
+  `true` should trigger warnings referring to issues detected by Adblock Plus.
+* `blockedURLs`: a comma-separated list of URLs that should be considered
+  blocked (necessary to test the check for blocked scripts in sharing buttons).
diff --git a/ext/content.js b/ext/content.js
new file mode 100644
index 0000000..a62f0c5
--- /dev/null
+++ b/ext/content.js
@@ -0,0 +1,154 @@
+/*
+ * This file is part of Adblock Plus <http://adblockplus.org/>,
+ * Copyright (C) 2006-2014 Eyeo GmbH
+ *
+ * Adblock Plus is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 3 as
+ * published by the Free Software Foundation.
+ *
+ * Adblock Plus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Adblock Plus.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+(function(global)
+{
+  if (!global.ext)
+    global.ext = {};
+
+  function updateFromURL(data)
+  {
+    if (window.location.search)
+    {
+      var params = window.location.search.substr(1).split("&");
+      for (var i = 0; i < params.length; i++)
+      {
+        var parts = params[i].split("=", 2);
+        if (parts.length == 2 && parts[0] in data)
+          data[parts[0]] = decodeURIComponent(parts[1]);
+      }
+    }
+  }
+
+  var subscriptions =[
+    "https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt",
+    "https://easylist-downloads.adblockplus.org/exceptionrules.txt",
+    "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
+  ];
+
+  var listenerFilter = null;
+
+  global.ext.backgroundPage = {
+    sendMessage: function(message, responseCallback)
+    {
+      var respond = function(response)
+      {
+        setTimeout(responseCallback.bind(responseCallback, response), 0);
+      };
+
+      var dispatchListenerNotification = function(action)
+      {
+        var match = /^subscription\.(.*)/.exec(action);
+        if (match && listenerFilter && listenerFilter.indexOf(match[1]) >= 0)
+        {
+          global.ext.onMessage._dispatch({
+            type: "subscriptions.listen",
+            action: match[1],
+            args: Array.prototype.slice.call(arguments, 1)
+          });
+        }
+      };
+
+      switch (message.type)
+      {
+        case "app.get":
+          if (message.what == "issues")
+          {
+            var response = {seenDataCorruption: false, filterlistsReinitialized: false};
+            updateFromURL(response);
+
+            var info = {platform: "gecko", platformVersion: "34.0", application: "firefox", applicationVersion: "34.0"};
+            updateFromURL(info);
+            response.legacySafariVersion = (info.platform == "safari" && (
+              parseInt(info.platformVersion, 10) < 6 ||  // beforeload breaks websites in Safari 5
+              info.platformVersion == "6.1" ||           // extensions are broken in 6.1 and 7.0
+              info.platformVersion == "7.0"));
+
+            respond(response);
+          }
+          else if (message.what == "doclink")
+            respond("https://adblockplus.org/redirect?link=" + encodeURIComponent(message.link));
+          else
+            respond(null);
+          break;
+        case "app.open":
+          if (message.what == "options")
+            window.open("http://example.com/options.html", "_blank");
+          break;
+        case "subscriptions.get":
+          respond(subscriptions);
+          break;
+        case "filters.blocked":
+          var params = {blockedURLs: ""};
+          updateFromURL(params);
+          var blocked = params.blockedURLs.split(",");
+          respond(blocked.indexOf(message.url) >= 0);
+          break;
+        case "subscriptions.toggle":
+          var index = subscriptions.indexOf(message.url);
+          if (index >= 0)
+          {
+            subscriptions.splice(index, 1);
+            dispatchListenerNotification("subscription.removed", message.url);
+          }
+          else
+          {
+            subscriptions.push(message.url);
+            dispatchListenerNotification("subscription.added", message.url);
+          }
+          break;
+        case "subscriptions.listen":
+          listenerFilter = message.filter;
+          break;
+      }
+    }
+  };
+
+  var EventTarget = function(cancelable)
+  {
+    this._listeners = [];
+    this._cancelable = cancelable;
+  };
+  EventTarget.prototype = {
+    addListener: function(listener)
+    {
+      if (this._listeners.indexOf(listener) == -1)
+        this._listeners.push(listener);
+    },
+    removeListener: function(listener)
+    {
+      var idx = this._listeners.indexOf(listener);
+      if (idx != -1)
+        this._listeners.splice(idx, 1);
+    },
+    _dispatch: function()
+    {
+      var result = null;
+
+      for (var i = 0; i < this._listeners.length; i++)
+      {
+        result = this._listeners[i].apply(null, arguments);
+
+        if (this._cancelable && result === false)
+          break;
+      }
+
+      return result;
+    }
+  };
+  global.ext.onMessage = new EventTarget();
+})(this);
diff --git a/firstRun.js b/firstRun.js
index 4956565..1819805 100644
--- a/firstRun.js
+++ b/firstRun.js
@@ -41,92 +41,96 @@
     }
   ];
 
+  function getDocLink(link, callback)
+  {
+    ext.backgroundPage.sendMessage({
+      type: "app.get",
+      what: "doclink",
+      link: link
+    }, callback);
+  }
+
   function onDOMLoaded()
   {
     // Set up URLs
-    var donateLink = E("donate");
-    donateLink.href = Utils.getDocLink("donate");
+    getDocLink("donate", function(link)
+    {
+      E("donate").href = link;
+    });
 
-    var contributors = E("contributors");
-    contributors.href = Utils.getDocLink("contributors");
+    getDocLink("contributors", function(link)
+    {
+      E("contributors").href = link;
+    });
 
-    setLinks("acceptableAdsExplanation", Utils.getDocLink("acceptable_ads_criteria"), openFilters);
-    setLinks("share-headline", Utils.getDocLink("contribute"));
+    getDocLink("acceptable_ads_criteria", function(link)
+    {
+      setLinks("acceptableAdsExplanation", link, openFilters);
+    });
 
-    if (typeof backgroundPage != "undefined")
+    getDocLink("contribute", function(link)
+    {
+      setLinks("share-headline", link);
+    });
+
+    ext.backgroundPage.sendMessage({
+      type: "app.get",
+      what: "issues"
+    }, function(issues)
     {
       // Show warning if data corruption was detected
-      if (backgroundPage.seenDataCorruption)
+      if (issues.seenDataCorruption)
       {
         E("dataCorruptionWarning").removeAttribute("hidden");
-        setLinks("dataCorruptionWarning", Utils.getDocLink("knownIssuesChrome_filterstorage"));
+        getDocLink("knownIssuesChrome_filterstorage", function(link)
+        {
+          setLinks("dataCorruptionWarning", link);
+        });
       }
 
       // Show warning if filterlists settings were reinitialized
-      if (backgroundPage.filterlistsReinitialized)
+      if (issues.filterlistsReinitialized)
       {
         E("filterlistsReinitializedWarning").removeAttribute("hidden");
         setLinks("filterlistsReinitializedWarning", openFilters);
       }
-    }
 
-    // Show warning if Safari version isn't supported
-    var info = require("info");
-    if (info.platform == "safari" && (
-      Services.vc.compare(info.platformVersion, "6.0")  < 0 ||  // beforeload breaks websites in Safari 5
-      Services.vc.compare(info.platformVersion, "6.1") == 0 ||  // extensions are broken in 6.1 and 7.0
-      Services.vc.compare(info.platformVersion, "7.0") == 0
-    ))
-      E("legacySafariWarning").removeAttribute("hidden");
+      if (issues.legacySafariVersion)
+        E("legacySafariWarning").removeAttribute("hidden");
+    });
 
     // Set up feature buttons linked to subscriptions
-    featureSubscriptions.forEach(setToggleSubscriptionButton);
-    var filterListener = function(action)
+    featureSubscriptions.forEach(initToggleSubscriptionButton);
+    updateToggleButtons();
+    updateSocialLinks();
+
+    ext.onMessage.addListener(function(message)
     {
-      if (/^subscription\.(added|removed|disabled)$/.test(action))
+      if (message.type == "subscriptions.listen")
       {
-        for (var i = 0; i < featureSubscriptions.length; i++)
-        {
-          var featureSubscription = featureSubscriptions[i];
-          updateToggleButton(featureSubscription.feature, isSubscriptionEnabled(featureSubscription));
-        }
+        updateToggleButtons();
+        updateSocialLinks();
       }
-    }
-    FilterNotifier.addListener(filterListener);
-    window.addEventListener("unload", function(event)
-    {
-      FilterNotifier.removeListener(filterListener);
-    }, false);
-
-    initSocialLinks();
-  }
-
-  function isSubscriptionEnabled(featureSubscription)
-  {
-    return featureSubscription.url in FilterStorage.knownSubscriptions
-      && !Subscription.fromURL(featureSubscription.url).disabled;
+    });
+    ext.backgroundPage.sendMessage({
+      type: "subscriptions.listen",
+      filter: ["added", "removed", "disabled"]
+    });
   }
 
-  function setToggleSubscriptionButton(featureSubscription)
+  function initToggleSubscriptionButton(featureSubscription)
   {
     var feature = featureSubscription.feature;
 
     var element = E("toggle-" + feature);
-    updateToggleButton(feature, isSubscriptionEnabled(featureSubscription));
     element.addEventListener("click", function(event)
     {
-      var subscription = Subscription.fromURL(featureSubscription.url);
-      if (isSubscriptionEnabled(featureSubscription))
-        FilterStorage.removeSubscription(subscription);
-      else
-      {
-        subscription.disabled = false;
-        subscription.title = featureSubscription.title;
-        subscription.homepage = featureSubscription.homepage;
-        FilterStorage.addSubscription(subscription);
-        if (!subscription.lastDownload)
-          Synchronizer.execute(subscription);
-      }
+      ext.backgroundPage.sendMessage({
+        type: "subscriptions.toggle",
+        url: featureSubscription.url,
+        title: featureSubscription.title,
+        homepage: featureSubscription.homepage
+      });
     }, false);
   }
 
@@ -138,8 +142,7 @@
 
     var popupMessageListener = function(event)
     {
-      var originFilter = Filter.fromText("||adblockplus.org^");
-      if (!originFilter.matches(event.origin, "OTHER", null, null))
+      if (!/[.\/]adblockplus\.org$/.test(event.origin))
         return;
 
       var width = event.data.width;
@@ -181,25 +184,38 @@
     glassPane.className = "visible";
   }
 
-  function initSocialLinks()
+  function updateSocialLinks()
   {
     var networks = ["twitter", "facebook", "gplus"];
     networks.forEach(function(network)
     {
       var link = E("share-" + network);
-      link.addEventListener("click", onSocialLinkClick, false);
+      var message = {
+        type: "filters.blocked",
+        url: link.getAttribute("data-script"),
+        requestType: "SCRIPT",
+        docDomain: "adblockplus.org",
+        thirdParty: true
+      };
+      ext.backgroundPage.sendMessage(message, function(blocked)
+      {
+        // Don't open the share page if the sharing script would be blocked
+        if (blocked)
+          link.removeEventListener("click", onSocialLinkClick, false);
+        else
+          link.addEventListener("click", onSocialLinkClick, false);
+      });
     });
   }
 
   function onSocialLinkClick(event)
   {
-    // Don't open the share page if the sharing script would be blocked
-    var filter = defaultMatcher.matchesAny(event.target.getAttribute("data-script"), "SCRIPT", "adblockplus.org", true);
-    if (!(filter instanceof BlockingFilter))
+    event.preventDefault();
+
+    getDocLink(event.target.id, function(link)
     {
-      event.preventDefault();
-      openSharePopup(Utils.getDocLink(event.target.id));
-    }
+      openSharePopup(link);
+    });
   }
 
   function setLinks(id)
@@ -229,12 +245,23 @@
 
   function openFilters()
   {
-    if (typeof UI != "undefined")
-      UI.openFiltersDialog();
-    else
+    ext.backgroundPage.sendMessage({type: "app.open", what: "options"});
+  }
+
+  function updateToggleButtons()
+  {
+    ext.backgroundPage.sendMessage({
+      type: "subscriptions.get",
+      downloadable: true,
+      ignoreDisabled: true
+    }, function(subscriptions)
     {
-      backgroundPage.openOptions();
-    }
+      for (var i = 0; i < featureSubscriptions.length; i++)
+      {
+        var featureSubscription = featureSubscriptions[i];
+        updateToggleButton(featureSubscription.feature, subscriptions.indexOf(featureSubscription.url) >= 0);
+      }
+    });
   }
 
   function updateToggleButton(feature, isEnabled)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git



More information about the Pkg-mozext-commits mailing list