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

Unified Diff: lib/contentPolicy.js

Issue 6201308310667264: Issue 521- Inject our stylesheet on per-site basis rather than globally (Closed)
Patch Set: WIP Created June 4, 2014, 5:55 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
« no previous file with comments | « no previous file | lib/elemHide.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/contentPolicy.js
===================================================================
--- a/lib/contentPolicy.js
+++ b/lib/contentPolicy.js
@@ -25,17 +25,16 @@ Cu.import("resource://gre/modules/Servic
let {TimeLine} = require("timeline");
let {Utils} = require("utils");
let {Prefs} = require("prefs");
let {FilterStorage} = require("filterStorage");
let {BlockingFilter, WhitelistFilter} = require("filterClasses");
let {defaultMatcher} = require("matcher");
let {objectMouseEventHander} = require("objectTabs");
let {RequestNotifier} = require("requestNotifier");
-let {ElemHide} = require("elemHide");
/**
* List of explicitly supported content types
* @type Array of String
*/
let contentTypes = ["OTHER", "SCRIPT", "IMAGE", "STYLESHEET", "OBJECT", "SUBDOCUMENT", "DOCUMENT", "XMLHTTPREQUEST", "OBJECT_SUBREQUEST", "FONT", "MEDIA"];
/**
@@ -138,147 +137,134 @@ let Policy = exports.Policy =
{
Utils.styleService.unregisterSheet(collapseStyle, Ci.nsIStyleSheetService.USER_SHEET);
})
TimeLine.log("done registering stylesheet");
TimeLine.leave("Done initializing content policy");
},
+ processWindow: function(wnd, contentType)
+ {
+ let uri = wnd.document.documentURIObject;
+ if (!uri)
+ return true;
+
+ if (!Policy.isBlockableScheme(uri.scheme))
+ return true;
+
+ let topWnd = wnd.top;
+ if (!topWnd || !topWnd.location || !topWnd.location.href)
+ return true;
+
+ let testWnd = wnd;
+ let parentWndLocation = getWindowLocation(testWnd);
+ while (true)
+ {
+ let testWndLocation = parentWndLocation;
+ parentWndLocation = (testWnd == testWnd.parent ? testWndLocation : getWindowLocation(testWnd.parent));
+
+ let match = Policy.isWhitelisted(testWndLocation, parentWndLocation);
+
+ if (!(match instanceof WhitelistFilter))
tschuster 2014/06/04 18:07:40 I think we should make this match === null, becaus
+ {
+ let keydata = (testWnd.document && testWnd.document.documentElement ? testWnd.document.documentElement.getAttribute("data-adblockkey") : null);
+ if (keydata && keydata.indexOf("_") >= 0)
+ {
+ let [key, signature] = keydata.split("_", 2);
+ let keyMatch = defaultMatcher.matchesByKey(testWndLocation, key.replace(/=/g, ""), docDomain);
+ if (keyMatch && Utils.crypto)
+ {
+ // Website specifies a key that we know but is the signature valid?
+ let uri = Services.io.newURI(testWndLocation, null, null);
+ let params = [
+ uri.path.replace(/#.*/, ""), // REQUEST_URI
+ uri.asciiHost, // HTTP_HOST
+ Utils.httpProtocol.userAgent // HTTP_USER_AGENT
+ ];
+ if (Utils.verifySignature(key, signature, params.join("\0")))
+ match = keyMatch; // XXX what is this?
tschuster 2014/06/04 18:07:40 Okay I am not sure about this case, is that |keyMa
+ }
tschuster 2014/06/05 19:00:34 I have an other concern: This is never going to wo
+ }
+ }
+
+ if (match instanceof WhitelistFilter)
+ {
+ FilterStorage.increaseHitCount(match, wnd);
+ RequestNotifier.addNodeData(testWnd.document, topWnd, Policy.type.DOCUMENT, getHostname(parentWndLocation), false, testWndLocation, match);
+ return true;
+ }
+
+ if (contentType == Policy.type.ELEMHIDE)
+ {
+ let parentDocDomain = getHostname(parentWndLocation);
+ match = defaultMatcher.matchesAny(testWndLocation, "ELEMHIDE", parentDocDomain, false);
+ if (match instanceof WhitelistFilter)
+ {
+ FilterStorage.increaseHitCount(match, wnd);
+ RequestNotifier.addNodeData(testWnd.document, topWnd, contentType, parentDocDomain, false, testWndLocation, match);
+ return true;
+ }
+ }
+
+ if (testWnd.parent == testWnd)
+ break;
+ else
+ testWnd = testWnd.parent;
+ }
+
+ return false;
+ },
+
/**
* Checks whether a node should be blocked, hides it if necessary
* @param wnd {nsIDOMWindow}
* @param node {nsIDOMElement}
* @param contentType {String}
* @param location {nsIURI}
* @param collapse {Boolean} true to force hiding of the node
* @return {Boolean} false if the node should be blocked
*/
processNode: function(wnd, node, contentType, location, collapse)
{
- let topWnd = wnd.top;
- if (!topWnd || !topWnd.location || !topWnd.location.href)
- return true;
-
- let originWindow = Utils.getOriginWindow(wnd);
- let wndLocation = originWindow.location.href;
- let docDomain = getHostname(wndLocation);
- let match = null;
- if (!match && Prefs.enabled)
- {
- let testWnd = wnd;
- let parentWndLocation = getWindowLocation(testWnd);
- while (true)
- {
- let testWndLocation = parentWndLocation;
- parentWndLocation = (testWnd == testWnd.parent ? testWndLocation : getWindowLocation(testWnd.parent));
- match = Policy.isWhitelisted(testWndLocation, parentWndLocation);
-
- if (!(match instanceof WhitelistFilter))
- {
- let keydata = (testWnd.document && testWnd.document.documentElement ? testWnd.document.documentElement.getAttribute("data-adblockkey") : null);
- if (keydata && keydata.indexOf("_") >= 0)
- {
- let [key, signature] = keydata.split("_", 2);
- let keyMatch = defaultMatcher.matchesByKey(testWndLocation, key.replace(/=/g, ""), docDomain);
- if (keyMatch && Utils.crypto)
- {
- // Website specifies a key that we know but is the signature valid?
- let uri = Services.io.newURI(testWndLocation, null, null);
- let params = [
- uri.path.replace(/#.*/, ""), // REQUEST_URI
- uri.asciiHost, // HTTP_HOST
- Utils.httpProtocol.userAgent // HTTP_USER_AGENT
- ];
- if (Utils.verifySignature(key, signature, params.join("\0")))
- match = keyMatch;
- }
- }
- }
-
- if (match instanceof WhitelistFilter)
- {
- FilterStorage.increaseHitCount(match, wnd);
- RequestNotifier.addNodeData(testWnd.document, topWnd, Policy.type.DOCUMENT, getHostname(parentWndLocation), false, testWndLocation, match);
- return true;
- }
-
- if (testWnd.parent == testWnd)
- break;
- else
- testWnd = testWnd.parent;
- }
- }
+ if (contentType == Policy.type.ELEMHIDE)
+ throw new Error("unexpected!");
// Data loaded by plugins should be attached to the document
if (contentType == Policy.type.OBJECT_SUBREQUEST && node instanceof Ci.nsIDOMElement)
node = node.ownerDocument;
// Fix type for objects misrepresented as frames or images
if (contentType != Policy.type.OBJECT && (node instanceof Ci.nsIDOMHTMLObjectElement || node instanceof Ci.nsIDOMHTMLEmbedElement))
contentType = Policy.type.OBJECT;
+ if (Policy.processWindow(wnd, contentType))
+ return true;
+
+ let originWindow = Utils.getOriginWindow(wnd);
+ let wndLocation = originWindow.location.href;
+ let docDomain = getHostname(wndLocation);
+ let topWnd = wnd.top;
+
let locationText = location.spec;
- if (!match && contentType == Policy.type.ELEMHIDE)
+ let thirdParty = isThirdParty(location, docDomain);
+
+ let match = defaultMatcher.matchesAny(locationText, Policy.typeDescr[contentType] || "", docDomain, thirdParty);
+ if (match instanceof BlockingFilter && node.ownerDocument && !(contentType in Policy.nonVisual))
{
- let testWnd = wnd;
- let parentWndLocation = getWindowLocation(testWnd);
- while (true)
- {
- let testWndLocation = parentWndLocation;
- parentWndLocation = (testWnd == testWnd.parent ? testWndLocation : getWindowLocation(testWnd.parent));
- let parentDocDomain = getHostname(parentWndLocation);
- match = defaultMatcher.matchesAny(testWndLocation, "ELEMHIDE", parentDocDomain, false);
- if (match instanceof WhitelistFilter)
- {
- FilterStorage.increaseHitCount(match, wnd);
- RequestNotifier.addNodeData(testWnd.document, topWnd, contentType, parentDocDomain, false, testWndLocation, match);
- return true;
- }
-
- if (testWnd.parent == testWnd)
- break;
- else
- testWnd = testWnd.parent;
- }
-
- match = location;
- locationText = match.text.replace(/^.*?#/, '#');
- location = locationText;
-
- if (!match.isActiveOnDomain(docDomain))
- return true;
-
- let exception = ElemHide.getException(match, docDomain);
- if (exception)
- {
- FilterStorage.increaseHitCount(exception, wnd);
- RequestNotifier.addNodeData(node, topWnd, contentType, docDomain, thirdParty, locationText, exception);
- return true;
- }
+ let prefCollapse = (match.collapse != null ? match.collapse : !Prefs.fastcollapse);
+ if (collapse || prefCollapse)
+ schedulePostProcess(node);
}
- let thirdParty = (contentType == Policy.type.ELEMHIDE ? false : isThirdParty(location, docDomain));
-
- if (!match && Prefs.enabled)
+ // Track mouse events for objects
+ if (!match && contentType == Policy.type.OBJECT && node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE)
{
- match = defaultMatcher.matchesAny(locationText, Policy.typeDescr[contentType] || "", docDomain, thirdParty);
- if (match instanceof BlockingFilter && node.ownerDocument && !(contentType in Policy.nonVisual))
- {
- let prefCollapse = (match.collapse != null ? match.collapse : !Prefs.fastcollapse);
- if (collapse || prefCollapse)
- schedulePostProcess(node);
- }
-
- // Track mouse events for objects
- if (!match && contentType == Policy.type.OBJECT && node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE)
- {
- node.addEventListener("mouseover", objectMouseEventHander, true);
- node.addEventListener("mouseout", objectMouseEventHander, true);
- }
+ node.addEventListener("mouseover", objectMouseEventHander, true);
+ node.addEventListener("mouseout", objectMouseEventHander, true);
}
// Store node data
RequestNotifier.addNodeData(node, topWnd, contentType, docDomain, thirdParty, locationText, match);
if (match)
FilterStorage.increaseHitCount(match, wnd);
return !match || match instanceof WhitelistFilter;
« no previous file with comments | « no previous file | lib/elemHide.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld