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

Unified Diff: lib/child/contentPolicy.js

Issue 29329336: Issue 3208 - Inject content policy implementation into all processes (Closed)
Patch Set: Created Oct. 22, 2015, 9:03 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/child/contentPolicy.js
===================================================================
copy from lib/contentPolicy.js
copy to lib/child/contentPolicy.js
--- a/lib/contentPolicy.js
+++ b/lib/child/contentPolicy.js
@@ -14,351 +14,110 @@
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @fileOverview Content policy implementation, responsible for blocking things.
*/
-Cu.import("resource://gre/modules/XPCOMUtils.jsm");
-Cu.import("resource://gre/modules/Services.jsm");
+"use strict";
+
+try
+{
+ // Hack: SDK loader masks our Components object with a getter.
+ let proto = Object.getPrototypeOf(this);
+ let property = Object.getOwnPropertyDescriptor(proto, "Components");
+ if (property && property.get)
+ delete proto.Components;
+}
+catch (e)
+{
+ Cu.reportError(e);
+}
+
+let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
+let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
let {Utils} = require("utils");
-let {Prefs} = require("prefs");
-let {FilterStorage} = require("filterStorage");
-let {BlockingFilter, WhitelistFilter, RegExpFilter} = require("filterClasses");
-let {defaultMatcher} = require("matcher");
+let {getWindow, getFrames, isPrivate} = require("child/utils");
let {objectMouseEventHander} = require("objectTabs");
let {RequestNotifier} = require("requestNotifier");
-let {ElemHide} = require("elemHide");
/**
- * List of explicitly supported content types
- * @type string[]
+ * Maps numerical content type IDs to strings.
+ * @type Map
*/
-let contentTypes = ["OTHER", "SCRIPT", "IMAGE", "STYLESHEET", "OBJECT", "SUBDOCUMENT", "DOCUMENT", "XMLHTTPREQUEST", "OBJECT_SUBREQUEST", "FONT", "MEDIA"];
+let types = new Map();
Wladimir Palant 2015/10/22 21:51:12 The original implementation used three maps for tr
/**
- * List of content types that aren't associated with a visual document area
- * @type string[]
+ * Numerical ID for fake ELEMHIDE type.
*/
-let nonVisualTypes = ["SCRIPT", "STYLESHEET", "XMLHTTPREQUEST", "OBJECT_SUBREQUEST", "FONT"];
+const TYPE_ELEMHIDE = 0xFFFD;
+
+/**
+ * Numerical ID for fake POPUP type.
+ */
+const TYPE_POPUP = 0xFFFE;
/**
* Randomly generated class name, to be applied to collapsed nodes.
+ * @type String
*/
-let collapsedClass = "";
+let collapsedClass = null;
/**
- * Public policy checking functions and auxiliary objects
- * @class
+ * Checks whether a request should be blocked, hides the corresponding element
+ * if necessary.
+ * @param window {nsIDOMWindow}
+ * @param node {nsIDOMElement}
+ * @param contentType {Integer}
+ * @param location {String}
+ * @return {Boolean} true if the request should be blocked
*/
-var Policy = exports.Policy =
+function checkRequest(window, node, contentType, location)
Wladimir Palant 2015/10/22 21:51:13 This is mostly equivalent to the old Policy.proces
{
- /**
- * Map of content type identifiers by their name.
- * @type Object
- */
- type: {},
+ let response = sendSyncMessage("AdblockPlus:ShouldLoad", {
+ contentType: types.get(contentType),
+ location: location,
+ frames: getFrames(window),
+ isPrivate: isPrivate(window)
+ });
+ if (response.length == 0)
+ return false;
- /**
- * Map of content type names by their identifiers (reverse of type map).
- * @type Object
- */
- typeDescr: {},
+ let {block, collapse, hits} = JSON.parse(response[0]);
+ for (let {frameIndex, contentType, docDomain, thirdParty, location, match} of hits)
+ {
+ let context = node;
+ if (frameIndex >= 0)
+ {
+ context = window;
+ for (let i = 0; i < frameIndex; i++)
+ context = context.parent;
+ context = context.document;
+ }
+ RequestNotifier.addNodeData(context, window.top, contentType, docDomain, thirdParty, location, match);
+ }
- /**
- * Map of numerical content types with their corresponding masks
- * @type Object
- */
- typeMask: {},
-
- /**
- * Map of localized content type names by their identifiers.
- * @type Object
- */
- localizedDescr: {},
-
- /**
- * Lists the non-visual content types.
- * @type Object
- */
- nonVisual: {},
-
- /**
- * Map containing all schemes that should be ignored by content policy.
- * @type Object
- */
- whitelistSchemes: {},
-
- /**
- * Called on module startup, initializes various exported properties.
- */
- init: function()
+ if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE)
{
- // type constant by type description and type description by type constant
- let iface = Ci.nsIContentPolicy;
- for (let typeName of contentTypes)
+ // Track mouse events for objects
+ if (!block && contentType == Ci.nsIContentPolicy.TYPE_OBJECT)
{
- if ("TYPE_" + typeName in iface)
- {
- let id = iface["TYPE_" + typeName];
- this.type[typeName] = id;
- this.typeDescr[id] = typeName;
- this.localizedDescr[id] = Utils.getString("type_label_" + typeName.toLowerCase());
- this.typeMask[id] = RegExpFilter.typeMap[typeName];
- }
+ node.addEventListener("mouseover", objectMouseEventHander, true);
+ node.addEventListener("mouseout", objectMouseEventHander, true);
}
- this.type.ELEMHIDE = 0xFFFD;
- this.typeDescr[0xFFFD] = "ELEMHIDE";
- this.localizedDescr[0xFFFD] = Utils.getString("type_label_elemhide");
- this.typeMask[0xFFFD] = RegExpFilter.typeMap.ELEMHIDE;
+ if (collapse)
+ schedulePostProcess(node);
+ }
- this.type.POPUP = 0xFFFE;
- this.typeDescr[0xFFFE] = "POPUP";
- this.localizedDescr[0xFFFE] = Utils.getString("type_label_popup");
- this.typeMask[0xFFFE] = RegExpFilter.typeMap.POPUP;
-
- for (let type of nonVisualTypes)
- this.nonVisual[this.type[type]] = true;
-
- // whitelisted URL schemes
- for (let scheme of Prefs.whitelistschemes.toLowerCase().split(" "))
- this.whitelistSchemes[scheme] = true;
-
- // Generate class identifier used to collapse node and register corresponding
- // stylesheet.
- let offset = "a".charCodeAt(0);
- for (let i = 0; i < 20; i++)
- collapsedClass += String.fromCharCode(offset + Math.random() * 26);
-
- let collapseStyle = Services.io.newURI("data:text/css," +
- encodeURIComponent("." + collapsedClass +
- "{-moz-binding: url(chrome://global/content/bindings/general.xml#foobarbazdummy) !important;}"), null, null);
- Utils.styleService.loadAndRegisterSheet(collapseStyle, Ci.nsIStyleSheetService.USER_SHEET);
- onShutdown.add(function()
- {
- Utils.styleService.unregisterSheet(collapseStyle, Ci.nsIStyleSheetService.USER_SHEET);
- });
- },
-
- /**
- * 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;
- let [sitekey, sitekeyWnd] = getSitekey(wnd);
- let nogeneric = false;
-
- if (!match && Prefs.enabled)
- {
- let testWnd = wnd;
- let testSitekey = sitekey;
- let testSitekeyWnd = sitekeyWnd;
- let parentWndLocation = getWindowLocation(testWnd);
- while (true)
- {
- let testWndLocation = parentWndLocation;
- parentWndLocation = (testWnd == testWnd.parent ? testWndLocation : getWindowLocation(testWnd.parent));
- match = Policy.isWhitelisted(testWndLocation, parentWndLocation, testSitekey);
-
- if (match instanceof WhitelistFilter)
- {
- FilterStorage.increaseHitCount(match, wnd);
- RequestNotifier.addNodeData(testWnd.document, topWnd, Policy.type.DOCUMENT, getHostname(parentWndLocation), false, testWndLocation, match);
- return true;
- }
-
- let genericType = (contentType == Policy.type.ELEMHIDE ?
- RegExpFilter.typeMap.GENERICHIDE :
- RegExpFilter.typeMap.GENERICBLOCK);
- let parentDocDomain = getHostname(parentWndLocation);
- let nogenericMatch = defaultMatcher.matchesAny(
- testWndLocation, genericType, parentDocDomain, false, testSitekey
- );
- if (nogenericMatch instanceof WhitelistFilter)
- {
- nogeneric = true;
-
- FilterStorage.increaseHitCount(nogenericMatch, wnd);
- RequestNotifier.addNodeData(testWnd.document, topWnd, contentType,
- parentDocDomain, false, testWndLocation,
- nogenericMatch);
- }
-
- if (testWnd.parent == testWnd)
- break;
-
- if (testWnd == testSitekeyWnd)
- [testSitekey, testSitekeyWnd] = getSitekey(testWnd.parent);
- testWnd = testWnd.parent;
- }
- }
-
- // 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;
-
- let locationText = location.spec;
- if (!match && contentType == Policy.type.ELEMHIDE)
- {
- 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, RegExpFilter.typeMap.ELEMHIDE, parentDocDomain, false, sitekey);
- 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, false, locationText, exception);
- return true;
- }
-
- if (nogeneric && match.isGeneric())
- return true;
- }
-
- let thirdParty = (contentType == Policy.type.ELEMHIDE ? false : isThirdParty(location, docDomain));
-
- if (!match && Prefs.enabled && contentType in Policy.typeMask)
- {
- match = defaultMatcher.matchesAny(locationText, Policy.typeMask[contentType],
- docDomain, thirdParty, sitekey, nogeneric);
- 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);
- }
- }
-
- // Store node data
- RequestNotifier.addNodeData(node, topWnd, contentType, docDomain, thirdParty, locationText, match);
- if (match)
- FilterStorage.increaseHitCount(match, wnd);
-
- return !match || match instanceof WhitelistFilter;
- },
-
- /**
- * Checks whether the location's scheme is blockable.
- * @param location {nsIURI}
- * @return {Boolean}
- */
- isBlockableScheme: function(location)
- {
- return !(location.scheme in Policy.whitelistSchemes);
- },
-
- /**
- * Checks whether a page is whitelisted.
- * @param {String} url
- * @param {String} [parentUrl] location of the parent page
- * @param {String} [sitekey] public key provided on the page
- * @return {Filter} filter that matched the URL or null if not whitelisted
- */
- isWhitelisted: function(url, parentUrl, sitekey)
- {
- if (!url)
- return null;
-
- // Do not apply exception rules to schemes on our whitelistschemes list.
- let match = /^([\w\-]+):/.exec(url);
- if (match && match[1] in Policy.whitelistSchemes)
- return null;
-
- if (!parentUrl)
- parentUrl = url;
-
- // Ignore fragment identifier
- let index = url.indexOf("#");
- if (index >= 0)
- url = url.substring(0, index);
-
- let result = defaultMatcher.matchesAny(url, RegExpFilter.typeMap.DOCUMENT, getHostname(parentUrl), false, sitekey);
- return (result instanceof WhitelistFilter ? result : null);
- },
-
- /**
- * Checks whether the page loaded in a window is whitelisted for indication in the UI.
- * @param wnd {nsIDOMWindow}
- * @return {Filter} matching exception rule or null if not whitelisted
- */
- isWindowWhitelisted: function(wnd)
- {
- return Policy.isWhitelisted(getWindowLocation(wnd));
- },
-
- /**
- * Asynchronously re-checks filters for given nodes.
- * @param {Node[]} nodes
- * @param {RequestEntry} entry
- */
- refilterNodes: function(nodes, entry)
- {
- // Ignore nodes that have been blocked already
- if (entry.filter && !(entry.filter instanceof WhitelistFilter))
- return;
-
- for (let node of nodes)
- Utils.runAsync(() => refilterNode(node, entry));
- }
-};
-Policy.init();
+ return block;
+}
/**
* Actual nsIContentPolicy and nsIChannelEventSink implementation
* @class
*/
var PolicyImplementation =
{
classDescription: "Adblock Plus content policy",
@@ -366,114 +125,90 @@ var PolicyImplementation =
contractID: "@adblockplus.org/abp/policy;1",
xpcom_categories: ["content-policy", "net-channel-event-sinks"],
/**
* Registers the content policy on startup.
*/
init: function()
{
+ // Populate types map
+ let iface = Ci.nsIContentPolicy;
+ for (let name in iface)
+ if (name.indexOf("TYPE_") == 0)
+ types.set(iface[name], name.substr(5));
+ types.set(TYPE_ELEMHIDE, "ELEMHIDE");
+ types.set(TYPE_POPUP, "POPUP");
+
let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
- try
- {
- registrar.registerFactory(this.classID, this.classDescription, this.contractID, this);
- }
- catch (e)
- {
- // See bug 924340 - it might be too early to init now, the old version
- // we are replacing didn't finish removing itself yet.
- if (e.result == Cr.NS_ERROR_FACTORY_EXISTS)
- {
- Utils.runAsync(this.init.bind(this));
- return;
- }
-
- Cu.reportError(e);
- }
+ registrar.registerFactory(this.classID, this.classDescription, this.contractID, this);
Wladimir Palant 2015/10/22 21:51:13 Bug 924340 has been fixed in Firefox 26, bug 75368
let catMan = Utils.categoryManager;
+ let addCategoryEntry = Utils.getPropertyWithoutCompatShims(catMan, "addCategoryEntry");
for (let category of this.xpcom_categories)
- catMan.addCategoryEntry(category, this.contractID, this.contractID, false, true);
+ addCategoryEntry.call(catMan, category, this.contractID, this.contractID, false, true);
- // http-on-opening-request is new in Gecko 18, http-on-modify-request can
- // be used in earlier releases.
- let httpTopic = "http-on-opening-request";
- if (Services.vc.compare(Utils.platformVersion, "18.0") < 0)
- httpTopic = "http-on-modify-request";
+ let addObserver = Utils.getPropertyWithoutCompatShims(Services.obs, "addObserver");
+ addObserver.call(Services.obs, this, "content-document-global-created", true);
- Services.obs.addObserver(this, httpTopic, true);
- Services.obs.addObserver(this, "content-document-global-created", true);
- Services.obs.addObserver(this, "xpcom-category-entry-removed", true);
- Services.obs.addObserver(this, "xpcom-category-cleared", true);
-
- onShutdown.add(function()
+ onShutdown.add(() =>
{
// Our category observers should be removed before changing category
// memberships, just in case.
- Services.obs.removeObserver(this, httpTopic);
- Services.obs.removeObserver(this, "content-document-global-created");
- Services.obs.removeObserver(this, "xpcom-category-entry-removed");
- Services.obs.removeObserver(this, "xpcom-category-cleared");
+ let removeObserver = Utils.getPropertyWithoutCompatShims(Services.obs, "removeObserver");
+ removeObserver.call(Services.obs, this, "content-document-global-created");
for (let category of this.xpcom_categories)
catMan.deleteCategoryEntry(category, this.contractID, false);
- // This needs to run asynchronously, see bug 753687
- Utils.runAsync(function()
- {
- registrar.unregisterFactory(this.classID, this);
- }.bind(this));
+ registrar.unregisterFactory(this.classID, this);
+ });
- this.previousRequest = null;
- }.bind(this));
+ let setCollapsedClass = (message) => {
+ collapsedClass = message.data;
+ removeMessageListener("AdblockPlus:CollapsedClass", setCollapsedClass);
+ };
+ addMessageListener("AdblockPlus:CollapsedClass", setCollapsedClass);
Wladimir Palant 2015/10/22 21:51:12 This class name has to be identical in all process
},
//
// nsISupports interface implementation
//
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIObserver,
Ci.nsIChannelEventSink, Ci.nsIFactory, Ci.nsISupportsWeakReference]),
//
// nsIContentPolicy interface implementation
//
shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTypeGuess, extra)
{
// Ignore requests without context and top-level documents
- if (!node || contentType == Policy.type.DOCUMENT)
+ if (!node || contentType == Ci.nsIContentPolicy.TYPE_DOCUMENT)
return Ci.nsIContentPolicy.ACCEPT;
// Ignore standalone objects
- if (contentType == Policy.type.OBJECT && node.ownerDocument && !/^text\/|[+\/]xml$/.test(node.ownerDocument.contentType))
+ if (contentType == Ci.nsIContentPolicy.TYPE_OBJECT && node.ownerDocument && !/^text\/|[+\/]xml$/.test(node.ownerDocument.contentType))
return Ci.nsIContentPolicy.ACCEPT;
- let wnd = Utils.getWindow(node);
- if (!wnd)
+ let window = getWindow(node);
Wladimir Palant 2015/10/22 21:51:12 I'm not using Utils.getWindow() here for a reason
+ if (!window)
return Ci.nsIContentPolicy.ACCEPT;
- // Ignore whitelisted schemes
- let location = Utils.unwrapURL(contentLocation);
- if (!Policy.isBlockableScheme(location))
- return Ci.nsIContentPolicy.ACCEPT;
+ // Fix type for objects misrepresented as frames or images
+ if (contentType != Ci.nsIContentPolicy.TYPE_OBJECT && (node instanceof Ci.nsIDOMHTMLObjectElement || node instanceof Ci.nsIDOMHTMLEmbedElement))
+ contentType = Ci.nsIContentPolicy.TYPE_OBJECT;
- // Interpret unknown types as "other"
- if (!(contentType in Policy.typeDescr))
- contentType = Policy.type.OTHER;
+ // Data loaded by plugins should be attached to the document
+ if (contentType == Ci.nsIContentPolicy.TYPE_OBJECT_SUBREQUEST && node instanceof Ci.nsIDOMElement)
+ node = node.ownerDocument;
Wladimir Palant 2015/10/22 21:51:13 The checks had to be shuffled around somewhat. Che
- let result = Policy.processNode(wnd, node, contentType, location, false);
- if (result)
- {
- // We didn't block this request so we will probably see it again in
- // http-on-opening-request. Keep it so that we can associate it with the
- // channel there - will be needed in case of redirect.
- this.previousRequest = [location, contentType];
- }
- return (result ? Ci.nsIContentPolicy.ACCEPT : Ci.nsIContentPolicy.REJECT_REQUEST);
+ let block = checkRequest(window, node, contentType, contentLocation.spec);
+ return (block ? Ci.nsIContentPolicy.REJECT_REQUEST : Ci.nsIContentPolicy.ACCEPT);
},
shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode, mimeType, extra)
{
return Ci.nsIContentPolicy.ACCEPT;
},
//
@@ -483,135 +218,80 @@ var PolicyImplementation =
{
switch (topic)
{
case "content-document-global-created":
{
if (!(subject instanceof Ci.nsIDOMWindow) || !subject.opener)
return;
- let uri = additional || Utils.makeURI(subject.location.href);
- if (!Policy.processNode(subject.opener, subject.opener.document, Policy.type.POPUP, uri, false))
+ let uri = additional || subject.location.href;
+ if (checkRequest(subject.opener, subject.opener.document, TYPE_POPUP, uri))
{
subject.stop();
Utils.runAsync(() => subject.close());
}
- else if (uri.spec == "about:blank")
+ else if (uri == "about:blank")
{
// An about:blank pop-up most likely means that a load will be
- // initiated synchronously. Set a flag for our "http-on-opening-request"
- // handler.
- this.expectingPopupLoad = true;
- Utils.runAsync(function()
+ // initiated asynchronously. Wait for that.
+ Utils.runAsync(() =>
{
- this.expectingPopupLoad = false;
+ let channel = subject.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIDocShell)
+ .QueryInterface(Ci.nsIDocumentLoader)
+ .documentChannel;
+ if (channel)
+ this.observe(subject, topic, data, channel.URI.spec);
Wladimir Palant 2015/10/22 21:51:13 This relied on the http-on-opening-request notific
});
}
break;
}
- case "http-on-opening-request":
- case "http-on-modify-request":
- {
- if (!(subject instanceof Ci.nsIHttpChannel))
- return;
-
- if (this.previousRequest && subject.URI == this.previousRequest[0] &&
- subject instanceof Ci.nsIWritablePropertyBag)
- {
- // We just handled a content policy call for this request - associate
- // the data with the channel so that we can find it in case of a redirect.
- subject.setProperty("abpRequestType", this.previousRequest[1]);
- this.previousRequest = null;
- }
-
- if (this.expectingPopupLoad)
- {
- let wnd = Utils.getRequestWindow(subject);
- if (wnd && wnd.opener && wnd.location.href == "about:blank")
- {
- this.observe(wnd, "content-document-global-created", null, subject.URI);
- if (subject instanceof Ci.nsIWritablePropertyBag)
- subject.setProperty("abpRequestType", Policy.type.POPUP);
- }
- }
-
- break;
- }
- case "xpcom-category-entry-removed":
- case "xpcom-category-cleared":
- {
- let category = data;
- if (this.xpcom_categories.indexOf(category) < 0)
- return;
-
- if (topic == "xpcom-category-entry-removed" &&
- subject instanceof Ci.nsISupportsCString &&
- subject.data != this.contractID)
- {
- return;
- }
-
- // Our category entry was removed, make sure to add it back
- let catMan = Utils.categoryManager;
- catMan.addCategoryEntry(category, this.contractID, this.contractID, false, true);
- break;
- }
Wladimir Palant 2015/10/22 21:51:13 The code above was added because of AVG Secure Sea
}
},
//
// nsIChannelEventSink interface implementation
//
asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback)
{
let result = Cr.NS_OK;
try
{
- // Try to retrieve previously stored request data from the channel
- let contentType;
- if (oldChannel instanceof Ci.nsIWritablePropertyBag)
+ // nsILoadInfo.contentPolicyType was introduced in Gecko 35, then
+ // renamed to nsILoadInfo.externalContentPolicyType in Gecko 44.
+ let loadInfo = oldChannel.loadInfo;
+ let contentType = loadInfo.externalContentPolicyType || loadInfo.contentPolicyType;
+ if (!contentType)
+ return;
Wladimir Palant 2015/10/22 21:51:13 Originally we used a complicated and rather fragil
+
+ let wnd = Utils.getRequestWindow(newChannel);
+ if (!wnd)
+ return;
+
+ if (contentType == Ci.nsIContentPolicy.TYPE_DOCUMENT)
{
- try
+ if (wnd.history.length <= 1 && wnd.opener)
Wladimir Palant 2015/10/22 21:51:13 Since we no longer attach our content type to the
{
- contentType = oldChannel.getProperty("abpRequestType");
+ // Special treatment for pop-up windows
+ this.observe(wnd, "content-document-global-created", null);
Wladimir Palant 2015/10/22 21:51:13 This fixes a bug in the original code. It was mere
}
- catch(e)
- {
- // No data attached, ignore this redirect
- return;
- }
+ return;
}
let newLocation = null;
try
{
newLocation = newChannel.URI;
} catch(e2) {}
if (!newLocation)
return;
- let wnd = Utils.getRequestWindow(newChannel);
- if (!wnd)
- return;
-
- if (contentType == Policy.type.SUBDOCUMENT && wnd.parent == wnd.top && wnd.opener)
- {
- // This is a window opened in a new tab miscategorized as frame load,
- // see bug 467514. Get the frame as context to be at least consistent.
- wnd = wnd.opener;
- }
Wladimir Palant 2015/10/22 21:51:12 In bug 467514 I confirmed that the issue is gone i
-
- if (contentType == Policy.type.POPUP && wnd.opener)
- {
- // Popups are initiated by their opener, not their own window.
- wnd = wnd.opener;
- }
-
- if (!Policy.processNode(wnd, wnd.document, contentType, newLocation, false))
+ if (checkRequest(wnd, wnd.document, contentType, newLocation.spec))
result = Cr.NS_BINDING_ABORTED;
}
catch (e)
{
// We shouldn't throw exceptions here - this will prevent the redirect.
Cu.reportError(e);
}
finally
@@ -653,16 +333,22 @@ function schedulePostProcess(/**Element*
}
}
/**
* Processes nodes scheduled for post-processing (typically hides them).
*/
function postProcessNodes()
{
+ if (!collapsedClass)
+ {
+ Utils.runAsync(postProcessNodes);
+ return;
+ }
+
let nodes = scheduledNodes;
scheduledNodes = null;
for (let node of nodes)
{
// adjust frameset's cols/rows for frames
let parentNode = node.parentNode;
if (parentNode && parentNode instanceof Ci.nsIDOMHTMLFrameSetElement)
@@ -681,147 +367,8 @@ function postProcessNodes()
weights[index] = "0";
parentNode[property] = weights.join(",");
}
}
else
node.classList.add(collapsedClass);
}
}
-
-/**
- * Extracts the hostname from a URL (might return null).
- */
-function getHostname(/**String*/ url) /**String*/
-{
- try
- {
- return Utils.unwrapURL(url).host;
- }
- catch(e)
- {
- return null;
- }
-}
-
-/**
- * Retrieves the sitekey of a window.
- */
-function getSitekey(wnd)
-{
- let sitekey = null;
-
- while (true)
- {
- if (wnd.document && wnd.document.documentElement)
- {
- let keydata = wnd.document.documentElement.getAttribute("data-adblockkey");
- if (keydata && keydata.indexOf("_") >= 0)
- {
- let [key, signature] = keydata.split("_", 2);
- key = key.replace(/=/g, "");
-
- // Website specifies a key but is the signature valid?
- let uri = Services.io.newURI(getWindowLocation(wnd), null, null);
- let host = uri.asciiHost;
- if (uri.port > 0)
- host += ":" + uri.port;
- let params = [
- uri.path.replace(/#.*/, ""), // REQUEST_URI
- host, // HTTP_HOST
- Utils.httpProtocol.userAgent // HTTP_USER_AGENT
- ];
- if (Utils.verifySignature(key, signature, params.join("\0")))
- return [key, wnd];
- }
- }
-
- if (wnd === wnd.parent)
- break;
-
- wnd = wnd.parent;
- }
-
- return [sitekey, wnd];
-}
-
-/**
- * Retrieves the location of a window.
- * @param wnd {nsIDOMWindow}
- * @return {String} window location or null on failure
- */
-function getWindowLocation(wnd)
-{
- if ("name" in wnd && wnd.name == "messagepane")
- {
- // Thunderbird branch
- try
- {
- let mailWnd = wnd.QueryInterface(Ci.nsIInterfaceRequestor)
- .getInterface(Ci.nsIWebNavigation)
- .QueryInterface(Ci.nsIDocShellTreeItem)
- .rootTreeItem
- .QueryInterface(Ci.nsIInterfaceRequestor)
- .getInterface(Ci.nsIDOMWindow);
-
- // Typically we get a wrapped mail window here, need to unwrap
- try
- {
- mailWnd = mailWnd.wrappedJSObject;
- } catch(e) {}
-
- if ("currentHeaderData" in mailWnd && "content-base" in mailWnd.currentHeaderData)
- {
- return mailWnd.currentHeaderData["content-base"].headerValue;
- }
- else if ("currentHeaderData" in mailWnd && "from" in mailWnd.currentHeaderData)
- {
- let emailAddress = Utils.headerParser.extractHeaderAddressMailboxes(mailWnd.currentHeaderData.from.headerValue);
- if (emailAddress)
- return 'mailto:' + emailAddress.replace(/^[\s"]+/, "").replace(/[\s"]+$/, "").replace(/\s/g, '%20');
- }
- } catch(e) {}
- }
-
- // Firefox branch
- return wnd.location.href;
-}
-
-/**
- * Checks whether the location's origin is different from document's origin.
- */
-function isThirdParty(/**nsIURI*/location, /**String*/ docDomain) /**Boolean*/
-{
- if (!location || !docDomain)
- return true;
-
- try
- {
- return Utils.effectiveTLD.getBaseDomain(location) != Utils.effectiveTLD.getBaseDomainFromHost(docDomain);
- }
- catch (e)
- {
- // EffectiveTLDService throws on IP addresses, just compare the host name
- let host = "";
- try
- {
- host = location.host;
- } catch (e) {}
- return host != docDomain;
- }
-}
-
-/**
- * Re-checks filters on an element.
- */
-function refilterNode(/**Node*/ node, /**RequestEntry*/ entry)
-{
- let wnd = Utils.getWindow(node);
- if (!wnd || wnd.closed)
- return;
-
- if (entry.type == Policy.type.OBJECT)
- {
- node.removeEventListener("mouseover", objectMouseEventHander, true);
- node.removeEventListener("mouseout", objectMouseEventHander, true);
- }
- Policy.processNode(wnd, node, entry.type, Utils.makeURI(entry.location), true);
-}

Powered by Google App Engine
This is Rietveld