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

Unified Diff: lib/elemHide.js

Issue 29329246: Issue 3108 - Inject our about: module into all processes (Closed)
Patch Set: Created Oct. 16, 2015, 12:02 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/elemHide.js
===================================================================
--- a/lib/elemHide.js
+++ b/lib/elemHide.js
@@ -21,17 +21,16 @@
Cu.import("resource://gre/modules/Services.jsm");
let {Utils} = require("utils");
let {IO} = require("io");
let {Prefs} = require("prefs");
let {ElemHideException} = require("filterClasses");
let {FilterNotifier} = require("filterNotifier");
-let {AboutHandler} = require("elemHideHitRegistration");
/**
* Lookup table, filters by their associated key
* @type Object
*/
let filterByKey = Object.create(null);
/**
@@ -76,25 +75,32 @@ let ElemHide = exports.ElemHide =
*/
applied: false,
/**
* Called on module startup.
*/
init: function()
{
+ let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"]
+ .getService(Ci.nsIMessageListenerManager);
+ let hitHandler = (message => {
+ let result = ElemHide.shouldHide(message.data);
+ let target = message.target.QueryInterface(Ci.nsIMessageSender);
+ target.sendAsyncMessage(message.data.responseMessage, result);
+ });
+ messageManager.addMessageListener("AdblockPlus:ElemHideHit", hitHandler);
+ onShutdown.add(() => messageManager.removeMessageListener("AdblockPlus:ElemHideHit", hitHandler));
+
Prefs.addListener(function(name)
{
if (name == "enabled")
ElemHide.apply();
});
- onShutdown.add(function()
- {
- ElemHide.unapply();
- });
+ onShutdown.add(() => ElemHide.unapply());
let styleFile = IO.resolveFilePath(Prefs.data_directory);
styleFile.append("elemhide.css");
styleURL = Services.io.newFileURI(styleFile).QueryInterface(Ci.nsIFileURL);
},
/**
* Removes all known filters
@@ -184,16 +190,64 @@ let ElemHide = exports.ElemHide =
for (let i = list.length - 1; i >= 0; i--)
if (list[i].isActiveOnDomain(docDomain))
return list[i];
return null;
},
/**
+ * Processes an element hiding hit
+ * @param {String} message.key
+ * key of the matching element hiding rule
+ * @param {Object[]} message.frames
+ * information required to reconstruct frame
+ * data for the hit
+ * @return {Boolean}
+ */
+ shouldHide: function(message)
+ {
+ let filter = ElemHide.getFilterByKey(message.key);
+ if (!filter || !message.frames.length)
+ return false;
+
+ let fakeFrame = null;
+ for (let i = message.frames.length - 1; i >= 0; i--)
+ {
+ fakeFrame = {
+ parent: fakeFrame,
+ location: {
+ href: message.frames[i].location
+ },
+ document: {
+ documentElement: {}
+ },
+ QueryInterface: function() {return this;},
+ getInterface: function() {return this;},
+ usePrivateBrowsing: (fakeFrame ? fakeFrame.usePrivateBrowsing : message.frames[i].privateBrowsing)
+ };
+ fakeFrame.top = fakeFrame.parent || fakeFrame;
+ if (!fakeFrame.parent)
+ fakeFrame.parent = fakeFrame;
+
+ let sitekey = message.frames[i].sitekey || null;
+ fakeFrame.document.documentElement.getAttribute = function(attr)
+ {
+ if (attr == "data-adblockkey")
+ return sitekey;
+ else
+ return null;
+ };
+ }
+
+ let {Policy} = require("contentPolicy");
+ return !Policy.processNode(fakeFrame, fakeFrame.document, Policy.type.ELEMHIDE, filter);
Wladimir Palant 2015/10/16 12:15:10 Side-effect here: the hit info is attached to a fa
+ },
+
+ /**
* Will be set to true if apply() is running (reentrance protection).
* @type Boolean
*/
_applying: false,
/**
* Will be set to true if an apply() call arrives while apply() is already
* running (delayed execution).
@@ -308,17 +362,17 @@ let ElemHide = exports.ElemHide =
throw Cr.NS_ERROR_NOT_AVAILABLE;
function escapeChar(match)
{
return "\\" + match.charCodeAt(0).toString(16) + " ";
}
// Return CSS data
- let cssTemplate = "-moz-binding: url(about:" + AboutHandler.aboutPrefix + "?%ID%#dummy) !important;";
+ let cssTemplate = "-moz-binding: url(about:abp-elemhidehit?%ID%#dummy) !important;";
for (let domain in domains)
{
let rules = [];
let list = domains[domain];
if (domain)
yield ('@-moz-document domain("' + domain.split(",").join('"),domain("') + '"){').replace(/[^\x01-\x7F]/g, escapeChar);
else

Powered by Google App Engine
This is Rietveld