Backed out changeset f1426851ae30 (bug 1844755) for causing failures on test_printpre...
[gecko.git] / netwerk / cookie / CookiePersistentStorage.h
blob630b565746b4276eecd61ee34d5c0dd9f2656393
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_net_CookiePersistentStorage_h
7 #define mozilla_net_CookiePersistentStorage_h
9 #include "CookieStorage.h"
11 #include "mozilla/Atomics.h"
12 #include "mozilla/Monitor.h"
13 #include "mozilla/net/NeckoChannelParams.h"
14 #include "mozIStorageBindingParamsArray.h"
15 #include "mozIStorageCompletionCallback.h"
16 #include "mozIStorageStatement.h"
17 #include "mozIStorageStatementCallback.h"
19 class mozIStorageAsyncStatement;
20 class mozIStorageService;
21 class nsICookieTransactionCallback;
22 class nsIEffectiveTLDService;
24 namespace mozilla {
25 namespace net {
27 class CookiePersistentStorage final : public CookieStorage {
28 public:
29 // Result codes for TryInitDB() and Read().
30 enum OpenDBResult { RESULT_OK, RESULT_RETRY, RESULT_FAILURE };
32 static already_AddRefed<CookiePersistentStorage> Create();
34 void HandleCorruptDB();
36 void RemoveCookiesWithOriginAttributes(
37 const OriginAttributesPattern& aPattern,
38 const nsACString& aBaseDomain) override;
40 void RemoveCookiesFromExactHost(
41 const nsACString& aHost, const nsACString& aBaseDomain,
42 const OriginAttributesPattern& aPattern) override;
44 void StaleCookies(const nsTArray<Cookie*>& aCookieList,
45 int64_t aCurrentTimeInUsec) override;
47 void Close() override;
49 void EnsureInitialized() override;
51 void CleanupCachedStatements();
52 void CleanupDBConnection();
54 void Activate();
56 void RebuildCorruptDB();
57 void HandleDBClosed();
59 nsresult RunInTransaction(nsICookieTransactionCallback* aCallback) override;
61 // State of the database connection.
62 enum CorruptFlag {
63 OK, // normal
64 CLOSING_FOR_REBUILD, // corruption detected, connection closing
65 REBUILDING // close complete, rebuilding database from memory
68 CorruptFlag GetCorruptFlag() const { return mCorruptFlag; }
70 void SetCorruptFlag(CorruptFlag aFlag) { mCorruptFlag = aFlag; }
72 protected:
73 const char* NotificationTopic() const override { return "cookie-changed"; }
75 void NotifyChangedInternal(nsICookieNotification* aNotification,
76 bool aOldCookieIsSession) override;
78 void RemoveAllInternal() override;
80 void RemoveCookieFromDB(const Cookie& aCookie) override;
82 void StoreCookie(const nsACString& aBaseDomain,
83 const OriginAttributes& aOriginAttributes,
84 Cookie* aCookie) override;
86 private:
87 CookiePersistentStorage();
89 static void UpdateCookieInList(Cookie* aCookie, int64_t aLastAccessed,
90 mozIStorageBindingParamsArray* aParamsArray);
92 void PrepareCookieRemoval(const Cookie& aCookie,
93 mozIStorageBindingParamsArray* aParamsArray);
95 void InitDBConn();
96 nsresult InitDBConnInternal();
98 OpenDBResult TryInitDB(bool aRecreateDB);
99 OpenDBResult Read();
101 nsresult CreateTableWorker(const char* aName);
102 nsresult CreateTable();
103 nsresult CreateTableForSchemaVersion6();
104 nsresult CreateTableForSchemaVersion5();
106 static UniquePtr<CookieStruct> GetCookieFromRow(mozIStorageStatement* aRow);
108 already_AddRefed<nsIArray> PurgeCookies(int64_t aCurrentTimeInUsec,
109 uint16_t aMaxNumberOfCookies,
110 int64_t aCookiePurgeAge) override;
112 void DeleteFromDB(mozIStorageBindingParamsArray* aParamsArray);
114 void MaybeStoreCookiesToDB(mozIStorageBindingParamsArray* aParamsArray);
116 nsCOMPtr<nsIThread> mThread;
117 nsCOMPtr<mozIStorageService> mStorageService;
118 nsCOMPtr<nsIEffectiveTLDService> mTLDService;
120 // encapsulates a (key, Cookie) tuple for temporary storage purposes.
121 struct CookieDomainTuple {
122 CookieKey key;
123 OriginAttributes originAttributes;
124 UniquePtr<CookieStruct> cookie;
127 // thread
128 TimeStamp mEndInitDBConn;
129 nsTArray<CookieDomainTuple> mReadArray;
131 Monitor mMonitor MOZ_UNANNOTATED;
133 Atomic<bool> mInitialized;
134 Atomic<bool> mInitializedDBConn;
136 nsCOMPtr<nsIFile> mCookieFile;
137 nsCOMPtr<mozIStorageConnection> mDBConn;
138 nsCOMPtr<mozIStorageAsyncStatement> mStmtInsert;
139 nsCOMPtr<mozIStorageAsyncStatement> mStmtDelete;
140 nsCOMPtr<mozIStorageAsyncStatement> mStmtUpdate;
142 CorruptFlag mCorruptFlag;
144 // Various parts representing asynchronous read state. These are useful
145 // while the background read is taking place.
146 nsCOMPtr<mozIStorageConnection> mSyncConn;
148 // DB completion handlers.
149 nsCOMPtr<mozIStorageStatementCallback> mInsertListener;
150 nsCOMPtr<mozIStorageStatementCallback> mUpdateListener;
151 nsCOMPtr<mozIStorageStatementCallback> mRemoveListener;
152 nsCOMPtr<mozIStorageCompletionCallback> mCloseListener;
155 } // namespace net
156 } // namespace mozilla
158 #endif // mozilla_net_CookiePersistentStorage_h