2003-10-10 23:09:18 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 22:01:16 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2003-10-10 23:09:18 +00:00
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
2003-10-10 23:09:18 +00: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.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 22:01:16 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2003-10-10 23:09:18 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2003
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Daniel Witte (dwitte@stanford.edu)
|
|
|
|
* Michiel van Leeuwen (mvl@exedo.nl)
|
|
|
|
*
|
|
|
|
* 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
|
2004-04-18 22:01:16 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2003-10-10 23:09:18 +00:00
|
|
|
* 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
|
2004-04-18 22:01:16 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2003-10-10 23:09:18 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef nsCookieService_h__
|
|
|
|
#define nsCookieService_h__
|
|
|
|
|
2005-09-15 11:03:09 +00:00
|
|
|
#include "nsICookieServiceInternal.h"
|
2003-10-10 23:09:18 +00:00
|
|
|
#include "nsICookieManager.h"
|
|
|
|
#include "nsICookieManager2.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
|
|
|
|
#include "nsCookie.h"
|
|
|
|
#include "nsString.h"
|
2003-10-22 06:53:19 +00:00
|
|
|
#include "nsTHashtable.h"
|
2003-10-10 23:09:18 +00:00
|
|
|
|
2004-06-26 18:47:01 +00:00
|
|
|
#include "nsInt64.h"
|
|
|
|
|
2003-10-10 23:09:18 +00:00
|
|
|
struct nsCookieAttributes;
|
2003-10-22 06:53:19 +00:00
|
|
|
struct nsListIter;
|
|
|
|
struct nsEnumerationData;
|
2005-09-15 11:03:09 +00:00
|
|
|
|
|
|
|
class nsAutoVoidArray;
|
|
|
|
|
2003-11-14 06:43:08 +00:00
|
|
|
class nsIPrefBranch;
|
2003-10-10 23:09:18 +00:00
|
|
|
class nsICookieConsent;
|
|
|
|
class nsICookiePermission;
|
|
|
|
class nsIPrefBranch;
|
|
|
|
class nsIObserverService;
|
|
|
|
class nsIURI;
|
|
|
|
class nsIChannel;
|
|
|
|
class nsITimer;
|
|
|
|
class nsIFile;
|
|
|
|
|
2003-10-22 06:53:19 +00:00
|
|
|
// hash entry class
|
|
|
|
class nsCookieEntry : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Hash methods
|
|
|
|
typedef const char* KeyType;
|
|
|
|
typedef const char* KeyTypePointer;
|
|
|
|
|
|
|
|
// do nothing with aHost - we require mHead to be set before we're live!
|
|
|
|
nsCookieEntry(KeyTypePointer aHost)
|
|
|
|
: mHead(nsnull)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCookieEntry(const nsCookieEntry& toCopy)
|
|
|
|
{
|
|
|
|
// if we end up here, things will break. nsTHashtable shouldn't
|
|
|
|
// allow this, since we set ALLOW_MEMMOVE to true.
|
|
|
|
NS_NOTREACHED("nsCookieEntry copy constructor is forbidden!");
|
|
|
|
}
|
|
|
|
|
|
|
|
~nsCookieEntry()
|
|
|
|
{
|
|
|
|
// walk the linked list, and de-link everything by releasing & nulling.
|
|
|
|
// this allows the parent host entry to be deleted by the hashtable.
|
|
|
|
// note: we know mHead cannot be null here - we always set mHead to a
|
|
|
|
// valid nsCookie (if it were null, the hashtable wouldn't be able to find
|
|
|
|
// this entry, because the key string is provided by mHead).
|
|
|
|
nsCookie *current = mHead, *next;
|
|
|
|
do {
|
|
|
|
next = current->Next();
|
|
|
|
NS_RELEASE(current);
|
|
|
|
} while ((current = next));
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyType GetKey() const
|
|
|
|
{
|
|
|
|
return HostPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool KeyEquals(KeyTypePointer aKey) const
|
|
|
|
{
|
|
|
|
return !strcmp(HostPtr(), aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey)
|
|
|
|
{
|
|
|
|
return aKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
// PL_DHashStringKey doesn't use the table parameter, so we can safely
|
|
|
|
// pass nsnull
|
|
|
|
return PL_DHashStringKey(nsnull, aKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { ALLOW_MEMMOVE = PR_TRUE };
|
|
|
|
|
|
|
|
// get methods
|
|
|
|
inline const nsDependentCString Host() const { return mHead->Host(); }
|
|
|
|
|
|
|
|
// linked list management helper
|
|
|
|
inline nsCookie*& Head() { return mHead; }
|
|
|
|
|
|
|
|
inline KeyTypePointer HostPtr() const
|
|
|
|
{
|
|
|
|
return mHead->Host().get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCookie *mHead;
|
|
|
|
};
|
|
|
|
|
2003-10-10 23:09:18 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService:
|
|
|
|
* class declaration
|
|
|
|
******************************************************************************/
|
|
|
|
|
2005-09-15 11:03:09 +00:00
|
|
|
class nsCookieService : public nsICookieServiceInternal
|
2003-10-10 23:09:18 +00:00
|
|
|
, public nsICookieManager2
|
|
|
|
, public nsIObserver
|
|
|
|
, public nsSupportsWeakReference
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nsISupports
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSICOOKIESERVICE
|
2005-09-15 11:03:09 +00:00
|
|
|
NS_DECL_NSICOOKIESERVICEINTERNAL
|
2003-10-10 23:09:18 +00:00
|
|
|
NS_DECL_NSICOOKIEMANAGER
|
|
|
|
NS_DECL_NSICOOKIEMANAGER2
|
|
|
|
|
|
|
|
nsCookieService();
|
|
|
|
virtual ~nsCookieService();
|
|
|
|
static nsCookieService* GetSingleton();
|
2003-10-22 06:53:19 +00:00
|
|
|
nsresult Init();
|
2003-10-10 23:09:18 +00:00
|
|
|
|
|
|
|
protected:
|
2003-11-14 06:43:08 +00:00
|
|
|
void PrefChanged(nsIPrefBranch *aPrefBranch);
|
2003-10-10 23:09:18 +00:00
|
|
|
nsresult Read();
|
|
|
|
nsresult Write();
|
2007-03-14 17:22:01 +00:00
|
|
|
void GetCookieList(nsIURI *aHostURI, nsIURI *aFirstURI, nsIChannel *aChannel, const nsACString *aName, PRBool isHttpBound, nsAutoVoidArray &aResult);
|
|
|
|
char* CookieStringFromArray(const nsAutoVoidArray& aCookieList, nsIURI *aHostURI);
|
2003-10-10 23:09:18 +00:00
|
|
|
PRBool SetCookieInternal(nsIURI *aHostURI, nsIChannel *aChannel, nsDependentCString &aCookieHeader, nsInt64 aServerTime, nsCookieStatus aStatus, nsCookiePolicy aPolicy);
|
2005-09-15 11:03:09 +00:00
|
|
|
void CheckAndAdd(nsIURI *aHostURI, nsIChannel *aChannel, nsCookieAttributes &aAttributes, nsCookieStatus aStatus, nsCookiePolicy aPolicy, const nsAFlatCString &aCookieHeader);
|
2003-10-22 06:53:19 +00:00
|
|
|
void AddInternal(nsCookie *aCookie, nsInt64 aCurrentTime, nsIURI *aHostURI, const char *aCookieHeader);
|
|
|
|
void RemoveCookieFromList(nsListIter &aIter);
|
2003-11-11 09:39:32 +00:00
|
|
|
PRBool AddCookieToList(nsCookie *aCookie);
|
2004-02-19 02:44:03 +00:00
|
|
|
static PRBool GetTokenValue(nsASingleFragmentCString::const_char_iterator &aIter, nsASingleFragmentCString::const_char_iterator &aEndIter, nsDependentCSubstring &aTokenString, nsDependentCSubstring &aTokenValue, PRBool &aEqualsFound);
|
2003-10-10 23:09:18 +00:00
|
|
|
static PRBool ParseAttributes(nsDependentCString &aCookieHeader, nsCookieAttributes &aCookie);
|
|
|
|
static PRBool IsIPAddress(const nsAFlatCString &aHost);
|
|
|
|
static PRBool IsInDomain(const nsACString &aDomain, const nsACString &aHost, PRBool aIsDomain = PR_TRUE);
|
|
|
|
static PRBool IsForeign(nsIURI *aHostURI, nsIURI *aFirstURI);
|
2003-11-14 06:43:08 +00:00
|
|
|
nsCookieStatus CheckPrefs(nsIURI *aHostURI, nsIURI *aFirstURI, nsIChannel *aChannel, const char *aCookieHeader, nsCookiePolicy &aPolicy);
|
2003-10-31 08:44:00 +00:00
|
|
|
static PRBool CheckDomain(nsCookieAttributes &aCookie, nsIURI *aHostURI);
|
2003-10-10 23:09:18 +00:00
|
|
|
static PRBool CheckPath(nsCookieAttributes &aCookie, nsIURI *aHostURI);
|
2003-10-31 08:44:00 +00:00
|
|
|
static PRBool GetExpiry(nsCookieAttributes &aCookie, nsInt64 aServerTime, nsInt64 aCurrentTime, nsCookieStatus aStatus);
|
2003-10-10 23:09:18 +00:00
|
|
|
void RemoveAllFromMemory();
|
2003-10-22 06:53:19 +00:00
|
|
|
void RemoveExpiredCookies(nsInt64 aCurrentTime);
|
|
|
|
PRBool FindCookie(const nsAFlatCString &aHost, const nsAFlatCString &aName, const nsAFlatCString &aPath, nsListIter &aIter);
|
|
|
|
void FindOldestCookie(nsEnumerationData &aData);
|
2007-05-05 21:09:54 +00:00
|
|
|
PRUint32 CountCookiesFromHostInternal(const nsACString &aHost, nsEnumerationData &aData);
|
2003-10-22 06:53:19 +00:00
|
|
|
void NotifyRejected(nsIURI *aHostURI);
|
|
|
|
void NotifyChanged(nsICookie2 *aCookie, const PRUnichar *aData);
|
2003-10-10 23:09:18 +00:00
|
|
|
|
|
|
|
// Use LazyWrite to save the cookies file on a timer. It will write
|
|
|
|
// the file only once if repeatedly hammered quickly.
|
|
|
|
void LazyWrite();
|
|
|
|
static void DoLazyWrite(nsITimer *aTimer, void *aClosure);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// cached members
|
|
|
|
nsCOMPtr<nsIFile> mCookieFile;
|
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
|
|
|
nsCOMPtr<nsICookieConsent> mP3PService;
|
|
|
|
nsCOMPtr<nsICookiePermission> mPermissionService;
|
|
|
|
|
|
|
|
// impl members
|
|
|
|
nsCOMPtr<nsITimer> mWriteTimer;
|
2003-10-22 06:53:19 +00:00
|
|
|
nsTHashtable<nsCookieEntry> mHostTable;
|
|
|
|
PRUint32 mCookieCount;
|
2003-10-10 23:09:18 +00:00
|
|
|
PRPackedBool mCookieChanged;
|
|
|
|
PRPackedBool mCookieIconVisible;
|
|
|
|
|
|
|
|
// cached prefs
|
2003-10-16 20:54:29 +00:00
|
|
|
PRUint8 mCookiesPermissions; // BEHAVIOR_{ACCEPT, REJECTFOREIGN, REJECT, P3P}
|
2004-04-24 04:17:31 +00:00
|
|
|
PRUint16 mMaxNumberOfCookies;
|
|
|
|
PRUint16 mMaxCookiesPerHost;
|
2003-10-10 23:09:18 +00:00
|
|
|
|
|
|
|
// private static member, used to cache a ptr to nsCookieService,
|
|
|
|
// so we can make nsCookieService a singleton xpcom object.
|
|
|
|
static nsCookieService *gCookieService;
|
2003-10-22 06:53:19 +00:00
|
|
|
|
|
|
|
// this callback needs access to member functions
|
|
|
|
friend PLDHashOperator PR_CALLBACK removeExpiredCallback(nsCookieEntry *aEntry, void *aArg);
|
2003-10-10 23:09:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsCookieService_h__
|