summaryrefslogtreecommitdiffstats
path: root/dom/quota/CachingDatabaseConnection.cpp
blob: cd07dba4e7ff401194b2c081bf8b054ac793b153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/quota/CachingDatabaseConnection.h"

#include "mozilla/ProfilerLabels.h"
#include "mozilla/ipc/BackgroundParent.h"

namespace mozilla::dom::quota {

CachingDatabaseConnection::CachingDatabaseConnection(
    MovingNotNull<nsCOMPtr<mozIStorageConnection>> aStorageConnection)
    :
#ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
      mOwningThread{nsAutoOwningThread{}},
#endif
      mStorageConnection(std::move(aStorageConnection)) {
}

void CachingDatabaseConnection::LazyInit(
    MovingNotNull<nsCOMPtr<mozIStorageConnection>> aStorageConnection) {
#ifdef MOZ_THREAD_SAFETY_OWNERSHIP_CHECKS_SUPPORTED
  mOwningThread.init();
#endif
  mStorageConnection.init(std::move(aStorageConnection));
}

Result<CachingDatabaseConnection::CachedStatement, nsresult>
CachingDatabaseConnection::GetCachedStatement(const nsACString& aQuery) {
  AssertIsOnConnectionThread();
  MOZ_ASSERT(!aQuery.IsEmpty());
  MOZ_ASSERT(mStorageConnection);

  AUTO_PROFILER_LABEL("CachingDatabaseConnection::GetCachedStatement", DOM);

  QM_TRY_UNWRAP(
      auto stmt,
      mCachedStatements.TryLookupOrInsertWith(
          aQuery, [&]() -> Result<nsCOMPtr<mozIStorageStatement>, nsresult> {
            const auto extraInfo =
                ScopedLogExtraInfo{ScopedLogExtraInfo::kTagQuery, aQuery};

            QM_TRY_RETURN(
                MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
                    nsCOMPtr<mozIStorageStatement>, **mStorageConnection,
                    CreateStatement, aQuery),
                QM_PROPAGATE,
                ([&aQuery,
                  &storageConnection = **mStorageConnection](const auto&) {
#ifdef DEBUG
                  nsCString msg;
                  MOZ_ALWAYS_SUCCEEDS(
                      storageConnection.GetLastErrorString(msg));

                  nsAutoCString error =
                      "The statement '"_ns + aQuery +
                      "' failed to compile with the error message '"_ns + msg +
                      "'."_ns;

                  NS_WARNING(error.get());
#else
                    (void)aQuery;
#endif
                }));
          }));

  return CachedStatement{this, std::move(stmt), aQuery};
}

Result<CachingDatabaseConnection::BorrowedStatement, nsresult>
CachingDatabaseConnection::BorrowCachedStatement(const nsACString& aQuery) {
  QM_TRY_UNWRAP(auto cachedStatement, GetCachedStatement(aQuery));

  return cachedStatement.Borrow();
}

nsresult CachingDatabaseConnection::ExecuteCachedStatement(
    const nsACString& aQuery) {
  return ExecuteCachedStatement(
      aQuery, [](auto&) -> Result<Ok, nsresult> { return Ok{}; });
}

void CachingDatabaseConnection::Close() {
  AssertIsOnConnectionThread();
  MOZ_ASSERT(HasStorageConnection());

  AUTO_PROFILER_LABEL("CachingDatabaseConnection::Close", DOM);

  mCachedStatements.Clear();

  MOZ_ALWAYS_SUCCEEDS((*mStorageConnection)->Close());
  mStorageConnection.destroy();
}

#if defined(DEBUG) || defined(NS_BUILD_REFCNT_LOGGING)
CachingDatabaseConnection::CachedStatement::CachedStatement()
#  ifdef DEBUG
    : mDEBUGConnection(nullptr)
#  endif
{
  AssertIsOnConnectionThread();

  MOZ_COUNT_CTOR(CachingDatabaseConnection::CachedStatement);
}

CachingDatabaseConnection::CachedStatement::~CachedStatement() {
  AssertIsOnConnectionThread();

  MOZ_COUNT_DTOR(CachingDatabaseConnection::CachedStatement);
}
#endif

CachingDatabaseConnection::CachedStatement::operator bool() const {
  AssertIsOnConnectionThread();

  return mStatement;
}

mozIStorageStatement& CachingDatabaseConnection::BorrowedStatement::operator*()
    const {
  return *operator->();
}

mozIStorageStatement* CachingDatabaseConnection::BorrowedStatement::operator->()
    const {
  MOZ_ASSERT(mStatement);

  return mStatement;
}

CachingDatabaseConnection::BorrowedStatement
CachingDatabaseConnection::CachedStatement::Borrow() const {
  AssertIsOnConnectionThread();

#ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
  return BorrowedStatement{WrapNotNull(mStatement), mQuery};
#else
  return BorrowedStatement{WrapNotNull(mStatement)};
#endif
}

CachingDatabaseConnection::CachedStatement::CachedStatement(
    CachingDatabaseConnection* aConnection,
    nsCOMPtr<mozIStorageStatement> aStatement, const nsACString& aQuery)
    : mStatement(std::move(aStatement))
#ifdef QM_SCOPED_LOG_EXTRA_INFO_ENABLED
      ,
      mQuery(aQuery)
#endif
#if defined(DEBUG)
      ,
      mDEBUGConnection(aConnection)
#endif
{
#ifdef DEBUG
  MOZ_ASSERT(aConnection);
  aConnection->AssertIsOnConnectionThread();
#endif
  MOZ_ASSERT(mStatement);
  AssertIsOnConnectionThread();

  MOZ_COUNT_CTOR(CachingDatabaseConnection::CachedStatement);
}

void CachingDatabaseConnection::CachedStatement::AssertIsOnConnectionThread()
    const {
#ifdef DEBUG
  if (mDEBUGConnection) {
    mDEBUGConnection->AssertIsOnConnectionThread();
  }
  MOZ_ASSERT(!NS_IsMainThread());
  MOZ_ASSERT(!mozilla::ipc::IsOnBackgroundThread());
#endif
}

Result<CachingDatabaseConnection::BorrowedStatement, nsresult>
CachingDatabaseConnection::LazyStatement::Borrow() {
  if (!mCachedStatement) {
    QM_TRY(Initialize());
  }

  return mCachedStatement.Borrow();
}

Result<Ok, nsresult> CachingDatabaseConnection::LazyStatement::Initialize() {
  QM_TRY_UNWRAP(mCachedStatement, mConnection.GetCachedStatement(mQueryString));
  return Ok{};
}

}  // namespace mozilla::dom::quota