[Pkg-mozext-commits] [adblock-plus] 17/98: Issue 521 - Inject our stylesheet on per-site basis rather than globally

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 1eebb50ad19f62082f3b25e277dbe1ac660a3e44
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Fri Jun 17 15:05:45 2016 +0100

    Issue 521 - Inject our stylesheet on per-site basis rather than globally
---
 lib/child/elemHide.js     |  62 +++++++++++++++++++++++++++
 lib/child/main.js         |   2 +-
 lib/elemHideFF.js         |  50 ++++++++++++++++++++++
 lib/elemHideStylesheet.js | 107 ----------------------------------------------
 lib/main.js               |   2 +-
 5 files changed, 114 insertions(+), 109 deletions(-)

diff --git a/lib/child/elemHide.js b/lib/child/elemHide.js
index d14a76a..036f4d8 100644
--- a/lib/child/elemHide.js
+++ b/lib/child/elemHide.js
@@ -33,8 +33,11 @@ catch (e)
 }
 
 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
+let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
 
 let {shouldAllowAsync} = require("child/contentPolicy");
+let {getFrames, isPrivate} = require("child/utils");
+let {RequestNotifier} = require("child/requestNotifier");
 let {port} = require("messaging");
 let {Utils} = require("utils");
 
@@ -293,3 +296,62 @@ HitRegistrationChannel.prototype = {
     });
   }
 };
+
+let observer = {
+  QueryInterface: XPCOMUtils.generateQI([
+    Ci.nsIObserver, Ci.nsISupportsWeakReference
+  ]),
+
+  topic: "content-document-global-created",
+  styleURL: Utils.makeURI("about:abp-elemhide?css"),
+  sheet: null,
+
+  init: function()
+  {
+    Services.obs.addObserver(this, this.topic, true);
+    onShutdown.add(() =>
+    {
+      Services.obs.removeObserver(this, this.topic);
+    });
+
+    port.on("elemhideupdate", () =>
+    {
+      this.sheet = null;
+    });
+  },
+
+  observe: function(subject, topic, data)
+  {
+    if (topic != this.topic)
+      return;
+
+    port.emitWithResponse("elemhideEnabled", {
+      frames: getFrames(subject),
+      isPrivate: isPrivate(subject)
+    }).then(({
+      enabled, contentType, docDomain, thirdParty, location, filter,
+      filterType
+    }) =>
+    {
+      if (enabled)
+      {
+        if (!this.sheet)
+        {
+          this.sheet = Utils.styleService.preloadSheet(this.styleURL,
+              Ci.nsIStyleSheetService.USER_SHEET);
+        }
+
+        let utils = subject.QueryInterface(Ci.nsIInterfaceRequestor)
+                           .getInterface(Ci.nsIDOMWindowUtils);
+        utils.addSheet(this.sheet, Ci.nsIStyleSheetService.USER_SHEET);
+      }
+      else if (filter)
+      {
+        RequestNotifier.addNodeData(subject.document, subject.top, {
+          contentType, docDomain, thirdParty, location, filter, filterType
+        });
+      }
+    });
+  }
+};
+observer.init();
diff --git a/lib/child/main.js b/lib/child/main.js
index 6d8871a..f751db3 100644
--- a/lib/child/main.js
+++ b/lib/child/main.js
@@ -28,4 +28,4 @@ port.emitWithResponse("ping").then(() =>
   require("child/dataCollector");
   require("child/cssProperties");
   require("child/subscribeLinks");
-});
+}).catch(e => Cu.reportError(e));
diff --git a/lib/elemHideFF.js b/lib/elemHideFF.js
new file mode 100644
index 0000000..692379a
--- /dev/null
+++ b/lib/elemHideFF.js
@@ -0,0 +1,50 @@
+/*
+ * 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 {FilterStorage} = require("filterStorage");
+let {Prefs} = require("prefs");
+let {Policy} = require("contentPolicy");
+
+FilterNotifier.on("elemhideupdate", () => port.emit("elemhideupdate"));
+
+port.on("getSelectors", () => ElemHide.getSelectors());
+
+port.on("elemhideEnabled", ({frames, isPrivate}) =>
+{
+  if (!Prefs.enabled)
+    return {enabled: false};
+
+  let hit = Policy.isFrameWhitelisted(frames, true);
+  if (hit)
+  {
+    let [frameIndex, contentType, docDomain, thirdParty, location, filter] = hit;
+    if (!isPrivate)
+      FilterStorage.increaseHitCount(filter);
+    return {
+      enabled: false,
+      contentType, docDomain, thirdParty, location,
+      filter: filter.text, filterType: filter.type
+    };
+  }
+  else
+    return {enabled: true};
+});
diff --git a/lib/elemHideStylesheet.js b/lib/elemHideStylesheet.js
deleted file mode 100644
index 07b39a2..0000000
--- a/lib/elemHideStylesheet.js
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * 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-elemhide?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 c1c3f28..8738375 100644
--- a/lib/main.js
+++ b/lib/main.js
@@ -32,7 +32,7 @@ require("sync");
 require("messageResponder");
 require("ui");
 require("objectTabs");
-require("elemHideStylesheet");
+require("elemHideFF");
 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