Created
January 18, 2012 01:16
-
-
Save nmaier/1630169 to your computer and use it in GitHub Desktop.
http request/response observer template module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Any copyright is dedicated to the Public Domain. | |
* http://creativecommons.org/publicdomain/zero/1.0/ */ | |
"use strict"; | |
const EXPORTED_SYMBOLS = ['HttpRequestObserver']; | |
const {classes: Cc, interfaces: Ci, utils: Cu} = Components; | |
const module = Cu.import; | |
const error = Cu.reportError; | |
module("resource://gre/modules/XPCOMUtils.jsm"); | |
module("resource://gre/modules/Services.jsm"); | |
function HttpRequestObserver() { | |
this._init(); | |
} | |
HttpRequestObserver.prototype = { | |
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference, Ci.nsIWeakReference]), | |
QueryReferent: function(iid) this.QueryInterface(iid), | |
GetWeakReference: function() this, | |
_init: function _init() { | |
Services.obs.addObserver(this, 'xpcom-shutdown', true); | |
this._registerHttpObservers(); | |
}, | |
_uninit: function _uninit() { | |
this._unregisterHttpObservers(); | |
Services.obs.removeObserver(this, 'xpcom-shutdown'); | |
}, | |
_registerHttpObservers: function _registerHttpObservers() { | |
Services.obs.addObserver(this, 'http-on-modify-request', true); | |
Services.obs.addObserver(this, 'http-on-examine-response', true); | |
Services.obs.addObserver(this, 'http-on-examine-cached-response', true); | |
}, | |
_unregisterHttpObservers: function _unregisterHttpObservers() { | |
Services.obs.removeObserver(this, 'http-on-modify-request'); | |
Services.obs.removeObserver(this, 'http-on-examine-response'); | |
Services.obs.removeObserver(this, 'http-on-examine-cached-response'); | |
}, | |
observe: function observe(subject, topic, data) { | |
switch(topic) { | |
case 'xpcom-shutdown': | |
this._uninit(); | |
break; | |
case 'http-on-modify-request': | |
this.observeRequest(subject, topic, data); | |
break; | |
case 'http-on-examine-response': | |
case 'http-on-examine-cached-response': | |
this.observeResponse(subject, topic, data); | |
break; | |
} | |
}, | |
observeRequest: function observeRequest(channel, topic, data) { | |
if (!(channel instanceof Ci.nsIHttpChannel)) { | |
return; | |
} | |
error("Request: " + channel.requestMethod + ": " + channel.URI.spec); | |
}, | |
observeResponse: function observeResponse(channel, topic, data) { | |
if (!(channel instanceof Ci.nsIHttpChannel)) { | |
return; | |
} | |
error("Response: " + channel.responseStatus + ": " + channel.URI.spec); | |
} | |
}; | |
HttpRequestObserver = new HttpRequestObserver(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment