Bug 1766606 [wpt PR 33809] - Update wpt metadata, a=testonly
[gecko.git] / storage / mozStorageConnection.cpp
blob27872b958843987eebe2b802c86280f0b2068080
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 <stdio.h>
9 #include "nsError.h"
10 #include "nsThreadUtils.h"
11 #include "nsIFile.h"
12 #include "nsIFileURL.h"
13 #include "nsIXPConnect.h"
14 #include "mozilla/Telemetry.h"
15 #include "mozilla/Mutex.h"
16 #include "mozilla/CondVar.h"
17 #include "mozilla/Attributes.h"
18 #include "mozilla/ErrorNames.h"
19 #include "mozilla/Unused.h"
20 #include "mozilla/dom/quota/QuotaObject.h"
21 #include "mozilla/ScopeExit.h"
22 #include "mozilla/SpinEventLoopUntil.h"
23 #include "mozilla/StaticPrefs_storage.h"
25 #include "mozIStorageCompletionCallback.h"
26 #include "mozIStorageFunction.h"
28 #include "mozStorageAsyncStatementExecution.h"
29 #include "mozStorageSQLFunctions.h"
30 #include "mozStorageConnection.h"
31 #include "mozStorageService.h"
32 #include "mozStorageStatement.h"
33 #include "mozStorageAsyncStatement.h"
34 #include "mozStorageArgValueArray.h"
35 #include "mozStoragePrivateHelpers.h"
36 #include "mozStorageStatementData.h"
37 #include "StorageBaseStatementInternal.h"
38 #include "SQLCollations.h"
39 #include "FileSystemModule.h"
40 #include "mozStorageHelper.h"
42 #include "mozilla/Logging.h"
43 #include "mozilla/Printf.h"
44 #include "mozilla/ProfilerLabels.h"
45 #include "nsProxyRelease.h"
46 #include "nsURLHelper.h"
48 #include <algorithm>
50 #define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
52 // Maximum size of the pages cache per connection.
53 #define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
55 mozilla::LazyLogModule gStorageLog("mozStorage");
57 // Checks that the protected code is running on the main-thread only if the
58 // connection was also opened on it.
59 #ifdef DEBUG
60 # define CHECK_MAINTHREAD_ABUSE() \
61 do { \
62 nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); \
63 NS_WARNING_ASSERTION( \
64 threadOpenedOn == mainThread || !NS_IsMainThread(), \
65 "Using Storage synchronous API on main-thread, but " \
66 "the connection was " \
67 "opened on another thread."); \
68 } while (0)
69 #else
70 # define CHECK_MAINTHREAD_ABUSE() \
71 do { /* Nothing */ \
72 } while (0)
73 #endif
75 namespace mozilla::storage {
77 using mozilla::dom::quota::QuotaObject;
78 using mozilla::Telemetry::AccumulateCategoricalKeyed;
79 using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN;
80 using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY;
82 const char* GetTelemetryVFSName(bool);
83 const char* GetObfuscatingVFSName();
85 namespace {
87 int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
88 if (NS_SUCCEEDED(aXPCOMResultCode)) {
89 return SQLITE_OK;
92 switch (aXPCOMResultCode) {
93 case NS_ERROR_FILE_CORRUPTED:
94 return SQLITE_CORRUPT;
95 case NS_ERROR_FILE_ACCESS_DENIED:
96 return SQLITE_CANTOPEN;
97 case NS_ERROR_STORAGE_BUSY:
98 return SQLITE_BUSY;
99 case NS_ERROR_FILE_IS_LOCKED:
100 return SQLITE_LOCKED;
101 case NS_ERROR_FILE_READ_ONLY:
102 return SQLITE_READONLY;
103 case NS_ERROR_STORAGE_IOERR:
104 return SQLITE_IOERR;
105 case NS_ERROR_FILE_NO_DEVICE_SPACE:
106 return SQLITE_FULL;
107 case NS_ERROR_OUT_OF_MEMORY:
108 return SQLITE_NOMEM;
109 case NS_ERROR_UNEXPECTED:
110 return SQLITE_MISUSE;
111 case NS_ERROR_ABORT:
112 return SQLITE_ABORT;
113 case NS_ERROR_STORAGE_CONSTRAINT:
114 return SQLITE_CONSTRAINT;
115 default:
116 return SQLITE_ERROR;
119 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!");
122 ////////////////////////////////////////////////////////////////////////////////
123 //// Variant Specialization Functions (variantToSQLiteT)
125 int sqlite3_T_int(sqlite3_context* aCtx, int aValue) {
126 ::sqlite3_result_int(aCtx, aValue);
127 return SQLITE_OK;
130 int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
131 ::sqlite3_result_int64(aCtx, aValue);
132 return SQLITE_OK;
135 int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
136 ::sqlite3_result_double(aCtx, aValue);
137 return SQLITE_OK;
140 int sqlite3_T_text(sqlite3_context* aCtx, const nsCString& aValue) {
141 ::sqlite3_result_text(aCtx, aValue.get(), aValue.Length(), SQLITE_TRANSIENT);
142 return SQLITE_OK;
145 int sqlite3_T_text16(sqlite3_context* aCtx, const nsString& aValue) {
146 ::sqlite3_result_text16(
147 aCtx, aValue.get(),
148 aValue.Length() * sizeof(char16_t), // Number of bytes.
149 SQLITE_TRANSIENT);
150 return SQLITE_OK;
153 int sqlite3_T_null(sqlite3_context* aCtx) {
154 ::sqlite3_result_null(aCtx);
155 return SQLITE_OK;
158 int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
159 ::sqlite3_result_blob(aCtx, aData, aSize, free);
160 return SQLITE_OK;
163 #include "variantToSQLiteT_impl.h"
165 ////////////////////////////////////////////////////////////////////////////////
166 //// Modules
168 struct Module {
169 const char* name;
170 int (*registerFunc)(sqlite3*, const char*);
173 Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
175 ////////////////////////////////////////////////////////////////////////////////
176 //// Local Functions
178 int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
179 switch (aReason) {
180 case SQLITE_TRACE_STMT: {
181 // aP is a pointer to the prepared statement.
182 sqlite3_stmt* stmt = static_cast<sqlite3_stmt*>(aP);
183 // aX is a pointer to a string containing the unexpanded SQL or a comment,
184 // starting with "--"" in case of a trigger.
185 char* expanded = static_cast<char*>(aX);
186 // Simulate what sqlite_trace was doing.
187 if (!::strncmp(expanded, "--", 2)) {
188 MOZ_LOG(gStorageLog, LogLevel::Debug,
189 ("TRACE_STMT on %p: '%s'", aClosure, expanded));
190 } else {
191 char* sql = ::sqlite3_expanded_sql(stmt);
192 MOZ_LOG(gStorageLog, LogLevel::Debug,
193 ("TRACE_STMT on %p: '%s'", aClosure, sql));
194 ::sqlite3_free(sql);
196 break;
198 case SQLITE_TRACE_PROFILE: {
199 // aX is pointer to a 64bit integer containing nanoseconds it took to
200 // execute the last command.
201 sqlite_int64 time = *(static_cast<sqlite_int64*>(aX)) / 1000000;
202 if (time > 0) {
203 MOZ_LOG(gStorageLog, LogLevel::Debug,
204 ("TRACE_TIME on %p: %lldms", aClosure, time));
206 break;
209 return 0;
212 void basicFunctionHelper(sqlite3_context* aCtx, int aArgc,
213 sqlite3_value** aArgv) {
214 void* userData = ::sqlite3_user_data(aCtx);
216 mozIStorageFunction* func = static_cast<mozIStorageFunction*>(userData);
218 RefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
219 if (!arguments) return;
221 nsCOMPtr<nsIVariant> result;
222 nsresult rv = func->OnFunctionCall(arguments, getter_AddRefs(result));
223 if (NS_FAILED(rv)) {
224 nsAutoCString errorMessage;
225 GetErrorName(rv, errorMessage);
226 errorMessage.InsertLiteral("User function returned ", 0);
227 errorMessage.Append('!');
229 NS_WARNING(errorMessage.get());
231 ::sqlite3_result_error(aCtx, errorMessage.get(), -1);
232 ::sqlite3_result_error_code(aCtx, nsresultToSQLiteResult(rv));
233 return;
235 int retcode = variantToSQLiteT(aCtx, result);
236 if (retcode != SQLITE_OK) {
237 NS_WARNING("User function returned invalid data type!");
238 ::sqlite3_result_error(aCtx, "User function returned invalid data type",
239 -1);
244 * This code is heavily based on the sample at:
245 * http://www.sqlite.org/unlock_notify.html
247 class UnlockNotification {
248 public:
249 UnlockNotification()
250 : mMutex("UnlockNotification mMutex"),
251 mCondVar(mMutex, "UnlockNotification condVar"),
252 mSignaled(false) {}
254 void Wait() {
255 MutexAutoLock lock(mMutex);
256 while (!mSignaled) {
257 (void)mCondVar.Wait();
261 void Signal() {
262 MutexAutoLock lock(mMutex);
263 mSignaled = true;
264 (void)mCondVar.Notify();
267 private:
268 Mutex mMutex MOZ_UNANNOTATED;
269 CondVar mCondVar;
270 bool mSignaled;
273 void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
274 for (int i = 0; i < aArgsSize; i++) {
275 UnlockNotification* notification =
276 static_cast<UnlockNotification*>(aArgs[i]);
277 notification->Signal();
281 int WaitForUnlockNotify(sqlite3* aDatabase) {
282 UnlockNotification notification;
283 int srv =
284 ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, &notification);
285 MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
286 if (srv == SQLITE_OK) {
287 notification.Wait();
290 return srv;
293 ////////////////////////////////////////////////////////////////////////////////
294 //// Local Classes
296 class AsyncCloseConnection final : public Runnable {
297 public:
298 AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
299 nsIRunnable* aCallbackEvent)
300 : Runnable("storage::AsyncCloseConnection"),
301 mConnection(aConnection),
302 mNativeConnection(aNativeConnection),
303 mCallbackEvent(aCallbackEvent) {}
305 NS_IMETHOD Run() override {
306 // This code is executed on the background thread
307 MOZ_ASSERT(NS_GetCurrentThread() != mConnection->threadOpenedOn);
309 nsCOMPtr<nsIRunnable> event =
310 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
311 mConnection, &Connection::shutdownAsyncThread);
312 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event));
314 // Internal close.
315 (void)mConnection->internalClose(mNativeConnection);
317 // Callback
318 if (mCallbackEvent) {
319 nsCOMPtr<nsIThread> thread;
320 (void)NS_GetMainThread(getter_AddRefs(thread));
321 (void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
324 return NS_OK;
327 ~AsyncCloseConnection() override {
328 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
329 mConnection.forget());
330 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
331 mCallbackEvent.forget());
334 private:
335 RefPtr<Connection> mConnection;
336 sqlite3* mNativeConnection;
337 nsCOMPtr<nsIRunnable> mCallbackEvent;
341 * An event used to initialize the clone of a connection.
343 * Must be executed on the clone's async execution thread.
345 class AsyncInitializeClone final : public Runnable {
346 public:
348 * @param aConnection The connection being cloned.
349 * @param aClone The clone.
350 * @param aReadOnly If |true|, the clone is read only.
351 * @param aCallback A callback to trigger once initialization
352 * is complete. This event will be called on
353 * aClone->threadOpenedOn.
355 AsyncInitializeClone(Connection* aConnection, Connection* aClone,
356 const bool aReadOnly,
357 mozIStorageCompletionCallback* aCallback)
358 : Runnable("storage::AsyncInitializeClone"),
359 mConnection(aConnection),
360 mClone(aClone),
361 mReadOnly(aReadOnly),
362 mCallback(aCallback) {
363 MOZ_ASSERT(NS_IsMainThread());
366 NS_IMETHOD Run() override {
367 MOZ_ASSERT(!NS_IsMainThread());
368 nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
369 if (NS_FAILED(rv)) {
370 return Dispatch(rv, nullptr);
372 return Dispatch(NS_OK,
373 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
376 private:
377 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
378 RefPtr<CallbackComplete> event =
379 new CallbackComplete(aResult, aValue, mCallback.forget());
380 return mClone->threadOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
383 ~AsyncInitializeClone() override {
384 nsCOMPtr<nsIThread> thread;
385 DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
386 MOZ_ASSERT(NS_SUCCEEDED(rv));
388 // Handle ambiguous nsISupports inheritance.
389 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
390 mConnection.forget());
391 NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
393 // Generally, the callback will be released by CallbackComplete.
394 // However, if for some reason Run() is not executed, we still
395 // need to ensure that it is released here.
396 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
397 mCallback.forget());
400 RefPtr<Connection> mConnection;
401 RefPtr<Connection> mClone;
402 const bool mReadOnly;
403 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
407 * A listener for async connection closing.
409 class CloseListener final : public mozIStorageCompletionCallback {
410 public:
411 NS_DECL_ISUPPORTS
412 CloseListener() : mClosed(false) {}
414 NS_IMETHOD Complete(nsresult, nsISupports*) override {
415 mClosed = true;
416 return NS_OK;
419 bool mClosed;
421 private:
422 ~CloseListener() = default;
425 NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)
427 } // namespace
429 ////////////////////////////////////////////////////////////////////////////////
430 //// Connection
432 Connection::Connection(Service* aService, int aFlags,
433 ConnectionOperation aSupportedOperations,
434 bool aInterruptible, bool aIgnoreLockingMode)
435 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
436 sharedDBMutex("Connection::sharedDBMutex"),
437 threadOpenedOn(do_GetCurrentThread()),
438 mDBConn(nullptr),
439 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
440 mDestroying(false),
441 mProgressHandler(nullptr),
442 mStorageService(aService),
443 mFlags(aFlags),
444 mTransactionNestingLevel(0),
445 mSupportedOperations(aSupportedOperations),
446 mInterruptible(aSupportedOperations == Connection::ASYNCHRONOUS ||
447 aInterruptible),
448 mIgnoreLockingMode(aIgnoreLockingMode),
449 mAsyncExecutionThreadShuttingDown(false),
450 mConnectionClosed(false) {
451 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
452 "Can't ignore locking for a non-readonly connection!");
453 mStorageService->registerConnection(this);
456 Connection::~Connection() {
457 // Failsafe Close() occurs in our custom Release method because of
458 // complications related to Close() potentially invoking AsyncClose() which
459 // will increment our refcount.
460 MOZ_ASSERT(!mAsyncExecutionThread,
461 "The async thread has not been shutdown properly!");
464 NS_IMPL_ADDREF(Connection)
466 NS_INTERFACE_MAP_BEGIN(Connection)
467 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
468 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
469 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
470 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
471 NS_INTERFACE_MAP_END
473 // This is identical to what NS_IMPL_RELEASE provides, but with the
474 // extra |1 == count| case.
475 NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
476 MOZ_ASSERT(0 != mRefCnt, "dup release");
477 nsrefcnt count = --mRefCnt;
478 NS_LOG_RELEASE(this, count, "Connection");
479 if (1 == count) {
480 // If the refcount went to 1, the single reference must be from
481 // gService->mConnections (in class |Service|). And the code calling
482 // Release is either:
483 // - The "user" code that had created the connection, releasing on any
484 // thread.
485 // - One of Service's getConnections() callers had acquired a strong
486 // reference to the Connection that out-lived the last "user" reference,
487 // and now that just got dropped. Note that this reference could be
488 // getting dropped on the main thread or Connection->threadOpenedOn
489 // (because of the NewRunnableMethod used by minimizeMemory).
491 // Either way, we should now perform our failsafe Close() and unregister.
492 // However, we only want to do this once, and the reality is that our
493 // refcount could go back up above 1 and down again at any time if we are
494 // off the main thread and getConnections() gets called on the main thread,
495 // so we use an atomic here to do this exactly once.
496 if (mDestroying.compareExchange(false, true)) {
497 // Close the connection, dispatching to the opening thread if we're not
498 // on that thread already and that thread is still accepting runnables.
499 // We do this because it's possible we're on the main thread because of
500 // getConnections(), and we REALLY don't want to transfer I/O to the main
501 // thread if we can avoid it.
502 if (threadOpenedOn->IsOnCurrentThread()) {
503 // This could cause SpinningSynchronousClose() to be invoked and AddRef
504 // triggered for AsyncCloseConnection's strong ref if the conn was ever
505 // use for async purposes. (Main-thread only, though.)
506 Unused << synchronousClose();
507 } else {
508 nsCOMPtr<nsIRunnable> event =
509 NewRunnableMethod("storage::Connection::synchronousClose", this,
510 &Connection::synchronousClose);
511 if (NS_FAILED(
512 threadOpenedOn->Dispatch(event.forget(), NS_DISPATCH_NORMAL))) {
513 // The target thread was dead and so we've just leaked our runnable.
514 // This should not happen because our non-main-thread consumers should
515 // be explicitly closing their connections, not relying on us to close
516 // them for them. (It's okay to let a statement go out of scope for
517 // automatic cleanup, but not a Connection.)
518 MOZ_ASSERT(false,
519 "Leaked Connection::synchronousClose(), ownership fail.");
520 Unused << synchronousClose();
524 // This will drop its strong reference right here, right now.
525 mStorageService->unregisterConnection(this);
527 } else if (0 == count) {
528 mRefCnt = 1; /* stabilize */
529 #if 0 /* enable this to find non-threadsafe destructors: */
530 NS_ASSERT_OWNINGTHREAD(Connection);
531 #endif
532 delete (this);
533 return 0;
535 return count;
538 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
539 int32_t* aMaxValue) {
540 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
541 int curr = 0, max = 0;
542 DebugOnly<int> rc =
543 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
544 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
545 if (aMaxValue) *aMaxValue = max;
546 return curr;
549 nsIEventTarget* Connection::getAsyncExecutionTarget() {
550 NS_ENSURE_TRUE(threadOpenedOn == NS_GetCurrentThread(), nullptr);
552 // Don't return the asynchronous thread if we are shutting down.
553 if (mAsyncExecutionThreadShuttingDown) {
554 return nullptr;
557 // Create the async thread if there's none yet.
558 if (!mAsyncExecutionThread) {
559 static nsThreadPoolNaming naming;
560 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName("mozStorage"),
561 getter_AddRefs(mAsyncExecutionThread));
562 if (NS_FAILED(rv)) {
563 NS_WARNING("Failed to create async thread.");
564 return nullptr;
566 mAsyncExecutionThread->SetNameForWakeupTelemetry("mozStorage (all)"_ns);
569 return mAsyncExecutionThread;
572 void Connection::RecordOpenStatus(nsresult rv) {
573 nsCString histogramKey = mTelemetryFilename;
575 if (histogramKey.IsEmpty()) {
576 histogramKey.AssignLiteral("unknown");
579 if (NS_SUCCEEDED(rv)) {
580 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
581 return;
584 switch (rv) {
585 case NS_ERROR_FILE_CORRUPTED:
586 AccumulateCategoricalKeyed(histogramKey,
587 LABELS_SQLITE_STORE_OPEN::corrupt);
588 break;
589 case NS_ERROR_STORAGE_IOERR:
590 AccumulateCategoricalKeyed(histogramKey,
591 LABELS_SQLITE_STORE_OPEN::diskio);
592 break;
593 case NS_ERROR_FILE_ACCESS_DENIED:
594 case NS_ERROR_FILE_IS_LOCKED:
595 case NS_ERROR_FILE_READ_ONLY:
596 AccumulateCategoricalKeyed(histogramKey,
597 LABELS_SQLITE_STORE_OPEN::access);
598 break;
599 case NS_ERROR_FILE_NO_DEVICE_SPACE:
600 AccumulateCategoricalKeyed(histogramKey,
601 LABELS_SQLITE_STORE_OPEN::diskspace);
602 break;
603 default:
604 AccumulateCategoricalKeyed(histogramKey,
605 LABELS_SQLITE_STORE_OPEN::failure);
609 void Connection::RecordQueryStatus(int srv) {
610 nsCString histogramKey = mTelemetryFilename;
612 if (histogramKey.IsEmpty()) {
613 histogramKey.AssignLiteral("unknown");
616 switch (srv) {
617 case SQLITE_OK:
618 case SQLITE_ROW:
619 case SQLITE_DONE:
621 // Note that these are returned when we intentionally cancel a statement so
622 // they aren't indicating a failure.
623 case SQLITE_ABORT:
624 case SQLITE_INTERRUPT:
625 AccumulateCategoricalKeyed(histogramKey,
626 LABELS_SQLITE_STORE_QUERY::success);
627 break;
628 case SQLITE_CORRUPT:
629 case SQLITE_NOTADB:
630 AccumulateCategoricalKeyed(histogramKey,
631 LABELS_SQLITE_STORE_QUERY::corrupt);
632 break;
633 case SQLITE_PERM:
634 case SQLITE_CANTOPEN:
635 case SQLITE_LOCKED:
636 case SQLITE_READONLY:
637 AccumulateCategoricalKeyed(histogramKey,
638 LABELS_SQLITE_STORE_QUERY::access);
639 break;
640 case SQLITE_IOERR:
641 case SQLITE_NOLFS:
642 AccumulateCategoricalKeyed(histogramKey,
643 LABELS_SQLITE_STORE_QUERY::diskio);
644 break;
645 case SQLITE_FULL:
646 case SQLITE_TOOBIG:
647 AccumulateCategoricalKeyed(histogramKey,
648 LABELS_SQLITE_STORE_OPEN::diskspace);
649 break;
650 case SQLITE_CONSTRAINT:
651 case SQLITE_RANGE:
652 case SQLITE_MISMATCH:
653 case SQLITE_MISUSE:
654 AccumulateCategoricalKeyed(histogramKey,
655 LABELS_SQLITE_STORE_OPEN::misuse);
656 break;
657 case SQLITE_BUSY:
658 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::busy);
659 break;
660 default:
661 AccumulateCategoricalKeyed(histogramKey,
662 LABELS_SQLITE_STORE_QUERY::failure);
666 nsresult Connection::initialize(const nsACString& aStorageKey,
667 const nsACString& aName) {
668 MOZ_ASSERT(aStorageKey.Equals(kMozStorageMemoryStorageKey));
669 NS_ASSERTION(!connectionReady(),
670 "Initialize called on already opened database!");
671 MOZ_ASSERT(!mIgnoreLockingMode, "Can't ignore locking on an in-memory db.");
672 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
674 mStorageKey = aStorageKey;
675 mName = aName;
677 // in memory database requested, sqlite uses a magic file name
679 const nsAutoCString path =
680 mName.IsEmpty() ? nsAutoCString(":memory:"_ns)
681 : "file:"_ns + mName + "?mode=memory&cache=shared"_ns;
683 mTelemetryFilename.AssignLiteral(":memory:");
685 int srv = ::sqlite3_open_v2(path.get(), &mDBConn, mFlags,
686 GetTelemetryVFSName(true));
687 if (srv != SQLITE_OK) {
688 mDBConn = nullptr;
689 nsresult rv = convertResultCode(srv);
690 RecordOpenStatus(rv);
691 return rv;
694 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
695 srv =
696 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
697 MOZ_ASSERT(srv == SQLITE_OK,
698 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
699 #endif
701 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
702 // database.
704 nsresult rv = initializeInternal();
705 RecordOpenStatus(rv);
706 NS_ENSURE_SUCCESS(rv, rv);
708 return NS_OK;
711 nsresult Connection::initialize(nsIFile* aDatabaseFile) {
712 NS_ASSERTION(aDatabaseFile, "Passed null file!");
713 NS_ASSERTION(!connectionReady(),
714 "Initialize called on already opened database!");
715 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
717 // Do not set mFileURL here since this is database does not have an associated
718 // URL.
719 mDatabaseFile = aDatabaseFile;
720 aDatabaseFile->GetNativeLeafName(mTelemetryFilename);
722 nsAutoString path;
723 nsresult rv = aDatabaseFile->GetPath(path);
724 NS_ENSURE_SUCCESS(rv, rv);
726 #ifdef XP_WIN
727 static const char* sIgnoreLockingVFS = "win32-none";
728 #else
729 static const char* sIgnoreLockingVFS = "unix-none";
730 #endif
732 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
733 int srv;
734 if (mIgnoreLockingMode) {
735 exclusive = false;
736 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
737 sIgnoreLockingVFS);
738 } else {
739 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
740 GetTelemetryVFSName(exclusive));
741 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
742 // Retry without trying to get an exclusive lock.
743 exclusive = false;
744 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
745 mFlags, GetTelemetryVFSName(false));
748 if (srv != SQLITE_OK) {
749 mDBConn = nullptr;
750 rv = convertResultCode(srv);
751 RecordOpenStatus(rv);
752 return rv;
755 rv = initializeInternal();
756 if (exclusive &&
757 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
758 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
759 // some cases it may successfully open the database and then lock on the
760 // first query execution. When initializeInternal fails it closes the
761 // connection, so we can try to restart it in non-exclusive mode.
762 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
763 GetTelemetryVFSName(false));
764 if (srv == SQLITE_OK) {
765 rv = initializeInternal();
769 RecordOpenStatus(rv);
770 NS_ENSURE_SUCCESS(rv, rv);
772 return NS_OK;
775 nsresult Connection::initialize(nsIFileURL* aFileURL,
776 const nsACString& aTelemetryFilename) {
777 NS_ASSERTION(aFileURL, "Passed null file URL!");
778 NS_ASSERTION(!connectionReady(),
779 "Initialize called on already opened database!");
780 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
782 nsCOMPtr<nsIFile> databaseFile;
783 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
784 NS_ENSURE_SUCCESS(rv, rv);
786 // Set both mDatabaseFile and mFileURL here.
787 mFileURL = aFileURL;
788 mDatabaseFile = databaseFile;
790 if (!aTelemetryFilename.IsEmpty()) {
791 mTelemetryFilename = aTelemetryFilename;
792 } else {
793 databaseFile->GetNativeLeafName(mTelemetryFilename);
796 nsAutoCString spec;
797 rv = aFileURL->GetSpec(spec);
798 NS_ENSURE_SUCCESS(rv, rv);
800 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
802 // If there is a key specified, we need to use the obfuscating VFS.
803 nsAutoCString query;
804 rv = aFileURL->GetQuery(query);
805 NS_ENSURE_SUCCESS(rv, rv);
806 const char* const vfs =
807 URLParams::Parse(query,
808 [](const nsAString& aName, const nsAString& aValue) {
809 return aName.EqualsLiteral("key");
811 ? GetObfuscatingVFSName()
812 : GetTelemetryVFSName(exclusive);
813 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
814 if (srv != SQLITE_OK) {
815 mDBConn = nullptr;
816 rv = convertResultCode(srv);
817 RecordOpenStatus(rv);
818 return rv;
821 rv = initializeInternal();
822 RecordOpenStatus(rv);
823 NS_ENSURE_SUCCESS(rv, rv);
825 return NS_OK;
828 nsresult Connection::initializeInternal() {
829 MOZ_ASSERT(mDBConn);
830 auto guard = MakeScopeExit([&]() { initializeFailed(); });
832 mConnectionClosed = false;
834 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
835 DebugOnly<int> srv2 =
836 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
837 MOZ_ASSERT(srv2 == SQLITE_OK,
838 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
839 #endif
841 MOZ_ASSERT(!mTelemetryFilename.IsEmpty(),
842 "A telemetry filename should have been set by now.");
844 // Properly wrap the database handle's mutex.
845 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
847 // SQLite tracing can slow down queries (especially long queries)
848 // significantly. Don't trace unless the user is actively monitoring SQLite.
849 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
850 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
851 tracefunc, this);
853 MOZ_LOG(
854 gStorageLog, LogLevel::Debug,
855 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
858 int64_t pageSize = Service::kDefaultPageSize;
860 // Set page_size to the preferred default value. This is effective only if
861 // the database has just been created, otherwise, if the database does not
862 // use WAL journal mode, a VACUUM operation will updated its page_size.
863 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
864 "PRAGMA page_size = ");
865 pageSizeQuery.AppendInt(pageSize);
866 int srv = executeSql(mDBConn, pageSizeQuery.get());
867 if (srv != SQLITE_OK) {
868 return convertResultCode(srv);
871 // Setting the cache_size forces the database open, verifying if it is valid
872 // or corrupt. So this is executed regardless it being actually needed.
873 // The cache_size is calculated from the actual page_size, to save memory.
874 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
875 "PRAGMA cache_size = ");
876 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
877 srv = executeSql(mDBConn, cacheSizeQuery.get());
878 if (srv != SQLITE_OK) {
879 return convertResultCode(srv);
882 // Register our built-in SQL functions.
883 srv = registerFunctions(mDBConn);
884 if (srv != SQLITE_OK) {
885 return convertResultCode(srv);
888 // Register our built-in SQL collating sequences.
889 srv = registerCollations(mDBConn, mStorageService);
890 if (srv != SQLITE_OK) {
891 return convertResultCode(srv);
894 // Set the default synchronous value. Each consumer can switch this
895 // accordingly to their needs.
896 #if defined(ANDROID)
897 // Android prefers synchronous = OFF for performance reasons.
898 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
899 #else
900 // Normal is the suggested value for WAL journals.
901 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
902 #endif
904 // Initialization succeeded, we can stop guarding for failures.
905 guard.release();
906 return NS_OK;
909 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
910 MOZ_ASSERT(threadOpenedOn != NS_GetCurrentThread());
911 nsresult rv = aStorageFile
912 ? initialize(aStorageFile)
913 : initialize(kMozStorageMemoryStorageKey, VoidCString());
914 if (NS_FAILED(rv)) {
915 // Shutdown the async thread, since initialization failed.
916 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
917 mAsyncExecutionThreadShuttingDown = true;
918 nsCOMPtr<nsIRunnable> event =
919 NewRunnableMethod("Connection::shutdownAsyncThread", this,
920 &Connection::shutdownAsyncThread);
921 Unused << NS_DispatchToMainThread(event);
923 return rv;
926 void Connection::initializeFailed() {
928 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
929 mConnectionClosed = true;
931 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
932 mDBConn = nullptr;
933 sharedDBMutex.destroy();
936 nsresult Connection::databaseElementExists(
937 enum DatabaseElementType aElementType, const nsACString& aElementName,
938 bool* _exists) {
939 if (!connectionReady()) {
940 return NS_ERROR_NOT_AVAILABLE;
942 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
943 if (NS_FAILED(rv)) {
944 return rv;
947 // When constructing the query, make sure to SELECT the correct db's
948 // sqlite_master if the user is prefixing the element with a specific db. ex:
949 // sample.test
950 nsCString query("SELECT name FROM (SELECT * FROM ");
951 nsDependentCSubstring element;
952 int32_t ind = aElementName.FindChar('.');
953 if (ind == kNotFound) {
954 element.Assign(aElementName);
955 } else {
956 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
957 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
958 query.Append(db);
960 query.AppendLiteral(
961 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
962 "'");
964 switch (aElementType) {
965 case INDEX:
966 query.AppendLiteral("index");
967 break;
968 case TABLE:
969 query.AppendLiteral("table");
970 break;
972 query.AppendLiteral("' AND name ='");
973 query.Append(element);
974 query.Append('\'');
976 sqlite3_stmt* stmt;
977 int srv = prepareStatement(mDBConn, query, &stmt);
978 if (srv != SQLITE_OK) {
979 RecordQueryStatus(srv);
980 return convertResultCode(srv);
983 srv = stepStatement(mDBConn, stmt);
984 // we just care about the return value from step
985 (void)::sqlite3_finalize(stmt);
987 RecordQueryStatus(srv);
989 if (srv == SQLITE_ROW) {
990 *_exists = true;
991 return NS_OK;
993 if (srv == SQLITE_DONE) {
994 *_exists = false;
995 return NS_OK;
998 return convertResultCode(srv);
1001 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
1002 sharedDBMutex.assertCurrentThreadOwns();
1004 for (const auto& data : mFunctions.Values()) {
1005 if (data.function == aInstance) {
1006 return true;
1009 return false;
1012 /* static */
1013 int Connection::sProgressHelper(void* aArg) {
1014 Connection* _this = static_cast<Connection*>(aArg);
1015 return _this->progressHandler();
1018 int Connection::progressHandler() {
1019 sharedDBMutex.assertCurrentThreadOwns();
1020 if (mProgressHandler) {
1021 bool result;
1022 nsresult rv = mProgressHandler->OnProgress(this, &result);
1023 if (NS_FAILED(rv)) return 0; // Don't break request
1024 return result ? 1 : 0;
1026 return 0;
1029 nsresult Connection::setClosedState() {
1030 // Ensure that we are on the correct thread to close the database.
1031 bool onOpenedThread;
1032 nsresult rv = threadOpenedOn->IsOnCurrentThread(&onOpenedThread);
1033 NS_ENSURE_SUCCESS(rv, rv);
1034 if (!onOpenedThread) {
1035 NS_ERROR("Must close the database on the thread that you opened it with!");
1036 return NS_ERROR_UNEXPECTED;
1039 // Flag that we are shutting down the async thread, so that
1040 // getAsyncExecutionTarget knows not to expose/create the async thread.
1042 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1043 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1044 mAsyncExecutionThreadShuttingDown = true;
1046 // Set the property to null before closing the connection, otherwise the
1047 // other functions in the module may try to use the connection after it is
1048 // closed.
1049 mDBConn = nullptr;
1051 return NS_OK;
1054 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1055 if (aOperationType == ASYNCHRONOUS) {
1056 // Async operations are supported for all connections, on any thread.
1057 return true;
1059 // Sync operations are supported for sync connections (on any thread), and
1060 // async connections on a background thread.
1061 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1062 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1065 nsresult Connection::ensureOperationSupported(
1066 ConnectionOperation aOperationType) {
1067 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1068 #ifdef DEBUG
1069 if (NS_IsMainThread()) {
1070 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1071 Unused << xpc->DebugDumpJSStack(false, false, false);
1073 #endif
1074 MOZ_ASSERT(false,
1075 "Don't use async connections synchronously on the main thread");
1076 return NS_ERROR_NOT_AVAILABLE;
1078 return NS_OK;
1081 bool Connection::isConnectionReadyOnThisThread() {
1082 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1083 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1084 return true;
1086 return connectionReady();
1089 bool Connection::isClosing() {
1090 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1091 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1094 bool Connection::isClosed() {
1095 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1096 return mConnectionClosed;
1099 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1101 bool Connection::isAsyncExecutionThreadAvailable() {
1102 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1103 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1106 void Connection::shutdownAsyncThread() {
1107 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1108 MOZ_ASSERT(mAsyncExecutionThread);
1109 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1111 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1112 mAsyncExecutionThread = nullptr;
1115 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1116 #ifdef DEBUG
1117 { // Make sure we have marked our async thread as shutting down.
1118 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1119 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1120 "Did not call setClosedState!");
1121 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1123 #endif // DEBUG
1125 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1126 nsAutoCString leafName(":memory");
1127 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1128 MOZ_LOG(gStorageLog, LogLevel::Debug,
1129 ("Closing connection to '%s'", leafName.get()));
1132 // At this stage, we may still have statements that need to be
1133 // finalized. Attempt to close the database connection. This will
1134 // always disconnect any virtual tables and cleanly finalize their
1135 // internal statements. Once this is done, closing may fail due to
1136 // unfinalized client statements, in which case we need to finalize
1137 // these statements and close again.
1139 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1140 mConnectionClosed = true;
1143 // Nothing else needs to be done if we don't have a connection here.
1144 if (!aNativeConnection) return NS_OK;
1146 int srv = ::sqlite3_close(aNativeConnection);
1148 if (srv == SQLITE_BUSY) {
1150 // Nothing else should change the connection or statements status until we
1151 // are done here.
1152 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1153 // We still have non-finalized statements. Finalize them.
1154 sqlite3_stmt* stmt = nullptr;
1155 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1156 MOZ_LOG(gStorageLog, LogLevel::Debug,
1157 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1158 stmt));
1160 #ifdef DEBUG
1161 SmprintfPointer msg = ::mozilla::Smprintf(
1162 "SQL statement '%s' (%p) should have been finalized before closing "
1163 "the connection",
1164 ::sqlite3_sql(stmt), stmt);
1165 NS_WARNING(msg.get());
1166 #endif // DEBUG
1168 srv = ::sqlite3_finalize(stmt);
1170 #ifdef DEBUG
1171 if (srv != SQLITE_OK) {
1172 SmprintfPointer msg = ::mozilla::Smprintf(
1173 "Could not finalize SQL statement (%p)", stmt);
1174 NS_WARNING(msg.get());
1176 #endif // DEBUG
1178 // Ensure that the loop continues properly, whether closing has
1179 // succeeded or not.
1180 if (srv == SQLITE_OK) {
1181 stmt = nullptr;
1184 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1185 // again, since Sqlite will try to acquire it.
1188 // Now that all statements have been finalized, we
1189 // should be able to close.
1190 srv = ::sqlite3_close(aNativeConnection);
1191 MOZ_ASSERT(false,
1192 "Had to forcibly close the database connection because not all "
1193 "the statements have been finalized.");
1196 if (srv == SQLITE_OK) {
1197 sharedDBMutex.destroy();
1198 } else {
1199 MOZ_ASSERT(false,
1200 "sqlite3_close failed. There are probably outstanding "
1201 "statements that are listed above!");
1204 return convertResultCode(srv);
1207 nsCString Connection::getFilename() { return mTelemetryFilename; }
1209 int Connection::stepStatement(sqlite3* aNativeConnection,
1210 sqlite3_stmt* aStatement) {
1211 MOZ_ASSERT(aStatement);
1213 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1214 ::sqlite3_sql(aStatement));
1216 bool checkedMainThread = false;
1217 TimeStamp startTime = TimeStamp::Now();
1219 // The connection may have been closed if the executing statement has been
1220 // created and cached after a call to asyncClose() but before the actual
1221 // sqlite3_close(). This usually happens when other tasks using cached
1222 // statements are asynchronously scheduled for execution and any of them ends
1223 // up after asyncClose. See bug 728653 for details.
1224 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1226 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1228 int srv;
1229 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1230 if (!checkedMainThread) {
1231 checkedMainThread = true;
1232 if (::NS_IsMainThread()) {
1233 NS_WARNING("We won't allow blocking on the main thread!");
1234 break;
1238 srv = WaitForUnlockNotify(aNativeConnection);
1239 if (srv != SQLITE_OK) {
1240 break;
1243 ::sqlite3_reset(aStatement);
1246 // Report very slow SQL statements to Telemetry
1247 TimeDuration duration = TimeStamp::Now() - startTime;
1248 const uint32_t threshold = NS_IsMainThread()
1249 ? Telemetry::kSlowSQLThresholdForMainThread
1250 : Telemetry::kSlowSQLThresholdForHelperThreads;
1251 if (duration.ToMilliseconds() >= threshold) {
1252 nsDependentCString statementString(::sqlite3_sql(aStatement));
1253 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1254 duration.ToMilliseconds());
1257 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1258 // Drop off the extended result bits of the result code.
1259 return srv & 0xFF;
1262 int Connection::prepareStatement(sqlite3* aNativeConnection,
1263 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1264 // We should not even try to prepare statements after the connection has
1265 // been closed.
1266 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1268 bool checkedMainThread = false;
1270 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1272 int srv;
1273 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1274 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1275 if (!checkedMainThread) {
1276 checkedMainThread = true;
1277 if (::NS_IsMainThread()) {
1278 NS_WARNING("We won't allow blocking on the main thread!");
1279 break;
1283 srv = WaitForUnlockNotify(aNativeConnection);
1284 if (srv != SQLITE_OK) {
1285 break;
1289 if (srv != SQLITE_OK) {
1290 nsCString warnMsg;
1291 warnMsg.AppendLiteral("The SQL statement '");
1292 warnMsg.Append(aSQL);
1293 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1294 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1296 #ifdef DEBUG
1297 NS_WARNING(warnMsg.get());
1298 #endif
1299 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1302 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1303 // Drop off the extended result bits of the result code.
1304 int rc = srv & 0xFF;
1305 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1306 // The callers of this function are used to only checking the return value,
1307 // so it is safer to return an error code.
1308 if (rc == SQLITE_OK && *_stmt == nullptr) {
1309 return SQLITE_MISUSE;
1312 return rc;
1315 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1316 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1318 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1320 TimeStamp startTime = TimeStamp::Now();
1321 int srv =
1322 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1323 RecordQueryStatus(srv);
1325 // Report very slow SQL statements to Telemetry
1326 TimeDuration duration = TimeStamp::Now() - startTime;
1327 const uint32_t threshold = NS_IsMainThread()
1328 ? Telemetry::kSlowSQLThresholdForMainThread
1329 : Telemetry::kSlowSQLThresholdForHelperThreads;
1330 if (duration.ToMilliseconds() >= threshold) {
1331 nsDependentCString statementString(aSqlString);
1332 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1333 duration.ToMilliseconds());
1336 return srv;
1339 ////////////////////////////////////////////////////////////////////////////////
1340 //// nsIInterfaceRequestor
1342 NS_IMETHODIMP
1343 Connection::GetInterface(const nsIID& aIID, void** _result) {
1344 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1345 nsIEventTarget* background = getAsyncExecutionTarget();
1346 NS_IF_ADDREF(background);
1347 *_result = background;
1348 return NS_OK;
1350 return NS_ERROR_NO_INTERFACE;
1353 ////////////////////////////////////////////////////////////////////////////////
1354 //// mozIStorageConnection
1356 NS_IMETHODIMP
1357 Connection::Close() {
1358 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1359 if (NS_FAILED(rv)) {
1360 return rv;
1362 return synchronousClose();
1365 nsresult Connection::synchronousClose() {
1366 if (!connectionReady()) {
1367 return NS_ERROR_NOT_INITIALIZED;
1370 #ifdef DEBUG
1371 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1372 // thread. We make this check outside of debug code below in setClosedState,
1373 // but this is here to be explicit.
1374 bool onOpenerThread = false;
1375 (void)threadOpenedOn->IsOnCurrentThread(&onOpenerThread);
1376 MOZ_ASSERT(onOpenerThread);
1377 #endif // DEBUG
1379 // Make sure we have not executed any asynchronous statements.
1380 // If this fails, the mDBConn may be left open, resulting in a leak.
1381 // We'll try to finalize the pending statements and close the connection.
1382 if (isAsyncExecutionThreadAvailable()) {
1383 #ifdef DEBUG
1384 if (NS_IsMainThread()) {
1385 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1386 Unused << xpc->DebugDumpJSStack(false, false, false);
1388 #endif
1389 MOZ_ASSERT(false,
1390 "Close() was invoked on a connection that executed asynchronous "
1391 "statements. "
1392 "Should have used asyncClose().");
1393 // Try to close the database regardless, to free up resources.
1394 Unused << SpinningSynchronousClose();
1395 return NS_ERROR_UNEXPECTED;
1398 // setClosedState nullifies our connection pointer, so we take a raw pointer
1399 // off it, to pass it through the close procedure.
1400 sqlite3* nativeConn = mDBConn;
1401 nsresult rv = setClosedState();
1402 NS_ENSURE_SUCCESS(rv, rv);
1404 return internalClose(nativeConn);
1407 NS_IMETHODIMP
1408 Connection::SpinningSynchronousClose() {
1409 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1410 if (NS_FAILED(rv)) {
1411 return rv;
1413 if (threadOpenedOn != NS_GetCurrentThread()) {
1414 return NS_ERROR_NOT_SAME_THREAD;
1417 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1418 // Our only existing caller will never have called close; assert if misused
1419 // so that no new callers assume this works after an AsyncClose.
1420 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1421 if (!connectionReady()) {
1422 return NS_ERROR_UNEXPECTED;
1425 RefPtr<CloseListener> listener = new CloseListener();
1426 rv = AsyncClose(listener);
1427 NS_ENSURE_SUCCESS(rv, rv);
1428 MOZ_ALWAYS_TRUE(
1429 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,
1430 [&]() { return listener->mClosed; }));
1431 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1433 return rv;
1436 NS_IMETHODIMP
1437 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1438 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1439 // Check if AsyncClose or Close were already invoked.
1440 if (!connectionReady()) {
1441 return NS_ERROR_NOT_INITIALIZED;
1443 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1444 if (NS_FAILED(rv)) {
1445 return rv;
1448 // The two relevant factors at this point are whether we have a database
1449 // connection and whether we have an async execution thread. Here's what the
1450 // states mean and how we handle them:
1452 // - (mDBConn && asyncThread): The expected case where we are either an
1453 // async connection or a sync connection that has been used asynchronously.
1454 // Either way the caller must call us and not Close(). Nothing surprising
1455 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1456 // async thread.
1458 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1459 // opened the connection synchronously and was planning to use it
1460 // asynchronously, but never got around to using it asynchronously before
1461 // needing to shutdown. This has been observed to happen for the cookie
1462 // service in a case where Firefox shuts itself down almost immediately
1463 // after startup (for unknown reasons). In the Firefox shutdown case,
1464 // we may also fail to create a new async execution thread if one does not
1465 // already exist. (nsThreadManager will refuse to create new threads when
1466 // it has already been told to shutdown.) As such, we need to handle a
1467 // failure to create the async execution thread by falling back to
1468 // synchronous Close() and also dispatching the completion callback because
1469 // at least Places likes to spin a nested event loop that depends on the
1470 // callback being invoked.
1472 // Note that we have considered not trying to spin up the async execution
1473 // thread in this case if it does not already exist, but the overhead of
1474 // thread startup (if successful) is significantly less expensive than the
1475 // worst-case potential I/O hit of synchronously closing a database when we
1476 // could close it asynchronously.
1478 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1479 // OpenAsyncDatabase encountered a problem opening the database. If it
1480 // happened in all cases AsyncInitDatabase would just shut down the thread
1481 // directly and we would avoid this case. But it doesn't, so for simplicity
1482 // and consistency AsyncCloseConnection knows how to handle this and we
1483 // act like this was the (mDBConn && asyncThread) case in this method.
1485 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1486 // Close() or AsyncClose() has already been called (at least) once. This is
1487 // undeniably a misuse case by the caller. We could optimize for this
1488 // case by adding an additional check of mAsyncExecutionThread without using
1489 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1490 // shut it down. But this complicates the method for broken caller code
1491 // whereas we're still correct and safe without the special-case.
1492 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1494 // Create our callback event if we were given a callback. This will
1495 // eventually be dispatched in all cases, even if we fall back to Close() and
1496 // the database wasn't open and we return an error. The rationale is that
1497 // no existing consumer checks our return value and several of them like to
1498 // spin nested event loops until the callback fires. Given that, it seems
1499 // preferable for us to dispatch the callback in all cases. (Except the
1500 // wrong thread misuse case we bailed on up above. But that's okay because
1501 // that is statically wrong whereas these edge cases are dynamic.)
1502 nsCOMPtr<nsIRunnable> completeEvent;
1503 if (aCallback) {
1504 completeEvent = newCompletionEvent(aCallback);
1507 if (!asyncThread) {
1508 // We were unable to create an async thread, so we need to fall back to
1509 // using normal Close(). Since there is no async thread, Close() will
1510 // not complain about that. (Close() may, however, complain if the
1511 // connection is closed, but that's okay.)
1512 if (completeEvent) {
1513 // Closing the database is more important than returning an error code
1514 // about a failure to dispatch, especially because all existing native
1515 // callers ignore our return value.
1516 Unused << NS_DispatchToMainThread(completeEvent.forget());
1518 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1519 // Return a success inconditionally here, since Close() is unlikely to fail
1520 // and we want to reassure the consumer that its callback will be invoked.
1521 return NS_OK;
1524 // setClosedState nullifies our connection pointer, so we take a raw pointer
1525 // off it, to pass it through the close procedure.
1526 sqlite3* nativeConn = mDBConn;
1527 rv = setClosedState();
1528 NS_ENSURE_SUCCESS(rv, rv);
1530 // Create and dispatch our close event to the background thread.
1531 nsCOMPtr<nsIRunnable> closeEvent =
1532 new AsyncCloseConnection(this, nativeConn, completeEvent);
1533 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1534 NS_ENSURE_SUCCESS(rv, rv);
1536 return NS_OK;
1539 NS_IMETHODIMP
1540 Connection::AsyncClone(bool aReadOnly,
1541 mozIStorageCompletionCallback* aCallback) {
1542 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1544 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1545 if (!connectionReady()) {
1546 return NS_ERROR_NOT_INITIALIZED;
1548 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1549 if (NS_FAILED(rv)) {
1550 return rv;
1552 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1554 int flags = mFlags;
1555 if (aReadOnly) {
1556 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1557 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1558 // Turn off SQLITE_OPEN_CREATE.
1559 flags = (~SQLITE_OPEN_CREATE & flags);
1562 // The cloned connection will still implement the synchronous API, but throw
1563 // if any synchronous methods are called on the main thread.
1564 RefPtr<Connection> clone =
1565 new Connection(mStorageService, flags, ASYNCHRONOUS);
1567 RefPtr<AsyncInitializeClone> initEvent =
1568 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1569 // Dispatch to our async thread, since the originating connection must remain
1570 // valid and open for the whole cloning process. This also ensures we are
1571 // properly serialized with a `close` operation, rather than race with it.
1572 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1573 if (!target) {
1574 return NS_ERROR_UNEXPECTED;
1576 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1579 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1580 nsresult rv;
1581 if (!mStorageKey.IsEmpty()) {
1582 rv = aClone->initialize(mStorageKey, mName);
1583 } else if (mFileURL) {
1584 rv = aClone->initialize(mFileURL, mTelemetryFilename);
1585 } else {
1586 rv = aClone->initialize(mDatabaseFile);
1588 if (NS_FAILED(rv)) {
1589 return rv;
1592 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1594 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1595 NS_ENSURE_SUCCESS(rv, rv);
1597 // Re-attach on-disk databases that were attached to the original connection.
1599 nsCOMPtr<mozIStorageStatement> stmt;
1600 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1601 MOZ_ASSERT(NS_SUCCEEDED(rv));
1602 bool hasResult = false;
1603 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1604 nsAutoCString name;
1605 rv = stmt->GetUTF8String(1, name);
1606 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1607 !name.EqualsLiteral("temp")) {
1608 nsCString path;
1609 rv = stmt->GetUTF8String(2, path);
1610 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1611 nsCOMPtr<mozIStorageStatement> attachStmt;
1612 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1613 getter_AddRefs(attachStmt));
1614 MOZ_ASSERT(NS_SUCCEEDED(rv));
1615 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1616 MOZ_ASSERT(NS_SUCCEEDED(rv));
1617 rv = attachStmt->Execute();
1618 MOZ_ASSERT(NS_SUCCEEDED(rv),
1619 "couldn't re-attach database to cloned connection");
1625 // Copy over pragmas from the original connection.
1626 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1627 // schema ("main" and any other attached databases), and this implmentation
1628 // fails to propagate them. This is being addressed on trunk.
1629 static const char* pragmas[] = {
1630 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1631 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1632 for (auto& pragma : pragmas) {
1633 // Read-only connections just need cache_size and temp_store pragmas.
1634 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1635 ::strcmp(pragma, "temp_store") != 0) {
1636 continue;
1639 nsAutoCString pragmaQuery("PRAGMA ");
1640 pragmaQuery.Append(pragma);
1641 nsCOMPtr<mozIStorageStatement> stmt;
1642 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
1643 MOZ_ASSERT(NS_SUCCEEDED(rv));
1644 bool hasResult = false;
1645 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1646 pragmaQuery.AppendLiteral(" = ");
1647 pragmaQuery.AppendInt(stmt->AsInt32(0));
1648 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
1649 MOZ_ASSERT(NS_SUCCEEDED(rv));
1653 // Copy over temporary tables, triggers, and views from the original
1654 // connections. Entities in `sqlite_temp_master` are only visible to the
1655 // connection that created them.
1656 if (!aReadOnly) {
1657 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
1658 NS_ENSURE_SUCCESS(rv, rv);
1660 nsCOMPtr<mozIStorageStatement> stmt;
1661 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1662 "WHERE type IN ('table', 'view', "
1663 "'index', 'trigger')"),
1664 getter_AddRefs(stmt));
1665 // Propagate errors, because failing to copy triggers might cause schema
1666 // coherency issues when writing to the database from the cloned connection.
1667 NS_ENSURE_SUCCESS(rv, rv);
1668 bool hasResult = false;
1669 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1670 nsAutoCString query;
1671 rv = stmt->GetUTF8String(0, query);
1672 NS_ENSURE_SUCCESS(rv, rv);
1674 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1675 // keyword. We need to add it back, or we'll recreate temporary entities
1676 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1677 // statements, but those don't need `TEMP` keywords.
1678 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
1679 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
1680 StringBeginsWith(query, "CREATE VIEW "_ns)) {
1681 query.Replace(0, 6, "CREATE TEMP");
1684 rv = aClone->ExecuteSimpleSQL(query);
1685 NS_ENSURE_SUCCESS(rv, rv);
1688 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
1689 NS_ENSURE_SUCCESS(rv, rv);
1692 // Copy any functions that have been added to this connection.
1693 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1694 for (const auto& entry : mFunctions) {
1695 const nsACString& key = entry.GetKey();
1696 Connection::FunctionInfo data = entry.GetData();
1698 rv = aClone->CreateFunction(key, data.numArgs, data.function);
1699 if (NS_FAILED(rv)) {
1700 NS_WARNING("Failed to copy function to cloned connection");
1704 guard.release();
1705 return NS_OK;
1708 NS_IMETHODIMP
1709 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
1710 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1712 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
1714 if (!connectionReady()) {
1715 return NS_ERROR_NOT_INITIALIZED;
1717 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1718 if (NS_FAILED(rv)) {
1719 return rv;
1722 int flags = mFlags;
1723 if (aReadOnly) {
1724 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1725 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1726 // Turn off SQLITE_OPEN_CREATE.
1727 flags = (~SQLITE_OPEN_CREATE & flags);
1730 RefPtr<Connection> clone = new Connection(
1731 mStorageService, flags, mSupportedOperations, mInterruptible);
1733 rv = initializeClone(clone, aReadOnly);
1734 if (NS_FAILED(rv)) {
1735 return rv;
1738 NS_IF_ADDREF(*_connection = clone);
1739 return NS_OK;
1742 NS_IMETHODIMP
1743 Connection::Interrupt() {
1744 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
1745 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
1746 threadOpenedOn != NS_GetCurrentThread());
1747 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
1748 threadOpenedOn == NS_GetCurrentThread());
1750 if (!connectionReady()) {
1751 return NS_ERROR_NOT_INITIALIZED;
1754 if (isClosing()) { // Closing already in asynchronous case
1755 return NS_OK;
1759 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
1760 // it is not safe to call sqlite3_interrupt() when
1761 // database connection is closed or might close before
1762 // sqlite3_interrupt() returns.
1763 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1764 if (!isClosed(lockedScope)) {
1765 MOZ_ASSERT(mDBConn);
1766 ::sqlite3_interrupt(mDBConn);
1770 return NS_OK;
1773 NS_IMETHODIMP
1774 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
1775 *_defaultPageSize = Service::kDefaultPageSize;
1776 return NS_OK;
1779 NS_IMETHODIMP
1780 Connection::GetConnectionReady(bool* _ready) {
1781 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1782 *_ready = connectionReady();
1783 return NS_OK;
1786 NS_IMETHODIMP
1787 Connection::GetDatabaseFile(nsIFile** _dbFile) {
1788 if (!connectionReady()) {
1789 return NS_ERROR_NOT_INITIALIZED;
1791 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1792 if (NS_FAILED(rv)) {
1793 return rv;
1796 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
1798 return NS_OK;
1801 NS_IMETHODIMP
1802 Connection::GetLastInsertRowID(int64_t* _id) {
1803 if (!connectionReady()) {
1804 return NS_ERROR_NOT_INITIALIZED;
1806 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1807 if (NS_FAILED(rv)) {
1808 return rv;
1811 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
1812 *_id = id;
1814 return NS_OK;
1817 NS_IMETHODIMP
1818 Connection::GetAffectedRows(int32_t* _rows) {
1819 if (!connectionReady()) {
1820 return NS_ERROR_NOT_INITIALIZED;
1822 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1823 if (NS_FAILED(rv)) {
1824 return rv;
1827 *_rows = ::sqlite3_changes(mDBConn);
1829 return NS_OK;
1832 NS_IMETHODIMP
1833 Connection::GetLastError(int32_t* _error) {
1834 if (!connectionReady()) {
1835 return NS_ERROR_NOT_INITIALIZED;
1837 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1838 if (NS_FAILED(rv)) {
1839 return rv;
1842 *_error = ::sqlite3_errcode(mDBConn);
1844 return NS_OK;
1847 NS_IMETHODIMP
1848 Connection::GetLastErrorString(nsACString& _errorString) {
1849 if (!connectionReady()) {
1850 return NS_ERROR_NOT_INITIALIZED;
1852 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1853 if (NS_FAILED(rv)) {
1854 return rv;
1857 const char* serr = ::sqlite3_errmsg(mDBConn);
1858 _errorString.Assign(serr);
1860 return NS_OK;
1863 NS_IMETHODIMP
1864 Connection::GetSchemaVersion(int32_t* _version) {
1865 if (!connectionReady()) {
1866 return NS_ERROR_NOT_INITIALIZED;
1868 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1869 if (NS_FAILED(rv)) {
1870 return rv;
1873 nsCOMPtr<mozIStorageStatement> stmt;
1874 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
1875 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
1877 *_version = 0;
1878 bool hasResult;
1879 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult)
1880 *_version = stmt->AsInt32(0);
1882 return NS_OK;
1885 NS_IMETHODIMP
1886 Connection::SetSchemaVersion(int32_t aVersion) {
1887 if (!connectionReady()) {
1888 return NS_ERROR_NOT_INITIALIZED;
1890 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1891 if (NS_FAILED(rv)) {
1892 return rv;
1895 nsAutoCString stmt("PRAGMA user_version = "_ns);
1896 stmt.AppendInt(aVersion);
1898 return ExecuteSimpleSQL(stmt);
1901 NS_IMETHODIMP
1902 Connection::CreateStatement(const nsACString& aSQLStatement,
1903 mozIStorageStatement** _stmt) {
1904 NS_ENSURE_ARG_POINTER(_stmt);
1905 if (!connectionReady()) {
1906 return NS_ERROR_NOT_INITIALIZED;
1908 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1909 if (NS_FAILED(rv)) {
1910 return rv;
1913 RefPtr<Statement> statement(new Statement());
1914 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1916 rv = statement->initialize(this, mDBConn, aSQLStatement);
1917 NS_ENSURE_SUCCESS(rv, rv);
1919 Statement* rawPtr;
1920 statement.forget(&rawPtr);
1921 *_stmt = rawPtr;
1922 return NS_OK;
1925 NS_IMETHODIMP
1926 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
1927 mozIStorageAsyncStatement** _stmt) {
1928 NS_ENSURE_ARG_POINTER(_stmt);
1929 if (!connectionReady()) {
1930 return NS_ERROR_NOT_INITIALIZED;
1932 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1933 if (NS_FAILED(rv)) {
1934 return rv;
1937 RefPtr<AsyncStatement> statement(new AsyncStatement());
1938 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1940 rv = statement->initialize(this, mDBConn, aSQLStatement);
1941 NS_ENSURE_SUCCESS(rv, rv);
1943 AsyncStatement* rawPtr;
1944 statement.forget(&rawPtr);
1945 *_stmt = rawPtr;
1946 return NS_OK;
1949 NS_IMETHODIMP
1950 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
1951 CHECK_MAINTHREAD_ABUSE();
1952 if (!connectionReady()) {
1953 return NS_ERROR_NOT_INITIALIZED;
1955 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1956 if (NS_FAILED(rv)) {
1957 return rv;
1960 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
1961 return convertResultCode(srv);
1964 NS_IMETHODIMP
1965 Connection::ExecuteAsync(
1966 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
1967 mozIStorageStatementCallback* aCallback,
1968 mozIStoragePendingStatement** _handle) {
1969 nsTArray<StatementData> stmts(aStatements.Length());
1970 for (uint32_t i = 0; i < aStatements.Length(); i++) {
1971 nsCOMPtr<StorageBaseStatementInternal> stmt =
1972 do_QueryInterface(aStatements[i]);
1973 NS_ENSURE_STATE(stmt);
1975 // Obtain our StatementData.
1976 StatementData data;
1977 nsresult rv = stmt->getAsynchronousStatementData(data);
1978 NS_ENSURE_SUCCESS(rv, rv);
1980 NS_ASSERTION(stmt->getOwner() == this,
1981 "Statement must be from this database connection!");
1983 // Now append it to our array.
1984 stmts.AppendElement(data);
1987 // Dispatch to the background
1988 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
1989 aCallback, _handle);
1992 NS_IMETHODIMP
1993 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
1994 mozIStorageStatementCallback* aCallback,
1995 mozIStoragePendingStatement** _handle) {
1996 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1998 nsCOMPtr<mozIStorageAsyncStatement> stmt;
1999 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
2000 if (NS_FAILED(rv)) {
2001 return rv;
2004 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
2005 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
2006 if (NS_FAILED(rv)) {
2007 return rv;
2010 pendingStatement.forget(_handle);
2011 return rv;
2014 NS_IMETHODIMP
2015 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
2016 return databaseElementExists(TABLE, aTableName, _exists);
2019 NS_IMETHODIMP
2020 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2021 return databaseElementExists(INDEX, aIndexName, _exists);
2024 NS_IMETHODIMP
2025 Connection::GetTransactionInProgress(bool* _inProgress) {
2026 if (!connectionReady()) {
2027 return NS_ERROR_NOT_INITIALIZED;
2029 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2030 if (NS_FAILED(rv)) {
2031 return rv;
2034 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2035 *_inProgress = transactionInProgress(lockedScope);
2036 return NS_OK;
2039 NS_IMETHODIMP
2040 Connection::GetDefaultTransactionType(int32_t* _type) {
2041 *_type = mDefaultTransactionType;
2042 return NS_OK;
2045 NS_IMETHODIMP
2046 Connection::SetDefaultTransactionType(int32_t aType) {
2047 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2048 mDefaultTransactionType = aType;
2049 return NS_OK;
2052 NS_IMETHODIMP
2053 Connection::GetVariableLimit(int32_t* _limit) {
2054 if (!connectionReady()) {
2055 return NS_ERROR_NOT_INITIALIZED;
2057 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2058 if (limit < 0) {
2059 return NS_ERROR_UNEXPECTED;
2061 *_limit = limit;
2062 return NS_OK;
2065 NS_IMETHODIMP
2066 Connection::BeginTransaction() {
2067 if (!connectionReady()) {
2068 return NS_ERROR_NOT_INITIALIZED;
2070 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2071 if (NS_FAILED(rv)) {
2072 return rv;
2075 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2076 return beginTransactionInternal(lockedScope, mDBConn,
2077 mDefaultTransactionType);
2080 nsresult Connection::beginTransactionInternal(
2081 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2082 int32_t aTransactionType) {
2083 if (transactionInProgress(aProofOfLock)) {
2084 return NS_ERROR_FAILURE;
2086 nsresult rv;
2087 switch (aTransactionType) {
2088 case TRANSACTION_DEFERRED:
2089 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2090 break;
2091 case TRANSACTION_IMMEDIATE:
2092 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2093 break;
2094 case TRANSACTION_EXCLUSIVE:
2095 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2096 break;
2097 default:
2098 return NS_ERROR_ILLEGAL_VALUE;
2100 return rv;
2103 NS_IMETHODIMP
2104 Connection::CommitTransaction() {
2105 if (!connectionReady()) {
2106 return NS_ERROR_NOT_INITIALIZED;
2108 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2109 if (NS_FAILED(rv)) {
2110 return rv;
2113 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2114 return commitTransactionInternal(lockedScope, mDBConn);
2117 nsresult Connection::commitTransactionInternal(
2118 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2119 if (!transactionInProgress(aProofOfLock)) {
2120 return NS_ERROR_UNEXPECTED;
2122 nsresult rv =
2123 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2124 return rv;
2127 NS_IMETHODIMP
2128 Connection::RollbackTransaction() {
2129 if (!connectionReady()) {
2130 return NS_ERROR_NOT_INITIALIZED;
2132 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2133 if (NS_FAILED(rv)) {
2134 return rv;
2137 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2138 return rollbackTransactionInternal(lockedScope, mDBConn);
2141 nsresult Connection::rollbackTransactionInternal(
2142 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2143 if (!transactionInProgress(aProofOfLock)) {
2144 return NS_ERROR_UNEXPECTED;
2147 nsresult rv =
2148 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2149 return rv;
2152 NS_IMETHODIMP
2153 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2154 if (!connectionReady()) {
2155 return NS_ERROR_NOT_INITIALIZED;
2157 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2158 if (NS_FAILED(rv)) {
2159 return rv;
2162 SmprintfPointer buf =
2163 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2164 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2166 int srv = executeSql(mDBConn, buf.get());
2168 return convertResultCode(srv);
2171 NS_IMETHODIMP
2172 Connection::CreateFunction(const nsACString& aFunctionName,
2173 int32_t aNumArguments,
2174 mozIStorageFunction* aFunction) {
2175 if (!connectionReady()) {
2176 return NS_ERROR_NOT_INITIALIZED;
2178 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2179 if (NS_FAILED(rv)) {
2180 return rv;
2183 // Check to see if this function is already defined. We only check the name
2184 // because a function can be defined with the same body but different names.
2185 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2186 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2188 int srv = ::sqlite3_create_function(
2189 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2190 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2191 if (srv != SQLITE_OK) return convertResultCode(srv);
2193 FunctionInfo info = {aFunction, aNumArguments};
2194 mFunctions.InsertOrUpdate(aFunctionName, info);
2196 return NS_OK;
2199 NS_IMETHODIMP
2200 Connection::RemoveFunction(const nsACString& aFunctionName) {
2201 if (!connectionReady()) {
2202 return NS_ERROR_NOT_INITIALIZED;
2204 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2205 if (NS_FAILED(rv)) {
2206 return rv;
2209 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2210 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2212 int srv = ::sqlite3_create_function(
2213 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2214 nullptr, nullptr, nullptr, nullptr);
2215 if (srv != SQLITE_OK) return convertResultCode(srv);
2217 mFunctions.Remove(aFunctionName);
2219 return NS_OK;
2222 NS_IMETHODIMP
2223 Connection::SetProgressHandler(int32_t aGranularity,
2224 mozIStorageProgressHandler* aHandler,
2225 mozIStorageProgressHandler** _oldHandler) {
2226 if (!connectionReady()) {
2227 return NS_ERROR_NOT_INITIALIZED;
2229 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2230 if (NS_FAILED(rv)) {
2231 return rv;
2234 // Return previous one
2235 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2236 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2238 if (!aHandler || aGranularity <= 0) {
2239 aHandler = nullptr;
2240 aGranularity = 0;
2242 mProgressHandler = aHandler;
2243 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2245 return NS_OK;
2248 NS_IMETHODIMP
2249 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2250 if (!connectionReady()) {
2251 return NS_ERROR_NOT_INITIALIZED;
2253 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2254 if (NS_FAILED(rv)) {
2255 return rv;
2258 // Return previous one
2259 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2260 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2262 mProgressHandler = nullptr;
2263 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2265 return NS_OK;
2268 NS_IMETHODIMP
2269 Connection::SetGrowthIncrement(int32_t aChunkSize,
2270 const nsACString& aDatabaseName) {
2271 if (!connectionReady()) {
2272 return NS_ERROR_NOT_INITIALIZED;
2274 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2275 if (NS_FAILED(rv)) {
2276 return rv;
2279 // Bug 597215: Disk space is extremely limited on Android
2280 // so don't preallocate space. This is also not effective
2281 // on log structured file systems used by Android devices
2282 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2283 // Don't preallocate if less than 500MiB is available.
2284 int64_t bytesAvailable;
2285 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2286 NS_ENSURE_SUCCESS(rv, rv);
2287 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2288 return NS_ERROR_FILE_TOO_BIG;
2291 (void)::sqlite3_file_control(mDBConn,
2292 aDatabaseName.Length()
2293 ? nsPromiseFlatCString(aDatabaseName).get()
2294 : nullptr,
2295 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2296 #endif
2297 return NS_OK;
2300 NS_IMETHODIMP
2301 Connection::EnableModule(const nsACString& aModuleName) {
2302 if (!connectionReady()) {
2303 return NS_ERROR_NOT_INITIALIZED;
2305 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2306 if (NS_FAILED(rv)) {
2307 return rv;
2310 for (auto& gModule : gModules) {
2311 struct Module* m = &gModule;
2312 if (aModuleName.Equals(m->name)) {
2313 int srv = m->registerFunc(mDBConn, m->name);
2314 if (srv != SQLITE_OK) return convertResultCode(srv);
2316 return NS_OK;
2320 return NS_ERROR_FAILURE;
2323 // Implemented in TelemetryVFS.cpp
2324 already_AddRefed<QuotaObject> GetQuotaObjectForFile(sqlite3_file* pFile);
2326 NS_IMETHODIMP
2327 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2328 QuotaObject** aJournalQuotaObject) {
2329 MOZ_ASSERT(aDatabaseQuotaObject);
2330 MOZ_ASSERT(aJournalQuotaObject);
2332 if (!connectionReady()) {
2333 return NS_ERROR_NOT_INITIALIZED;
2335 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2336 if (NS_FAILED(rv)) {
2337 return rv;
2340 sqlite3_file* file;
2341 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2342 &file);
2343 if (srv != SQLITE_OK) {
2344 return convertResultCode(srv);
2347 RefPtr<QuotaObject> databaseQuotaObject = GetQuotaObjectForFile(file);
2348 if (NS_WARN_IF(!databaseQuotaObject)) {
2349 return NS_ERROR_FAILURE;
2352 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2353 &file);
2354 if (srv != SQLITE_OK) {
2355 return convertResultCode(srv);
2358 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObjectForFile(file);
2359 if (NS_WARN_IF(!journalQuotaObject)) {
2360 return NS_ERROR_FAILURE;
2363 databaseQuotaObject.forget(aDatabaseQuotaObject);
2364 journalQuotaObject.forget(aJournalQuotaObject);
2365 return NS_OK;
2368 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2370 uint32_t Connection::GetTransactionNestingLevel(
2371 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2372 return mTransactionNestingLevel;
2375 uint32_t Connection::IncreaseTransactionNestingLevel(
2376 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2377 return ++mTransactionNestingLevel;
2380 uint32_t Connection::DecreaseTransactionNestingLevel(
2381 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2382 return --mTransactionNestingLevel;
2385 } // namespace mozilla::storage