I do something very similar in my own ff addon. I'm assuming that you want a reference to the nsiHttpChannel
associated with the connection. However, I'm not sure you can just add properties to it (and have them persist), since its probably backed by native code, I'm not sure either way. But you can store the nsiHttpChannel
elsewhere and keep an id on it that way.
Here's some simplified code that I use to monitor http traffic in my addon, which should solve your problem.
var Cc = Components.classes;
var Ci = Components.interfaces;
var MyHttpObserver = {
// must be exposed so that the ObserverService can register itself
observe: function(subject, topic, data) {
subject.QueryInterface(Ci.nsIHttpChannel);
if( topic === "http-on-modify-request" ){
// store 'subject' somewhere
} else if( topic === "http-on-examine-response" ){
// look up 'subject' it will be the same reference as before
}
},
register: function() {
var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
observerService.addObserver(this, "http-on-modify-request", false);
observerService.addObserver(this, "http-on-examine-response", false);
},
unregister: function() {
var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
observerService.removeObserver(this, "http-on-modify-request");
observerService.removeObserver(this, "http-on-examine-response");
},
QueryInterface: function (aIID) {
if (aIID.equals(Ci.nsIObserver) || aIID.equals(Ci.nsISupports) ){
return this;
}
throw Components.results.NS_NOINTERFACE;
}
};