Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / dom / quota / CachingDatabaseConnection.h
blobf88eeae0f7a2b00c40a2b96e1e32dd5b98f34495
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_QUOTA_CACHINGDATABASECONNECTION_H_
8 #define DOM_QUOTA_CACHINGDATABASECONNECTION_H_
10 #include "mozilla/dom/quota/Config.h"
12 #include "mozStorageHelper.h"
13 #include "nsCOMPtr.h"
14 #include "nscore.h"
15 #include "nsHashKeys.h"
16 #include "nsInterfaceHashtable.h"
17 #include "nsISupportsImpl.h"
18 #include "nsString.h"
19 #include "mozilla/Assertions.h"
20 #include "mozilla/Atomics.h"
21 #include "mozilla/Attributes.h"
22 #include "mozilla/InitializedOnce.h"
23 #include "mozilla/NotNull.h"
24 #include "mozilla/dom/quota/QuotaCommon.h"
25 #include "mozilla/dom/quota/ResultExtensions.h"
26 #include "mozilla/dom/quota/ScopedLogExtraInfo.h"
28 namespace mozilla::dom::quota {
30 class CachingDatabaseConnection {
31 public:
32 class CachedStatement;
34 // A stack-only RAII wrapper that resets its borrowed statement when the
35 // wrapper goes out of scope. Note it's intentionally not declared MOZ_RAII,
36 // because it actually is used as a temporary in simple cases like
37 // `stmt.Borrow()->Execute()`. It also automatically exposes the current query
38 // to ScopedLogExtraInfo as "query" in builds where this mechanism is active.
39 class MOZ_STACK_CLASS BorrowedStatement : mozStorageStatementScoper {
40 public:
41 mozIStorageStatement& operator*() const;
43 MOZ_NONNULL_RETURN mozIStorageStatement* operator->() const
44 MOZ_NO_ADDREF_RELEASE_ON_RETURN;
46 BorrowedStatement(BorrowedStatement&& aOther) = default;
48 // No funny business allowed.
49 BorrowedStatement& operator=(BorrowedStatement&&) = delete;
50 BorrowedStatement(const BorrowedStatement&) = delete;
51 BorrowedStatement& operator=(const BorrowedStatement&) = delete;
53 private:
54 friend class CachedStatement;
56 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
57 BorrowedStatement(NotNull<mozIStorageStatement*> aStatement,
58 const nsACString& aQuery)
59 : mozStorageStatementScoper(aStatement),
60 mExtraInfo{ScopedLogExtraInfo::kTagQueryTainted, aQuery} {}
62 ScopedLogExtraInfo mExtraInfo;
63 #else
64 MOZ_IMPLICIT BorrowedStatement(NotNull<mozIStorageStatement*> aStatement)
65 : mozStorageStatementScoper(aStatement) {}
66 #endif
69 class LazyStatement;
71 void AssertIsOnConnectionThread() const {
72 #ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
73 mOwningEventTarget->AssertOwnership(
74 "CachingDatabaseConnection not thread-safe");
75 #endif
78 bool HasStorageConnection() const {
79 return static_cast<bool>(mStorageConnection);
82 mozIStorageConnection& MutableStorageConnection() const {
83 AssertIsOnConnectionThread();
84 MOZ_ASSERT(mStorageConnection);
86 return **mStorageConnection;
89 bool Closed() const { return mClosed; }
91 Result<CachedStatement, nsresult> GetCachedStatement(
92 const nsACString& aQuery);
94 Result<BorrowedStatement, nsresult> BorrowCachedStatement(
95 const nsACString& aQuery);
97 template <typename BindFunctor>
98 nsresult ExecuteCachedStatement(const nsACString& aQuery,
99 BindFunctor&& aBindFunctor) {
100 QM_TRY_INSPECT(const auto& stmt, BorrowCachedStatement(aQuery));
101 QM_TRY(std::forward<BindFunctor>(aBindFunctor)(*stmt));
102 QM_TRY(MOZ_TO_RESULT(stmt->Execute()));
104 return NS_OK;
107 nsresult ExecuteCachedStatement(const nsACString& aQuery);
109 template <typename BindFunctor>
110 Result<Maybe<BorrowedStatement>, nsresult>
111 BorrowAndExecuteSingleStepStatement(const nsACString& aQuery,
112 BindFunctor&& aBindFunctor);
114 #ifdef DEBUG
115 ~CachingDatabaseConnection() {
116 MOZ_ASSERT(!mStorageConnection);
117 MOZ_ASSERT(!mCachedStatements.Count());
119 #endif
121 protected:
122 explicit CachingDatabaseConnection(
123 MovingNotNull<nsCOMPtr<mozIStorageConnection>> aStorageConnection);
125 CachingDatabaseConnection() = default;
127 void LazyInit(
128 MovingNotNull<nsCOMPtr<mozIStorageConnection>> aStorageConnection);
130 void Close();
132 private:
133 #ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
134 LazyInitializedOnce<const nsAutoOwningEventTarget> mOwningEventTarget;
135 #endif
137 LazyInitializedOnceEarlyDestructible<
138 const NotNull<nsCOMPtr<mozIStorageConnection>>>
139 mStorageConnection;
140 nsInterfaceHashtable<nsCStringHashKey, mozIStorageStatement>
141 mCachedStatements;
142 Atomic<bool> mClosed;
145 class CachingDatabaseConnection::CachedStatement final {
146 friend class CachingDatabaseConnection;
148 nsCOMPtr<mozIStorageStatement> mStatement;
150 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
151 nsCString mQuery;
152 #endif
154 #ifdef DEBUG
155 CachingDatabaseConnection* mDEBUGConnection;
156 #endif
158 public:
159 #if defined(DEBUG) || defined(NS_BUILD_REFCNT_LOGGING)
160 CachedStatement();
161 ~CachedStatement();
162 #else
163 CachedStatement() = default;
164 #endif
166 void AssertIsOnConnectionThread() const;
168 explicit operator bool() const;
170 BorrowedStatement Borrow() const;
172 private:
173 // Only called by CachingDatabaseConnection.
174 CachedStatement(CachingDatabaseConnection* aConnection,
175 nsCOMPtr<mozIStorageStatement> aStatement,
176 const nsACString& aQuery);
178 public:
179 #if defined(NS_BUILD_REFCNT_LOGGING)
180 CachedStatement(CachedStatement&& aOther)
181 : mStatement(std::move(aOther.mStatement))
182 # ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
184 mQuery(std::move(aOther.mQuery))
185 # endif
186 # ifdef DEBUG
188 mDEBUGConnection(aOther.mDEBUGConnection)
189 # endif
191 MOZ_COUNT_CTOR(CachingDatabaseConnection::CachedStatement);
193 #else
194 CachedStatement(CachedStatement&&) = default;
195 #endif
197 CachedStatement& operator=(CachedStatement&&) = default;
199 // No funny business allowed.
200 CachedStatement(const CachedStatement&) = delete;
201 CachedStatement& operator=(const CachedStatement&) = delete;
204 class CachingDatabaseConnection::LazyStatement final {
205 public:
206 LazyStatement(CachingDatabaseConnection& aConnection,
207 const nsACString& aQueryString)
208 : mConnection{aConnection}, mQueryString{aQueryString} {}
210 Result<CachingDatabaseConnection::BorrowedStatement, nsresult> Borrow();
212 template <typename BindFunctor>
213 Result<Maybe<CachingDatabaseConnection::BorrowedStatement>, nsresult>
214 BorrowAndExecuteSingleStep(BindFunctor&& aBindFunctor) {
215 QM_TRY_UNWRAP(auto borrowedStatement, Borrow());
217 QM_TRY(std::forward<BindFunctor>(aBindFunctor)(*borrowedStatement));
219 QM_TRY_INSPECT(
220 const bool& hasResult,
221 MOZ_TO_RESULT_INVOKE_MEMBER(&*borrowedStatement, ExecuteStep));
223 return hasResult ? Some(std::move(borrowedStatement)) : Nothing{};
226 private:
227 Result<Ok, nsresult> Initialize();
229 CachingDatabaseConnection& mConnection;
230 const nsCString mQueryString;
231 CachingDatabaseConnection::CachedStatement mCachedStatement;
234 template <typename BindFunctor>
235 Result<Maybe<CachingDatabaseConnection::BorrowedStatement>, nsresult>
236 CachingDatabaseConnection::BorrowAndExecuteSingleStepStatement(
237 const nsACString& aQuery, BindFunctor&& aBindFunctor) {
238 return LazyStatement{*this, aQuery}.BorrowAndExecuteSingleStep(
239 std::forward<BindFunctor>(aBindFunctor));
242 } // namespace mozilla::dom::quota
244 #endif // DOM_QUOTA_CACHINGDATABASECONNECTION_H_