How to use nsicookieservice to get cookie-changed list

570 views Asked by At

I've been reading the MDN docs about nsicookieservice and I'm interested in the notifications of this service: cookie-changed and cookie-rejected. Does that mean that we can get a list of the changed or rejected cookies? Would that be possible by doing somthing like this:

    var cookieSvc = Components.classes["@mozilla.org/cookieService;1"]
              .getService(Components.interfaces.nsICookieService);
    var cookies=cookieSvc.cookie-changed;

Thank you for the explanation.

I reread the documentation(I really did) and searched in the source codes of many extensions. The problem that the extension that i'm building is bootstarpped for firefox for android but I'm not using SDK. Based on what I understood I wrote this:

  var cookieMonster = {
//cookieManager: null,
current: null,
observerService: null,
cookieService: null,
init: function() {
this.observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
this.observerService.addObserver(this, "cookie-changed", false);
this.cookieService = Components.classes["@mozilla.org/cookieService;1"].getService(Components.interfaces.nsICookieService);
//  this.cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager);
},
uninit: function() {
//var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
this.observerService.removeObserver(this, "cookie-changed", false);
//this.cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager);
},
observe: function(subject, topic, data) {
    if (topic != 'cookie-changed') { this.current= "no cookie changed"; return; }
    try {
    //  if (!ghostery.prefs.cookieProtect) { return; }
    this.current="there is a chnged cookie!!!";
    if ( (data == 'added') || (data == 'changed') ) {
    var cookie = subject.QueryInterface(Components.interfaces.nsICookie2);
    this.current = "name "+cookie.name+" host "+cookie.host;
        }
    } catch (err) {}
  }
    };

Then I do this to add the observer when I load the page:

    let addListener = function() {
    window.BrowserApp.deck.addEventListener("load", cookieMonster.init, false);
    window.NativeWindow.toast.show(cookieMonster.current, "long");
    };
    if(window.BrowserApp.deck) {
    // BrowserApp.deck (and maybe whole BrowserApp?) has been initialized
    addListener();
    }
    else {
    // use the chrome window to wait for BrowserApp to initialize
    window.BrowserApp.deck.addEventListener("UIReady", addListener);
    }

That doesn't work and I don't find any error in the logcat which confuse me more.I'm really stuck and I need your help.

1

There are 1 answers

3
nmaier On

Yes, your add-on can receive notifications about cookies, but you don't receive them via the cookies manager, but the general purpose nsIObserverService.

  • XUL overlay add-ons should use the nsIObserverService directly.
  • SDK add-ons will want to use the system/events module instead, which is a bit higher level and does clean up after itself.

There is also a list of the most commonly used notification types.