1999-06-11 02:08:59 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The contents of this file are subject to the Netscape Public
|
|
|
|
* License Version 1.1 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.mozilla.org/NPL/
|
1999-06-11 02:08:59 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* Software distributed under the License is distributed on an "AS
|
|
|
|
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
* implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
1999-06-11 02:08:59 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-06-11 02:08:59 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:43:54 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-06-11 02:08:59 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define NS_IMPL_IDS
|
|
|
|
|
|
|
|
#include "nsIServiceManager.h"
|
2000-01-23 01:46:13 +03:00
|
|
|
#include "nsCookieService.h"
|
1999-06-11 02:08:59 +04:00
|
|
|
#include "nsCookieHTTPNotify.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
1999-06-12 04:01:52 +04:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsCookie.h"
|
1999-10-04 18:08:52 +04:00
|
|
|
#include "nsIGenericFactory.h"
|
1999-12-07 02:29:41 +03:00
|
|
|
#include "nsXPIDLString.h"
|
1999-06-11 02:08:59 +04:00
|
|
|
|
2000-01-23 01:46:13 +03:00
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
1999-06-11 02:08:59 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsCookieService Implementation
|
|
|
|
|
1999-11-30 01:02:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS1(nsCookieService, nsICookieService);
|
1999-06-11 02:08:59 +04:00
|
|
|
|
1999-12-17 23:47:46 +03:00
|
|
|
nsCookieService::nsCookieService()
|
|
|
|
: mInitted(PR_FALSE)
|
|
|
|
{
|
1999-07-31 17:22:20 +04:00
|
|
|
NS_INIT_REFCNT();
|
1999-06-11 02:08:59 +04:00
|
|
|
}
|
|
|
|
|
1999-12-17 23:47:46 +03:00
|
|
|
nsCookieService::~nsCookieService(void)
|
|
|
|
{
|
1999-06-11 02:08:59 +04:00
|
|
|
}
|
|
|
|
|
1999-12-17 23:47:46 +03:00
|
|
|
nsresult nsCookieService::Init()
|
|
|
|
{
|
|
|
|
// make sure we're not initted twice, because this has the serious
|
|
|
|
// consequence of reading the cookies file twice
|
|
|
|
if (mInitted)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(0, "Baking the cookies twice. Doesn't that make them biscuits?");
|
|
|
|
return NS_ERROR_ALREADY_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
1999-07-31 17:22:20 +04:00
|
|
|
nsresult rv;
|
2000-01-24 17:15:10 +03:00
|
|
|
|
2000-01-23 01:46:13 +03:00
|
|
|
// Make sure there exists the cookie http notify service
|
|
|
|
nsCOMPtr<nsIHTTPNotify> cookieNotifier = do_GetService(NS_COOKIEHTTPNOTIFY_PROGID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-11-24 00:55:50 +03:00
|
|
|
|
1999-07-31 17:22:20 +04:00
|
|
|
COOKIE_RegisterCookiePrefCallbacks();
|
1999-12-17 23:47:46 +03:00
|
|
|
COOKIE_ReadCookies();
|
|
|
|
mInitted = PR_TRUE;
|
1999-07-31 17:22:20 +04:00
|
|
|
return rv;
|
1999-06-11 02:08:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-12 04:01:52 +04:00
|
|
|
NS_IMETHODIMP
|
1999-07-31 17:22:20 +04:00
|
|
|
nsCookieService::GetCookieString(nsIURI *aURL, nsString& aCookie) {
|
1999-12-07 02:29:41 +03:00
|
|
|
nsXPIDLCString spec;
|
|
|
|
nsresult rv = aURL->GetSpec(getter_Copies(spec));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
char *cookie = COOKIE_GetCookie((char *)(const char *)spec);
|
1999-07-31 17:22:20 +04:00
|
|
|
if (nsnull != cookie) {
|
|
|
|
aCookie.SetString(cookie);
|
|
|
|
nsCRT::free(cookie);
|
|
|
|
} else {
|
1999-12-07 02:29:41 +03:00
|
|
|
// No Cookie isn't an error condition.
|
1999-07-31 17:22:20 +04:00
|
|
|
aCookie.SetString("");
|
|
|
|
}
|
1999-10-28 02:43:49 +04:00
|
|
|
return NS_OK;
|
1999-06-12 04:01:52 +04:00
|
|
|
}
|
|
|
|
|
1999-12-18 08:48:26 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::GetCookieStringFromHTTP(nsIURI *aURL, nsIURI *aFirstURL, nsString& aCookie) {
|
|
|
|
nsXPIDLCString spec;
|
|
|
|
nsresult rv = aURL->GetSpec(getter_Copies(spec));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
nsXPIDLCString firstSpec;
|
|
|
|
rv = aFirstURL->GetSpec(getter_Copies(firstSpec));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
char *cookie = COOKIE_GetCookieFromHttp((char *)(const char *)spec, (char *)(const char *)firstSpec);
|
|
|
|
if (nsnull != cookie) {
|
|
|
|
aCookie.SetString(cookie);
|
|
|
|
nsCRT::free(cookie);
|
|
|
|
} else {
|
|
|
|
// No Cookie isn't an error condition.
|
|
|
|
aCookie.SetString("");
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-12 04:01:52 +04:00
|
|
|
NS_IMETHODIMP
|
1999-07-31 17:22:20 +04:00
|
|
|
nsCookieService::SetCookieString(nsIURI *aURL, const nsString& aCookie) {
|
|
|
|
char *spec = NULL;
|
|
|
|
nsresult result = aURL->GetSpec(&spec);
|
|
|
|
NS_ASSERTION(result == NS_OK, "deal with this");
|
|
|
|
char *cookie = aCookie.ToNewCString();
|
|
|
|
COOKIE_SetCookieString((char *)spec, cookie);
|
|
|
|
nsCRT::free(spec);
|
1999-09-06 10:34:47 +04:00
|
|
|
nsCRT::free(cookie);
|
1999-07-31 17:22:20 +04:00
|
|
|
return NS_OK;
|
1999-06-12 04:01:52 +04:00
|
|
|
}
|
|
|
|
|
1999-11-30 01:02:20 +03:00
|
|
|
NS_IMETHODIMP
|
1999-12-18 08:48:26 +03:00
|
|
|
nsCookieService::SetCookieStringFromHttp(nsIURI *aURL, nsIURI *aFirstURL, const char *aCookie, const char *aExpires) {
|
1999-11-30 01:02:20 +03:00
|
|
|
char *spec = NULL;
|
|
|
|
nsresult rv = aURL->GetSpec(&spec);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-12-18 08:48:26 +03:00
|
|
|
char *firstSpec = NULL;
|
|
|
|
rv = aFirstURL->GetSpec(&firstSpec); if (NS_FAILED(rv)) return rv;
|
|
|
|
COOKIE_SetCookieStringFromHttp(spec, firstSpec, (char *)aCookie, (char *)aExpires);
|
1999-11-30 01:02:20 +03:00
|
|
|
nsCRT::free(spec);
|
1999-12-18 08:48:26 +03:00
|
|
|
nsCRT::free(firstSpec);
|
1999-11-30 01:02:20 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-09 22:36:55 +04:00
|
|
|
NS_IMETHODIMP nsCookieService::Cookie_RemoveAllCookies(void) {
|
|
|
|
::COOKIE_RemoveAllCookies();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-31 17:22:20 +04:00
|
|
|
NS_IMETHODIMP nsCookieService::Cookie_CookieViewerReturn(nsAutoString results) {
|
|
|
|
::COOKIE_CookieViewerReturn(results);
|
|
|
|
return NS_OK;
|
1999-06-12 04:01:52 +04:00
|
|
|
}
|
|
|
|
|
1999-07-31 17:22:20 +04:00
|
|
|
NS_IMETHODIMP nsCookieService::Cookie_GetCookieListForViewer(nsString& aCookieList) {
|
|
|
|
::COOKIE_GetCookieListForViewer(aCookieList);
|
|
|
|
return NS_OK;
|
1999-06-12 04:01:52 +04:00
|
|
|
}
|
|
|
|
|
1999-07-31 17:22:20 +04:00
|
|
|
NS_IMETHODIMP nsCookieService::Cookie_GetPermissionListForViewer(nsString& aPermissionList) {
|
|
|
|
::COOKIE_GetPermissionListForViewer(aPermissionList);
|
|
|
|
return NS_OK;
|
1999-06-12 04:01:52 +04:00
|
|
|
}
|
|
|
|
|
1999-12-23 01:41:18 +03:00
|
|
|
NS_IMETHODIMP nsCookieService::CookieEnabled(PRBool* aEnabled)
|
|
|
|
{
|
|
|
|
*aEnabled = (COOKIE_GetBehaviorPref() != COOKIE_DontUse);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-11 02:08:59 +04:00
|
|
|
|
1999-10-04 18:08:52 +04:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
|
1999-11-20 23:19:01 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Define the contructor function for the objects
|
1999-12-17 03:42:33 +03:00
|
|
|
|
|
|
|
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsCookieService, Init)
|
2000-01-23 01:46:13 +03:00
|
|
|
NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsCookieHTTPNotify, Init)
|
1999-06-11 02:08:59 +04:00
|
|
|
|
1999-11-20 23:19:01 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Define a table of CIDs implemented by this module along with other
|
|
|
|
// information like the function to create an instance, progid, and
|
|
|
|
// class name.
|
|
|
|
//
|
|
|
|
static nsModuleComponentInfo components[] = {
|
|
|
|
{ "CookieService", NS_COOKIESERVICE_CID,
|
1999-12-17 03:42:33 +03:00
|
|
|
NS_COOKIESERVICE_PROGID, nsCookieServiceConstructor, }, // XXX Singleton
|
2000-01-23 01:46:13 +03:00
|
|
|
{ NS_COOKIEHTTPNOTIFY_CLASSNAME,
|
|
|
|
NS_COOKIEHTTPNOTIFY_CID,
|
|
|
|
NS_COOKIEHTTPNOTIFY_PROGID,
|
2000-01-28 13:22:47 +03:00
|
|
|
nsCookieHTTPNotifyConstructor,
|
|
|
|
nsCookieHTTPNotify::RegisterProc,
|
|
|
|
nsCookieHTTPNotify::UnregisterProc
|
|
|
|
},
|
1999-10-04 18:08:52 +04:00
|
|
|
};
|
1999-06-11 02:08:59 +04:00
|
|
|
|
1999-11-20 23:19:01 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Implement the NSGetModule() exported function for your module
|
|
|
|
// and the entire implementation of the module object.
|
|
|
|
//
|
|
|
|
NS_IMPL_NSGETMODULE("nsCookieModule", components)
|