Backed out changeset af2c13b9fdb7 (bug 1801244) per developer request.
[gecko.git] / storage / mozStorageConnection.cpp
blob1ad9fc6faacee1adaa93f349eeead8c1a7ceb1dd
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 NS_WARNING_ASSERTION( \
58 eventTargetOpenedOn == GetMainThreadSerialEventTarget() || \
59 !NS_IsMainThread(), \
60 "Using Storage synchronous API on main-thread, but " \
61 "the connection was opened on another thread."); \
62 } while (0)
63 #else
64 # define CHECK_MAINTHREAD_ABUSE() \
65 do { /* Nothing */ \
66 } while (0)
67 #endif
69 namespace mozilla::storage {
71 using mozilla::dom::quota::QuotaObject;
72 using mozilla::Telemetry::AccumulateCategoricalKeyed;
73 using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN;
74 using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY;
76 const char* GetTelemetryVFSName(bool);
77 const char* GetObfuscatingVFSName();
79 namespace {
81 int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
82 if (NS_SUCCEEDED(aXPCOMResultCode)) {
83 return SQLITE_OK;
86 switch (aXPCOMResultCode) {
87 case NS_ERROR_FILE_CORRUPTED:
88 return SQLITE_CORRUPT;
89 case NS_ERROR_FILE_ACCESS_DENIED:
90 return SQLITE_CANTOPEN;
91 case NS_ERROR_STORAGE_BUSY:
92 return SQLITE_BUSY;
93 case NS_ERROR_FILE_IS_LOCKED:
94 return SQLITE_LOCKED;
95 case NS_ERROR_FILE_READ_ONLY:
96 return SQLITE_READONLY;
97 case NS_ERROR_STORAGE_IOERR:
98 return SQLITE_IOERR;
99 case NS_ERROR_FILE_NO_DEVICE_SPACE:
100 return SQLITE_FULL;
101 case NS_ERROR_OUT_OF_MEMORY:
102 return SQLITE_NOMEM;
103 case NS_ERROR_UNEXPECTED:
104 return SQLITE_MISUSE;
105 case NS_ERROR_ABORT:
106 return SQLITE_ABORT;
107 case NS_ERROR_STORAGE_CONSTRAINT:
108 return SQLITE_CONSTRAINT;
109 default:
110 return SQLITE_ERROR;
113 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!");
116 ////////////////////////////////////////////////////////////////////////////////
117 //// Variant Specialization Functions (variantToSQLiteT)
119 int sqlite3_T_int(sqlite3_context* aCtx, int aValue) {
120 ::sqlite3_result_int(aCtx, aValue);
121 return SQLITE_OK;
124 int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
125 ::sqlite3_result_int64(aCtx, aValue);
126 return SQLITE_OK;
129 int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
130 ::sqlite3_result_double(aCtx, aValue);
131 return SQLITE_OK;
134 int sqlite3_T_text(sqlite3_context* aCtx, const nsCString& aValue) {
135 ::sqlite3_result_text(aCtx, aValue.get(), aValue.Length(), SQLITE_TRANSIENT);
136 return SQLITE_OK;
139 int sqlite3_T_text16(sqlite3_context* aCtx, const nsString& aValue) {
140 ::sqlite3_result_text16(
141 aCtx, aValue.get(),
142 aValue.Length() * sizeof(char16_t), // Number of bytes.
143 SQLITE_TRANSIENT);
144 return SQLITE_OK;
147 int sqlite3_T_null(sqlite3_context* aCtx) {
148 ::sqlite3_result_null(aCtx);
149 return SQLITE_OK;
152 int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
153 ::sqlite3_result_blob(aCtx, aData, aSize, free);
154 return SQLITE_OK;
157 #include "variantToSQLiteT_impl.h"
159 ////////////////////////////////////////////////////////////////////////////////
160 //// Modules
162 struct Module {
163 const char* name;
164 int (*registerFunc)(sqlite3*, const char*);
167 Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
169 ////////////////////////////////////////////////////////////////////////////////
170 //// Local Functions
172 int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
173 switch (aReason) {
174 case SQLITE_TRACE_STMT: {
175 // aP is a pointer to the prepared statement.
176 sqlite3_stmt* stmt = static_cast<sqlite3_stmt*>(aP);
177 // aX is a pointer to a string containing the unexpanded SQL or a comment,
178 // starting with "--"" in case of a trigger.
179 char* expanded = static_cast<char*>(aX);
180 // Simulate what sqlite_trace was doing.
181 if (!::strncmp(expanded, "--", 2)) {
182 MOZ_LOG(gStorageLog, LogLevel::Debug,
183 ("TRACE_STMT on %p: '%s'", aClosure, expanded));
184 } else {
185 char* sql = ::sqlite3_expanded_sql(stmt);
186 MOZ_LOG(gStorageLog, LogLevel::Debug,
187 ("TRACE_STMT on %p: '%s'", aClosure, sql));
188 ::sqlite3_free(sql);
190 break;
192 case SQLITE_TRACE_PROFILE: {
193 // aX is pointer to a 64bit integer containing nanoseconds it took to
194 // execute the last command.
195 sqlite_int64 time = *(static_cast<sqlite_int64*>(aX)) / 1000000;
196 if (time > 0) {
197 MOZ_LOG(gStorageLog, LogLevel::Debug,
198 ("TRACE_TIME on %p: %lldms", aClosure, time));
200 break;
203 return 0;
206 void basicFunctionHelper(sqlite3_context* aCtx, int aArgc,
207 sqlite3_value** aArgv) {
208 void* userData = ::sqlite3_user_data(aCtx);
210 mozIStorageFunction* func = static_cast<mozIStorageFunction*>(userData);
212 RefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
213 if (!arguments) return;
215 nsCOMPtr<nsIVariant> result;
216 nsresult rv = func->OnFunctionCall(arguments, getter_AddRefs(result));
217 if (NS_FAILED(rv)) {
218 nsAutoCString errorMessage;
219 GetErrorName(rv, errorMessage);
220 errorMessage.InsertLiteral("User function returned ", 0);
221 errorMessage.Append('!');
223 NS_WARNING(errorMessage.get());
225 ::sqlite3_result_error(aCtx, errorMessage.get(), -1);
226 ::sqlite3_result_error_code(aCtx, nsresultToSQLiteResult(rv));
227 return;
229 int retcode = variantToSQLiteT(aCtx, result);
230 if (retcode != SQLITE_OK) {
231 NS_WARNING("User function returned invalid data type!");
232 ::sqlite3_result_error(aCtx, "User function returned invalid data type",
233 -1);
238 * This code is heavily based on the sample at:
239 * http://www.sqlite.org/unlock_notify.html
241 class UnlockNotification {
242 public:
243 UnlockNotification()
244 : mMutex("UnlockNotification mMutex"),
245 mCondVar(mMutex, "UnlockNotification condVar"),
246 mSignaled(false) {}
248 void Wait() {
249 MutexAutoLock lock(mMutex);
250 while (!mSignaled) {
251 (void)mCondVar.Wait();
255 void Signal() {
256 MutexAutoLock lock(mMutex);
257 mSignaled = true;
258 (void)mCondVar.Notify();
261 private:
262 Mutex mMutex MOZ_UNANNOTATED;
263 CondVar mCondVar;
264 bool mSignaled;
267 void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
268 for (int i = 0; i < aArgsSize; i++) {
269 UnlockNotification* notification =
270 static_cast<UnlockNotification*>(aArgs[i]);
271 notification->Signal();
275 int WaitForUnlockNotify(sqlite3* aDatabase) {
276 UnlockNotification notification;
277 int srv =
278 ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, &notification);
279 MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
280 if (srv == SQLITE_OK) {
281 notification.Wait();
284 return srv;
287 ////////////////////////////////////////////////////////////////////////////////
288 //// Local Classes
290 class AsyncCloseConnection final : public Runnable {
291 public:
292 AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
293 nsIRunnable* aCallbackEvent)
294 : Runnable("storage::AsyncCloseConnection"),
295 mConnection(aConnection),
296 mNativeConnection(aNativeConnection),
297 mCallbackEvent(aCallbackEvent) {}
299 NS_IMETHOD Run() override {
300 // Make sure we don't dispatch to the current thread.
301 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn));
303 nsCOMPtr<nsIRunnable> event =
304 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
305 mConnection, &Connection::shutdownAsyncThread);
306 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event));
308 // Internal close.
309 (void)mConnection->internalClose(mNativeConnection);
311 // Callback
312 if (mCallbackEvent) {
313 nsCOMPtr<nsIThread> thread;
314 (void)NS_GetMainThread(getter_AddRefs(thread));
315 (void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
318 return NS_OK;
321 ~AsyncCloseConnection() override {
322 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
323 mConnection.forget());
324 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
325 mCallbackEvent.forget());
328 private:
329 RefPtr<Connection> mConnection;
330 sqlite3* mNativeConnection;
331 nsCOMPtr<nsIRunnable> mCallbackEvent;
335 * An event used to initialize the clone of a connection.
337 * Must be executed on the clone's async execution thread.
339 class AsyncInitializeClone final : public Runnable {
340 public:
342 * @param aConnection The connection being cloned.
343 * @param aClone The clone.
344 * @param aReadOnly If |true|, the clone is read only.
345 * @param aCallback A callback to trigger once initialization
346 * is complete. This event will be called on
347 * aClone->eventTargetOpenedOn.
349 AsyncInitializeClone(Connection* aConnection, Connection* aClone,
350 const bool aReadOnly,
351 mozIStorageCompletionCallback* aCallback)
352 : Runnable("storage::AsyncInitializeClone"),
353 mConnection(aConnection),
354 mClone(aClone),
355 mReadOnly(aReadOnly),
356 mCallback(aCallback) {
357 MOZ_ASSERT(NS_IsMainThread());
360 NS_IMETHOD Run() override {
361 MOZ_ASSERT(!NS_IsMainThread());
362 nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
363 if (NS_FAILED(rv)) {
364 return Dispatch(rv, nullptr);
366 return Dispatch(NS_OK,
367 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
370 private:
371 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
372 RefPtr<CallbackComplete> event =
373 new CallbackComplete(aResult, aValue, mCallback.forget());
374 return mClone->eventTargetOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
377 ~AsyncInitializeClone() override {
378 nsCOMPtr<nsIThread> thread;
379 DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
380 MOZ_ASSERT(NS_SUCCEEDED(rv));
382 // Handle ambiguous nsISupports inheritance.
383 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
384 mConnection.forget());
385 NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
387 // Generally, the callback will be released by CallbackComplete.
388 // However, if for some reason Run() is not executed, we still
389 // need to ensure that it is released here.
390 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
391 mCallback.forget());
394 RefPtr<Connection> mConnection;
395 RefPtr<Connection> mClone;
396 const bool mReadOnly;
397 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
401 * A listener for async connection closing.
403 class CloseListener final : public mozIStorageCompletionCallback {
404 public:
405 NS_DECL_ISUPPORTS
406 CloseListener() : mClosed(false) {}
408 NS_IMETHOD Complete(nsresult, nsISupports*) override {
409 mClosed = true;
410 return NS_OK;
413 bool mClosed;
415 private:
416 ~CloseListener() = default;
419 NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)
421 } // namespace
423 ////////////////////////////////////////////////////////////////////////////////
424 //// Connection
426 Connection::Connection(Service* aService, int aFlags,
427 ConnectionOperation aSupportedOperations,
428 bool aInterruptible, bool aIgnoreLockingMode)
429 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
430 sharedDBMutex("Connection::sharedDBMutex"),
431 eventTargetOpenedOn(WrapNotNull(GetCurrentSerialEventTarget())),
432 mDBConn(nullptr),
433 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
434 mDestroying(false),
435 mProgressHandler(nullptr),
436 mStorageService(aService),
437 mFlags(aFlags),
438 mTransactionNestingLevel(0),
439 mSupportedOperations(aSupportedOperations),
440 mInterruptible(aSupportedOperations == Connection::ASYNCHRONOUS ||
441 aInterruptible),
442 mIgnoreLockingMode(aIgnoreLockingMode),
443 mAsyncExecutionThreadShuttingDown(false),
444 mConnectionClosed(false) {
445 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
446 "Can't ignore locking for a non-readonly connection!");
447 mStorageService->registerConnection(this);
450 Connection::~Connection() {
451 // Failsafe Close() occurs in our custom Release method because of
452 // complications related to Close() potentially invoking AsyncClose() which
453 // will increment our refcount.
454 MOZ_ASSERT(!mAsyncExecutionThread,
455 "The async thread has not been shutdown properly!");
458 NS_IMPL_ADDREF(Connection)
460 NS_INTERFACE_MAP_BEGIN(Connection)
461 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
462 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
463 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
464 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
465 NS_INTERFACE_MAP_END
467 // This is identical to what NS_IMPL_RELEASE provides, but with the
468 // extra |1 == count| case.
469 NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
470 MOZ_ASSERT(0 != mRefCnt, "dup release");
471 nsrefcnt count = --mRefCnt;
472 NS_LOG_RELEASE(this, count, "Connection");
473 if (1 == count) {
474 // If the refcount went to 1, the single reference must be from
475 // gService->mConnections (in class |Service|). And the code calling
476 // Release is either:
477 // - The "user" code that had created the connection, releasing on any
478 // thread.
479 // - One of Service's getConnections() callers had acquired a strong
480 // reference to the Connection that out-lived the last "user" reference,
481 // and now that just got dropped. Note that this reference could be
482 // getting dropped on the main thread or Connection->eventTargetOpenedOn
483 // (because of the NewRunnableMethod used by minimizeMemory).
485 // Either way, we should now perform our failsafe Close() and unregister.
486 // However, we only want to do this once, and the reality is that our
487 // refcount could go back up above 1 and down again at any time if we are
488 // off the main thread and getConnections() gets called on the main thread,
489 // so we use an atomic here to do this exactly once.
490 if (mDestroying.compareExchange(false, true)) {
491 // Close the connection, dispatching to the opening event target if we're
492 // not on that event target already and that event target is still
493 // accepting runnables. We do this because it's possible we're on the main
494 // thread because of getConnections(), and we REALLY don't want to
495 // transfer I/O to the main thread if we can avoid it.
496 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
497 // This could cause SpinningSynchronousClose() to be invoked and AddRef
498 // triggered for AsyncCloseConnection's strong ref if the conn was ever
499 // use for async purposes. (Main-thread only, though.)
500 Unused << synchronousClose();
501 } else {
502 nsCOMPtr<nsIRunnable> event =
503 NewRunnableMethod("storage::Connection::synchronousClose", this,
504 &Connection::synchronousClose);
505 if (NS_FAILED(eventTargetOpenedOn->Dispatch(event.forget(),
506 NS_DISPATCH_NORMAL))) {
507 // The event target was dead and so we've just leaked our runnable.
508 // This should not happen because our non-main-thread consumers should
509 // be explicitly closing their connections, not relying on us to close
510 // them for them. (It's okay to let a statement go out of scope for
511 // automatic cleanup, but not a Connection.)
512 MOZ_ASSERT(false,
513 "Leaked Connection::synchronousClose(), ownership fail.");
514 Unused << synchronousClose();
518 // This will drop its strong reference right here, right now.
519 mStorageService->unregisterConnection(this);
521 } else if (0 == count) {
522 mRefCnt = 1; /* stabilize */
523 #if 0 /* enable this to find non-threadsafe destructors: */
524 NS_ASSERT_OWNINGTHREAD(Connection);
525 #endif
526 delete (this);
527 return 0;
529 return count;
532 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
533 int32_t* aMaxValue) {
534 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
535 int curr = 0, max = 0;
536 DebugOnly<int> rc =
537 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
538 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
539 if (aMaxValue) *aMaxValue = max;
540 return curr;
543 nsIEventTarget* Connection::getAsyncExecutionTarget() {
544 NS_ENSURE_TRUE(IsOnCurrentSerialEventTarget(eventTargetOpenedOn), nullptr);
546 // Don't return the asynchronous event target if we are shutting down.
547 if (mAsyncExecutionThreadShuttingDown) {
548 return nullptr;
551 // Create the async event target if there's none yet.
552 if (!mAsyncExecutionThread) {
553 static nsThreadPoolNaming naming;
554 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName("mozStorage"),
555 getter_AddRefs(mAsyncExecutionThread));
556 if (NS_FAILED(rv)) {
557 NS_WARNING("Failed to create async thread.");
558 return nullptr;
560 mAsyncExecutionThread->SetNameForWakeupTelemetry("mozStorage (all)"_ns);
563 return mAsyncExecutionThread;
566 void Connection::RecordOpenStatus(nsresult rv) {
567 nsCString histogramKey = mTelemetryFilename;
569 if (histogramKey.IsEmpty()) {
570 histogramKey.AssignLiteral("unknown");
573 if (NS_SUCCEEDED(rv)) {
574 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
575 return;
578 switch (rv) {
579 case NS_ERROR_FILE_CORRUPTED:
580 AccumulateCategoricalKeyed(histogramKey,
581 LABELS_SQLITE_STORE_OPEN::corrupt);
582 break;
583 case NS_ERROR_STORAGE_IOERR:
584 AccumulateCategoricalKeyed(histogramKey,
585 LABELS_SQLITE_STORE_OPEN::diskio);
586 break;
587 case NS_ERROR_FILE_ACCESS_DENIED:
588 case NS_ERROR_FILE_IS_LOCKED:
589 case NS_ERROR_FILE_READ_ONLY:
590 AccumulateCategoricalKeyed(histogramKey,
591 LABELS_SQLITE_STORE_OPEN::access);
592 break;
593 case NS_ERROR_FILE_NO_DEVICE_SPACE:
594 AccumulateCategoricalKeyed(histogramKey,
595 LABELS_SQLITE_STORE_OPEN::diskspace);
596 break;
597 default:
598 AccumulateCategoricalKeyed(histogramKey,
599 LABELS_SQLITE_STORE_OPEN::failure);
603 void Connection::RecordQueryStatus(int srv) {
604 nsCString histogramKey = mTelemetryFilename;
606 if (histogramKey.IsEmpty()) {
607 histogramKey.AssignLiteral("unknown");
610 switch (srv) {
611 case SQLITE_OK:
612 case SQLITE_ROW:
613 case SQLITE_DONE:
615 // Note that these are returned when we intentionally cancel a statement so
616 // they aren't indicating a failure.
617 case SQLITE_ABORT:
618 case SQLITE_INTERRUPT:
619 AccumulateCategoricalKeyed(histogramKey,
620 LABELS_SQLITE_STORE_QUERY::success);
621 break;
622 case SQLITE_CORRUPT:
623 case SQLITE_NOTADB:
624 AccumulateCategoricalKeyed(histogramKey,
625 LABELS_SQLITE_STORE_QUERY::corrupt);
626 break;
627 case SQLITE_PERM:
628 case SQLITE_CANTOPEN:
629 case SQLITE_LOCKED:
630 case SQLITE_READONLY:
631 AccumulateCategoricalKeyed(histogramKey,
632 LABELS_SQLITE_STORE_QUERY::access);
633 break;
634 case SQLITE_IOERR:
635 case SQLITE_NOLFS:
636 AccumulateCategoricalKeyed(histogramKey,
637 LABELS_SQLITE_STORE_QUERY::diskio);
638 break;
639 case SQLITE_FULL:
640 case SQLITE_TOOBIG:
641 AccumulateCategoricalKeyed(histogramKey,
642 LABELS_SQLITE_STORE_OPEN::diskspace);
643 break;
644 case SQLITE_CONSTRAINT:
645 case SQLITE_RANGE:
646 case SQLITE_MISMATCH:
647 case SQLITE_MISUSE:
648 AccumulateCategoricalKeyed(histogramKey,
649 LABELS_SQLITE_STORE_OPEN::misuse);
650 break;
651 case SQLITE_BUSY:
652 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::busy);
653 break;
654 default:
655 AccumulateCategoricalKeyed(histogramKey,
656 LABELS_SQLITE_STORE_QUERY::failure);
660 nsresult Connection::initialize(const nsACString& aStorageKey,
661 const nsACString& aName) {
662 MOZ_ASSERT(aStorageKey.Equals(kMozStorageMemoryStorageKey));
663 NS_ASSERTION(!connectionReady(),
664 "Initialize called on already opened database!");
665 MOZ_ASSERT(!mIgnoreLockingMode, "Can't ignore locking on an in-memory db.");
666 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
668 mStorageKey = aStorageKey;
669 mName = aName;
671 // in memory database requested, sqlite uses a magic file name
673 const nsAutoCString path =
674 mName.IsEmpty() ? nsAutoCString(":memory:"_ns)
675 : "file:"_ns + mName + "?mode=memory&cache=shared"_ns;
677 mTelemetryFilename.AssignLiteral(":memory:");
679 int srv = ::sqlite3_open_v2(path.get(), &mDBConn, mFlags,
680 GetTelemetryVFSName(true));
681 if (srv != SQLITE_OK) {
682 mDBConn = nullptr;
683 nsresult rv = convertResultCode(srv);
684 RecordOpenStatus(rv);
685 return rv;
688 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
689 srv =
690 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
691 MOZ_ASSERT(srv == SQLITE_OK,
692 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
693 #endif
695 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
696 // database.
698 nsresult rv = initializeInternal();
699 RecordOpenStatus(rv);
700 NS_ENSURE_SUCCESS(rv, rv);
702 return NS_OK;
705 nsresult Connection::initialize(nsIFile* aDatabaseFile) {
706 NS_ASSERTION(aDatabaseFile, "Passed null file!");
707 NS_ASSERTION(!connectionReady(),
708 "Initialize called on already opened database!");
709 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
711 // Do not set mFileURL here since this is database does not have an associated
712 // URL.
713 mDatabaseFile = aDatabaseFile;
714 aDatabaseFile->GetNativeLeafName(mTelemetryFilename);
716 nsAutoString path;
717 nsresult rv = aDatabaseFile->GetPath(path);
718 NS_ENSURE_SUCCESS(rv, rv);
720 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
721 int srv;
722 if (mIgnoreLockingMode) {
723 exclusive = false;
724 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
725 "readonly-immutable-nolock");
726 } else {
727 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
728 GetTelemetryVFSName(exclusive));
729 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
730 // Retry without trying to get an exclusive lock.
731 exclusive = false;
732 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
733 mFlags, GetTelemetryVFSName(false));
736 if (srv != SQLITE_OK) {
737 mDBConn = nullptr;
738 rv = convertResultCode(srv);
739 RecordOpenStatus(rv);
740 return rv;
743 rv = initializeInternal();
744 if (exclusive &&
745 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
746 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
747 // some cases it may successfully open the database and then lock on the
748 // first query execution. When initializeInternal fails it closes the
749 // connection, so we can try to restart it in non-exclusive mode.
750 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
751 GetTelemetryVFSName(false));
752 if (srv == SQLITE_OK) {
753 rv = initializeInternal();
757 RecordOpenStatus(rv);
758 NS_ENSURE_SUCCESS(rv, rv);
760 return NS_OK;
763 nsresult Connection::initialize(nsIFileURL* aFileURL,
764 const nsACString& aTelemetryFilename) {
765 NS_ASSERTION(aFileURL, "Passed null file URL!");
766 NS_ASSERTION(!connectionReady(),
767 "Initialize called on already opened database!");
768 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
770 nsCOMPtr<nsIFile> databaseFile;
771 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
772 NS_ENSURE_SUCCESS(rv, rv);
774 // Set both mDatabaseFile and mFileURL here.
775 mFileURL = aFileURL;
776 mDatabaseFile = databaseFile;
778 if (!aTelemetryFilename.IsEmpty()) {
779 mTelemetryFilename = aTelemetryFilename;
780 } else {
781 databaseFile->GetNativeLeafName(mTelemetryFilename);
784 nsAutoCString spec;
785 rv = aFileURL->GetSpec(spec);
786 NS_ENSURE_SUCCESS(rv, rv);
788 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
790 // If there is a key specified, we need to use the obfuscating VFS.
791 nsAutoCString query;
792 rv = aFileURL->GetQuery(query);
793 NS_ENSURE_SUCCESS(rv, rv);
795 const char* const vfs =
796 !URLParams::Parse(query,
797 [](const nsAString& aName, const nsAString& aValue) {
798 return !aName.EqualsLiteral("key");
800 ? GetObfuscatingVFSName()
801 : GetTelemetryVFSName(exclusive);
803 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
804 if (srv != SQLITE_OK) {
805 mDBConn = nullptr;
806 rv = convertResultCode(srv);
807 RecordOpenStatus(rv);
808 return rv;
811 rv = initializeInternal();
812 RecordOpenStatus(rv);
813 NS_ENSURE_SUCCESS(rv, rv);
815 return NS_OK;
818 nsresult Connection::initializeInternal() {
819 MOZ_ASSERT(mDBConn);
820 auto guard = MakeScopeExit([&]() { initializeFailed(); });
822 mConnectionClosed = false;
824 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
825 DebugOnly<int> srv2 =
826 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
827 MOZ_ASSERT(srv2 == SQLITE_OK,
828 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
829 #endif
831 MOZ_ASSERT(!mTelemetryFilename.IsEmpty(),
832 "A telemetry filename should have been set by now.");
834 // Properly wrap the database handle's mutex.
835 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
837 // SQLite tracing can slow down queries (especially long queries)
838 // significantly. Don't trace unless the user is actively monitoring SQLite.
839 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
840 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
841 tracefunc, this);
843 MOZ_LOG(
844 gStorageLog, LogLevel::Debug,
845 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
848 int64_t pageSize = Service::kDefaultPageSize;
850 // Set page_size to the preferred default value. This is effective only if
851 // the database has just been created, otherwise, if the database does not
852 // use WAL journal mode, a VACUUM operation will updated its page_size.
853 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
854 "PRAGMA page_size = ");
855 pageSizeQuery.AppendInt(pageSize);
856 int srv = executeSql(mDBConn, pageSizeQuery.get());
857 if (srv != SQLITE_OK) {
858 return convertResultCode(srv);
861 // Setting the cache_size forces the database open, verifying if it is valid
862 // or corrupt. So this is executed regardless it being actually needed.
863 // The cache_size is calculated from the actual page_size, to save memory.
864 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
865 "PRAGMA cache_size = ");
866 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
867 srv = executeSql(mDBConn, cacheSizeQuery.get());
868 if (srv != SQLITE_OK) {
869 return convertResultCode(srv);
872 // Register our built-in SQL functions.
873 srv = registerFunctions(mDBConn);
874 if (srv != SQLITE_OK) {
875 return convertResultCode(srv);
878 // Register our built-in SQL collating sequences.
879 srv = registerCollations(mDBConn, mStorageService);
880 if (srv != SQLITE_OK) {
881 return convertResultCode(srv);
884 // Set the default synchronous value. Each consumer can switch this
885 // accordingly to their needs.
886 #if defined(ANDROID)
887 // Android prefers synchronous = OFF for performance reasons.
888 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
889 #else
890 // Normal is the suggested value for WAL journals.
891 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
892 #endif
894 // Initialization succeeded, we can stop guarding for failures.
895 guard.release();
896 return NS_OK;
899 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
900 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
901 nsresult rv = aStorageFile
902 ? initialize(aStorageFile)
903 : initialize(kMozStorageMemoryStorageKey, VoidCString());
904 if (NS_FAILED(rv)) {
905 // Shutdown the async thread, since initialization failed.
906 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
907 mAsyncExecutionThreadShuttingDown = true;
908 nsCOMPtr<nsIRunnable> event =
909 NewRunnableMethod("Connection::shutdownAsyncThread", this,
910 &Connection::shutdownAsyncThread);
911 Unused << NS_DispatchToMainThread(event);
913 return rv;
916 void Connection::initializeFailed() {
918 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
919 mConnectionClosed = true;
921 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
922 mDBConn = nullptr;
923 sharedDBMutex.destroy();
926 nsresult Connection::databaseElementExists(
927 enum DatabaseElementType aElementType, const nsACString& aElementName,
928 bool* _exists) {
929 if (!connectionReady()) {
930 return NS_ERROR_NOT_AVAILABLE;
932 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
933 if (NS_FAILED(rv)) {
934 return rv;
937 // When constructing the query, make sure to SELECT the correct db's
938 // sqlite_master if the user is prefixing the element with a specific db. ex:
939 // sample.test
940 nsCString query("SELECT name FROM (SELECT * FROM ");
941 nsDependentCSubstring element;
942 int32_t ind = aElementName.FindChar('.');
943 if (ind == kNotFound) {
944 element.Assign(aElementName);
945 } else {
946 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
947 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
948 query.Append(db);
950 query.AppendLiteral(
951 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
952 "'");
954 switch (aElementType) {
955 case INDEX:
956 query.AppendLiteral("index");
957 break;
958 case TABLE:
959 query.AppendLiteral("table");
960 break;
962 query.AppendLiteral("' AND name ='");
963 query.Append(element);
964 query.Append('\'');
966 sqlite3_stmt* stmt;
967 int srv = prepareStatement(mDBConn, query, &stmt);
968 if (srv != SQLITE_OK) {
969 RecordQueryStatus(srv);
970 return convertResultCode(srv);
973 srv = stepStatement(mDBConn, stmt);
974 // we just care about the return value from step
975 (void)::sqlite3_finalize(stmt);
977 RecordQueryStatus(srv);
979 if (srv == SQLITE_ROW) {
980 *_exists = true;
981 return NS_OK;
983 if (srv == SQLITE_DONE) {
984 *_exists = false;
985 return NS_OK;
988 return convertResultCode(srv);
991 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
992 sharedDBMutex.assertCurrentThreadOwns();
994 for (const auto& data : mFunctions.Values()) {
995 if (data.function == aInstance) {
996 return true;
999 return false;
1002 /* static */
1003 int Connection::sProgressHelper(void* aArg) {
1004 Connection* _this = static_cast<Connection*>(aArg);
1005 return _this->progressHandler();
1008 int Connection::progressHandler() {
1009 sharedDBMutex.assertCurrentThreadOwns();
1010 if (mProgressHandler) {
1011 bool result;
1012 nsresult rv = mProgressHandler->OnProgress(this, &result);
1013 if (NS_FAILED(rv)) return 0; // Don't break request
1014 return result ? 1 : 0;
1016 return 0;
1019 nsresult Connection::setClosedState() {
1020 // Flag that we are shutting down the async thread, so that
1021 // getAsyncExecutionTarget knows not to expose/create the async thread.
1022 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1023 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1025 mAsyncExecutionThreadShuttingDown = true;
1027 // Set the property to null before closing the connection, otherwise the
1028 // other functions in the module may try to use the connection after it is
1029 // closed.
1030 mDBConn = nullptr;
1032 return NS_OK;
1035 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1036 if (aOperationType == ASYNCHRONOUS) {
1037 // Async operations are supported for all connections, on any thread.
1038 return true;
1040 // Sync operations are supported for sync connections (on any thread), and
1041 // async connections on a background thread.
1042 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1043 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1046 nsresult Connection::ensureOperationSupported(
1047 ConnectionOperation aOperationType) {
1048 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1049 #ifdef DEBUG
1050 if (NS_IsMainThread()) {
1051 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1052 Unused << xpc->DebugDumpJSStack(false, false, false);
1054 #endif
1055 MOZ_ASSERT(false,
1056 "Don't use async connections synchronously on the main thread");
1057 return NS_ERROR_NOT_AVAILABLE;
1059 return NS_OK;
1062 bool Connection::isConnectionReadyOnThisThread() {
1063 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1064 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1065 return true;
1067 return connectionReady();
1070 bool Connection::isClosing() {
1071 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1072 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1075 bool Connection::isClosed() {
1076 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1077 return mConnectionClosed;
1080 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1082 bool Connection::isAsyncExecutionThreadAvailable() {
1083 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1084 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1087 void Connection::shutdownAsyncThread() {
1088 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1089 MOZ_ASSERT(mAsyncExecutionThread);
1090 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1092 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1093 mAsyncExecutionThread = nullptr;
1096 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1097 #ifdef DEBUG
1098 { // Make sure we have marked our async thread as shutting down.
1099 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1100 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1101 "Did not call setClosedState!");
1102 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1104 #endif // DEBUG
1106 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1107 nsAutoCString leafName(":memory");
1108 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1109 MOZ_LOG(gStorageLog, LogLevel::Debug,
1110 ("Closing connection to '%s'", leafName.get()));
1113 // At this stage, we may still have statements that need to be
1114 // finalized. Attempt to close the database connection. This will
1115 // always disconnect any virtual tables and cleanly finalize their
1116 // internal statements. Once this is done, closing may fail due to
1117 // unfinalized client statements, in which case we need to finalize
1118 // these statements and close again.
1120 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1121 mConnectionClosed = true;
1124 // Nothing else needs to be done if we don't have a connection here.
1125 if (!aNativeConnection) return NS_OK;
1127 int srv = ::sqlite3_close(aNativeConnection);
1129 if (srv == SQLITE_BUSY) {
1131 // Nothing else should change the connection or statements status until we
1132 // are done here.
1133 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1134 // We still have non-finalized statements. Finalize them.
1135 sqlite3_stmt* stmt = nullptr;
1136 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1137 MOZ_LOG(gStorageLog, LogLevel::Debug,
1138 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1139 stmt));
1141 #ifdef DEBUG
1142 SmprintfPointer msg = ::mozilla::Smprintf(
1143 "SQL statement '%s' (%p) should have been finalized before closing "
1144 "the connection",
1145 ::sqlite3_sql(stmt), stmt);
1146 NS_WARNING(msg.get());
1147 #endif // DEBUG
1149 srv = ::sqlite3_finalize(stmt);
1151 #ifdef DEBUG
1152 if (srv != SQLITE_OK) {
1153 SmprintfPointer msg = ::mozilla::Smprintf(
1154 "Could not finalize SQL statement (%p)", stmt);
1155 NS_WARNING(msg.get());
1157 #endif // DEBUG
1159 // Ensure that the loop continues properly, whether closing has
1160 // succeeded or not.
1161 if (srv == SQLITE_OK) {
1162 stmt = nullptr;
1165 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1166 // again, since Sqlite will try to acquire it.
1169 // Now that all statements have been finalized, we
1170 // should be able to close.
1171 srv = ::sqlite3_close(aNativeConnection);
1172 MOZ_ASSERT(false,
1173 "Had to forcibly close the database connection because not all "
1174 "the statements have been finalized.");
1177 if (srv == SQLITE_OK) {
1178 sharedDBMutex.destroy();
1179 } else {
1180 MOZ_ASSERT(false,
1181 "sqlite3_close failed. There are probably outstanding "
1182 "statements that are listed above!");
1185 return convertResultCode(srv);
1188 nsCString Connection::getFilename() { return mTelemetryFilename; }
1190 int Connection::stepStatement(sqlite3* aNativeConnection,
1191 sqlite3_stmt* aStatement) {
1192 MOZ_ASSERT(aStatement);
1194 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1195 ::sqlite3_sql(aStatement));
1197 bool checkedMainThread = false;
1198 TimeStamp startTime = TimeStamp::Now();
1200 // The connection may have been closed if the executing statement has been
1201 // created and cached after a call to asyncClose() but before the actual
1202 // sqlite3_close(). This usually happens when other tasks using cached
1203 // statements are asynchronously scheduled for execution and any of them ends
1204 // up after asyncClose. See bug 728653 for details.
1205 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1207 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1209 int srv;
1210 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1211 if (!checkedMainThread) {
1212 checkedMainThread = true;
1213 if (::NS_IsMainThread()) {
1214 NS_WARNING("We won't allow blocking on the main thread!");
1215 break;
1219 srv = WaitForUnlockNotify(aNativeConnection);
1220 if (srv != SQLITE_OK) {
1221 break;
1224 ::sqlite3_reset(aStatement);
1227 // Report very slow SQL statements to Telemetry
1228 TimeDuration duration = TimeStamp::Now() - startTime;
1229 const uint32_t threshold = NS_IsMainThread()
1230 ? Telemetry::kSlowSQLThresholdForMainThread
1231 : Telemetry::kSlowSQLThresholdForHelperThreads;
1232 if (duration.ToMilliseconds() >= threshold) {
1233 nsDependentCString statementString(::sqlite3_sql(aStatement));
1234 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1235 duration.ToMilliseconds());
1238 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1239 // Drop off the extended result bits of the result code.
1240 return srv & 0xFF;
1243 int Connection::prepareStatement(sqlite3* aNativeConnection,
1244 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1245 // We should not even try to prepare statements after the connection has
1246 // been closed.
1247 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1249 bool checkedMainThread = false;
1251 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1253 int srv;
1254 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1255 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1256 if (!checkedMainThread) {
1257 checkedMainThread = true;
1258 if (::NS_IsMainThread()) {
1259 NS_WARNING("We won't allow blocking on the main thread!");
1260 break;
1264 srv = WaitForUnlockNotify(aNativeConnection);
1265 if (srv != SQLITE_OK) {
1266 break;
1270 if (srv != SQLITE_OK) {
1271 nsCString warnMsg;
1272 warnMsg.AppendLiteral("The SQL statement '");
1273 warnMsg.Append(aSQL);
1274 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1275 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1277 #ifdef DEBUG
1278 NS_WARNING(warnMsg.get());
1279 #endif
1280 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1283 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1284 // Drop off the extended result bits of the result code.
1285 int rc = srv & 0xFF;
1286 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1287 // The callers of this function are used to only checking the return value,
1288 // so it is safer to return an error code.
1289 if (rc == SQLITE_OK && *_stmt == nullptr) {
1290 return SQLITE_MISUSE;
1293 return rc;
1296 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1297 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1299 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1301 TimeStamp startTime = TimeStamp::Now();
1302 int srv =
1303 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1304 RecordQueryStatus(srv);
1306 // Report very slow SQL statements to Telemetry
1307 TimeDuration duration = TimeStamp::Now() - startTime;
1308 const uint32_t threshold = NS_IsMainThread()
1309 ? Telemetry::kSlowSQLThresholdForMainThread
1310 : Telemetry::kSlowSQLThresholdForHelperThreads;
1311 if (duration.ToMilliseconds() >= threshold) {
1312 nsDependentCString statementString(aSqlString);
1313 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1314 duration.ToMilliseconds());
1317 return srv;
1320 ////////////////////////////////////////////////////////////////////////////////
1321 //// nsIInterfaceRequestor
1323 NS_IMETHODIMP
1324 Connection::GetInterface(const nsIID& aIID, void** _result) {
1325 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1326 nsIEventTarget* background = getAsyncExecutionTarget();
1327 NS_IF_ADDREF(background);
1328 *_result = background;
1329 return NS_OK;
1331 return NS_ERROR_NO_INTERFACE;
1334 ////////////////////////////////////////////////////////////////////////////////
1335 //// mozIStorageConnection
1337 NS_IMETHODIMP
1338 Connection::Close() {
1339 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1340 if (NS_FAILED(rv)) {
1341 return rv;
1343 return synchronousClose();
1346 nsresult Connection::synchronousClose() {
1347 if (!connectionReady()) {
1348 return NS_ERROR_NOT_INITIALIZED;
1351 #ifdef DEBUG
1352 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1353 // event target. We make this check outside of debug code below in
1354 // setClosedState, but this is here to be explicit.
1355 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1356 #endif // DEBUG
1358 // Make sure we have not executed any asynchronous statements.
1359 // If this fails, the mDBConn may be left open, resulting in a leak.
1360 // We'll try to finalize the pending statements and close the connection.
1361 if (isAsyncExecutionThreadAvailable()) {
1362 #ifdef DEBUG
1363 if (NS_IsMainThread()) {
1364 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1365 Unused << xpc->DebugDumpJSStack(false, false, false);
1367 #endif
1368 MOZ_ASSERT(false,
1369 "Close() was invoked on a connection that executed asynchronous "
1370 "statements. "
1371 "Should have used asyncClose().");
1372 // Try to close the database regardless, to free up resources.
1373 Unused << SpinningSynchronousClose();
1374 return NS_ERROR_UNEXPECTED;
1377 // setClosedState nullifies our connection pointer, so we take a raw pointer
1378 // off it, to pass it through the close procedure.
1379 sqlite3* nativeConn = mDBConn;
1380 nsresult rv = setClosedState();
1381 NS_ENSURE_SUCCESS(rv, rv);
1383 return internalClose(nativeConn);
1386 NS_IMETHODIMP
1387 Connection::SpinningSynchronousClose() {
1388 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1389 if (NS_FAILED(rv)) {
1390 return rv;
1392 if (!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
1393 return NS_ERROR_NOT_SAME_THREAD;
1396 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1397 // Our only existing caller will never have called close; assert if misused
1398 // so that no new callers assume this works after an AsyncClose.
1399 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1400 if (!connectionReady()) {
1401 return NS_ERROR_UNEXPECTED;
1404 RefPtr<CloseListener> listener = new CloseListener();
1405 rv = AsyncClose(listener);
1406 NS_ENSURE_SUCCESS(rv, rv);
1407 MOZ_ALWAYS_TRUE(
1408 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,
1409 [&]() { return listener->mClosed; }));
1410 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1412 return rv;
1415 NS_IMETHODIMP
1416 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1417 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1418 // Check if AsyncClose or Close were already invoked.
1419 if (!connectionReady()) {
1420 return NS_ERROR_NOT_INITIALIZED;
1422 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1423 if (NS_FAILED(rv)) {
1424 return rv;
1427 // The two relevant factors at this point are whether we have a database
1428 // connection and whether we have an async execution thread. Here's what the
1429 // states mean and how we handle them:
1431 // - (mDBConn && asyncThread): The expected case where we are either an
1432 // async connection or a sync connection that has been used asynchronously.
1433 // Either way the caller must call us and not Close(). Nothing surprising
1434 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1435 // async thread.
1437 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1438 // opened the connection synchronously and was planning to use it
1439 // asynchronously, but never got around to using it asynchronously before
1440 // needing to shutdown. This has been observed to happen for the cookie
1441 // service in a case where Firefox shuts itself down almost immediately
1442 // after startup (for unknown reasons). In the Firefox shutdown case,
1443 // we may also fail to create a new async execution thread if one does not
1444 // already exist. (nsThreadManager will refuse to create new threads when
1445 // it has already been told to shutdown.) As such, we need to handle a
1446 // failure to create the async execution thread by falling back to
1447 // synchronous Close() and also dispatching the completion callback because
1448 // at least Places likes to spin a nested event loop that depends on the
1449 // callback being invoked.
1451 // Note that we have considered not trying to spin up the async execution
1452 // thread in this case if it does not already exist, but the overhead of
1453 // thread startup (if successful) is significantly less expensive than the
1454 // worst-case potential I/O hit of synchronously closing a database when we
1455 // could close it asynchronously.
1457 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1458 // OpenAsyncDatabase encountered a problem opening the database. If it
1459 // happened in all cases AsyncInitDatabase would just shut down the thread
1460 // directly and we would avoid this case. But it doesn't, so for simplicity
1461 // and consistency AsyncCloseConnection knows how to handle this and we
1462 // act like this was the (mDBConn && asyncThread) case in this method.
1464 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1465 // Close() or AsyncClose() has already been called (at least) once. This is
1466 // undeniably a misuse case by the caller. We could optimize for this
1467 // case by adding an additional check of mAsyncExecutionThread without using
1468 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1469 // shut it down. But this complicates the method for broken caller code
1470 // whereas we're still correct and safe without the special-case.
1471 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1473 // Create our callback event if we were given a callback. This will
1474 // eventually be dispatched in all cases, even if we fall back to Close() and
1475 // the database wasn't open and we return an error. The rationale is that
1476 // no existing consumer checks our return value and several of them like to
1477 // spin nested event loops until the callback fires. Given that, it seems
1478 // preferable for us to dispatch the callback in all cases. (Except the
1479 // wrong thread misuse case we bailed on up above. But that's okay because
1480 // that is statically wrong whereas these edge cases are dynamic.)
1481 nsCOMPtr<nsIRunnable> completeEvent;
1482 if (aCallback) {
1483 completeEvent = newCompletionEvent(aCallback);
1486 if (!asyncThread) {
1487 // We were unable to create an async thread, so we need to fall back to
1488 // using normal Close(). Since there is no async thread, Close() will
1489 // not complain about that. (Close() may, however, complain if the
1490 // connection is closed, but that's okay.)
1491 if (completeEvent) {
1492 // Closing the database is more important than returning an error code
1493 // about a failure to dispatch, especially because all existing native
1494 // callers ignore our return value.
1495 Unused << NS_DispatchToMainThread(completeEvent.forget());
1497 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1498 // Return a success inconditionally here, since Close() is unlikely to fail
1499 // and we want to reassure the consumer that its callback will be invoked.
1500 return NS_OK;
1503 // setClosedState nullifies our connection pointer, so we take a raw pointer
1504 // off it, to pass it through the close procedure.
1505 sqlite3* nativeConn = mDBConn;
1506 rv = setClosedState();
1507 NS_ENSURE_SUCCESS(rv, rv);
1509 // Create and dispatch our close event to the background thread.
1510 nsCOMPtr<nsIRunnable> closeEvent =
1511 new AsyncCloseConnection(this, nativeConn, completeEvent);
1512 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1513 NS_ENSURE_SUCCESS(rv, rv);
1515 return NS_OK;
1518 NS_IMETHODIMP
1519 Connection::AsyncClone(bool aReadOnly,
1520 mozIStorageCompletionCallback* aCallback) {
1521 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1523 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1524 if (!connectionReady()) {
1525 return NS_ERROR_NOT_INITIALIZED;
1527 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1528 if (NS_FAILED(rv)) {
1529 return rv;
1531 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1533 int flags = mFlags;
1534 if (aReadOnly) {
1535 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1536 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1537 // Turn off SQLITE_OPEN_CREATE.
1538 flags = (~SQLITE_OPEN_CREATE & flags);
1541 // The cloned connection will still implement the synchronous API, but throw
1542 // if any synchronous methods are called on the main thread.
1543 RefPtr<Connection> clone =
1544 new Connection(mStorageService, flags, ASYNCHRONOUS);
1546 RefPtr<AsyncInitializeClone> initEvent =
1547 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1548 // Dispatch to our async thread, since the originating connection must remain
1549 // valid and open for the whole cloning process. This also ensures we are
1550 // properly serialized with a `close` operation, rather than race with it.
1551 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1552 if (!target) {
1553 return NS_ERROR_UNEXPECTED;
1555 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1558 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1559 nsresult rv;
1560 if (!mStorageKey.IsEmpty()) {
1561 rv = aClone->initialize(mStorageKey, mName);
1562 } else if (mFileURL) {
1563 rv = aClone->initialize(mFileURL, mTelemetryFilename);
1564 } else {
1565 rv = aClone->initialize(mDatabaseFile);
1567 if (NS_FAILED(rv)) {
1568 return rv;
1571 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1573 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1574 NS_ENSURE_SUCCESS(rv, rv);
1576 // Re-attach on-disk databases that were attached to the original connection.
1578 nsCOMPtr<mozIStorageStatement> stmt;
1579 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1580 MOZ_ASSERT(NS_SUCCEEDED(rv));
1581 bool hasResult = false;
1582 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1583 nsAutoCString name;
1584 rv = stmt->GetUTF8String(1, name);
1585 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1586 !name.EqualsLiteral("temp")) {
1587 nsCString path;
1588 rv = stmt->GetUTF8String(2, path);
1589 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1590 nsCOMPtr<mozIStorageStatement> attachStmt;
1591 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1592 getter_AddRefs(attachStmt));
1593 MOZ_ASSERT(NS_SUCCEEDED(rv));
1594 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1595 MOZ_ASSERT(NS_SUCCEEDED(rv));
1596 rv = attachStmt->Execute();
1597 MOZ_ASSERT(NS_SUCCEEDED(rv),
1598 "couldn't re-attach database to cloned connection");
1604 // Copy over pragmas from the original connection.
1605 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1606 // schema ("main" and any other attached databases), and this implmentation
1607 // fails to propagate them. This is being addressed on trunk.
1608 static const char* pragmas[] = {
1609 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1610 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1611 for (auto& pragma : pragmas) {
1612 // Read-only connections just need cache_size and temp_store pragmas.
1613 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1614 ::strcmp(pragma, "temp_store") != 0) {
1615 continue;
1618 nsAutoCString pragmaQuery("PRAGMA ");
1619 pragmaQuery.Append(pragma);
1620 nsCOMPtr<mozIStorageStatement> stmt;
1621 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
1622 MOZ_ASSERT(NS_SUCCEEDED(rv));
1623 bool hasResult = false;
1624 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1625 pragmaQuery.AppendLiteral(" = ");
1626 pragmaQuery.AppendInt(stmt->AsInt32(0));
1627 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
1628 MOZ_ASSERT(NS_SUCCEEDED(rv));
1632 // Copy over temporary tables, triggers, and views from the original
1633 // connections. Entities in `sqlite_temp_master` are only visible to the
1634 // connection that created them.
1635 if (!aReadOnly) {
1636 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
1637 NS_ENSURE_SUCCESS(rv, rv);
1639 nsCOMPtr<mozIStorageStatement> stmt;
1640 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1641 "WHERE type IN ('table', 'view', "
1642 "'index', 'trigger')"),
1643 getter_AddRefs(stmt));
1644 // Propagate errors, because failing to copy triggers might cause schema
1645 // coherency issues when writing to the database from the cloned connection.
1646 NS_ENSURE_SUCCESS(rv, rv);
1647 bool hasResult = false;
1648 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1649 nsAutoCString query;
1650 rv = stmt->GetUTF8String(0, query);
1651 NS_ENSURE_SUCCESS(rv, rv);
1653 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1654 // keyword. We need to add it back, or we'll recreate temporary entities
1655 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1656 // statements, but those don't need `TEMP` keywords.
1657 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
1658 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
1659 StringBeginsWith(query, "CREATE VIEW "_ns)) {
1660 query.Replace(0, 6, "CREATE TEMP");
1663 rv = aClone->ExecuteSimpleSQL(query);
1664 NS_ENSURE_SUCCESS(rv, rv);
1667 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
1668 NS_ENSURE_SUCCESS(rv, rv);
1671 // Copy any functions that have been added to this connection.
1672 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1673 for (const auto& entry : mFunctions) {
1674 const nsACString& key = entry.GetKey();
1675 Connection::FunctionInfo data = entry.GetData();
1677 rv = aClone->CreateFunction(key, data.numArgs, data.function);
1678 if (NS_FAILED(rv)) {
1679 NS_WARNING("Failed to copy function to cloned connection");
1683 guard.release();
1684 return NS_OK;
1687 NS_IMETHODIMP
1688 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
1689 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1691 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
1693 if (!connectionReady()) {
1694 return NS_ERROR_NOT_INITIALIZED;
1696 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1697 if (NS_FAILED(rv)) {
1698 return rv;
1701 int flags = mFlags;
1702 if (aReadOnly) {
1703 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1704 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1705 // Turn off SQLITE_OPEN_CREATE.
1706 flags = (~SQLITE_OPEN_CREATE & flags);
1709 RefPtr<Connection> clone = new Connection(
1710 mStorageService, flags, mSupportedOperations, mInterruptible);
1712 rv = initializeClone(clone, aReadOnly);
1713 if (NS_FAILED(rv)) {
1714 return rv;
1717 NS_IF_ADDREF(*_connection = clone);
1718 return NS_OK;
1721 NS_IMETHODIMP
1722 Connection::Interrupt() {
1723 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
1724 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
1725 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1726 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
1727 IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1729 if (!connectionReady()) {
1730 return NS_ERROR_NOT_INITIALIZED;
1733 if (isClosing()) { // Closing already in asynchronous case
1734 return NS_OK;
1738 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
1739 // it is not safe to call sqlite3_interrupt() when
1740 // database connection is closed or might close before
1741 // sqlite3_interrupt() returns.
1742 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1743 if (!isClosed(lockedScope)) {
1744 MOZ_ASSERT(mDBConn);
1745 ::sqlite3_interrupt(mDBConn);
1749 return NS_OK;
1752 NS_IMETHODIMP
1753 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
1754 *_defaultPageSize = Service::kDefaultPageSize;
1755 return NS_OK;
1758 NS_IMETHODIMP
1759 Connection::GetConnectionReady(bool* _ready) {
1760 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1761 *_ready = connectionReady();
1762 return NS_OK;
1765 NS_IMETHODIMP
1766 Connection::GetDatabaseFile(nsIFile** _dbFile) {
1767 if (!connectionReady()) {
1768 return NS_ERROR_NOT_INITIALIZED;
1770 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1771 if (NS_FAILED(rv)) {
1772 return rv;
1775 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
1777 return NS_OK;
1780 NS_IMETHODIMP
1781 Connection::GetLastInsertRowID(int64_t* _id) {
1782 if (!connectionReady()) {
1783 return NS_ERROR_NOT_INITIALIZED;
1785 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1786 if (NS_FAILED(rv)) {
1787 return rv;
1790 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
1791 *_id = id;
1793 return NS_OK;
1796 NS_IMETHODIMP
1797 Connection::GetAffectedRows(int32_t* _rows) {
1798 if (!connectionReady()) {
1799 return NS_ERROR_NOT_INITIALIZED;
1801 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1802 if (NS_FAILED(rv)) {
1803 return rv;
1806 *_rows = ::sqlite3_changes(mDBConn);
1808 return NS_OK;
1811 NS_IMETHODIMP
1812 Connection::GetLastError(int32_t* _error) {
1813 if (!connectionReady()) {
1814 return NS_ERROR_NOT_INITIALIZED;
1816 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1817 if (NS_FAILED(rv)) {
1818 return rv;
1821 *_error = ::sqlite3_errcode(mDBConn);
1823 return NS_OK;
1826 NS_IMETHODIMP
1827 Connection::GetLastErrorString(nsACString& _errorString) {
1828 if (!connectionReady()) {
1829 return NS_ERROR_NOT_INITIALIZED;
1831 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1832 if (NS_FAILED(rv)) {
1833 return rv;
1836 const char* serr = ::sqlite3_errmsg(mDBConn);
1837 _errorString.Assign(serr);
1839 return NS_OK;
1842 NS_IMETHODIMP
1843 Connection::GetSchemaVersion(int32_t* _version) {
1844 if (!connectionReady()) {
1845 return NS_ERROR_NOT_INITIALIZED;
1847 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1848 if (NS_FAILED(rv)) {
1849 return rv;
1852 nsCOMPtr<mozIStorageStatement> stmt;
1853 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
1854 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
1856 *_version = 0;
1857 bool hasResult;
1858 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult)
1859 *_version = stmt->AsInt32(0);
1861 return NS_OK;
1864 NS_IMETHODIMP
1865 Connection::SetSchemaVersion(int32_t aVersion) {
1866 if (!connectionReady()) {
1867 return NS_ERROR_NOT_INITIALIZED;
1869 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1870 if (NS_FAILED(rv)) {
1871 return rv;
1874 nsAutoCString stmt("PRAGMA user_version = "_ns);
1875 stmt.AppendInt(aVersion);
1877 return ExecuteSimpleSQL(stmt);
1880 NS_IMETHODIMP
1881 Connection::CreateStatement(const nsACString& aSQLStatement,
1882 mozIStorageStatement** _stmt) {
1883 NS_ENSURE_ARG_POINTER(_stmt);
1884 if (!connectionReady()) {
1885 return NS_ERROR_NOT_INITIALIZED;
1887 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1888 if (NS_FAILED(rv)) {
1889 return rv;
1892 RefPtr<Statement> statement(new Statement());
1893 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1895 rv = statement->initialize(this, mDBConn, aSQLStatement);
1896 NS_ENSURE_SUCCESS(rv, rv);
1898 Statement* rawPtr;
1899 statement.forget(&rawPtr);
1900 *_stmt = rawPtr;
1901 return NS_OK;
1904 NS_IMETHODIMP
1905 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
1906 mozIStorageAsyncStatement** _stmt) {
1907 NS_ENSURE_ARG_POINTER(_stmt);
1908 if (!connectionReady()) {
1909 return NS_ERROR_NOT_INITIALIZED;
1911 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1912 if (NS_FAILED(rv)) {
1913 return rv;
1916 RefPtr<AsyncStatement> statement(new AsyncStatement());
1917 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1919 rv = statement->initialize(this, mDBConn, aSQLStatement);
1920 NS_ENSURE_SUCCESS(rv, rv);
1922 AsyncStatement* rawPtr;
1923 statement.forget(&rawPtr);
1924 *_stmt = rawPtr;
1925 return NS_OK;
1928 NS_IMETHODIMP
1929 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
1930 CHECK_MAINTHREAD_ABUSE();
1931 if (!connectionReady()) {
1932 return NS_ERROR_NOT_INITIALIZED;
1934 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1935 if (NS_FAILED(rv)) {
1936 return rv;
1939 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
1940 return convertResultCode(srv);
1943 NS_IMETHODIMP
1944 Connection::ExecuteAsync(
1945 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
1946 mozIStorageStatementCallback* aCallback,
1947 mozIStoragePendingStatement** _handle) {
1948 nsTArray<StatementData> stmts(aStatements.Length());
1949 for (uint32_t i = 0; i < aStatements.Length(); i++) {
1950 nsCOMPtr<StorageBaseStatementInternal> stmt =
1951 do_QueryInterface(aStatements[i]);
1952 NS_ENSURE_STATE(stmt);
1954 // Obtain our StatementData.
1955 StatementData data;
1956 nsresult rv = stmt->getAsynchronousStatementData(data);
1957 NS_ENSURE_SUCCESS(rv, rv);
1959 NS_ASSERTION(stmt->getOwner() == this,
1960 "Statement must be from this database connection!");
1962 // Now append it to our array.
1963 stmts.AppendElement(data);
1966 // Dispatch to the background
1967 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
1968 aCallback, _handle);
1971 NS_IMETHODIMP
1972 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
1973 mozIStorageStatementCallback* aCallback,
1974 mozIStoragePendingStatement** _handle) {
1975 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1977 nsCOMPtr<mozIStorageAsyncStatement> stmt;
1978 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
1979 if (NS_FAILED(rv)) {
1980 return rv;
1983 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
1984 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
1985 if (NS_FAILED(rv)) {
1986 return rv;
1989 pendingStatement.forget(_handle);
1990 return rv;
1993 NS_IMETHODIMP
1994 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
1995 return databaseElementExists(TABLE, aTableName, _exists);
1998 NS_IMETHODIMP
1999 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2000 return databaseElementExists(INDEX, aIndexName, _exists);
2003 NS_IMETHODIMP
2004 Connection::GetTransactionInProgress(bool* _inProgress) {
2005 if (!connectionReady()) {
2006 return NS_ERROR_NOT_INITIALIZED;
2008 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2009 if (NS_FAILED(rv)) {
2010 return rv;
2013 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2014 *_inProgress = transactionInProgress(lockedScope);
2015 return NS_OK;
2018 NS_IMETHODIMP
2019 Connection::GetDefaultTransactionType(int32_t* _type) {
2020 *_type = mDefaultTransactionType;
2021 return NS_OK;
2024 NS_IMETHODIMP
2025 Connection::SetDefaultTransactionType(int32_t aType) {
2026 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2027 mDefaultTransactionType = aType;
2028 return NS_OK;
2031 NS_IMETHODIMP
2032 Connection::GetVariableLimit(int32_t* _limit) {
2033 if (!connectionReady()) {
2034 return NS_ERROR_NOT_INITIALIZED;
2036 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2037 if (limit < 0) {
2038 return NS_ERROR_UNEXPECTED;
2040 *_limit = limit;
2041 return NS_OK;
2044 NS_IMETHODIMP
2045 Connection::BeginTransaction() {
2046 if (!connectionReady()) {
2047 return NS_ERROR_NOT_INITIALIZED;
2049 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2050 if (NS_FAILED(rv)) {
2051 return rv;
2054 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2055 return beginTransactionInternal(lockedScope, mDBConn,
2056 mDefaultTransactionType);
2059 nsresult Connection::beginTransactionInternal(
2060 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2061 int32_t aTransactionType) {
2062 if (transactionInProgress(aProofOfLock)) {
2063 return NS_ERROR_FAILURE;
2065 nsresult rv;
2066 switch (aTransactionType) {
2067 case TRANSACTION_DEFERRED:
2068 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2069 break;
2070 case TRANSACTION_IMMEDIATE:
2071 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2072 break;
2073 case TRANSACTION_EXCLUSIVE:
2074 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2075 break;
2076 default:
2077 return NS_ERROR_ILLEGAL_VALUE;
2079 return rv;
2082 NS_IMETHODIMP
2083 Connection::CommitTransaction() {
2084 if (!connectionReady()) {
2085 return NS_ERROR_NOT_INITIALIZED;
2087 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2088 if (NS_FAILED(rv)) {
2089 return rv;
2092 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2093 return commitTransactionInternal(lockedScope, mDBConn);
2096 nsresult Connection::commitTransactionInternal(
2097 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2098 if (!transactionInProgress(aProofOfLock)) {
2099 return NS_ERROR_UNEXPECTED;
2101 nsresult rv =
2102 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2103 return rv;
2106 NS_IMETHODIMP
2107 Connection::RollbackTransaction() {
2108 if (!connectionReady()) {
2109 return NS_ERROR_NOT_INITIALIZED;
2111 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2112 if (NS_FAILED(rv)) {
2113 return rv;
2116 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2117 return rollbackTransactionInternal(lockedScope, mDBConn);
2120 nsresult Connection::rollbackTransactionInternal(
2121 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2122 if (!transactionInProgress(aProofOfLock)) {
2123 return NS_ERROR_UNEXPECTED;
2126 nsresult rv =
2127 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2128 return rv;
2131 NS_IMETHODIMP
2132 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2133 if (!connectionReady()) {
2134 return NS_ERROR_NOT_INITIALIZED;
2136 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2137 if (NS_FAILED(rv)) {
2138 return rv;
2141 SmprintfPointer buf =
2142 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2143 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2145 int srv = executeSql(mDBConn, buf.get());
2147 return convertResultCode(srv);
2150 NS_IMETHODIMP
2151 Connection::CreateFunction(const nsACString& aFunctionName,
2152 int32_t aNumArguments,
2153 mozIStorageFunction* aFunction) {
2154 if (!connectionReady()) {
2155 return NS_ERROR_NOT_INITIALIZED;
2157 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2158 if (NS_FAILED(rv)) {
2159 return rv;
2162 // Check to see if this function is already defined. We only check the name
2163 // because a function can be defined with the same body but different names.
2164 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2165 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2167 int srv = ::sqlite3_create_function(
2168 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2169 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2170 if (srv != SQLITE_OK) return convertResultCode(srv);
2172 FunctionInfo info = {aFunction, aNumArguments};
2173 mFunctions.InsertOrUpdate(aFunctionName, info);
2175 return NS_OK;
2178 NS_IMETHODIMP
2179 Connection::RemoveFunction(const nsACString& aFunctionName) {
2180 if (!connectionReady()) {
2181 return NS_ERROR_NOT_INITIALIZED;
2183 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2184 if (NS_FAILED(rv)) {
2185 return rv;
2188 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2189 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2191 int srv = ::sqlite3_create_function(
2192 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2193 nullptr, nullptr, nullptr, nullptr);
2194 if (srv != SQLITE_OK) return convertResultCode(srv);
2196 mFunctions.Remove(aFunctionName);
2198 return NS_OK;
2201 NS_IMETHODIMP
2202 Connection::SetProgressHandler(int32_t aGranularity,
2203 mozIStorageProgressHandler* aHandler,
2204 mozIStorageProgressHandler** _oldHandler) {
2205 if (!connectionReady()) {
2206 return NS_ERROR_NOT_INITIALIZED;
2208 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2209 if (NS_FAILED(rv)) {
2210 return rv;
2213 // Return previous one
2214 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2215 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2217 if (!aHandler || aGranularity <= 0) {
2218 aHandler = nullptr;
2219 aGranularity = 0;
2221 mProgressHandler = aHandler;
2222 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2224 return NS_OK;
2227 NS_IMETHODIMP
2228 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2229 if (!connectionReady()) {
2230 return NS_ERROR_NOT_INITIALIZED;
2232 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2233 if (NS_FAILED(rv)) {
2234 return rv;
2237 // Return previous one
2238 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2239 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2241 mProgressHandler = nullptr;
2242 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2244 return NS_OK;
2247 NS_IMETHODIMP
2248 Connection::SetGrowthIncrement(int32_t aChunkSize,
2249 const nsACString& aDatabaseName) {
2250 if (!connectionReady()) {
2251 return NS_ERROR_NOT_INITIALIZED;
2253 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2254 if (NS_FAILED(rv)) {
2255 return rv;
2258 // Bug 597215: Disk space is extremely limited on Android
2259 // so don't preallocate space. This is also not effective
2260 // on log structured file systems used by Android devices
2261 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2262 // Don't preallocate if less than 500MiB is available.
2263 int64_t bytesAvailable;
2264 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2265 NS_ENSURE_SUCCESS(rv, rv);
2266 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2267 return NS_ERROR_FILE_TOO_BIG;
2270 (void)::sqlite3_file_control(mDBConn,
2271 aDatabaseName.Length()
2272 ? nsPromiseFlatCString(aDatabaseName).get()
2273 : nullptr,
2274 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2275 #endif
2276 return NS_OK;
2279 NS_IMETHODIMP
2280 Connection::EnableModule(const nsACString& aModuleName) {
2281 if (!connectionReady()) {
2282 return NS_ERROR_NOT_INITIALIZED;
2284 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2285 if (NS_FAILED(rv)) {
2286 return rv;
2289 for (auto& gModule : gModules) {
2290 struct Module* m = &gModule;
2291 if (aModuleName.Equals(m->name)) {
2292 int srv = m->registerFunc(mDBConn, m->name);
2293 if (srv != SQLITE_OK) return convertResultCode(srv);
2295 return NS_OK;
2299 return NS_ERROR_FAILURE;
2302 // Implemented in TelemetryVFS.cpp
2303 already_AddRefed<QuotaObject> GetQuotaObjectForFile(sqlite3_file* pFile);
2305 NS_IMETHODIMP
2306 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2307 QuotaObject** aJournalQuotaObject) {
2308 MOZ_ASSERT(aDatabaseQuotaObject);
2309 MOZ_ASSERT(aJournalQuotaObject);
2311 if (!connectionReady()) {
2312 return NS_ERROR_NOT_INITIALIZED;
2314 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2315 if (NS_FAILED(rv)) {
2316 return rv;
2319 sqlite3_file* file;
2320 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2321 &file);
2322 if (srv != SQLITE_OK) {
2323 return convertResultCode(srv);
2326 RefPtr<QuotaObject> databaseQuotaObject = GetQuotaObjectForFile(file);
2327 if (NS_WARN_IF(!databaseQuotaObject)) {
2328 return NS_ERROR_FAILURE;
2331 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2332 &file);
2333 if (srv != SQLITE_OK) {
2334 return convertResultCode(srv);
2337 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObjectForFile(file);
2338 if (NS_WARN_IF(!journalQuotaObject)) {
2339 return NS_ERROR_FAILURE;
2342 databaseQuotaObject.forget(aDatabaseQuotaObject);
2343 journalQuotaObject.forget(aJournalQuotaObject);
2344 return NS_OK;
2347 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2349 uint32_t Connection::GetTransactionNestingLevel(
2350 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2351 return mTransactionNestingLevel;
2354 uint32_t Connection::IncreaseTransactionNestingLevel(
2355 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2356 return ++mTransactionNestingLevel;
2359 uint32_t Connection::DecreaseTransactionNestingLevel(
2360 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2361 return --mTransactionNestingLevel;
2364 } // namespace mozilla::storage