1999-06-11 02:08:59 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2001-09-26 03:31:24 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-06-11 02:08:59 +04:00
|
|
|
*
|
2001-09-26 03:31:24 +04: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/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2001-09-26 03:31:24 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-26 03:31:24 +04:00
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-06-11 02:08:59 +04:00
|
|
|
|
|
|
|
#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"
|
1999-06-12 04:01:52 +04:00
|
|
|
#include "nsCRT.h"
|
2001-03-29 06:11:48 +04:00
|
|
|
#include "nsCookies.h"
|
1999-10-04 18:08:52 +04:00
|
|
|
#include "nsIGenericFactory.h"
|
1999-12-07 02:29:41 +03:00
|
|
|
#include "nsXPIDLString.h"
|
2000-09-07 11:00:36 +04:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIDOMWindowInternal.h"
|
|
|
|
#include "nsIPrompt.h"
|
2001-01-24 18:51:00 +03:00
|
|
|
#include "nsIObserverService.h"
|
2001-03-29 06:11:48 +04:00
|
|
|
#include "nsIDocumentLoader.h"
|
2001-05-05 00:15:38 +04:00
|
|
|
#include "nsIWebProgress.h"
|
2001-03-29 06:11:48 +04:00
|
|
|
#include "nsCURILoader.h"
|
2001-10-19 06:28:06 +04:00
|
|
|
#include "nsNetCID.h"
|
2001-03-29 06:11:48 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_IID(kDocLoaderServiceCID, NS_DOCUMENTLOADER_SERVICE_CID);
|
2001-10-19 06:28:06 +04:00
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
1999-06-11 02:08:59 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsCookieService Implementation
|
|
|
|
|
2001-03-29 06:11:48 +04:00
|
|
|
NS_IMPL_ISUPPORTS4(nsCookieService, nsICookieService,
|
2001-05-05 00:15:38 +04:00
|
|
|
nsIObserver, nsIWebProgressListener, nsISupportsWeakReference);
|
1999-06-11 02:08:59 +04:00
|
|
|
|
2002-02-07 00:55:16 +03:00
|
|
|
PRBool gCookieIconVisible = PR_FALSE;
|
|
|
|
|
1999-12-17 23:47:46 +03:00
|
|
|
nsCookieService::nsCookieService()
|
|
|
|
{
|
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)
|
|
|
|
{
|
2001-03-29 06:11:48 +04:00
|
|
|
COOKIE_Write(); /* in case any deleted cookies didn't get removed from file yet */
|
|
|
|
COOKIE_RemoveAll();
|
1999-06-11 02:08:59 +04:00
|
|
|
}
|
|
|
|
|
1999-12-17 23:47:46 +03:00
|
|
|
nsresult nsCookieService::Init()
|
|
|
|
{
|
2001-03-29 06:11:48 +04:00
|
|
|
COOKIE_RegisterPrefCallbacks();
|
2001-10-19 06:28:06 +04:00
|
|
|
nsresult rv;
|
|
|
|
mIOService = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
|
2001-03-29 06:11:48 +04:00
|
|
|
COOKIE_Read();
|
|
|
|
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
2001-10-23 02:01:27 +04:00
|
|
|
do_GetService("@mozilla.org/observer-service;1", &rv);
|
2001-01-24 18:51:00 +03:00
|
|
|
if (observerService) {
|
2001-10-20 00:52:59 +04:00
|
|
|
observerService->AddObserver(this, "profile-before-change", PR_TRUE);
|
|
|
|
observerService->AddObserver(this, "profile-do-change", PR_TRUE);
|
2002-02-07 00:55:16 +03:00
|
|
|
observerService->AddObserver(this, "cookieIcon", PR_FALSE);
|
2001-01-24 18:51:00 +03:00
|
|
|
}
|
2001-03-29 06:11:48 +04:00
|
|
|
|
|
|
|
// Register as an observer for the document loader
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIDocumentLoader> docLoaderService =
|
|
|
|
do_GetService(kDocLoaderServiceCID, &rv);
|
2001-03-29 06:11:48 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && docLoaderService) {
|
2001-05-05 00:15:38 +04:00
|
|
|
nsCOMPtr<nsIWebProgress> progress(do_QueryInterface(docLoaderService));
|
|
|
|
if (progress)
|
|
|
|
(void) progress->AddProgressListener((nsIWebProgressListener*)this);
|
2001-03-29 06:11:48 +04:00
|
|
|
} else {
|
|
|
|
NS_ASSERTION(PR_FALSE, "Could not get nsIDocumentLoader");
|
|
|
|
}
|
|
|
|
|
2000-08-04 02:04:28 +04:00
|
|
|
return NS_OK;
|
1999-06-11 02:08:59 +04:00
|
|
|
}
|
|
|
|
|
2001-05-05 00:15:38 +04:00
|
|
|
// nsIWebProgressListener implementation
|
2001-03-29 06:11:48 +04:00
|
|
|
NS_IMETHODIMP
|
2001-05-05 00:15:38 +04:00
|
|
|
nsCookieService::OnProgressChange(nsIWebProgress *aProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRInt32 curSelfProgress,
|
|
|
|
PRInt32 maxSelfProgress,
|
|
|
|
PRInt32 curTotalProgress,
|
|
|
|
PRInt32 maxTotalProgress)
|
2001-03-29 06:11:48 +04:00
|
|
|
{
|
2001-05-05 00:15:38 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
2001-05-05 00:15:38 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::OnStateChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRInt32 progressStateFlags,
|
|
|
|
nsresult aStatus)
|
2001-03-29 06:11:48 +04:00
|
|
|
{
|
2001-05-05 00:15:38 +04:00
|
|
|
if (progressStateFlags & STATE_IS_DOCUMENT)
|
|
|
|
if (progressStateFlags & STATE_STOP)
|
|
|
|
COOKIE_Write();
|
|
|
|
return NS_OK;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-05-05 00:15:38 +04:00
|
|
|
/* void onLocationChange (in nsIURI location); */
|
|
|
|
NS_IMETHODIMP nsCookieService::OnLocationChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
nsIURI *location)
|
2001-03-29 06:11:48 +04:00
|
|
|
{
|
2001-05-05 00:15:38 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
2001-05-05 00:15:38 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::OnStatusChange(nsIWebProgress* aWebProgress,
|
|
|
|
nsIRequest* aRequest,
|
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aMessage)
|
2001-03-29 06:11:48 +04:00
|
|
|
{
|
2001-05-05 00:15:38 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
|
|
|
|
2001-05-05 00:15:38 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::OnSecurityChange(nsIWebProgress *aWebProgress,
|
|
|
|
nsIRequest *aRequest,
|
|
|
|
PRInt32 state)
|
2001-03-29 06:11:48 +04:00
|
|
|
{
|
2001-05-05 00:15:38 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
2001-03-29 06:11:48 +04:00
|
|
|
}
|
1999-06-11 02:08:59 +04:00
|
|
|
|
2001-05-05 00:15:38 +04:00
|
|
|
|
1999-06-12 04:01:52 +04:00
|
|
|
NS_IMETHODIMP
|
2001-04-14 01:45:34 +04:00
|
|
|
nsCookieService::GetCookieString(nsIURI *aURL, char ** aCookie) {
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString spec;
|
|
|
|
nsresult rv = aURL->GetAsciiSpec(spec);
|
1999-12-07 02:29:41 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-03-06 10:48:55 +03:00
|
|
|
*aCookie = COOKIE_GetCookie((char *)spec.get(), mIOService);
|
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
|
2001-04-14 01:45:34 +04:00
|
|
|
nsCookieService::GetCookieStringFromHttp(nsIURI *aURL, nsIURI *aFirstURL, char ** aCookie) {
|
|
|
|
if (!aURL) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString spec;
|
|
|
|
nsresult rv = aURL->GetAsciiSpec(spec);
|
1999-12-18 08:48:26 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-04-14 01:45:34 +04:00
|
|
|
if (aFirstURL) {
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString firstSpec;
|
|
|
|
rv = aFirstURL->GetAsciiSpec(firstSpec);
|
2001-04-14 01:45:34 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-03-06 10:48:55 +03:00
|
|
|
*aCookie = COOKIE_GetCookieFromHttp((char *) spec.get(), (char *) firstSpec.get(), mIOService);
|
2001-04-14 01:45:34 +04:00
|
|
|
} else {
|
2002-03-06 10:48:55 +03:00
|
|
|
*aCookie = COOKIE_GetCookieFromHttp((char *) spec.get(), nsnull, mIOService);
|
2001-04-14 01:45:34 +04:00
|
|
|
}
|
1999-12-18 08:48:26 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-12 04:01:52 +04:00
|
|
|
NS_IMETHODIMP
|
2001-11-16 18:05:53 +03:00
|
|
|
nsCookieService::SetCookieString(nsIURI *aURL, nsIPrompt* aPrompt, const char * aCookie, nsIHttpChannel* aHttpChannel) {
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString spec;
|
|
|
|
nsresult result = aURL->GetAsciiSpec(spec);
|
1999-07-31 17:22:20 +04:00
|
|
|
NS_ASSERTION(result == NS_OK, "deal with this");
|
2000-09-07 11:00:36 +04:00
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
COOKIE_SetCookieString((char *) spec.get(), aPrompt, aCookie, mIOService, aHttpChannel);
|
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
|
2001-11-16 18:05:53 +03:00
|
|
|
nsCookieService::SetCookieStringFromHttp(nsIURI *aURL, nsIURI *aFirstURL, nsIPrompt *aPrompter, const char *aCookie, const char *aExpires, nsIHttpChannel* aHttpChannel)
|
2001-04-25 01:47:51 +04:00
|
|
|
{
|
2002-03-07 09:09:30 +03:00
|
|
|
nsXPIDLCString spec;
|
|
|
|
nsresult rv = aURL->GetSpec(spec);
|
1999-11-30 01:02:20 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-03-07 09:09:30 +03:00
|
|
|
nsIURI* firstURL = aFirstURL;
|
|
|
|
if (!firstURL) {
|
|
|
|
firstURL = aURL;
|
2001-04-25 01:47:51 +04:00
|
|
|
}
|
2002-03-07 09:09:30 +03:00
|
|
|
nsXPIDLCString firstSpec;
|
|
|
|
rv = firstURL->GetSpec(firstSpec);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
COOKIE_SetCookieStringFromHttp(
|
|
|
|
NS_CONST_CAST(char *, spec.get()),
|
|
|
|
NS_CONST_CAST(char *, firstSpec.get()),
|
|
|
|
aPrompter, aCookie, (char *)aExpires, mIOService, aHttpChannel);
|
1999-11-30 01:02:20 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-02-07 00:55:16 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsCookieService::GetCookieIconIsVisible(PRBool *aIsVisible) {
|
|
|
|
*aIsVisible = gCookieIconVisible;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
NS_IMETHODIMP nsCookieService::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData)
|
2001-01-24 18:51:00 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2001-03-29 06:11:48 +04:00
|
|
|
|
2001-10-20 00:52:59 +04:00
|
|
|
if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
|
2001-03-12 01:12:21 +03:00
|
|
|
// The profile is about to change.
|
2001-01-24 18:51:00 +03:00
|
|
|
|
|
|
|
// Dump current cookies. This will be done by calling
|
2001-03-29 06:11:48 +04:00
|
|
|
// COOKIE_RemoveAll which clears the memory-resident
|
2001-03-12 01:12:21 +03:00
|
|
|
// cookie table. The reason the cookie file does not
|
2001-01-24 18:51:00 +03:00
|
|
|
// need to be updated is because the file was updated every time
|
|
|
|
// the memory-resident table changed (i.e., whenever a new cookie
|
2001-03-12 01:12:21 +03:00
|
|
|
// was accepted). If this condition ever changes, the cookie
|
|
|
|
// file would need to be updated here.
|
2001-01-24 18:51:00 +03:00
|
|
|
|
2001-03-29 06:11:48 +04:00
|
|
|
COOKIE_RemoveAll();
|
2001-03-12 01:12:21 +03:00
|
|
|
if (!nsCRT::strcmp(someData, NS_LITERAL_STRING("shutdown-cleanse").get()))
|
|
|
|
COOKIE_DeletePersistentUserData();
|
2002-02-07 00:55:16 +03:00
|
|
|
} else if (!nsCRT::strcmp(aTopic, "profile-do-change")) {
|
2001-03-12 01:12:21 +03:00
|
|
|
// The profile has aleady changed.
|
|
|
|
// Now just read them from the new profile location.
|
2001-03-29 06:11:48 +04:00
|
|
|
COOKIE_Read();
|
2002-02-07 00:55:16 +03:00
|
|
|
} else if (!nsCRT::strcmp(aTopic, "cookieIcon")) {
|
|
|
|
gCookieIconVisible = (!nsCRT::strcmp(someData, NS_LITERAL_STRING("on").get()));
|
2001-01-24 18:51:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|