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"
15 #include "nsHashKeys.h"
16 #include "nsInterfaceHashtable.h"
17 #include "nsISupportsImpl.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
{
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
{
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;
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
;
64 MOZ_IMPLICIT
BorrowedStatement(NotNull
<mozIStorageStatement
*> aStatement
)
65 : mozStorageStatementScoper(aStatement
) {}
71 void AssertIsOnConnectionThread() const {
72 #ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
73 mOwningEventTarget
->AssertOwnership(
74 "CachingDatabaseConnection not thread-safe");
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()));
107 nsresult
ExecuteCachedStatement(const nsACString
& aQuery
);
109 template <typename BindFunctor
>
110 Result
<Maybe
<BorrowedStatement
>, nsresult
>
111 BorrowAndExecuteSingleStepStatement(const nsACString
& aQuery
,
112 BindFunctor
&& aBindFunctor
);
115 ~CachingDatabaseConnection() {
116 MOZ_ASSERT(!mStorageConnection
);
117 MOZ_ASSERT(!mCachedStatements
.Count());
122 explicit CachingDatabaseConnection(
123 MovingNotNull
<nsCOMPtr
<mozIStorageConnection
>> aStorageConnection
);
125 CachingDatabaseConnection() = default;
128 MovingNotNull
<nsCOMPtr
<mozIStorageConnection
>> aStorageConnection
);
133 #ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
134 LazyInitializedOnce
<const nsAutoOwningEventTarget
> mOwningEventTarget
;
137 LazyInitializedOnceEarlyDestructible
<
138 const NotNull
<nsCOMPtr
<mozIStorageConnection
>>>
140 nsInterfaceHashtable
<nsCStringHashKey
, mozIStorageStatement
>
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
155 CachingDatabaseConnection
* mDEBUGConnection
;
159 #if defined(DEBUG) || defined(NS_BUILD_REFCNT_LOGGING)
163 CachedStatement() = default;
166 void AssertIsOnConnectionThread() const;
168 explicit operator bool() const;
170 BorrowedStatement
Borrow() const;
173 // Only called by CachingDatabaseConnection.
174 CachedStatement(CachingDatabaseConnection
* aConnection
,
175 nsCOMPtr
<mozIStorageStatement
> aStatement
,
176 const nsACString
& aQuery
);
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
))
188 mDEBUGConnection(aOther
.mDEBUGConnection
)
191 MOZ_COUNT_CTOR(CachingDatabaseConnection::CachedStatement
);
194 CachedStatement(CachedStatement
&&) = default;
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
{
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
));
220 const bool& hasResult
,
221 MOZ_TO_RESULT_INVOKE_MEMBER(&*borrowedStatement
, ExecuteStep
));
223 return hasResult
? Some(std::move(borrowedStatement
)) : Nothing
{};
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_