diff options
| author | Einar Egilsson <einar@einaregilsson.com> | 2009-10-16 08:11:33 +0000 | 
|---|---|---|
| committer | Einar Egilsson <einar@einaregilsson.com> | 2009-10-16 08:11:33 +0000 | 
| commit | 372aa4eee4e9b0b0a930d83a378502e9cec2e786 (patch) | |
| tree | 1ca3dc9a5aed593fab4f671a1e3c9a87d51c59e0 | |
| parent | 6177295e1d9a9ebba5acc68df4800d8557b22cab (diff) | |
Greatly simplified XPCOM plumbing
git-svn-id: http://einaregilsson.googlecode.com/svn/mozilla/redirector/trunk@270 119bf307-c92d-0410-89bd-8f53e6181181
| -rw-r--r-- | chrome/content/code/redirector.prototype.js | 23 | ||||
| -rw-r--r-- | components/redirector.component.js | 118 | 
2 files changed, 32 insertions, 109 deletions
| diff --git a/chrome/content/code/redirector.prototype.js b/chrome/content/code/redirector.prototype.js index 979e9f7..d575e52 100644 --- a/chrome/content/code/redirector.prototype.js +++ b/chrome/content/code/redirector.prototype.js @@ -1,18 +1,11 @@  //// $Id$
 -Cc = Components.classes;
 -Ci = Components.interfaces;
 -Cr = Components.results;
 -nsIContentPolicy = Ci.nsIContentPolicy;
 -
  Redirector.prototype = {
  	prefs : null,
  	list : null,
      strings : null,
      cout : Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService),
 -    
 -    
      init : function() {
  	    this.prefs = new Prefs();
 @@ -72,19 +65,19 @@ Redirector.prototype = {  	    try {
  		    if (!this.prefs.enabled) {
 -	            return nsIContentPolicy.ACCEPT;
 +	            return Ci.nsIContentPolicy.ACCEPT;
  	        }
 -	        if (contentType != nsIContentPolicy.TYPE_DOCUMENT) {
 -	            return nsIContentPolicy.ACCEPT;
 +	        if (contentType != Ci.nsIContentPolicy.TYPE_DOCUMENT) {
 +	            return Ci.nsIContentPolicy.ACCEPT;
  	        }
  	        if (contentLocation.scheme != "http" && contentLocation.scheme != "https") {
 -	            return nsIContentPolicy.ACCEPT;
 +	            return Ci.nsIContentPolicy.ACCEPT;
  	        }
  	        if (!aContext || !aContext.loadURI) {
 -	            return nsIContentPolicy.ACCEPT;
 +	            return Ci.nsIContentPolicy.ACCEPT;
  	        }
  	        this.debug("Checking " + contentLocation.spec);
 @@ -100,18 +93,18 @@ Redirector.prototype = {  	                redirectUrl = this.makeAbsoluteUrl(url, result.redirectTo);
  	                this.debug('Redirecting ' + url + ' to ' + redirectUrl);
  	                aContext.loadURI(redirectUrl, requestOrigin, null);
 -	                return nsIContentPolicy.REJECT_REQUEST;
 +	                return Ci.nsIContentPolicy.REJECT_REQUEST;
  	            }
  	        }
          } catch(e) {
  	    	this.debug(e);   
          }
 -        return nsIContentPolicy.ACCEPT;
 +        return Ci.nsIContentPolicy.ACCEPT;
      },
      // nsIContentPolicy interface implementation
      shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode, mimeType, extra) {
 -        return nsIContentPolicy.ACCEPT;
 +        return Ci.nsIContentPolicy.ACCEPT;
      },
      //nsIChannelEventSink interface implementation
 diff --git a/components/redirector.component.js b/components/redirector.component.js index bebae88..6dce455 100644 --- a/components/redirector.component.js +++ b/components/redirector.component.js @@ -1,112 +1,42 @@  //// $Id$
 -const CSSB_CONTRACTID = "@einaregilsson.com/redirector;1";
 -const CSSB_CID = Components.ID("{b7a7a54f-0581-47ff-b086-d6920cb7a3f7}");
 -var Cc = Components.classes;
 -var Ci = Components.interfaces;
 -var Cr = Components.results;
 -var nsIContentPolicy = Ci.nsIContentPolicy;
 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
 +Ci = Components.interfaces;
 +Cr = Components.results;
 +Cc = Components.classes;
 +const loader = Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader);
  function Redirector() {
  	this.init();
 +	this.wrappedJSObject = this;
  }
 -const loader = Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader);
 -	
  try {
  	loader.loadSubScript('chrome://redirector/content/code/redirector.prototype.js');
  	loader.loadSubScript('chrome://redirector/content/code/redirect.js');
  	loader.loadSubScript('chrome://redirector/content/code/prefs.js');
  } catch(e) {
 -	for (i in e) 
 +	for (i in e) {
  		Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage('REDIRECTOR: Loading Redirector implementation failed: ' + i + e[i]);
 +	}
  }
 -/*
 - * Factory object
 - */
 -
 -var redirectorInstance = null;
 -
 -const factory = {
 -    // nsIFactory interface implementation
 -    createInstance: function(outer, iid) {
 -		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;
 -        }
 -
 -        return redirectorInstance;
 -    },
 -
 -    // nsISupports interface implementation
 -    QueryInterface: function(iid) {
 -        if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIModule) || iid.equals(Ci.nsIFactory)) {
 -            return this;
 -        }
 -        Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;          
 -        return null;   
 -    }
 -}
 +//Add the xpcom stuff to the prototype
 +var xpcomInfo = Redirector.prototype;
 +xpcomInfo.classDescription 	= "Redirector Component";
 +xpcomInfo.classID 			= Components.ID("{b7a7a54f-0581-47ff-b086-d6920cb7a3f7}");
 +xpcomInfo.contractID 		= "@einaregilsson.com/redirector;1";
 +xpcomInfo._xpcom_categories = [{category:'content-policy'},{category:'net-channel-event-sinks'}];
 +xpcomInfo.QueryInterface 	= XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIChannelEventSink]);
 +xpcomInfo._xpcom_factory 	= {
 +	createInstance: function(outer, iid) {
 +		if (outer) throw Cr.NS_ERROR_NO_AGGREGATION;
 +		return redirector.QueryInterface(iid);
 +	}
 +};
 -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;   
 +function NSGetModule(compMgr, fileSpec) {
 +	return XPCOMUtils.generateModule([Redirector]);
  }
 -/*
 - * Module object
 - */
 -const module = {
 -    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;
 -        }
 -
 -        Components.returnCode = Cr.NS_ERROR_NOT_REGISTERED;
 -        return null;
 -    },
 -
 -    canUnload: function(compMgr) {
 -        return true;
 -    }
 -};
 -
 -function NSGetModule(comMgr, fileSpec) {
 -    return module;
 -}
\ No newline at end of file +const redirector = new Redirector();
 | 
