1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Daniel Witte (dwitte@stanford.edu)
24 : * Michiel van Leeuwen (mvl@exedo.nl)
25 : * Michael Ventnor <m.ventnor@gmail.com>
26 : * Ehsan Akhgari <ehsan.akhgari@gmail.com>
27 : *
28 : * Alternatively, the contents of this file may be used under the terms of
29 : * either the GNU General Public License Version 2 or later (the "GPL"), or
30 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
31 : * in which case the provisions of the GPL or the LGPL are applicable instead
32 : * of those above. If you wish to allow use of your version of this file only
33 : * under the terms of either the GPL or the LGPL, and not to allow others to
34 : * use your version of this file under the terms of the MPL, indicate your
35 : * decision by deleting the provisions above and replace them with the notice
36 : * and other provisions required by the GPL or the LGPL. If you do not delete
37 : * the provisions above, a recipient may use your version of this file under
38 : * the terms of any one of the MPL, the GPL or the LGPL.
39 : *
40 : * ***** END LICENSE BLOCK ***** */
41 :
42 : #ifndef nsCookieService_h__
43 : #define nsCookieService_h__
44 :
45 : #include "nsICookieService.h"
46 : #include "nsICookieManager.h"
47 : #include "nsICookieManager2.h"
48 : #include "nsIObserver.h"
49 : #include "nsWeakReference.h"
50 :
51 : #include "nsCookie.h"
52 : #include "nsString.h"
53 : #include "nsAutoPtr.h"
54 : #include "nsHashKeys.h"
55 : #include "nsTHashtable.h"
56 : #include "mozIStorageStatement.h"
57 : #include "mozIStorageAsyncStatement.h"
58 : #include "mozIStoragePendingStatement.h"
59 : #include "mozIStorageConnection.h"
60 : #include "mozIStorageRow.h"
61 : #include "mozIStorageCompletionCallback.h"
62 : #include "mozIStorageStatementCallback.h"
63 :
64 : class nsICookiePermission;
65 : class nsIEffectiveTLDService;
66 : class nsIIDNService;
67 : class nsIPrefBranch;
68 : class nsIObserverService;
69 : class nsIURI;
70 : class nsIChannel;
71 : class nsIArray;
72 : class mozIStorageService;
73 : class mozIThirdPartyUtil;
74 : class ReadCookieDBListener;
75 :
76 : struct nsCookieAttributes;
77 : struct nsListIter;
78 : struct nsEnumerationData;
79 :
80 : namespace mozilla {
81 : namespace net {
82 : class CookieServiceParent;
83 : }
84 : }
85 :
86 : // hash entry class
87 : class nsCookieEntry : public PLDHashEntryHdr
88 : {
89 : public:
90 : // Hash methods
91 : typedef const nsCString& KeyType;
92 : typedef const nsCString* KeyTypePointer;
93 : typedef nsTArray< nsRefPtr<nsCookie> > ArrayType;
94 : typedef ArrayType::index_type IndexType;
95 :
96 : explicit
97 25031 : nsCookieEntry(KeyTypePointer aBaseDomain)
98 25031 : : mBaseDomain(*aBaseDomain)
99 : {
100 25031 : }
101 :
102 : nsCookieEntry(const nsCookieEntry& toCopy)
103 : {
104 : // if we end up here, things will break. nsTHashtable shouldn't
105 : // allow this, since we set ALLOW_MEMMOVE to true.
106 : NS_NOTREACHED("nsCookieEntry copy constructor is forbidden!");
107 : }
108 :
109 25031 : ~nsCookieEntry()
110 25031 : {
111 25031 : }
112 :
113 17 : KeyType GetKey() const
114 : {
115 17 : return mBaseDomain;
116 : }
117 :
118 16839 : bool KeyEquals(KeyTypePointer aKey) const
119 : {
120 16839 : return mBaseDomain == *aKey;
121 : }
122 :
123 71070 : static KeyTypePointer KeyToPointer(KeyType aKey)
124 : {
125 71070 : return &aKey;
126 : }
127 :
128 71070 : static PLDHashNumber HashKey(KeyTypePointer aKey)
129 : {
130 71070 : return mozilla::HashString(*aKey);
131 : }
132 :
133 : enum { ALLOW_MEMMOVE = true };
134 :
135 62418 : inline ArrayType& GetCookies() { return mCookies; }
136 :
137 : private:
138 : nsCString mBaseDomain;
139 : ArrayType mCookies;
140 : };
141 :
142 : // encapsulates a (baseDomain, nsCookie) tuple for temporary storage purposes.
143 : struct CookieDomainTuple
144 45950 : {
145 : nsCString baseDomain;
146 : nsRefPtr<nsCookie> cookie;
147 : };
148 :
149 : // encapsulates in-memory and on-disk DB states, so we can
150 : // conveniently switch state when entering or exiting private browsing.
151 : struct DBState
152 354 : {
153 354 : DBState() : cookieCount(0), cookieOldestTime(LL_MAXINT), corruptFlag(OK)
154 : {
155 354 : hostTable.Init();
156 354 : }
157 :
158 3142 : NS_INLINE_DECL_REFCOUNTING(DBState)
159 :
160 : // State of the database connection.
161 : enum CorruptFlag {
162 : OK, // normal
163 : CLOSING_FOR_REBUILD, // corruption detected, connection closing
164 : REBUILDING // close complete, rebuilding database from memory
165 : };
166 :
167 : nsTHashtable<nsCookieEntry> hostTable;
168 : PRUint32 cookieCount;
169 : PRInt64 cookieOldestTime;
170 : nsCOMPtr<nsIFile> cookieFile;
171 : nsCOMPtr<mozIStorageConnection> dbConn;
172 : nsCOMPtr<mozIStorageAsyncStatement> stmtInsert;
173 : nsCOMPtr<mozIStorageAsyncStatement> stmtDelete;
174 : nsCOMPtr<mozIStorageAsyncStatement> stmtUpdate;
175 : CorruptFlag corruptFlag;
176 :
177 : // Various parts representing asynchronous read state. These are useful
178 : // while the background read is taking place.
179 : nsCOMPtr<mozIStorageConnection> syncConn;
180 : nsCOMPtr<mozIStorageStatement> stmtReadDomain;
181 : nsCOMPtr<mozIStoragePendingStatement> pendingRead;
182 : // The asynchronous read listener. This is a weak ref (storage has ownership)
183 : // since it may need to outlive the DBState's database connection.
184 : ReadCookieDBListener* readListener;
185 : // An array of (baseDomain, cookie) tuples representing data read in
186 : // asynchronously. This is merged into hostTable once read is complete.
187 : nsTArray<CookieDomainTuple> hostArray;
188 : // A hashset of baseDomains read in synchronously, while the async read is
189 : // in flight. This is used to keep track of which data in hostArray is stale
190 : // when the time comes to merge.
191 : nsTHashtable<nsCStringHashKey> readSet;
192 :
193 : // DB completion handlers.
194 : nsCOMPtr<mozIStorageStatementCallback> insertListener;
195 : nsCOMPtr<mozIStorageStatementCallback> updateListener;
196 : nsCOMPtr<mozIStorageStatementCallback> removeListener;
197 : nsCOMPtr<mozIStorageCompletionCallback> closeListener;
198 : };
199 :
200 : // these constants represent a decision about a cookie based on user prefs.
201 : enum CookieStatus
202 : {
203 : STATUS_ACCEPTED,
204 : STATUS_ACCEPT_SESSION,
205 : STATUS_REJECTED,
206 : // STATUS_REJECTED_WITH_ERROR indicates the cookie should be rejected because
207 : // of an error (rather than something the user can control). this is used for
208 : // notification purposes, since we only want to notify of rejections where
209 : // the user can do something about it (e.g. whitelist the site).
210 : STATUS_REJECTED_WITH_ERROR
211 : };
212 :
213 : // Result codes for TryInitDB() and Read().
214 : enum OpenDBResult
215 : {
216 : RESULT_OK,
217 : RESULT_RETRY,
218 : RESULT_FAILURE
219 : };
220 :
221 : /******************************************************************************
222 : * nsCookieService:
223 : * class declaration
224 : ******************************************************************************/
225 :
226 : class nsCookieService : public nsICookieService
227 : , public nsICookieManager2
228 : , public nsIObserver
229 : , public nsSupportsWeakReference
230 : {
231 : public:
232 : // nsISupports
233 : NS_DECL_ISUPPORTS
234 : NS_DECL_NSIOBSERVER
235 : NS_DECL_NSICOOKIESERVICE
236 : NS_DECL_NSICOOKIEMANAGER
237 : NS_DECL_NSICOOKIEMANAGER2
238 :
239 : nsCookieService();
240 : virtual ~nsCookieService();
241 : static nsICookieService* GetXPCOMSingleton();
242 : nsresult Init();
243 :
244 : protected:
245 : void PrefChanged(nsIPrefBranch *aPrefBranch);
246 : void InitDBStates();
247 : OpenDBResult TryInitDB(bool aDeleteExistingDB);
248 : nsresult CreateTable();
249 : void CloseDBStates();
250 : void CloseDefaultDBConnection();
251 : void HandleDBClosed(DBState* aDBState);
252 : void HandleCorruptDB(DBState* aDBState);
253 : void RebuildCorruptDB(DBState* aDBState);
254 : OpenDBResult Read();
255 11319 : template<class T> nsCookie* GetCookieFromRow(T &aRow);
256 : void AsyncReadComplete();
257 : void CancelAsyncRead(bool aPurgeReadSet);
258 : void EnsureReadDomain(const nsCString &aBaseDomain);
259 : void EnsureReadComplete();
260 : nsresult NormalizeHost(nsCString &aHost);
261 : nsresult GetBaseDomain(nsIURI *aHostURI, nsCString &aBaseDomain, bool &aRequireHostMatch);
262 : nsresult GetBaseDomainFromHost(const nsACString &aHost, nsCString &aBaseDomain);
263 : nsresult GetCookieStringCommon(nsIURI *aHostURI, nsIChannel *aChannel, bool aHttpBound, char** aCookie);
264 : void GetCookieStringInternal(nsIURI *aHostURI, bool aIsForeign, bool aHttpBound, nsCString &aCookie);
265 : nsresult SetCookieStringCommon(nsIURI *aHostURI, const char *aCookieHeader, const char *aServerTime, nsIChannel *aChannel, bool aFromHttp);
266 : void SetCookieStringInternal(nsIURI *aHostURI, bool aIsForeign, nsDependentCString &aCookieHeader, const nsCString &aServerTime, bool aFromHttp);
267 : bool SetCookieInternal(nsIURI *aHostURI, const nsCString& aBaseDomain, bool aRequireHostMatch, CookieStatus aStatus, nsDependentCString &aCookieHeader, PRInt64 aServerTime, bool aFromHttp);
268 : void AddInternal(const nsCString& aBaseDomain, nsCookie *aCookie, PRInt64 aCurrentTimeInUsec, nsIURI *aHostURI, const char *aCookieHeader, bool aFromHttp);
269 : void RemoveCookieFromList(const nsListIter &aIter, mozIStorageBindingParamsArray *aParamsArray = NULL);
270 : void AddCookieToList(const nsCString& aBaseDomain, nsCookie *aCookie, DBState *aDBState, mozIStorageBindingParamsArray *aParamsArray, bool aWriteToDB = true);
271 : void UpdateCookieInList(nsCookie *aCookie, PRInt64 aLastAccessed, mozIStorageBindingParamsArray *aParamsArray);
272 : static bool GetTokenValue(nsASingleFragmentCString::const_char_iterator &aIter, nsASingleFragmentCString::const_char_iterator &aEndIter, nsDependentCSubstring &aTokenString, nsDependentCSubstring &aTokenValue, bool &aEqualsFound);
273 : static bool ParseAttributes(nsDependentCString &aCookieHeader, nsCookieAttributes &aCookie);
274 : bool RequireThirdPartyCheck();
275 : CookieStatus CheckPrefs(nsIURI *aHostURI, bool aIsForeign, const nsCString &aBaseDomain, bool aRequireHostMatch, const char *aCookieHeader);
276 : bool CheckDomain(nsCookieAttributes &aCookie, nsIURI *aHostURI, const nsCString &aBaseDomain, bool aRequireHostMatch);
277 : static bool CheckPath(nsCookieAttributes &aCookie, nsIURI *aHostURI);
278 : static bool GetExpiry(nsCookieAttributes &aCookie, PRInt64 aServerTime, PRInt64 aCurrentTime);
279 : void RemoveAllFromMemory();
280 : already_AddRefed<nsIArray> PurgeCookies(PRInt64 aCurrentTimeInUsec);
281 : bool FindCookie(const nsCString& aBaseDomain, const nsAFlatCString &aHost, const nsAFlatCString &aName, const nsAFlatCString &aPath, nsListIter &aIter);
282 : static void FindStaleCookie(nsCookieEntry *aEntry, PRInt64 aCurrentTime, nsListIter &aIter);
283 : void NotifyRejected(nsIURI *aHostURI);
284 : void NotifyChanged(nsISupports *aSubject, const PRUnichar *aData);
285 : void NotifyPurged(nsICookie2* aCookie);
286 : already_AddRefed<nsIArray> CreatePurgeList(nsICookie2* aCookie);
287 :
288 : protected:
289 : // cached members.
290 : nsCOMPtr<nsIObserverService> mObserverService;
291 : nsCOMPtr<nsICookiePermission> mPermissionService;
292 : nsCOMPtr<mozIThirdPartyUtil> mThirdPartyUtil;
293 : nsCOMPtr<nsIEffectiveTLDService> mTLDService;
294 : nsCOMPtr<nsIIDNService> mIDNService;
295 : nsCOMPtr<mozIStorageService> mStorageService;
296 :
297 : // we have two separate DB states: one for normal browsing and one for
298 : // private browsing, switching between them as appropriate. this state
299 : // encapsulates both the in-memory table and the on-disk DB.
300 : // note that the private states' dbConn should always be null - we never
301 : // want to be dealing with the on-disk DB when in private browsing.
302 : DBState *mDBState;
303 : nsRefPtr<DBState> mDefaultDBState;
304 : nsRefPtr<DBState> mPrivateDBState;
305 :
306 : // cached prefs
307 : PRUint8 mCookieBehavior; // BEHAVIOR_{ACCEPT, REJECTFOREIGN, REJECT}
308 : bool mThirdPartySession;
309 : PRUint16 mMaxNumberOfCookies;
310 : PRUint16 mMaxCookiesPerHost;
311 : PRInt64 mCookiePurgeAge;
312 :
313 : // friends!
314 : friend PLDHashOperator purgeCookiesCallback(nsCookieEntry *aEntry, void *aArg);
315 : friend class DBListenerErrorHandler;
316 : friend class ReadCookieDBListener;
317 : friend class CloseCookieDBListener;
318 :
319 : static nsCookieService* GetSingleton();
320 : friend class mozilla::net::CookieServiceParent;
321 : };
322 :
323 : #endif // nsCookieService_h__
|