pjs/extensions/cookie/nsCookieService.cpp

228 строки
6.9 KiB
C++
Исходник Обычный вид История

1999-06-11 02:08:59 +04:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* 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
*
* 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
*
* 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
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
1999-06-11 02:08:59 +04:00
*/
#define NS_IMPL_IDS
1999-10-04 18:08:52 +04:00
#include "nsCOMPtr.h"
1999-06-11 02:08:59 +04:00
#include "nsIFactory.h"
#include "nsIServiceManager.h"
#include "nsICookieService.h"
#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 "nsIModule.h"
#include "nsIGenericFactory.h"
1999-06-11 02:08:59 +04:00
static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
////////////////////////////////////////////////////////////////////////////////
class nsCookieService : public nsICookieService {
public:
1999-07-31 17:22:20 +04:00
// nsISupports
NS_DECL_ISUPPORTS
1999-06-11 02:08:59 +04:00
1999-07-31 17:22:20 +04:00
// nsICookieService
static nsresult GetCookieService(nsICookieService** aCookieService);
1999-06-12 04:01:52 +04:00
1999-07-31 17:22:20 +04:00
NS_IMETHOD GetCookieString(nsIURI *aURL, nsString& aCookie);
NS_IMETHOD SetCookieString(nsIURI *aURL, const nsString& aCookie);
NS_IMETHOD SetCookieStringFromHttp(nsIURI *aURL, const char *aCookie, const char *aExpires);
1999-08-09 22:36:55 +04:00
NS_IMETHOD Cookie_RemoveAllCookies(void);
1999-07-31 17:22:20 +04:00
NS_IMETHOD Cookie_CookieViewerReturn(nsAutoString results);
NS_IMETHOD Cookie_GetCookieListForViewer(nsString& aCookieList);
NS_IMETHOD Cookie_GetPermissionListForViewer(nsString& aPermissionList);
1999-06-11 02:08:59 +04:00
1999-07-31 17:22:20 +04:00
nsCookieService();
virtual ~nsCookieService(void);
1999-06-11 02:08:59 +04:00
protected:
private:
1999-07-31 17:22:20 +04:00
nsIHTTPNotify *mCookieHTTPNotify;
nsresult Init();
1999-06-11 02:08:59 +04:00
};
static nsCookieService* gCookieService = nsnull; // The one-and-only CookieService
////////////////////////////////////////////////////////////////////////////////
// nsCookieService Implementation
NS_IMPL_ISUPPORTS1(nsCookieService, nsICookieService);
1999-06-11 02:08:59 +04:00
1999-07-31 17:22:20 +04:00
NS_EXPORT nsresult NS_NewCookieService(nsICookieService** aCookieService) {
return nsCookieService::GetCookieService(aCookieService);
1999-06-11 02:08:59 +04:00
}
1999-07-31 17:22:20 +04:00
nsCookieService::nsCookieService() {
NS_INIT_REFCNT();
mCookieHTTPNotify = nsnull;
Init();
1999-06-11 02:08:59 +04:00
}
1999-07-31 17:22:20 +04:00
nsCookieService::~nsCookieService(void) {
NS_IF_RELEASE(mCookieHTTPNotify);
1999-07-31 17:22:20 +04:00
gCookieService = nsnull;
1999-06-11 02:08:59 +04:00
}
1999-07-31 17:22:20 +04:00
nsresult nsCookieService::GetCookieService(nsICookieService** aCookieService) {
if (! gCookieService) {
nsCookieService* it = new nsCookieService();
if (! it) {
return NS_ERROR_OUT_OF_MEMORY;
1999-06-11 02:08:59 +04:00
}
1999-07-31 17:22:20 +04:00
gCookieService = it;
}
NS_ADDREF(gCookieService);
*aCookieService = gCookieService;
return NS_OK;
1999-06-11 02:08:59 +04:00
}
1999-07-31 17:22:20 +04:00
nsresult nsCookieService::Init() {
nsresult rv;
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
1999-11-24 00:55:50 +03:00
if (NS_FAILED(rv)) return rv;
rv = eventQService->CreateThreadEventQueue();
if (NS_FAILED(rv)) return rv;
1999-07-31 17:22:20 +04:00
if (NS_FAILED(rv = NS_NewCookieHTTPNotify(&mCookieHTTPNotify))) {
return rv;
}
1999-11-24 00:55:50 +03:00
1999-07-31 17:22:20 +04:00
COOKIE_RegisterCookiePrefCallbacks();
COOKIE_ReadCookies();
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) {
char *spec = NULL;
nsresult result = aURL->GetSpec(&spec);
NS_ASSERTION(result == NS_OK, "deal with this");
char *cookie = COOKIE_GetCookie((char *)spec);
if (nsnull != cookie) {
aCookie.SetString(cookie);
nsCRT::free(cookie);
} else {
aCookie.SetString("");
}
nsCRT::free(spec);
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);
nsCRT::free(cookie);
1999-07-31 17:22:20 +04:00
return NS_OK;
1999-06-12 04:01:52 +04:00
}
NS_IMETHODIMP
nsCookieService::SetCookieStringFromHttp(nsIURI *aURL, const char *aCookie, const char *aExpires) {
char *spec = NULL;
nsresult rv = aURL->GetSpec(&spec);
if (NS_FAILED(rv)) return rv;
COOKIE_SetCookieStringFromHttp(spec, (char *)aCookie, (char *)aExpires);
nsCRT::free(spec);
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-06-11 02:08:59 +04:00
1999-10-04 18:08:52 +04:00
//----------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////
// Define the contructor function for the objects
//
// NOTE: This creates an instance of objects by using the default constructor
//
// NS_GENERIC_FACTORY_CONSTRUCTOR(nsCookieService)
1999-10-04 18:08:52 +04:00
static NS_IMETHODIMP
CreateNewCookieService(nsISupports* aOuter, REFNSIID aIID, void **aResult)
{
if (!aResult) {
return NS_ERROR_INVALID_POINTER;
}
if (aOuter) {
*aResult = nsnull;
return NS_ERROR_NO_AGGREGATION;
}
nsICookieService* inst = nsnull;
nsresult rv = NS_NewCookieService(&inst);
if (NS_FAILED(rv)) {
*aResult = nsnull;
return rv;
}
if(!inst) {
return NS_ERROR_OUT_OF_MEMORY;
}
rv = inst->QueryInterface(aIID, aResult);
if (NS_FAILED(rv)) {
*aResult = nsnull;
}
NS_RELEASE(inst); /* get rid of extra refcnt */
return rv;
}
1999-06-11 02:08:59 +04: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,
NS_COOKIESERVICE_PROGID, CreateNewCookieService, }, // XXX Singleton
1999-10-04 18:08:52 +04:00
};
1999-06-11 02:08:59 +04:00
////////////////////////////////////////////////////////////////////////
// Implement the NSGetModule() exported function for your module
// and the entire implementation of the module object.
//
NS_IMPL_NSGETMODULE("nsCookieModule", components)