Bug 1751069 [wpt PR 32460] - Add some basic parsing tests for viewport units, a=testonly
[gecko.git] / storage / StorageBaseStatementInternal.cpp
blob1ed9f65d9ac9952f48af8c79f7d8ae3b8d95d0c9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 sts=2 expandtab
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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "StorageBaseStatementInternal.h"
9 #include "nsProxyRelease.h"
11 #include "mozStorageBindingParamsArray.h"
12 #include "mozStorageStatementData.h"
13 #include "mozStorageAsyncStatementExecution.h"
15 namespace mozilla {
16 namespace storage {
18 ////////////////////////////////////////////////////////////////////////////////
19 //// Local Classes
21 /**
22 * Used to finalize an asynchronous statement on the background thread.
24 class AsyncStatementFinalizer : public Runnable {
25 public:
26 /**
27 * Constructor for the event.
29 * @param aStatement
30 * We need the AsyncStatement to be able to get at the sqlite3_stmt;
31 * we only access/create it on the async thread.
32 * @param aConnection
33 * We need the connection to know what thread to release the statement
34 * on. We release the statement on that thread since releasing the
35 * statement might end up releasing the connection too.
37 AsyncStatementFinalizer(StorageBaseStatementInternal* aStatement,
38 Connection* aConnection)
39 : Runnable("storage::AsyncStatementFinalizer"),
40 mStatement(aStatement),
41 mConnection(aConnection) {}
43 NS_IMETHOD Run() override {
44 if (mStatement->mAsyncStatement) {
45 sqlite3_finalize(mStatement->mAsyncStatement);
46 mStatement->mAsyncStatement = nullptr;
49 nsCOMPtr<nsIThread> targetThread(mConnection->threadOpenedOn);
50 NS_ProxyRelease("AsyncStatementFinalizer::mStatement", targetThread,
51 mStatement.forget());
52 return NS_OK;
55 private:
56 RefPtr<StorageBaseStatementInternal> mStatement;
57 RefPtr<Connection> mConnection;
60 /**
61 * Finalize a sqlite3_stmt on the background thread for a statement whose
62 * destructor was invoked and the statement was non-null.
64 class LastDitchSqliteStatementFinalizer : public Runnable {
65 public:
66 /**
67 * Event constructor.
69 * @param aConnection
70 * Used to keep the connection alive. If we failed to do this, it
71 * is possible that the statement going out of scope invoking us
72 * might have the last reference to the connection and so trigger
73 * an attempt to close the connection which is doomed to fail
74 * (because the asynchronous execution thread must exist which will
75 * trigger the failure case).
76 * @param aStatement
77 * The sqlite3_stmt to finalize. This object takes ownership /
78 * responsibility for the instance and all other references to it
79 * should be forgotten.
81 LastDitchSqliteStatementFinalizer(RefPtr<Connection>& aConnection,
82 sqlite3_stmt* aStatement)
83 : Runnable("storage::LastDitchSqliteStatementFinalizer"),
84 mConnection(aConnection),
85 mAsyncStatement(aStatement) {
86 MOZ_ASSERT(aConnection, "You must provide a Connection");
89 NS_IMETHOD Run() override {
90 (void)::sqlite3_finalize(mAsyncStatement);
91 mAsyncStatement = nullptr;
93 nsCOMPtr<nsIThread> target(mConnection->threadOpenedOn);
94 (void)::NS_ProxyRelease("LastDitchSqliteStatementFinalizer::mConnection",
95 target, mConnection.forget());
96 return NS_OK;
99 private:
100 RefPtr<Connection> mConnection;
101 sqlite3_stmt* mAsyncStatement;
104 ////////////////////////////////////////////////////////////////////////////////
105 //// StorageBaseStatementInternal
107 StorageBaseStatementInternal::StorageBaseStatementInternal()
108 : mNativeConnection(nullptr), mAsyncStatement(nullptr) {}
110 void StorageBaseStatementInternal::asyncFinalize() {
111 nsIEventTarget* target = mDBConnection->getAsyncExecutionTarget();
112 if (target) {
113 // Attempt to finalize asynchronously
114 nsCOMPtr<nsIRunnable> event =
115 new AsyncStatementFinalizer(this, mDBConnection);
117 // Dispatch. Note that dispatching can fail, typically if
118 // we have a race condition with asyncClose(). It's ok,
119 // let asyncClose() win.
120 (void)target->Dispatch(event, NS_DISPATCH_NORMAL);
122 // If we cannot get the background thread,
123 // mozStorageConnection::AsyncClose() has already been called and
124 // the statement either has been or will be cleaned up by
125 // internalClose().
128 void StorageBaseStatementInternal::destructorAsyncFinalize() {
129 if (!mAsyncStatement) return;
131 bool isOwningThread = false;
132 (void)mDBConnection->threadOpenedOn->IsOnCurrentThread(&isOwningThread);
133 if (isOwningThread) {
134 // If we are the owning thread (currently that means we're also the
135 // main thread), then we can get the async target and just dispatch
136 // to it.
137 nsIEventTarget* target = mDBConnection->getAsyncExecutionTarget();
138 if (target) {
139 nsCOMPtr<nsIRunnable> event =
140 new LastDitchSqliteStatementFinalizer(mDBConnection, mAsyncStatement);
141 (void)target->Dispatch(event, NS_DISPATCH_NORMAL);
143 } else {
144 // If we're not the owning thread, assume we're the async thread, and
145 // just run the statement.
146 nsCOMPtr<nsIRunnable> event =
147 new LastDitchSqliteStatementFinalizer(mDBConnection, mAsyncStatement);
148 (void)event->Run();
151 // We might not be able to dispatch to the background thread,
152 // presumably because it is being shutdown. Since said shutdown will
153 // finalize the statement, we just need to clean-up around here.
154 mAsyncStatement = nullptr;
157 NS_IMETHODIMP
158 StorageBaseStatementInternal::NewBindingParamsArray(
159 mozIStorageBindingParamsArray** _array) {
160 nsCOMPtr<mozIStorageBindingParamsArray> array = new BindingParamsArray(this);
161 NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
163 array.forget(_array);
164 return NS_OK;
167 NS_IMETHODIMP
168 StorageBaseStatementInternal::ExecuteAsync(
169 mozIStorageStatementCallback* aCallback,
170 mozIStoragePendingStatement** _stmt) {
171 // We used to call Connection::ExecuteAsync but it takes a
172 // mozIStorageBaseStatement signature because it is also a public API. Since
173 // our 'this' has no static concept of mozIStorageBaseStatement and Connection
174 // would just QI it back across to a StorageBaseStatementInternal and the
175 // actual logic is very simple, we now roll our own.
176 nsTArray<StatementData> stmts(1);
177 StatementData data;
178 nsresult rv = getAsynchronousStatementData(data);
179 NS_ENSURE_SUCCESS(rv, rv);
180 stmts.AppendElement(data);
182 // Dispatch to the background
183 return AsyncExecuteStatements::execute(std::move(stmts), mDBConnection,
184 mNativeConnection, aCallback, _stmt);
187 template <typename T>
188 void EscapeStringForLIKEInternal(const T& aValue,
189 const typename T::char_type aEscapeChar,
190 T& aResult) {
191 const typename T::char_type MATCH_ALL('%');
192 const typename T::char_type MATCH_ONE('_');
194 aResult.Truncate(0);
196 for (uint32_t i = 0; i < aValue.Length(); i++) {
197 if (aValue[i] == aEscapeChar || aValue[i] == MATCH_ALL ||
198 aValue[i] == MATCH_ONE) {
199 aResult += aEscapeChar;
201 aResult += aValue[i];
205 NS_IMETHODIMP
206 StorageBaseStatementInternal::EscapeStringForLIKE(const nsAString& aValue,
207 const char16_t aEscapeChar,
208 nsAString& _escapedString) {
209 EscapeStringForLIKEInternal(aValue, aEscapeChar, _escapedString);
211 return NS_OK;
214 NS_IMETHODIMP
215 StorageBaseStatementInternal::EscapeUTF8StringForLIKE(
216 const nsACString& aValue, const char aEscapeChar,
217 nsACString& _escapedString) {
218 EscapeStringForLIKEInternal(aValue, aEscapeChar, _escapedString);
220 return NS_OK;
223 } // namespace storage
224 } // namespace mozilla