Bug 1771374 - Fix lint warnings. r=gfx-reviewers,aosmond
[gecko.git] / storage / mozStorageConnection.cpp
blob0bfd614a6442ea548436c3f121b4c78d973023f7
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 "nsThreadUtils.h"
8 #include "nsIFile.h"
9 #include "nsIFileURL.h"
10 #include "nsIXPConnect.h"
11 #include "mozilla/Telemetry.h"
12 #include "mozilla/Mutex.h"
13 #include "mozilla/CondVar.h"
14 #include "mozilla/Attributes.h"
15 #include "mozilla/ErrorNames.h"
16 #include "mozilla/Unused.h"
17 #include "mozilla/dom/quota/QuotaObject.h"
18 #include "mozilla/ScopeExit.h"
19 #include "mozilla/SpinEventLoopUntil.h"
20 #include "mozilla/StaticPrefs_storage.h"
22 #include "mozIStorageCompletionCallback.h"
23 #include "mozIStorageFunction.h"
25 #include "mozStorageAsyncStatementExecution.h"
26 #include "mozStorageSQLFunctions.h"
27 #include "mozStorageConnection.h"
28 #include "mozStorageService.h"
29 #include "mozStorageStatement.h"
30 #include "mozStorageAsyncStatement.h"
31 #include "mozStorageArgValueArray.h"
32 #include "mozStoragePrivateHelpers.h"
33 #include "mozStorageStatementData.h"
34 #include "StorageBaseStatementInternal.h"
35 #include "SQLCollations.h"
36 #include "FileSystemModule.h"
37 #include "mozStorageHelper.h"
39 #include "mozilla/Logging.h"
40 #include "mozilla/Printf.h"
41 #include "mozilla/ProfilerLabels.h"
42 #include "nsProxyRelease.h"
43 #include "nsURLHelper.h"
45 #define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
47 // Maximum size of the pages cache per connection.
48 #define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
50 mozilla::LazyLogModule gStorageLog("mozStorage");
52 // Checks that the protected code is running on the main-thread only if the
53 // connection was also opened on it.
54 #ifdef DEBUG
55 # define CHECK_MAINTHREAD_ABUSE() \
56 do { \
57 nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); \
58 NS_WARNING_ASSERTION( \
59 threadOpenedOn == mainThread || !NS_IsMainThread(), \
60 "Using Storage synchronous API on main-thread, but " \
61 "the connection was " \
62 "opened on another thread."); \
63 } while (0)
64 #else
65 # define CHECK_MAINTHREAD_ABUSE() \
66 do { /* Nothing */ \
67 } while (0)
68 #endif
70 namespace mozilla::storage {
72 using mozilla::dom::quota::QuotaObject;
73 using mozilla::Telemetry::AccumulateCategoricalKeyed;
74 using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN;
75 using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY;
77 const char* GetTelemetryVFSName(bool);
78 const char* GetObfuscatingVFSName();
80 namespace {
82 int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
83 if (NS_SUCCEEDED(aXPCOMResultCode)) {
84 return SQLITE_OK;
87 switch (aXPCOMResultCode) {
88 case NS_ERROR_FILE_CORRUPTED:
89 return SQLITE_CORRUPT;
90 case NS_ERROR_FILE_ACCESS_DENIED:
91 return SQLITE_CANTOPEN;
92 case NS_ERROR_STORAGE_BUSY:
93 return SQLITE_BUSY;
94 case NS_ERROR_FILE_IS_LOCKED:
95 return SQLITE_LOCKED;
96 case NS_ERROR_FILE_READ_ONLY:
97 return SQLITE_READONLY;
98 case NS_ERROR_STORAGE_IOERR:
99 return SQLITE_IOERR;
100 case NS_ERROR_FILE_NO_DEVICE_SPACE:
101 return SQLITE_FULL;
102 case NS_ERROR_OUT_OF_MEMORY:
103 return SQLITE_NOMEM;
104 case NS_ERROR_UNEXPECTED:
105 return SQLITE_MISUSE;
106 case NS_ERROR_ABORT:
107 return SQLITE_ABORT;
108 case NS_ERROR_STORAGE_CONSTRAINT:
109 return SQLITE_CONSTRAINT;
110 default:
111 return SQLITE_ERROR;
114 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!");
117 ////////////////////////////////////////////////////////////////////////////////
118 //// Variant Specialization Functions (variantToSQLiteT)
120 int sqlite3_T_int(sqlite3_context* aCtx, int aValue) {
121 ::sqlite3_result_int(aCtx, aValue);
122 return SQLITE_OK;
125 int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
126 ::sqlite3_result_int64(aCtx, aValue);
127 return SQLITE_OK;
130 int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
131 ::sqlite3_result_double(aCtx, aValue);
132 return SQLITE_OK;
135 int sqlite3_T_text(sqlite3_context* aCtx, const nsCString& aValue) {
136 ::sqlite3_result_text(aCtx, aValue.get(), aValue.Length(), SQLITE_TRANSIENT);
137 return SQLITE_OK;
140 int sqlite3_T_text16(sqlite3_context* aCtx, const nsString& aValue) {
141 ::sqlite3_result_text16(
142 aCtx, aValue.get(),
143 aValue.Length() * sizeof(char16_t), // Number of bytes.
144 SQLITE_TRANSIENT);
145 return SQLITE_OK;
148 int sqlite3_T_null(sqlite3_context* aCtx) {
149 ::sqlite3_result_null(aCtx);
150 return SQLITE_OK;
153 int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
154 ::sqlite3_result_blob(aCtx, aData, aSize, free);
155 return SQLITE_OK;
158 #include "variantToSQLiteT_impl.h"
160 ////////////////////////////////////////////////////////////////////////////////
161 //// Modules
163 struct Module {
164 const char* name;
165 int (*registerFunc)(sqlite3*, const char*);
168 Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
170 ////////////////////////////////////////////////////////////////////////////////
171 //// Local Functions
173 int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
174 switch (aReason) {
175 case SQLITE_TRACE_STMT: {
176 // aP is a pointer to the prepared statement.
177 sqlite3_stmt* stmt = static_cast<sqlite3_stmt*>(aP);
178 // aX is a pointer to a string containing the unexpanded SQL or a comment,
179 // starting with "--"" in case of a trigger.
180 char* expanded = static_cast<char*>(aX);
181 // Simulate what sqlite_trace was doing.
182 if (!::strncmp(expanded, "--", 2)) {
183 MOZ_LOG(gStorageLog, LogLevel::Debug,
184 ("TRACE_STMT on %p: '%s'", aClosure, expanded));
185 } else {
186 char* sql = ::sqlite3_expanded_sql(stmt);
187 MOZ_LOG(gStorageLog, LogLevel::Debug,
188 ("TRACE_STMT on %p: '%s'", aClosure, sql));
189 ::sqlite3_free(sql);
191 break;
193 case SQLITE_TRACE_PROFILE: {
194 // aX is pointer to a 64bit integer containing nanoseconds it took to
195 // execute the last command.
196 sqlite_int64 time = *(static_cast<sqlite_int64*>(aX)) / 1000000;
197 if (time > 0) {
198 MOZ_LOG(gStorageLog, LogLevel::Debug,
199 ("TRACE_TIME on %p: %lldms", aClosure, time));
201 break;
204 return 0;
207 void basicFunctionHelper(sqlite3_context* aCtx, int aArgc,
208 sqlite3_value** aArgv) {
209 void* userData = ::sqlite3_user_data(aCtx);
211 mozIStorageFunction* func = static_cast<mozIStorageFunction*>(userData);
213 RefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
214 if (!arguments) return;
216 nsCOMPtr<nsIVariant> result;
217 nsresult rv = func->OnFunctionCall(arguments, getter_AddRefs(result));
218 if (NS_FAILED(rv)) {
219 nsAutoCString errorMessage;
220 GetErrorName(rv, errorMessage);
221 errorMessage.InsertLiteral("User function returned ", 0);
222 errorMessage.Append('!');
224 NS_WARNING(errorMessage.get());
226 ::sqlite3_result_error(aCtx, errorMessage.get(), -1);
227 ::sqlite3_result_error_code(aCtx, nsresultToSQLiteResult(rv));
228 return;
230 int retcode = variantToSQLiteT(aCtx, result);
231 if (retcode != SQLITE_OK) {
232 NS_WARNING("User function returned invalid data type!");
233 ::sqlite3_result_error(aCtx, "User function returned invalid data type",
234 -1);
239 * This code is heavily based on the sample at:
240 * http://www.sqlite.org/unlock_notify.html
242 class UnlockNotification {
243 public:
244 UnlockNotification()
245 : mMutex("UnlockNotification mMutex"),
246 mCondVar(mMutex, "UnlockNotification condVar"),
247 mSignaled(false) {}
249 void Wait() {
250 MutexAutoLock lock(mMutex);
251 while (!mSignaled) {
252 (void)mCondVar.Wait();
256 void Signal() {
257 MutexAutoLock lock(mMutex);
258 mSignaled = true;
259 (void)mCondVar.Notify();
262 private:
263 Mutex mMutex MOZ_UNANNOTATED;
264 CondVar mCondVar;
265 bool mSignaled;
268 void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
269 for (int i = 0; i < aArgsSize; i++) {
270 UnlockNotification* notification =
271 static_cast<UnlockNotification*>(aArgs[i]);
272 notification->Signal();
276 int WaitForUnlockNotify(sqlite3* aDatabase) {
277 UnlockNotification notification;
278 int srv =
279 ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, &notification);
280 MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
281 if (srv == SQLITE_OK) {
282 notification.Wait();
285 return srv;
288 ////////////////////////////////////////////////////////////////////////////////
289 //// Local Classes
291 class AsyncCloseConnection final : public Runnable {
292 public:
293 AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
294 nsIRunnable* aCallbackEvent)
295 : Runnable("storage::AsyncCloseConnection"),
296 mConnection(aConnection),
297 mNativeConnection(aNativeConnection),
298 mCallbackEvent(aCallbackEvent) {}
300 NS_IMETHOD Run() override {
301 // This code is executed on the background thread
302 MOZ_ASSERT(NS_GetCurrentThread() != mConnection->threadOpenedOn);
304 nsCOMPtr<nsIRunnable> event =
305 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
306 mConnection, &Connection::shutdownAsyncThread);
307 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event));
309 // Internal close.
310 (void)mConnection->internalClose(mNativeConnection);
312 // Callback
313 if (mCallbackEvent) {
314 nsCOMPtr<nsIThread> thread;
315 (void)NS_GetMainThread(getter_AddRefs(thread));
316 (void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
319 return NS_OK;
322 ~AsyncCloseConnection() override {
323 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
324 mConnection.forget());
325 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
326 mCallbackEvent.forget());
329 private:
330 RefPtr<Connection> mConnection;
331 sqlite3* mNativeConnection;
332 nsCOMPtr<nsIRunnable> mCallbackEvent;
336 * An event used to initialize the clone of a connection.
338 * Must be executed on the clone's async execution thread.
340 class AsyncInitializeClone final : public Runnable {
341 public:
343 * @param aConnection The connection being cloned.
344 * @param aClone The clone.
345 * @param aReadOnly If |true|, the clone is read only.
346 * @param aCallback A callback to trigger once initialization
347 * is complete. This event will be called on
348 * aClone->threadOpenedOn.
350 AsyncInitializeClone(Connection* aConnection, Connection* aClone,
351 const bool aReadOnly,
352 mozIStorageCompletionCallback* aCallback)
353 : Runnable("storage::AsyncInitializeClone"),
354 mConnection(aConnection),
355 mClone(aClone),
356 mReadOnly(aReadOnly),
357 mCallback(aCallback) {
358 MOZ_ASSERT(NS_IsMainThread());
361 NS_IMETHOD Run() override {
362 MOZ_ASSERT(!NS_IsMainThread());
363 nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
364 if (NS_FAILED(rv)) {
365 return Dispatch(rv, nullptr);
367 return Dispatch(NS_OK,
368 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
371 private:
372 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
373 RefPtr<CallbackComplete> event =
374 new CallbackComplete(aResult, aValue, mCallback.forget());
375 return mClone->threadOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
378 ~AsyncInitializeClone() override {
379 nsCOMPtr<nsIThread> thread;
380 DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
381 MOZ_ASSERT(NS_SUCCEEDED(rv));
383 // Handle ambiguous nsISupports inheritance.
384 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
385 mConnection.forget());
386 NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
388 // Generally, the callback will be released by CallbackComplete.
389 // However, if for some reason Run() is not executed, we still
390 // need to ensure that it is released here.
391 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
392 mCallback.forget());
395 RefPtr<Connection> mConnection;
396 RefPtr<Connection> mClone;
397 const bool mReadOnly;
398 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
402 * A listener for async connection closing.
404 class CloseListener final : public mozIStorageCompletionCallback {
405 public:
406 NS_DECL_ISUPPORTS
407 CloseListener() : mClosed(false) {}
409 NS_IMETHOD Complete(nsresult, nsISupports*) override {
410 mClosed = true;
411 return NS_OK;
414 bool mClosed;
416 private:
417 ~CloseListener() = default;
420 NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)
422 } // namespace
424 ////////////////////////////////////////////////////////////////////////////////
425 //// Connection
427 Connection::Connection(Service* aService, int aFlags,
428 ConnectionOperation aSupportedOperations,
429 bool aInterruptible, bool aIgnoreLockingMode)
430 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
431 sharedDBMutex("Connection::sharedDBMutex"),
432 threadOpenedOn(do_GetCurrentThread()),
433 mDBConn(nullptr),
434 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
435 mDestroying(false),
436 mProgressHandler(nullptr),
437 mStorageService(aService),
438 mFlags(aFlags),
439 mTransactionNestingLevel(0),
440 mSupportedOperations(aSupportedOperations),
441 mInterruptible(aSupportedOperations == Connection::ASYNCHRONOUS ||
442 aInterruptible),
443 mIgnoreLockingMode(aIgnoreLockingMode),
444 mAsyncExecutionThreadShuttingDown(false),
445 mConnectionClosed(false) {
446 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
447 "Can't ignore locking for a non-readonly connection!");
448 mStorageService->registerConnection(this);
451 Connection::~Connection() {
452 // Failsafe Close() occurs in our custom Release method because of
453 // complications related to Close() potentially invoking AsyncClose() which
454 // will increment our refcount.
455 MOZ_ASSERT(!mAsyncExecutionThread,
456 "The async thread has not been shutdown properly!");
459 NS_IMPL_ADDREF(Connection)
461 NS_INTERFACE_MAP_BEGIN(Connection)
462 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
463 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
464 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
465 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
466 NS_INTERFACE_MAP_END
468 // This is identical to what NS_IMPL_RELEASE provides, but with the
469 // extra |1 == count| case.
470 NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
471 MOZ_ASSERT(0 != mRefCnt, "dup release");
472 nsrefcnt count = --mRefCnt;
473 NS_LOG_RELEASE(this, count, "Connection");
474 if (1 == count) {
475 // If the refcount went to 1, the single reference must be from
476 // gService->mConnections (in class |Service|). And the code calling
477 // Release is either:
478 // - The "user" code that had created the connection, releasing on any
479 // thread.
480 // - One of Service's getConnections() callers had acquired a strong
481 // reference to the Connection that out-lived the last "user" reference,
482 // and now that just got dropped. Note that this reference could be
483 // getting dropped on the main thread or Connection->threadOpenedOn
484 // (because of the NewRunnableMethod used by minimizeMemory).
486 // Either way, we should now perform our failsafe Close() and unregister.
487 // However, we only want to do this once, and the reality is that our
488 // refcount could go back up above 1 and down again at any time if we are
489 // off the main thread and getConnections() gets called on the main thread,
490 // so we use an atomic here to do this exactly once.
491 if (mDestroying.compareExchange(false, true)) {
492 // Close the connection, dispatching to the opening thread if we're not
493 // on that thread already and that thread is still accepting runnables.
494 // We do this because it's possible we're on the main thread because of
495 // getConnections(), and we REALLY don't want to transfer I/O to the main
496 // thread if we can avoid it.
497 if (threadOpenedOn->IsOnCurrentThread()) {
498 // This could cause SpinningSynchronousClose() to be invoked and AddRef
499 // triggered for AsyncCloseConnection's strong ref if the conn was ever
500 // use for async purposes. (Main-thread only, though.)
501 Unused << synchronousClose();
502 } else {
503 nsCOMPtr<nsIRunnable> event =
504 NewRunnableMethod("storage::Connection::synchronousClose", this,
505 &Connection::synchronousClose);
506 if (NS_FAILED(
507 threadOpenedOn->Dispatch(event.forget(), NS_DISPATCH_NORMAL))) {
508 // The target thread was dead and so we've just leaked our runnable.
509 // This should not happen because our non-main-thread consumers should
510 // be explicitly closing their connections, not relying on us to close
511 // them for them. (It's okay to let a statement go out of scope for
512 // automatic cleanup, but not a Connection.)
513 MOZ_ASSERT(false,
514 "Leaked Connection::synchronousClose(), ownership fail.");
515 Unused << synchronousClose();
519 // This will drop its strong reference right here, right now.
520 mStorageService->unregisterConnection(this);
522 } else if (0 == count) {
523 mRefCnt = 1; /* stabilize */
524 #if 0 /* enable this to find non-threadsafe destructors: */
525 NS_ASSERT_OWNINGTHREAD(Connection);
526 #endif
527 delete (this);
528 return 0;
530 return count;
533 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
534 int32_t* aMaxValue) {
535 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
536 int curr = 0, max = 0;
537 DebugOnly<int> rc =
538 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
539 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
540 if (aMaxValue) *aMaxValue = max;
541 return curr;
544 nsIEventTarget* Connection::getAsyncExecutionTarget() {
545 NS_ENSURE_TRUE(threadOpenedOn == NS_GetCurrentThread(), nullptr);
547 // Don't return the asynchronous thread if we are shutting down.
548 if (mAsyncExecutionThreadShuttingDown) {
549 return nullptr;
552 // Create the async thread if there's none yet.
553 if (!mAsyncExecutionThread) {
554 static nsThreadPoolNaming naming;
555 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName("mozStorage"),
556 getter_AddRefs(mAsyncExecutionThread));
557 if (NS_FAILED(rv)) {
558 NS_WARNING("Failed to create async thread.");
559 return nullptr;
561 mAsyncExecutionThread->SetNameForWakeupTelemetry("mozStorage (all)"_ns);
564 return mAsyncExecutionThread;
567 void Connection::RecordOpenStatus(nsresult rv) {
568 nsCString histogramKey = mTelemetryFilename;
570 if (histogramKey.IsEmpty()) {
571 histogramKey.AssignLiteral("unknown");
574 if (NS_SUCCEEDED(rv)) {
575 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
576 return;
579 switch (rv) {
580 case NS_ERROR_FILE_CORRUPTED:
581 AccumulateCategoricalKeyed(histogramKey,
582 LABELS_SQLITE_STORE_OPEN::corrupt);
583 break;
584 case NS_ERROR_STORAGE_IOERR:
585 AccumulateCategoricalKeyed(histogramKey,
586 LABELS_SQLITE_STORE_OPEN::diskio);
587 break;
588 case NS_ERROR_FILE_ACCESS_DENIED:
589 case NS_ERROR_FILE_IS_LOCKED:
590 case NS_ERROR_FILE_READ_ONLY:
591 AccumulateCategoricalKeyed(histogramKey,
592 LABELS_SQLITE_STORE_OPEN::access);
593 break;
594 case NS_ERROR_FILE_NO_DEVICE_SPACE:
595 AccumulateCategoricalKeyed(histogramKey,
596 LABELS_SQLITE_STORE_OPEN::diskspace);
597 break;
598 default:
599 AccumulateCategoricalKeyed(histogramKey,
600 LABELS_SQLITE_STORE_OPEN::failure);
604 void Connection::RecordQueryStatus(int srv) {
605 nsCString histogramKey = mTelemetryFilename;
607 if (histogramKey.IsEmpty()) {
608 histogramKey.AssignLiteral("unknown");
611 switch (srv) {
612 case SQLITE_OK:
613 case SQLITE_ROW:
614 case SQLITE_DONE:
616 // Note that these are returned when we intentionally cancel a statement so
617 // they aren't indicating a failure.
618 case SQLITE_ABORT:
619 case SQLITE_INTERRUPT:
620 AccumulateCategoricalKeyed(histogramKey,
621 LABELS_SQLITE_STORE_QUERY::success);
622 break;
623 case SQLITE_CORRUPT:
624 case SQLITE_NOTADB:
625 AccumulateCategoricalKeyed(histogramKey,
626 LABELS_SQLITE_STORE_QUERY::corrupt);
627 break;
628 case SQLITE_PERM:
629 case SQLITE_CANTOPEN:
630 case SQLITE_LOCKED:
631 case SQLITE_READONLY:
632 AccumulateCategoricalKeyed(histogramKey,
633 LABELS_SQLITE_STORE_QUERY::access);
634 break;
635 case SQLITE_IOERR:
636 case SQLITE_NOLFS:
637 AccumulateCategoricalKeyed(histogramKey,
638 LABELS_SQLITE_STORE_QUERY::diskio);
639 break;
640 case SQLITE_FULL:
641 case SQLITE_TOOBIG:
642 AccumulateCategoricalKeyed(histogramKey,
643 LABELS_SQLITE_STORE_OPEN::diskspace);
644 break;
645 case SQLITE_CONSTRAINT:
646 case SQLITE_RANGE:
647 case SQLITE_MISMATCH:
648 case SQLITE_MISUSE:
649 AccumulateCategoricalKeyed(histogramKey,
650 LABELS_SQLITE_STORE_OPEN::misuse);
651 break;
652 case SQLITE_BUSY:
653 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::busy);
654 break;
655 default:
656 AccumulateCategoricalKeyed(histogramKey,
657 LABELS_SQLITE_STORE_QUERY::failure);
661 nsresult Connection::initialize(const nsACString& aStorageKey,
662 const nsACString& aName) {
663 MOZ_ASSERT(aStorageKey.Equals(kMozStorageMemoryStorageKey));
664 NS_ASSERTION(!connectionReady(),
665 "Initialize called on already opened database!");
666 MOZ_ASSERT(!mIgnoreLockingMode, "Can't ignore locking on an in-memory db.");
667 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
669 mStorageKey = aStorageKey;
670 mName = aName;
672 // in memory database requested, sqlite uses a magic file name
674 const nsAutoCString path =
675 mName.IsEmpty() ? nsAutoCString(":memory:"_ns)
676 : "file:"_ns + mName + "?mode=memory&cache=shared"_ns;
678 mTelemetryFilename.AssignLiteral(":memory:");
680 int srv = ::sqlite3_open_v2(path.get(), &mDBConn, mFlags,
681 GetTelemetryVFSName(true));
682 if (srv != SQLITE_OK) {
683 mDBConn = nullptr;
684 nsresult rv = convertResultCode(srv);
685 RecordOpenStatus(rv);
686 return rv;
689 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
690 srv =
691 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
692 MOZ_ASSERT(srv == SQLITE_OK,
693 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
694 #endif
696 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
697 // database.
699 nsresult rv = initializeInternal();
700 RecordOpenStatus(rv);
701 NS_ENSURE_SUCCESS(rv, rv);
703 return NS_OK;
706 nsresult Connection::initialize(nsIFile* aDatabaseFile) {
707 NS_ASSERTION(aDatabaseFile, "Passed null file!");
708 NS_ASSERTION(!connectionReady(),
709 "Initialize called on already opened database!");
710 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
712 // Do not set mFileURL here since this is database does not have an associated
713 // URL.
714 mDatabaseFile = aDatabaseFile;
715 aDatabaseFile->GetNativeLeafName(mTelemetryFilename);
717 nsAutoString path;
718 nsresult rv = aDatabaseFile->GetPath(path);
719 NS_ENSURE_SUCCESS(rv, rv);
721 #ifdef XP_WIN
722 static const char* sIgnoreLockingVFS = "win32-none";
723 #else
724 static const char* sIgnoreLockingVFS = "unix-none";
725 #endif
727 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
728 int srv;
729 if (mIgnoreLockingMode) {
730 exclusive = false;
731 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
732 sIgnoreLockingVFS);
733 } else {
734 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
735 GetTelemetryVFSName(exclusive));
736 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
737 // Retry without trying to get an exclusive lock.
738 exclusive = false;
739 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
740 mFlags, GetTelemetryVFSName(false));
743 if (srv != SQLITE_OK) {
744 mDBConn = nullptr;
745 rv = convertResultCode(srv);
746 RecordOpenStatus(rv);
747 return rv;
750 rv = initializeInternal();
751 if (exclusive &&
752 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
753 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
754 // some cases it may successfully open the database and then lock on the
755 // first query execution. When initializeInternal fails it closes the
756 // connection, so we can try to restart it in non-exclusive mode.
757 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
758 GetTelemetryVFSName(false));
759 if (srv == SQLITE_OK) {
760 rv = initializeInternal();
764 RecordOpenStatus(rv);
765 NS_ENSURE_SUCCESS(rv, rv);
767 return NS_OK;
770 nsresult Connection::initialize(nsIFileURL* aFileURL,
771 const nsACString& aTelemetryFilename) {
772 NS_ASSERTION(aFileURL, "Passed null file URL!");
773 NS_ASSERTION(!connectionReady(),
774 "Initialize called on already opened database!");
775 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
777 nsCOMPtr<nsIFile> databaseFile;
778 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
779 NS_ENSURE_SUCCESS(rv, rv);
781 // Set both mDatabaseFile and mFileURL here.
782 mFileURL = aFileURL;
783 mDatabaseFile = databaseFile;
785 if (!aTelemetryFilename.IsEmpty()) {
786 mTelemetryFilename = aTelemetryFilename;
787 } else {
788 databaseFile->GetNativeLeafName(mTelemetryFilename);
791 nsAutoCString spec;
792 rv = aFileURL->GetSpec(spec);
793 NS_ENSURE_SUCCESS(rv, rv);
795 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
797 // If there is a key specified, we need to use the obfuscating VFS.
798 nsAutoCString query;
799 rv = aFileURL->GetQuery(query);
800 NS_ENSURE_SUCCESS(rv, rv);
801 const char* const vfs =
802 URLParams::Parse(query,
803 [](const nsAString& aName, const nsAString& aValue) {
804 return aName.EqualsLiteral("key");
806 ? GetObfuscatingVFSName()
807 : GetTelemetryVFSName(exclusive);
808 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
809 if (srv != SQLITE_OK) {
810 mDBConn = nullptr;
811 rv = convertResultCode(srv);
812 RecordOpenStatus(rv);
813 return rv;
816 rv = initializeInternal();
817 RecordOpenStatus(rv);
818 NS_ENSURE_SUCCESS(rv, rv);
820 return NS_OK;
823 nsresult Connection::initializeInternal() {
824 MOZ_ASSERT(mDBConn);
825 auto guard = MakeScopeExit([&]() { initializeFailed(); });
827 mConnectionClosed = false;
829 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
830 DebugOnly<int> srv2 =
831 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
832 MOZ_ASSERT(srv2 == SQLITE_OK,
833 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
834 #endif
836 MOZ_ASSERT(!mTelemetryFilename.IsEmpty(),
837 "A telemetry filename should have been set by now.");
839 // Properly wrap the database handle's mutex.
840 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
842 // SQLite tracing can slow down queries (especially long queries)
843 // significantly. Don't trace unless the user is actively monitoring SQLite.
844 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
845 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
846 tracefunc, this);
848 MOZ_LOG(
849 gStorageLog, LogLevel::Debug,
850 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
853 int64_t pageSize = Service::kDefaultPageSize;
855 // Set page_size to the preferred default value. This is effective only if
856 // the database has just been created, otherwise, if the database does not
857 // use WAL journal mode, a VACUUM operation will updated its page_size.
858 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
859 "PRAGMA page_size = ");
860 pageSizeQuery.AppendInt(pageSize);
861 int srv = executeSql(mDBConn, pageSizeQuery.get());
862 if (srv != SQLITE_OK) {
863 return convertResultCode(srv);
866 // Setting the cache_size forces the database open, verifying if it is valid
867 // or corrupt. So this is executed regardless it being actually needed.
868 // The cache_size is calculated from the actual page_size, to save memory.
869 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
870 "PRAGMA cache_size = ");
871 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
872 srv = executeSql(mDBConn, cacheSizeQuery.get());
873 if (srv != SQLITE_OK) {
874 return convertResultCode(srv);
877 // Register our built-in SQL functions.
878 srv = registerFunctions(mDBConn);
879 if (srv != SQLITE_OK) {
880 return convertResultCode(srv);
883 // Register our built-in SQL collating sequences.
884 srv = registerCollations(mDBConn, mStorageService);
885 if (srv != SQLITE_OK) {
886 return convertResultCode(srv);
889 // Set the default synchronous value. Each consumer can switch this
890 // accordingly to their needs.
891 #if defined(ANDROID)
892 // Android prefers synchronous = OFF for performance reasons.
893 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
894 #else
895 // Normal is the suggested value for WAL journals.
896 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
897 #endif
899 // Initialization succeeded, we can stop guarding for failures.
900 guard.release();
901 return NS_OK;
904 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
905 MOZ_ASSERT(threadOpenedOn != NS_GetCurrentThread());
906 nsresult rv = aStorageFile
907 ? initialize(aStorageFile)
908 : initialize(kMozStorageMemoryStorageKey, VoidCString());
909 if (NS_FAILED(rv)) {
910 // Shutdown the async thread, since initialization failed.
911 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
912 mAsyncExecutionThreadShuttingDown = true;
913 nsCOMPtr<nsIRunnable> event =
914 NewRunnableMethod("Connection::shutdownAsyncThread", this,
915 &Connection::shutdownAsyncThread);
916 Unused << NS_DispatchToMainThread(event);
918 return rv;
921 void Connection::initializeFailed() {
923 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
924 mConnectionClosed = true;
926 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
927 mDBConn = nullptr;
928 sharedDBMutex.destroy();
931 nsresult Connection::databaseElementExists(
932 enum DatabaseElementType aElementType, const nsACString& aElementName,
933 bool* _exists) {
934 if (!connectionReady()) {
935 return NS_ERROR_NOT_AVAILABLE;
937 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
938 if (NS_FAILED(rv)) {
939 return rv;
942 // When constructing the query, make sure to SELECT the correct db's
943 // sqlite_master if the user is prefixing the element with a specific db. ex:
944 // sample.test
945 nsCString query("SELECT name FROM (SELECT * FROM ");
946 nsDependentCSubstring element;
947 int32_t ind = aElementName.FindChar('.');
948 if (ind == kNotFound) {
949 element.Assign(aElementName);
950 } else {
951 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
952 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
953 query.Append(db);
955 query.AppendLiteral(
956 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
957 "'");
959 switch (aElementType) {
960 case INDEX:
961 query.AppendLiteral("index");
962 break;
963 case TABLE:
964 query.AppendLiteral("table");
965 break;
967 query.AppendLiteral("' AND name ='");
968 query.Append(element);
969 query.Append('\'');
971 sqlite3_stmt* stmt;
972 int srv = prepareStatement(mDBConn, query, &stmt);
973 if (srv != SQLITE_OK) {
974 RecordQueryStatus(srv);
975 return convertResultCode(srv);
978 srv = stepStatement(mDBConn, stmt);
979 // we just care about the return value from step
980 (void)::sqlite3_finalize(stmt);
982 RecordQueryStatus(srv);
984 if (srv == SQLITE_ROW) {
985 *_exists = true;
986 return NS_OK;
988 if (srv == SQLITE_DONE) {
989 *_exists = false;
990 return NS_OK;
993 return convertResultCode(srv);
996 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
997 sharedDBMutex.assertCurrentThreadOwns();
999 for (const auto& data : mFunctions.Values()) {
1000 if (data.function == aInstance) {
1001 return true;
1004 return false;
1007 /* static */
1008 int Connection::sProgressHelper(void* aArg) {
1009 Connection* _this = static_cast<Connection*>(aArg);
1010 return _this->progressHandler();
1013 int Connection::progressHandler() {
1014 sharedDBMutex.assertCurrentThreadOwns();
1015 if (mProgressHandler) {
1016 bool result;
1017 nsresult rv = mProgressHandler->OnProgress(this, &result);
1018 if (NS_FAILED(rv)) return 0; // Don't break request
1019 return result ? 1 : 0;
1021 return 0;
1024 nsresult Connection::setClosedState() {
1025 // Ensure that we are on the correct thread to close the database.
1026 bool onOpenedThread;
1027 nsresult rv = threadOpenedOn->IsOnCurrentThread(&onOpenedThread);
1028 NS_ENSURE_SUCCESS(rv, rv);
1029 if (!onOpenedThread) {
1030 NS_ERROR("Must close the database on the thread that you opened it with!");
1031 return NS_ERROR_UNEXPECTED;
1034 // Flag that we are shutting down the async thread, so that
1035 // getAsyncExecutionTarget knows not to expose/create the async thread.
1037 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1038 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1039 mAsyncExecutionThreadShuttingDown = true;
1041 // Set the property to null before closing the connection, otherwise the
1042 // other functions in the module may try to use the connection after it is
1043 // closed.
1044 mDBConn = nullptr;
1046 return NS_OK;
1049 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1050 if (aOperationType == ASYNCHRONOUS) {
1051 // Async operations are supported for all connections, on any thread.
1052 return true;
1054 // Sync operations are supported for sync connections (on any thread), and
1055 // async connections on a background thread.
1056 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1057 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1060 nsresult Connection::ensureOperationSupported(
1061 ConnectionOperation aOperationType) {
1062 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1063 #ifdef DEBUG
1064 if (NS_IsMainThread()) {
1065 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1066 Unused << xpc->DebugDumpJSStack(false, false, false);
1068 #endif
1069 MOZ_ASSERT(false,
1070 "Don't use async connections synchronously on the main thread");
1071 return NS_ERROR_NOT_AVAILABLE;
1073 return NS_OK;
1076 bool Connection::isConnectionReadyOnThisThread() {
1077 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1078 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1079 return true;
1081 return connectionReady();
1084 bool Connection::isClosing() {
1085 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1086 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1089 bool Connection::isClosed() {
1090 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1091 return mConnectionClosed;
1094 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1096 bool Connection::isAsyncExecutionThreadAvailable() {
1097 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1098 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1101 void Connection::shutdownAsyncThread() {
1102 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1103 MOZ_ASSERT(mAsyncExecutionThread);
1104 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1106 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1107 mAsyncExecutionThread = nullptr;
1110 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1111 #ifdef DEBUG
1112 { // Make sure we have marked our async thread as shutting down.
1113 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1114 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1115 "Did not call setClosedState!");
1116 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1118 #endif // DEBUG
1120 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1121 nsAutoCString leafName(":memory");
1122 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1123 MOZ_LOG(gStorageLog, LogLevel::Debug,
1124 ("Closing connection to '%s'", leafName.get()));
1127 // At this stage, we may still have statements that need to be
1128 // finalized. Attempt to close the database connection. This will
1129 // always disconnect any virtual tables and cleanly finalize their
1130 // internal statements. Once this is done, closing may fail due to
1131 // unfinalized client statements, in which case we need to finalize
1132 // these statements and close again.
1134 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1135 mConnectionClosed = true;
1138 // Nothing else needs to be done if we don't have a connection here.
1139 if (!aNativeConnection) return NS_OK;
1141 int srv = ::sqlite3_close(aNativeConnection);
1143 if (srv == SQLITE_BUSY) {
1145 // Nothing else should change the connection or statements status until we
1146 // are done here.
1147 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1148 // We still have non-finalized statements. Finalize them.
1149 sqlite3_stmt* stmt = nullptr;
1150 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1151 MOZ_LOG(gStorageLog, LogLevel::Debug,
1152 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1153 stmt));
1155 #ifdef DEBUG
1156 SmprintfPointer msg = ::mozilla::Smprintf(
1157 "SQL statement '%s' (%p) should have been finalized before closing "
1158 "the connection",
1159 ::sqlite3_sql(stmt), stmt);
1160 NS_WARNING(msg.get());
1161 #endif // DEBUG
1163 srv = ::sqlite3_finalize(stmt);
1165 #ifdef DEBUG
1166 if (srv != SQLITE_OK) {
1167 SmprintfPointer msg = ::mozilla::Smprintf(
1168 "Could not finalize SQL statement (%p)", stmt);
1169 NS_WARNING(msg.get());
1171 #endif // DEBUG
1173 // Ensure that the loop continues properly, whether closing has
1174 // succeeded or not.
1175 if (srv == SQLITE_OK) {
1176 stmt = nullptr;
1179 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1180 // again, since Sqlite will try to acquire it.
1183 // Now that all statements have been finalized, we
1184 // should be able to close.
1185 srv = ::sqlite3_close(aNativeConnection);
1186 MOZ_ASSERT(false,
1187 "Had to forcibly close the database connection because not all "
1188 "the statements have been finalized.");
1191 if (srv == SQLITE_OK) {
1192 sharedDBMutex.destroy();
1193 } else {
1194 MOZ_ASSERT(false,
1195 "sqlite3_close failed. There are probably outstanding "
1196 "statements that are listed above!");
1199 return convertResultCode(srv);
1202 nsCString Connection::getFilename() { return mTelemetryFilename; }
1204 int Connection::stepStatement(sqlite3* aNativeConnection,
1205 sqlite3_stmt* aStatement) {
1206 MOZ_ASSERT(aStatement);
1208 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1209 ::sqlite3_sql(aStatement));
1211 bool checkedMainThread = false;
1212 TimeStamp startTime = TimeStamp::Now();
1214 // The connection may have been closed if the executing statement has been
1215 // created and cached after a call to asyncClose() but before the actual
1216 // sqlite3_close(). This usually happens when other tasks using cached
1217 // statements are asynchronously scheduled for execution and any of them ends
1218 // up after asyncClose. See bug 728653 for details.
1219 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1221 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1223 int srv;
1224 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1225 if (!checkedMainThread) {
1226 checkedMainThread = true;
1227 if (::NS_IsMainThread()) {
1228 NS_WARNING("We won't allow blocking on the main thread!");
1229 break;
1233 srv = WaitForUnlockNotify(aNativeConnection);
1234 if (srv != SQLITE_OK) {
1235 break;
1238 ::sqlite3_reset(aStatement);
1241 // Report very slow SQL statements to Telemetry
1242 TimeDuration duration = TimeStamp::Now() - startTime;
1243 const uint32_t threshold = NS_IsMainThread()
1244 ? Telemetry::kSlowSQLThresholdForMainThread
1245 : Telemetry::kSlowSQLThresholdForHelperThreads;
1246 if (duration.ToMilliseconds() >= threshold) {
1247 nsDependentCString statementString(::sqlite3_sql(aStatement));
1248 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1249 duration.ToMilliseconds());
1252 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1253 // Drop off the extended result bits of the result code.
1254 return srv & 0xFF;
1257 int Connection::prepareStatement(sqlite3* aNativeConnection,
1258 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1259 // We should not even try to prepare statements after the connection has
1260 // been closed.
1261 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1263 bool checkedMainThread = false;
1265 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1267 int srv;
1268 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1269 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1270 if (!checkedMainThread) {
1271 checkedMainThread = true;
1272 if (::NS_IsMainThread()) {
1273 NS_WARNING("We won't allow blocking on the main thread!");
1274 break;
1278 srv = WaitForUnlockNotify(aNativeConnection);
1279 if (srv != SQLITE_OK) {
1280 break;
1284 if (srv != SQLITE_OK) {
1285 nsCString warnMsg;
1286 warnMsg.AppendLiteral("The SQL statement '");
1287 warnMsg.Append(aSQL);
1288 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1289 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1291 #ifdef DEBUG
1292 NS_WARNING(warnMsg.get());
1293 #endif
1294 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1297 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1298 // Drop off the extended result bits of the result code.
1299 int rc = srv & 0xFF;
1300 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1301 // The callers of this function are used to only checking the return value,
1302 // so it is safer to return an error code.
1303 if (rc == SQLITE_OK && *_stmt == nullptr) {
1304 return SQLITE_MISUSE;
1307 return rc;
1310 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1311 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1313 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1315 TimeStamp startTime = TimeStamp::Now();
1316 int srv =
1317 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1318 RecordQueryStatus(srv);
1320 // Report very slow SQL statements to Telemetry
1321 TimeDuration duration = TimeStamp::Now() - startTime;
1322 const uint32_t threshold = NS_IsMainThread()
1323 ? Telemetry::kSlowSQLThresholdForMainThread
1324 : Telemetry::kSlowSQLThresholdForHelperThreads;
1325 if (duration.ToMilliseconds() >= threshold) {
1326 nsDependentCString statementString(aSqlString);
1327 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1328 duration.ToMilliseconds());
1331 return srv;
1334 ////////////////////////////////////////////////////////////////////////////////
1335 //// nsIInterfaceRequestor
1337 NS_IMETHODIMP
1338 Connection::GetInterface(const nsIID& aIID, void** _result) {
1339 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1340 nsIEventTarget* background = getAsyncExecutionTarget();
1341 NS_IF_ADDREF(background);
1342 *_result = background;
1343 return NS_OK;
1345 return NS_ERROR_NO_INTERFACE;
1348 ////////////////////////////////////////////////////////////////////////////////
1349 //// mozIStorageConnection
1351 NS_IMETHODIMP
1352 Connection::Close() {
1353 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1354 if (NS_FAILED(rv)) {
1355 return rv;
1357 return synchronousClose();
1360 nsresult Connection::synchronousClose() {
1361 if (!connectionReady()) {
1362 return NS_ERROR_NOT_INITIALIZED;
1365 #ifdef DEBUG
1366 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1367 // thread. We make this check outside of debug code below in setClosedState,
1368 // but this is here to be explicit.
1369 bool onOpenerThread = false;
1370 (void)threadOpenedOn->IsOnCurrentThread(&onOpenerThread);
1371 MOZ_ASSERT(onOpenerThread);
1372 #endif // DEBUG
1374 // Make sure we have not executed any asynchronous statements.
1375 // If this fails, the mDBConn may be left open, resulting in a leak.
1376 // We'll try to finalize the pending statements and close the connection.
1377 if (isAsyncExecutionThreadAvailable()) {
1378 #ifdef DEBUG
1379 if (NS_IsMainThread()) {
1380 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1381 Unused << xpc->DebugDumpJSStack(false, false, false);
1383 #endif
1384 MOZ_ASSERT(false,
1385 "Close() was invoked on a connection that executed asynchronous "
1386 "statements. "
1387 "Should have used asyncClose().");
1388 // Try to close the database regardless, to free up resources.
1389 Unused << SpinningSynchronousClose();
1390 return NS_ERROR_UNEXPECTED;
1393 // setClosedState nullifies our connection pointer, so we take a raw pointer
1394 // off it, to pass it through the close procedure.
1395 sqlite3* nativeConn = mDBConn;
1396 nsresult rv = setClosedState();
1397 NS_ENSURE_SUCCESS(rv, rv);
1399 return internalClose(nativeConn);
1402 NS_IMETHODIMP
1403 Connection::SpinningSynchronousClose() {
1404 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1405 if (NS_FAILED(rv)) {
1406 return rv;
1408 if (threadOpenedOn != NS_GetCurrentThread()) {
1409 return NS_ERROR_NOT_SAME_THREAD;
1412 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1413 // Our only existing caller will never have called close; assert if misused
1414 // so that no new callers assume this works after an AsyncClose.
1415 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1416 if (!connectionReady()) {
1417 return NS_ERROR_UNEXPECTED;
1420 RefPtr<CloseListener> listener = new CloseListener();
1421 rv = AsyncClose(listener);
1422 NS_ENSURE_SUCCESS(rv, rv);
1423 MOZ_ALWAYS_TRUE(
1424 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,
1425 [&]() { return listener->mClosed; }));
1426 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1428 return rv;
1431 NS_IMETHODIMP
1432 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1433 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1434 // Check if AsyncClose or Close were already invoked.
1435 if (!connectionReady()) {
1436 return NS_ERROR_NOT_INITIALIZED;
1438 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1439 if (NS_FAILED(rv)) {
1440 return rv;
1443 // The two relevant factors at this point are whether we have a database
1444 // connection and whether we have an async execution thread. Here's what the
1445 // states mean and how we handle them:
1447 // - (mDBConn && asyncThread): The expected case where we are either an
1448 // async connection or a sync connection that has been used asynchronously.
1449 // Either way the caller must call us and not Close(). Nothing surprising
1450 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1451 // async thread.
1453 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1454 // opened the connection synchronously and was planning to use it
1455 // asynchronously, but never got around to using it asynchronously before
1456 // needing to shutdown. This has been observed to happen for the cookie
1457 // service in a case where Firefox shuts itself down almost immediately
1458 // after startup (for unknown reasons). In the Firefox shutdown case,
1459 // we may also fail to create a new async execution thread if one does not
1460 // already exist. (nsThreadManager will refuse to create new threads when
1461 // it has already been told to shutdown.) As such, we need to handle a
1462 // failure to create the async execution thread by falling back to
1463 // synchronous Close() and also dispatching the completion callback because
1464 // at least Places likes to spin a nested event loop that depends on the
1465 // callback being invoked.
1467 // Note that we have considered not trying to spin up the async execution
1468 // thread in this case if it does not already exist, but the overhead of
1469 // thread startup (if successful) is significantly less expensive than the
1470 // worst-case potential I/O hit of synchronously closing a database when we
1471 // could close it asynchronously.
1473 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1474 // OpenAsyncDatabase encountered a problem opening the database. If it
1475 // happened in all cases AsyncInitDatabase would just shut down the thread
1476 // directly and we would avoid this case. But it doesn't, so for simplicity
1477 // and consistency AsyncCloseConnection knows how to handle this and we
1478 // act like this was the (mDBConn && asyncThread) case in this method.
1480 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1481 // Close() or AsyncClose() has already been called (at least) once. This is
1482 // undeniably a misuse case by the caller. We could optimize for this
1483 // case by adding an additional check of mAsyncExecutionThread without using
1484 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1485 // shut it down. But this complicates the method for broken caller code
1486 // whereas we're still correct and safe without the special-case.
1487 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1489 // Create our callback event if we were given a callback. This will
1490 // eventually be dispatched in all cases, even if we fall back to Close() and
1491 // the database wasn't open and we return an error. The rationale is that
1492 // no existing consumer checks our return value and several of them like to
1493 // spin nested event loops until the callback fires. Given that, it seems
1494 // preferable for us to dispatch the callback in all cases. (Except the
1495 // wrong thread misuse case we bailed on up above. But that's okay because
1496 // that is statically wrong whereas these edge cases are dynamic.)
1497 nsCOMPtr<nsIRunnable> completeEvent;
1498 if (aCallback) {
1499 completeEvent = newCompletionEvent(aCallback);
1502 if (!asyncThread) {
1503 // We were unable to create an async thread, so we need to fall back to
1504 // using normal Close(). Since there is no async thread, Close() will
1505 // not complain about that. (Close() may, however, complain if the
1506 // connection is closed, but that's okay.)
1507 if (completeEvent) {
1508 // Closing the database is more important than returning an error code
1509 // about a failure to dispatch, especially because all existing native
1510 // callers ignore our return value.
1511 Unused << NS_DispatchToMainThread(completeEvent.forget());
1513 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1514 // Return a success inconditionally here, since Close() is unlikely to fail
1515 // and we want to reassure the consumer that its callback will be invoked.
1516 return NS_OK;
1519 // setClosedState nullifies our connection pointer, so we take a raw pointer
1520 // off it, to pass it through the close procedure.
1521 sqlite3* nativeConn = mDBConn;
1522 rv = setClosedState();
1523 NS_ENSURE_SUCCESS(rv, rv);
1525 // Create and dispatch our close event to the background thread.
1526 nsCOMPtr<nsIRunnable> closeEvent =
1527 new AsyncCloseConnection(this, nativeConn, completeEvent);
1528 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1529 NS_ENSURE_SUCCESS(rv, rv);
1531 return NS_OK;
1534 NS_IMETHODIMP
1535 Connection::AsyncClone(bool aReadOnly,
1536 mozIStorageCompletionCallback* aCallback) {
1537 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1539 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1540 if (!connectionReady()) {
1541 return NS_ERROR_NOT_INITIALIZED;
1543 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1544 if (NS_FAILED(rv)) {
1545 return rv;
1547 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1549 int flags = mFlags;
1550 if (aReadOnly) {
1551 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1552 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1553 // Turn off SQLITE_OPEN_CREATE.
1554 flags = (~SQLITE_OPEN_CREATE & flags);
1557 // The cloned connection will still implement the synchronous API, but throw
1558 // if any synchronous methods are called on the main thread.
1559 RefPtr<Connection> clone =
1560 new Connection(mStorageService, flags, ASYNCHRONOUS);
1562 RefPtr<AsyncInitializeClone> initEvent =
1563 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1564 // Dispatch to our async thread, since the originating connection must remain
1565 // valid and open for the whole cloning process. This also ensures we are
1566 // properly serialized with a `close` operation, rather than race with it.
1567 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1568 if (!target) {
1569 return NS_ERROR_UNEXPECTED;
1571 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1574 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1575 nsresult rv;
1576 if (!mStorageKey.IsEmpty()) {
1577 rv = aClone->initialize(mStorageKey, mName);
1578 } else if (mFileURL) {
1579 rv = aClone->initialize(mFileURL, mTelemetryFilename);
1580 } else {
1581 rv = aClone->initialize(mDatabaseFile);
1583 if (NS_FAILED(rv)) {
1584 return rv;
1587 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1589 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1590 NS_ENSURE_SUCCESS(rv, rv);
1592 // Re-attach on-disk databases that were attached to the original connection.
1594 nsCOMPtr<mozIStorageStatement> stmt;
1595 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1596 MOZ_ASSERT(NS_SUCCEEDED(rv));
1597 bool hasResult = false;
1598 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1599 nsAutoCString name;
1600 rv = stmt->GetUTF8String(1, name);
1601 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1602 !name.EqualsLiteral("temp")) {
1603 nsCString path;
1604 rv = stmt->GetUTF8String(2, path);
1605 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1606 nsCOMPtr<mozIStorageStatement> attachStmt;
1607 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1608 getter_AddRefs(attachStmt));
1609 MOZ_ASSERT(NS_SUCCEEDED(rv));
1610 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1611 MOZ_ASSERT(NS_SUCCEEDED(rv));
1612 rv = attachStmt->Execute();
1613 MOZ_ASSERT(NS_SUCCEEDED(rv),
1614 "couldn't re-attach database to cloned connection");
1620 // Copy over pragmas from the original connection.
1621 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1622 // schema ("main" and any other attached databases), and this implmentation
1623 // fails to propagate them. This is being addressed on trunk.
1624 static const char* pragmas[] = {
1625 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1626 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1627 for (auto& pragma : pragmas) {
1628 // Read-only connections just need cache_size and temp_store pragmas.
1629 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1630 ::strcmp(pragma, "temp_store") != 0) {
1631 continue;
1634 nsAutoCString pragmaQuery("PRAGMA ");
1635 pragmaQuery.Append(pragma);
1636 nsCOMPtr<mozIStorageStatement> stmt;
1637 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
1638 MOZ_ASSERT(NS_SUCCEEDED(rv));
1639 bool hasResult = false;
1640 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1641 pragmaQuery.AppendLiteral(" = ");
1642 pragmaQuery.AppendInt(stmt->AsInt32(0));
1643 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
1644 MOZ_ASSERT(NS_SUCCEEDED(rv));
1648 // Copy over temporary tables, triggers, and views from the original
1649 // connections. Entities in `sqlite_temp_master` are only visible to the
1650 // connection that created them.
1651 if (!aReadOnly) {
1652 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
1653 NS_ENSURE_SUCCESS(rv, rv);
1655 nsCOMPtr<mozIStorageStatement> stmt;
1656 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1657 "WHERE type IN ('table', 'view', "
1658 "'index', 'trigger')"),
1659 getter_AddRefs(stmt));
1660 // Propagate errors, because failing to copy triggers might cause schema
1661 // coherency issues when writing to the database from the cloned connection.
1662 NS_ENSURE_SUCCESS(rv, rv);
1663 bool hasResult = false;
1664 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1665 nsAutoCString query;
1666 rv = stmt->GetUTF8String(0, query);
1667 NS_ENSURE_SUCCESS(rv, rv);
1669 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1670 // keyword. We need to add it back, or we'll recreate temporary entities
1671 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1672 // statements, but those don't need `TEMP` keywords.
1673 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
1674 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
1675 StringBeginsWith(query, "CREATE VIEW "_ns)) {
1676 query.Replace(0, 6, "CREATE TEMP");
1679 rv = aClone->ExecuteSimpleSQL(query);
1680 NS_ENSURE_SUCCESS(rv, rv);
1683 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
1684 NS_ENSURE_SUCCESS(rv, rv);
1687 // Copy any functions that have been added to this connection.
1688 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1689 for (const auto& entry : mFunctions) {
1690 const nsACString& key = entry.GetKey();
1691 Connection::FunctionInfo data = entry.GetData();
1693 rv = aClone->CreateFunction(key, data.numArgs, data.function);
1694 if (NS_FAILED(rv)) {
1695 NS_WARNING("Failed to copy function to cloned connection");
1699 guard.release();
1700 return NS_OK;
1703 NS_IMETHODIMP
1704 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
1705 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1707 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
1709 if (!connectionReady()) {
1710 return NS_ERROR_NOT_INITIALIZED;
1712 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1713 if (NS_FAILED(rv)) {
1714 return rv;
1717 int flags = mFlags;
1718 if (aReadOnly) {
1719 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1720 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1721 // Turn off SQLITE_OPEN_CREATE.
1722 flags = (~SQLITE_OPEN_CREATE & flags);
1725 RefPtr<Connection> clone = new Connection(
1726 mStorageService, flags, mSupportedOperations, mInterruptible);
1728 rv = initializeClone(clone, aReadOnly);
1729 if (NS_FAILED(rv)) {
1730 return rv;
1733 NS_IF_ADDREF(*_connection = clone);
1734 return NS_OK;
1737 NS_IMETHODIMP
1738 Connection::Interrupt() {
1739 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
1740 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
1741 threadOpenedOn != NS_GetCurrentThread());
1742 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
1743 threadOpenedOn == NS_GetCurrentThread());
1745 if (!connectionReady()) {
1746 return NS_ERROR_NOT_INITIALIZED;
1749 if (isClosing()) { // Closing already in asynchronous case
1750 return NS_OK;
1754 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
1755 // it is not safe to call sqlite3_interrupt() when
1756 // database connection is closed or might close before
1757 // sqlite3_interrupt() returns.
1758 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1759 if (!isClosed(lockedScope)) {
1760 MOZ_ASSERT(mDBConn);
1761 ::sqlite3_interrupt(mDBConn);
1765 return NS_OK;
1768 NS_IMETHODIMP
1769 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
1770 *_defaultPageSize = Service::kDefaultPageSize;
1771 return NS_OK;
1774 NS_IMETHODIMP
1775 Connection::GetConnectionReady(bool* _ready) {
1776 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1777 *_ready = connectionReady();
1778 return NS_OK;
1781 NS_IMETHODIMP
1782 Connection::GetDatabaseFile(nsIFile** _dbFile) {
1783 if (!connectionReady()) {
1784 return NS_ERROR_NOT_INITIALIZED;
1786 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1787 if (NS_FAILED(rv)) {
1788 return rv;
1791 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
1793 return NS_OK;
1796 NS_IMETHODIMP
1797 Connection::GetLastInsertRowID(int64_t* _id) {
1798 if (!connectionReady()) {
1799 return NS_ERROR_NOT_INITIALIZED;
1801 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1802 if (NS_FAILED(rv)) {
1803 return rv;
1806 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
1807 *_id = id;
1809 return NS_OK;
1812 NS_IMETHODIMP
1813 Connection::GetAffectedRows(int32_t* _rows) {
1814 if (!connectionReady()) {
1815 return NS_ERROR_NOT_INITIALIZED;
1817 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1818 if (NS_FAILED(rv)) {
1819 return rv;
1822 *_rows = ::sqlite3_changes(mDBConn);
1824 return NS_OK;
1827 NS_IMETHODIMP
1828 Connection::GetLastError(int32_t* _error) {
1829 if (!connectionReady()) {
1830 return NS_ERROR_NOT_INITIALIZED;
1832 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1833 if (NS_FAILED(rv)) {
1834 return rv;
1837 *_error = ::sqlite3_errcode(mDBConn);
1839 return NS_OK;
1842 NS_IMETHODIMP
1843 Connection::GetLastErrorString(nsACString& _errorString) {
1844 if (!connectionReady()) {
1845 return NS_ERROR_NOT_INITIALIZED;
1847 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1848 if (NS_FAILED(rv)) {
1849 return rv;
1852 const char* serr = ::sqlite3_errmsg(mDBConn);
1853 _errorString.Assign(serr);
1855 return NS_OK;
1858 NS_IMETHODIMP
1859 Connection::GetSchemaVersion(int32_t* _version) {
1860 if (!connectionReady()) {
1861 return NS_ERROR_NOT_INITIALIZED;
1863 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1864 if (NS_FAILED(rv)) {
1865 return rv;
1868 nsCOMPtr<mozIStorageStatement> stmt;
1869 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
1870 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
1872 *_version = 0;
1873 bool hasResult;
1874 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult)
1875 *_version = stmt->AsInt32(0);
1877 return NS_OK;
1880 NS_IMETHODIMP
1881 Connection::SetSchemaVersion(int32_t aVersion) {
1882 if (!connectionReady()) {
1883 return NS_ERROR_NOT_INITIALIZED;
1885 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1886 if (NS_FAILED(rv)) {
1887 return rv;
1890 nsAutoCString stmt("PRAGMA user_version = "_ns);
1891 stmt.AppendInt(aVersion);
1893 return ExecuteSimpleSQL(stmt);
1896 NS_IMETHODIMP
1897 Connection::CreateStatement(const nsACString& aSQLStatement,
1898 mozIStorageStatement** _stmt) {
1899 NS_ENSURE_ARG_POINTER(_stmt);
1900 if (!connectionReady()) {
1901 return NS_ERROR_NOT_INITIALIZED;
1903 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1904 if (NS_FAILED(rv)) {
1905 return rv;
1908 RefPtr<Statement> statement(new Statement());
1909 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1911 rv = statement->initialize(this, mDBConn, aSQLStatement);
1912 NS_ENSURE_SUCCESS(rv, rv);
1914 Statement* rawPtr;
1915 statement.forget(&rawPtr);
1916 *_stmt = rawPtr;
1917 return NS_OK;
1920 NS_IMETHODIMP
1921 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
1922 mozIStorageAsyncStatement** _stmt) {
1923 NS_ENSURE_ARG_POINTER(_stmt);
1924 if (!connectionReady()) {
1925 return NS_ERROR_NOT_INITIALIZED;
1927 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1928 if (NS_FAILED(rv)) {
1929 return rv;
1932 RefPtr<AsyncStatement> statement(new AsyncStatement());
1933 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1935 rv = statement->initialize(this, mDBConn, aSQLStatement);
1936 NS_ENSURE_SUCCESS(rv, rv);
1938 AsyncStatement* rawPtr;
1939 statement.forget(&rawPtr);
1940 *_stmt = rawPtr;
1941 return NS_OK;
1944 NS_IMETHODIMP
1945 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
1946 CHECK_MAINTHREAD_ABUSE();
1947 if (!connectionReady()) {
1948 return NS_ERROR_NOT_INITIALIZED;
1950 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1951 if (NS_FAILED(rv)) {
1952 return rv;
1955 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
1956 return convertResultCode(srv);
1959 NS_IMETHODIMP
1960 Connection::ExecuteAsync(
1961 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
1962 mozIStorageStatementCallback* aCallback,
1963 mozIStoragePendingStatement** _handle) {
1964 nsTArray<StatementData> stmts(aStatements.Length());
1965 for (uint32_t i = 0; i < aStatements.Length(); i++) {
1966 nsCOMPtr<StorageBaseStatementInternal> stmt =
1967 do_QueryInterface(aStatements[i]);
1968 NS_ENSURE_STATE(stmt);
1970 // Obtain our StatementData.
1971 StatementData data;
1972 nsresult rv = stmt->getAsynchronousStatementData(data);
1973 NS_ENSURE_SUCCESS(rv, rv);
1975 NS_ASSERTION(stmt->getOwner() == this,
1976 "Statement must be from this database connection!");
1978 // Now append it to our array.
1979 stmts.AppendElement(data);
1982 // Dispatch to the background
1983 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
1984 aCallback, _handle);
1987 NS_IMETHODIMP
1988 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
1989 mozIStorageStatementCallback* aCallback,
1990 mozIStoragePendingStatement** _handle) {
1991 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1993 nsCOMPtr<mozIStorageAsyncStatement> stmt;
1994 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
1995 if (NS_FAILED(rv)) {
1996 return rv;
1999 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
2000 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
2001 if (NS_FAILED(rv)) {
2002 return rv;
2005 pendingStatement.forget(_handle);
2006 return rv;
2009 NS_IMETHODIMP
2010 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
2011 return databaseElementExists(TABLE, aTableName, _exists);
2014 NS_IMETHODIMP
2015 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2016 return databaseElementExists(INDEX, aIndexName, _exists);
2019 NS_IMETHODIMP
2020 Connection::GetTransactionInProgress(bool* _inProgress) {
2021 if (!connectionReady()) {
2022 return NS_ERROR_NOT_INITIALIZED;
2024 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2025 if (NS_FAILED(rv)) {
2026 return rv;
2029 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2030 *_inProgress = transactionInProgress(lockedScope);
2031 return NS_OK;
2034 NS_IMETHODIMP
2035 Connection::GetDefaultTransactionType(int32_t* _type) {
2036 *_type = mDefaultTransactionType;
2037 return NS_OK;
2040 NS_IMETHODIMP
2041 Connection::SetDefaultTransactionType(int32_t aType) {
2042 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2043 mDefaultTransactionType = aType;
2044 return NS_OK;
2047 NS_IMETHODIMP
2048 Connection::GetVariableLimit(int32_t* _limit) {
2049 if (!connectionReady()) {
2050 return NS_ERROR_NOT_INITIALIZED;
2052 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2053 if (limit < 0) {
2054 return NS_ERROR_UNEXPECTED;
2056 *_limit = limit;
2057 return NS_OK;
2060 NS_IMETHODIMP
2061 Connection::BeginTransaction() {
2062 if (!connectionReady()) {
2063 return NS_ERROR_NOT_INITIALIZED;
2065 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2066 if (NS_FAILED(rv)) {
2067 return rv;
2070 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2071 return beginTransactionInternal(lockedScope, mDBConn,
2072 mDefaultTransactionType);
2075 nsresult Connection::beginTransactionInternal(
2076 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2077 int32_t aTransactionType) {
2078 if (transactionInProgress(aProofOfLock)) {
2079 return NS_ERROR_FAILURE;
2081 nsresult rv;
2082 switch (aTransactionType) {
2083 case TRANSACTION_DEFERRED:
2084 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2085 break;
2086 case TRANSACTION_IMMEDIATE:
2087 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2088 break;
2089 case TRANSACTION_EXCLUSIVE:
2090 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2091 break;
2092 default:
2093 return NS_ERROR_ILLEGAL_VALUE;
2095 return rv;
2098 NS_IMETHODIMP
2099 Connection::CommitTransaction() {
2100 if (!connectionReady()) {
2101 return NS_ERROR_NOT_INITIALIZED;
2103 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2104 if (NS_FAILED(rv)) {
2105 return rv;
2108 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2109 return commitTransactionInternal(lockedScope, mDBConn);
2112 nsresult Connection::commitTransactionInternal(
2113 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2114 if (!transactionInProgress(aProofOfLock)) {
2115 return NS_ERROR_UNEXPECTED;
2117 nsresult rv =
2118 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2119 return rv;
2122 NS_IMETHODIMP
2123 Connection::RollbackTransaction() {
2124 if (!connectionReady()) {
2125 return NS_ERROR_NOT_INITIALIZED;
2127 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2128 if (NS_FAILED(rv)) {
2129 return rv;
2132 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2133 return rollbackTransactionInternal(lockedScope, mDBConn);
2136 nsresult Connection::rollbackTransactionInternal(
2137 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2138 if (!transactionInProgress(aProofOfLock)) {
2139 return NS_ERROR_UNEXPECTED;
2142 nsresult rv =
2143 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2144 return rv;
2147 NS_IMETHODIMP
2148 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2149 if (!connectionReady()) {
2150 return NS_ERROR_NOT_INITIALIZED;
2152 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2153 if (NS_FAILED(rv)) {
2154 return rv;
2157 SmprintfPointer buf =
2158 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2159 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2161 int srv = executeSql(mDBConn, buf.get());
2163 return convertResultCode(srv);
2166 NS_IMETHODIMP
2167 Connection::CreateFunction(const nsACString& aFunctionName,
2168 int32_t aNumArguments,
2169 mozIStorageFunction* aFunction) {
2170 if (!connectionReady()) {
2171 return NS_ERROR_NOT_INITIALIZED;
2173 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2174 if (NS_FAILED(rv)) {
2175 return rv;
2178 // Check to see if this function is already defined. We only check the name
2179 // because a function can be defined with the same body but different names.
2180 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2181 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2183 int srv = ::sqlite3_create_function(
2184 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2185 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2186 if (srv != SQLITE_OK) return convertResultCode(srv);
2188 FunctionInfo info = {aFunction, aNumArguments};
2189 mFunctions.InsertOrUpdate(aFunctionName, info);
2191 return NS_OK;
2194 NS_IMETHODIMP
2195 Connection::RemoveFunction(const nsACString& aFunctionName) {
2196 if (!connectionReady()) {
2197 return NS_ERROR_NOT_INITIALIZED;
2199 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2200 if (NS_FAILED(rv)) {
2201 return rv;
2204 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2205 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2207 int srv = ::sqlite3_create_function(
2208 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2209 nullptr, nullptr, nullptr, nullptr);
2210 if (srv != SQLITE_OK) return convertResultCode(srv);
2212 mFunctions.Remove(aFunctionName);
2214 return NS_OK;
2217 NS_IMETHODIMP
2218 Connection::SetProgressHandler(int32_t aGranularity,
2219 mozIStorageProgressHandler* aHandler,
2220 mozIStorageProgressHandler** _oldHandler) {
2221 if (!connectionReady()) {
2222 return NS_ERROR_NOT_INITIALIZED;
2224 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2225 if (NS_FAILED(rv)) {
2226 return rv;
2229 // Return previous one
2230 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2231 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2233 if (!aHandler || aGranularity <= 0) {
2234 aHandler = nullptr;
2235 aGranularity = 0;
2237 mProgressHandler = aHandler;
2238 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2240 return NS_OK;
2243 NS_IMETHODIMP
2244 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2245 if (!connectionReady()) {
2246 return NS_ERROR_NOT_INITIALIZED;
2248 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2249 if (NS_FAILED(rv)) {
2250 return rv;
2253 // Return previous one
2254 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2255 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2257 mProgressHandler = nullptr;
2258 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2260 return NS_OK;
2263 NS_IMETHODIMP
2264 Connection::SetGrowthIncrement(int32_t aChunkSize,
2265 const nsACString& aDatabaseName) {
2266 if (!connectionReady()) {
2267 return NS_ERROR_NOT_INITIALIZED;
2269 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2270 if (NS_FAILED(rv)) {
2271 return rv;
2274 // Bug 597215: Disk space is extremely limited on Android
2275 // so don't preallocate space. This is also not effective
2276 // on log structured file systems used by Android devices
2277 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2278 // Don't preallocate if less than 500MiB is available.
2279 int64_t bytesAvailable;
2280 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2281 NS_ENSURE_SUCCESS(rv, rv);
2282 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2283 return NS_ERROR_FILE_TOO_BIG;
2286 (void)::sqlite3_file_control(mDBConn,
2287 aDatabaseName.Length()
2288 ? nsPromiseFlatCString(aDatabaseName).get()
2289 : nullptr,
2290 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2291 #endif
2292 return NS_OK;
2295 NS_IMETHODIMP
2296 Connection::EnableModule(const nsACString& aModuleName) {
2297 if (!connectionReady()) {
2298 return NS_ERROR_NOT_INITIALIZED;
2300 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2301 if (NS_FAILED(rv)) {
2302 return rv;
2305 for (auto& gModule : gModules) {
2306 struct Module* m = &gModule;
2307 if (aModuleName.Equals(m->name)) {
2308 int srv = m->registerFunc(mDBConn, m->name);
2309 if (srv != SQLITE_OK) return convertResultCode(srv);
2311 return NS_OK;
2315 return NS_ERROR_FAILURE;
2318 // Implemented in TelemetryVFS.cpp
2319 already_AddRefed<QuotaObject> GetQuotaObjectForFile(sqlite3_file* pFile);
2321 NS_IMETHODIMP
2322 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2323 QuotaObject** aJournalQuotaObject) {
2324 MOZ_ASSERT(aDatabaseQuotaObject);
2325 MOZ_ASSERT(aJournalQuotaObject);
2327 if (!connectionReady()) {
2328 return NS_ERROR_NOT_INITIALIZED;
2330 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2331 if (NS_FAILED(rv)) {
2332 return rv;
2335 sqlite3_file* file;
2336 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2337 &file);
2338 if (srv != SQLITE_OK) {
2339 return convertResultCode(srv);
2342 RefPtr<QuotaObject> databaseQuotaObject = GetQuotaObjectForFile(file);
2343 if (NS_WARN_IF(!databaseQuotaObject)) {
2344 return NS_ERROR_FAILURE;
2347 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2348 &file);
2349 if (srv != SQLITE_OK) {
2350 return convertResultCode(srv);
2353 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObjectForFile(file);
2354 if (NS_WARN_IF(!journalQuotaObject)) {
2355 return NS_ERROR_FAILURE;
2358 databaseQuotaObject.forget(aDatabaseQuotaObject);
2359 journalQuotaObject.forget(aJournalQuotaObject);
2360 return NS_OK;
2363 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2365 uint32_t Connection::GetTransactionNestingLevel(
2366 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2367 return mTransactionNestingLevel;
2370 uint32_t Connection::IncreaseTransactionNestingLevel(
2371 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2372 return ++mTransactionNestingLevel;
2375 uint32_t Connection::DecreaseTransactionNestingLevel(
2376 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2377 return --mTransactionNestingLevel;
2380 } // namespace mozilla::storage