1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 2003
20 * the Initial Developer. All Rights Reserved.
23 * Daniel Witte (dwitte@stanford.edu)
24 * Michiel van Leeuwen (mvl@exedo.nl)
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef nsCookieService_h__
41 #define nsCookieService_h__
43 #include "nsICookieService.h"
44 #include "nsICookieManager.h"
45 #include "nsICookieManager2.h"
46 #include "nsIObserver.h"
47 #include "nsWeakReference.h"
51 #include "nsTHashtable.h"
53 struct nsCookieAttributes
;
55 struct nsEnumerationData
;
57 class nsICookiePermission
;
58 class nsIEffectiveTLDService
;
60 class nsIObserverService
;
63 class mozIStorageConnection
;
64 class mozIStorageStatement
;
67 class nsCookieEntry
: public PLDHashEntryHdr
71 typedef const char* KeyType
;
72 typedef const char* KeyTypePointer
;
74 // do nothing with aHost - we require mHead to be set before we're live!
75 nsCookieEntry(KeyTypePointer aHost
)
80 nsCookieEntry(const nsCookieEntry
& toCopy
)
82 // if we end up here, things will break. nsTHashtable shouldn't
83 // allow this, since we set ALLOW_MEMMOVE to true.
84 NS_NOTREACHED("nsCookieEntry copy constructor is forbidden!");
89 // walk the linked list, and de-link everything by releasing & nulling.
90 // this allows the parent host entry to be deleted by the hashtable.
91 // note: we know mHead cannot be null here - we always set mHead to a
92 // valid nsCookie (if it were null, the hashtable wouldn't be able to find
93 // this entry, because the key string is provided by mHead).
94 nsCookie
*current
= mHead
, *next
;
96 next
= current
->Next();
98 } while ((current
= next
));
101 KeyType
GetKey() const
106 PRBool
KeyEquals(KeyTypePointer aKey
) const
108 return !strcmp(HostPtr(), aKey
);
111 static KeyTypePointer
KeyToPointer(KeyType aKey
)
116 static PLDHashNumber
HashKey(KeyTypePointer aKey
)
118 // PL_DHashStringKey doesn't use the table parameter, so we can safely
120 return PL_DHashStringKey(nsnull
, aKey
);
123 enum { ALLOW_MEMMOVE
= PR_TRUE
};
126 inline const nsDependentCString
Host() const { return mHead
->Host(); }
128 // linked list management helper
129 inline nsCookie
*& Head() { return mHead
; }
131 inline KeyTypePointer
HostPtr() const
133 return mHead
->Host().get();
140 /******************************************************************************
143 ******************************************************************************/
145 class nsCookieService
: public nsICookieService
146 , public nsICookieManager2
148 , public nsSupportsWeakReference
154 NS_DECL_NSICOOKIESERVICE
155 NS_DECL_NSICOOKIEMANAGER
156 NS_DECL_NSICOOKIEMANAGER2
159 virtual ~nsCookieService();
160 static nsCookieService
* GetSingleton();
164 void PrefChanged(nsIPrefBranch
*aPrefBranch
);
166 nsresult
CreateTable();
168 void GetCookieInternal(nsIURI
*aHostURI
, nsIChannel
*aChannel
, PRBool aHttpBound
, char **aCookie
);
169 nsresult
SetCookieStringInternal(nsIURI
*aHostURI
, nsIPrompt
*aPrompt
, const char *aCookieHeader
, const char *aServerTime
, nsIChannel
*aChannel
, PRBool aFromHttp
);
170 PRBool
SetCookieInternal(nsIURI
*aHostURI
, nsIChannel
*aChannel
, nsDependentCString
&aCookieHeader
, PRInt64 aServerTime
, PRBool aFromHttp
);
171 void AddInternal(nsCookie
*aCookie
, PRInt64 aCurrentTime
, nsIURI
*aHostURI
, const char *aCookieHeader
, PRBool aFromHttp
);
172 void RemoveCookieFromList(nsListIter
&aIter
);
173 PRBool
AddCookieToList(nsCookie
*aCookie
, PRBool aWriteToDB
= PR_TRUE
);
174 void UpdateCookieInList(nsCookie
*aCookie
, PRInt64 aLastAccessed
);
175 static PRBool
GetTokenValue(nsASingleFragmentCString::const_char_iterator
&aIter
, nsASingleFragmentCString::const_char_iterator
&aEndIter
, nsDependentCSubstring
&aTokenString
, nsDependentCSubstring
&aTokenValue
, PRBool
&aEqualsFound
);
176 static PRBool
ParseAttributes(nsDependentCString
&aCookieHeader
, nsCookieAttributes
&aCookie
);
177 PRBool
IsForeign(nsIURI
*aHostURI
, nsIURI
*aFirstURI
);
178 PRUint32
CheckPrefs(nsIURI
*aHostURI
, nsIChannel
*aChannel
, const char *aCookieHeader
);
179 PRBool
CheckDomain(nsCookieAttributes
&aCookie
, nsIURI
*aHostURI
);
180 static PRBool
CheckPath(nsCookieAttributes
&aCookie
, nsIURI
*aHostURI
);
181 static PRBool
GetExpiry(nsCookieAttributes
&aCookie
, PRInt64 aServerTime
, PRInt64 aCurrentTime
);
182 void RemoveAllFromMemory();
183 void RemoveExpiredCookies(PRInt64 aCurrentTime
);
184 PRBool
FindCookie(const nsAFlatCString
&aHost
, const nsAFlatCString
&aName
, const nsAFlatCString
&aPath
, nsListIter
&aIter
, PRInt64 aCurrentTime
);
185 void FindOldestCookie(nsEnumerationData
&aData
);
186 PRUint32
CountCookiesFromHostInternal(const nsACString
&aHost
, nsEnumerationData
&aData
);
187 void NotifyRejected(nsIURI
*aHostURI
);
188 void NotifyChanged(nsICookie2
*aCookie
, const PRUnichar
*aData
);
192 nsCOMPtr
<mozIStorageConnection
> mDBConn
;
193 nsCOMPtr
<mozIStorageStatement
> mStmtInsert
;
194 nsCOMPtr
<mozIStorageStatement
> mStmtDelete
;
195 nsCOMPtr
<mozIStorageStatement
> mStmtUpdate
;
196 nsCOMPtr
<nsIObserverService
> mObserverService
;
197 nsCOMPtr
<nsICookiePermission
> mPermissionService
;
198 nsCOMPtr
<nsIEffectiveTLDService
> mTLDService
;
201 nsTHashtable
<nsCookieEntry
> mHostTable
;
202 PRUint32 mCookieCount
;
205 PRUint8 mCookiesPermissions
; // BEHAVIOR_{ACCEPT, REJECTFOREIGN, REJECT}
206 PRUint16 mMaxNumberOfCookies
;
207 PRUint16 mMaxCookiesPerHost
;
209 // private static member, used to cache a ptr to nsCookieService,
210 // so we can make nsCookieService a singleton xpcom object.
211 static nsCookieService
*gCookieService
;
213 // this callback needs access to member functions
214 friend PLDHashOperator PR_CALLBACK
removeExpiredCallback(nsCookieEntry
*aEntry
, void *aArg
);
217 #endif // nsCookieService_h__