View Full Version: nsICookieManager2 interface

Addneditcookies > Main > nsICookieManager2 interface



Title: nsICookieManager2 interface
Description: Using the nsICookieManager2 interface


ministre - July 4, 2005 12:11 PM (GMT)
Hello,

I am trying to use the nsICookieManager2 interface of the "@mozilla.org/cookiemanager;1" component to add cookies to the browser. Unfortunately, the nsICookieManager2 is not scriptable so I cannot call it directly from JavaScript. So I wrote "wrapper" XPCOM component in C++ that calls the nsICookieManager2 to add the cookie. Reference to the nsICookieManager2 interface can be found here: http://www.xulplanet.com/references/xpcomr...ieManager2.html

The component I wrote is very simple and just for testing I wrote something like that:

CODE

nsresult rv;

nsCOMPtr<nsICookieManager2> cm =
   do_CreateInstance("@mozilla.org/cookiemanager;1", &rv);

if (cm)
{
   rv = file->Add(NS_LITERAL_CSTRING("my_domain"),
                       NS_LITERAL_CSTRING("my_path"),
                       NS_LITERAL_CSTRING("my_name"),
                       NS_LITERAL_CSTRING("my_value"),  0x1, 0x1, 0);

   if (NS_FAILED(rv))
   {
       /* Some clean up code and return from function */
      ...
      ...
   }
   ....
}



So the call to the cookiemanager is done correctly, my "rv" variable does not show a failure. However, the cookie is NOT added to the browser. I am using firefox-1.0.4.

I had a look at the lxr.mozilla to see how the cookiemanager is used. I found the following code: http://lxr.mozilla.org/seamonkey/source/ex...ncesWin.cpp#540
Function "CopyCookiesFromBuffer()" calls the cookiemanager on line #618.

I am using the cookiemanager in the same way but yet no cookies are added.

Any help and suggestions are welcome.

Thanks
ministre

goodwill - July 7, 2005 05:31 AM (GMT)
nsICookieManager2 interface not being scriptable is reason I had to do it the hard way.
I am not really sure why you need to write a c++ wrapper, it seems overly complicated. You should just extract teh code from my extension and take a look it. I have some nice checking function in there too, though the code is a bit sloppy.
Go over it and if that method of adding cookies suits and have some question fel free to ask. Hint: .xpi file and .jar file inside of it are just zip files you can extract them easily.

Goodwill.




Hosted for free by InvisionFree