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/Attributes.h"
21 #include "mozilla/InitializedOnce.h"
22 #include "mozilla/NotNull.h"
23 #include "mozilla/dom/quota/QuotaCommon.h"
24 #include "mozilla/dom/quota/ResultExtensions.h"
25 #include "mozilla/dom/quota/ScopedLogExtraInfo.h"
27 namespace mozilla::dom::quota
{
29 class CachingDatabaseConnection
{
31 class CachedStatement
;
33 // A stack-only RAII wrapper that resets its borrowed statement when the
34 // wrapper goes out of scope. Note it's intentionally not declared MOZ_RAII,
35 // because it actually is used as a temporary in simple cases like
36 // `stmt.Borrow()->Execute()`. It also automatically exposes the current query
37 // to ScopedLogExtraInfo as "query" in builds where this mechanism is active.
38 class MOZ_STACK_CLASS BorrowedStatement
: mozStorageStatementScoper
{
40 mozIStorageStatement
& operator*() const;
42 MOZ_NONNULL_RETURN mozIStorageStatement
* operator->() const
43 MOZ_NO_ADDREF_RELEASE_ON_RETURN
;
45 BorrowedStatement(BorrowedStatement
&& aOther
) = default;
47 // No funny business allowed.
48 BorrowedStatement
& operator=(BorrowedStatement
&&) = delete;
49 BorrowedStatement(const BorrowedStatement
&) = delete;
50 BorrowedStatement
& operator=(const BorrowedStatement
&) = delete;
53 friend class CachedStatement
;
55 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
56 BorrowedStatement(NotNull
<mozIStorageStatement
*> aStatement
,
57 const nsACString
& aQuery
)
58 : mozStorageStatementScoper(aStatement
),
59 mExtraInfo
{ScopedLogExtraInfo::kTagQueryTainted
, aQuery
} {}
61 ScopedLogExtraInfo mExtraInfo
;
63 MOZ_IMPLICIT
BorrowedStatement(NotNull
<mozIStorageStatement
*> aStatement
)
64 : mozStorageStatementScoper(aStatement
) {}
70 void AssertIsOnConnectionThread() const {
71 #ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
72 mOwningEventTarget
->AssertOwnership(
73 "CachingDatabaseConnection not thread-safe");
77 bool HasStorageConnection() const {
78 return static_cast<bool>(mStorageConnection
);
81 mozIStorageConnection
& MutableStorageConnection() const {
82 AssertIsOnConnectionThread();
83 MOZ_ASSERT(mStorageConnection
);
85 return **mStorageConnection
;
88 Result
<CachedStatement
, nsresult
> GetCachedStatement(
89 const nsACString
& aQuery
);
91 Result
<BorrowedStatement
, nsresult
> BorrowCachedStatement(
92 const nsACString
& aQuery
);
94 template <typename BindFunctor
>
95 nsresult
ExecuteCachedStatement(const nsACString
& aQuery
,
96 BindFunctor
&& aBindFunctor
) {
97 QM_TRY_INSPECT(const auto& stmt
, BorrowCachedStatement(aQuery
));
98 QM_TRY(std::forward
<BindFunctor
>(aBindFunctor
)(*stmt
));
99 QM_TRY(MOZ_TO_RESULT(stmt
->Execute()));
104 nsresult
ExecuteCachedStatement(const nsACString
& aQuery
);
106 template <typename BindFunctor
>
107 Result
<Maybe
<BorrowedStatement
>, nsresult
>
108 BorrowAndExecuteSingleStepStatement(const nsACString
& aQuery
,
109 BindFunctor
&& aBindFunctor
);
112 ~CachingDatabaseConnection() {
113 MOZ_ASSERT(!mStorageConnection
);
114 MOZ_ASSERT(!mCachedStatements
.Count());
119 explicit CachingDatabaseConnection(
120 MovingNotNull
<nsCOMPtr
<mozIStorageConnection
>> aStorageConnection
);
122 CachingDatabaseConnection() = default;
125 MovingNotNull
<nsCOMPtr
<mozIStorageConnection
>> aStorageConnection
);
130 #ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
131 LazyInitializedOnce
<const nsAutoOwningEventTarget
> mOwningEventTarget
;
134 LazyInitializedOnceEarlyDestructible
<
135 const NotNull
<nsCOMPtr
<mozIStorageConnection
>>>
137 nsInterfaceHashtable
<nsCStringHashKey
, mozIStorageStatement
>
141 class CachingDatabaseConnection::CachedStatement final
{
142 friend class CachingDatabaseConnection
;
144 nsCOMPtr
<mozIStorageStatement
> mStatement
;
146 #ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
151 CachingDatabaseConnection
* mDEBUGConnection
;
155 #if defined(DEBUG) || defined(NS_BUILD_REFCNT_LOGGING)
159 CachedStatement() = default;
162 void AssertIsOnConnectionThread() const;
164 explicit operator bool() const;
166 BorrowedStatement
Borrow() const;
169 // Only called by CachingDatabaseConnection.
170 CachedStatement(CachingDatabaseConnection
* aConnection
,
171 nsCOMPtr
<mozIStorageStatement
> aStatement
,
172 const nsACString
& aQuery
);
175 #if defined(NS_BUILD_REFCNT_LOGGING)
176 CachedStatement(CachedStatement
&& aOther
)
177 : mStatement(std::move(aOther
.mStatement
))
178 # ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
180 mQuery(std::move(aOther
.mQuery
))
184 mDEBUGConnection(aOther
.mDEBUGConnection
)
187 MOZ_COUNT_CTOR(CachingDatabaseConnection::CachedStatement
);
190 CachedStatement(CachedStatement
&&) = default;
193 CachedStatement
& operator=(CachedStatement
&&) = default;
195 // No funny business allowed.
196 CachedStatement(const CachedStatement
&) = delete;
197 CachedStatement
& operator=(const CachedStatement
&) = delete;
200 class CachingDatabaseConnection::LazyStatement final
{
202 LazyStatement(CachingDatabaseConnection
& aConnection
,
203 const nsACString
& aQueryString
)
204 : mConnection
{aConnection
}, mQueryString
{aQueryString
} {}
206 Result
<CachingDatabaseConnection::BorrowedStatement
, nsresult
> Borrow();
208 template <typename BindFunctor
>
209 Result
<Maybe
<CachingDatabaseConnection::BorrowedStatement
>, nsresult
>
210 BorrowAndExecuteSingleStep(BindFunctor
&& aBindFunctor
) {
211 QM_TRY_UNWRAP(auto borrowedStatement
, Borrow());
213 QM_TRY(std::forward
<BindFunctor
>(aBindFunctor
)(*borrowedStatement
));
216 const bool& hasResult
,
217 MOZ_TO_RESULT_INVOKE_MEMBER(&*borrowedStatement
, ExecuteStep
));
219 return hasResult
? Some(std::move(borrowedStatement
)) : Nothing
{};
223 Result
<Ok
, nsresult
> Initialize();
225 CachingDatabaseConnection
& mConnection
;
226 const nsCString mQueryString
;
227 CachingDatabaseConnection::CachedStatement mCachedStatement
;
230 template <typename BindFunctor
>
231 Result
<Maybe
<CachingDatabaseConnection::BorrowedStatement
>, nsresult
>
232 CachingDatabaseConnection::BorrowAndExecuteSingleStepStatement(
233 const nsACString
& aQuery
, BindFunctor
&& aBindFunctor
) {
234 return LazyStatement
{*this, aQuery
}.BorrowAndExecuteSingleStep(
235 std::forward
<BindFunctor
>(aBindFunctor
));
238 } // namespace mozilla::dom::quota
240 #endif // DOM_QUOTA_CACHINGDATABASECONNECTION_H_