[Pkg-mozext-commits] [adblock-plus] 14/98: Issue 4139 - Don't save element hiding filters on disk

David Prévot taffit at moszumanska.debian.org
Tue Oct 24 01:30:13 UTC 2017


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

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

commit bf16507d7f8649ea61cf0412f26a1d06544e80bf
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Wed Jun 8 22:25:17 2016 +0200

    Issue 4139 - Don't save element hiding filters on disk
---
 dependencies              |   2 +-
 lib/child/elemHide.js     | 192 ++++++++++++++++++++++++++++++++++++----------
 lib/elemHideStylesheet.js | 107 ++++++++++++++++++++++++++
 lib/main.js               |   1 +
 4 files changed, 262 insertions(+), 40 deletions(-)

diff --git a/dependencies b/dependencies
index 2719a0e..d2c93f8 100644
--- a/dependencies
+++ b/dependencies
@@ -1,5 +1,5 @@
 _root = hg:https://hg.adblockplus.org/ git:https://github.com/adblockplus/
 _self = buildtools/ensure_dependencies.py
 buildtools = buildtools hg:d865256754db git:4454e6b
-adblockpluscore = adblockpluscore hg:3f1bdb2a4fd4 git:e929b89
+adblockpluscore = adblockpluscore hg:f08c7cc52b2d git:74d6248
 adblockplusui = adblockplusui hg:75534a4a1e0e git:31583ee
diff --git a/lib/child/elemHide.js b/lib/child/elemHide.js
index cffb12e..b29181c 100644
--- a/lib/child/elemHide.js
+++ b/lib/child/elemHide.js
@@ -35,6 +35,7 @@ catch (e)
 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
 
 let {shouldAllowAsync} = require("child/contentPolicy");
+let {port} = require("messaging");
 let {Utils} = require("utils");
 
 // The allowXBL binding below won't have any effect on the element. For elements
@@ -43,6 +44,8 @@ let {Utils} = require("utils");
 const allowXBL = "<bindings xmlns='http://www.mozilla.org/xbl'><binding id='dummy' bindToUntrustedContent='true'/></bindings>";
 const hideXBL = "<bindings xmlns='http://www.mozilla.org/xbl'/>";
 
+const notImplemented = () => Cr.NS_ERROR_NOT_IMPLEMENTED;
+
 /**
  * about: URL module used to count hits.
  * @class
@@ -90,11 +93,14 @@ let AboutHandler =
 
   newChannel: function(uri, loadInfo)
   {
-    let match = /\?(\d+)/.exec(uri.path);
+    let match = /\?(\d+|css)$/.exec(uri.path);
     if (!match)
       throw Cr.NS_ERROR_FAILURE;
 
-    return new HitRegistrationChannel(uri, loadInfo, match[1]);
+    if (match[1] == "css")
+      return new StyleDataChannel(uri, loadInfo);
+    else
+      return new HitRegistrationChannel(uri, loadInfo, match[1]);
   },
 
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory, Ci.nsIAboutModule])
@@ -102,22 +108,21 @@ let AboutHandler =
 AboutHandler.init();
 
 /**
- * Channel returning data for element hiding hits.
+ * Base class for channel implementations, subclasses usually only need to
+ * override BaseChannel._getResponse() method.
  * @constructor
  */
-function HitRegistrationChannel(uri, loadInfo, key)
+function BaseChannel(uri, loadInfo)
 {
-  this.key = key;
   this.URI = this.originalURI = uri;
   this.loadInfo = loadInfo;
 }
-HitRegistrationChannel.prototype = {
-  key: null,
+BaseChannel.prototype = {
   URI: null,
   originalURI: null,
   contentCharset: "utf-8",
   contentLength: 0,
-  contentType: "text/xml",
+  contentType: null,
   owner: Utils.systemPrincipal,
   securityInfo: null,
   notificationCallbacks: null,
@@ -126,56 +131,165 @@ HitRegistrationChannel.prototype = {
   name: null,
   status: Cr.NS_OK,
 
-  asyncOpen: function(listener, context)
+  _getResponse: notImplemented,
+
+  _checkSecurity: function()
   {
-    let processResponse = (allow) =>
+    if (!this.loadInfo.triggeringPrincipal.equals(Utils.systemPrincipal))
+      throw Cr.NS_ERROR_FAILURE;
+  },
+
+  asyncOpen: function()
+  {
+    Promise.resolve(this._getResponse()).then(data =>
     {
-      let data = (allow ? allowXBL : hideXBL);
-      let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
+      let stream = Cc["@mozilla.org/io/string-input-stream;1"]
+                     .createInstance(Ci.nsIStringInputStream);
       stream.setData(data, data.length);
 
-      try {
+      try
+      {
         listener.onStartRequest(this, context);
-      } catch(e) {}
-      try {
+      }
+      catch(e)
+      {
+        // Listener failing isn't our problem
+      }
+
+      try
+      {
         listener.onDataAvailable(this, context, stream, 0, stream.available());
-      } catch(e) {}
-      try {
-        listener.onStopRequest(this, context, Cr.NS_OK);
-      } catch(e) {}
-    };
+      }
+      catch(e)
+      {
+        // Listener failing isn't our problem
+      }
 
-    let window = Utils.getRequestWindow(this);
-    shouldAllowAsync(window, window.document, "ELEMHIDE", this.key, processResponse);
+      try
+      {
+        listener.onStopRequest(this, context, Cr.NS_OK);
+      }
+      catch(e)
+      {
+        // Listener failing isn't our problem
+      }
+    });
   },
 
   asyncOpen2: function(listener)
   {
-    if (!this.loadInfo.triggeringPrincipal.equals(Utils.systemPrincipal))
-      throw Cr.NS_ERROR_FAILURE;
+    this._checkSecurity();
     this.asyncOpen(listener, null);
   },
 
   open: function()
   {
-    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
-  },
-  isPending: function()
-  {
-    return false;
-  },
-  cancel: function()
-  {
-    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
-  },
-  suspend: function()
-  {
-    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+    let data = this._getResponse();
+    if (typeof data.then == "function")
+      throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+
+    let stream = Cc["@mozilla.org/io/string-input-stream;1"]
+                   .createInstance(Ci.nsIStringInputStream);
+    stream.setData(data, data.length);
+    return stream;
   },
-  resume: function()
+
+  open2: function()
   {
-    throw Cr.NS_ERROR_NOT_IMPLEMENTED;
+    this._checkSecurity();
+    return this.open();
   },
 
+  isPending: () => false,
+  cancel: notImplemented,
+  suspend: notImplemented,
+  resume: notImplemented,
+
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIChannel, Ci.nsIRequest])
 };
+
+/**
+ * Channel returning CSS data for the global stylesheet.
+ * @constructor
+ */
+function StyleDataChannel(uri, loadInfo)
+{
+  BaseChannel.call(this, uri, loadInfo);
+}
+StyleDataChannel.prototype = {
+  __proto__: BaseChannel.prototype,
+  contentType: "text/css",
+
+  _getResponse: function()
+  {
+    function escapeChar(match)
+    {
+      return "\\" + match.charCodeAt(0).toString(16) + " ";
+    }
+
+    // Would be great to avoid sync messaging here but nsIStyleSheetService
+    // insists on opening channels synchronously.
+    let domains = port.emitSync("getSelectors");
+
+    let cssPrefix = "{-moz-binding: url(about:abp-elemhidehit?";
+    let cssSuffix = "#dummy) !important;}\n";
+    let result = [];
+
+    for (let [domain, selectors] of domains)
+    {
+      if (domain)
+      {
+        result.push('@-moz-document domain("',
+            domain.replace(/[^\x01-\x7F]/g, escapeChar)
+                  .split(",").join('"),domain("'),
+            '"){\n');
+      }
+      else
+      {
+        // Only allow unqualified rules on a few protocols to prevent them
+        // from blocking chrome content
+        result.push('@-moz-document url-prefix("http://"),',
+            'url-prefix("https://"),url-prefix("mailbox://"),',
+            'url-prefix("imap://"),url-prefix("news://"),',
+            'url-prefix("snews://"){\n');
+      }
+
+      for (let [selector, key] of selectors)
+      {
+        result.push(selector.replace(/[^\x01-\x7F]/g, escapeChar),
+            cssPrefix, key, cssSuffix);
+      }
+
+      result.push("}\n");
+    }
+
+    return result.join("");
+  }
+};
+
+/**
+ * Channel returning data for element hiding hits.
+ * @constructor
+ */
+function HitRegistrationChannel(uri, loadInfo, key)
+{
+  BaseChannel.call(this, uri, loadInfo);
+  this.key = key;
+}
+HitRegistrationChannel.prototype = {
+  __proto__: BaseChannel.prototype,
+  key: null,
+  contentType: "text/xml",
+
+  _getResponse: function()
+  {
+    return new Promise((resolve, reject) =>
+    {
+      let window = Utils.getRequestWindow(this);
+      shouldAllowAsync(window, window.document, "ELEMHIDE", this.key, allow =>
+      {
+        resolve(allow ? allowXBL : hideXBL);
+      });
+    });
+  }
+};
diff --git a/lib/elemHideStylesheet.js b/lib/elemHideStylesheet.js
new file mode 100644
index 0000000..26ba639
--- /dev/null
+++ b/lib/elemHideStylesheet.js
@@ -0,0 +1,107 @@
+/*
+ * This file is part of Adblock Plus <https://adblockplus.org/>,
+ * Copyright (C) 2006-2016 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/>.
+ */
+
+"use strict";
+
+let {port} = require("messaging");
+let {ElemHide} = require("elemHide");
+let {FilterNotifier} = require("filterNotifier");
+let {Prefs} = require("prefs");
+let {Utils} = require("utils");
+
+/**
+ * Indicates whether the element hiding stylesheet is currently applied.
+ * @type Boolean
+ */
+let applied = false;
+
+/**
+ * Stylesheet URL to be registered
+ * @type nsIURI
+ */
+let styleURL = Utils.makeURI("about:abp-elemhidehit?css");
+
+function init()
+{
+  port.on("getSelectors", () => ElemHide.getSelectors());
+
+  apply();
+  onShutdown.add(unapply);
+
+  Prefs.addListener(function(name)
+  {
+    if (name == "enabled")
+      apply();
+  });
+
+  FilterNotifier.on("elemhideupdate", onUpdate);
+}
+
+function onUpdate()
+{
+  // Call apply() asynchronously and protect against reentrance - multiple
+  // change events shouldn't result in multiple calls.
+  if (onUpdate.inProgress)
+    return;
+
+  onUpdate.inProgress = true;
+  Utils.runAsync(() =>
+  {
+    onUpdate.inProgress = false;
+    apply();
+  });
+}
+
+function apply()
+{
+  unapply();
+
+  if (!Prefs.enabled)
+    return;
+
+  try
+  {
+    Utils.styleService.loadAndRegisterSheet(styleURL,
+        Ci.nsIStyleSheetService.USER_SHEET);
+    applied = true;
+  }
+  catch (e)
+  {
+    Cu.reportError(e);
+  }
+}
+
+function unapply()
+{
+  if (applied)
+  {
+    try
+    {
+      Utils.styleService.unregisterSheet(styleURL,
+          Ci.nsIStyleSheetService.USER_SHEET);
+    }
+    catch (e)
+    {
+      Cu.reportError(e);
+    }
+    applied = false;
+  }
+}
+
+// Send dummy message before initializing, this delay makes sure that the child
+// modules are loaded and our protocol handler registered.
+port.emitWithResponse("ping").then(init);
diff --git a/lib/main.js b/lib/main.js
index dadca3a..c1c3f28 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -32,6 +32,7 @@ require("sync");
 require("messageResponder");
 require("ui");
 require("objectTabs");
+require("elemHideStylesheet");
 require("cssProperties");
 
 function bootstrapChildProcesses()

-- 
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