Bug 1919824 - Make `NativeKey::GetFollowingCharMessage` return `false` when removed...
[gecko.git] / storage / mozStorageConnection.cpp
blob775cddbfdb8d1c12edb31c4cde69501f7830ee41
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 "nsComponentManagerUtils.h"
52 #include "nsProxyRelease.h"
53 #include "nsStringFwd.h"
54 #include "nsURLHelper.h"
56 #define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
58 // Maximum size of the pages cache per connection.
59 #define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
61 mozilla::LazyLogModule gStorageLog("mozStorage");
63 // Checks that the protected code is running on the main-thread only if the
64 // connection was also opened on it.
65 #ifdef DEBUG
66 # define CHECK_MAINTHREAD_ABUSE() \
67 do { \
68 NS_WARNING_ASSERTION( \
69 eventTargetOpenedOn == GetMainThreadSerialEventTarget() || \
70 !NS_IsMainThread(), \
71 "Using Storage synchronous API on main-thread, but " \
72 "the connection was opened on another thread."); \
73 } while (0)
74 #else
75 # define CHECK_MAINTHREAD_ABUSE() \
76 do { /* Nothing */ \
77 } while (0)
78 #endif
80 namespace mozilla::storage {
82 using mozilla::dom::quota::QuotaObject;
83 using mozilla::Telemetry::AccumulateCategoricalKeyed;
84 using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN;
85 using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY;
87 namespace {
89 int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
90 if (NS_SUCCEEDED(aXPCOMResultCode)) {
91 return SQLITE_OK;
94 switch (aXPCOMResultCode) {
95 case NS_ERROR_FILE_CORRUPTED:
96 return SQLITE_CORRUPT;
97 case NS_ERROR_FILE_ACCESS_DENIED:
98 return SQLITE_CANTOPEN;
99 case NS_ERROR_STORAGE_BUSY:
100 return SQLITE_BUSY;
101 case NS_ERROR_FILE_IS_LOCKED:
102 return SQLITE_LOCKED;
103 case NS_ERROR_FILE_READ_ONLY:
104 return SQLITE_READONLY;
105 case NS_ERROR_STORAGE_IOERR:
106 return SQLITE_IOERR;
107 case NS_ERROR_FILE_NO_DEVICE_SPACE:
108 return SQLITE_FULL;
109 case NS_ERROR_OUT_OF_MEMORY:
110 return SQLITE_NOMEM;
111 case NS_ERROR_UNEXPECTED:
112 return SQLITE_MISUSE;
113 case NS_ERROR_ABORT:
114 return SQLITE_ABORT;
115 case NS_ERROR_STORAGE_CONSTRAINT:
116 return SQLITE_CONSTRAINT;
117 default:
118 return SQLITE_ERROR;
121 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!");
124 ////////////////////////////////////////////////////////////////////////////////
125 //// Variant Specialization Functions (variantToSQLiteT)
127 int sqlite3_T_int(sqlite3_context* aCtx, int aValue) {
128 ::sqlite3_result_int(aCtx, aValue);
129 return SQLITE_OK;
132 int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
133 ::sqlite3_result_int64(aCtx, aValue);
134 return SQLITE_OK;
137 int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
138 ::sqlite3_result_double(aCtx, aValue);
139 return SQLITE_OK;
142 int sqlite3_T_text(sqlite3_context* aCtx, const nsCString& aValue) {
143 CheckedInt<int32_t> length(aValue.Length());
144 if (!length.isValid()) {
145 return SQLITE_MISUSE;
147 ::sqlite3_result_text(aCtx, aValue.get(), length.value(), SQLITE_TRANSIENT);
148 return SQLITE_OK;
151 int sqlite3_T_text16(sqlite3_context* aCtx, const nsString& aValue) {
152 CheckedInt<int32_t> n_bytes =
153 CheckedInt<int32_t>(aValue.Length()) * sizeof(char16_t);
154 if (!n_bytes.isValid()) {
155 return SQLITE_MISUSE;
157 ::sqlite3_result_text16(aCtx, aValue.get(), n_bytes.value(),
158 SQLITE_TRANSIENT);
159 return SQLITE_OK;
162 int sqlite3_T_null(sqlite3_context* aCtx) {
163 ::sqlite3_result_null(aCtx);
164 return SQLITE_OK;
167 int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
168 ::sqlite3_result_blob(aCtx, aData, aSize, free);
169 return SQLITE_OK;
172 #include "variantToSQLiteT_impl.h"
174 ////////////////////////////////////////////////////////////////////////////////
175 //// Modules
177 struct Module {
178 const char* name;
179 int (*registerFunc)(sqlite3*, const char*);
182 Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
184 ////////////////////////////////////////////////////////////////////////////////
185 //// Local Functions
187 int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
188 switch (aReason) {
189 case SQLITE_TRACE_STMT: {
190 // aP is a pointer to the prepared statement.
191 sqlite3_stmt* stmt = static_cast<sqlite3_stmt*>(aP);
192 // aX is a pointer to a string containing the unexpanded SQL or a comment,
193 // starting with "--"" in case of a trigger.
194 char* expanded = static_cast<char*>(aX);
195 // Simulate what sqlite_trace was doing.
196 if (!::strncmp(expanded, "--", 2)) {
197 MOZ_LOG(gStorageLog, LogLevel::Debug,
198 ("TRACE_STMT on %p: '%s'", aClosure, expanded));
199 } else {
200 char* sql = ::sqlite3_expanded_sql(stmt);
201 MOZ_LOG(gStorageLog, LogLevel::Debug,
202 ("TRACE_STMT on %p: '%s'", aClosure, sql));
203 ::sqlite3_free(sql);
205 break;
207 case SQLITE_TRACE_PROFILE: {
208 // aX is pointer to a 64bit integer containing nanoseconds it took to
209 // execute the last command.
210 sqlite_int64 time = *(static_cast<sqlite_int64*>(aX)) / 1000000;
211 if (time > 0) {
212 MOZ_LOG(gStorageLog, LogLevel::Debug,
213 ("TRACE_TIME on %p: %lldms", aClosure, time));
215 break;
218 return 0;
221 void basicFunctionHelper(sqlite3_context* aCtx, int aArgc,
222 sqlite3_value** aArgv) {
223 void* userData = ::sqlite3_user_data(aCtx);
225 mozIStorageFunction* func = static_cast<mozIStorageFunction*>(userData);
227 RefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
228 if (!arguments) return;
230 nsCOMPtr<nsIVariant> result;
231 nsresult rv = func->OnFunctionCall(arguments, getter_AddRefs(result));
232 if (NS_FAILED(rv)) {
233 nsAutoCString errorMessage;
234 GetErrorName(rv, errorMessage);
235 errorMessage.InsertLiteral("User function returned ", 0);
236 errorMessage.Append('!');
238 NS_WARNING(errorMessage.get());
240 ::sqlite3_result_error(aCtx, errorMessage.get(), -1);
241 ::sqlite3_result_error_code(aCtx, nsresultToSQLiteResult(rv));
242 return;
244 int retcode = variantToSQLiteT(aCtx, result);
245 if (retcode != SQLITE_OK) {
246 NS_WARNING("User function returned invalid data type!");
247 ::sqlite3_result_error(aCtx, "User function returned invalid data type",
248 -1);
252 RefPtr<QuotaObject> GetQuotaObject(sqlite3_file* aFile, bool obfuscatingVFS) {
253 return obfuscatingVFS
254 ? mozilla::storage::obfsvfs::GetQuotaObjectForFile(aFile)
255 : mozilla::storage::quotavfs::GetQuotaObjectForFile(aFile);
259 * This code is heavily based on the sample at:
260 * http://www.sqlite.org/unlock_notify.html
262 class UnlockNotification {
263 public:
264 UnlockNotification()
265 : mMutex("UnlockNotification mMutex"),
266 mCondVar(mMutex, "UnlockNotification condVar"),
267 mSignaled(false) {}
269 void Wait() {
270 MutexAutoLock lock(mMutex);
271 while (!mSignaled) {
272 (void)mCondVar.Wait();
276 void Signal() {
277 MutexAutoLock lock(mMutex);
278 mSignaled = true;
279 (void)mCondVar.Notify();
282 private:
283 Mutex mMutex MOZ_UNANNOTATED;
284 CondVar mCondVar;
285 bool mSignaled;
288 void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
289 for (int i = 0; i < aArgsSize; i++) {
290 UnlockNotification* notification =
291 static_cast<UnlockNotification*>(aArgs[i]);
292 notification->Signal();
296 int WaitForUnlockNotify(sqlite3* aDatabase) {
297 UnlockNotification notification;
298 int srv =
299 ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, &notification);
300 MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
301 if (srv == SQLITE_OK) {
302 notification.Wait();
305 return srv;
308 ////////////////////////////////////////////////////////////////////////////////
309 //// Local Classes
311 class AsyncCloseConnection final : public Runnable {
312 public:
313 AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
314 nsIRunnable* aCallbackEvent)
315 : Runnable("storage::AsyncCloseConnection"),
316 mConnection(aConnection),
317 mNativeConnection(aNativeConnection),
318 mCallbackEvent(aCallbackEvent) {}
320 NS_IMETHOD Run() override {
321 // Make sure we don't dispatch to the current thread.
322 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn));
324 nsCOMPtr<nsIRunnable> event =
325 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
326 mConnection, &Connection::shutdownAsyncThread);
327 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event));
329 // Internal close.
330 (void)mConnection->internalClose(mNativeConnection);
332 // Callback
333 if (mCallbackEvent) {
334 nsCOMPtr<nsIThread> thread;
335 (void)NS_GetMainThread(getter_AddRefs(thread));
336 (void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
339 return NS_OK;
342 ~AsyncCloseConnection() override {
343 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
344 mConnection.forget());
345 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
346 mCallbackEvent.forget());
349 private:
350 RefPtr<Connection> mConnection;
351 sqlite3* mNativeConnection;
352 nsCOMPtr<nsIRunnable> mCallbackEvent;
356 * An event used to initialize the clone of a connection.
358 * Must be executed on the clone's async execution thread.
360 class AsyncInitializeClone final : public Runnable {
361 public:
363 * @param aConnection The connection being cloned.
364 * @param aClone The clone.
365 * @param aReadOnly If |true|, the clone is read only.
366 * @param aCallback A callback to trigger once initialization
367 * is complete. This event will be called on
368 * aClone->eventTargetOpenedOn.
370 AsyncInitializeClone(Connection* aConnection, Connection* aClone,
371 const bool aReadOnly,
372 mozIStorageCompletionCallback* aCallback)
373 : Runnable("storage::AsyncInitializeClone"),
374 mConnection(aConnection),
375 mClone(aClone),
376 mReadOnly(aReadOnly),
377 mCallback(aCallback) {
378 MOZ_ASSERT(NS_IsMainThread());
381 NS_IMETHOD Run() override {
382 MOZ_ASSERT(!NS_IsMainThread());
383 nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
384 if (NS_FAILED(rv)) {
385 return Dispatch(rv, nullptr);
387 return Dispatch(NS_OK,
388 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
391 private:
392 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
393 RefPtr<CallbackComplete> event =
394 new CallbackComplete(aResult, aValue, mCallback.forget());
395 return mClone->eventTargetOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
398 ~AsyncInitializeClone() override {
399 nsCOMPtr<nsIThread> thread;
400 DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
401 MOZ_ASSERT(NS_SUCCEEDED(rv));
403 // Handle ambiguous nsISupports inheritance.
404 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
405 mConnection.forget());
406 NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
408 // Generally, the callback will be released by CallbackComplete.
409 // However, if for some reason Run() is not executed, we still
410 // need to ensure that it is released here.
411 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
412 mCallback.forget());
415 RefPtr<Connection> mConnection;
416 RefPtr<Connection> mClone;
417 const bool mReadOnly;
418 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
422 * A listener for async connection closing.
424 class CloseListener final : public mozIStorageCompletionCallback {
425 public:
426 NS_DECL_ISUPPORTS
427 CloseListener() : mClosed(false) {}
429 NS_IMETHOD Complete(nsresult, nsISupports*) override {
430 mClosed = true;
431 return NS_OK;
434 bool mClosed;
436 private:
437 ~CloseListener() = default;
440 NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)
442 class AsyncVacuumEvent final : public Runnable {
443 public:
444 AsyncVacuumEvent(Connection* aConnection,
445 mozIStorageCompletionCallback* aCallback,
446 bool aUseIncremental, int32_t aSetPageSize)
447 : Runnable("storage::AsyncVacuum"),
448 mConnection(aConnection),
449 mCallback(aCallback),
450 mUseIncremental(aUseIncremental),
451 mSetPageSize(aSetPageSize),
452 mStatus(NS_ERROR_UNEXPECTED) {}
454 NS_IMETHOD Run() override {
455 // This is initially dispatched to the helper thread, then re-dispatched
456 // to the opener thread, where it will callback.
457 if (IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn)) {
458 // Send the completion event.
459 if (mCallback) {
460 mozilla::Unused << mCallback->Complete(mStatus, nullptr);
462 return NS_OK;
465 // Ensure to invoke the callback regardless of errors.
466 auto guard = MakeScopeExit([&]() {
467 mConnection->mIsStatementOnHelperThreadInterruptible = false;
468 mozilla::Unused << mConnection->eventTargetOpenedOn->Dispatch(
469 this, NS_DISPATCH_NORMAL);
472 // Get list of attached databases.
473 nsCOMPtr<mozIStorageStatement> stmt;
474 nsresult rv = mConnection->CreateStatement(MOZ_STORAGE_UNIQUIFY_QUERY_STR
475 "PRAGMA database_list"_ns,
476 getter_AddRefs(stmt));
477 NS_ENSURE_SUCCESS(rv, rv);
478 // We must accumulate names and loop through them later, otherwise VACUUM
479 // will see an ongoing statement and bail out.
480 nsTArray<nsCString> schemaNames;
481 bool hasResult = false;
482 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
483 nsAutoCString name;
484 rv = stmt->GetUTF8String(1, name);
485 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("temp")) {
486 schemaNames.AppendElement(name);
489 mStatus = NS_OK;
490 // Mark this vacuum as an interruptible operation, so it can be interrupted
491 // if the connection closes during shutdown.
492 mConnection->mIsStatementOnHelperThreadInterruptible = true;
493 for (const nsCString& schemaName : schemaNames) {
494 rv = this->Vacuum(schemaName);
495 if (NS_FAILED(rv)) {
496 // This is sub-optimal since it's only keeping the last error reason,
497 // but it will do for now.
498 mStatus = rv;
501 return mStatus;
504 nsresult Vacuum(const nsACString& aSchemaName) {
505 // Abort if we're in shutdown.
506 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
507 return NS_ERROR_ABORT;
509 int32_t removablePages = mConnection->RemovablePagesInFreeList(aSchemaName);
510 if (!removablePages) {
511 // There's no empty pages to remove, so skip this vacuum for now.
512 return NS_OK;
514 nsresult rv;
515 bool needsFullVacuum = true;
517 if (mSetPageSize) {
518 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
519 query.Append(aSchemaName);
520 query.AppendLiteral(".page_size = ");
521 query.AppendInt(mSetPageSize);
522 nsCOMPtr<mozIStorageStatement> stmt;
523 rv = mConnection->ExecuteSimpleSQL(query);
524 NS_ENSURE_SUCCESS(rv, rv);
527 // Check auto_vacuum.
529 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
530 query.Append(aSchemaName);
531 query.AppendLiteral(".auto_vacuum");
532 nsCOMPtr<mozIStorageStatement> stmt;
533 rv = mConnection->CreateStatement(query, getter_AddRefs(stmt));
534 NS_ENSURE_SUCCESS(rv, rv);
535 bool hasResult = false;
536 bool changeAutoVacuum = false;
537 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
538 bool isIncrementalVacuum = stmt->AsInt32(0) == 2;
539 changeAutoVacuum = isIncrementalVacuum != mUseIncremental;
540 if (isIncrementalVacuum && !changeAutoVacuum) {
541 needsFullVacuum = false;
544 // Changing auto_vacuum is only supported on the main schema.
545 if (aSchemaName.EqualsLiteral("main") && changeAutoVacuum) {
546 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
547 query.Append(aSchemaName);
548 query.AppendLiteral(".auto_vacuum = ");
549 query.AppendInt(mUseIncremental ? 2 : 0);
550 rv = mConnection->ExecuteSimpleSQL(query);
551 NS_ENSURE_SUCCESS(rv, rv);
555 if (needsFullVacuum) {
556 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "VACUUM ");
557 query.Append(aSchemaName);
558 rv = mConnection->ExecuteSimpleSQL(query);
559 // TODO (Bug 1818039): Report failed vacuum telemetry.
560 NS_ENSURE_SUCCESS(rv, rv);
561 } else {
562 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
563 query.Append(aSchemaName);
564 query.AppendLiteral(".incremental_vacuum(");
565 query.AppendInt(removablePages);
566 query.AppendLiteral(")");
567 rv = mConnection->ExecuteSimpleSQL(query);
568 NS_ENSURE_SUCCESS(rv, rv);
571 return NS_OK;
574 ~AsyncVacuumEvent() override {
575 NS_ReleaseOnMainThread("AsyncVacuum::mConnection", mConnection.forget());
576 NS_ReleaseOnMainThread("AsyncVacuum::mCallback", mCallback.forget());
579 private:
580 RefPtr<Connection> mConnection;
581 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
582 bool mUseIncremental;
583 int32_t mSetPageSize;
584 Atomic<nsresult> mStatus;
588 * A runnable to perform an SQLite database backup when there may be one or more
589 * open connections on that database.
591 class AsyncBackupDatabaseFile final : public Runnable, public nsITimerCallback {
592 public:
593 NS_DECL_ISUPPORTS_INHERITED
596 * @param aConnection The connection to the database being backed up.
597 * @param aNativeConnection The native connection to the database being backed
598 * up.
599 * @param aDestinationFile The destination file for the created backup.
600 * @param aCallback A callback to trigger once the backup process has
601 * completed. The callback will be supplied with an nsresult
602 * indicating whether or not the backup was successfully
603 * created. This callback will be called on the
604 * mConnection->eventTargetOpenedOn thread.
605 * @throws
607 AsyncBackupDatabaseFile(Connection* aConnection, sqlite3* aNativeConnection,
608 nsIFile* aDestinationFile,
609 mozIStorageCompletionCallback* aCallback,
610 int32_t aPagesPerStep, uint32_t aStepDelayMs)
611 : Runnable("storage::AsyncBackupDatabaseFile"),
612 mConnection(aConnection),
613 mNativeConnection(aNativeConnection),
614 mDestinationFile(aDestinationFile),
615 mCallback(aCallback),
616 mPagesPerStep(aPagesPerStep),
617 mStepDelayMs(aStepDelayMs),
618 mBackupFile(nullptr),
619 mBackupHandle(nullptr) {
620 MOZ_ASSERT(NS_IsMainThread());
623 NS_IMETHOD Run() override {
624 MOZ_ASSERT(!NS_IsMainThread());
626 nsAutoString path;
627 nsresult rv = mDestinationFile->GetPath(path);
628 if (NS_FAILED(rv)) {
629 return Dispatch(rv, nullptr);
631 // Put a .tmp on the end of the destination while the backup is underway.
632 // This extension will be stripped off after the backup successfully
633 // completes.
634 path.AppendLiteral(".tmp");
636 int srv = ::sqlite3_open(NS_ConvertUTF16toUTF8(path).get(), &mBackupFile);
637 if (srv != SQLITE_OK) {
638 return Dispatch(NS_ERROR_FAILURE, nullptr);
641 static const char* mainDBName = "main";
643 mBackupHandle = ::sqlite3_backup_init(mBackupFile, mainDBName,
644 mNativeConnection, mainDBName);
645 if (!mBackupHandle) {
646 MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK);
647 return Dispatch(NS_ERROR_FAILURE, nullptr);
650 return DoStep();
653 NS_IMETHOD
654 Notify(nsITimer* aTimer) override { return DoStep(); }
656 private:
657 nsresult DoStep() {
658 #define DISPATCH_AND_RETURN_IF_FAILED(rv) \
659 if (NS_FAILED(rv)) { \
660 return Dispatch(rv, nullptr); \
663 // This guard is used to close the backup database in the event of
664 // some failure throughout this process. We release the exit guard
665 // only if we complete the backup successfully, or defer to another
666 // later call to DoStep.
667 auto guard = MakeScopeExit([&]() {
668 MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK);
669 mBackupFile = nullptr;
672 MOZ_ASSERT(!NS_IsMainThread());
673 nsAutoString originalPath;
674 nsresult rv = mDestinationFile->GetPath(originalPath);
675 DISPATCH_AND_RETURN_IF_FAILED(rv);
677 nsAutoString tempPath = originalPath;
678 tempPath.AppendLiteral(".tmp");
680 nsCOMPtr<nsIFile> file =
681 do_CreateInstance("@mozilla.org/file/local;1", &rv);
682 DISPATCH_AND_RETURN_IF_FAILED(rv);
684 rv = file->InitWithPath(tempPath);
685 DISPATCH_AND_RETURN_IF_FAILED(rv);
687 int srv = ::sqlite3_backup_step(mBackupHandle, mPagesPerStep);
688 if (srv == SQLITE_OK || srv == SQLITE_BUSY || srv == SQLITE_LOCKED) {
689 // We're continuing the backup later. Release the guard to avoid closing
690 // the database.
691 guard.release();
692 // Queue up the next step
693 return NS_NewTimerWithCallback(getter_AddRefs(mTimer), this, mStepDelayMs,
694 nsITimer::TYPE_ONE_SHOT,
695 GetCurrentSerialEventTarget());
697 #ifdef DEBUG
698 if (srv != SQLITE_DONE) {
699 nsCString warnMsg;
700 warnMsg.AppendLiteral(
701 "The SQLite database copy could not be completed due to an error: ");
702 warnMsg.Append(::sqlite3_errmsg(mBackupFile));
703 NS_WARNING(warnMsg.get());
705 #endif
707 (void)::sqlite3_backup_finish(mBackupHandle);
708 MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK);
709 mBackupFile = nullptr;
711 // The database is already closed, so we can release this guard now.
712 guard.release();
714 if (srv != SQLITE_DONE) {
715 NS_WARNING("Failed to create database copy.");
717 // The partially created database file is not useful. Let's remove it.
718 rv = file->Remove(false);
719 if (NS_FAILED(rv)) {
720 NS_WARNING(
721 "Removing a partially backed up SQLite database file failed.");
724 return Dispatch(convertResultCode(srv), nullptr);
727 // Now that we've successfully created the copy, we'll strip off the .tmp
728 // extension.
730 nsAutoString leafName;
731 rv = mDestinationFile->GetLeafName(leafName);
732 DISPATCH_AND_RETURN_IF_FAILED(rv);
734 rv = file->RenameTo(nullptr, leafName);
735 DISPATCH_AND_RETURN_IF_FAILED(rv);
737 #undef DISPATCH_AND_RETURN_IF_FAILED
738 return Dispatch(NS_OK, nullptr);
741 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
742 RefPtr<CallbackComplete> event =
743 new CallbackComplete(aResult, aValue, mCallback.forget());
744 return mConnection->eventTargetOpenedOn->Dispatch(event,
745 NS_DISPATCH_NORMAL);
748 ~AsyncBackupDatabaseFile() override {
749 nsresult rv;
750 nsCOMPtr<nsIThread> thread =
751 do_QueryInterface(mConnection->eventTargetOpenedOn, &rv);
752 MOZ_ASSERT(NS_SUCCEEDED(rv));
754 // Handle ambiguous nsISupports inheritance.
755 NS_ProxyRelease("AsyncBackupDatabaseFile::mConnection", thread,
756 mConnection.forget());
757 NS_ProxyRelease("AsyncBackupDatabaseFile::mDestinationFile", thread,
758 mDestinationFile.forget());
760 // Generally, the callback will be released by CallbackComplete.
761 // However, if for some reason Run() is not executed, we still
762 // need to ensure that it is released here.
763 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
764 mCallback.forget());
767 RefPtr<Connection> mConnection;
768 sqlite3* mNativeConnection;
769 nsCOMPtr<nsITimer> mTimer;
770 nsCOMPtr<nsIFile> mDestinationFile;
771 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
772 int32_t mPagesPerStep;
773 uint32_t mStepDelayMs;
774 sqlite3* mBackupFile;
775 sqlite3_backup* mBackupHandle;
778 NS_IMPL_ISUPPORTS_INHERITED(AsyncBackupDatabaseFile, Runnable, nsITimerCallback)
780 } // namespace
782 ////////////////////////////////////////////////////////////////////////////////
783 //// Connection
785 Connection::Connection(Service* aService, int aFlags,
786 ConnectionOperation aSupportedOperations,
787 const nsCString& aTelemetryFilename, bool aInterruptible,
788 bool aIgnoreLockingMode, bool aOpenNotExclusive)
789 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
790 sharedDBMutex("Connection::sharedDBMutex"),
791 eventTargetOpenedOn(WrapNotNull(GetCurrentSerialEventTarget())),
792 mIsStatementOnHelperThreadInterruptible(false),
793 mDBConn(nullptr),
794 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
795 mDestroying(false),
796 mProgressHandler(nullptr),
797 mStorageService(aService),
798 mFlags(aFlags),
799 mTransactionNestingLevel(0),
800 mSupportedOperations(aSupportedOperations),
801 mInterruptible(aSupportedOperations == Connection::ASYNCHRONOUS ||
802 aInterruptible),
803 mIgnoreLockingMode(aIgnoreLockingMode),
804 mOpenNotExclusive(aOpenNotExclusive),
805 mAsyncExecutionThreadShuttingDown(false),
806 mConnectionClosed(false),
807 mGrowthChunkSize(0) {
808 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
809 "Can't ignore locking for a non-readonly connection!");
810 mStorageService->registerConnection(this);
811 MOZ_ASSERT(!aTelemetryFilename.IsEmpty(),
812 "A telemetry filename should have been passed-in.");
813 mTelemetryFilename.Assign(aTelemetryFilename);
816 Connection::~Connection() {
817 // Failsafe Close() occurs in our custom Release method because of
818 // complications related to Close() potentially invoking AsyncClose() which
819 // will increment our refcount.
820 MOZ_ASSERT(!mAsyncExecutionThread,
821 "The async thread has not been shutdown properly!");
824 NS_IMPL_ADDREF(Connection)
826 NS_INTERFACE_MAP_BEGIN(Connection)
827 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
828 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
829 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
830 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
831 NS_INTERFACE_MAP_END
833 // This is identical to what NS_IMPL_RELEASE provides, but with the
834 // extra |1 == count| case.
835 NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
836 MOZ_ASSERT(0 != mRefCnt, "dup release");
837 nsrefcnt count = --mRefCnt;
838 NS_LOG_RELEASE(this, count, "Connection");
839 if (1 == count) {
840 // If the refcount went to 1, the single reference must be from
841 // gService->mConnections (in class |Service|). And the code calling
842 // Release is either:
843 // - The "user" code that had created the connection, releasing on any
844 // thread.
845 // - One of Service's getConnections() callers had acquired a strong
846 // reference to the Connection that out-lived the last "user" reference,
847 // and now that just got dropped. Note that this reference could be
848 // getting dropped on the main thread or Connection->eventTargetOpenedOn
849 // (because of the NewRunnableMethod used by minimizeMemory).
851 // Either way, we should now perform our failsafe Close() and unregister.
852 // However, we only want to do this once, and the reality is that our
853 // refcount could go back up above 1 and down again at any time if we are
854 // off the main thread and getConnections() gets called on the main thread,
855 // so we use an atomic here to do this exactly once.
856 if (mDestroying.compareExchange(false, true)) {
857 // Close the connection, dispatching to the opening event target if we're
858 // not on that event target already and that event target is still
859 // accepting runnables. We do this because it's possible we're on the main
860 // thread because of getConnections(), and we REALLY don't want to
861 // transfer I/O to the main thread if we can avoid it.
862 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
863 // This could cause SpinningSynchronousClose() to be invoked and AddRef
864 // triggered for AsyncCloseConnection's strong ref if the conn was ever
865 // use for async purposes. (Main-thread only, though.)
866 Unused << synchronousClose();
867 } else {
868 nsCOMPtr<nsIRunnable> event =
869 NewRunnableMethod("storage::Connection::synchronousClose", this,
870 &Connection::synchronousClose);
871 if (NS_FAILED(eventTargetOpenedOn->Dispatch(event.forget(),
872 NS_DISPATCH_NORMAL))) {
873 // The event target was dead and so we've just leaked our runnable.
874 // This should not happen because our non-main-thread consumers should
875 // be explicitly closing their connections, not relying on us to close
876 // them for them. (It's okay to let a statement go out of scope for
877 // automatic cleanup, but not a Connection.)
878 MOZ_ASSERT(false,
879 "Leaked Connection::synchronousClose(), ownership fail.");
880 Unused << synchronousClose();
884 // This will drop its strong reference right here, right now.
885 mStorageService->unregisterConnection(this);
887 } else if (0 == count) {
888 mRefCnt = 1; /* stabilize */
889 #if 0 /* enable this to find non-threadsafe destructors: */
890 NS_ASSERT_OWNINGTHREAD(Connection);
891 #endif
892 delete (this);
893 return 0;
895 return count;
898 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
899 int32_t* aMaxValue) {
900 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
901 int curr = 0, max = 0;
902 DebugOnly<int> rc =
903 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
904 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
905 if (aMaxValue) *aMaxValue = max;
906 return curr;
909 nsIEventTarget* Connection::getAsyncExecutionTarget() {
910 NS_ENSURE_TRUE(IsOnCurrentSerialEventTarget(eventTargetOpenedOn), nullptr);
912 // Don't return the asynchronous event target if we are shutting down.
913 if (mAsyncExecutionThreadShuttingDown) {
914 return nullptr;
917 // Create the async event target if there's none yet.
918 if (!mAsyncExecutionThread) {
919 // Names start with "sqldb:" followed by a recognizable name, like the
920 // database file name, or a specially crafted name like "memory".
921 // This name will be surfaced on https://crash-stats.mozilla.org, so any
922 // sensitive part of the file name (e.g. an URL origin) should be replaced
923 // by passing an explicit telemetryName to openDatabaseWithFileURL.
924 nsAutoCString name("sqldb:"_ns);
925 name.Append(mTelemetryFilename);
926 static nsThreadPoolNaming naming;
927 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName(name),
928 getter_AddRefs(mAsyncExecutionThread));
929 if (NS_FAILED(rv)) {
930 NS_WARNING("Failed to create async thread.");
931 return nullptr;
935 return mAsyncExecutionThread;
938 void Connection::RecordOpenStatus(nsresult rv) {
939 nsCString histogramKey = mTelemetryFilename;
941 if (histogramKey.IsEmpty()) {
942 histogramKey.AssignLiteral("unknown");
945 if (NS_SUCCEEDED(rv)) {
946 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
947 return;
950 switch (rv) {
951 case NS_ERROR_FILE_CORRUPTED:
952 AccumulateCategoricalKeyed(histogramKey,
953 LABELS_SQLITE_STORE_OPEN::corrupt);
954 break;
955 case NS_ERROR_STORAGE_IOERR:
956 AccumulateCategoricalKeyed(histogramKey,
957 LABELS_SQLITE_STORE_OPEN::diskio);
958 break;
959 case NS_ERROR_FILE_ACCESS_DENIED:
960 case NS_ERROR_FILE_IS_LOCKED:
961 case NS_ERROR_FILE_READ_ONLY:
962 AccumulateCategoricalKeyed(histogramKey,
963 LABELS_SQLITE_STORE_OPEN::access);
964 break;
965 case NS_ERROR_FILE_NO_DEVICE_SPACE:
966 AccumulateCategoricalKeyed(histogramKey,
967 LABELS_SQLITE_STORE_OPEN::diskspace);
968 break;
969 default:
970 AccumulateCategoricalKeyed(histogramKey,
971 LABELS_SQLITE_STORE_OPEN::failure);
975 void Connection::RecordQueryStatus(int srv) {
976 nsCString histogramKey = mTelemetryFilename;
978 if (histogramKey.IsEmpty()) {
979 histogramKey.AssignLiteral("unknown");
982 switch (srv) {
983 case SQLITE_OK:
984 case SQLITE_ROW:
985 case SQLITE_DONE:
987 // Note that these are returned when we intentionally cancel a statement so
988 // they aren't indicating a failure.
989 case SQLITE_ABORT:
990 case SQLITE_INTERRUPT:
991 AccumulateCategoricalKeyed(histogramKey,
992 LABELS_SQLITE_STORE_QUERY::success);
993 break;
994 case SQLITE_CORRUPT:
995 case SQLITE_NOTADB:
996 AccumulateCategoricalKeyed(histogramKey,
997 LABELS_SQLITE_STORE_QUERY::corrupt);
998 break;
999 case SQLITE_PERM:
1000 case SQLITE_CANTOPEN:
1001 case SQLITE_LOCKED:
1002 case SQLITE_READONLY:
1003 AccumulateCategoricalKeyed(histogramKey,
1004 LABELS_SQLITE_STORE_QUERY::access);
1005 break;
1006 case SQLITE_IOERR:
1007 case SQLITE_NOLFS:
1008 AccumulateCategoricalKeyed(histogramKey,
1009 LABELS_SQLITE_STORE_QUERY::diskio);
1010 break;
1011 case SQLITE_FULL:
1012 case SQLITE_TOOBIG:
1013 AccumulateCategoricalKeyed(histogramKey,
1014 LABELS_SQLITE_STORE_QUERY::diskspace);
1015 break;
1016 case SQLITE_CONSTRAINT:
1017 case SQLITE_RANGE:
1018 case SQLITE_MISMATCH:
1019 case SQLITE_MISUSE:
1020 AccumulateCategoricalKeyed(histogramKey,
1021 LABELS_SQLITE_STORE_QUERY::misuse);
1022 break;
1023 case SQLITE_BUSY:
1024 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_QUERY::busy);
1025 break;
1026 default:
1027 AccumulateCategoricalKeyed(histogramKey,
1028 LABELS_SQLITE_STORE_QUERY::failure);
1032 nsresult Connection::initialize(const nsACString& aStorageKey,
1033 const nsACString& aName) {
1034 MOZ_ASSERT(aStorageKey.Equals(kMozStorageMemoryStorageKey));
1035 NS_ASSERTION(!connectionReady(),
1036 "Initialize called on already opened database!");
1037 MOZ_ASSERT(!mIgnoreLockingMode, "Can't ignore locking on an in-memory db.");
1038 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
1040 mStorageKey = aStorageKey;
1041 mName = aName;
1043 // in memory database requested, sqlite uses a magic file name
1045 const nsAutoCString path =
1046 mName.IsEmpty() ? nsAutoCString(":memory:"_ns)
1047 : "file:"_ns + mName + "?mode=memory&cache=shared"_ns;
1049 int srv = ::sqlite3_open_v2(path.get(), &mDBConn, mFlags,
1050 basevfs::GetVFSName(true));
1051 if (srv != SQLITE_OK) {
1052 mDBConn = nullptr;
1053 nsresult rv = convertResultCode(srv);
1054 RecordOpenStatus(rv);
1055 return rv;
1058 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
1059 srv =
1060 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
1061 MOZ_ASSERT(srv == SQLITE_OK,
1062 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
1063 #endif
1065 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
1066 // database.
1068 nsresult rv = initializeInternal();
1069 RecordOpenStatus(rv);
1070 NS_ENSURE_SUCCESS(rv, rv);
1072 return NS_OK;
1075 nsresult Connection::initialize(nsIFile* aDatabaseFile) {
1076 NS_ASSERTION(aDatabaseFile, "Passed null file!");
1077 NS_ASSERTION(!connectionReady(),
1078 "Initialize called on already opened database!");
1079 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
1081 // Do not set mFileURL here since this is database does not have an associated
1082 // URL.
1083 mDatabaseFile = aDatabaseFile;
1085 nsAutoString path;
1086 nsresult rv = aDatabaseFile->GetPath(path);
1087 NS_ENSURE_SUCCESS(rv, rv);
1089 bool exclusive =
1090 StaticPrefs::storage_sqlite_exclusiveLock_enabled() && !mOpenNotExclusive;
1091 int srv;
1092 if (mIgnoreLockingMode) {
1093 exclusive = false;
1094 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
1095 "readonly-immutable-nolock");
1096 } else {
1097 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
1098 basevfs::GetVFSName(exclusive));
1099 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
1100 // Retry without trying to get an exclusive lock.
1101 exclusive = false;
1102 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
1103 mFlags, basevfs::GetVFSName(false));
1106 if (srv != SQLITE_OK) {
1107 mDBConn = nullptr;
1108 rv = convertResultCode(srv);
1109 RecordOpenStatus(rv);
1110 return rv;
1113 rv = initializeInternal();
1114 if (exclusive &&
1115 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
1116 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
1117 // some cases it may successfully open the database and then lock on the
1118 // first query execution. When initializeInternal fails it closes the
1119 // connection, so we can try to restart it in non-exclusive mode.
1120 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
1121 basevfs::GetVFSName(false));
1122 if (srv == SQLITE_OK) {
1123 rv = initializeInternal();
1127 RecordOpenStatus(rv);
1128 NS_ENSURE_SUCCESS(rv, rv);
1130 return NS_OK;
1133 nsresult Connection::initialize(nsIFileURL* aFileURL) {
1134 NS_ASSERTION(aFileURL, "Passed null file URL!");
1135 NS_ASSERTION(!connectionReady(),
1136 "Initialize called on already opened database!");
1137 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
1139 nsCOMPtr<nsIFile> databaseFile;
1140 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
1141 NS_ENSURE_SUCCESS(rv, rv);
1143 // Set both mDatabaseFile and mFileURL here.
1144 mFileURL = aFileURL;
1145 mDatabaseFile = databaseFile;
1147 nsAutoCString spec;
1148 rv = aFileURL->GetSpec(spec);
1149 NS_ENSURE_SUCCESS(rv, rv);
1151 // If there is a key specified, we need to use the obfuscating VFS.
1152 nsAutoCString query;
1153 rv = aFileURL->GetQuery(query);
1154 NS_ENSURE_SUCCESS(rv, rv);
1156 bool hasKey = false;
1157 bool hasDirectoryLockId = false;
1159 MOZ_ALWAYS_TRUE(
1160 URLParams::Parse(query, true,
1161 [&hasKey, &hasDirectoryLockId](
1162 const nsACString& aName, const nsACString& aValue) {
1163 if (aName.EqualsLiteral("key")) {
1164 hasKey = true;
1165 return true;
1167 if (aName.EqualsLiteral("directoryLockId")) {
1168 hasDirectoryLockId = true;
1169 return true;
1171 return true;
1172 }));
1174 bool exclusive =
1175 StaticPrefs::storage_sqlite_exclusiveLock_enabled() && !mOpenNotExclusive;
1177 const char* const vfs = hasKey ? obfsvfs::GetVFSName()
1178 : hasDirectoryLockId ? quotavfs::GetVFSName()
1179 : basevfs::GetVFSName(exclusive);
1181 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
1182 if (srv != SQLITE_OK) {
1183 mDBConn = nullptr;
1184 rv = convertResultCode(srv);
1185 RecordOpenStatus(rv);
1186 return rv;
1189 rv = initializeInternal();
1190 RecordOpenStatus(rv);
1191 NS_ENSURE_SUCCESS(rv, rv);
1193 return NS_OK;
1196 nsresult Connection::initializeInternal() {
1197 MOZ_ASSERT(mDBConn);
1198 auto guard = MakeScopeExit([&]() { initializeFailed(); });
1200 mConnectionClosed = false;
1202 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
1203 DebugOnly<int> srv2 =
1204 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
1205 MOZ_ASSERT(srv2 == SQLITE_OK,
1206 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
1207 #endif
1209 // Properly wrap the database handle's mutex.
1210 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
1212 // SQLite tracing can slow down queries (especially long queries)
1213 // significantly. Don't trace unless the user is actively monitoring SQLite.
1214 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1215 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
1216 tracefunc, this);
1218 MOZ_LOG(
1219 gStorageLog, LogLevel::Debug,
1220 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
1223 int64_t pageSize = Service::kDefaultPageSize;
1225 // Set page_size to the preferred default value. This is effective only if
1226 // the database has just been created, otherwise, if the database does not
1227 // use WAL journal mode, a VACUUM operation will updated its page_size.
1228 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1229 "PRAGMA page_size = ");
1230 pageSizeQuery.AppendInt(pageSize);
1231 int srv = executeSql(mDBConn, pageSizeQuery.get());
1232 if (srv != SQLITE_OK) {
1233 return convertResultCode(srv);
1236 // Setting the cache_size forces the database open, verifying if it is valid
1237 // or corrupt. So this is executed regardless it being actually needed.
1238 // The cache_size is calculated from the actual page_size, to save memory.
1239 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1240 "PRAGMA cache_size = ");
1241 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
1242 srv = executeSql(mDBConn, cacheSizeQuery.get());
1243 if (srv != SQLITE_OK) {
1244 return convertResultCode(srv);
1247 // Register our built-in SQL functions.
1248 srv = registerFunctions(mDBConn);
1249 if (srv != SQLITE_OK) {
1250 return convertResultCode(srv);
1253 // Register our built-in SQL collating sequences.
1254 srv = registerCollations(mDBConn, mStorageService);
1255 if (srv != SQLITE_OK) {
1256 return convertResultCode(srv);
1259 // Set the default synchronous value. Each consumer can switch this
1260 // accordingly to their needs.
1261 #if defined(ANDROID)
1262 // Android prefers synchronous = OFF for performance reasons.
1263 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
1264 #else
1265 // Normal is the suggested value for WAL journals.
1266 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
1267 #endif
1269 // Initialization succeeded, we can stop guarding for failures.
1270 guard.release();
1271 return NS_OK;
1274 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
1275 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1276 nsresult rv = aStorageFile
1277 ? initialize(aStorageFile)
1278 : initialize(kMozStorageMemoryStorageKey, VoidCString());
1279 if (NS_FAILED(rv)) {
1280 // Shutdown the async thread, since initialization failed.
1281 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1282 mAsyncExecutionThreadShuttingDown = true;
1283 nsCOMPtr<nsIRunnable> event =
1284 NewRunnableMethod("Connection::shutdownAsyncThread", this,
1285 &Connection::shutdownAsyncThread);
1286 Unused << NS_DispatchToMainThread(event);
1288 return rv;
1291 void Connection::initializeFailed() {
1293 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1294 mConnectionClosed = true;
1296 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
1297 mDBConn = nullptr;
1298 sharedDBMutex.destroy();
1301 nsresult Connection::databaseElementExists(
1302 enum DatabaseElementType aElementType, const nsACString& aElementName,
1303 bool* _exists) {
1304 if (!connectionReady()) {
1305 return NS_ERROR_NOT_AVAILABLE;
1307 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1308 if (NS_FAILED(rv)) {
1309 return rv;
1312 // When constructing the query, make sure to SELECT the correct db's
1313 // sqlite_master if the user is prefixing the element with a specific db. ex:
1314 // sample.test
1315 nsCString query("SELECT name FROM (SELECT * FROM ");
1316 nsDependentCSubstring element;
1317 int32_t ind = aElementName.FindChar('.');
1318 if (ind == kNotFound) {
1319 element.Assign(aElementName);
1320 } else {
1321 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
1322 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
1323 query.Append(db);
1325 query.AppendLiteral(
1326 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
1327 "'");
1329 switch (aElementType) {
1330 case INDEX:
1331 query.AppendLiteral("index");
1332 break;
1333 case TABLE:
1334 query.AppendLiteral("table");
1335 break;
1337 query.AppendLiteral("' AND name ='");
1338 query.Append(element);
1339 query.Append('\'');
1341 sqlite3_stmt* stmt;
1342 int srv = prepareStatement(mDBConn, query, &stmt);
1343 if (srv != SQLITE_OK) {
1344 RecordQueryStatus(srv);
1345 return convertResultCode(srv);
1348 srv = stepStatement(mDBConn, stmt);
1349 // we just care about the return value from step
1350 (void)::sqlite3_finalize(stmt);
1352 RecordQueryStatus(srv);
1354 if (srv == SQLITE_ROW) {
1355 *_exists = true;
1356 return NS_OK;
1358 if (srv == SQLITE_DONE) {
1359 *_exists = false;
1360 return NS_OK;
1363 return convertResultCode(srv);
1366 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
1367 sharedDBMutex.assertCurrentThreadOwns();
1369 for (const auto& data : mFunctions.Values()) {
1370 if (data.function == aInstance) {
1371 return true;
1374 return false;
1377 /* static */
1378 int Connection::sProgressHelper(void* aArg) {
1379 Connection* _this = static_cast<Connection*>(aArg);
1380 return _this->progressHandler();
1383 int Connection::progressHandler() {
1384 sharedDBMutex.assertCurrentThreadOwns();
1385 if (mProgressHandler) {
1386 bool result;
1387 nsresult rv = mProgressHandler->OnProgress(this, &result);
1388 if (NS_FAILED(rv)) return 0; // Don't break request
1389 return result ? 1 : 0;
1391 return 0;
1394 nsresult Connection::setClosedState() {
1395 // Flag that we are shutting down the async thread, so that
1396 // getAsyncExecutionTarget knows not to expose/create the async thread.
1397 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1398 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1400 mAsyncExecutionThreadShuttingDown = true;
1402 // Set the property to null before closing the connection, otherwise the
1403 // other functions in the module may try to use the connection after it is
1404 // closed.
1405 mDBConn = nullptr;
1407 return NS_OK;
1410 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1411 if (aOperationType == ASYNCHRONOUS) {
1412 // Async operations are supported for all connections, on any thread.
1413 return true;
1415 // Sync operations are supported for sync connections (on any thread), and
1416 // async connections on a background thread.
1417 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1418 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1421 nsresult Connection::ensureOperationSupported(
1422 ConnectionOperation aOperationType) {
1423 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1424 #ifdef DEBUG
1425 if (NS_IsMainThread()) {
1426 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1427 Unused << xpc->DebugDumpJSStack(false, false, false);
1429 #endif
1430 MOZ_ASSERT(false,
1431 "Don't use async connections synchronously on the main thread");
1432 return NS_ERROR_NOT_AVAILABLE;
1434 return NS_OK;
1437 bool Connection::isConnectionReadyOnThisThread() {
1438 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1439 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1440 return true;
1442 return connectionReady();
1445 bool Connection::isClosing() {
1446 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1447 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1450 bool Connection::isClosed() {
1451 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1452 return mConnectionClosed;
1455 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1457 bool Connection::isAsyncExecutionThreadAvailable() {
1458 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1459 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1462 void Connection::shutdownAsyncThread() {
1463 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1464 MOZ_ASSERT(mAsyncExecutionThread);
1465 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1467 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1468 mAsyncExecutionThread = nullptr;
1471 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1472 #ifdef DEBUG
1473 { // Make sure we have marked our async thread as shutting down.
1474 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1475 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1476 "Did not call setClosedState!");
1477 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1479 #endif // DEBUG
1481 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1482 nsAutoCString leafName(":memory");
1483 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1484 MOZ_LOG(gStorageLog, LogLevel::Debug,
1485 ("Closing connection to '%s'", leafName.get()));
1488 // At this stage, we may still have statements that need to be
1489 // finalized. Attempt to close the database connection. This will
1490 // always disconnect any virtual tables and cleanly finalize their
1491 // internal statements. Once this is done, closing may fail due to
1492 // unfinalized client statements, in which case we need to finalize
1493 // these statements and close again.
1495 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1496 mConnectionClosed = true;
1499 // Nothing else needs to be done if we don't have a connection here.
1500 if (!aNativeConnection) return NS_OK;
1502 int srv = ::sqlite3_close(aNativeConnection);
1504 if (srv == SQLITE_BUSY) {
1506 // Nothing else should change the connection or statements status until we
1507 // are done here.
1508 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1509 // We still have non-finalized statements. Finalize them.
1510 sqlite3_stmt* stmt = nullptr;
1511 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1512 MOZ_LOG(gStorageLog, LogLevel::Debug,
1513 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1514 stmt));
1516 #ifdef DEBUG
1517 SmprintfPointer msg = ::mozilla::Smprintf(
1518 "SQL statement '%s' (%p) should have been finalized before closing "
1519 "the connection",
1520 ::sqlite3_sql(stmt), stmt);
1521 NS_WARNING(msg.get());
1522 #endif // DEBUG
1524 srv = ::sqlite3_finalize(stmt);
1526 #ifdef DEBUG
1527 if (srv != SQLITE_OK) {
1528 SmprintfPointer msg = ::mozilla::Smprintf(
1529 "Could not finalize SQL statement (%p)", stmt);
1530 NS_WARNING(msg.get());
1532 #endif // DEBUG
1534 // Ensure that the loop continues properly, whether closing has
1535 // succeeded or not.
1536 if (srv == SQLITE_OK) {
1537 stmt = nullptr;
1540 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1541 // again, since Sqlite will try to acquire it.
1544 // Now that all statements have been finalized, we
1545 // should be able to close.
1546 srv = ::sqlite3_close(aNativeConnection);
1547 MOZ_ASSERT(false,
1548 "Had to forcibly close the database connection because not all "
1549 "the statements have been finalized.");
1552 if (srv == SQLITE_OK) {
1553 sharedDBMutex.destroy();
1554 } else {
1555 MOZ_ASSERT(false,
1556 "sqlite3_close failed. There are probably outstanding "
1557 "statements that are listed above!");
1560 return convertResultCode(srv);
1563 nsCString Connection::getFilename() { return mTelemetryFilename; }
1565 int Connection::stepStatement(sqlite3* aNativeConnection,
1566 sqlite3_stmt* aStatement) {
1567 MOZ_ASSERT(aStatement);
1569 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1570 ::sqlite3_sql(aStatement));
1572 bool checkedMainThread = false;
1573 TimeStamp startTime = TimeStamp::Now();
1575 // The connection may have been closed if the executing statement has been
1576 // created and cached after a call to asyncClose() but before the actual
1577 // sqlite3_close(). This usually happens when other tasks using cached
1578 // statements are asynchronously scheduled for execution and any of them ends
1579 // up after asyncClose. See bug 728653 for details.
1580 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1582 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1584 int srv;
1585 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1586 if (!checkedMainThread) {
1587 checkedMainThread = true;
1588 if (::NS_IsMainThread()) {
1589 NS_WARNING("We won't allow blocking on the main thread!");
1590 break;
1594 srv = WaitForUnlockNotify(aNativeConnection);
1595 if (srv != SQLITE_OK) {
1596 break;
1599 ::sqlite3_reset(aStatement);
1602 // Report very slow SQL statements to Telemetry
1603 TimeDuration duration = TimeStamp::Now() - startTime;
1604 const uint32_t threshold = NS_IsMainThread()
1605 ? Telemetry::kSlowSQLThresholdForMainThread
1606 : Telemetry::kSlowSQLThresholdForHelperThreads;
1607 if (duration.ToMilliseconds() >= threshold) {
1608 nsDependentCString statementString(::sqlite3_sql(aStatement));
1609 Telemetry::RecordSlowSQLStatement(
1610 statementString, mTelemetryFilename,
1611 static_cast<uint32_t>(duration.ToMilliseconds()));
1614 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1615 // Drop off the extended result bits of the result code.
1616 return srv & 0xFF;
1619 int Connection::prepareStatement(sqlite3* aNativeConnection,
1620 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1621 // We should not even try to prepare statements after the connection has
1622 // been closed.
1623 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1625 bool checkedMainThread = false;
1627 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1629 int srv;
1630 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1631 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1632 if (!checkedMainThread) {
1633 checkedMainThread = true;
1634 if (::NS_IsMainThread()) {
1635 NS_WARNING("We won't allow blocking on the main thread!");
1636 break;
1640 srv = WaitForUnlockNotify(aNativeConnection);
1641 if (srv != SQLITE_OK) {
1642 break;
1646 if (srv != SQLITE_OK) {
1647 nsCString warnMsg;
1648 warnMsg.AppendLiteral("The SQL statement '");
1649 warnMsg.Append(aSQL);
1650 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1651 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1653 #ifdef DEBUG
1654 NS_WARNING(warnMsg.get());
1655 #endif
1656 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1659 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1660 // Drop off the extended result bits of the result code.
1661 int rc = srv & 0xFF;
1662 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1663 // The callers of this function are used to only checking the return value,
1664 // so it is safer to return an error code.
1665 if (rc == SQLITE_OK && *_stmt == nullptr) {
1666 return SQLITE_MISUSE;
1669 return rc;
1672 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1673 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1675 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1677 TimeStamp startTime = TimeStamp::Now();
1678 int srv =
1679 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1680 RecordQueryStatus(srv);
1682 // Report very slow SQL statements to Telemetry
1683 TimeDuration duration = TimeStamp::Now() - startTime;
1684 const uint32_t threshold = NS_IsMainThread()
1685 ? Telemetry::kSlowSQLThresholdForMainThread
1686 : Telemetry::kSlowSQLThresholdForHelperThreads;
1687 if (duration.ToMilliseconds() >= threshold) {
1688 nsDependentCString statementString(aSqlString);
1689 Telemetry::RecordSlowSQLStatement(
1690 statementString, mTelemetryFilename,
1691 static_cast<uint32_t>(duration.ToMilliseconds()));
1694 return srv;
1697 ////////////////////////////////////////////////////////////////////////////////
1698 //// nsIInterfaceRequestor
1700 NS_IMETHODIMP
1701 Connection::GetInterface(const nsIID& aIID, void** _result) {
1702 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1703 nsIEventTarget* background = getAsyncExecutionTarget();
1704 NS_IF_ADDREF(background);
1705 *_result = background;
1706 return NS_OK;
1708 return NS_ERROR_NO_INTERFACE;
1711 ////////////////////////////////////////////////////////////////////////////////
1712 //// mozIStorageConnection
1714 NS_IMETHODIMP
1715 Connection::Close() {
1716 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1717 if (NS_FAILED(rv)) {
1718 return rv;
1720 return synchronousClose();
1723 nsresult Connection::synchronousClose() {
1724 if (!connectionReady()) {
1725 return NS_ERROR_NOT_INITIALIZED;
1728 #ifdef DEBUG
1729 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1730 // event target. We make this check outside of debug code below in
1731 // setClosedState, but this is here to be explicit.
1732 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1733 #endif // DEBUG
1735 // Make sure we have not executed any asynchronous statements.
1736 // If this fails, the mDBConn may be left open, resulting in a leak.
1737 // We'll try to finalize the pending statements and close the connection.
1738 if (isAsyncExecutionThreadAvailable()) {
1739 #ifdef DEBUG
1740 if (NS_IsMainThread()) {
1741 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1742 Unused << xpc->DebugDumpJSStack(false, false, false);
1744 #endif
1745 MOZ_ASSERT(false,
1746 "Close() was invoked on a connection that executed asynchronous "
1747 "statements. "
1748 "Should have used asyncClose().");
1749 // Try to close the database regardless, to free up resources.
1750 Unused << SpinningSynchronousClose();
1751 return NS_ERROR_UNEXPECTED;
1754 // setClosedState nullifies our connection pointer, so we take a raw pointer
1755 // off it, to pass it through the close procedure.
1756 sqlite3* nativeConn = mDBConn;
1757 nsresult rv = setClosedState();
1758 NS_ENSURE_SUCCESS(rv, rv);
1760 return internalClose(nativeConn);
1763 NS_IMETHODIMP
1764 Connection::SpinningSynchronousClose() {
1765 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1766 if (NS_FAILED(rv)) {
1767 return rv;
1769 if (!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
1770 return NS_ERROR_NOT_SAME_THREAD;
1773 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1774 // Our only existing caller will never have called close; assert if misused
1775 // so that no new callers assume this works after an AsyncClose.
1776 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1777 if (!connectionReady()) {
1778 return NS_ERROR_UNEXPECTED;
1781 RefPtr<CloseListener> listener = new CloseListener();
1782 rv = AsyncClose(listener);
1783 NS_ENSURE_SUCCESS(rv, rv);
1784 MOZ_ALWAYS_TRUE(
1785 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,
1786 [&]() { return listener->mClosed; }));
1787 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1789 return rv;
1792 NS_IMETHODIMP
1793 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1794 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1795 // Check if AsyncClose or Close were already invoked.
1796 if (!connectionReady()) {
1797 return NS_ERROR_NOT_INITIALIZED;
1799 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1800 if (NS_FAILED(rv)) {
1801 return rv;
1804 // The two relevant factors at this point are whether we have a database
1805 // connection and whether we have an async execution thread. Here's what the
1806 // states mean and how we handle them:
1808 // - (mDBConn && asyncThread): The expected case where we are either an
1809 // async connection or a sync connection that has been used asynchronously.
1810 // Either way the caller must call us and not Close(). Nothing surprising
1811 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1812 // async thread.
1814 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1815 // opened the connection synchronously and was planning to use it
1816 // asynchronously, but never got around to using it asynchronously before
1817 // needing to shutdown. This has been observed to happen for the cookie
1818 // service in a case where Firefox shuts itself down almost immediately
1819 // after startup (for unknown reasons). In the Firefox shutdown case,
1820 // we may also fail to create a new async execution thread if one does not
1821 // already exist. (nsThreadManager will refuse to create new threads when
1822 // it has already been told to shutdown.) As such, we need to handle a
1823 // failure to create the async execution thread by falling back to
1824 // synchronous Close() and also dispatching the completion callback because
1825 // at least Places likes to spin a nested event loop that depends on the
1826 // callback being invoked.
1828 // Note that we have considered not trying to spin up the async execution
1829 // thread in this case if it does not already exist, but the overhead of
1830 // thread startup (if successful) is significantly less expensive than the
1831 // worst-case potential I/O hit of synchronously closing a database when we
1832 // could close it asynchronously.
1834 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1835 // OpenAsyncDatabase encountered a problem opening the database. If it
1836 // happened in all cases AsyncInitDatabase would just shut down the thread
1837 // directly and we would avoid this case. But it doesn't, so for simplicity
1838 // and consistency AsyncCloseConnection knows how to handle this and we
1839 // act like this was the (mDBConn && asyncThread) case in this method.
1841 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1842 // Close() or AsyncClose() has already been called (at least) once. This is
1843 // undeniably a misuse case by the caller. We could optimize for this
1844 // case by adding an additional check of mAsyncExecutionThread without using
1845 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1846 // shut it down. But this complicates the method for broken caller code
1847 // whereas we're still correct and safe without the special-case.
1848 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1850 // Create our callback event if we were given a callback. This will
1851 // eventually be dispatched in all cases, even if we fall back to Close() and
1852 // the database wasn't open and we return an error. The rationale is that
1853 // no existing consumer checks our return value and several of them like to
1854 // spin nested event loops until the callback fires. Given that, it seems
1855 // preferable for us to dispatch the callback in all cases. (Except the
1856 // wrong thread misuse case we bailed on up above. But that's okay because
1857 // that is statically wrong whereas these edge cases are dynamic.)
1858 nsCOMPtr<nsIRunnable> completeEvent;
1859 if (aCallback) {
1860 completeEvent = newCompletionEvent(aCallback);
1863 if (!asyncThread) {
1864 // We were unable to create an async thread, so we need to fall back to
1865 // using normal Close(). Since there is no async thread, Close() will
1866 // not complain about that. (Close() may, however, complain if the
1867 // connection is closed, but that's okay.)
1868 if (completeEvent) {
1869 // Closing the database is more important than returning an error code
1870 // about a failure to dispatch, especially because all existing native
1871 // callers ignore our return value.
1872 Unused << NS_DispatchToMainThread(completeEvent.forget());
1874 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1875 // Return a success inconditionally here, since Close() is unlikely to fail
1876 // and we want to reassure the consumer that its callback will be invoked.
1877 return NS_OK;
1880 // If we're closing the connection during shutdown, and there is an
1881 // interruptible statement running on the helper thread, issue a
1882 // sqlite3_interrupt() to avoid crashing when that statement takes a long
1883 // time (for example a vacuum).
1884 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed) &&
1885 mInterruptible && mIsStatementOnHelperThreadInterruptible) {
1886 MOZ_ASSERT(!isClosing(), "Must not be closing, see Interrupt()");
1887 DebugOnly<nsresult> rv2 = Interrupt();
1888 MOZ_ASSERT(NS_SUCCEEDED(rv2));
1891 // setClosedState nullifies our connection pointer, so we take a raw pointer
1892 // off it, to pass it through the close procedure.
1893 sqlite3* nativeConn = mDBConn;
1894 rv = setClosedState();
1895 NS_ENSURE_SUCCESS(rv, rv);
1897 // Create and dispatch our close event to the background thread.
1898 nsCOMPtr<nsIRunnable> closeEvent =
1899 new AsyncCloseConnection(this, nativeConn, completeEvent);
1900 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1901 NS_ENSURE_SUCCESS(rv, rv);
1903 return NS_OK;
1906 NS_IMETHODIMP
1907 Connection::AsyncClone(bool aReadOnly,
1908 mozIStorageCompletionCallback* aCallback) {
1909 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1911 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1912 if (!connectionReady()) {
1913 return NS_ERROR_NOT_INITIALIZED;
1915 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1916 if (NS_FAILED(rv)) {
1917 return rv;
1919 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1921 int flags = mFlags;
1922 if (aReadOnly) {
1923 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1924 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1925 // Turn off SQLITE_OPEN_CREATE.
1926 flags = (~SQLITE_OPEN_CREATE & flags);
1929 // The cloned connection will still implement the synchronous API, but throw
1930 // if any synchronous methods are called on the main thread.
1931 RefPtr<Connection> clone =
1932 new Connection(mStorageService, flags, ASYNCHRONOUS, mTelemetryFilename,
1933 mInterruptible, mIgnoreLockingMode, mOpenNotExclusive);
1935 RefPtr<AsyncInitializeClone> initEvent =
1936 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1937 // Dispatch to our async thread, since the originating connection must remain
1938 // valid and open for the whole cloning process. This also ensures we are
1939 // properly serialized with a `close` operation, rather than race with it.
1940 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1941 if (!target) {
1942 return NS_ERROR_UNEXPECTED;
1944 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1947 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1948 nsresult rv;
1949 if (!mStorageKey.IsEmpty()) {
1950 rv = aClone->initialize(mStorageKey, mName);
1951 } else if (mFileURL) {
1952 rv = aClone->initialize(mFileURL);
1953 } else {
1954 rv = aClone->initialize(mDatabaseFile);
1956 if (NS_FAILED(rv)) {
1957 return rv;
1960 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1962 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1963 NS_ENSURE_SUCCESS(rv, rv);
1965 // Re-attach on-disk databases that were attached to the original connection.
1967 nsCOMPtr<mozIStorageStatement> stmt;
1968 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1969 NS_ENSURE_SUCCESS(rv, rv);
1970 bool hasResult = false;
1971 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1972 nsAutoCString name;
1973 rv = stmt->GetUTF8String(1, name);
1974 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1975 !name.EqualsLiteral("temp")) {
1976 nsCString path;
1977 rv = stmt->GetUTF8String(2, path);
1978 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1979 nsCOMPtr<mozIStorageStatement> attachStmt;
1980 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1981 getter_AddRefs(attachStmt));
1982 NS_ENSURE_SUCCESS(rv, rv);
1983 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1984 NS_ENSURE_SUCCESS(rv, rv);
1985 rv = attachStmt->Execute();
1986 NS_ENSURE_SUCCESS(rv, rv);
1992 // Copy over pragmas from the original connection.
1993 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1994 // schema ("main" and any other attached databases), and this implmentation
1995 // fails to propagate them. This is being addressed on trunk.
1996 static const char* pragmas[] = {
1997 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1998 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1999 for (auto& pragma : pragmas) {
2000 // Read-only connections just need cache_size and temp_store pragmas.
2001 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
2002 ::strcmp(pragma, "temp_store") != 0) {
2003 continue;
2006 nsAutoCString pragmaQuery("PRAGMA ");
2007 pragmaQuery.Append(pragma);
2008 nsCOMPtr<mozIStorageStatement> stmt;
2009 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
2010 NS_ENSURE_SUCCESS(rv, rv);
2011 bool hasResult = false;
2012 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2013 pragmaQuery.AppendLiteral(" = ");
2014 pragmaQuery.AppendInt(stmt->AsInt32(0));
2015 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
2016 NS_ENSURE_SUCCESS(rv, rv);
2020 // Copy over temporary tables, triggers, and views from the original
2021 // connections. Entities in `sqlite_temp_master` are only visible to the
2022 // connection that created them.
2023 if (!aReadOnly) {
2024 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
2025 NS_ENSURE_SUCCESS(rv, rv);
2027 nsCOMPtr<mozIStorageStatement> stmt;
2028 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
2029 "WHERE type IN ('table', 'view', "
2030 "'index', 'trigger')"),
2031 getter_AddRefs(stmt));
2032 // Propagate errors, because failing to copy triggers might cause schema
2033 // coherency issues when writing to the database from the cloned connection.
2034 NS_ENSURE_SUCCESS(rv, rv);
2035 bool hasResult = false;
2036 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2037 nsAutoCString query;
2038 rv = stmt->GetUTF8String(0, query);
2039 NS_ENSURE_SUCCESS(rv, rv);
2041 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
2042 // keyword. We need to add it back, or we'll recreate temporary entities
2043 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
2044 // statements, but those don't need `TEMP` keywords.
2045 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
2046 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
2047 StringBeginsWith(query, "CREATE VIEW "_ns)) {
2048 query.Replace(0, 6, "CREATE TEMP");
2051 rv = aClone->ExecuteSimpleSQL(query);
2052 NS_ENSURE_SUCCESS(rv, rv);
2055 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
2056 NS_ENSURE_SUCCESS(rv, rv);
2059 // Copy any functions that have been added to this connection.
2060 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2061 for (const auto& entry : mFunctions) {
2062 const nsACString& key = entry.GetKey();
2063 Connection::FunctionInfo data = entry.GetData();
2065 rv = aClone->CreateFunction(key, data.numArgs, data.function);
2066 if (NS_FAILED(rv)) {
2067 NS_WARNING("Failed to copy function to cloned connection");
2071 // Load SQLite extensions that were on this connection.
2072 // Copy into an array rather than holding the mutex while we load extensions.
2073 nsTArray<nsCString> loadedExtensions;
2075 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2076 AppendToArray(loadedExtensions, mLoadedExtensions);
2078 for (const auto& extension : loadedExtensions) {
2079 (void)aClone->LoadExtension(extension, nullptr);
2082 guard.release();
2083 return NS_OK;
2086 NS_IMETHODIMP
2087 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
2088 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
2090 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
2092 if (!connectionReady()) {
2093 return NS_ERROR_NOT_INITIALIZED;
2095 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2096 if (NS_FAILED(rv)) {
2097 return rv;
2100 int flags = mFlags;
2101 if (aReadOnly) {
2102 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
2103 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
2104 // Turn off SQLITE_OPEN_CREATE.
2105 flags = (~SQLITE_OPEN_CREATE & flags);
2108 RefPtr<Connection> clone =
2109 new Connection(mStorageService, flags, mSupportedOperations,
2110 mTelemetryFilename, mInterruptible);
2112 rv = initializeClone(clone, aReadOnly);
2113 if (NS_FAILED(rv)) {
2114 return rv;
2117 NS_IF_ADDREF(*_connection = clone);
2118 return NS_OK;
2121 NS_IMETHODIMP
2122 Connection::Interrupt() {
2123 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
2124 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
2125 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
2126 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
2127 IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
2129 if (!connectionReady()) {
2130 return NS_ERROR_NOT_INITIALIZED;
2133 if (isClosing()) { // Closing already in asynchronous case
2134 return NS_OK;
2138 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
2139 // it is not safe to call sqlite3_interrupt() when
2140 // database connection is closed or might close before
2141 // sqlite3_interrupt() returns.
2142 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2143 if (!isClosed(lockedScope)) {
2144 MOZ_ASSERT(mDBConn);
2145 ::sqlite3_interrupt(mDBConn);
2149 return NS_OK;
2152 NS_IMETHODIMP
2153 Connection::AsyncVacuum(mozIStorageCompletionCallback* aCallback,
2154 bool aUseIncremental, int32_t aSetPageSize) {
2155 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
2156 // Abort if we're shutting down.
2157 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
2158 return NS_ERROR_ABORT;
2160 // Check if AsyncClose or Close were already invoked.
2161 if (!connectionReady()) {
2162 return NS_ERROR_NOT_INITIALIZED;
2164 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2165 if (NS_FAILED(rv)) {
2166 return rv;
2168 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
2169 if (!asyncThread) {
2170 return NS_ERROR_NOT_INITIALIZED;
2173 // Create and dispatch our vacuum event to the background thread.
2174 nsCOMPtr<nsIRunnable> vacuumEvent =
2175 new AsyncVacuumEvent(this, aCallback, aUseIncremental, aSetPageSize);
2176 rv = asyncThread->Dispatch(vacuumEvent, NS_DISPATCH_NORMAL);
2177 NS_ENSURE_SUCCESS(rv, rv);
2179 return NS_OK;
2182 NS_IMETHODIMP
2183 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
2184 *_defaultPageSize = Service::kDefaultPageSize;
2185 return NS_OK;
2188 NS_IMETHODIMP
2189 Connection::GetConnectionReady(bool* _ready) {
2190 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
2191 *_ready = connectionReady();
2192 return NS_OK;
2195 NS_IMETHODIMP
2196 Connection::GetDatabaseFile(nsIFile** _dbFile) {
2197 if (!connectionReady()) {
2198 return NS_ERROR_NOT_INITIALIZED;
2200 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2201 if (NS_FAILED(rv)) {
2202 return rv;
2205 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
2207 return NS_OK;
2210 NS_IMETHODIMP
2211 Connection::GetLastInsertRowID(int64_t* _id) {
2212 if (!connectionReady()) {
2213 return NS_ERROR_NOT_INITIALIZED;
2215 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2216 if (NS_FAILED(rv)) {
2217 return rv;
2220 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
2221 *_id = id;
2223 return NS_OK;
2226 NS_IMETHODIMP
2227 Connection::GetAffectedRows(int32_t* _rows) {
2228 if (!connectionReady()) {
2229 return NS_ERROR_NOT_INITIALIZED;
2231 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2232 if (NS_FAILED(rv)) {
2233 return rv;
2236 *_rows = ::sqlite3_changes(mDBConn);
2238 return NS_OK;
2241 NS_IMETHODIMP
2242 Connection::GetLastError(int32_t* _error) {
2243 if (!connectionReady()) {
2244 return NS_ERROR_NOT_INITIALIZED;
2246 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2247 if (NS_FAILED(rv)) {
2248 return rv;
2251 *_error = ::sqlite3_errcode(mDBConn);
2253 return NS_OK;
2256 NS_IMETHODIMP
2257 Connection::GetLastErrorString(nsACString& _errorString) {
2258 if (!connectionReady()) {
2259 return NS_ERROR_NOT_INITIALIZED;
2261 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2262 if (NS_FAILED(rv)) {
2263 return rv;
2266 const char* serr = ::sqlite3_errmsg(mDBConn);
2267 _errorString.Assign(serr);
2269 return NS_OK;
2272 NS_IMETHODIMP
2273 Connection::GetSchemaVersion(int32_t* _version) {
2274 if (!connectionReady()) {
2275 return NS_ERROR_NOT_INITIALIZED;
2277 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2278 if (NS_FAILED(rv)) {
2279 return rv;
2282 nsCOMPtr<mozIStorageStatement> stmt;
2283 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
2284 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
2286 *_version = 0;
2287 bool hasResult;
2288 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2289 *_version = stmt->AsInt32(0);
2292 return NS_OK;
2295 NS_IMETHODIMP
2296 Connection::SetSchemaVersion(int32_t aVersion) {
2297 if (!connectionReady()) {
2298 return NS_ERROR_NOT_INITIALIZED;
2300 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2301 if (NS_FAILED(rv)) {
2302 return rv;
2305 nsAutoCString stmt("PRAGMA user_version = "_ns);
2306 stmt.AppendInt(aVersion);
2308 return ExecuteSimpleSQL(stmt);
2311 NS_IMETHODIMP
2312 Connection::CreateStatement(const nsACString& aSQLStatement,
2313 mozIStorageStatement** _stmt) {
2314 NS_ENSURE_ARG_POINTER(_stmt);
2315 if (!connectionReady()) {
2316 return NS_ERROR_NOT_INITIALIZED;
2318 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2319 if (NS_FAILED(rv)) {
2320 return rv;
2323 RefPtr<Statement> statement(new Statement());
2324 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2326 rv = statement->initialize(this, mDBConn, aSQLStatement);
2327 NS_ENSURE_SUCCESS(rv, rv);
2329 Statement* rawPtr;
2330 statement.forget(&rawPtr);
2331 *_stmt = rawPtr;
2332 return NS_OK;
2335 NS_IMETHODIMP
2336 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
2337 mozIStorageAsyncStatement** _stmt) {
2338 NS_ENSURE_ARG_POINTER(_stmt);
2339 if (!connectionReady()) {
2340 return NS_ERROR_NOT_INITIALIZED;
2342 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2343 if (NS_FAILED(rv)) {
2344 return rv;
2347 RefPtr<AsyncStatement> statement(new AsyncStatement());
2348 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2350 rv = statement->initialize(this, mDBConn, aSQLStatement);
2351 NS_ENSURE_SUCCESS(rv, rv);
2353 AsyncStatement* rawPtr;
2354 statement.forget(&rawPtr);
2355 *_stmt = rawPtr;
2356 return NS_OK;
2359 NS_IMETHODIMP
2360 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
2361 CHECK_MAINTHREAD_ABUSE();
2362 if (!connectionReady()) {
2363 return NS_ERROR_NOT_INITIALIZED;
2365 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2366 if (NS_FAILED(rv)) {
2367 return rv;
2370 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
2371 return convertResultCode(srv);
2374 NS_IMETHODIMP
2375 Connection::ExecuteAsync(
2376 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
2377 mozIStorageStatementCallback* aCallback,
2378 mozIStoragePendingStatement** _handle) {
2379 nsTArray<StatementData> stmts(aStatements.Length());
2380 for (uint32_t i = 0; i < aStatements.Length(); i++) {
2381 nsCOMPtr<StorageBaseStatementInternal> stmt =
2382 do_QueryInterface(aStatements[i]);
2383 NS_ENSURE_STATE(stmt);
2385 // Obtain our StatementData.
2386 StatementData data;
2387 nsresult rv = stmt->getAsynchronousStatementData(data);
2388 NS_ENSURE_SUCCESS(rv, rv);
2390 NS_ASSERTION(stmt->getOwner() == this,
2391 "Statement must be from this database connection!");
2393 // Now append it to our array.
2394 stmts.AppendElement(data);
2397 // Dispatch to the background
2398 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
2399 aCallback, _handle);
2402 NS_IMETHODIMP
2403 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
2404 mozIStorageStatementCallback* aCallback,
2405 mozIStoragePendingStatement** _handle) {
2406 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
2408 nsCOMPtr<mozIStorageAsyncStatement> stmt;
2409 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
2410 if (NS_FAILED(rv)) {
2411 return rv;
2414 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
2415 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
2416 if (NS_FAILED(rv)) {
2417 return rv;
2420 pendingStatement.forget(_handle);
2421 return rv;
2424 NS_IMETHODIMP
2425 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
2426 return databaseElementExists(TABLE, aTableName, _exists);
2429 NS_IMETHODIMP
2430 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2431 return databaseElementExists(INDEX, aIndexName, _exists);
2434 NS_IMETHODIMP
2435 Connection::GetTransactionInProgress(bool* _inProgress) {
2436 if (!connectionReady()) {
2437 return NS_ERROR_NOT_INITIALIZED;
2439 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2440 if (NS_FAILED(rv)) {
2441 return rv;
2444 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2445 *_inProgress = transactionInProgress(lockedScope);
2446 return NS_OK;
2449 NS_IMETHODIMP
2450 Connection::GetDefaultTransactionType(int32_t* _type) {
2451 *_type = mDefaultTransactionType;
2452 return NS_OK;
2455 NS_IMETHODIMP
2456 Connection::SetDefaultTransactionType(int32_t aType) {
2457 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2458 mDefaultTransactionType = aType;
2459 return NS_OK;
2462 NS_IMETHODIMP
2463 Connection::GetVariableLimit(int32_t* _limit) {
2464 if (!connectionReady()) {
2465 return NS_ERROR_NOT_INITIALIZED;
2467 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2468 if (limit < 0) {
2469 return NS_ERROR_UNEXPECTED;
2471 *_limit = limit;
2472 return NS_OK;
2475 NS_IMETHODIMP
2476 Connection::SetVariableLimit(int32_t limit) {
2477 if (!connectionReady()) {
2478 return NS_ERROR_NOT_INITIALIZED;
2480 int oldLimit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, limit);
2481 if (oldLimit < 0) {
2482 return NS_ERROR_UNEXPECTED;
2484 return NS_OK;
2487 NS_IMETHODIMP
2488 Connection::BeginTransaction() {
2489 if (!connectionReady()) {
2490 return NS_ERROR_NOT_INITIALIZED;
2492 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2493 if (NS_FAILED(rv)) {
2494 return rv;
2497 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2498 return beginTransactionInternal(lockedScope, mDBConn,
2499 mDefaultTransactionType);
2502 nsresult Connection::beginTransactionInternal(
2503 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2504 int32_t aTransactionType) {
2505 if (transactionInProgress(aProofOfLock)) {
2506 return NS_ERROR_FAILURE;
2508 nsresult rv;
2509 switch (aTransactionType) {
2510 case TRANSACTION_DEFERRED:
2511 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2512 break;
2513 case TRANSACTION_IMMEDIATE:
2514 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2515 break;
2516 case TRANSACTION_EXCLUSIVE:
2517 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2518 break;
2519 default:
2520 return NS_ERROR_ILLEGAL_VALUE;
2522 return rv;
2525 NS_IMETHODIMP
2526 Connection::CommitTransaction() {
2527 if (!connectionReady()) {
2528 return NS_ERROR_NOT_INITIALIZED;
2530 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2531 if (NS_FAILED(rv)) {
2532 return rv;
2535 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2536 return commitTransactionInternal(lockedScope, mDBConn);
2539 nsresult Connection::commitTransactionInternal(
2540 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2541 if (!transactionInProgress(aProofOfLock)) {
2542 return NS_ERROR_UNEXPECTED;
2544 nsresult rv =
2545 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2546 return rv;
2549 NS_IMETHODIMP
2550 Connection::RollbackTransaction() {
2551 if (!connectionReady()) {
2552 return NS_ERROR_NOT_INITIALIZED;
2554 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2555 if (NS_FAILED(rv)) {
2556 return rv;
2559 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2560 return rollbackTransactionInternal(lockedScope, mDBConn);
2563 nsresult Connection::rollbackTransactionInternal(
2564 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2565 if (!transactionInProgress(aProofOfLock)) {
2566 return NS_ERROR_UNEXPECTED;
2569 nsresult rv =
2570 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2571 return rv;
2574 NS_IMETHODIMP
2575 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2576 if (!connectionReady()) {
2577 return NS_ERROR_NOT_INITIALIZED;
2579 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2580 if (NS_FAILED(rv)) {
2581 return rv;
2584 SmprintfPointer buf =
2585 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2586 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2588 int srv = executeSql(mDBConn, buf.get());
2590 return convertResultCode(srv);
2593 NS_IMETHODIMP
2594 Connection::CreateFunction(const nsACString& aFunctionName,
2595 int32_t aNumArguments,
2596 mozIStorageFunction* aFunction) {
2597 if (!connectionReady()) {
2598 return NS_ERROR_NOT_INITIALIZED;
2600 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2601 if (NS_FAILED(rv)) {
2602 return rv;
2605 // Check to see if this function is already defined. We only check the name
2606 // because a function can be defined with the same body but different names.
2607 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2608 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2610 int srv = ::sqlite3_create_function(
2611 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2612 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2613 if (srv != SQLITE_OK) return convertResultCode(srv);
2615 FunctionInfo info = {aFunction, aNumArguments};
2616 mFunctions.InsertOrUpdate(aFunctionName, info);
2618 return NS_OK;
2621 NS_IMETHODIMP
2622 Connection::RemoveFunction(const nsACString& aFunctionName) {
2623 if (!connectionReady()) {
2624 return NS_ERROR_NOT_INITIALIZED;
2626 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2627 if (NS_FAILED(rv)) {
2628 return rv;
2631 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2632 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2634 int srv = ::sqlite3_create_function(
2635 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2636 nullptr, nullptr, nullptr, nullptr);
2637 if (srv != SQLITE_OK) return convertResultCode(srv);
2639 mFunctions.Remove(aFunctionName);
2641 return NS_OK;
2644 NS_IMETHODIMP
2645 Connection::SetProgressHandler(int32_t aGranularity,
2646 mozIStorageProgressHandler* aHandler,
2647 mozIStorageProgressHandler** _oldHandler) {
2648 if (!connectionReady()) {
2649 return NS_ERROR_NOT_INITIALIZED;
2651 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2652 if (NS_FAILED(rv)) {
2653 return rv;
2656 // Return previous one
2657 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2658 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2660 if (!aHandler || aGranularity <= 0) {
2661 aHandler = nullptr;
2662 aGranularity = 0;
2664 mProgressHandler = aHandler;
2665 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2667 return NS_OK;
2670 NS_IMETHODIMP
2671 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2672 if (!connectionReady()) {
2673 return NS_ERROR_NOT_INITIALIZED;
2675 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2676 if (NS_FAILED(rv)) {
2677 return rv;
2680 // Return previous one
2681 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2682 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2684 mProgressHandler = nullptr;
2685 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2687 return NS_OK;
2690 NS_IMETHODIMP
2691 Connection::SetGrowthIncrement(int32_t aChunkSize,
2692 const nsACString& aDatabaseName) {
2693 if (!connectionReady()) {
2694 return NS_ERROR_NOT_INITIALIZED;
2696 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2697 if (NS_FAILED(rv)) {
2698 return rv;
2701 // Bug 597215: Disk space is extremely limited on Android
2702 // so don't preallocate space. This is also not effective
2703 // on log structured file systems used by Android devices
2704 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2705 // Don't preallocate if less than 500MiB is available.
2706 int64_t bytesAvailable;
2707 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2708 NS_ENSURE_SUCCESS(rv, rv);
2709 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2710 return NS_ERROR_FILE_TOO_BIG;
2713 int srv = ::sqlite3_file_control(
2714 mDBConn,
2715 aDatabaseName.Length() ? nsPromiseFlatCString(aDatabaseName).get()
2716 : nullptr,
2717 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2718 if (srv == SQLITE_OK) {
2719 mGrowthChunkSize = aChunkSize;
2721 #endif
2722 return NS_OK;
2725 int32_t Connection::RemovablePagesInFreeList(const nsACString& aSchemaName) {
2726 int32_t freeListPagesCount = 0;
2727 if (!isConnectionReadyOnThisThread()) {
2728 MOZ_ASSERT(false, "Database connection is not ready");
2729 return freeListPagesCount;
2732 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2733 query.Append(aSchemaName);
2734 query.AppendLiteral(".freelist_count");
2735 nsCOMPtr<mozIStorageStatement> stmt;
2736 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2737 MOZ_ASSERT(NS_SUCCEEDED(rv));
2738 bool hasResult = false;
2739 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2740 freeListPagesCount = stmt->AsInt32(0);
2743 // If there's no chunk size set, any page is good to be removed.
2744 if (mGrowthChunkSize == 0 || freeListPagesCount == 0) {
2745 return freeListPagesCount;
2747 int32_t pageSize;
2749 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2750 query.Append(aSchemaName);
2751 query.AppendLiteral(".page_size");
2752 nsCOMPtr<mozIStorageStatement> stmt;
2753 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2754 MOZ_ASSERT(NS_SUCCEEDED(rv));
2755 bool hasResult = false;
2756 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2757 pageSize = stmt->AsInt32(0);
2758 } else {
2759 MOZ_ASSERT(false, "Couldn't get page_size");
2760 return 0;
2763 return std::max(0, freeListPagesCount - (mGrowthChunkSize / pageSize));
2766 NS_IMETHODIMP
2767 Connection::LoadExtension(const nsACString& aExtensionName,
2768 mozIStorageCompletionCallback* aCallback) {
2769 AUTO_PROFILER_LABEL("Connection::LoadExtension", OTHER);
2771 // This is a static list of extensions we can load.
2772 // Please use lowercase ASCII names and keep this list alphabetically ordered.
2773 static constexpr nsLiteralCString sSupportedExtensions[] = {
2774 // clang-format off
2775 "fts5"_ns,
2776 // clang-format on
2778 if (std::find(std::begin(sSupportedExtensions),
2779 std::end(sSupportedExtensions),
2780 aExtensionName) == std::end(sSupportedExtensions)) {
2781 return NS_ERROR_INVALID_ARG;
2784 if (!connectionReady()) {
2785 return NS_ERROR_NOT_INITIALIZED;
2788 int srv = ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION,
2789 1, nullptr);
2790 if (srv != SQLITE_OK) {
2791 return NS_ERROR_UNEXPECTED;
2794 // Track the loaded extension for later connection cloning operations.
2796 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2797 if (!mLoadedExtensions.EnsureInserted(aExtensionName)) {
2798 // Already loaded, bail out but issue a warning.
2799 NS_WARNING(nsPrintfCString(
2800 "Tried to register '%s' SQLite extension multiple times!",
2801 PromiseFlatCString(aExtensionName).get())
2802 .get());
2803 return NS_OK;
2807 nsAutoCString entryPoint("sqlite3_");
2808 entryPoint.Append(aExtensionName);
2809 entryPoint.AppendLiteral("_init");
2811 RefPtr<Runnable> loadTask = NS_NewRunnableFunction(
2812 "mozStorageConnection::LoadExtension",
2813 [this, self = RefPtr(this), entryPoint,
2814 callback = RefPtr(aCallback)]() mutable {
2815 MOZ_ASSERT(
2816 !NS_IsMainThread() ||
2817 (operationSupported(Connection::SYNCHRONOUS) &&
2818 eventTargetOpenedOn == GetMainThreadSerialEventTarget()),
2819 "Should happen on main-thread only for synchronous connections "
2820 "opened on the main thread");
2821 #ifdef MOZ_FOLD_LIBS
2822 int srv = ::sqlite3_load_extension(mDBConn,
2823 MOZ_DLL_PREFIX "nss3" MOZ_DLL_SUFFIX,
2824 entryPoint.get(), nullptr);
2825 #else
2826 int srv = ::sqlite3_load_extension(
2827 mDBConn, MOZ_DLL_PREFIX "mozsqlite3" MOZ_DLL_SUFFIX,
2828 entryPoint.get(), nullptr);
2829 #endif
2830 if (!callback) {
2831 return;
2833 RefPtr<Runnable> callbackTask = NS_NewRunnableFunction(
2834 "mozStorageConnection::LoadExtension_callback",
2835 [callback = std::move(callback), srv]() {
2836 (void)callback->Complete(convertResultCode(srv), nullptr);
2838 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
2839 MOZ_ALWAYS_SUCCEEDS(callbackTask->Run());
2840 } else {
2841 // Redispatch the callback to the calling thread.
2842 MOZ_ALWAYS_SUCCEEDS(eventTargetOpenedOn->Dispatch(
2843 callbackTask.forget(), NS_DISPATCH_NORMAL));
2847 if (NS_IsMainThread() && !operationSupported(Connection::SYNCHRONOUS)) {
2848 // This is a main-thread call to an async-only connection, thus we should
2849 // load the library in the helper thread.
2850 nsIEventTarget* helperThread = getAsyncExecutionTarget();
2851 if (!helperThread) {
2852 return NS_ERROR_NOT_INITIALIZED;
2854 MOZ_ALWAYS_SUCCEEDS(
2855 helperThread->Dispatch(loadTask.forget(), NS_DISPATCH_NORMAL));
2856 } else {
2857 // In any other case we just load the extension on the current thread.
2858 MOZ_ALWAYS_SUCCEEDS(loadTask->Run());
2860 return NS_OK;
2863 NS_IMETHODIMP
2864 Connection::EnableModule(const nsACString& aModuleName) {
2865 if (!connectionReady()) {
2866 return NS_ERROR_NOT_INITIALIZED;
2868 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2869 if (NS_FAILED(rv)) {
2870 return rv;
2873 for (auto& gModule : gModules) {
2874 struct Module* m = &gModule;
2875 if (aModuleName.Equals(m->name)) {
2876 int srv = m->registerFunc(mDBConn, m->name);
2877 if (srv != SQLITE_OK) return convertResultCode(srv);
2879 return NS_OK;
2883 return NS_ERROR_FAILURE;
2886 NS_IMETHODIMP
2887 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2888 QuotaObject** aJournalQuotaObject) {
2889 MOZ_ASSERT(aDatabaseQuotaObject);
2890 MOZ_ASSERT(aJournalQuotaObject);
2892 if (!connectionReady()) {
2893 return NS_ERROR_NOT_INITIALIZED;
2895 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2896 if (NS_FAILED(rv)) {
2897 return rv;
2900 sqlite3_file* file;
2901 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2902 &file);
2903 if (srv != SQLITE_OK) {
2904 return convertResultCode(srv);
2907 sqlite3_vfs* vfs;
2908 srv =
2909 ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_VFS_POINTER, &vfs);
2910 if (srv != SQLITE_OK) {
2911 return convertResultCode(srv);
2914 bool obfusactingVFS = false;
2917 const nsDependentCString vfsName{vfs->zName};
2919 if (vfsName == obfsvfs::GetVFSName()) {
2920 obfusactingVFS = true;
2921 } else if (vfsName != quotavfs::GetVFSName()) {
2922 NS_WARNING("Got unexpected vfs");
2923 return NS_ERROR_FAILURE;
2927 RefPtr<QuotaObject> databaseQuotaObject =
2928 GetQuotaObject(file, obfusactingVFS);
2929 if (NS_WARN_IF(!databaseQuotaObject)) {
2930 return NS_ERROR_FAILURE;
2933 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2934 &file);
2935 if (srv != SQLITE_OK) {
2936 return convertResultCode(srv);
2939 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObject(file, obfusactingVFS);
2940 if (NS_WARN_IF(!journalQuotaObject)) {
2941 return NS_ERROR_FAILURE;
2944 databaseQuotaObject.forget(aDatabaseQuotaObject);
2945 journalQuotaObject.forget(aJournalQuotaObject);
2946 return NS_OK;
2949 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2951 uint32_t Connection::GetTransactionNestingLevel(
2952 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2953 return mTransactionNestingLevel;
2956 uint32_t Connection::IncreaseTransactionNestingLevel(
2957 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2958 return ++mTransactionNestingLevel;
2961 uint32_t Connection::DecreaseTransactionNestingLevel(
2962 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2963 return --mTransactionNestingLevel;
2966 NS_IMETHODIMP
2967 Connection::BackupToFileAsync(nsIFile* aDestinationFile,
2968 mozIStorageCompletionCallback* aCallback,
2969 uint32_t aPagesPerStep, uint32_t aStepDelayMs) {
2970 NS_ENSURE_ARG(aDestinationFile);
2971 NS_ENSURE_ARG(aCallback);
2972 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
2974 // Abort if we're shutting down.
2975 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
2976 return NS_ERROR_ABORT;
2978 // Check if AsyncClose or Close were already invoked.
2979 if (!connectionReady()) {
2980 return NS_ERROR_NOT_INITIALIZED;
2982 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2983 if (NS_FAILED(rv)) {
2984 return rv;
2986 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
2987 if (!asyncThread) {
2988 return NS_ERROR_NOT_INITIALIZED;
2991 // The number of pages of the database to copy per step
2992 static constexpr int32_t DEFAULT_PAGES_PER_STEP = 5;
2993 // The number of milliseconds to wait between each step.
2994 static constexpr uint32_t DEFAULT_STEP_DELAY_MS = 250;
2996 CheckedInt<int32_t> pagesPerStep(aPagesPerStep);
2997 if (!pagesPerStep.isValid()) {
2998 return NS_ERROR_INVALID_ARG;
3001 if (!pagesPerStep.value()) {
3002 pagesPerStep = DEFAULT_PAGES_PER_STEP;
3005 if (!aStepDelayMs) {
3006 aStepDelayMs = DEFAULT_STEP_DELAY_MS;
3009 // Create and dispatch our backup event to the execution thread.
3010 nsCOMPtr<nsIRunnable> backupEvent =
3011 new AsyncBackupDatabaseFile(this, mDBConn, aDestinationFile, aCallback,
3012 pagesPerStep.value(), aStepDelayMs);
3013 rv = asyncThread->Dispatch(backupEvent, NS_DISPATCH_NORMAL);
3014 return rv;
3017 } // namespace mozilla::storage