Merge mozilla-central to autoland. a=merge CLOSED TREE
[gecko.git] / storage / mozStorageConnection.cpp
blobdc3df7c95229f599a73f6d6aed4fac4996abcb3f
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 : Runnable("storage::AsyncBackupDatabaseFile"),
611 mConnection(aConnection),
612 mNativeConnection(aNativeConnection),
613 mDestinationFile(aDestinationFile),
614 mCallback(aCallback),
615 mBackupFile(nullptr),
616 mBackupHandle(nullptr) {
617 MOZ_ASSERT(NS_IsMainThread());
620 NS_IMETHOD Run() override {
621 MOZ_ASSERT(!NS_IsMainThread());
623 nsAutoString path;
624 nsresult rv = mDestinationFile->GetPath(path);
625 if (NS_FAILED(rv)) {
626 return Dispatch(rv, nullptr);
628 // Put a .tmp on the end of the destination while the backup is underway.
629 // This extension will be stripped off after the backup successfully
630 // completes.
631 path.AppendLiteral(".tmp");
633 int srv = ::sqlite3_open(NS_ConvertUTF16toUTF8(path).get(), &mBackupFile);
634 if (srv != SQLITE_OK) {
635 return Dispatch(NS_ERROR_FAILURE, nullptr);
638 static const char* mainDBName = "main";
640 mBackupHandle = ::sqlite3_backup_init(mBackupFile, mainDBName,
641 mNativeConnection, mainDBName);
642 if (!mBackupHandle) {
643 MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK);
644 return Dispatch(NS_ERROR_FAILURE, nullptr);
647 return DoStep();
650 NS_IMETHOD
651 Notify(nsITimer* aTimer) override { return DoStep(); }
653 private:
654 nsresult DoStep() {
655 #define DISPATCH_AND_RETURN_IF_FAILED(rv) \
656 if (NS_FAILED(rv)) { \
657 return Dispatch(rv, nullptr); \
660 // This guard is used to close the backup database in the event of
661 // some failure throughout this process. We release the exit guard
662 // only if we complete the backup successfully, or defer to another
663 // later call to DoStep.
664 auto guard = MakeScopeExit([&]() {
665 MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK);
666 mBackupFile = nullptr;
669 MOZ_ASSERT(!NS_IsMainThread());
670 nsAutoString originalPath;
671 nsresult rv = mDestinationFile->GetPath(originalPath);
672 DISPATCH_AND_RETURN_IF_FAILED(rv);
674 nsAutoString tempPath = originalPath;
675 tempPath.AppendLiteral(".tmp");
677 nsCOMPtr<nsIFile> file =
678 do_CreateInstance("@mozilla.org/file/local;1", &rv);
679 DISPATCH_AND_RETURN_IF_FAILED(rv);
681 rv = file->InitWithPath(tempPath);
682 DISPATCH_AND_RETURN_IF_FAILED(rv);
684 // The number of milliseconds to wait between each batch of copies.
685 static constexpr uint32_t STEP_DELAY_MS = 250;
686 // The number of pages to copy per step
687 static constexpr int COPY_PAGES = 5;
689 int srv = ::sqlite3_backup_step(mBackupHandle, COPY_PAGES);
690 if (srv == SQLITE_OK || srv == SQLITE_BUSY || srv == SQLITE_LOCKED) {
691 // We're continuing the backup later. Release the guard to avoid closing
692 // the database.
693 guard.release();
694 // Queue up the next step
695 return NS_NewTimerWithCallback(getter_AddRefs(mTimer), this,
696 STEP_DELAY_MS, nsITimer::TYPE_ONE_SHOT,
697 GetCurrentSerialEventTarget());
699 #ifdef DEBUG
700 if (srv != SQLITE_DONE) {
701 nsCString warnMsg;
702 warnMsg.AppendLiteral(
703 "The SQLite database copy could not be completed due to an error: ");
704 warnMsg.Append(::sqlite3_errmsg(mBackupFile));
705 NS_WARNING(warnMsg.get());
707 #endif
709 (void)::sqlite3_backup_finish(mBackupHandle);
710 MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK);
711 mBackupFile = nullptr;
713 // The database is already closed, so we can release this guard now.
714 guard.release();
716 if (srv != SQLITE_DONE) {
717 NS_WARNING("Failed to create database copy.");
719 // The partially created database file is not useful. Let's remove it.
720 rv = file->Remove(false);
721 if (NS_FAILED(rv)) {
722 NS_WARNING(
723 "Removing a partially backed up SQLite database file failed.");
726 return Dispatch(convertResultCode(srv), nullptr);
729 // Now that we've successfully created the copy, we'll strip off the .tmp
730 // extension.
732 nsAutoString leafName;
733 rv = mDestinationFile->GetLeafName(leafName);
734 DISPATCH_AND_RETURN_IF_FAILED(rv);
736 rv = file->RenameTo(nullptr, leafName);
737 DISPATCH_AND_RETURN_IF_FAILED(rv);
739 #undef DISPATCH_AND_RETURN_IF_FAILED
740 return Dispatch(NS_OK, nullptr);
743 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
744 RefPtr<CallbackComplete> event =
745 new CallbackComplete(aResult, aValue, mCallback.forget());
746 return mConnection->eventTargetOpenedOn->Dispatch(event,
747 NS_DISPATCH_NORMAL);
750 ~AsyncBackupDatabaseFile() override {
751 nsresult rv;
752 nsCOMPtr<nsIThread> thread =
753 do_QueryInterface(mConnection->eventTargetOpenedOn, &rv);
754 MOZ_ASSERT(NS_SUCCEEDED(rv));
756 // Handle ambiguous nsISupports inheritance.
757 NS_ProxyRelease("AsyncBackupDatabaseFile::mConnection", thread,
758 mConnection.forget());
759 NS_ProxyRelease("AsyncBackupDatabaseFile::mDestinationFile", thread,
760 mDestinationFile.forget());
762 // Generally, the callback will be released by CallbackComplete.
763 // However, if for some reason Run() is not executed, we still
764 // need to ensure that it is released here.
765 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
766 mCallback.forget());
769 RefPtr<Connection> mConnection;
770 sqlite3* mNativeConnection;
771 nsCOMPtr<nsITimer> mTimer;
772 nsCOMPtr<nsIFile> mDestinationFile;
773 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
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)
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 mAsyncExecutionThreadShuttingDown(false),
805 mConnectionClosed(false),
806 mGrowthChunkSize(0) {
807 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
808 "Can't ignore locking for a non-readonly connection!");
809 mStorageService->registerConnection(this);
810 MOZ_ASSERT(!aTelemetryFilename.IsEmpty(),
811 "A telemetry filename should have been passed-in.");
812 mTelemetryFilename.Assign(aTelemetryFilename);
815 Connection::~Connection() {
816 // Failsafe Close() occurs in our custom Release method because of
817 // complications related to Close() potentially invoking AsyncClose() which
818 // will increment our refcount.
819 MOZ_ASSERT(!mAsyncExecutionThread,
820 "The async thread has not been shutdown properly!");
823 NS_IMPL_ADDREF(Connection)
825 NS_INTERFACE_MAP_BEGIN(Connection)
826 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
827 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
828 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
829 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
830 NS_INTERFACE_MAP_END
832 // This is identical to what NS_IMPL_RELEASE provides, but with the
833 // extra |1 == count| case.
834 NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
835 MOZ_ASSERT(0 != mRefCnt, "dup release");
836 nsrefcnt count = --mRefCnt;
837 NS_LOG_RELEASE(this, count, "Connection");
838 if (1 == count) {
839 // If the refcount went to 1, the single reference must be from
840 // gService->mConnections (in class |Service|). And the code calling
841 // Release is either:
842 // - The "user" code that had created the connection, releasing on any
843 // thread.
844 // - One of Service's getConnections() callers had acquired a strong
845 // reference to the Connection that out-lived the last "user" reference,
846 // and now that just got dropped. Note that this reference could be
847 // getting dropped on the main thread or Connection->eventTargetOpenedOn
848 // (because of the NewRunnableMethod used by minimizeMemory).
850 // Either way, we should now perform our failsafe Close() and unregister.
851 // However, we only want to do this once, and the reality is that our
852 // refcount could go back up above 1 and down again at any time if we are
853 // off the main thread and getConnections() gets called on the main thread,
854 // so we use an atomic here to do this exactly once.
855 if (mDestroying.compareExchange(false, true)) {
856 // Close the connection, dispatching to the opening event target if we're
857 // not on that event target already and that event target is still
858 // accepting runnables. We do this because it's possible we're on the main
859 // thread because of getConnections(), and we REALLY don't want to
860 // transfer I/O to the main thread if we can avoid it.
861 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
862 // This could cause SpinningSynchronousClose() to be invoked and AddRef
863 // triggered for AsyncCloseConnection's strong ref if the conn was ever
864 // use for async purposes. (Main-thread only, though.)
865 Unused << synchronousClose();
866 } else {
867 nsCOMPtr<nsIRunnable> event =
868 NewRunnableMethod("storage::Connection::synchronousClose", this,
869 &Connection::synchronousClose);
870 if (NS_FAILED(eventTargetOpenedOn->Dispatch(event.forget(),
871 NS_DISPATCH_NORMAL))) {
872 // The event target was dead and so we've just leaked our runnable.
873 // This should not happen because our non-main-thread consumers should
874 // be explicitly closing their connections, not relying on us to close
875 // them for them. (It's okay to let a statement go out of scope for
876 // automatic cleanup, but not a Connection.)
877 MOZ_ASSERT(false,
878 "Leaked Connection::synchronousClose(), ownership fail.");
879 Unused << synchronousClose();
883 // This will drop its strong reference right here, right now.
884 mStorageService->unregisterConnection(this);
886 } else if (0 == count) {
887 mRefCnt = 1; /* stabilize */
888 #if 0 /* enable this to find non-threadsafe destructors: */
889 NS_ASSERT_OWNINGTHREAD(Connection);
890 #endif
891 delete (this);
892 return 0;
894 return count;
897 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
898 int32_t* aMaxValue) {
899 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
900 int curr = 0, max = 0;
901 DebugOnly<int> rc =
902 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
903 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
904 if (aMaxValue) *aMaxValue = max;
905 return curr;
908 nsIEventTarget* Connection::getAsyncExecutionTarget() {
909 NS_ENSURE_TRUE(IsOnCurrentSerialEventTarget(eventTargetOpenedOn), nullptr);
911 // Don't return the asynchronous event target if we are shutting down.
912 if (mAsyncExecutionThreadShuttingDown) {
913 return nullptr;
916 // Create the async event target if there's none yet.
917 if (!mAsyncExecutionThread) {
918 // Names start with "sqldb:" followed by a recognizable name, like the
919 // database file name, or a specially crafted name like "memory".
920 // This name will be surfaced on https://crash-stats.mozilla.org, so any
921 // sensitive part of the file name (e.g. an URL origin) should be replaced
922 // by passing an explicit telemetryName to openDatabaseWithFileURL.
923 nsAutoCString name("sqldb:"_ns);
924 name.Append(mTelemetryFilename);
925 static nsThreadPoolNaming naming;
926 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName(name),
927 getter_AddRefs(mAsyncExecutionThread));
928 if (NS_FAILED(rv)) {
929 NS_WARNING("Failed to create async thread.");
930 return nullptr;
932 mAsyncExecutionThread->SetNameForWakeupTelemetry("mozStorage (all)"_ns);
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 = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
1090 int srv;
1091 if (mIgnoreLockingMode) {
1092 exclusive = false;
1093 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
1094 "readonly-immutable-nolock");
1095 } else {
1096 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
1097 basevfs::GetVFSName(exclusive));
1098 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
1099 // Retry without trying to get an exclusive lock.
1100 exclusive = false;
1101 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
1102 mFlags, basevfs::GetVFSName(false));
1105 if (srv != SQLITE_OK) {
1106 mDBConn = nullptr;
1107 rv = convertResultCode(srv);
1108 RecordOpenStatus(rv);
1109 return rv;
1112 rv = initializeInternal();
1113 if (exclusive &&
1114 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
1115 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
1116 // some cases it may successfully open the database and then lock on the
1117 // first query execution. When initializeInternal fails it closes the
1118 // connection, so we can try to restart it in non-exclusive mode.
1119 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
1120 basevfs::GetVFSName(false));
1121 if (srv == SQLITE_OK) {
1122 rv = initializeInternal();
1126 RecordOpenStatus(rv);
1127 NS_ENSURE_SUCCESS(rv, rv);
1129 return NS_OK;
1132 nsresult Connection::initialize(nsIFileURL* aFileURL) {
1133 NS_ASSERTION(aFileURL, "Passed null file URL!");
1134 NS_ASSERTION(!connectionReady(),
1135 "Initialize called on already opened database!");
1136 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
1138 nsCOMPtr<nsIFile> databaseFile;
1139 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
1140 NS_ENSURE_SUCCESS(rv, rv);
1142 // Set both mDatabaseFile and mFileURL here.
1143 mFileURL = aFileURL;
1144 mDatabaseFile = databaseFile;
1146 nsAutoCString spec;
1147 rv = aFileURL->GetSpec(spec);
1148 NS_ENSURE_SUCCESS(rv, rv);
1150 // If there is a key specified, we need to use the obfuscating VFS.
1151 nsAutoCString query;
1152 rv = aFileURL->GetQuery(query);
1153 NS_ENSURE_SUCCESS(rv, rv);
1155 bool hasKey = false;
1156 bool hasDirectoryLockId = false;
1158 MOZ_ALWAYS_TRUE(URLParams::Parse(
1159 query, [&hasKey, &hasDirectoryLockId](const nsAString& aName,
1160 const nsAString& aValue) {
1161 if (aName.EqualsLiteral("key")) {
1162 hasKey = true;
1163 return true;
1165 if (aName.EqualsLiteral("directoryLockId")) {
1166 hasDirectoryLockId = true;
1167 return true;
1169 return true;
1170 }));
1172 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
1174 const char* const vfs = hasKey ? obfsvfs::GetVFSName()
1175 : hasDirectoryLockId ? quotavfs::GetVFSName()
1176 : basevfs::GetVFSName(exclusive);
1178 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
1179 if (srv != SQLITE_OK) {
1180 mDBConn = nullptr;
1181 rv = convertResultCode(srv);
1182 RecordOpenStatus(rv);
1183 return rv;
1186 rv = initializeInternal();
1187 RecordOpenStatus(rv);
1188 NS_ENSURE_SUCCESS(rv, rv);
1190 return NS_OK;
1193 nsresult Connection::initializeInternal() {
1194 MOZ_ASSERT(mDBConn);
1195 auto guard = MakeScopeExit([&]() { initializeFailed(); });
1197 mConnectionClosed = false;
1199 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
1200 DebugOnly<int> srv2 =
1201 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
1202 MOZ_ASSERT(srv2 == SQLITE_OK,
1203 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
1204 #endif
1206 // Properly wrap the database handle's mutex.
1207 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
1209 // SQLite tracing can slow down queries (especially long queries)
1210 // significantly. Don't trace unless the user is actively monitoring SQLite.
1211 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1212 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
1213 tracefunc, this);
1215 MOZ_LOG(
1216 gStorageLog, LogLevel::Debug,
1217 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
1220 int64_t pageSize = Service::kDefaultPageSize;
1222 // Set page_size to the preferred default value. This is effective only if
1223 // the database has just been created, otherwise, if the database does not
1224 // use WAL journal mode, a VACUUM operation will updated its page_size.
1225 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1226 "PRAGMA page_size = ");
1227 pageSizeQuery.AppendInt(pageSize);
1228 int srv = executeSql(mDBConn, pageSizeQuery.get());
1229 if (srv != SQLITE_OK) {
1230 return convertResultCode(srv);
1233 // Setting the cache_size forces the database open, verifying if it is valid
1234 // or corrupt. So this is executed regardless it being actually needed.
1235 // The cache_size is calculated from the actual page_size, to save memory.
1236 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1237 "PRAGMA cache_size = ");
1238 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
1239 srv = executeSql(mDBConn, cacheSizeQuery.get());
1240 if (srv != SQLITE_OK) {
1241 return convertResultCode(srv);
1244 // Register our built-in SQL functions.
1245 srv = registerFunctions(mDBConn);
1246 if (srv != SQLITE_OK) {
1247 return convertResultCode(srv);
1250 // Register our built-in SQL collating sequences.
1251 srv = registerCollations(mDBConn, mStorageService);
1252 if (srv != SQLITE_OK) {
1253 return convertResultCode(srv);
1256 // Set the default synchronous value. Each consumer can switch this
1257 // accordingly to their needs.
1258 #if defined(ANDROID)
1259 // Android prefers synchronous = OFF for performance reasons.
1260 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
1261 #else
1262 // Normal is the suggested value for WAL journals.
1263 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
1264 #endif
1266 // Initialization succeeded, we can stop guarding for failures.
1267 guard.release();
1268 return NS_OK;
1271 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
1272 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1273 nsresult rv = aStorageFile
1274 ? initialize(aStorageFile)
1275 : initialize(kMozStorageMemoryStorageKey, VoidCString());
1276 if (NS_FAILED(rv)) {
1277 // Shutdown the async thread, since initialization failed.
1278 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1279 mAsyncExecutionThreadShuttingDown = true;
1280 nsCOMPtr<nsIRunnable> event =
1281 NewRunnableMethod("Connection::shutdownAsyncThread", this,
1282 &Connection::shutdownAsyncThread);
1283 Unused << NS_DispatchToMainThread(event);
1285 return rv;
1288 void Connection::initializeFailed() {
1290 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1291 mConnectionClosed = true;
1293 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
1294 mDBConn = nullptr;
1295 sharedDBMutex.destroy();
1298 nsresult Connection::databaseElementExists(
1299 enum DatabaseElementType aElementType, const nsACString& aElementName,
1300 bool* _exists) {
1301 if (!connectionReady()) {
1302 return NS_ERROR_NOT_AVAILABLE;
1304 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1305 if (NS_FAILED(rv)) {
1306 return rv;
1309 // When constructing the query, make sure to SELECT the correct db's
1310 // sqlite_master if the user is prefixing the element with a specific db. ex:
1311 // sample.test
1312 nsCString query("SELECT name FROM (SELECT * FROM ");
1313 nsDependentCSubstring element;
1314 int32_t ind = aElementName.FindChar('.');
1315 if (ind == kNotFound) {
1316 element.Assign(aElementName);
1317 } else {
1318 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
1319 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
1320 query.Append(db);
1322 query.AppendLiteral(
1323 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
1324 "'");
1326 switch (aElementType) {
1327 case INDEX:
1328 query.AppendLiteral("index");
1329 break;
1330 case TABLE:
1331 query.AppendLiteral("table");
1332 break;
1334 query.AppendLiteral("' AND name ='");
1335 query.Append(element);
1336 query.Append('\'');
1338 sqlite3_stmt* stmt;
1339 int srv = prepareStatement(mDBConn, query, &stmt);
1340 if (srv != SQLITE_OK) {
1341 RecordQueryStatus(srv);
1342 return convertResultCode(srv);
1345 srv = stepStatement(mDBConn, stmt);
1346 // we just care about the return value from step
1347 (void)::sqlite3_finalize(stmt);
1349 RecordQueryStatus(srv);
1351 if (srv == SQLITE_ROW) {
1352 *_exists = true;
1353 return NS_OK;
1355 if (srv == SQLITE_DONE) {
1356 *_exists = false;
1357 return NS_OK;
1360 return convertResultCode(srv);
1363 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
1364 sharedDBMutex.assertCurrentThreadOwns();
1366 for (const auto& data : mFunctions.Values()) {
1367 if (data.function == aInstance) {
1368 return true;
1371 return false;
1374 /* static */
1375 int Connection::sProgressHelper(void* aArg) {
1376 Connection* _this = static_cast<Connection*>(aArg);
1377 return _this->progressHandler();
1380 int Connection::progressHandler() {
1381 sharedDBMutex.assertCurrentThreadOwns();
1382 if (mProgressHandler) {
1383 bool result;
1384 nsresult rv = mProgressHandler->OnProgress(this, &result);
1385 if (NS_FAILED(rv)) return 0; // Don't break request
1386 return result ? 1 : 0;
1388 return 0;
1391 nsresult Connection::setClosedState() {
1392 // Flag that we are shutting down the async thread, so that
1393 // getAsyncExecutionTarget knows not to expose/create the async thread.
1394 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1395 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1397 mAsyncExecutionThreadShuttingDown = true;
1399 // Set the property to null before closing the connection, otherwise the
1400 // other functions in the module may try to use the connection after it is
1401 // closed.
1402 mDBConn = nullptr;
1404 return NS_OK;
1407 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1408 if (aOperationType == ASYNCHRONOUS) {
1409 // Async operations are supported for all connections, on any thread.
1410 return true;
1412 // Sync operations are supported for sync connections (on any thread), and
1413 // async connections on a background thread.
1414 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1415 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1418 nsresult Connection::ensureOperationSupported(
1419 ConnectionOperation aOperationType) {
1420 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1421 #ifdef DEBUG
1422 if (NS_IsMainThread()) {
1423 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1424 Unused << xpc->DebugDumpJSStack(false, false, false);
1426 #endif
1427 MOZ_ASSERT(false,
1428 "Don't use async connections synchronously on the main thread");
1429 return NS_ERROR_NOT_AVAILABLE;
1431 return NS_OK;
1434 bool Connection::isConnectionReadyOnThisThread() {
1435 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1436 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1437 return true;
1439 return connectionReady();
1442 bool Connection::isClosing() {
1443 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1444 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1447 bool Connection::isClosed() {
1448 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1449 return mConnectionClosed;
1452 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1454 bool Connection::isAsyncExecutionThreadAvailable() {
1455 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1456 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1459 void Connection::shutdownAsyncThread() {
1460 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1461 MOZ_ASSERT(mAsyncExecutionThread);
1462 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1464 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1465 mAsyncExecutionThread = nullptr;
1468 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1469 #ifdef DEBUG
1470 { // Make sure we have marked our async thread as shutting down.
1471 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1472 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1473 "Did not call setClosedState!");
1474 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1476 #endif // DEBUG
1478 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1479 nsAutoCString leafName(":memory");
1480 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1481 MOZ_LOG(gStorageLog, LogLevel::Debug,
1482 ("Closing connection to '%s'", leafName.get()));
1485 // At this stage, we may still have statements that need to be
1486 // finalized. Attempt to close the database connection. This will
1487 // always disconnect any virtual tables and cleanly finalize their
1488 // internal statements. Once this is done, closing may fail due to
1489 // unfinalized client statements, in which case we need to finalize
1490 // these statements and close again.
1492 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1493 mConnectionClosed = true;
1496 // Nothing else needs to be done if we don't have a connection here.
1497 if (!aNativeConnection) return NS_OK;
1499 int srv = ::sqlite3_close(aNativeConnection);
1501 if (srv == SQLITE_BUSY) {
1503 // Nothing else should change the connection or statements status until we
1504 // are done here.
1505 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1506 // We still have non-finalized statements. Finalize them.
1507 sqlite3_stmt* stmt = nullptr;
1508 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1509 MOZ_LOG(gStorageLog, LogLevel::Debug,
1510 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1511 stmt));
1513 #ifdef DEBUG
1514 SmprintfPointer msg = ::mozilla::Smprintf(
1515 "SQL statement '%s' (%p) should have been finalized before closing "
1516 "the connection",
1517 ::sqlite3_sql(stmt), stmt);
1518 NS_WARNING(msg.get());
1519 #endif // DEBUG
1521 srv = ::sqlite3_finalize(stmt);
1523 #ifdef DEBUG
1524 if (srv != SQLITE_OK) {
1525 SmprintfPointer msg = ::mozilla::Smprintf(
1526 "Could not finalize SQL statement (%p)", stmt);
1527 NS_WARNING(msg.get());
1529 #endif // DEBUG
1531 // Ensure that the loop continues properly, whether closing has
1532 // succeeded or not.
1533 if (srv == SQLITE_OK) {
1534 stmt = nullptr;
1537 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1538 // again, since Sqlite will try to acquire it.
1541 // Now that all statements have been finalized, we
1542 // should be able to close.
1543 srv = ::sqlite3_close(aNativeConnection);
1544 MOZ_ASSERT(false,
1545 "Had to forcibly close the database connection because not all "
1546 "the statements have been finalized.");
1549 if (srv == SQLITE_OK) {
1550 sharedDBMutex.destroy();
1551 } else {
1552 MOZ_ASSERT(false,
1553 "sqlite3_close failed. There are probably outstanding "
1554 "statements that are listed above!");
1557 return convertResultCode(srv);
1560 nsCString Connection::getFilename() { return mTelemetryFilename; }
1562 int Connection::stepStatement(sqlite3* aNativeConnection,
1563 sqlite3_stmt* aStatement) {
1564 MOZ_ASSERT(aStatement);
1566 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1567 ::sqlite3_sql(aStatement));
1569 bool checkedMainThread = false;
1570 TimeStamp startTime = TimeStamp::Now();
1572 // The connection may have been closed if the executing statement has been
1573 // created and cached after a call to asyncClose() but before the actual
1574 // sqlite3_close(). This usually happens when other tasks using cached
1575 // statements are asynchronously scheduled for execution and any of them ends
1576 // up after asyncClose. See bug 728653 for details.
1577 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1579 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1581 int srv;
1582 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1583 if (!checkedMainThread) {
1584 checkedMainThread = true;
1585 if (::NS_IsMainThread()) {
1586 NS_WARNING("We won't allow blocking on the main thread!");
1587 break;
1591 srv = WaitForUnlockNotify(aNativeConnection);
1592 if (srv != SQLITE_OK) {
1593 break;
1596 ::sqlite3_reset(aStatement);
1599 // Report very slow SQL statements to Telemetry
1600 TimeDuration duration = TimeStamp::Now() - startTime;
1601 const uint32_t threshold = NS_IsMainThread()
1602 ? Telemetry::kSlowSQLThresholdForMainThread
1603 : Telemetry::kSlowSQLThresholdForHelperThreads;
1604 if (duration.ToMilliseconds() >= threshold) {
1605 nsDependentCString statementString(::sqlite3_sql(aStatement));
1606 Telemetry::RecordSlowSQLStatement(
1607 statementString, mTelemetryFilename,
1608 static_cast<uint32_t>(duration.ToMilliseconds()));
1611 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1612 // Drop off the extended result bits of the result code.
1613 return srv & 0xFF;
1616 int Connection::prepareStatement(sqlite3* aNativeConnection,
1617 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1618 // We should not even try to prepare statements after the connection has
1619 // been closed.
1620 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1622 bool checkedMainThread = false;
1624 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1626 int srv;
1627 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1628 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1629 if (!checkedMainThread) {
1630 checkedMainThread = true;
1631 if (::NS_IsMainThread()) {
1632 NS_WARNING("We won't allow blocking on the main thread!");
1633 break;
1637 srv = WaitForUnlockNotify(aNativeConnection);
1638 if (srv != SQLITE_OK) {
1639 break;
1643 if (srv != SQLITE_OK) {
1644 nsCString warnMsg;
1645 warnMsg.AppendLiteral("The SQL statement '");
1646 warnMsg.Append(aSQL);
1647 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1648 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1650 #ifdef DEBUG
1651 NS_WARNING(warnMsg.get());
1652 #endif
1653 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1656 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1657 // Drop off the extended result bits of the result code.
1658 int rc = srv & 0xFF;
1659 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1660 // The callers of this function are used to only checking the return value,
1661 // so it is safer to return an error code.
1662 if (rc == SQLITE_OK && *_stmt == nullptr) {
1663 return SQLITE_MISUSE;
1666 return rc;
1669 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1670 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1672 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1674 TimeStamp startTime = TimeStamp::Now();
1675 int srv =
1676 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1677 RecordQueryStatus(srv);
1679 // Report very slow SQL statements to Telemetry
1680 TimeDuration duration = TimeStamp::Now() - startTime;
1681 const uint32_t threshold = NS_IsMainThread()
1682 ? Telemetry::kSlowSQLThresholdForMainThread
1683 : Telemetry::kSlowSQLThresholdForHelperThreads;
1684 if (duration.ToMilliseconds() >= threshold) {
1685 nsDependentCString statementString(aSqlString);
1686 Telemetry::RecordSlowSQLStatement(
1687 statementString, mTelemetryFilename,
1688 static_cast<uint32_t>(duration.ToMilliseconds()));
1691 return srv;
1694 ////////////////////////////////////////////////////////////////////////////////
1695 //// nsIInterfaceRequestor
1697 NS_IMETHODIMP
1698 Connection::GetInterface(const nsIID& aIID, void** _result) {
1699 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1700 nsIEventTarget* background = getAsyncExecutionTarget();
1701 NS_IF_ADDREF(background);
1702 *_result = background;
1703 return NS_OK;
1705 return NS_ERROR_NO_INTERFACE;
1708 ////////////////////////////////////////////////////////////////////////////////
1709 //// mozIStorageConnection
1711 NS_IMETHODIMP
1712 Connection::Close() {
1713 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1714 if (NS_FAILED(rv)) {
1715 return rv;
1717 return synchronousClose();
1720 nsresult Connection::synchronousClose() {
1721 if (!connectionReady()) {
1722 return NS_ERROR_NOT_INITIALIZED;
1725 #ifdef DEBUG
1726 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1727 // event target. We make this check outside of debug code below in
1728 // setClosedState, but this is here to be explicit.
1729 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1730 #endif // DEBUG
1732 // Make sure we have not executed any asynchronous statements.
1733 // If this fails, the mDBConn may be left open, resulting in a leak.
1734 // We'll try to finalize the pending statements and close the connection.
1735 if (isAsyncExecutionThreadAvailable()) {
1736 #ifdef DEBUG
1737 if (NS_IsMainThread()) {
1738 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1739 Unused << xpc->DebugDumpJSStack(false, false, false);
1741 #endif
1742 MOZ_ASSERT(false,
1743 "Close() was invoked on a connection that executed asynchronous "
1744 "statements. "
1745 "Should have used asyncClose().");
1746 // Try to close the database regardless, to free up resources.
1747 Unused << SpinningSynchronousClose();
1748 return NS_ERROR_UNEXPECTED;
1751 // setClosedState nullifies our connection pointer, so we take a raw pointer
1752 // off it, to pass it through the close procedure.
1753 sqlite3* nativeConn = mDBConn;
1754 nsresult rv = setClosedState();
1755 NS_ENSURE_SUCCESS(rv, rv);
1757 return internalClose(nativeConn);
1760 NS_IMETHODIMP
1761 Connection::SpinningSynchronousClose() {
1762 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1763 if (NS_FAILED(rv)) {
1764 return rv;
1766 if (!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
1767 return NS_ERROR_NOT_SAME_THREAD;
1770 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1771 // Our only existing caller will never have called close; assert if misused
1772 // so that no new callers assume this works after an AsyncClose.
1773 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1774 if (!connectionReady()) {
1775 return NS_ERROR_UNEXPECTED;
1778 RefPtr<CloseListener> listener = new CloseListener();
1779 rv = AsyncClose(listener);
1780 NS_ENSURE_SUCCESS(rv, rv);
1781 MOZ_ALWAYS_TRUE(
1782 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,
1783 [&]() { return listener->mClosed; }));
1784 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1786 return rv;
1789 NS_IMETHODIMP
1790 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1791 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1792 // Check if AsyncClose or Close were already invoked.
1793 if (!connectionReady()) {
1794 return NS_ERROR_NOT_INITIALIZED;
1796 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1797 if (NS_FAILED(rv)) {
1798 return rv;
1801 // The two relevant factors at this point are whether we have a database
1802 // connection and whether we have an async execution thread. Here's what the
1803 // states mean and how we handle them:
1805 // - (mDBConn && asyncThread): The expected case where we are either an
1806 // async connection or a sync connection that has been used asynchronously.
1807 // Either way the caller must call us and not Close(). Nothing surprising
1808 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1809 // async thread.
1811 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1812 // opened the connection synchronously and was planning to use it
1813 // asynchronously, but never got around to using it asynchronously before
1814 // needing to shutdown. This has been observed to happen for the cookie
1815 // service in a case where Firefox shuts itself down almost immediately
1816 // after startup (for unknown reasons). In the Firefox shutdown case,
1817 // we may also fail to create a new async execution thread if one does not
1818 // already exist. (nsThreadManager will refuse to create new threads when
1819 // it has already been told to shutdown.) As such, we need to handle a
1820 // failure to create the async execution thread by falling back to
1821 // synchronous Close() and also dispatching the completion callback because
1822 // at least Places likes to spin a nested event loop that depends on the
1823 // callback being invoked.
1825 // Note that we have considered not trying to spin up the async execution
1826 // thread in this case if it does not already exist, but the overhead of
1827 // thread startup (if successful) is significantly less expensive than the
1828 // worst-case potential I/O hit of synchronously closing a database when we
1829 // could close it asynchronously.
1831 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1832 // OpenAsyncDatabase encountered a problem opening the database. If it
1833 // happened in all cases AsyncInitDatabase would just shut down the thread
1834 // directly and we would avoid this case. But it doesn't, so for simplicity
1835 // and consistency AsyncCloseConnection knows how to handle this and we
1836 // act like this was the (mDBConn && asyncThread) case in this method.
1838 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1839 // Close() or AsyncClose() has already been called (at least) once. This is
1840 // undeniably a misuse case by the caller. We could optimize for this
1841 // case by adding an additional check of mAsyncExecutionThread without using
1842 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1843 // shut it down. But this complicates the method for broken caller code
1844 // whereas we're still correct and safe without the special-case.
1845 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1847 // Create our callback event if we were given a callback. This will
1848 // eventually be dispatched in all cases, even if we fall back to Close() and
1849 // the database wasn't open and we return an error. The rationale is that
1850 // no existing consumer checks our return value and several of them like to
1851 // spin nested event loops until the callback fires. Given that, it seems
1852 // preferable for us to dispatch the callback in all cases. (Except the
1853 // wrong thread misuse case we bailed on up above. But that's okay because
1854 // that is statically wrong whereas these edge cases are dynamic.)
1855 nsCOMPtr<nsIRunnable> completeEvent;
1856 if (aCallback) {
1857 completeEvent = newCompletionEvent(aCallback);
1860 if (!asyncThread) {
1861 // We were unable to create an async thread, so we need to fall back to
1862 // using normal Close(). Since there is no async thread, Close() will
1863 // not complain about that. (Close() may, however, complain if the
1864 // connection is closed, but that's okay.)
1865 if (completeEvent) {
1866 // Closing the database is more important than returning an error code
1867 // about a failure to dispatch, especially because all existing native
1868 // callers ignore our return value.
1869 Unused << NS_DispatchToMainThread(completeEvent.forget());
1871 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1872 // Return a success inconditionally here, since Close() is unlikely to fail
1873 // and we want to reassure the consumer that its callback will be invoked.
1874 return NS_OK;
1877 // If we're closing the connection during shutdown, and there is an
1878 // interruptible statement running on the helper thread, issue a
1879 // sqlite3_interrupt() to avoid crashing when that statement takes a long
1880 // time (for example a vacuum).
1881 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed) &&
1882 mInterruptible && mIsStatementOnHelperThreadInterruptible) {
1883 MOZ_ASSERT(!isClosing(), "Must not be closing, see Interrupt()");
1884 DebugOnly<nsresult> rv2 = Interrupt();
1885 MOZ_ASSERT(NS_SUCCEEDED(rv2));
1888 // setClosedState nullifies our connection pointer, so we take a raw pointer
1889 // off it, to pass it through the close procedure.
1890 sqlite3* nativeConn = mDBConn;
1891 rv = setClosedState();
1892 NS_ENSURE_SUCCESS(rv, rv);
1894 // Create and dispatch our close event to the background thread.
1895 nsCOMPtr<nsIRunnable> closeEvent =
1896 new AsyncCloseConnection(this, nativeConn, completeEvent);
1897 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1898 NS_ENSURE_SUCCESS(rv, rv);
1900 return NS_OK;
1903 NS_IMETHODIMP
1904 Connection::AsyncClone(bool aReadOnly,
1905 mozIStorageCompletionCallback* aCallback) {
1906 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1908 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1909 if (!connectionReady()) {
1910 return NS_ERROR_NOT_INITIALIZED;
1912 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1913 if (NS_FAILED(rv)) {
1914 return rv;
1916 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1918 int flags = mFlags;
1919 if (aReadOnly) {
1920 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1921 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1922 // Turn off SQLITE_OPEN_CREATE.
1923 flags = (~SQLITE_OPEN_CREATE & flags);
1926 // The cloned connection will still implement the synchronous API, but throw
1927 // if any synchronous methods are called on the main thread.
1928 RefPtr<Connection> clone =
1929 new Connection(mStorageService, flags, ASYNCHRONOUS, mTelemetryFilename);
1931 RefPtr<AsyncInitializeClone> initEvent =
1932 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1933 // Dispatch to our async thread, since the originating connection must remain
1934 // valid and open for the whole cloning process. This also ensures we are
1935 // properly serialized with a `close` operation, rather than race with it.
1936 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1937 if (!target) {
1938 return NS_ERROR_UNEXPECTED;
1940 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1943 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1944 nsresult rv;
1945 if (!mStorageKey.IsEmpty()) {
1946 rv = aClone->initialize(mStorageKey, mName);
1947 } else if (mFileURL) {
1948 rv = aClone->initialize(mFileURL);
1949 } else {
1950 rv = aClone->initialize(mDatabaseFile);
1952 if (NS_FAILED(rv)) {
1953 return rv;
1956 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1958 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1959 NS_ENSURE_SUCCESS(rv, rv);
1961 // Re-attach on-disk databases that were attached to the original connection.
1963 nsCOMPtr<mozIStorageStatement> stmt;
1964 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1965 NS_ENSURE_SUCCESS(rv, rv);
1966 bool hasResult = false;
1967 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1968 nsAutoCString name;
1969 rv = stmt->GetUTF8String(1, name);
1970 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1971 !name.EqualsLiteral("temp")) {
1972 nsCString path;
1973 rv = stmt->GetUTF8String(2, path);
1974 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1975 nsCOMPtr<mozIStorageStatement> attachStmt;
1976 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1977 getter_AddRefs(attachStmt));
1978 NS_ENSURE_SUCCESS(rv, rv);
1979 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1980 NS_ENSURE_SUCCESS(rv, rv);
1981 rv = attachStmt->Execute();
1982 NS_ENSURE_SUCCESS(rv, rv);
1988 // Copy over pragmas from the original connection.
1989 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1990 // schema ("main" and any other attached databases), and this implmentation
1991 // fails to propagate them. This is being addressed on trunk.
1992 static const char* pragmas[] = {
1993 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1994 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1995 for (auto& pragma : pragmas) {
1996 // Read-only connections just need cache_size and temp_store pragmas.
1997 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1998 ::strcmp(pragma, "temp_store") != 0) {
1999 continue;
2002 nsAutoCString pragmaQuery("PRAGMA ");
2003 pragmaQuery.Append(pragma);
2004 nsCOMPtr<mozIStorageStatement> stmt;
2005 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
2006 NS_ENSURE_SUCCESS(rv, rv);
2007 bool hasResult = false;
2008 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2009 pragmaQuery.AppendLiteral(" = ");
2010 pragmaQuery.AppendInt(stmt->AsInt32(0));
2011 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
2012 NS_ENSURE_SUCCESS(rv, rv);
2016 // Copy over temporary tables, triggers, and views from the original
2017 // connections. Entities in `sqlite_temp_master` are only visible to the
2018 // connection that created them.
2019 if (!aReadOnly) {
2020 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
2021 NS_ENSURE_SUCCESS(rv, rv);
2023 nsCOMPtr<mozIStorageStatement> stmt;
2024 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
2025 "WHERE type IN ('table', 'view', "
2026 "'index', 'trigger')"),
2027 getter_AddRefs(stmt));
2028 // Propagate errors, because failing to copy triggers might cause schema
2029 // coherency issues when writing to the database from the cloned connection.
2030 NS_ENSURE_SUCCESS(rv, rv);
2031 bool hasResult = false;
2032 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2033 nsAutoCString query;
2034 rv = stmt->GetUTF8String(0, query);
2035 NS_ENSURE_SUCCESS(rv, rv);
2037 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
2038 // keyword. We need to add it back, or we'll recreate temporary entities
2039 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
2040 // statements, but those don't need `TEMP` keywords.
2041 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
2042 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
2043 StringBeginsWith(query, "CREATE VIEW "_ns)) {
2044 query.Replace(0, 6, "CREATE TEMP");
2047 rv = aClone->ExecuteSimpleSQL(query);
2048 NS_ENSURE_SUCCESS(rv, rv);
2051 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
2052 NS_ENSURE_SUCCESS(rv, rv);
2055 // Copy any functions that have been added to this connection.
2056 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2057 for (const auto& entry : mFunctions) {
2058 const nsACString& key = entry.GetKey();
2059 Connection::FunctionInfo data = entry.GetData();
2061 rv = aClone->CreateFunction(key, data.numArgs, data.function);
2062 if (NS_FAILED(rv)) {
2063 NS_WARNING("Failed to copy function to cloned connection");
2067 // Load SQLite extensions that were on this connection.
2068 // Copy into an array rather than holding the mutex while we load extensions.
2069 nsTArray<nsCString> loadedExtensions;
2071 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2072 AppendToArray(loadedExtensions, mLoadedExtensions);
2074 for (const auto& extension : loadedExtensions) {
2075 (void)aClone->LoadExtension(extension, nullptr);
2078 guard.release();
2079 return NS_OK;
2082 NS_IMETHODIMP
2083 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
2084 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
2086 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
2088 if (!connectionReady()) {
2089 return NS_ERROR_NOT_INITIALIZED;
2091 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2092 if (NS_FAILED(rv)) {
2093 return rv;
2096 int flags = mFlags;
2097 if (aReadOnly) {
2098 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
2099 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
2100 // Turn off SQLITE_OPEN_CREATE.
2101 flags = (~SQLITE_OPEN_CREATE & flags);
2104 RefPtr<Connection> clone =
2105 new Connection(mStorageService, flags, mSupportedOperations,
2106 mTelemetryFilename, mInterruptible);
2108 rv = initializeClone(clone, aReadOnly);
2109 if (NS_FAILED(rv)) {
2110 return rv;
2113 NS_IF_ADDREF(*_connection = clone);
2114 return NS_OK;
2117 NS_IMETHODIMP
2118 Connection::Interrupt() {
2119 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
2120 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
2121 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
2122 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
2123 IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
2125 if (!connectionReady()) {
2126 return NS_ERROR_NOT_INITIALIZED;
2129 if (isClosing()) { // Closing already in asynchronous case
2130 return NS_OK;
2134 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
2135 // it is not safe to call sqlite3_interrupt() when
2136 // database connection is closed or might close before
2137 // sqlite3_interrupt() returns.
2138 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2139 if (!isClosed(lockedScope)) {
2140 MOZ_ASSERT(mDBConn);
2141 ::sqlite3_interrupt(mDBConn);
2145 return NS_OK;
2148 NS_IMETHODIMP
2149 Connection::AsyncVacuum(mozIStorageCompletionCallback* aCallback,
2150 bool aUseIncremental, int32_t aSetPageSize) {
2151 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
2152 // Abort if we're shutting down.
2153 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
2154 return NS_ERROR_ABORT;
2156 // Check if AsyncClose or Close were already invoked.
2157 if (!connectionReady()) {
2158 return NS_ERROR_NOT_INITIALIZED;
2160 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2161 if (NS_FAILED(rv)) {
2162 return rv;
2164 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
2165 if (!asyncThread) {
2166 return NS_ERROR_NOT_INITIALIZED;
2169 // Create and dispatch our vacuum event to the background thread.
2170 nsCOMPtr<nsIRunnable> vacuumEvent =
2171 new AsyncVacuumEvent(this, aCallback, aUseIncremental, aSetPageSize);
2172 rv = asyncThread->Dispatch(vacuumEvent, NS_DISPATCH_NORMAL);
2173 NS_ENSURE_SUCCESS(rv, rv);
2175 return NS_OK;
2178 NS_IMETHODIMP
2179 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
2180 *_defaultPageSize = Service::kDefaultPageSize;
2181 return NS_OK;
2184 NS_IMETHODIMP
2185 Connection::GetConnectionReady(bool* _ready) {
2186 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
2187 *_ready = connectionReady();
2188 return NS_OK;
2191 NS_IMETHODIMP
2192 Connection::GetDatabaseFile(nsIFile** _dbFile) {
2193 if (!connectionReady()) {
2194 return NS_ERROR_NOT_INITIALIZED;
2196 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2197 if (NS_FAILED(rv)) {
2198 return rv;
2201 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
2203 return NS_OK;
2206 NS_IMETHODIMP
2207 Connection::GetLastInsertRowID(int64_t* _id) {
2208 if (!connectionReady()) {
2209 return NS_ERROR_NOT_INITIALIZED;
2211 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2212 if (NS_FAILED(rv)) {
2213 return rv;
2216 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
2217 *_id = id;
2219 return NS_OK;
2222 NS_IMETHODIMP
2223 Connection::GetAffectedRows(int32_t* _rows) {
2224 if (!connectionReady()) {
2225 return NS_ERROR_NOT_INITIALIZED;
2227 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2228 if (NS_FAILED(rv)) {
2229 return rv;
2232 *_rows = ::sqlite3_changes(mDBConn);
2234 return NS_OK;
2237 NS_IMETHODIMP
2238 Connection::GetLastError(int32_t* _error) {
2239 if (!connectionReady()) {
2240 return NS_ERROR_NOT_INITIALIZED;
2242 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2243 if (NS_FAILED(rv)) {
2244 return rv;
2247 *_error = ::sqlite3_errcode(mDBConn);
2249 return NS_OK;
2252 NS_IMETHODIMP
2253 Connection::GetLastErrorString(nsACString& _errorString) {
2254 if (!connectionReady()) {
2255 return NS_ERROR_NOT_INITIALIZED;
2257 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2258 if (NS_FAILED(rv)) {
2259 return rv;
2262 const char* serr = ::sqlite3_errmsg(mDBConn);
2263 _errorString.Assign(serr);
2265 return NS_OK;
2268 NS_IMETHODIMP
2269 Connection::GetSchemaVersion(int32_t* _version) {
2270 if (!connectionReady()) {
2271 return NS_ERROR_NOT_INITIALIZED;
2273 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2274 if (NS_FAILED(rv)) {
2275 return rv;
2278 nsCOMPtr<mozIStorageStatement> stmt;
2279 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
2280 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
2282 *_version = 0;
2283 bool hasResult;
2284 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2285 *_version = stmt->AsInt32(0);
2288 return NS_OK;
2291 NS_IMETHODIMP
2292 Connection::SetSchemaVersion(int32_t aVersion) {
2293 if (!connectionReady()) {
2294 return NS_ERROR_NOT_INITIALIZED;
2296 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2297 if (NS_FAILED(rv)) {
2298 return rv;
2301 nsAutoCString stmt("PRAGMA user_version = "_ns);
2302 stmt.AppendInt(aVersion);
2304 return ExecuteSimpleSQL(stmt);
2307 NS_IMETHODIMP
2308 Connection::CreateStatement(const nsACString& aSQLStatement,
2309 mozIStorageStatement** _stmt) {
2310 NS_ENSURE_ARG_POINTER(_stmt);
2311 if (!connectionReady()) {
2312 return NS_ERROR_NOT_INITIALIZED;
2314 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2315 if (NS_FAILED(rv)) {
2316 return rv;
2319 RefPtr<Statement> statement(new Statement());
2320 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2322 rv = statement->initialize(this, mDBConn, aSQLStatement);
2323 NS_ENSURE_SUCCESS(rv, rv);
2325 Statement* rawPtr;
2326 statement.forget(&rawPtr);
2327 *_stmt = rawPtr;
2328 return NS_OK;
2331 NS_IMETHODIMP
2332 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
2333 mozIStorageAsyncStatement** _stmt) {
2334 NS_ENSURE_ARG_POINTER(_stmt);
2335 if (!connectionReady()) {
2336 return NS_ERROR_NOT_INITIALIZED;
2338 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2339 if (NS_FAILED(rv)) {
2340 return rv;
2343 RefPtr<AsyncStatement> statement(new AsyncStatement());
2344 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2346 rv = statement->initialize(this, mDBConn, aSQLStatement);
2347 NS_ENSURE_SUCCESS(rv, rv);
2349 AsyncStatement* rawPtr;
2350 statement.forget(&rawPtr);
2351 *_stmt = rawPtr;
2352 return NS_OK;
2355 NS_IMETHODIMP
2356 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
2357 CHECK_MAINTHREAD_ABUSE();
2358 if (!connectionReady()) {
2359 return NS_ERROR_NOT_INITIALIZED;
2361 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2362 if (NS_FAILED(rv)) {
2363 return rv;
2366 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
2367 return convertResultCode(srv);
2370 NS_IMETHODIMP
2371 Connection::ExecuteAsync(
2372 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
2373 mozIStorageStatementCallback* aCallback,
2374 mozIStoragePendingStatement** _handle) {
2375 nsTArray<StatementData> stmts(aStatements.Length());
2376 for (uint32_t i = 0; i < aStatements.Length(); i++) {
2377 nsCOMPtr<StorageBaseStatementInternal> stmt =
2378 do_QueryInterface(aStatements[i]);
2379 NS_ENSURE_STATE(stmt);
2381 // Obtain our StatementData.
2382 StatementData data;
2383 nsresult rv = stmt->getAsynchronousStatementData(data);
2384 NS_ENSURE_SUCCESS(rv, rv);
2386 NS_ASSERTION(stmt->getOwner() == this,
2387 "Statement must be from this database connection!");
2389 // Now append it to our array.
2390 stmts.AppendElement(data);
2393 // Dispatch to the background
2394 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
2395 aCallback, _handle);
2398 NS_IMETHODIMP
2399 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
2400 mozIStorageStatementCallback* aCallback,
2401 mozIStoragePendingStatement** _handle) {
2402 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
2404 nsCOMPtr<mozIStorageAsyncStatement> stmt;
2405 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
2406 if (NS_FAILED(rv)) {
2407 return rv;
2410 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
2411 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
2412 if (NS_FAILED(rv)) {
2413 return rv;
2416 pendingStatement.forget(_handle);
2417 return rv;
2420 NS_IMETHODIMP
2421 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
2422 return databaseElementExists(TABLE, aTableName, _exists);
2425 NS_IMETHODIMP
2426 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2427 return databaseElementExists(INDEX, aIndexName, _exists);
2430 NS_IMETHODIMP
2431 Connection::GetTransactionInProgress(bool* _inProgress) {
2432 if (!connectionReady()) {
2433 return NS_ERROR_NOT_INITIALIZED;
2435 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2436 if (NS_FAILED(rv)) {
2437 return rv;
2440 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2441 *_inProgress = transactionInProgress(lockedScope);
2442 return NS_OK;
2445 NS_IMETHODIMP
2446 Connection::GetDefaultTransactionType(int32_t* _type) {
2447 *_type = mDefaultTransactionType;
2448 return NS_OK;
2451 NS_IMETHODIMP
2452 Connection::SetDefaultTransactionType(int32_t aType) {
2453 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2454 mDefaultTransactionType = aType;
2455 return NS_OK;
2458 NS_IMETHODIMP
2459 Connection::GetVariableLimit(int32_t* _limit) {
2460 if (!connectionReady()) {
2461 return NS_ERROR_NOT_INITIALIZED;
2463 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2464 if (limit < 0) {
2465 return NS_ERROR_UNEXPECTED;
2467 *_limit = limit;
2468 return NS_OK;
2471 NS_IMETHODIMP
2472 Connection::SetVariableLimit(int32_t limit) {
2473 if (!connectionReady()) {
2474 return NS_ERROR_NOT_INITIALIZED;
2476 int oldLimit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, limit);
2477 if (oldLimit < 0) {
2478 return NS_ERROR_UNEXPECTED;
2480 return NS_OK;
2483 NS_IMETHODIMP
2484 Connection::BeginTransaction() {
2485 if (!connectionReady()) {
2486 return NS_ERROR_NOT_INITIALIZED;
2488 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2489 if (NS_FAILED(rv)) {
2490 return rv;
2493 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2494 return beginTransactionInternal(lockedScope, mDBConn,
2495 mDefaultTransactionType);
2498 nsresult Connection::beginTransactionInternal(
2499 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2500 int32_t aTransactionType) {
2501 if (transactionInProgress(aProofOfLock)) {
2502 return NS_ERROR_FAILURE;
2504 nsresult rv;
2505 switch (aTransactionType) {
2506 case TRANSACTION_DEFERRED:
2507 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2508 break;
2509 case TRANSACTION_IMMEDIATE:
2510 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2511 break;
2512 case TRANSACTION_EXCLUSIVE:
2513 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2514 break;
2515 default:
2516 return NS_ERROR_ILLEGAL_VALUE;
2518 return rv;
2521 NS_IMETHODIMP
2522 Connection::CommitTransaction() {
2523 if (!connectionReady()) {
2524 return NS_ERROR_NOT_INITIALIZED;
2526 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2527 if (NS_FAILED(rv)) {
2528 return rv;
2531 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2532 return commitTransactionInternal(lockedScope, mDBConn);
2535 nsresult Connection::commitTransactionInternal(
2536 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2537 if (!transactionInProgress(aProofOfLock)) {
2538 return NS_ERROR_UNEXPECTED;
2540 nsresult rv =
2541 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2542 return rv;
2545 NS_IMETHODIMP
2546 Connection::RollbackTransaction() {
2547 if (!connectionReady()) {
2548 return NS_ERROR_NOT_INITIALIZED;
2550 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2551 if (NS_FAILED(rv)) {
2552 return rv;
2555 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2556 return rollbackTransactionInternal(lockedScope, mDBConn);
2559 nsresult Connection::rollbackTransactionInternal(
2560 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2561 if (!transactionInProgress(aProofOfLock)) {
2562 return NS_ERROR_UNEXPECTED;
2565 nsresult rv =
2566 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2567 return rv;
2570 NS_IMETHODIMP
2571 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2572 if (!connectionReady()) {
2573 return NS_ERROR_NOT_INITIALIZED;
2575 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2576 if (NS_FAILED(rv)) {
2577 return rv;
2580 SmprintfPointer buf =
2581 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2582 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2584 int srv = executeSql(mDBConn, buf.get());
2586 return convertResultCode(srv);
2589 NS_IMETHODIMP
2590 Connection::CreateFunction(const nsACString& aFunctionName,
2591 int32_t aNumArguments,
2592 mozIStorageFunction* aFunction) {
2593 if (!connectionReady()) {
2594 return NS_ERROR_NOT_INITIALIZED;
2596 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2597 if (NS_FAILED(rv)) {
2598 return rv;
2601 // Check to see if this function is already defined. We only check the name
2602 // because a function can be defined with the same body but different names.
2603 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2604 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2606 int srv = ::sqlite3_create_function(
2607 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2608 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2609 if (srv != SQLITE_OK) return convertResultCode(srv);
2611 FunctionInfo info = {aFunction, aNumArguments};
2612 mFunctions.InsertOrUpdate(aFunctionName, info);
2614 return NS_OK;
2617 NS_IMETHODIMP
2618 Connection::RemoveFunction(const nsACString& aFunctionName) {
2619 if (!connectionReady()) {
2620 return NS_ERROR_NOT_INITIALIZED;
2622 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2623 if (NS_FAILED(rv)) {
2624 return rv;
2627 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2628 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2630 int srv = ::sqlite3_create_function(
2631 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2632 nullptr, nullptr, nullptr, nullptr);
2633 if (srv != SQLITE_OK) return convertResultCode(srv);
2635 mFunctions.Remove(aFunctionName);
2637 return NS_OK;
2640 NS_IMETHODIMP
2641 Connection::SetProgressHandler(int32_t aGranularity,
2642 mozIStorageProgressHandler* aHandler,
2643 mozIStorageProgressHandler** _oldHandler) {
2644 if (!connectionReady()) {
2645 return NS_ERROR_NOT_INITIALIZED;
2647 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2648 if (NS_FAILED(rv)) {
2649 return rv;
2652 // Return previous one
2653 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2654 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2656 if (!aHandler || aGranularity <= 0) {
2657 aHandler = nullptr;
2658 aGranularity = 0;
2660 mProgressHandler = aHandler;
2661 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2663 return NS_OK;
2666 NS_IMETHODIMP
2667 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2668 if (!connectionReady()) {
2669 return NS_ERROR_NOT_INITIALIZED;
2671 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2672 if (NS_FAILED(rv)) {
2673 return rv;
2676 // Return previous one
2677 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2678 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2680 mProgressHandler = nullptr;
2681 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2683 return NS_OK;
2686 NS_IMETHODIMP
2687 Connection::SetGrowthIncrement(int32_t aChunkSize,
2688 const nsACString& aDatabaseName) {
2689 if (!connectionReady()) {
2690 return NS_ERROR_NOT_INITIALIZED;
2692 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2693 if (NS_FAILED(rv)) {
2694 return rv;
2697 // Bug 597215: Disk space is extremely limited on Android
2698 // so don't preallocate space. This is also not effective
2699 // on log structured file systems used by Android devices
2700 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2701 // Don't preallocate if less than 500MiB is available.
2702 int64_t bytesAvailable;
2703 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2704 NS_ENSURE_SUCCESS(rv, rv);
2705 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2706 return NS_ERROR_FILE_TOO_BIG;
2709 int srv = ::sqlite3_file_control(
2710 mDBConn,
2711 aDatabaseName.Length() ? nsPromiseFlatCString(aDatabaseName).get()
2712 : nullptr,
2713 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2714 if (srv == SQLITE_OK) {
2715 mGrowthChunkSize = aChunkSize;
2717 #endif
2718 return NS_OK;
2721 int32_t Connection::RemovablePagesInFreeList(const nsACString& aSchemaName) {
2722 int32_t freeListPagesCount = 0;
2723 if (!isConnectionReadyOnThisThread()) {
2724 MOZ_ASSERT(false, "Database connection is not ready");
2725 return freeListPagesCount;
2728 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2729 query.Append(aSchemaName);
2730 query.AppendLiteral(".freelist_count");
2731 nsCOMPtr<mozIStorageStatement> stmt;
2732 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2733 MOZ_ASSERT(NS_SUCCEEDED(rv));
2734 bool hasResult = false;
2735 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2736 freeListPagesCount = stmt->AsInt32(0);
2739 // If there's no chunk size set, any page is good to be removed.
2740 if (mGrowthChunkSize == 0 || freeListPagesCount == 0) {
2741 return freeListPagesCount;
2743 int32_t pageSize;
2745 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2746 query.Append(aSchemaName);
2747 query.AppendLiteral(".page_size");
2748 nsCOMPtr<mozIStorageStatement> stmt;
2749 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2750 MOZ_ASSERT(NS_SUCCEEDED(rv));
2751 bool hasResult = false;
2752 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2753 pageSize = stmt->AsInt32(0);
2754 } else {
2755 MOZ_ASSERT(false, "Couldn't get page_size");
2756 return 0;
2759 return std::max(0, freeListPagesCount - (mGrowthChunkSize / pageSize));
2762 NS_IMETHODIMP
2763 Connection::LoadExtension(const nsACString& aExtensionName,
2764 mozIStorageCompletionCallback* aCallback) {
2765 AUTO_PROFILER_LABEL("Connection::LoadExtension", OTHER);
2767 // This is a static list of extensions we can load.
2768 // Please use lowercase ASCII names and keep this list alphabetically ordered.
2769 static constexpr nsLiteralCString sSupportedExtensions[] = {
2770 // clang-format off
2771 "fts5"_ns,
2772 // clang-format on
2774 if (std::find(std::begin(sSupportedExtensions),
2775 std::end(sSupportedExtensions),
2776 aExtensionName) == std::end(sSupportedExtensions)) {
2777 return NS_ERROR_INVALID_ARG;
2780 if (!connectionReady()) {
2781 return NS_ERROR_NOT_INITIALIZED;
2784 int srv = ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION,
2785 1, nullptr);
2786 if (srv != SQLITE_OK) {
2787 return NS_ERROR_UNEXPECTED;
2790 // Track the loaded extension for later connection cloning operations.
2792 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
2793 if (!mLoadedExtensions.EnsureInserted(aExtensionName)) {
2794 // Already loaded, bail out but issue a warning.
2795 NS_WARNING(nsPrintfCString(
2796 "Tried to register '%s' SQLite extension multiple times!",
2797 PromiseFlatCString(aExtensionName).get())
2798 .get());
2799 return NS_OK;
2803 nsAutoCString entryPoint("sqlite3_");
2804 entryPoint.Append(aExtensionName);
2805 entryPoint.AppendLiteral("_init");
2807 RefPtr<Runnable> loadTask = NS_NewRunnableFunction(
2808 "mozStorageConnection::LoadExtension",
2809 [this, self = RefPtr(this), entryPoint,
2810 callback = RefPtr(aCallback)]() mutable {
2811 MOZ_ASSERT(
2812 !NS_IsMainThread() ||
2813 (operationSupported(Connection::SYNCHRONOUS) &&
2814 eventTargetOpenedOn == GetMainThreadSerialEventTarget()),
2815 "Should happen on main-thread only for synchronous connections "
2816 "opened on the main thread");
2817 #ifdef MOZ_FOLD_LIBS
2818 int srv = ::sqlite3_load_extension(mDBConn,
2819 MOZ_DLL_PREFIX "nss3" MOZ_DLL_SUFFIX,
2820 entryPoint.get(), nullptr);
2821 #else
2822 int srv = ::sqlite3_load_extension(
2823 mDBConn, MOZ_DLL_PREFIX "mozsqlite3" MOZ_DLL_SUFFIX,
2824 entryPoint.get(), nullptr);
2825 #endif
2826 if (!callback) {
2827 return;
2829 RefPtr<Runnable> callbackTask = NS_NewRunnableFunction(
2830 "mozStorageConnection::LoadExtension_callback",
2831 [callback = std::move(callback), srv]() {
2832 (void)callback->Complete(convertResultCode(srv), nullptr);
2834 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
2835 MOZ_ALWAYS_SUCCEEDS(callbackTask->Run());
2836 } else {
2837 // Redispatch the callback to the calling thread.
2838 MOZ_ALWAYS_SUCCEEDS(eventTargetOpenedOn->Dispatch(
2839 callbackTask.forget(), NS_DISPATCH_NORMAL));
2843 if (NS_IsMainThread() && !operationSupported(Connection::SYNCHRONOUS)) {
2844 // This is a main-thread call to an async-only connection, thus we should
2845 // load the library in the helper thread.
2846 nsIEventTarget* helperThread = getAsyncExecutionTarget();
2847 if (!helperThread) {
2848 return NS_ERROR_NOT_INITIALIZED;
2850 MOZ_ALWAYS_SUCCEEDS(
2851 helperThread->Dispatch(loadTask.forget(), NS_DISPATCH_NORMAL));
2852 } else {
2853 // In any other case we just load the extension on the current thread.
2854 MOZ_ALWAYS_SUCCEEDS(loadTask->Run());
2856 return NS_OK;
2859 NS_IMETHODIMP
2860 Connection::EnableModule(const nsACString& aModuleName) {
2861 if (!connectionReady()) {
2862 return NS_ERROR_NOT_INITIALIZED;
2864 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2865 if (NS_FAILED(rv)) {
2866 return rv;
2869 for (auto& gModule : gModules) {
2870 struct Module* m = &gModule;
2871 if (aModuleName.Equals(m->name)) {
2872 int srv = m->registerFunc(mDBConn, m->name);
2873 if (srv != SQLITE_OK) return convertResultCode(srv);
2875 return NS_OK;
2879 return NS_ERROR_FAILURE;
2882 NS_IMETHODIMP
2883 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2884 QuotaObject** aJournalQuotaObject) {
2885 MOZ_ASSERT(aDatabaseQuotaObject);
2886 MOZ_ASSERT(aJournalQuotaObject);
2888 if (!connectionReady()) {
2889 return NS_ERROR_NOT_INITIALIZED;
2891 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2892 if (NS_FAILED(rv)) {
2893 return rv;
2896 sqlite3_file* file;
2897 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2898 &file);
2899 if (srv != SQLITE_OK) {
2900 return convertResultCode(srv);
2903 sqlite3_vfs* vfs;
2904 srv =
2905 ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_VFS_POINTER, &vfs);
2906 if (srv != SQLITE_OK) {
2907 return convertResultCode(srv);
2910 bool obfusactingVFS = false;
2913 const nsDependentCString vfsName{vfs->zName};
2915 if (vfsName == obfsvfs::GetVFSName()) {
2916 obfusactingVFS = true;
2917 } else if (vfsName != quotavfs::GetVFSName()) {
2918 NS_WARNING("Got unexpected vfs");
2919 return NS_ERROR_FAILURE;
2923 RefPtr<QuotaObject> databaseQuotaObject =
2924 GetQuotaObject(file, obfusactingVFS);
2925 if (NS_WARN_IF(!databaseQuotaObject)) {
2926 return NS_ERROR_FAILURE;
2929 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2930 &file);
2931 if (srv != SQLITE_OK) {
2932 return convertResultCode(srv);
2935 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObject(file, obfusactingVFS);
2936 if (NS_WARN_IF(!journalQuotaObject)) {
2937 return NS_ERROR_FAILURE;
2940 databaseQuotaObject.forget(aDatabaseQuotaObject);
2941 journalQuotaObject.forget(aJournalQuotaObject);
2942 return NS_OK;
2945 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2947 uint32_t Connection::GetTransactionNestingLevel(
2948 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2949 return mTransactionNestingLevel;
2952 uint32_t Connection::IncreaseTransactionNestingLevel(
2953 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2954 return ++mTransactionNestingLevel;
2957 uint32_t Connection::DecreaseTransactionNestingLevel(
2958 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2959 return --mTransactionNestingLevel;
2962 NS_IMETHODIMP
2963 Connection::BackupToFileAsync(nsIFile* aDestinationFile,
2964 mozIStorageCompletionCallback* aCallback) {
2965 NS_ENSURE_ARG(aDestinationFile);
2966 NS_ENSURE_ARG(aCallback);
2967 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
2969 // Abort if we're shutting down.
2970 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
2971 return NS_ERROR_ABORT;
2973 // Check if AsyncClose or Close were already invoked.
2974 if (!connectionReady()) {
2975 return NS_ERROR_NOT_INITIALIZED;
2977 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2978 if (NS_FAILED(rv)) {
2979 return rv;
2981 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
2982 if (!asyncThread) {
2983 return NS_ERROR_NOT_INITIALIZED;
2986 // Create and dispatch our backup event to the execution thread.
2987 nsCOMPtr<nsIRunnable> backupEvent =
2988 new AsyncBackupDatabaseFile(this, mDBConn, aDestinationFile, aCallback);
2989 rv = asyncThread->Dispatch(backupEvent, NS_DISPATCH_NORMAL);
2990 return rv;
2993 } // namespace mozilla::storage