nsICookieService

IID:011C3190-1434-11d6-A618-0010A401EB10
Inherits From:nsISupports

Provides methods for setting and getting cookies in the context of a page load. See nsICookieManager for methods to manipulate the cookie database directly. This separation of interface is mainly historical.

This service broadcasts the following notifications when the cookie list is changed, or a cookie is rejected:

Topic : "cookie-changed" broadcast whenever the cookie list changes in some way. there are four possible data strings for this notification; one notification will be broadcast for each change, and will involve a single cookie. subject: an nsICookie2 interface pointer representing the cookie object that changed. data : "deleted" a cookie was deleted. the subject is the deleted cookie. "added" a cookie was added. the subject is the added cookie. "changed" a cookie was changed. the subject is the new cookie. "cleared" the entire cookie list was cleared. the subject is null.

Topic : "cookie-rejected" broadcast whenever a cookie was rejected from being set as a result of user prefs. subject: an nsIURI interface pointer representing the URI that attempted to set the cookie. data : none.

This interface is intended to be used as a service. To create an object implementing this interface:

var obj = Components.classes["@mozilla.org/cookieService;1"].
            getService(Components.interfaces.nsICookieService);

This interface is implemented by the following components:


Properties

readonly PRBool cookieIconIsVisible

This attribute really doesn't belong on this interface. CVS blame will tell you why it is here. It remains until we can find a better home for it. Read the source if you want to know what it does :-(


Methods

char* getCookieString ( nsIURI URI , nsIChannel channel ) char* getCookieStringFromHttp ( nsIURI URI , nsIURI firstURI , nsIChannel channel ) void setCookieString ( nsIURI URI , nsIPrompt prompt , char* cookie , nsIChannel channel ) void setCookieStringFromHttp ( nsIURI URI , nsIURI firstURI , nsIPrompt prompt , char* cookie , char* serverTime , nsIChannel channel )

char* getCookieString ( nsIURI URI , nsIChannel channel )

Arguments:
URI
channel

char* getCookieStringFromHttp ( nsIURI URI , nsIURI firstURI , nsIChannel channel )

Arguments:
URI
firstURI
channel

void setCookieString ( nsIURI URI , nsIPrompt prompt , char* cookie , nsIChannel channel )

Arguments:
URI
prompt
cookie
channel

void setCookieStringFromHttp ( nsIURI URI , nsIURI firstURI , nsIPrompt prompt , char* cookie , char* serverTime , nsIChannel channel )

Arguments:
URI
firstURI
prompt
cookie
serverTime
channel

Reference documentation is generated from Mozilla's source.

Add a note User Contributed Notes
October 23, 2005, 8:05 pm mike at example dot com
If you do have privileged access, the following code will set a cookie for any domain. I am not sure it is 100% correct for everything but it does the job for simple sites.

function set_any_cookie(uri_string, name, value){
if (Components){
var Cc = Components.classes;
var Ci = Components.interfaces;

try {
cookie_manager = Cc["@mozilla.org/cookieService;1"].createInstance(Ci.nsICookieService);
uri = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService).newURI(uri_string, 'ISO-8859-1', null);

cookie_manager.setCookieString(uri, null, name+'='+value, null);
}
catch(e){
alert(e);
return false;
}
}
}

call like this

set_any_cookie('http://www.example.com', 'language', 'en');
August 26, 2005, 9:12 am pike-xul at kw dot nl
Please note, that this method is *not* available to unprivileged javascript
and that the simple document.cookie property is not available in a XUL document. As far as I can see, setting and reading cookies from unprivileged XUL is simply not possible.

Copyright © 1999 - 2005 XULPlanet.com