home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 June / PersonalComputerWorld-June2009-CoverdiscCD.iso / Software / Freeware / Firebug 1.3.3 / firebug-1.3.3-fx.xpi / components / firebug-http-observer.js next >
Encoding:
Text File  |  2009-02-19  |  5.8 KB  |  216 lines

  1. /* See license.txt for terms of usage */
  2.  
  3. // ************************************************************************************************
  4. // Constants
  5.  
  6. const CLASS_ID = Components.ID("{2D92593E-14D0-48ce-B260-A9881BBF9C8B}");
  7. const CLASS_NAME = "Firebug HTTP Observer Service";
  8. const CONTRACT_ID = "@joehewitt.com/firebug-http-observer;1";
  9.  
  10. const Cc = Components.classes;
  11. const Ci = Components.interfaces;
  12. const Cr = Components.results;
  13.  
  14. var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
  15. var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
  16.  
  17. // ************************************************************************************************
  18. // HTTP Request Observer implementation
  19.  
  20. var FBTrace = null;
  21.  
  22. /**
  23.  * This service is intended as the only HTTP observer registered by Firebug.
  24.  * All FB extensions and Firebug itself should register a listener within this
  25.  * servise in order to listen for http-on-modify-request and http-on-examine-response.
  26.  * See also: http://developer.mozilla.org/en/Setting_HTTP_request_headers
  27.  */
  28. function HttpRequestObserver()
  29. {
  30.     // Get firebug-trace service for logging (the service should be already
  31.     // registered at this moment).
  32.     FBTrace = Cc["@joehewitt.com/firebug-trace-service;1"]
  33.        .getService(Ci.nsISupports).wrappedJSObject.getTracer("extensions.firebug");
  34.  
  35.     this.observers = [];
  36. }
  37.  
  38. HttpRequestObserver.prototype = 
  39. {
  40.     initialize: function()
  41.     {
  42.         observerService.addObserver(this, "quit-application", false);
  43.         observerService.addObserver(this, "http-on-modify-request", false);
  44.         observerService.addObserver(this, "http-on-examine-response", false);
  45.  
  46.     },
  47.  
  48.     shutdown: function()
  49.     {
  50.         observerService.removeObserver(this, "quit-application");
  51.         observerService.removeObserver(this, "http-on-modify-request");
  52.         observerService.removeObserver(this, "http-on-examine-response");
  53.  
  54.     },
  55.  
  56.     /* nsIObserve */
  57.     observe: function(subject, topic, data)
  58.     {
  59.         if (topic == "app-startup") {
  60.             this.initialize();
  61.             return;
  62.         }
  63.         else if (topic == "quit-application") {
  64.             this.shutdown();
  65.             return;
  66.         }
  67.  
  68.         try 
  69.         {
  70.             if (topic == "http-on-modify-request" || topic == "http-on-examine-response")
  71.                 this.notifyObservers(subject, topic, data);
  72.         }
  73.         catch (err)
  74.         {
  75.         }
  76.     },
  77.  
  78.     /* nsIObserverService */
  79.     addObserver: function(observer, topic, weak)
  80.     {
  81.         if (topic != "firebug-http-event")
  82.             throw Cr.NS_ERROR_INVALID_ARG;
  83.  
  84.         this.observers.push(observer);
  85.     },
  86.  
  87.     removeObserver: function(observer, topic)
  88.     {
  89.         if (topic != "firebug-http-event")
  90.             throw Cr.NS_ERROR_INVALID_ARG;
  91.  
  92.         for (var i=0; i<this.observers.length; i++) {
  93.             if (this.observers[i] == observer) {
  94.                 this.observers.splice(i, 1);
  95.                 break;
  96.             }
  97.         }
  98.     },
  99.  
  100.     notifyObservers: function(subject, topic, data)
  101.     {
  102.         for (var i=0; i<this.observers.length; i++)
  103.             this.observers[i].observe(subject, topic, data);
  104.     },
  105.  
  106.     enumerateObservers: function(topic)
  107.     {
  108.         return null;
  109.     },
  110.  
  111.     /* nsISupports */
  112.     QueryInterface: function(iid) 
  113.     {
  114.         if (iid.equals(Ci.nsISupports) || 
  115.             iid.equals(Ci.nsIObserverService) ||
  116.             iid.equals(Ci.nsIObserver)) {
  117.              return this;
  118.          }
  119.         
  120.         throw Cr.NS_ERROR_NO_INTERFACE;
  121.     }
  122. }
  123.  
  124. function safeGetName(request)
  125. {
  126.     try
  127.     {
  128.         return request.name;
  129.     }
  130.     catch (exc)
  131.     {
  132.         return null;
  133.     }
  134. }
  135.  
  136. // ************************************************************************************************
  137. // Service factory
  138.  
  139. var gHttpObserverSingleton = null;
  140. var HttpRequestObserverFactory = 
  141. {
  142.     createInstance: function (outer, iid)
  143.     {
  144.         if (outer != null)
  145.             throw Cr.NS_ERROR_NO_AGGREGATION;
  146.  
  147.         if (iid.equals(Ci.nsISupports) ||
  148.             iid.equals(Ci.nsIObserverService) ||
  149.             iid.equals(Ci.nsIObserver))
  150.         {
  151.             if (!gHttpObserverSingleton)
  152.                 gHttpObserverSingleton = new HttpRequestObserver();
  153.             return gHttpObserverSingleton.QueryInterface(iid);
  154.         }
  155.         
  156.         throw Cr.NS_ERROR_NO_INTERFACE;
  157.     },
  158.     
  159.     QueryInterface: function(iid) 
  160.     {
  161.         if (iid.equals(Ci.nsISupports) ||
  162.             iid.equals(Ci.nsISupportsWeakReference) ||
  163.             iid.equals(Ci.nsIFactory))
  164.             return this;
  165.             
  166.         throw Cr.NS_ERROR_NO_INTERFACE;
  167.     }
  168. };
  169.  
  170. // ************************************************************************************************
  171. // Module implementation
  172.  
  173. var HttpRequestObserverModule =
  174. {
  175.     registerSelf: function (compMgr, fileSpec, location, type)
  176.     {
  177.         compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  178.         compMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME,
  179.             CONTRACT_ID, fileSpec, location, type);
  180.  
  181.         categoryManager.addCategoryEntry("app-startup", CLASS_NAME, 
  182.             "service," + CONTRACT_ID, true, true);
  183.     },
  184.  
  185.     unregisterSelf: function(compMgr, fileSpec, location)
  186.     {
  187.         compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
  188.         compMgr.unregisterFactoryLocation(CLASS_ID, location);
  189.  
  190.         categoryManager.deleteCategoryEntry("app-startup", CLASS_NAME, true);
  191.     },
  192.  
  193.     getClassObject: function (compMgr, cid, iid)
  194.     {
  195.         if (!iid.equals(Ci.nsIFactory))
  196.             throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  197.  
  198.         if (cid.equals(CLASS_ID))
  199.             return HttpRequestObserverFactory;
  200.  
  201.         throw Cr.NS_ERROR_NO_INTERFACE;
  202.     },
  203.  
  204.     canUnload: function(compMgr)
  205.     {
  206.         return true;
  207.     }
  208. };
  209.  
  210. // ************************************************************************************************
  211.  
  212. function NSGetModule(compMgr, fileSpec)
  213. {
  214.     return HttpRequestObserverModule;
  215. }
  216.