2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* 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/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* 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)
|
2008-10-19 14:12:53 -07:00
|
|
|
* Michael Ventnor <m.ventnor@gmail.com>
|
|
|
|
* Ehsan Akhgari <ehsan.akhgari@gmail.com>
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
|
|
|
* 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 MPL, 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 MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef nsCookieService_h__
|
|
|
|
#define nsCookieService_h__
|
|
|
|
|
2007-06-26 03:41:58 -07:00
|
|
|
#include "nsICookieService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsICookieManager.h"
|
|
|
|
#include "nsICookieManager2.h"
|
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsWeakReference.h"
|
|
|
|
|
|
|
|
#include "nsCookie.h"
|
|
|
|
#include "nsString.h"
|
2009-12-19 15:55:56 -08:00
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsHashKeys.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsTHashtable.h"
|
2009-10-16 14:26:00 -07:00
|
|
|
#include "mozIStorageStatement.h"
|
|
|
|
#include "mozIStorageConnection.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
struct nsCookieAttributes;
|
|
|
|
struct nsListIter;
|
|
|
|
struct nsEnumerationData;
|
|
|
|
|
|
|
|
class nsICookiePermission;
|
2007-12-04 16:03:22 -08:00
|
|
|
class nsIEffectiveTLDService;
|
2010-01-12 10:29:20 -08:00
|
|
|
class nsIIDNService;
|
2007-03-22 10:30:00 -07:00
|
|
|
class nsIPrefBranch;
|
|
|
|
class nsIObserverService;
|
|
|
|
class nsIURI;
|
|
|
|
class nsIChannel;
|
|
|
|
|
|
|
|
// hash entry class
|
|
|
|
class nsCookieEntry : public PLDHashEntryHdr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// Hash methods
|
2009-12-19 15:55:56 -08:00
|
|
|
typedef const nsCString& KeyType;
|
|
|
|
typedef const nsCString* KeyTypePointer;
|
|
|
|
typedef nsTArray< nsRefPtr<nsCookie> > ArrayType;
|
|
|
|
typedef ArrayType::index_type IndexType;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-07-15 17:57:29 -07:00
|
|
|
explicit
|
2009-12-19 15:55:56 -08:00
|
|
|
nsCookieEntry(KeyTypePointer aBaseDomain)
|
|
|
|
: mBaseDomain(*aBaseDomain)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyType GetKey() const
|
|
|
|
{
|
2009-12-19 15:55:56 -08:00
|
|
|
return mBaseDomain;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool KeyEquals(KeyTypePointer aKey) const
|
|
|
|
{
|
2009-12-19 15:55:56 -08:00
|
|
|
return mBaseDomain == *aKey;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey)
|
|
|
|
{
|
2009-12-19 15:55:56 -08:00
|
|
|
return &aKey;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
2009-12-19 15:55:56 -08:00
|
|
|
return HashString(*aKey);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
enum { ALLOW_MEMMOVE = PR_TRUE };
|
|
|
|
|
2009-12-19 15:55:56 -08:00
|
|
|
inline ArrayType& GetCookies() { return mCookies; }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
2009-12-19 15:55:56 -08:00
|
|
|
nsCString mBaseDomain;
|
|
|
|
ArrayType mCookies;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
2009-10-16 14:01:47 -07:00
|
|
|
// encapsulates in-memory and on-disk DB states, so we can
|
|
|
|
// conveniently switch state when entering or exiting private browsing.
|
|
|
|
struct DBState
|
|
|
|
{
|
2009-12-15 14:21:28 -08:00
|
|
|
DBState() : cookieCount(0), cookieOldestTime(LL_MAXINT) { }
|
2009-10-16 14:01:47 -07:00
|
|
|
|
|
|
|
nsTHashtable<nsCookieEntry> hostTable;
|
|
|
|
PRUint32 cookieCount;
|
2009-12-15 14:21:28 -08:00
|
|
|
PRInt64 cookieOldestTime;
|
2009-10-16 14:01:47 -07:00
|
|
|
nsCOMPtr<mozIStorageConnection> dbConn;
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmtInsert;
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmtDelete;
|
|
|
|
nsCOMPtr<mozIStorageStatement> stmtUpdate;
|
|
|
|
};
|
|
|
|
|
2010-05-27 14:38:43 -07:00
|
|
|
// these constants represent a decision about a cookie based on user prefs.
|
|
|
|
enum CookieStatus
|
|
|
|
{
|
|
|
|
STATUS_ACCEPTED,
|
|
|
|
STATUS_ACCEPT_SESSION,
|
|
|
|
STATUS_REJECTED,
|
|
|
|
// STATUS_REJECTED_WITH_ERROR indicates the cookie should be rejected because
|
|
|
|
// of an error (rather than something the user can control). this is used for
|
|
|
|
// notification purposes, since we only want to notify of rejections where
|
|
|
|
// the user can do something about it (e.g. whitelist the site).
|
|
|
|
STATUS_REJECTED_WITH_ERROR
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* nsCookieService:
|
|
|
|
* class declaration
|
|
|
|
******************************************************************************/
|
|
|
|
|
2007-06-26 03:41:58 -07:00
|
|
|
class nsCookieService : public nsICookieService
|
2007-03-22 10:30:00 -07:00
|
|
|
, public nsICookieManager2
|
|
|
|
, public nsIObserver
|
|
|
|
, public nsSupportsWeakReference
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// nsISupports
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSICOOKIESERVICE
|
|
|
|
NS_DECL_NSICOOKIEMANAGER
|
|
|
|
NS_DECL_NSICOOKIEMANAGER2
|
|
|
|
|
|
|
|
nsCookieService();
|
|
|
|
virtual ~nsCookieService();
|
|
|
|
static nsCookieService* GetSingleton();
|
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void PrefChanged(nsIPrefBranch *aPrefBranch);
|
2009-10-16 14:01:47 -07:00
|
|
|
nsresult InitDB();
|
|
|
|
nsresult TryInitDB(PRBool aDeleteExistingDB);
|
2007-06-17 14:52:22 -07:00
|
|
|
nsresult CreateTable();
|
2008-12-10 15:54:57 -08:00
|
|
|
void CloseDB();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult Read();
|
2010-01-12 10:29:20 -08:00
|
|
|
nsresult NormalizeHost(nsCString &aHost);
|
|
|
|
nsresult GetBaseDomain(nsIURI *aHostURI, nsCString &aBaseDomain, PRBool &aRequireHostMatch);
|
2009-12-19 15:55:56 -08:00
|
|
|
nsresult GetBaseDomainFromHost(const nsACString &aHost, nsCString &aBaseDomain);
|
2008-03-12 00:53:50 -07:00
|
|
|
void GetCookieInternal(nsIURI *aHostURI, nsIChannel *aChannel, PRBool aHttpBound, char **aCookie);
|
|
|
|
nsresult SetCookieStringInternal(nsIURI *aHostURI, nsIPrompt *aPrompt, const char *aCookieHeader, const char *aServerTime, nsIChannel *aChannel, PRBool aFromHttp);
|
2010-05-27 14:38:43 -07:00
|
|
|
PRBool SetCookieInternal(nsIURI *aHostURI, nsIChannel *aChannel, const nsCString& aBaseDomain, PRBool aRequireHostMatch, CookieStatus aStatus, nsDependentCString &aCookieHeader, PRInt64 aServerTime, PRBool aFromHttp);
|
2010-05-25 15:00:44 -07:00
|
|
|
void AddInternal(const nsCString& aBaseDomain, nsCookie *aCookie, PRInt64 aCurrentTimeInUsec, nsIURI *aHostURI, const char *aCookieHeader, PRBool aFromHttp);
|
2010-03-12 07:18:38 -08:00
|
|
|
void RemoveCookieFromList(const nsListIter &aIter, mozIStorageBindingParamsArray *aParamsArray = NULL);
|
|
|
|
PRBool AddCookieToList(const nsCString& aBaseDomain, nsCookie *aCookie, mozIStorageBindingParamsArray *aParamsArray, PRBool aWriteToDB = PR_TRUE);
|
|
|
|
void UpdateCookieInList(nsCookie *aCookie, PRInt64 aLastAccessed, mozIStorageBindingParamsArray *aParamsArray);
|
2007-03-22 10:30:00 -07:00
|
|
|
static PRBool GetTokenValue(nsASingleFragmentCString::const_char_iterator &aIter, nsASingleFragmentCString::const_char_iterator &aEndIter, nsDependentCSubstring &aTokenString, nsDependentCSubstring &aTokenValue, PRBool &aEqualsFound);
|
|
|
|
static PRBool ParseAttributes(nsDependentCString &aCookieHeader, nsCookieAttributes &aCookie);
|
2010-01-12 10:29:20 -08:00
|
|
|
PRBool IsForeign(const nsCString &aBaseDomain, PRBool aRequireHostMatch, nsIURI *aFirstURI);
|
2010-05-27 14:38:43 -07:00
|
|
|
CookieStatus CheckPrefs(nsIURI *aHostURI, nsIChannel *aChannel, const nsCString &aBaseDomain, PRBool aRequireHostMatch, const char *aCookieHeader);
|
2010-01-12 10:29:20 -08:00
|
|
|
PRBool CheckDomain(nsCookieAttributes &aCookie, nsIURI *aHostURI, const nsCString &aBaseDomain, PRBool aRequireHostMatch);
|
2007-03-22 10:30:00 -07:00
|
|
|
static PRBool CheckPath(nsCookieAttributes &aCookie, nsIURI *aHostURI);
|
2007-06-20 22:17:08 -07:00
|
|
|
static PRBool GetExpiry(nsCookieAttributes &aCookie, PRInt64 aServerTime, PRInt64 aCurrentTime);
|
2007-03-22 10:30:00 -07:00
|
|
|
void RemoveAllFromMemory();
|
2009-12-15 14:21:28 -08:00
|
|
|
void PurgeCookies(PRInt64 aCurrentTimeInUsec);
|
2009-12-19 15:55:56 -08:00
|
|
|
PRBool FindCookie(const nsCString& aBaseDomain, const nsAFlatCString &aHost, const nsAFlatCString &aName, const nsAFlatCString &aPath, nsListIter &aIter, PRInt64 aCurrentTime);
|
|
|
|
PRUint32 CountCookiesFromHostInternal(const nsCString &aBaseDomain, nsEnumerationData &aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
void NotifyRejected(nsIURI *aHostURI);
|
2009-12-15 14:21:28 -08:00
|
|
|
void NotifyChanged(nsISupports *aSubject, const PRUnichar *aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
protected:
|
2009-10-16 14:01:47 -07:00
|
|
|
// cached members.
|
2007-12-04 16:03:22 -08:00
|
|
|
nsCOMPtr<nsIObserverService> mObserverService;
|
|
|
|
nsCOMPtr<nsICookiePermission> mPermissionService;
|
|
|
|
nsCOMPtr<nsIEffectiveTLDService> mTLDService;
|
2010-01-12 10:29:20 -08:00
|
|
|
nsCOMPtr<nsIIDNService> mIDNService;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-10-16 14:01:47 -07:00
|
|
|
// we have two separate DB states: one for normal browsing and one for
|
|
|
|
// private browsing, switching between them as appropriate. this state
|
|
|
|
// encapsulates both the in-memory table and the on-disk DB.
|
|
|
|
// note that the private states' dbConn should always be null - we never
|
|
|
|
// want to be dealing with the on-disk DB when in private browsing.
|
|
|
|
DBState *mDBState;
|
|
|
|
DBState mDefaultDBState;
|
|
|
|
DBState mPrivateDBState;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// cached prefs
|
2010-05-27 14:38:43 -07:00
|
|
|
PRUint8 mCookieBehavior; // BEHAVIOR_{ACCEPT, REJECTFOREIGN, REJECT}
|
|
|
|
PRBool mThirdPartySession;
|
2007-03-22 10:30:00 -07:00
|
|
|
PRUint16 mMaxNumberOfCookies;
|
|
|
|
PRUint16 mMaxCookiesPerHost;
|
2009-12-15 14:21:28 -08:00
|
|
|
PRInt64 mCookiePurgeAge;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// private static member, used to cache a ptr to nsCookieService,
|
|
|
|
// so we can make nsCookieService a singleton xpcom object.
|
|
|
|
static nsCookieService *gCookieService;
|
|
|
|
|
|
|
|
// this callback needs access to member functions
|
2009-12-15 14:21:28 -08:00
|
|
|
friend PLDHashOperator purgeCookiesCallback(nsCookieEntry *aEntry, void *aArg);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsCookieService_h__
|