Bug 1869043 assert that graph set access is main thread only r=padenot
[gecko.git] / netwerk / cookie / CookiePersistentStorage.h
blobe973c74ab61354eacf575c824c4758e4e551640d
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 CollectCookieJarSizeData() override;
114 void DeleteFromDB(mozIStorageBindingParamsArray* aParamsArray);
116 void MaybeStoreCookiesToDB(mozIStorageBindingParamsArray* aParamsArray);
118 nsCOMPtr<nsIThread> mThread;
119 nsCOMPtr<mozIStorageService> mStorageService;
120 nsCOMPtr<nsIEffectiveTLDService> mTLDService;
122 // encapsulates a (key, Cookie) tuple for temporary storage purposes.
123 struct CookieDomainTuple {
124 CookieKey key;
125 OriginAttributes originAttributes;
126 UniquePtr<CookieStruct> cookie;
129 // thread
130 TimeStamp mEndInitDBConn;
131 nsTArray<CookieDomainTuple> mReadArray;
133 Monitor mMonitor MOZ_UNANNOTATED;
135 Atomic<bool> mInitialized;
136 Atomic<bool> mInitializedDBConn;
138 nsCOMPtr<nsIFile> mCookieFile;
139 nsCOMPtr<mozIStorageConnection> mDBConn;
140 nsCOMPtr<mozIStorageAsyncStatement> mStmtInsert;
141 nsCOMPtr<mozIStorageAsyncStatement> mStmtDelete;
142 nsCOMPtr<mozIStorageAsyncStatement> mStmtUpdate;
144 CorruptFlag mCorruptFlag;
146 // Various parts representing asynchronous read state. These are useful
147 // while the background read is taking place.
148 nsCOMPtr<mozIStorageConnection> mSyncConn;
150 // DB completion handlers.
151 nsCOMPtr<mozIStorageStatementCallback> mInsertListener;
152 nsCOMPtr<mozIStorageStatementCallback> mUpdateListener;
153 nsCOMPtr<mozIStorageStatementCallback> mRemoveListener;
154 nsCOMPtr<mozIStorageCompletionCallback> mCloseListener;
157 } // namespace net
158 } // namespace mozilla
160 #endif // mozilla_net_CookiePersistentStorage_h