Bug 1869043 remove declaration of missing CreateOrDestroyAudioTracks r=padenot
[gecko.git] / storage / mozStorageConnection.cpp
blob8cead2098df4df5c4c3f0d571a00c44544326cc9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
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 "BaseVFS.h"
8 #include "ErrorList.h"
9 #include "nsError.h"
10 #include "nsThreadUtils.h"
11 #include "nsIFile.h"
12 #include "nsIFileURL.h"
13 #include "nsIXPConnect.h"
14 #include "mozilla/AppShutdown.h"
15 #include "mozilla/CheckedInt.h"
16 #include "mozilla/Telemetry.h"
17 #include "mozilla/Mutex.h"
18 #include "mozilla/CondVar.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/ErrorNames.h"
21 #include "mozilla/Unused.h"
22 #include "mozilla/dom/quota/QuotaObject.h"
23 #include "mozilla/ScopeExit.h"
24 #include "mozilla/SpinEventLoopUntil.h"
25 #include "mozilla/StaticPrefs_storage.h"
27 #include "mozIStorageCompletionCallback.h"
28 #include "mozIStorageFunction.h"
30 #include "mozStorageAsyncStatementExecution.h"
31 #include "mozStorageSQLFunctions.h"
32 #include "mozStorageConnection.h"
33 #include "mozStorageService.h"
34 #include "mozStorageStatement.h"
35 #include "mozStorageAsyncStatement.h"
36 #include "mozStorageArgValueArray.h"
37 #include "mozStoragePrivateHelpers.h"
38 #include "mozStorageStatementData.h"
39 #include "ObfuscatingVFS.h"
40 #include "QuotaVFS.h"
41 #include "StorageBaseStatementInternal.h"
42 #include "SQLCollations.h"
43 #include "FileSystemModule.h"
44 #include "mozStorageHelper.h"
46 #include "mozilla/Assertions.h"
47 #include "mozilla/Logging.h"
48 #include "mozilla/Printf.h"
49 #include "mozilla/ProfilerLabels.h"
50 #include "mozilla/RefPtr.h"
51 #include "nsProxyRelease.h"
52 #include "nsStringFwd.h"
53 #include "nsURLHelper.h"
55 #define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
57 // Maximum size of the pages cache per connection.
58 #define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
60 mozilla::LazyLogModule gStorageLog("mozStorage");
62 // Checks that the protected code is running on the main-thread only if the
63 // connection was also opened on it.
64 #ifdef DEBUG
65 # define CHECK_MAINTHREAD_ABUSE() \
66 do { \
67 NS_WARNING_ASSERTION( \
68 eventTargetOpenedOn == GetMainThreadSerialEventTarget() || \
69 !NS_IsMainThread(), \
70 "Using Storage synchronous API on main-thread, but " \
71 "the connection was opened on another thread."); \
72 } while (0)
73 #else
74 # define CHECK_MAINTHREAD_ABUSE() \
75 do { /* Nothing */ \
76 } while (0)
77 #endif
79 namespace mozilla::storage {
81 using mozilla::dom::quota::QuotaObject;
82 using mozilla::Telemetry::AccumulateCategoricalKeyed;
83 using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN;
84 using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY;
86 namespace {
88 int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
89 if (NS_SUCCEEDED(aXPCOMResultCode)) {
90 return SQLITE_OK;
93 switch (aXPCOMResultCode) {
94 case NS_ERROR_FILE_CORRUPTED:
95 return SQLITE_CORRUPT;
96 case NS_ERROR_FILE_ACCESS_DENIED:
97 return SQLITE_CANTOPEN;
98 case NS_ERROR_STORAGE_BUSY:
99 return SQLITE_BUSY;
100 case NS_ERROR_FILE_IS_LOCKED:
101 return SQLITE_LOCKED;
102 case NS_ERROR_FILE_READ_ONLY:
103 return SQLITE_READONLY;
104 case NS_ERROR_STORAGE_IOERR:
105 return SQLITE_IOERR;
106 case NS_ERROR_FILE_NO_DEVICE_SPACE:
107 return SQLITE_FULL;
108 case NS_ERROR_OUT_OF_MEMORY:
109 return SQLITE_NOMEM;
110 case NS_ERROR_UNEXPECTED:
111 return SQLITE_MISUSE;
112 case NS_ERROR_ABORT:
113 return SQLITE_ABORT;
114 case NS_ERROR_STORAGE_CONSTRAINT:
115 return SQLITE_CONSTRAINT;
116 default:
117 return SQLITE_ERROR;
120 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!");
123 ////////////////////////////////////////////////////////////////////////////////
124 //// Variant Specialization Functions (variantToSQLiteT)
126 int sqlite3_T_int(sqlite3_context* aCtx, int aValue) {
127 ::sqlite3_result_int(aCtx, aValue);
128 return SQLITE_OK;
131 int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
132 ::sqlite3_result_int64(aCtx, aValue);
133 return SQLITE_OK;
136 int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
137 ::sqlite3_result_double(aCtx, aValue);
138 return SQLITE_OK;
141 int sqlite3_T_text(sqlite3_context* aCtx, const nsCString& aValue) {
142 CheckedInt<int32_t> length(aValue.Length());
143 if (!length.isValid()) {
144 return SQLITE_MISUSE;
146 ::sqlite3_result_text(aCtx, aValue.get(), length.value(), SQLITE_TRANSIENT);
147 return SQLITE_OK;
150 int sqlite3_T_text16(sqlite3_context* aCtx, const nsString& aValue) {
151 CheckedInt<int32_t> n_bytes =
152 CheckedInt<int32_t>(aValue.Length()) * sizeof(char16_t);
153 if (!n_bytes.isValid()) {
154 return SQLITE_MISUSE;
156 ::sqlite3_result_text16(aCtx, aValue.get(), n_bytes.value(),
157 SQLITE_TRANSIENT);
158 return SQLITE_OK;
161 int sqlite3_T_null(sqlite3_context* aCtx) {
162 ::sqlite3_result_null(aCtx);
163 return SQLITE_OK;
166 int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
167 ::sqlite3_result_blob(aCtx, aData, aSize, free);
168 return SQLITE_OK;
171 #include "variantToSQLiteT_impl.h"
173 ////////////////////////////////////////////////////////////////////////////////
174 //// Modules
176 struct Module {
177 const char* name;
178 int (*registerFunc)(sqlite3*, const char*);
181 Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
183 ////////////////////////////////////////////////////////////////////////////////
184 //// Local Functions
186 int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
187 switch (aReason) {
188 case SQLITE_TRACE_STMT: {
189 // aP is a pointer to the prepared statement.
190 sqlite3_stmt* stmt = static_cast<sqlite3_stmt*>(aP);
191 // aX is a pointer to a string containing the unexpanded SQL or a comment,
192 // starting with "--"" in case of a trigger.
193 char* expanded = static_cast<char*>(aX);
194 // Simulate what sqlite_trace was doing.
195 if (!::strncmp(expanded, "--", 2)) {
196 MOZ_LOG(gStorageLog, LogLevel::Debug,
197 ("TRACE_STMT on %p: '%s'", aClosure, expanded));
198 } else {
199 char* sql = ::sqlite3_expanded_sql(stmt);
200 MOZ_LOG(gStorageLog, LogLevel::Debug,
201 ("TRACE_STMT on %p: '%s'", aClosure, sql));
202 ::sqlite3_free(sql);
204 break;
206 case SQLITE_TRACE_PROFILE: {
207 // aX is pointer to a 64bit integer containing nanoseconds it took to
208 // execute the last command.
209 sqlite_int64 time = *(static_cast<sqlite_int64*>(aX)) / 1000000;
210 if (time > 0) {
211 MOZ_LOG(gStorageLog, LogLevel::Debug,
212 ("TRACE_TIME on %p: %lldms", aClosure, time));
214 break;
217 return 0;
220 void basicFunctionHelper(sqlite3_context* aCtx, int aArgc,
221 sqlite3_value** aArgv) {
222 void* userData = ::sqlite3_user_data(aCtx);
224 mozIStorageFunction* func = static_cast<mozIStorageFunction*>(userData);
226 RefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
227 if (!arguments) return;
229 nsCOMPtr<nsIVariant> result;
230 nsresult rv = func->OnFunctionCall(arguments, getter_AddRefs(result));
231 if (NS_FAILED(rv)) {
232 nsAutoCString errorMessage;
233 GetErrorName(rv, errorMessage);
234 errorMessage.InsertLiteral("User function returned ", 0);
235 errorMessage.Append('!');
237 NS_WARNING(errorMessage.get());
239 ::sqlite3_result_error(aCtx, errorMessage.get(), -1);
240 ::sqlite3_result_error_code(aCtx, nsresultToSQLiteResult(rv));
241 return;
243 int retcode = variantToSQLiteT(aCtx, result);
244 if (retcode != SQLITE_OK) {
245 NS_WARNING("User function returned invalid data type!");
246 ::sqlite3_result_error(aCtx, "User function returned invalid data type",
247 -1);
251 RefPtr<QuotaObject> GetQuotaObject(sqlite3_file* aFile, bool obfuscatingVFS) {
252 return obfuscatingVFS
253 ? mozilla::storage::obfsvfs::GetQuotaObjectForFile(aFile)
254 : mozilla::storage::quotavfs::GetQuotaObjectForFile(aFile);
258 * This code is heavily based on the sample at:
259 * http://www.sqlite.org/unlock_notify.html
261 class UnlockNotification {
262 public:
263 UnlockNotification()
264 : mMutex("UnlockNotification mMutex"),
265 mCondVar(mMutex, "UnlockNotification condVar"),
266 mSignaled(false) {}
268 void Wait() {
269 MutexAutoLock lock(mMutex);
270 while (!mSignaled) {
271 (void)mCondVar.Wait();
275 void Signal() {
276 MutexAutoLock lock(mMutex);
277 mSignaled = true;
278 (void)mCondVar.Notify();
281 private:
282 Mutex mMutex MOZ_UNANNOTATED;
283 CondVar mCondVar;
284 bool mSignaled;
287 void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
288 for (int i = 0; i < aArgsSize; i++) {
289 UnlockNotification* notification =
290 static_cast<UnlockNotification*>(aArgs[i]);
291 notification->Signal();
295 int WaitForUnlockNotify(sqlite3* aDatabase) {
296 UnlockNotification notification;
297 int srv =
298 ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, &notification);
299 MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
300 if (srv == SQLITE_OK) {
301 notification.Wait();
304 return srv;
307 ////////////////////////////////////////////////////////////////////////////////
308 //// Local Classes
310 class AsyncCloseConnection final : public Runnable {
311 public:
312 AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
313 nsIRunnable* aCallbackEvent)
314 : Runnable("storage::AsyncCloseConnection"),
315 mConnection(aConnection),
316 mNativeConnection(aNativeConnection),
317 mCallbackEvent(aCallbackEvent) {}
319 NS_IMETHOD Run() override {
320 // Make sure we don't dispatch to the current thread.
321 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn));
323 nsCOMPtr<nsIRunnable> event =
324 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
325 mConnection, &Connection::shutdownAsyncThread);
326 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event));
328 // Internal close.
329 (void)mConnection->internalClose(mNativeConnection);
331 // Callback
332 if (mCallbackEvent) {
333 nsCOMPtr<nsIThread> thread;
334 (void)NS_GetMainThread(getter_AddRefs(thread));
335 (void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
338 return NS_OK;
341 ~AsyncCloseConnection() override {
342 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
343 mConnection.forget());
344 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
345 mCallbackEvent.forget());
348 private:
349 RefPtr<Connection> mConnection;
350 sqlite3* mNativeConnection;
351 nsCOMPtr<nsIRunnable> mCallbackEvent;
355 * An event used to initialize the clone of a connection.
357 * Must be executed on the clone's async execution thread.
359 class AsyncInitializeClone final : public Runnable {
360 public:
362 * @param aConnection The connection being cloned.
363 * @param aClone The clone.
364 * @param aReadOnly If |true|, the clone is read only.
365 * @param aCallback A callback to trigger once initialization
366 * is complete. This event will be called on
367 * aClone->eventTargetOpenedOn.
369 AsyncInitializeClone(Connection* aConnection, Connection* aClone,
370 const bool aReadOnly,
371 mozIStorageCompletionCallback* aCallback)
372 : Runnable("storage::AsyncInitializeClone"),
373 mConnection(aConnection),
374 mClone(aClone),
375 mReadOnly(aReadOnly),
376 mCallback(aCallback) {
377 MOZ_ASSERT(NS_IsMainThread());
380 NS_IMETHOD Run() override {
381 MOZ_ASSERT(!NS_IsMainThread());
382 nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
383 if (NS_FAILED(rv)) {
384 return Dispatch(rv, nullptr);
386 return Dispatch(NS_OK,
387 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
390 private:
391 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
392 RefPtr<CallbackComplete> event =
393 new CallbackComplete(aResult, aValue, mCallback.forget());
394 return mClone->eventTargetOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
397 ~AsyncInitializeClone() override {
398 nsCOMPtr<nsIThread> thread;
399 DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
400 MOZ_ASSERT(NS_SUCCEEDED(rv));
402 // Handle ambiguous nsISupports inheritance.
403 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
404 mConnection.forget());
405 NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
407 // Generally, the callback will be released by CallbackComplete.
408 // However, if for some reason Run() is not executed, we still
409 // need to ensure that it is released here.
410 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
411 mCallback.forget());
414 RefPtr<Connection> mConnection;
415 RefPtr<Connection> mClone;
416 const bool mReadOnly;
417 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
421 * A listener for async connection closing.
423 class CloseListener final : public mozIStorageCompletionCallback {
424 public:
425 NS_DECL_ISUPPORTS
426 CloseListener() : mClosed(false) {}
428 NS_IMETHOD Complete(nsresult, nsISupports*) override {
429 mClosed = true;
430 return NS_OK;
433 bool mClosed;
435 private:
436 ~CloseListener() = default;
439 NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)
441 class AsyncVacuumEvent final : public Runnable {
442 public:
443 AsyncVacuumEvent(Connection* aConnection,
444 mozIStorageCompletionCallback* aCallback,
445 bool aUseIncremental, int32_t aSetPageSize)
446 : Runnable("storage::AsyncVacuum"),
447 mConnection(aConnection),
448 mCallback(aCallback),
449 mUseIncremental(aUseIncremental),
450 mSetPageSize(aSetPageSize),
451 mStatus(NS_ERROR_UNEXPECTED) {}
453 NS_IMETHOD Run() override {
454 // This is initially dispatched to the helper thread, then re-dispatched
455 // to the opener thread, where it will callback.
456 if (IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn)) {
457 // Send the completion event.
458 if (mCallback) {
459 mozilla::Unused << mCallback->Complete(mStatus, nullptr);
461 return NS_OK;
464 // Ensure to invoke the callback regardless of errors.
465 auto guard = MakeScopeExit([&]() {
466 mConnection->mIsStatementOnHelperThreadInterruptible = false;
467 mozilla::Unused << mConnection->eventTargetOpenedOn->Dispatch(
468 this, NS_DISPATCH_NORMAL);
471 // Get list of attached databases.
472 nsCOMPtr<mozIStorageStatement> stmt;
473 nsresult rv = mConnection->CreateStatement(MOZ_STORAGE_UNIQUIFY_QUERY_STR
474 "PRAGMA database_list"_ns,
475 getter_AddRefs(stmt));
476 NS_ENSURE_SUCCESS(rv, rv);
477 // We must accumulate names and loop through them later, otherwise VACUUM
478 // will see an ongoing statement and bail out.
479 nsTArray<nsCString> schemaNames;
480 bool hasResult = false;
481 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
482 nsAutoCString name;
483 rv = stmt->GetUTF8String(1, name);
484 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("temp")) {
485 schemaNames.AppendElement(name);
488 mStatus = NS_OK;
489 // Mark this vacuum as an interruptible operation, so it can be interrupted
490 // if the connection closes during shutdown.
491 mConnection->mIsStatementOnHelperThreadInterruptible = true;
492 for (const nsCString& schemaName : schemaNames) {
493 rv = this->Vacuum(schemaName);
494 if (NS_FAILED(rv)) {
495 // This is sub-optimal since it's only keeping the last error reason,
496 // but it will do for now.
497 mStatus = rv;
500 return mStatus;
503 nsresult Vacuum(const nsACString& aSchemaName) {
504 // Abort if we're in shutdown.
505 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
506 return NS_ERROR_ABORT;
508 int32_t removablePages = mConnection->RemovablePagesInFreeList(aSchemaName);
509 if (!removablePages) {
510 // There's no empty pages to remove, so skip this vacuum for now.
511 return NS_OK;
513 nsresult rv;
514 bool needsFullVacuum = true;
516 if (mSetPageSize) {
517 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
518 query.Append(aSchemaName);
519 query.AppendLiteral(".page_size = ");
520 query.AppendInt(mSetPageSize);
521 nsCOMPtr<mozIStorageStatement> stmt;
522 rv = mConnection->ExecuteSimpleSQL(query);
523 NS_ENSURE_SUCCESS(rv, rv);
526 // Check auto_vacuum.
528 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
529 query.Append(aSchemaName);
530 query.AppendLiteral(".auto_vacuum");
531 nsCOMPtr<mozIStorageStatement> stmt;
532 rv = mConnection->CreateStatement(query, getter_AddRefs(stmt));
533 NS_ENSURE_SUCCESS(rv, rv);
534 bool hasResult = false;
535 bool changeAutoVacuum = false;
536 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
537 bool isIncrementalVacuum = stmt->AsInt32(0) == 2;
538 changeAutoVacuum = isIncrementalVacuum != mUseIncremental;
539 if (isIncrementalVacuum && !changeAutoVacuum) {
540 needsFullVacuum = false;
543 // Changing auto_vacuum is only supported on the main schema.
544 if (aSchemaName.EqualsLiteral("main") && changeAutoVacuum) {
545 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
546 query.Append(aSchemaName);
547 query.AppendLiteral(".auto_vacuum = ");
548 query.AppendInt(mUseIncremental ? 2 : 0);
549 rv = mConnection->ExecuteSimpleSQL(query);
550 NS_ENSURE_SUCCESS(rv, rv);
554 if (needsFullVacuum) {
555 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "VACUUM ");
556 query.Append(aSchemaName);
557 rv = mConnection->ExecuteSimpleSQL(query);
558 // TODO (Bug 1818039): Report failed vacuum telemetry.
559 NS_ENSURE_SUCCESS(rv, rv);
560 } else {
561 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
562 query.Append(aSchemaName);
563 query.AppendLiteral(".incremental_vacuum(");
564 query.AppendInt(removablePages);
565 query.AppendLiteral(")");
566 rv = mConnection->ExecuteSimpleSQL(query);
567 NS_ENSURE_SUCCESS(rv, rv);
570 return NS_OK;
573 ~AsyncVacuumEvent() override {
574 NS_ReleaseOnMainThread("AsyncVacuum::mConnection", mConnection.forget());
575 NS_ReleaseOnMainThread("AsyncVacuum::mCallback", mCallback.forget());
578 private:
579 RefPtr<Connection> mConnection;
580 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
581 bool mUseIncremental;
582 int32_t mSetPageSize;
583 Atomic<nsresult> mStatus;
586 } // namespace
588 ////////////////////////////////////////////////////////////////////////////////
589 //// Connection
591 Connection::Connection(Service* aService, int aFlags,
592 ConnectionOperation aSupportedOperations,
593 const nsCString& aTelemetryFilename, bool aInterruptible,
594 bool aIgnoreLockingMode)
595 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
596 sharedDBMutex("Connection::sharedDBMutex"),
597 eventTargetOpenedOn(WrapNotNull(GetCurrentSerialEventTarget())),
598 mIsStatementOnHelperThreadInterruptible(false),
599 mDBConn(nullptr),
600 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
601 mDestroying(false),
602 mProgressHandler(nullptr),
603 mStorageService(aService),
604 mFlags(aFlags),
605 mTransactionNestingLevel(0),
606 mSupportedOperations(aSupportedOperations),
607 mInterruptible(aSupportedOperations == Connection::ASYNCHRONOUS ||
608 aInterruptible),
609 mIgnoreLockingMode(aIgnoreLockingMode),
610 mAsyncExecutionThreadShuttingDown(false),
611 mConnectionClosed(false),
612 mGrowthChunkSize(0) {
613 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
614 "Can't ignore locking for a non-readonly connection!");
615 mStorageService->registerConnection(this);
616 MOZ_ASSERT(!aTelemetryFilename.IsEmpty(),
617 "A telemetry filename should have been passed-in.");
618 mTelemetryFilename.Assign(aTelemetryFilename);
621 Connection::~Connection() {
622 // Failsafe Close() occurs in our custom Release method because of
623 // complications related to Close() potentially invoking AsyncClose() which
624 // will increment our refcount.
625 MOZ_ASSERT(!mAsyncExecutionThread,
626 "The async thread has not been shutdown properly!");
629 NS_IMPL_ADDREF(Connection)
631 NS_INTERFACE_MAP_BEGIN(Connection)
632 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
633 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
634 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
635 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
636 NS_INTERFACE_MAP_END
638 // This is identical to what NS_IMPL_RELEASE provides, but with the
639 // extra |1 == count| case.
640 NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
641 MOZ_ASSERT(0 != mRefCnt, "dup release");
642 nsrefcnt count = --mRefCnt;
643 NS_LOG_RELEASE(this, count, "Connection");
644 if (1 == count) {
645 // If the refcount went to 1, the single reference must be from
646 // gService->mConnections (in class |Service|). And the code calling
647 // Release is either:
648 // - The "user" code that had created the connection, releasing on any
649 // thread.
650 // - One of Service's getConnections() callers had acquired a strong
651 // reference to the Connection that out-lived the last "user" reference,
652 // and now that just got dropped. Note that this reference could be
653 // getting dropped on the main thread or Connection->eventTargetOpenedOn
654 // (because of the NewRunnableMethod used by minimizeMemory).
656 // Either way, we should now perform our failsafe Close() and unregister.
657 // However, we only want to do this once, and the reality is that our
658 // refcount could go back up above 1 and down again at any time if we are
659 // off the main thread and getConnections() gets called on the main thread,
660 // so we use an atomic here to do this exactly once.
661 if (mDestroying.compareExchange(false, true)) {
662 // Close the connection, dispatching to the opening event target if we're
663 // not on that event target already and that event target is still
664 // accepting runnables. We do this because it's possible we're on the main
665 // thread because of getConnections(), and we REALLY don't want to
666 // transfer I/O to the main thread if we can avoid it.
667 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
668 // This could cause SpinningSynchronousClose() to be invoked and AddRef
669 // triggered for AsyncCloseConnection's strong ref if the conn was ever
670 // use for async purposes. (Main-thread only, though.)
671 Unused << synchronousClose();
672 } else {
673 nsCOMPtr<nsIRunnable> event =
674 NewRunnableMethod("storage::Connection::synchronousClose", this,
675 &Connection::synchronousClose);
676 if (NS_FAILED(eventTargetOpenedOn->Dispatch(event.forget(),
677 NS_DISPATCH_NORMAL))) {
678 // The event target was dead and so we've just leaked our runnable.
679 // This should not happen because our non-main-thread consumers should
680 // be explicitly closing their connections, not relying on us to close
681 // them for them. (It's okay to let a statement go out of scope for
682 // automatic cleanup, but not a Connection.)
683 MOZ_ASSERT(false,
684 "Leaked Connection::synchronousClose(), ownership fail.");
685 Unused << synchronousClose();
689 // This will drop its strong reference right here, right now.
690 mStorageService->unregisterConnection(this);
692 } else if (0 == count) {
693 mRefCnt = 1; /* stabilize */
694 #if 0 /* enable this to find non-threadsafe destructors: */
695 NS_ASSERT_OWNINGTHREAD(Connection);
696 #endif
697 delete (this);
698 return 0;
700 return count;
703 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
704 int32_t* aMaxValue) {
705 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
706 int curr = 0, max = 0;
707 DebugOnly<int> rc =
708 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
709 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
710 if (aMaxValue) *aMaxValue = max;
711 return curr;
714 nsIEventTarget* Connection::getAsyncExecutionTarget() {
715 NS_ENSURE_TRUE(IsOnCurrentSerialEventTarget(eventTargetOpenedOn), nullptr);
717 // Don't return the asynchronous event target if we are shutting down.
718 if (mAsyncExecutionThreadShuttingDown) {
719 return nullptr;
722 // Create the async event target if there's none yet.
723 if (!mAsyncExecutionThread) {
724 // Names start with "sqldb:" followed by a recognizable name, like the
725 // database file name, or a specially crafted name like "memory".
726 // This name will be surfaced on https://crash-stats.mozilla.org, so any
727 // sensitive part of the file name (e.g. an URL origin) should be replaced
728 // by passing an explicit telemetryName to openDatabaseWithFileURL.
729 nsAutoCString name("sqldb:"_ns);
730 name.Append(mTelemetryFilename);
731 static nsThreadPoolNaming naming;
732 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName(name),
733 getter_AddRefs(mAsyncExecutionThread));
734 if (NS_FAILED(rv)) {
735 NS_WARNING("Failed to create async thread.");
736 return nullptr;
738 mAsyncExecutionThread->SetNameForWakeupTelemetry("mozStorage (all)"_ns);
741 return mAsyncExecutionThread;
744 void Connection::RecordOpenStatus(nsresult rv) {
745 nsCString histogramKey = mTelemetryFilename;
747 if (histogramKey.IsEmpty()) {
748 histogramKey.AssignLiteral("unknown");
751 if (NS_SUCCEEDED(rv)) {
752 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
753 return;
756 switch (rv) {
757 case NS_ERROR_FILE_CORRUPTED:
758 AccumulateCategoricalKeyed(histogramKey,
759 LABELS_SQLITE_STORE_OPEN::corrupt);
760 break;
761 case NS_ERROR_STORAGE_IOERR:
762 AccumulateCategoricalKeyed(histogramKey,
763 LABELS_SQLITE_STORE_OPEN::diskio);
764 break;
765 case NS_ERROR_FILE_ACCESS_DENIED:
766 case NS_ERROR_FILE_IS_LOCKED:
767 case NS_ERROR_FILE_READ_ONLY:
768 AccumulateCategoricalKeyed(histogramKey,
769 LABELS_SQLITE_STORE_OPEN::access);
770 break;
771 case NS_ERROR_FILE_NO_DEVICE_SPACE:
772 AccumulateCategoricalKeyed(histogramKey,
773 LABELS_SQLITE_STORE_OPEN::diskspace);
774 break;
775 default:
776 AccumulateCategoricalKeyed(histogramKey,
777 LABELS_SQLITE_STORE_OPEN::failure);
781 void Connection::RecordQueryStatus(int srv) {
782 nsCString histogramKey = mTelemetryFilename;
784 if (histogramKey.IsEmpty()) {
785 histogramKey.AssignLiteral("unknown");
788 switch (srv) {
789 case SQLITE_OK:
790 case SQLITE_ROW:
791 case SQLITE_DONE:
793 // Note that these are returned when we intentionally cancel a statement so
794 // they aren't indicating a failure.
795 case SQLITE_ABORT:
796 case SQLITE_INTERRUPT:
797 AccumulateCategoricalKeyed(histogramKey,
798 LABELS_SQLITE_STORE_QUERY::success);
799 break;
800 case SQLITE_CORRUPT:
801 case SQLITE_NOTADB:
802 AccumulateCategoricalKeyed(histogramKey,
803 LABELS_SQLITE_STORE_QUERY::corrupt);
804 break;
805 case SQLITE_PERM:
806 case SQLITE_CANTOPEN:
807 case SQLITE_LOCKED:
808 case SQLITE_READONLY:
809 AccumulateCategoricalKeyed(histogramKey,
810 LABELS_SQLITE_STORE_QUERY::access);
811 break;
812 case SQLITE_IOERR:
813 case SQLITE_NOLFS:
814 AccumulateCategoricalKeyed(histogramKey,
815 LABELS_SQLITE_STORE_QUERY::diskio);
816 break;
817 case SQLITE_FULL:
818 case SQLITE_TOOBIG:
819 AccumulateCategoricalKeyed(histogramKey,
820 LABELS_SQLITE_STORE_QUERY::diskspace);
821 break;
822 case SQLITE_CONSTRAINT:
823 case SQLITE_RANGE:
824 case SQLITE_MISMATCH:
825 case SQLITE_MISUSE:
826 AccumulateCategoricalKeyed(histogramKey,
827 LABELS_SQLITE_STORE_QUERY::misuse);
828 break;
829 case SQLITE_BUSY:
830 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_QUERY::busy);
831 break;
832 default:
833 AccumulateCategoricalKeyed(histogramKey,
834 LABELS_SQLITE_STORE_QUERY::failure);
838 nsresult Connection::initialize(const nsACString& aStorageKey,
839 const nsACString& aName) {
840 MOZ_ASSERT(aStorageKey.Equals(kMozStorageMemoryStorageKey));
841 NS_ASSERTION(!connectionReady(),
842 "Initialize called on already opened database!");
843 MOZ_ASSERT(!mIgnoreLockingMode, "Can't ignore locking on an in-memory db.");
844 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
846 mStorageKey = aStorageKey;
847 mName = aName;
849 // in memory database requested, sqlite uses a magic file name
851 const nsAutoCString path =
852 mName.IsEmpty() ? nsAutoCString(":memory:"_ns)
853 : "file:"_ns + mName + "?mode=memory&cache=shared"_ns;
855 int srv = ::sqlite3_open_v2(path.get(), &mDBConn, mFlags,
856 basevfs::GetVFSName(true));
857 if (srv != SQLITE_OK) {
858 mDBConn = nullptr;
859 nsresult rv = convertResultCode(srv);
860 RecordOpenStatus(rv);
861 return rv;
864 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
865 srv =
866 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
867 MOZ_ASSERT(srv == SQLITE_OK,
868 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
869 #endif
871 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
872 // database.
874 nsresult rv = initializeInternal();
875 RecordOpenStatus(rv);
876 NS_ENSURE_SUCCESS(rv, rv);
878 return NS_OK;
881 nsresult Connection::initialize(nsIFile* aDatabaseFile) {
882 NS_ASSERTION(aDatabaseFile, "Passed null file!");
883 NS_ASSERTION(!connectionReady(),
884 "Initialize called on already opened database!");
885 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
887 // Do not set mFileURL here since this is database does not have an associated
888 // URL.
889 mDatabaseFile = aDatabaseFile;
891 nsAutoString path;
892 nsresult rv = aDatabaseFile->GetPath(path);
893 NS_ENSURE_SUCCESS(rv, rv);
895 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
896 int srv;
897 if (mIgnoreLockingMode) {
898 exclusive = false;
899 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
900 "readonly-immutable-nolock");
901 } else {
902 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
903 basevfs::GetVFSName(exclusive));
904 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
905 // Retry without trying to get an exclusive lock.
906 exclusive = false;
907 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
908 mFlags, basevfs::GetVFSName(false));
911 if (srv != SQLITE_OK) {
912 mDBConn = nullptr;
913 rv = convertResultCode(srv);
914 RecordOpenStatus(rv);
915 return rv;
918 rv = initializeInternal();
919 if (exclusive &&
920 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
921 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
922 // some cases it may successfully open the database and then lock on the
923 // first query execution. When initializeInternal fails it closes the
924 // connection, so we can try to restart it in non-exclusive mode.
925 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
926 basevfs::GetVFSName(false));
927 if (srv == SQLITE_OK) {
928 rv = initializeInternal();
932 RecordOpenStatus(rv);
933 NS_ENSURE_SUCCESS(rv, rv);
935 return NS_OK;
938 nsresult Connection::initialize(nsIFileURL* aFileURL) {
939 NS_ASSERTION(aFileURL, "Passed null file URL!");
940 NS_ASSERTION(!connectionReady(),
941 "Initialize called on already opened database!");
942 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
944 nsCOMPtr<nsIFile> databaseFile;
945 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
946 NS_ENSURE_SUCCESS(rv, rv);
948 // Set both mDatabaseFile and mFileURL here.
949 mFileURL = aFileURL;
950 mDatabaseFile = databaseFile;
952 nsAutoCString spec;
953 rv = aFileURL->GetSpec(spec);
954 NS_ENSURE_SUCCESS(rv, rv);
956 // If there is a key specified, we need to use the obfuscating VFS.
957 nsAutoCString query;
958 rv = aFileURL->GetQuery(query);
959 NS_ENSURE_SUCCESS(rv, rv);
961 bool hasKey = false;
962 bool hasDirectoryLockId = false;
964 MOZ_ALWAYS_TRUE(URLParams::Parse(
965 query, [&hasKey, &hasDirectoryLockId](const nsAString& aName,
966 const nsAString& aValue) {
967 if (aName.EqualsLiteral("key")) {
968 hasKey = true;
969 return true;
971 if (aName.EqualsLiteral("directoryLockId")) {
972 hasDirectoryLockId = true;
973 return true;
975 return true;
976 }));
978 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
980 const char* const vfs = hasKey ? obfsvfs::GetVFSName()
981 : hasDirectoryLockId ? quotavfs::GetVFSName()
982 : basevfs::GetVFSName(exclusive);
984 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
985 if (srv != SQLITE_OK) {
986 mDBConn = nullptr;
987 rv = convertResultCode(srv);
988 RecordOpenStatus(rv);
989 return rv;
992 rv = initializeInternal();
993 RecordOpenStatus(rv);
994 NS_ENSURE_SUCCESS(rv, rv);
996 return NS_OK;
999 nsresult Connection::initializeInternal() {
1000 MOZ_ASSERT(mDBConn);
1001 auto guard = MakeScopeExit([&]() { initializeFailed(); });
1003 mConnectionClosed = false;
1005 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
1006 DebugOnly<int> srv2 =
1007 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
1008 MOZ_ASSERT(srv2 == SQLITE_OK,
1009 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
1010 #endif
1012 // Properly wrap the database handle's mutex.
1013 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
1015 // SQLite tracing can slow down queries (especially long queries)
1016 // significantly. Don't trace unless the user is actively monitoring SQLite.
1017 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1018 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
1019 tracefunc, this);
1021 MOZ_LOG(
1022 gStorageLog, LogLevel::Debug,
1023 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
1026 int64_t pageSize = Service::kDefaultPageSize;
1028 // Set page_size to the preferred default value. This is effective only if
1029 // the database has just been created, otherwise, if the database does not
1030 // use WAL journal mode, a VACUUM operation will updated its page_size.
1031 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1032 "PRAGMA page_size = ");
1033 pageSizeQuery.AppendInt(pageSize);
1034 int srv = executeSql(mDBConn, pageSizeQuery.get());
1035 if (srv != SQLITE_OK) {
1036 return convertResultCode(srv);
1039 // Setting the cache_size forces the database open, verifying if it is valid
1040 // or corrupt. So this is executed regardless it being actually needed.
1041 // The cache_size is calculated from the actual page_size, to save memory.
1042 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1043 "PRAGMA cache_size = ");
1044 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
1045 srv = executeSql(mDBConn, cacheSizeQuery.get());
1046 if (srv != SQLITE_OK) {
1047 return convertResultCode(srv);
1050 // Register our built-in SQL functions.
1051 srv = registerFunctions(mDBConn);
1052 if (srv != SQLITE_OK) {
1053 return convertResultCode(srv);
1056 // Register our built-in SQL collating sequences.
1057 srv = registerCollations(mDBConn, mStorageService);
1058 if (srv != SQLITE_OK) {
1059 return convertResultCode(srv);
1062 // Set the default synchronous value. Each consumer can switch this
1063 // accordingly to their needs.
1064 #if defined(ANDROID)
1065 // Android prefers synchronous = OFF for performance reasons.
1066 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
1067 #else
1068 // Normal is the suggested value for WAL journals.
1069 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
1070 #endif
1072 // Initialization succeeded, we can stop guarding for failures.
1073 guard.release();
1074 return NS_OK;
1077 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
1078 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1079 nsresult rv = aStorageFile
1080 ? initialize(aStorageFile)
1081 : initialize(kMozStorageMemoryStorageKey, VoidCString());
1082 if (NS_FAILED(rv)) {
1083 // Shutdown the async thread, since initialization failed.
1084 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1085 mAsyncExecutionThreadShuttingDown = true;
1086 nsCOMPtr<nsIRunnable> event =
1087 NewRunnableMethod("Connection::shutdownAsyncThread", this,
1088 &Connection::shutdownAsyncThread);
1089 Unused << NS_DispatchToMainThread(event);
1091 return rv;
1094 void Connection::initializeFailed() {
1096 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1097 mConnectionClosed = true;
1099 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
1100 mDBConn = nullptr;
1101 sharedDBMutex.destroy();
1104 nsresult Connection::databaseElementExists(
1105 enum DatabaseElementType aElementType, const nsACString& aElementName,
1106 bool* _exists) {
1107 if (!connectionReady()) {
1108 return NS_ERROR_NOT_AVAILABLE;
1110 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1111 if (NS_FAILED(rv)) {
1112 return rv;
1115 // When constructing the query, make sure to SELECT the correct db's
1116 // sqlite_master if the user is prefixing the element with a specific db. ex:
1117 // sample.test
1118 nsCString query("SELECT name FROM (SELECT * FROM ");
1119 nsDependentCSubstring element;
1120 int32_t ind = aElementName.FindChar('.');
1121 if (ind == kNotFound) {
1122 element.Assign(aElementName);
1123 } else {
1124 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
1125 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
1126 query.Append(db);
1128 query.AppendLiteral(
1129 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
1130 "'");
1132 switch (aElementType) {
1133 case INDEX:
1134 query.AppendLiteral("index");
1135 break;
1136 case TABLE:
1137 query.AppendLiteral("table");
1138 break;
1140 query.AppendLiteral("' AND name ='");
1141 query.Append(element);
1142 query.Append('\'');
1144 sqlite3_stmt* stmt;
1145 int srv = prepareStatement(mDBConn, query, &stmt);
1146 if (srv != SQLITE_OK) {
1147 RecordQueryStatus(srv);
1148 return convertResultCode(srv);
1151 srv = stepStatement(mDBConn, stmt);
1152 // we just care about the return value from step
1153 (void)::sqlite3_finalize(stmt);
1155 RecordQueryStatus(srv);
1157 if (srv == SQLITE_ROW) {
1158 *_exists = true;
1159 return NS_OK;
1161 if (srv == SQLITE_DONE) {
1162 *_exists = false;
1163 return NS_OK;
1166 return convertResultCode(srv);
1169 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
1170 sharedDBMutex.assertCurrentThreadOwns();
1172 for (const auto& data : mFunctions.Values()) {
1173 if (data.function == aInstance) {
1174 return true;
1177 return false;
1180 /* static */
1181 int Connection::sProgressHelper(void* aArg) {
1182 Connection* _this = static_cast<Connection*>(aArg);
1183 return _this->progressHandler();
1186 int Connection::progressHandler() {
1187 sharedDBMutex.assertCurrentThreadOwns();
1188 if (mProgressHandler) {
1189 bool result;
1190 nsresult rv = mProgressHandler->OnProgress(this, &result);
1191 if (NS_FAILED(rv)) return 0; // Don't break request
1192 return result ? 1 : 0;
1194 return 0;
1197 nsresult Connection::setClosedState() {
1198 // Flag that we are shutting down the async thread, so that
1199 // getAsyncExecutionTarget knows not to expose/create the async thread.
1200 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1201 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1203 mAsyncExecutionThreadShuttingDown = true;
1205 // Set the property to null before closing the connection, otherwise the
1206 // other functions in the module may try to use the connection after it is
1207 // closed.
1208 mDBConn = nullptr;
1210 return NS_OK;
1213 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1214 if (aOperationType == ASYNCHRONOUS) {
1215 // Async operations are supported for all connections, on any thread.
1216 return true;
1218 // Sync operations are supported for sync connections (on any thread), and
1219 // async connections on a background thread.
1220 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1221 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1224 nsresult Connection::ensureOperationSupported(
1225 ConnectionOperation aOperationType) {
1226 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1227 #ifdef DEBUG
1228 if (NS_IsMainThread()) {
1229 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1230 Unused << xpc->DebugDumpJSStack(false, false, false);
1232 #endif
1233 MOZ_ASSERT(false,
1234 "Don't use async connections synchronously on the main thread");
1235 return NS_ERROR_NOT_AVAILABLE;
1237 return NS_OK;
1240 bool Connection::isConnectionReadyOnThisThread() {
1241 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1242 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1243 return true;
1245 return connectionReady();
1248 bool Connection::isClosing() {
1249 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1250 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1253 bool Connection::isClosed() {
1254 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1255 return mConnectionClosed;
1258 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1260 bool Connection::isAsyncExecutionThreadAvailable() {
1261 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1262 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1265 void Connection::shutdownAsyncThread() {
1266 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1267 MOZ_ASSERT(mAsyncExecutionThread);
1268 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1270 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1271 mAsyncExecutionThread = nullptr;
1274 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1275 #ifdef DEBUG
1276 { // Make sure we have marked our async thread as shutting down.
1277 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1278 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1279 "Did not call setClosedState!");
1280 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1282 #endif // DEBUG
1284 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1285 nsAutoCString leafName(":memory");
1286 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1287 MOZ_LOG(gStorageLog, LogLevel::Debug,
1288 ("Closing connection to '%s'", leafName.get()));
1291 // At this stage, we may still have statements that need to be
1292 // finalized. Attempt to close the database connection. This will
1293 // always disconnect any virtual tables and cleanly finalize their
1294 // internal statements. Once this is done, closing may fail due to
1295 // unfinalized client statements, in which case we need to finalize
1296 // these statements and close again.
1298 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1299 mConnectionClosed = true;
1302 // Nothing else needs to be done if we don't have a connection here.
1303 if (!aNativeConnection) return NS_OK;
1305 int srv = ::sqlite3_close(aNativeConnection);
1307 if (srv == SQLITE_BUSY) {
1309 // Nothing else should change the connection or statements status until we
1310 // are done here.
1311 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1312 // We still have non-finalized statements. Finalize them.
1313 sqlite3_stmt* stmt = nullptr;
1314 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1315 MOZ_LOG(gStorageLog, LogLevel::Debug,
1316 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1317 stmt));
1319 #ifdef DEBUG
1320 SmprintfPointer msg = ::mozilla::Smprintf(
1321 "SQL statement '%s' (%p) should have been finalized before closing "
1322 "the connection",
1323 ::sqlite3_sql(stmt), stmt);
1324 NS_WARNING(msg.get());
1325 #endif // DEBUG
1327 srv = ::sqlite3_finalize(stmt);
1329 #ifdef DEBUG
1330 if (srv != SQLITE_OK) {
1331 SmprintfPointer msg = ::mozilla::Smprintf(
1332 "Could not finalize SQL statement (%p)", stmt);
1333 NS_WARNING(msg.get());
1335 #endif // DEBUG
1337 // Ensure that the loop continues properly, whether closing has
1338 // succeeded or not.
1339 if (srv == SQLITE_OK) {
1340 stmt = nullptr;
1343 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1344 // again, since Sqlite will try to acquire it.
1347 // Now that all statements have been finalized, we
1348 // should be able to close.
1349 srv = ::sqlite3_close(aNativeConnection);
1350 MOZ_ASSERT(false,
1351 "Had to forcibly close the database connection because not all "
1352 "the statements have been finalized.");
1355 if (srv == SQLITE_OK) {
1356 sharedDBMutex.destroy();
1357 } else {
1358 MOZ_ASSERT(false,
1359 "sqlite3_close failed. There are probably outstanding "
1360 "statements that are listed above!");
1363 return convertResultCode(srv);
1366 nsCString Connection::getFilename() { return mTelemetryFilename; }
1368 int Connection::stepStatement(sqlite3* aNativeConnection,
1369 sqlite3_stmt* aStatement) {
1370 MOZ_ASSERT(aStatement);
1372 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1373 ::sqlite3_sql(aStatement));
1375 bool checkedMainThread = false;
1376 TimeStamp startTime = TimeStamp::Now();
1378 // The connection may have been closed if the executing statement has been
1379 // created and cached after a call to asyncClose() but before the actual
1380 // sqlite3_close(). This usually happens when other tasks using cached
1381 // statements are asynchronously scheduled for execution and any of them ends
1382 // up after asyncClose. See bug 728653 for details.
1383 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1385 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1387 int srv;
1388 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1389 if (!checkedMainThread) {
1390 checkedMainThread = true;
1391 if (::NS_IsMainThread()) {
1392 NS_WARNING("We won't allow blocking on the main thread!");
1393 break;
1397 srv = WaitForUnlockNotify(aNativeConnection);
1398 if (srv != SQLITE_OK) {
1399 break;
1402 ::sqlite3_reset(aStatement);
1405 // Report very slow SQL statements to Telemetry
1406 TimeDuration duration = TimeStamp::Now() - startTime;
1407 const uint32_t threshold = NS_IsMainThread()
1408 ? Telemetry::kSlowSQLThresholdForMainThread
1409 : Telemetry::kSlowSQLThresholdForHelperThreads;
1410 if (duration.ToMilliseconds() >= threshold) {
1411 nsDependentCString statementString(::sqlite3_sql(aStatement));
1412 Telemetry::RecordSlowSQLStatement(
1413 statementString, mTelemetryFilename,
1414 static_cast<uint32_t>(duration.ToMilliseconds()));
1417 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1418 // Drop off the extended result bits of the result code.
1419 return srv & 0xFF;
1422 int Connection::prepareStatement(sqlite3* aNativeConnection,
1423 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1424 // We should not even try to prepare statements after the connection has
1425 // been closed.
1426 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1428 bool checkedMainThread = false;
1430 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1432 int srv;
1433 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1434 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1435 if (!checkedMainThread) {
1436 checkedMainThread = true;
1437 if (::NS_IsMainThread()) {
1438 NS_WARNING("We won't allow blocking on the main thread!");
1439 break;
1443 srv = WaitForUnlockNotify(aNativeConnection);
1444 if (srv != SQLITE_OK) {
1445 break;
1449 if (srv != SQLITE_OK) {
1450 nsCString warnMsg;
1451 warnMsg.AppendLiteral("The SQL statement '");
1452 warnMsg.Append(aSQL);
1453 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1454 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1456 #ifdef DEBUG
1457 NS_WARNING(warnMsg.get());
1458 #endif
1459 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1462 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1463 // Drop off the extended result bits of the result code.
1464 int rc = srv & 0xFF;
1465 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1466 // The callers of this function are used to only checking the return value,
1467 // so it is safer to return an error code.
1468 if (rc == SQLITE_OK && *_stmt == nullptr) {
1469 return SQLITE_MISUSE;
1472 return rc;
1475 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1476 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1478 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1480 TimeStamp startTime = TimeStamp::Now();
1481 int srv =
1482 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1483 RecordQueryStatus(srv);
1485 // Report very slow SQL statements to Telemetry
1486 TimeDuration duration = TimeStamp::Now() - startTime;
1487 const uint32_t threshold = NS_IsMainThread()
1488 ? Telemetry::kSlowSQLThresholdForMainThread
1489 : Telemetry::kSlowSQLThresholdForHelperThreads;
1490 if (duration.ToMilliseconds() >= threshold) {
1491 nsDependentCString statementString(aSqlString);
1492 Telemetry::RecordSlowSQLStatement(
1493 statementString, mTelemetryFilename,
1494 static_cast<uint32_t>(duration.ToMilliseconds()));
1497 return srv;
1500 ////////////////////////////////////////////////////////////////////////////////
1501 //// nsIInterfaceRequestor
1503 NS_IMETHODIMP
1504 Connection::GetInterface(const nsIID& aIID, void** _result) {
1505 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1506 nsIEventTarget* background = getAsyncExecutionTarget();
1507 NS_IF_ADDREF(background);
1508 *_result = background;
1509 return NS_OK;
1511 return NS_ERROR_NO_INTERFACE;
1514 ////////////////////////////////////////////////////////////////////////////////
1515 //// mozIStorageConnection
1517 NS_IMETHODIMP
1518 Connection::Close() {
1519 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1520 if (NS_FAILED(rv)) {
1521 return rv;
1523 return synchronousClose();
1526 nsresult Connection::synchronousClose() {
1527 if (!connectionReady()) {
1528 return NS_ERROR_NOT_INITIALIZED;
1531 #ifdef DEBUG
1532 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1533 // event target. We make this check outside of debug code below in
1534 // setClosedState, but this is here to be explicit.
1535 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1536 #endif // DEBUG
1538 // Make sure we have not executed any asynchronous statements.
1539 // If this fails, the mDBConn may be left open, resulting in a leak.
1540 // We'll try to finalize the pending statements and close the connection.
1541 if (isAsyncExecutionThreadAvailable()) {
1542 #ifdef DEBUG
1543 if (NS_IsMainThread()) {
1544 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1545 Unused << xpc->DebugDumpJSStack(false, false, false);
1547 #endif
1548 MOZ_ASSERT(false,
1549 "Close() was invoked on a connection that executed asynchronous "
1550 "statements. "
1551 "Should have used asyncClose().");
1552 // Try to close the database regardless, to free up resources.
1553 Unused << SpinningSynchronousClose();
1554 return NS_ERROR_UNEXPECTED;
1557 // setClosedState nullifies our connection pointer, so we take a raw pointer
1558 // off it, to pass it through the close procedure.
1559 sqlite3* nativeConn = mDBConn;
1560 nsresult rv = setClosedState();
1561 NS_ENSURE_SUCCESS(rv, rv);
1563 return internalClose(nativeConn);
1566 NS_IMETHODIMP
1567 Connection::SpinningSynchronousClose() {
1568 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1569 if (NS_FAILED(rv)) {
1570 return rv;
1572 if (!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
1573 return NS_ERROR_NOT_SAME_THREAD;
1576 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1577 // Our only existing caller will never have called close; assert if misused
1578 // so that no new callers assume this works after an AsyncClose.
1579 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1580 if (!connectionReady()) {
1581 return NS_ERROR_UNEXPECTED;
1584 RefPtr<CloseListener> listener = new CloseListener();
1585 rv = AsyncClose(listener);
1586 NS_ENSURE_SUCCESS(rv, rv);
1587 MOZ_ALWAYS_TRUE(
1588 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,
1589 [&]() { return listener->mClosed; }));
1590 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1592 return rv;
1595 NS_IMETHODIMP
1596 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1597 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1598 // Check if AsyncClose or Close were already invoked.
1599 if (!connectionReady()) {
1600 return NS_ERROR_NOT_INITIALIZED;
1602 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1603 if (NS_FAILED(rv)) {
1604 return rv;
1607 // The two relevant factors at this point are whether we have a database
1608 // connection and whether we have an async execution thread. Here's what the
1609 // states mean and how we handle them:
1611 // - (mDBConn && asyncThread): The expected case where we are either an
1612 // async connection or a sync connection that has been used asynchronously.
1613 // Either way the caller must call us and not Close(). Nothing surprising
1614 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1615 // async thread.
1617 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1618 // opened the connection synchronously and was planning to use it
1619 // asynchronously, but never got around to using it asynchronously before
1620 // needing to shutdown. This has been observed to happen for the cookie
1621 // service in a case where Firefox shuts itself down almost immediately
1622 // after startup (for unknown reasons). In the Firefox shutdown case,
1623 // we may also fail to create a new async execution thread if one does not
1624 // already exist. (nsThreadManager will refuse to create new threads when
1625 // it has already been told to shutdown.) As such, we need to handle a
1626 // failure to create the async execution thread by falling back to
1627 // synchronous Close() and also dispatching the completion callback because
1628 // at least Places likes to spin a nested event loop that depends on the
1629 // callback being invoked.
1631 // Note that we have considered not trying to spin up the async execution
1632 // thread in this case if it does not already exist, but the overhead of
1633 // thread startup (if successful) is significantly less expensive than the
1634 // worst-case potential I/O hit of synchronously closing a database when we
1635 // could close it asynchronously.
1637 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1638 // OpenAsyncDatabase encountered a problem opening the database. If it
1639 // happened in all cases AsyncInitDatabase would just shut down the thread
1640 // directly and we would avoid this case. But it doesn't, so for simplicity
1641 // and consistency AsyncCloseConnection knows how to handle this and we
1642 // act like this was the (mDBConn && asyncThread) case in this method.
1644 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1645 // Close() or AsyncClose() has already been called (at least) once. This is
1646 // undeniably a misuse case by the caller. We could optimize for this
1647 // case by adding an additional check of mAsyncExecutionThread without using
1648 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1649 // shut it down. But this complicates the method for broken caller code
1650 // whereas we're still correct and safe without the special-case.
1651 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1653 // Create our callback event if we were given a callback. This will
1654 // eventually be dispatched in all cases, even if we fall back to Close() and
1655 // the database wasn't open and we return an error. The rationale is that
1656 // no existing consumer checks our return value and several of them like to
1657 // spin nested event loops until the callback fires. Given that, it seems
1658 // preferable for us to dispatch the callback in all cases. (Except the
1659 // wrong thread misuse case we bailed on up above. But that's okay because
1660 // that is statically wrong whereas these edge cases are dynamic.)
1661 nsCOMPtr<nsIRunnable> completeEvent;
1662 if (aCallback) {
1663 completeEvent = newCompletionEvent(aCallback);
1666 if (!asyncThread) {
1667 // We were unable to create an async thread, so we need to fall back to
1668 // using normal Close(). Since there is no async thread, Close() will
1669 // not complain about that. (Close() may, however, complain if the
1670 // connection is closed, but that's okay.)
1671 if (completeEvent) {
1672 // Closing the database is more important than returning an error code
1673 // about a failure to dispatch, especially because all existing native
1674 // callers ignore our return value.
1675 Unused << NS_DispatchToMainThread(completeEvent.forget());
1677 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1678 // Return a success inconditionally here, since Close() is unlikely to fail
1679 // and we want to reassure the consumer that its callback will be invoked.
1680 return NS_OK;
1683 // If we're closing the connection during shutdown, and there is an
1684 // interruptible statement running on the helper thread, issue a
1685 // sqlite3_interrupt() to avoid crashing when that statement takes a long
1686 // time (for example a vacuum).
1687 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed) &&
1688 mInterruptible && mIsStatementOnHelperThreadInterruptible) {
1689 MOZ_ASSERT(!isClosing(), "Must not be closing, see Interrupt()");
1690 DebugOnly<nsresult> rv2 = Interrupt();
1691 MOZ_ASSERT(NS_SUCCEEDED(rv2));
1694 // setClosedState nullifies our connection pointer, so we take a raw pointer
1695 // off it, to pass it through the close procedure.
1696 sqlite3* nativeConn = mDBConn;
1697 rv = setClosedState();
1698 NS_ENSURE_SUCCESS(rv, rv);
1700 // Create and dispatch our close event to the background thread.
1701 nsCOMPtr<nsIRunnable> closeEvent =
1702 new AsyncCloseConnection(this, nativeConn, completeEvent);
1703 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1704 NS_ENSURE_SUCCESS(rv, rv);
1706 return NS_OK;
1709 NS_IMETHODIMP
1710 Connection::AsyncClone(bool aReadOnly,
1711 mozIStorageCompletionCallback* aCallback) {
1712 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1714 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1715 if (!connectionReady()) {
1716 return NS_ERROR_NOT_INITIALIZED;
1718 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1719 if (NS_FAILED(rv)) {
1720 return rv;
1722 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1724 int flags = mFlags;
1725 if (aReadOnly) {
1726 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1727 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1728 // Turn off SQLITE_OPEN_CREATE.
1729 flags = (~SQLITE_OPEN_CREATE & flags);
1732 // The cloned connection will still implement the synchronous API, but throw
1733 // if any synchronous methods are called on the main thread.
1734 RefPtr<Connection> clone =
1735 new Connection(mStorageService, flags, ASYNCHRONOUS, mTelemetryFilename);
1737 RefPtr<AsyncInitializeClone> initEvent =
1738 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1739 // Dispatch to our async thread, since the originating connection must remain
1740 // valid and open for the whole cloning process. This also ensures we are
1741 // properly serialized with a `close` operation, rather than race with it.
1742 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1743 if (!target) {
1744 return NS_ERROR_UNEXPECTED;
1746 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1749 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1750 nsresult rv;
1751 if (!mStorageKey.IsEmpty()) {
1752 rv = aClone->initialize(mStorageKey, mName);
1753 } else if (mFileURL) {
1754 rv = aClone->initialize(mFileURL);
1755 } else {
1756 rv = aClone->initialize(mDatabaseFile);
1758 if (NS_FAILED(rv)) {
1759 return rv;
1762 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1764 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1765 NS_ENSURE_SUCCESS(rv, rv);
1767 // Re-attach on-disk databases that were attached to the original connection.
1769 nsCOMPtr<mozIStorageStatement> stmt;
1770 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1771 NS_ENSURE_SUCCESS(rv, rv);
1772 bool hasResult = false;
1773 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1774 nsAutoCString name;
1775 rv = stmt->GetUTF8String(1, name);
1776 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1777 !name.EqualsLiteral("temp")) {
1778 nsCString path;
1779 rv = stmt->GetUTF8String(2, path);
1780 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1781 nsCOMPtr<mozIStorageStatement> attachStmt;
1782 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1783 getter_AddRefs(attachStmt));
1784 NS_ENSURE_SUCCESS(rv, rv);
1785 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1786 NS_ENSURE_SUCCESS(rv, rv);
1787 rv = attachStmt->Execute();
1788 NS_ENSURE_SUCCESS(rv, rv);
1794 // Copy over pragmas from the original connection.
1795 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1796 // schema ("main" and any other attached databases), and this implmentation
1797 // fails to propagate them. This is being addressed on trunk.
1798 static const char* pragmas[] = {
1799 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1800 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1801 for (auto& pragma : pragmas) {
1802 // Read-only connections just need cache_size and temp_store pragmas.
1803 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1804 ::strcmp(pragma, "temp_store") != 0) {
1805 continue;
1808 nsAutoCString pragmaQuery("PRAGMA ");
1809 pragmaQuery.Append(pragma);
1810 nsCOMPtr<mozIStorageStatement> stmt;
1811 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
1812 NS_ENSURE_SUCCESS(rv, rv);
1813 bool hasResult = false;
1814 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1815 pragmaQuery.AppendLiteral(" = ");
1816 pragmaQuery.AppendInt(stmt->AsInt32(0));
1817 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
1818 NS_ENSURE_SUCCESS(rv, rv);
1822 // Copy over temporary tables, triggers, and views from the original
1823 // connections. Entities in `sqlite_temp_master` are only visible to the
1824 // connection that created them.
1825 if (!aReadOnly) {
1826 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
1827 NS_ENSURE_SUCCESS(rv, rv);
1829 nsCOMPtr<mozIStorageStatement> stmt;
1830 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1831 "WHERE type IN ('table', 'view', "
1832 "'index', 'trigger')"),
1833 getter_AddRefs(stmt));
1834 // Propagate errors, because failing to copy triggers might cause schema
1835 // coherency issues when writing to the database from the cloned connection.
1836 NS_ENSURE_SUCCESS(rv, rv);
1837 bool hasResult = false;
1838 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1839 nsAutoCString query;
1840 rv = stmt->GetUTF8String(0, query);
1841 NS_ENSURE_SUCCESS(rv, rv);
1843 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1844 // keyword. We need to add it back, or we'll recreate temporary entities
1845 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1846 // statements, but those don't need `TEMP` keywords.
1847 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
1848 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
1849 StringBeginsWith(query, "CREATE VIEW "_ns)) {
1850 query.Replace(0, 6, "CREATE TEMP");
1853 rv = aClone->ExecuteSimpleSQL(query);
1854 NS_ENSURE_SUCCESS(rv, rv);
1857 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
1858 NS_ENSURE_SUCCESS(rv, rv);
1861 // Copy any functions that have been added to this connection.
1862 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1863 for (const auto& entry : mFunctions) {
1864 const nsACString& key = entry.GetKey();
1865 Connection::FunctionInfo data = entry.GetData();
1867 rv = aClone->CreateFunction(key, data.numArgs, data.function);
1868 if (NS_FAILED(rv)) {
1869 NS_WARNING("Failed to copy function to cloned connection");
1873 // Load SQLite extensions that were on this connection.
1874 // Copy into an array rather than holding the mutex while we load extensions.
1875 nsTArray<nsCString> loadedExtensions;
1877 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1878 AppendToArray(loadedExtensions, mLoadedExtensions);
1880 for (const auto& extension : loadedExtensions) {
1881 (void)aClone->LoadExtension(extension, nullptr);
1884 guard.release();
1885 return NS_OK;
1888 NS_IMETHODIMP
1889 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
1890 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1892 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
1894 if (!connectionReady()) {
1895 return NS_ERROR_NOT_INITIALIZED;
1897 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1898 if (NS_FAILED(rv)) {
1899 return rv;
1902 int flags = mFlags;
1903 if (aReadOnly) {
1904 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1905 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1906 // Turn off SQLITE_OPEN_CREATE.
1907 flags = (~SQLITE_OPEN_CREATE & flags);
1910 RefPtr<Connection> clone =
1911 new Connection(mStorageService, flags, mSupportedOperations,
1912 mTelemetryFilename, mInterruptible);
1914 rv = initializeClone(clone, aReadOnly);
1915 if (NS_FAILED(rv)) {
1916 return rv;
1919 NS_IF_ADDREF(*_connection = clone);
1920 return NS_OK;
1923 NS_IMETHODIMP
1924 Connection::Interrupt() {
1925 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
1926 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
1927 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1928 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
1929 IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1931 if (!connectionReady()) {
1932 return NS_ERROR_NOT_INITIALIZED;
1935 if (isClosing()) { // Closing already in asynchronous case
1936 return NS_OK;
1940 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
1941 // it is not safe to call sqlite3_interrupt() when
1942 // database connection is closed or might close before
1943 // sqlite3_interrupt() returns.
1944 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1945 if (!isClosed(lockedScope)) {
1946 MOZ_ASSERT(mDBConn);
1947 ::sqlite3_interrupt(mDBConn);
1951 return NS_OK;
1954 NS_IMETHODIMP
1955 Connection::AsyncVacuum(mozIStorageCompletionCallback* aCallback,
1956 bool aUseIncremental, int32_t aSetPageSize) {
1957 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1958 // Abort if we're shutting down.
1959 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
1960 return NS_ERROR_ABORT;
1962 // Check if AsyncClose or Close were already invoked.
1963 if (!connectionReady()) {
1964 return NS_ERROR_NOT_INITIALIZED;
1966 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1967 if (NS_FAILED(rv)) {
1968 return rv;
1970 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1971 if (!asyncThread) {
1972 return NS_ERROR_NOT_INITIALIZED;
1975 // Create and dispatch our vacuum event to the background thread.
1976 nsCOMPtr<nsIRunnable> vacuumEvent =
1977 new AsyncVacuumEvent(this, aCallback, aUseIncremental, aSetPageSize);
1978 rv = asyncThread->Dispatch(vacuumEvent, NS_DISPATCH_NORMAL);
1979 NS_ENSURE_SUCCESS(rv, rv);
1981 return NS_OK;
1984 NS_IMETHODIMP
1985 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
1986 *_defaultPageSize = Service::kDefaultPageSize;
1987 return NS_OK;
1990 NS_IMETHODIMP
1991 Connection::GetConnectionReady(bool* _ready) {
1992 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1993 *_ready = connectionReady();
1994 return NS_OK;
1997 NS_IMETHODIMP
1998 Connection::GetDatabaseFile(nsIFile** _dbFile) {
1999 if (!connectionReady()) {
2000 return NS_ERROR_NOT_INITIALIZED;
2002 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2003 if (NS_FAILED(rv)) {
2004 return rv;
2007 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
2009 return NS_OK;
2012 NS_IMETHODIMP
2013 Connection::GetLastInsertRowID(int64_t* _id) {
2014 if (!connectionReady()) {
2015 return NS_ERROR_NOT_INITIALIZED;
2017 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2018 if (NS_FAILED(rv)) {
2019 return rv;
2022 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
2023 *_id = id;
2025 return NS_OK;
2028 NS_IMETHODIMP
2029 Connection::GetAffectedRows(int32_t* _rows) {
2030 if (!connectionReady()) {
2031 return NS_ERROR_NOT_INITIALIZED;
2033 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2034 if (NS_FAILED(rv)) {
2035 return rv;
2038 *_rows = ::sqlite3_changes(mDBConn);
2040 return NS_OK;
2043 NS_IMETHODIMP
2044 Connection::GetLastError(int32_t* _error) {
2045 if (!connectionReady()) {
2046 return NS_ERROR_NOT_INITIALIZED;
2048 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2049 if (NS_FAILED(rv)) {
2050 return rv;
2053 *_error = ::sqlite3_errcode(mDBConn);
2055 return NS_OK;
2058 NS_IMETHODIMP
2059 Connection::GetLastErrorString(nsACString& _errorString) {
2060 if (!connectionReady()) {
2061 return NS_ERROR_NOT_INITIALIZED;
2063 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2064 if (NS_FAILED(rv)) {
2065 return rv;
2068 const char* serr = ::sqlite3_errmsg(mDBConn);
2069 _errorString.Assign(serr);
2071 return NS_OK;
2074 NS_IMETHODIMP
2075 Connection::GetSchemaVersion(int32_t* _version) {
2076 if (!connectionReady()) {
2077 return NS_ERROR_NOT_INITIALIZED;
2079 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2080 if (NS_FAILED(rv)) {
2081 return rv;
2084 nsCOMPtr<mozIStorageStatement> stmt;
2085 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
2086 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
2088 *_version = 0;
2089 bool hasResult;
2090 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2091 *_version = stmt->AsInt32(0);
2094 return NS_OK;
2097 NS_IMETHODIMP
2098 Connection::SetSchemaVersion(int32_t aVersion) {
2099 if (!connectionReady()) {
2100 return NS_ERROR_NOT_INITIALIZED;
2102 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2103 if (NS_FAILED(rv)) {
2104 return rv;
2107 nsAutoCString stmt("PRAGMA user_version = "_ns);
2108 stmt.AppendInt(aVersion);
2110 return ExecuteSimpleSQL(stmt);
2113 NS_IMETHODIMP
2114 Connection::CreateStatement(const nsACString& aSQLStatement,
2115 mozIStorageStatement** _stmt) {
2116 NS_ENSURE_ARG_POINTER(_stmt);
2117 if (!connectionReady()) {
2118 return NS_ERROR_NOT_INITIALIZED;
2120 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2121 if (NS_FAILED(rv)) {
2122 return rv;
2125 RefPtr<Statement> statement(new Statement());
2126 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2128 rv = statement->initialize(this, mDBConn, aSQLStatement);
2129 NS_ENSURE_SUCCESS(rv, rv);
2131 Statement* rawPtr;
2132 statement.forget(&rawPtr);
2133 *_stmt = rawPtr;
2134 return NS_OK;
2137 NS_IMETHODIMP
2138 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
2139 mozIStorageAsyncStatement** _stmt) {
2140 NS_ENSURE_ARG_POINTER(_stmt);
2141 if (!connectionReady()) {
2142 return NS_ERROR_NOT_INITIALIZED;
2144 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2145 if (NS_FAILED(rv)) {
2146 return rv;
2149 RefPtr<AsyncStatement> statement(new AsyncStatement());
2150 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2152 rv = statement->initialize(this, mDBConn, aSQLStatement);
2153 NS_ENSURE_SUCCESS(rv, rv);
2155 AsyncStatement* rawPtr;
2156 statement.forget(&rawPtr);
2157 *_stmt = rawPtr;
2158 return NS_OK;
2161 NS_IMETHODIMP
2162 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
2163 CHECK_MAINTHREAD_ABUSE();
2164 if (!connectionReady()) {
2165 return NS_ERROR_NOT_INITIALIZED;
2167 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2168 if (NS_FAILED(rv)) {
2169 return rv;
2172 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
2173 return convertResultCode(srv);
2176 NS_IMETHODIMP
2177 Connection::ExecuteAsync(
2178 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
2179 mozIStorageStatementCallback* aCallback,
2180 mozIStoragePendingStatement** _handle) {
2181 nsTArray<StatementData> stmts(aStatements.Length());
2182 for (uint32_t i = 0; i < aStatements.Length(); i++) {
2183 nsCOMPtr<StorageBaseStatementInternal> stmt =
2184 do_QueryInterface(aStatements[i]);
2185 NS_ENSURE_STATE(stmt);
2187 // Obtain our StatementData.
2188 StatementData data;
2189 nsresult rv = stmt->getAsynchronousStatementData(data);
2190 NS_ENSURE_SUCCESS(rv, rv);
2192 NS_ASSERTION(stmt->getOwner() == this,
2193 "Statement must be from this database connection!");
2195 // Now append it to our array.
2196 stmts.AppendElement(data);
2199 // Dispatch to the background
2200 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
2201 aCallback, _handle);
2204 NS_IMETHODIMP
2205 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
2206 mozIStorageStatementCallback* aCallback,
2207 mozIStoragePendingStatement** _handle) {
2208 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
2210 nsCOMPtr<mozIStorageAsyncStatement> stmt;
2211 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
2212 if (NS_FAILED(rv)) {
2213 return rv;
2216 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
2217 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
2218 if (NS_FAILED(rv)) {
2219 return rv;
2222 pendingStatement.forget(_handle);
2223 return rv;
2226 NS_IMETHODIMP
2227 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
2228 return databaseElementExists(TABLE, aTableName, _exists);
2231 NS_IMETHODIMP
2232 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2233 return databaseElementExists(INDEX, aIndexName, _exists);
2236 NS_IMETHODIMP
2237 Connection::GetTransactionInProgress(bool* _inProgress) {
2238 if (!connectionReady()) {
2239 return NS_ERROR_NOT_INITIALIZED;
2241 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2242 if (NS_FAILED(rv)) {
2243 return rv;
2246 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2247 *_inProgress = transactionInProgress(lockedScope);
2248 return NS_OK;
2251 NS_IMETHODIMP
2252 Connection::GetDefaultTransactionType(int32_t* _type) {
2253 *_type = mDefaultTransactionType;
2254 return NS_OK;
2257 NS_IMETHODIMP
2258 Connection::SetDefaultTransactionType(int32_t aType) {
2259 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2260 mDefaultTransactionType = aType;
2261 return NS_OK;
2264 NS_IMETHODIMP
2265 Connection::GetVariableLimit(int32_t* _limit) {
2266 if (!connectionReady()) {
2267 return NS_ERROR_NOT_INITIALIZED;
2269 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2270 if (limit < 0) {
2271 return NS_ERROR_UNEXPECTED;
2273 *_limit = limit;
2274 return NS_OK;
2277 NS_IMETHODIMP
2278 Connection::BeginTransaction() {
2279 if (!connectionReady()) {
2280 return NS_ERROR_NOT_INITIALIZED;
2282 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2283 if (NS_FAILED(rv)) {
2284 return rv;
2287 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2288 return beginTransactionInternal(lockedScope, mDBConn,
2289 mDefaultTransactionType);
2292 nsresult Connection::beginTransactionInternal(
2293 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2294 int32_t aTransactionType) {
2295 if (transactionInProgress(aProofOfLock)) {
2296 return NS_ERROR_FAILURE;
2298 nsresult rv;
2299 switch (aTransactionType) {
2300 case TRANSACTION_DEFERRED:
2301 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2302 break;
2303 case TRANSACTION_IMMEDIATE:
2304 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2305 break;
2306 case TRANSACTION_EXCLUSIVE:
2307 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2308 break;
2309 default:
2310 return NS_ERROR_ILLEGAL_VALUE;
2312 return rv;
2315 NS_IMETHODIMP
2316 Connection::CommitTransaction() {
2317 if (!connectionReady()) {
2318 return NS_ERROR_NOT_INITIALIZED;
2320 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2321 if (NS_FAILED(rv)) {
2322 return rv;
2325 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2326 return commitTransactionInternal(lockedScope, mDBConn);
2329 nsresult Connection::commitTransactionInternal(
2330 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2331 if (!transactionInProgress(aProofOfLock)) {
2332 return NS_ERROR_UNEXPECTED;
2334 nsresult rv =
2335 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2336 return rv;
2339 NS_IMETHODIMP
2340 Connection::RollbackTransaction() {
2341 if (!connectionReady()) {
2342 return NS_ERROR_NOT_INITIALIZED;
2344 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2345 if (NS_FAILED(rv)) {
2346 return rv;
2349 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2350 return rollbackTransactionInternal(lockedScope, mDBConn);
2353 nsresult Connection::rollbackTransactionInternal(
2354 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2355 if (!transactionInProgress(aProofOfLock)) {
2356 return NS_ERROR_UNEXPECTED;
2359 nsresult rv =
2360 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2361 return rv;
2364 NS_IMETHODIMP
2365 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2366 if (!connectionReady()) {
2367 return NS_ERROR_NOT_INITIALIZED;
2369 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2370 if (NS_FAILED(rv)) {
2371 return rv;
2374 SmprintfPointer buf =
2375 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2376 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2378 int srv = executeSql(mDBConn, buf.get());
2380 return convertResultCode(srv);
2383 NS_IMETHODIMP
2384 Connection::CreateFunction(const nsACString& aFunctionName,
2385 int32_t aNumArguments,
2386 mozIStorageFunction* aFunction) {
2387 if (!connectionReady()) {
2388 return NS_ERROR_NOT_INITIALIZED;
2390 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2391 if (NS_FAILED(rv)) {
2392 return rv;
2395 // Check to see if this function is already defined. We only check the name
2396 // because a function can be defined with the same body but different names.
2397 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2398 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2400 int srv = ::sqlite3_create_function(
2401 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2402 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2403 if (srv != SQLITE_OK) return convertResultCode(srv);
2405 FunctionInfo info = {aFunction, aNumArguments};
2406 mFunctions.InsertOrUpdate(aFunctionName, info);
2408 return NS_OK;
2411 NS_IMETHODIMP
2412 Connection::RemoveFunction(const nsACString& aFunctionName) {
2413 if (!connectionReady()) {
2414 return NS_ERROR_NOT_INITIALIZED;
2416 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2417 if (NS_FAILED(rv)) {
2418 return rv;
2421 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2422 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2424 int srv = ::sqlite3_create_function(
2425 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2426 nullptr, nullptr, nullptr, nullptr);
2427 if (srv != SQLITE_OK) return convertResultCode(srv);
2429 mFunctions.Remove(aFunctionName);
2431 return NS_OK;
2434 NS_IMETHODIMP
2435 Connection::SetProgressHandler(int32_t aGranularity,
2436 mozIStorageProgressHandler* aHandler,
2437 mozIStorageProgressHandler** _oldHandler) {
2438 if (!connectionReady()) {
2439 return NS_ERROR_NOT_INITIALIZED;
2441 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2442 if (NS_FAILED(rv)) {
2443 return rv;
2446 // Return previous one
2447 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2448 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2450 if (!aHandler || aGranularity <= 0) {
2451 aHandler = nullptr;
2452 aGranularity = 0;
2454 mProgressHandler = aHandler;
2455 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2457 return NS_OK;
2460 NS_IMETHODIMP
2461 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2462 if (!connectionReady()) {
2463 return NS_ERROR_NOT_INITIALIZED;
2465 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2466 if (NS_FAILED(rv)) {
2467 return rv;
2470 // Return previous one
2471 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2472 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2474 mProgressHandler = nullptr;
2475 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2477 return NS_OK;
2480 NS_IMETHODIMP
2481 Connection::SetGrowthIncrement(int32_t aChunkSize,
2482 const nsACString& aDatabaseName) {
2483 if (!connectionReady()) {
2484 return NS_ERROR_NOT_INITIALIZED;
2486 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2487 if (NS_FAILED(rv)) {
2488 return rv;
2491 // Bug 597215: Disk space is extremely limited on Android
2492 // so don't preallocate space. This is also not effective
2493 // on log structured file systems used by Android devices
2494 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2495 // Don't preallocate if less than 500MiB is available.
2496 int64_t bytesAvailable;
2497 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2498 NS_ENSURE_SUCCESS(rv, rv);
2499 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2500 return NS_ERROR_FILE_TOO_BIG;
2503 int srv = ::sqlite3_file_control(
2504 mDBConn,
2505 aDatabaseName.Length() ? nsPromiseFlatCString(aDatabaseName).get()
2506 : nullptr,
2507 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2508 if (srv == SQLITE_OK) {
2509 mGrowthChunkSize = aChunkSize;
2511 #endif
2512 return NS_OK;
2515 int32_t Connection::RemovablePagesInFreeList(const nsACString& aSchemaName) {
2516 int32_t freeListPagesCount = 0;
2517 if (!isConnectionReadyOnThisThread()) {
2518 MOZ_ASSERT(false, "Database connection is not ready");
2519 return freeListPagesCount;
2522 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2523 query.Append(aSchemaName);
2524 query.AppendLiteral(".freelist_count");
2525 nsCOMPtr<mozIStorageStatement> stmt;
2526 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2527 MOZ_ASSERT(NS_SUCCEEDED(rv));
2528 bool hasResult = false;
2529 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2530 freeListPagesCount = stmt->AsInt32(0);
2533 // If there's no chunk size set, any page is good to be removed.
2534 if (mGrowthChunkSize == 0 || freeListPagesCount == 0) {
2535 return freeListPagesCount;
2537 int32_t pageSize;
2539 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2540 query.Append(aSchemaName);
2541 query.AppendLiteral(".page_size");
2542 nsCOMPtr<mozIStorageStatement> stmt;
2543 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2544 MOZ_ASSERT(NS_SUCCEEDED(rv));
2545 bool hasResult = false;
2546 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2547 pageSize = stmt->AsInt32(0);
2548 } else {
2549 MOZ_ASSERT(false, "Couldn't get page_size");
2550 return 0;
2553 return std::max(0, freeListPagesCount - (mGrowthChunkSize / pageSize));
2556 NS_IMETHODIMP
2557 Connection::LoadExtension(const nsACString& aExtensionName,
2558 mozIStorageCompletionCallback* aCallback) {
2559 AUTO_PROFILER_LABEL("Connection::LoadExtension", OTHER);
2561 // This is a static list of extensions we can load.
2562 // Please use lowercase ASCII names and keep this list alphabetically ordered.
2563 static constexpr nsLiteralCString sSupportedExtensions[] = {
2564 // clang-format off
2565 "fts5"_ns,
2566 // clang-format on
2568 if (std::find(std::begin(sSupportedExtensions),
2569 std::end(sSupportedExtensions),
2570 aExtensionName) == std::end(sSupportedExtensions)) {
2571 return NS_ERROR_INVALID_ARG;
2574 if (!connectionReady()) {
2575 return NS_ERROR_NOT_INITIALIZED;
2578 int srv = ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION,
2579 1, nullptr);
2580 if (srv != SQLITE_OK) {
2581 return NS_ERROR_UNEXPECTED;
2584 // Track the loaded extension for later connection cloning operations.
2586 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2587 if (!mLoadedExtensions.EnsureInserted(aExtensionName)) {
2588 // Already loaded, bail out but issue a warning.
2589 NS_WARNING(nsPrintfCString(
2590 "Tried to register '%s' SQLite extension multiple times!",
2591 PromiseFlatCString(aExtensionName).get())
2592 .get());
2593 return NS_OK;
2597 nsAutoCString entryPoint("sqlite3_");
2598 entryPoint.Append(aExtensionName);
2599 entryPoint.AppendLiteral("_init");
2601 RefPtr<Runnable> loadTask = NS_NewRunnableFunction(
2602 "mozStorageConnection::LoadExtension",
2603 [this, self = RefPtr(this), entryPoint,
2604 callback = RefPtr(aCallback)]() mutable {
2605 MOZ_ASSERT(
2606 !NS_IsMainThread() ||
2607 (operationSupported(Connection::SYNCHRONOUS) &&
2608 eventTargetOpenedOn == GetMainThreadSerialEventTarget()),
2609 "Should happen on main-thread only for synchronous connections "
2610 "opened on the main thread");
2611 #ifdef MOZ_FOLD_LIBS
2612 int srv = ::sqlite3_load_extension(mDBConn,
2613 MOZ_DLL_PREFIX "nss3" MOZ_DLL_SUFFIX,
2614 entryPoint.get(), nullptr);
2615 #else
2616 int srv = ::sqlite3_load_extension(
2617 mDBConn, MOZ_DLL_PREFIX "mozsqlite3" MOZ_DLL_SUFFIX,
2618 entryPoint.get(), nullptr);
2619 #endif
2620 if (!callback) {
2621 return;
2623 RefPtr<Runnable> callbackTask = NS_NewRunnableFunction(
2624 "mozStorageConnection::LoadExtension_callback",
2625 [callback = std::move(callback), srv]() {
2626 (void)callback->Complete(convertResultCode(srv), nullptr);
2628 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
2629 MOZ_ALWAYS_SUCCEEDS(callbackTask->Run());
2630 } else {
2631 // Redispatch the callback to the calling thread.
2632 MOZ_ALWAYS_SUCCEEDS(eventTargetOpenedOn->Dispatch(
2633 callbackTask.forget(), NS_DISPATCH_NORMAL));
2637 if (NS_IsMainThread() && !operationSupported(Connection::SYNCHRONOUS)) {
2638 // This is a main-thread call to an async-only connection, thus we should
2639 // load the library in the helper thread.
2640 nsIEventTarget* helperThread = getAsyncExecutionTarget();
2641 if (!helperThread) {
2642 return NS_ERROR_NOT_INITIALIZED;
2644 MOZ_ALWAYS_SUCCEEDS(
2645 helperThread->Dispatch(loadTask.forget(), NS_DISPATCH_NORMAL));
2646 } else {
2647 // In any other case we just load the extension on the current thread.
2648 MOZ_ALWAYS_SUCCEEDS(loadTask->Run());
2650 return NS_OK;
2653 NS_IMETHODIMP
2654 Connection::EnableModule(const nsACString& aModuleName) {
2655 if (!connectionReady()) {
2656 return NS_ERROR_NOT_INITIALIZED;
2658 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2659 if (NS_FAILED(rv)) {
2660 return rv;
2663 for (auto& gModule : gModules) {
2664 struct Module* m = &gModule;
2665 if (aModuleName.Equals(m->name)) {
2666 int srv = m->registerFunc(mDBConn, m->name);
2667 if (srv != SQLITE_OK) return convertResultCode(srv);
2669 return NS_OK;
2673 return NS_ERROR_FAILURE;
2676 NS_IMETHODIMP
2677 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2678 QuotaObject** aJournalQuotaObject) {
2679 MOZ_ASSERT(aDatabaseQuotaObject);
2680 MOZ_ASSERT(aJournalQuotaObject);
2682 if (!connectionReady()) {
2683 return NS_ERROR_NOT_INITIALIZED;
2685 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2686 if (NS_FAILED(rv)) {
2687 return rv;
2690 sqlite3_file* file;
2691 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2692 &file);
2693 if (srv != SQLITE_OK) {
2694 return convertResultCode(srv);
2697 sqlite3_vfs* vfs;
2698 srv =
2699 ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_VFS_POINTER, &vfs);
2700 if (srv != SQLITE_OK) {
2701 return convertResultCode(srv);
2704 bool obfusactingVFS = false;
2707 const nsDependentCString vfsName{vfs->zName};
2709 if (vfsName == obfsvfs::GetVFSName()) {
2710 obfusactingVFS = true;
2711 } else if (vfsName != quotavfs::GetVFSName()) {
2712 NS_WARNING("Got unexpected vfs");
2713 return NS_ERROR_FAILURE;
2717 RefPtr<QuotaObject> databaseQuotaObject =
2718 GetQuotaObject(file, obfusactingVFS);
2719 if (NS_WARN_IF(!databaseQuotaObject)) {
2720 return NS_ERROR_FAILURE;
2723 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2724 &file);
2725 if (srv != SQLITE_OK) {
2726 return convertResultCode(srv);
2729 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObject(file, obfusactingVFS);
2730 if (NS_WARN_IF(!journalQuotaObject)) {
2731 return NS_ERROR_FAILURE;
2734 databaseQuotaObject.forget(aDatabaseQuotaObject);
2735 journalQuotaObject.forget(aJournalQuotaObject);
2736 return NS_OK;
2739 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2741 uint32_t Connection::GetTransactionNestingLevel(
2742 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2743 return mTransactionNestingLevel;
2746 uint32_t Connection::IncreaseTransactionNestingLevel(
2747 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2748 return ++mTransactionNestingLevel;
2751 uint32_t Connection::DecreaseTransactionNestingLevel(
2752 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2753 return --mTransactionNestingLevel;
2756 } // namespace mozilla::storage