From 6177295e1d9a9ebba5acc68df4800d8557b22cab Mon Sep 17 00:00:00 2001
From: Einar Egilsson
Date: Fri, 16 Oct 2009 07:23:54 +0000
Subject: Before using XPCOMUtils
git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@269 119bf307-c92d-0410-89bd-8f53e6181181
---
chrome/content/code/redirector.prototype.js | 62 +++++++++++++++++++++++++----
chrome/content/log.html | 7 +++-
components/redirector.component.js | 35 ++++++++++------
3 files changed, 84 insertions(+), 20 deletions(-)
diff --git a/chrome/content/code/redirector.prototype.js b/chrome/content/code/redirector.prototype.js
index b69a52f..979e9f7 100644
--- a/chrome/content/code/redirector.prototype.js
+++ b/chrome/content/code/redirector.prototype.js
@@ -70,6 +70,7 @@ Redirector.prototype = {
// nsIContentPolicy interface implementation
shouldLoad: function(contentType, contentLocation, requestOrigin, aContext, mimeTypeGuess, extra) {
try {
+
if (!this.prefs.enabled) {
return nsIContentPolicy.ACCEPT;
}
@@ -90,7 +91,6 @@ Redirector.prototype = {
var url = contentLocation.spec;
for each (var redirect in this.list) {
- //this.debug(redirect);
var result = redirect.getMatch(url);
if (result.isExcludeMatch) {
this.debug(url + ' matched exclude pattern ' + redirect.excludePattern + ' so the redirect ' + redirect.includePattern + ' will not be used');
@@ -114,6 +114,60 @@ Redirector.prototype = {
return nsIContentPolicy.ACCEPT;
},
+ //nsIChannelEventSink interface implementation
+ onChannelRedirect: function(oldChannel, newChannel, flags)
+ {
+ dump("****************** HI THERE ****************************");
+ //throw Cr.NS_BASE_STREAM_WOULD_BLOCK;
+/*
+ try {
+ let oldLocation = null;
+ let newLocation = null;
+ try {
+ oldLocation = oldChannel.originalURI.spec;
+ newLocation = newChannel.URI.spec;
+ }
+ catch(e2) {}
+
+ if (!oldLocation || !newLocation || oldLocation == newLocation)
+ return;
+
+ // Look for the request both in the origin window and in its parent (for frames)
+ let contexts = [getRequestWindow(newChannel)];
+ if (!contexts[0])
+ contexts.pop();
+ else if (contexts[0] && contexts[0].parent != contexts[0])
+ contexts.push(contexts[0].parent);
+
+ let info = null;
+ for each (let context in contexts)
+ {
+ // Did we record the original request in its own window?
+ let data = RequestList.getDataForWindow(context, true);
+ if (data)
+ info = data.getURLInfo(oldLocation);
+
+ if (info)
+ {
+ let nodes = info.nodes;
+ let node = (nodes.length > 0 ? nodes[nodes.length - 1] : context.document);
+
+ // HACK: NS_BINDING_ABORTED would be proper error code to throw but this will show up in error console (bug 287107)
+ if (!this.processNode(context, node, info.type, newChannel.URI))
+ throw Cr.NS_BASE_STREAM_WOULD_BLOCK;
+ else
+ return;
+ }
+ }
+ }
+ catch (e if (e != Cr.NS_BASE_STREAM_WOULD_BLOCK))
+ {
+ // We shouldn't throw exceptions here - this will prevent the redirect.
+ dump("Adblock Plus: Unexpected error in policy.onChannelRedirect: " + e + "\n");
+ }
+ */
+ },
+
reload : function() {
loader.loadSubScript('chrome://redirector/content/code/redirector.prototype.js');
loader.loadSubScript('chrome://redirector/content/code/redirect.js');
@@ -185,12 +239,6 @@ Redirector.prototype = {
containsRedirect : function(redirect) {
for each (var existing in this.list) {
- this.debug("EXAMPLEURL: " + (redirect.exampleUrl == existing.exampleUrl));
- this.debug("INCLUDEPATTERN: " + (redirect.includePattern == existing.includePattern));
- this.debug("EXCLUDEPATTERN: " + (redirect.excludePattern == existing.excludePattern));
- this.debug("unescape: " + (redirect.unescapeMatches == existing.unescapeMatches));
- this.debug("REDIRECTTO: " + (redirect.redirectUrl == existing.redirectUrl));
- this.debug("PATTERNTYPE: " + (redirect.patternType == existing.patternType));
if (existing.equals(redirect)) {
return true;
}
diff --git a/chrome/content/log.html b/chrome/content/log.html
index a70d9f4..a4f04d1 100644
--- a/chrome/content/log.html
+++ b/chrome/content/log.html
@@ -13,7 +13,12 @@
var prefix = 'REDIRECTOR:';
if (msg.message.substr(0, prefix.length) == prefix)
{
- document.body.innerHTML += msg.message.substr(prefix.length) + '
';
+ if (document) {
+ document.body.innerHTML += msg.message.substr(prefix.length) + '
';
+ } else {
+ //Been destroyed and we're still getting message, lets try to unsubscribe...
+ try { unload(); } catch(e) {}
+ }
}
}
};
diff --git a/components/redirector.component.js b/components/redirector.component.js
index eeb6b39..bebae88 100644
--- a/components/redirector.component.js
+++ b/components/redirector.component.js
@@ -30,17 +30,21 @@ var redirectorInstance = null;
const factory = {
// nsIFactory interface implementation
createInstance: function(outer, iid) {
- if (outer != null) {
- Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
- return null;
- }
- if (!iid.equals(Ci.nsIContentPolicy) &&
- !iid.equals(Ci.nsISupports)) {
+ if (outer != null) {
+ Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
+ return null;
+ }
+ dump("\n\nCreateInstance:\n");
+ if (!(iid.equals(Ci.nsIContentPolicy) || iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIChannelEventSink))) {
Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
return null;
}
-
+ dump("\n IID: " + iid);
+ dump("\n");
+ dump("\n nsIContentPolicy: " + iid.equals(Ci.nsIContentPolicy));
+ dump("\n nsISupports: " + iid.equals(Ci.nsISupports));
+ dump("\n nsIChannelEventSink: " + iid.equals(Ci.nsIChannelEventSink));
if(!redirectorInstance) {
redirectorInstance = new Redirector();
redirectorInstance.wrappedJSObject = redirectorInstance;
@@ -51,9 +55,7 @@ const factory = {
// nsISupports interface implementation
QueryInterface: function(iid) {
- if (iid.equals(Ci.nsISupports) ||
- iid.equals(Ci.nsIModule) ||
- iid.equals(Ci.nsIFactory)) {
+ if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIModule) || iid.equals(Ci.nsIFactory)) {
return this;
}
Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
@@ -61,28 +63,37 @@ const factory = {
}
}
+Redirector.prototype.QueryInterface = function(iid) {
+ if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIChannelEventSink) || iid.equals(Ci.nsIContentPolicy)) {
+ return this;
+ }
+ Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
+ return null;
+}
/*
* Module object
*/
const module = {
- registerSelf: function(compMgr, fileSpec, location, type) {
+ registerSelf : function(compMgr, fileSpec, location, type) {
compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
compMgr.registerFactoryLocation(CSSB_CID,
"Redirector content policy",
CSSB_CONTRACTID,
fileSpec, location, type);
-
var catman = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
+ catman.addCategoryEntry("net-channel-event-sinks", CSSB_CONTRACTID, CSSB_CONTRACTID, true, true);
catman.addCategoryEntry("content-policy", CSSB_CONTRACTID, CSSB_CONTRACTID, true, true);
},
unregisterSelf: function(compMgr, fileSpec, location) {
compMgr.QueryInterface(Ci.nsIComponentRegistrar).unregisterFactoryLocation(CSSB_CID, fileSpec);
Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager).deleteCategoryEntry("content-policy", CSSB_CONTRACTID, true);
+ Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager).deleteCategoryEntry("net-channel-event-sinks", CSSB_CONTRACTID, true);
},
getClassObject: function(compMgr, cid, iid) {
+ dump("\nGetClassObject: " + cid + ", iid: " + iid);
if (cid.equals(CSSB_CID)) {
return factory;
}
--
cgit v1.2.3-70-g09d2