Bug 1793654 [wpt PR 36261] - Update wpt metadata, a=testonly
[gecko.git] / storage / mozStorageConnection.cpp
blob8335a78144d632220345cf44fd1a72e5d864f52b
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 #ifdef XP_WIN
721 static const char* sIgnoreLockingVFS = "win32-none";
722 #else
723 static const char* sIgnoreLockingVFS = "unix-none";
724 #endif
726 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
727 int srv;
728 if (mIgnoreLockingMode) {
729 exclusive = false;
730 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
731 sIgnoreLockingVFS);
732 } else {
733 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
734 GetTelemetryVFSName(exclusive));
735 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
736 // Retry without trying to get an exclusive lock.
737 exclusive = false;
738 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
739 mFlags, GetTelemetryVFSName(false));
742 if (srv != SQLITE_OK) {
743 mDBConn = nullptr;
744 rv = convertResultCode(srv);
745 RecordOpenStatus(rv);
746 return rv;
749 rv = initializeInternal();
750 if (exclusive &&
751 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
752 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
753 // some cases it may successfully open the database and then lock on the
754 // first query execution. When initializeInternal fails it closes the
755 // connection, so we can try to restart it in non-exclusive mode.
756 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
757 GetTelemetryVFSName(false));
758 if (srv == SQLITE_OK) {
759 rv = initializeInternal();
763 RecordOpenStatus(rv);
764 NS_ENSURE_SUCCESS(rv, rv);
766 return NS_OK;
769 nsresult Connection::initialize(nsIFileURL* aFileURL,
770 const nsACString& aTelemetryFilename) {
771 NS_ASSERTION(aFileURL, "Passed null file URL!");
772 NS_ASSERTION(!connectionReady(),
773 "Initialize called on already opened database!");
774 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
776 nsCOMPtr<nsIFile> databaseFile;
777 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
778 NS_ENSURE_SUCCESS(rv, rv);
780 // Set both mDatabaseFile and mFileURL here.
781 mFileURL = aFileURL;
782 mDatabaseFile = databaseFile;
784 if (!aTelemetryFilename.IsEmpty()) {
785 mTelemetryFilename = aTelemetryFilename;
786 } else {
787 databaseFile->GetNativeLeafName(mTelemetryFilename);
790 nsAutoCString spec;
791 rv = aFileURL->GetSpec(spec);
792 NS_ENSURE_SUCCESS(rv, rv);
794 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
796 // If there is a key specified, we need to use the obfuscating VFS.
797 nsAutoCString query;
798 rv = aFileURL->GetQuery(query);
799 NS_ENSURE_SUCCESS(rv, rv);
800 const char* const vfs =
801 URLParams::Parse(query,
802 [](const nsAString& aName, const nsAString& aValue) {
803 return aName.EqualsLiteral("key");
805 ? GetObfuscatingVFSName()
806 : GetTelemetryVFSName(exclusive);
807 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
808 if (srv != SQLITE_OK) {
809 mDBConn = nullptr;
810 rv = convertResultCode(srv);
811 RecordOpenStatus(rv);
812 return rv;
815 rv = initializeInternal();
816 RecordOpenStatus(rv);
817 NS_ENSURE_SUCCESS(rv, rv);
819 return NS_OK;
822 nsresult Connection::initializeInternal() {
823 MOZ_ASSERT(mDBConn);
824 auto guard = MakeScopeExit([&]() { initializeFailed(); });
826 mConnectionClosed = false;
828 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
829 DebugOnly<int> srv2 =
830 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
831 MOZ_ASSERT(srv2 == SQLITE_OK,
832 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
833 #endif
835 MOZ_ASSERT(!mTelemetryFilename.IsEmpty(),
836 "A telemetry filename should have been set by now.");
838 // Properly wrap the database handle's mutex.
839 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
841 // SQLite tracing can slow down queries (especially long queries)
842 // significantly. Don't trace unless the user is actively monitoring SQLite.
843 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
844 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
845 tracefunc, this);
847 MOZ_LOG(
848 gStorageLog, LogLevel::Debug,
849 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
852 int64_t pageSize = Service::kDefaultPageSize;
854 // Set page_size to the preferred default value. This is effective only if
855 // the database has just been created, otherwise, if the database does not
856 // use WAL journal mode, a VACUUM operation will updated its page_size.
857 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
858 "PRAGMA page_size = ");
859 pageSizeQuery.AppendInt(pageSize);
860 int srv = executeSql(mDBConn, pageSizeQuery.get());
861 if (srv != SQLITE_OK) {
862 return convertResultCode(srv);
865 // Setting the cache_size forces the database open, verifying if it is valid
866 // or corrupt. So this is executed regardless it being actually needed.
867 // The cache_size is calculated from the actual page_size, to save memory.
868 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
869 "PRAGMA cache_size = ");
870 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
871 srv = executeSql(mDBConn, cacheSizeQuery.get());
872 if (srv != SQLITE_OK) {
873 return convertResultCode(srv);
876 // Register our built-in SQL functions.
877 srv = registerFunctions(mDBConn);
878 if (srv != SQLITE_OK) {
879 return convertResultCode(srv);
882 // Register our built-in SQL collating sequences.
883 srv = registerCollations(mDBConn, mStorageService);
884 if (srv != SQLITE_OK) {
885 return convertResultCode(srv);
888 // Set the default synchronous value. Each consumer can switch this
889 // accordingly to their needs.
890 #if defined(ANDROID)
891 // Android prefers synchronous = OFF for performance reasons.
892 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
893 #else
894 // Normal is the suggested value for WAL journals.
895 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
896 #endif
898 // Initialization succeeded, we can stop guarding for failures.
899 guard.release();
900 return NS_OK;
903 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
904 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
905 nsresult rv = aStorageFile
906 ? initialize(aStorageFile)
907 : initialize(kMozStorageMemoryStorageKey, VoidCString());
908 if (NS_FAILED(rv)) {
909 // Shutdown the async thread, since initialization failed.
910 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
911 mAsyncExecutionThreadShuttingDown = true;
912 nsCOMPtr<nsIRunnable> event =
913 NewRunnableMethod("Connection::shutdownAsyncThread", this,
914 &Connection::shutdownAsyncThread);
915 Unused << NS_DispatchToMainThread(event);
917 return rv;
920 void Connection::initializeFailed() {
922 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
923 mConnectionClosed = true;
925 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
926 mDBConn = nullptr;
927 sharedDBMutex.destroy();
930 nsresult Connection::databaseElementExists(
931 enum DatabaseElementType aElementType, const nsACString& aElementName,
932 bool* _exists) {
933 if (!connectionReady()) {
934 return NS_ERROR_NOT_AVAILABLE;
936 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
937 if (NS_FAILED(rv)) {
938 return rv;
941 // When constructing the query, make sure to SELECT the correct db's
942 // sqlite_master if the user is prefixing the element with a specific db. ex:
943 // sample.test
944 nsCString query("SELECT name FROM (SELECT * FROM ");
945 nsDependentCSubstring element;
946 int32_t ind = aElementName.FindChar('.');
947 if (ind == kNotFound) {
948 element.Assign(aElementName);
949 } else {
950 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
951 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
952 query.Append(db);
954 query.AppendLiteral(
955 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
956 "'");
958 switch (aElementType) {
959 case INDEX:
960 query.AppendLiteral("index");
961 break;
962 case TABLE:
963 query.AppendLiteral("table");
964 break;
966 query.AppendLiteral("' AND name ='");
967 query.Append(element);
968 query.Append('\'');
970 sqlite3_stmt* stmt;
971 int srv = prepareStatement(mDBConn, query, &stmt);
972 if (srv != SQLITE_OK) {
973 RecordQueryStatus(srv);
974 return convertResultCode(srv);
977 srv = stepStatement(mDBConn, stmt);
978 // we just care about the return value from step
979 (void)::sqlite3_finalize(stmt);
981 RecordQueryStatus(srv);
983 if (srv == SQLITE_ROW) {
984 *_exists = true;
985 return NS_OK;
987 if (srv == SQLITE_DONE) {
988 *_exists = false;
989 return NS_OK;
992 return convertResultCode(srv);
995 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
996 sharedDBMutex.assertCurrentThreadOwns();
998 for (const auto& data : mFunctions.Values()) {
999 if (data.function == aInstance) {
1000 return true;
1003 return false;
1006 /* static */
1007 int Connection::sProgressHelper(void* aArg) {
1008 Connection* _this = static_cast<Connection*>(aArg);
1009 return _this->progressHandler();
1012 int Connection::progressHandler() {
1013 sharedDBMutex.assertCurrentThreadOwns();
1014 if (mProgressHandler) {
1015 bool result;
1016 nsresult rv = mProgressHandler->OnProgress(this, &result);
1017 if (NS_FAILED(rv)) return 0; // Don't break request
1018 return result ? 1 : 0;
1020 return 0;
1023 nsresult Connection::setClosedState() {
1024 // Flag that we are shutting down the async thread, so that
1025 // getAsyncExecutionTarget knows not to expose/create the async thread.
1026 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1027 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1029 mAsyncExecutionThreadShuttingDown = true;
1031 // Set the property to null before closing the connection, otherwise the
1032 // other functions in the module may try to use the connection after it is
1033 // closed.
1034 mDBConn = nullptr;
1036 return NS_OK;
1039 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1040 if (aOperationType == ASYNCHRONOUS) {
1041 // Async operations are supported for all connections, on any thread.
1042 return true;
1044 // Sync operations are supported for sync connections (on any thread), and
1045 // async connections on a background thread.
1046 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1047 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1050 nsresult Connection::ensureOperationSupported(
1051 ConnectionOperation aOperationType) {
1052 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1053 #ifdef DEBUG
1054 if (NS_IsMainThread()) {
1055 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1056 Unused << xpc->DebugDumpJSStack(false, false, false);
1058 #endif
1059 MOZ_ASSERT(false,
1060 "Don't use async connections synchronously on the main thread");
1061 return NS_ERROR_NOT_AVAILABLE;
1063 return NS_OK;
1066 bool Connection::isConnectionReadyOnThisThread() {
1067 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1068 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1069 return true;
1071 return connectionReady();
1074 bool Connection::isClosing() {
1075 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1076 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1079 bool Connection::isClosed() {
1080 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1081 return mConnectionClosed;
1084 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1086 bool Connection::isAsyncExecutionThreadAvailable() {
1087 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1088 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1091 void Connection::shutdownAsyncThread() {
1092 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1093 MOZ_ASSERT(mAsyncExecutionThread);
1094 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1096 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1097 mAsyncExecutionThread = nullptr;
1100 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1101 #ifdef DEBUG
1102 { // Make sure we have marked our async thread as shutting down.
1103 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1104 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1105 "Did not call setClosedState!");
1106 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1108 #endif // DEBUG
1110 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1111 nsAutoCString leafName(":memory");
1112 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1113 MOZ_LOG(gStorageLog, LogLevel::Debug,
1114 ("Closing connection to '%s'", leafName.get()));
1117 // At this stage, we may still have statements that need to be
1118 // finalized. Attempt to close the database connection. This will
1119 // always disconnect any virtual tables and cleanly finalize their
1120 // internal statements. Once this is done, closing may fail due to
1121 // unfinalized client statements, in which case we need to finalize
1122 // these statements and close again.
1124 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1125 mConnectionClosed = true;
1128 // Nothing else needs to be done if we don't have a connection here.
1129 if (!aNativeConnection) return NS_OK;
1131 int srv = ::sqlite3_close(aNativeConnection);
1133 if (srv == SQLITE_BUSY) {
1135 // Nothing else should change the connection or statements status until we
1136 // are done here.
1137 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1138 // We still have non-finalized statements. Finalize them.
1139 sqlite3_stmt* stmt = nullptr;
1140 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1141 MOZ_LOG(gStorageLog, LogLevel::Debug,
1142 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1143 stmt));
1145 #ifdef DEBUG
1146 SmprintfPointer msg = ::mozilla::Smprintf(
1147 "SQL statement '%s' (%p) should have been finalized before closing "
1148 "the connection",
1149 ::sqlite3_sql(stmt), stmt);
1150 NS_WARNING(msg.get());
1151 #endif // DEBUG
1153 srv = ::sqlite3_finalize(stmt);
1155 #ifdef DEBUG
1156 if (srv != SQLITE_OK) {
1157 SmprintfPointer msg = ::mozilla::Smprintf(
1158 "Could not finalize SQL statement (%p)", stmt);
1159 NS_WARNING(msg.get());
1161 #endif // DEBUG
1163 // Ensure that the loop continues properly, whether closing has
1164 // succeeded or not.
1165 if (srv == SQLITE_OK) {
1166 stmt = nullptr;
1169 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1170 // again, since Sqlite will try to acquire it.
1173 // Now that all statements have been finalized, we
1174 // should be able to close.
1175 srv = ::sqlite3_close(aNativeConnection);
1176 MOZ_ASSERT(false,
1177 "Had to forcibly close the database connection because not all "
1178 "the statements have been finalized.");
1181 if (srv == SQLITE_OK) {
1182 sharedDBMutex.destroy();
1183 } else {
1184 MOZ_ASSERT(false,
1185 "sqlite3_close failed. There are probably outstanding "
1186 "statements that are listed above!");
1189 return convertResultCode(srv);
1192 nsCString Connection::getFilename() { return mTelemetryFilename; }
1194 int Connection::stepStatement(sqlite3* aNativeConnection,
1195 sqlite3_stmt* aStatement) {
1196 MOZ_ASSERT(aStatement);
1198 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1199 ::sqlite3_sql(aStatement));
1201 bool checkedMainThread = false;
1202 TimeStamp startTime = TimeStamp::Now();
1204 // The connection may have been closed if the executing statement has been
1205 // created and cached after a call to asyncClose() but before the actual
1206 // sqlite3_close(). This usually happens when other tasks using cached
1207 // statements are asynchronously scheduled for execution and any of them ends
1208 // up after asyncClose. See bug 728653 for details.
1209 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1211 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1213 int srv;
1214 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1215 if (!checkedMainThread) {
1216 checkedMainThread = true;
1217 if (::NS_IsMainThread()) {
1218 NS_WARNING("We won't allow blocking on the main thread!");
1219 break;
1223 srv = WaitForUnlockNotify(aNativeConnection);
1224 if (srv != SQLITE_OK) {
1225 break;
1228 ::sqlite3_reset(aStatement);
1231 // Report very slow SQL statements to Telemetry
1232 TimeDuration duration = TimeStamp::Now() - startTime;
1233 const uint32_t threshold = NS_IsMainThread()
1234 ? Telemetry::kSlowSQLThresholdForMainThread
1235 : Telemetry::kSlowSQLThresholdForHelperThreads;
1236 if (duration.ToMilliseconds() >= threshold) {
1237 nsDependentCString statementString(::sqlite3_sql(aStatement));
1238 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1239 duration.ToMilliseconds());
1242 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1243 // Drop off the extended result bits of the result code.
1244 return srv & 0xFF;
1247 int Connection::prepareStatement(sqlite3* aNativeConnection,
1248 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1249 // We should not even try to prepare statements after the connection has
1250 // been closed.
1251 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1253 bool checkedMainThread = false;
1255 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1257 int srv;
1258 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1259 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1260 if (!checkedMainThread) {
1261 checkedMainThread = true;
1262 if (::NS_IsMainThread()) {
1263 NS_WARNING("We won't allow blocking on the main thread!");
1264 break;
1268 srv = WaitForUnlockNotify(aNativeConnection);
1269 if (srv != SQLITE_OK) {
1270 break;
1274 if (srv != SQLITE_OK) {
1275 nsCString warnMsg;
1276 warnMsg.AppendLiteral("The SQL statement '");
1277 warnMsg.Append(aSQL);
1278 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1279 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1281 #ifdef DEBUG
1282 NS_WARNING(warnMsg.get());
1283 #endif
1284 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1287 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1288 // Drop off the extended result bits of the result code.
1289 int rc = srv & 0xFF;
1290 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1291 // The callers of this function are used to only checking the return value,
1292 // so it is safer to return an error code.
1293 if (rc == SQLITE_OK && *_stmt == nullptr) {
1294 return SQLITE_MISUSE;
1297 return rc;
1300 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1301 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1303 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1305 TimeStamp startTime = TimeStamp::Now();
1306 int srv =
1307 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1308 RecordQueryStatus(srv);
1310 // Report very slow SQL statements to Telemetry
1311 TimeDuration duration = TimeStamp::Now() - startTime;
1312 const uint32_t threshold = NS_IsMainThread()
1313 ? Telemetry::kSlowSQLThresholdForMainThread
1314 : Telemetry::kSlowSQLThresholdForHelperThreads;
1315 if (duration.ToMilliseconds() >= threshold) {
1316 nsDependentCString statementString(aSqlString);
1317 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1318 duration.ToMilliseconds());
1321 return srv;
1324 ////////////////////////////////////////////////////////////////////////////////
1325 //// nsIInterfaceRequestor
1327 NS_IMETHODIMP
1328 Connection::GetInterface(const nsIID& aIID, void** _result) {
1329 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1330 nsIEventTarget* background = getAsyncExecutionTarget();
1331 NS_IF_ADDREF(background);
1332 *_result = background;
1333 return NS_OK;
1335 return NS_ERROR_NO_INTERFACE;
1338 ////////////////////////////////////////////////////////////////////////////////
1339 //// mozIStorageConnection
1341 NS_IMETHODIMP
1342 Connection::Close() {
1343 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1344 if (NS_FAILED(rv)) {
1345 return rv;
1347 return synchronousClose();
1350 nsresult Connection::synchronousClose() {
1351 if (!connectionReady()) {
1352 return NS_ERROR_NOT_INITIALIZED;
1355 #ifdef DEBUG
1356 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1357 // event target. We make this check outside of debug code below in
1358 // setClosedState, but this is here to be explicit.
1359 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1360 #endif // DEBUG
1362 // Make sure we have not executed any asynchronous statements.
1363 // If this fails, the mDBConn may be left open, resulting in a leak.
1364 // We'll try to finalize the pending statements and close the connection.
1365 if (isAsyncExecutionThreadAvailable()) {
1366 #ifdef DEBUG
1367 if (NS_IsMainThread()) {
1368 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1369 Unused << xpc->DebugDumpJSStack(false, false, false);
1371 #endif
1372 MOZ_ASSERT(false,
1373 "Close() was invoked on a connection that executed asynchronous "
1374 "statements. "
1375 "Should have used asyncClose().");
1376 // Try to close the database regardless, to free up resources.
1377 Unused << SpinningSynchronousClose();
1378 return NS_ERROR_UNEXPECTED;
1381 // setClosedState nullifies our connection pointer, so we take a raw pointer
1382 // off it, to pass it through the close procedure.
1383 sqlite3* nativeConn = mDBConn;
1384 nsresult rv = setClosedState();
1385 NS_ENSURE_SUCCESS(rv, rv);
1387 return internalClose(nativeConn);
1390 NS_IMETHODIMP
1391 Connection::SpinningSynchronousClose() {
1392 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1393 if (NS_FAILED(rv)) {
1394 return rv;
1396 if (!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
1397 return NS_ERROR_NOT_SAME_THREAD;
1400 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1401 // Our only existing caller will never have called close; assert if misused
1402 // so that no new callers assume this works after an AsyncClose.
1403 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1404 if (!connectionReady()) {
1405 return NS_ERROR_UNEXPECTED;
1408 RefPtr<CloseListener> listener = new CloseListener();
1409 rv = AsyncClose(listener);
1410 NS_ENSURE_SUCCESS(rv, rv);
1411 MOZ_ALWAYS_TRUE(
1412 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,
1413 [&]() { return listener->mClosed; }));
1414 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1416 return rv;
1419 NS_IMETHODIMP
1420 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1421 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1422 // Check if AsyncClose or Close were already invoked.
1423 if (!connectionReady()) {
1424 return NS_ERROR_NOT_INITIALIZED;
1426 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1427 if (NS_FAILED(rv)) {
1428 return rv;
1431 // The two relevant factors at this point are whether we have a database
1432 // connection and whether we have an async execution thread. Here's what the
1433 // states mean and how we handle them:
1435 // - (mDBConn && asyncThread): The expected case where we are either an
1436 // async connection or a sync connection that has been used asynchronously.
1437 // Either way the caller must call us and not Close(). Nothing surprising
1438 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1439 // async thread.
1441 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1442 // opened the connection synchronously and was planning to use it
1443 // asynchronously, but never got around to using it asynchronously before
1444 // needing to shutdown. This has been observed to happen for the cookie
1445 // service in a case where Firefox shuts itself down almost immediately
1446 // after startup (for unknown reasons). In the Firefox shutdown case,
1447 // we may also fail to create a new async execution thread if one does not
1448 // already exist. (nsThreadManager will refuse to create new threads when
1449 // it has already been told to shutdown.) As such, we need to handle a
1450 // failure to create the async execution thread by falling back to
1451 // synchronous Close() and also dispatching the completion callback because
1452 // at least Places likes to spin a nested event loop that depends on the
1453 // callback being invoked.
1455 // Note that we have considered not trying to spin up the async execution
1456 // thread in this case if it does not already exist, but the overhead of
1457 // thread startup (if successful) is significantly less expensive than the
1458 // worst-case potential I/O hit of synchronously closing a database when we
1459 // could close it asynchronously.
1461 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1462 // OpenAsyncDatabase encountered a problem opening the database. If it
1463 // happened in all cases AsyncInitDatabase would just shut down the thread
1464 // directly and we would avoid this case. But it doesn't, so for simplicity
1465 // and consistency AsyncCloseConnection knows how to handle this and we
1466 // act like this was the (mDBConn && asyncThread) case in this method.
1468 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1469 // Close() or AsyncClose() has already been called (at least) once. This is
1470 // undeniably a misuse case by the caller. We could optimize for this
1471 // case by adding an additional check of mAsyncExecutionThread without using
1472 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1473 // shut it down. But this complicates the method for broken caller code
1474 // whereas we're still correct and safe without the special-case.
1475 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1477 // Create our callback event if we were given a callback. This will
1478 // eventually be dispatched in all cases, even if we fall back to Close() and
1479 // the database wasn't open and we return an error. The rationale is that
1480 // no existing consumer checks our return value and several of them like to
1481 // spin nested event loops until the callback fires. Given that, it seems
1482 // preferable for us to dispatch the callback in all cases. (Except the
1483 // wrong thread misuse case we bailed on up above. But that's okay because
1484 // that is statically wrong whereas these edge cases are dynamic.)
1485 nsCOMPtr<nsIRunnable> completeEvent;
1486 if (aCallback) {
1487 completeEvent = newCompletionEvent(aCallback);
1490 if (!asyncThread) {
1491 // We were unable to create an async thread, so we need to fall back to
1492 // using normal Close(). Since there is no async thread, Close() will
1493 // not complain about that. (Close() may, however, complain if the
1494 // connection is closed, but that's okay.)
1495 if (completeEvent) {
1496 // Closing the database is more important than returning an error code
1497 // about a failure to dispatch, especially because all existing native
1498 // callers ignore our return value.
1499 Unused << NS_DispatchToMainThread(completeEvent.forget());
1501 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1502 // Return a success inconditionally here, since Close() is unlikely to fail
1503 // and we want to reassure the consumer that its callback will be invoked.
1504 return NS_OK;
1507 // setClosedState nullifies our connection pointer, so we take a raw pointer
1508 // off it, to pass it through the close procedure.
1509 sqlite3* nativeConn = mDBConn;
1510 rv = setClosedState();
1511 NS_ENSURE_SUCCESS(rv, rv);
1513 // Create and dispatch our close event to the background thread.
1514 nsCOMPtr<nsIRunnable> closeEvent =
1515 new AsyncCloseConnection(this, nativeConn, completeEvent);
1516 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1517 NS_ENSURE_SUCCESS(rv, rv);
1519 return NS_OK;
1522 NS_IMETHODIMP
1523 Connection::AsyncClone(bool aReadOnly,
1524 mozIStorageCompletionCallback* aCallback) {
1525 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1527 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1528 if (!connectionReady()) {
1529 return NS_ERROR_NOT_INITIALIZED;
1531 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1532 if (NS_FAILED(rv)) {
1533 return rv;
1535 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1537 int flags = mFlags;
1538 if (aReadOnly) {
1539 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1540 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1541 // Turn off SQLITE_OPEN_CREATE.
1542 flags = (~SQLITE_OPEN_CREATE & flags);
1545 // The cloned connection will still implement the synchronous API, but throw
1546 // if any synchronous methods are called on the main thread.
1547 RefPtr<Connection> clone =
1548 new Connection(mStorageService, flags, ASYNCHRONOUS);
1550 RefPtr<AsyncInitializeClone> initEvent =
1551 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1552 // Dispatch to our async thread, since the originating connection must remain
1553 // valid and open for the whole cloning process. This also ensures we are
1554 // properly serialized with a `close` operation, rather than race with it.
1555 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1556 if (!target) {
1557 return NS_ERROR_UNEXPECTED;
1559 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1562 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1563 nsresult rv;
1564 if (!mStorageKey.IsEmpty()) {
1565 rv = aClone->initialize(mStorageKey, mName);
1566 } else if (mFileURL) {
1567 rv = aClone->initialize(mFileURL, mTelemetryFilename);
1568 } else {
1569 rv = aClone->initialize(mDatabaseFile);
1571 if (NS_FAILED(rv)) {
1572 return rv;
1575 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1577 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1578 NS_ENSURE_SUCCESS(rv, rv);
1580 // Re-attach on-disk databases that were attached to the original connection.
1582 nsCOMPtr<mozIStorageStatement> stmt;
1583 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1584 MOZ_ASSERT(NS_SUCCEEDED(rv));
1585 bool hasResult = false;
1586 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1587 nsAutoCString name;
1588 rv = stmt->GetUTF8String(1, name);
1589 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1590 !name.EqualsLiteral("temp")) {
1591 nsCString path;
1592 rv = stmt->GetUTF8String(2, path);
1593 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1594 nsCOMPtr<mozIStorageStatement> attachStmt;
1595 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1596 getter_AddRefs(attachStmt));
1597 MOZ_ASSERT(NS_SUCCEEDED(rv));
1598 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1599 MOZ_ASSERT(NS_SUCCEEDED(rv));
1600 rv = attachStmt->Execute();
1601 MOZ_ASSERT(NS_SUCCEEDED(rv),
1602 "couldn't re-attach database to cloned connection");
1608 // Copy over pragmas from the original connection.
1609 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1610 // schema ("main" and any other attached databases), and this implmentation
1611 // fails to propagate them. This is being addressed on trunk.
1612 static const char* pragmas[] = {
1613 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1614 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1615 for (auto& pragma : pragmas) {
1616 // Read-only connections just need cache_size and temp_store pragmas.
1617 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1618 ::strcmp(pragma, "temp_store") != 0) {
1619 continue;
1622 nsAutoCString pragmaQuery("PRAGMA ");
1623 pragmaQuery.Append(pragma);
1624 nsCOMPtr<mozIStorageStatement> stmt;
1625 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
1626 MOZ_ASSERT(NS_SUCCEEDED(rv));
1627 bool hasResult = false;
1628 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1629 pragmaQuery.AppendLiteral(" = ");
1630 pragmaQuery.AppendInt(stmt->AsInt32(0));
1631 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
1632 MOZ_ASSERT(NS_SUCCEEDED(rv));
1636 // Copy over temporary tables, triggers, and views from the original
1637 // connections. Entities in `sqlite_temp_master` are only visible to the
1638 // connection that created them.
1639 if (!aReadOnly) {
1640 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
1641 NS_ENSURE_SUCCESS(rv, rv);
1643 nsCOMPtr<mozIStorageStatement> stmt;
1644 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1645 "WHERE type IN ('table', 'view', "
1646 "'index', 'trigger')"),
1647 getter_AddRefs(stmt));
1648 // Propagate errors, because failing to copy triggers might cause schema
1649 // coherency issues when writing to the database from the cloned connection.
1650 NS_ENSURE_SUCCESS(rv, rv);
1651 bool hasResult = false;
1652 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1653 nsAutoCString query;
1654 rv = stmt->GetUTF8String(0, query);
1655 NS_ENSURE_SUCCESS(rv, rv);
1657 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1658 // keyword. We need to add it back, or we'll recreate temporary entities
1659 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1660 // statements, but those don't need `TEMP` keywords.
1661 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
1662 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
1663 StringBeginsWith(query, "CREATE VIEW "_ns)) {
1664 query.Replace(0, 6, "CREATE TEMP");
1667 rv = aClone->ExecuteSimpleSQL(query);
1668 NS_ENSURE_SUCCESS(rv, rv);
1671 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
1672 NS_ENSURE_SUCCESS(rv, rv);
1675 // Copy any functions that have been added to this connection.
1676 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1677 for (const auto& entry : mFunctions) {
1678 const nsACString& key = entry.GetKey();
1679 Connection::FunctionInfo data = entry.GetData();
1681 rv = aClone->CreateFunction(key, data.numArgs, data.function);
1682 if (NS_FAILED(rv)) {
1683 NS_WARNING("Failed to copy function to cloned connection");
1687 guard.release();
1688 return NS_OK;
1691 NS_IMETHODIMP
1692 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
1693 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1695 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
1697 if (!connectionReady()) {
1698 return NS_ERROR_NOT_INITIALIZED;
1700 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1701 if (NS_FAILED(rv)) {
1702 return rv;
1705 int flags = mFlags;
1706 if (aReadOnly) {
1707 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1708 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1709 // Turn off SQLITE_OPEN_CREATE.
1710 flags = (~SQLITE_OPEN_CREATE & flags);
1713 RefPtr<Connection> clone = new Connection(
1714 mStorageService, flags, mSupportedOperations, mInterruptible);
1716 rv = initializeClone(clone, aReadOnly);
1717 if (NS_FAILED(rv)) {
1718 return rv;
1721 NS_IF_ADDREF(*_connection = clone);
1722 return NS_OK;
1725 NS_IMETHODIMP
1726 Connection::Interrupt() {
1727 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
1728 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
1729 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1730 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
1731 IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1733 if (!connectionReady()) {
1734 return NS_ERROR_NOT_INITIALIZED;
1737 if (isClosing()) { // Closing already in asynchronous case
1738 return NS_OK;
1742 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
1743 // it is not safe to call sqlite3_interrupt() when
1744 // database connection is closed or might close before
1745 // sqlite3_interrupt() returns.
1746 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1747 if (!isClosed(lockedScope)) {
1748 MOZ_ASSERT(mDBConn);
1749 ::sqlite3_interrupt(mDBConn);
1753 return NS_OK;
1756 NS_IMETHODIMP
1757 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
1758 *_defaultPageSize = Service::kDefaultPageSize;
1759 return NS_OK;
1762 NS_IMETHODIMP
1763 Connection::GetConnectionReady(bool* _ready) {
1764 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1765 *_ready = connectionReady();
1766 return NS_OK;
1769 NS_IMETHODIMP
1770 Connection::GetDatabaseFile(nsIFile** _dbFile) {
1771 if (!connectionReady()) {
1772 return NS_ERROR_NOT_INITIALIZED;
1774 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1775 if (NS_FAILED(rv)) {
1776 return rv;
1779 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
1781 return NS_OK;
1784 NS_IMETHODIMP
1785 Connection::GetLastInsertRowID(int64_t* _id) {
1786 if (!connectionReady()) {
1787 return NS_ERROR_NOT_INITIALIZED;
1789 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1790 if (NS_FAILED(rv)) {
1791 return rv;
1794 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
1795 *_id = id;
1797 return NS_OK;
1800 NS_IMETHODIMP
1801 Connection::GetAffectedRows(int32_t* _rows) {
1802 if (!connectionReady()) {
1803 return NS_ERROR_NOT_INITIALIZED;
1805 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1806 if (NS_FAILED(rv)) {
1807 return rv;
1810 *_rows = ::sqlite3_changes(mDBConn);
1812 return NS_OK;
1815 NS_IMETHODIMP
1816 Connection::GetLastError(int32_t* _error) {
1817 if (!connectionReady()) {
1818 return NS_ERROR_NOT_INITIALIZED;
1820 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1821 if (NS_FAILED(rv)) {
1822 return rv;
1825 *_error = ::sqlite3_errcode(mDBConn);
1827 return NS_OK;
1830 NS_IMETHODIMP
1831 Connection::GetLastErrorString(nsACString& _errorString) {
1832 if (!connectionReady()) {
1833 return NS_ERROR_NOT_INITIALIZED;
1835 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1836 if (NS_FAILED(rv)) {
1837 return rv;
1840 const char* serr = ::sqlite3_errmsg(mDBConn);
1841 _errorString.Assign(serr);
1843 return NS_OK;
1846 NS_IMETHODIMP
1847 Connection::GetSchemaVersion(int32_t* _version) {
1848 if (!connectionReady()) {
1849 return NS_ERROR_NOT_INITIALIZED;
1851 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1852 if (NS_FAILED(rv)) {
1853 return rv;
1856 nsCOMPtr<mozIStorageStatement> stmt;
1857 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
1858 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
1860 *_version = 0;
1861 bool hasResult;
1862 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult)
1863 *_version = stmt->AsInt32(0);
1865 return NS_OK;
1868 NS_IMETHODIMP
1869 Connection::SetSchemaVersion(int32_t aVersion) {
1870 if (!connectionReady()) {
1871 return NS_ERROR_NOT_INITIALIZED;
1873 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1874 if (NS_FAILED(rv)) {
1875 return rv;
1878 nsAutoCString stmt("PRAGMA user_version = "_ns);
1879 stmt.AppendInt(aVersion);
1881 return ExecuteSimpleSQL(stmt);
1884 NS_IMETHODIMP
1885 Connection::CreateStatement(const nsACString& aSQLStatement,
1886 mozIStorageStatement** _stmt) {
1887 NS_ENSURE_ARG_POINTER(_stmt);
1888 if (!connectionReady()) {
1889 return NS_ERROR_NOT_INITIALIZED;
1891 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1892 if (NS_FAILED(rv)) {
1893 return rv;
1896 RefPtr<Statement> statement(new Statement());
1897 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1899 rv = statement->initialize(this, mDBConn, aSQLStatement);
1900 NS_ENSURE_SUCCESS(rv, rv);
1902 Statement* rawPtr;
1903 statement.forget(&rawPtr);
1904 *_stmt = rawPtr;
1905 return NS_OK;
1908 NS_IMETHODIMP
1909 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
1910 mozIStorageAsyncStatement** _stmt) {
1911 NS_ENSURE_ARG_POINTER(_stmt);
1912 if (!connectionReady()) {
1913 return NS_ERROR_NOT_INITIALIZED;
1915 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1916 if (NS_FAILED(rv)) {
1917 return rv;
1920 RefPtr<AsyncStatement> statement(new AsyncStatement());
1921 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1923 rv = statement->initialize(this, mDBConn, aSQLStatement);
1924 NS_ENSURE_SUCCESS(rv, rv);
1926 AsyncStatement* rawPtr;
1927 statement.forget(&rawPtr);
1928 *_stmt = rawPtr;
1929 return NS_OK;
1932 NS_IMETHODIMP
1933 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
1934 CHECK_MAINTHREAD_ABUSE();
1935 if (!connectionReady()) {
1936 return NS_ERROR_NOT_INITIALIZED;
1938 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1939 if (NS_FAILED(rv)) {
1940 return rv;
1943 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
1944 return convertResultCode(srv);
1947 NS_IMETHODIMP
1948 Connection::ExecuteAsync(
1949 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
1950 mozIStorageStatementCallback* aCallback,
1951 mozIStoragePendingStatement** _handle) {
1952 nsTArray<StatementData> stmts(aStatements.Length());
1953 for (uint32_t i = 0; i < aStatements.Length(); i++) {
1954 nsCOMPtr<StorageBaseStatementInternal> stmt =
1955 do_QueryInterface(aStatements[i]);
1956 NS_ENSURE_STATE(stmt);
1958 // Obtain our StatementData.
1959 StatementData data;
1960 nsresult rv = stmt->getAsynchronousStatementData(data);
1961 NS_ENSURE_SUCCESS(rv, rv);
1963 NS_ASSERTION(stmt->getOwner() == this,
1964 "Statement must be from this database connection!");
1966 // Now append it to our array.
1967 stmts.AppendElement(data);
1970 // Dispatch to the background
1971 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
1972 aCallback, _handle);
1975 NS_IMETHODIMP
1976 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
1977 mozIStorageStatementCallback* aCallback,
1978 mozIStoragePendingStatement** _handle) {
1979 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1981 nsCOMPtr<mozIStorageAsyncStatement> stmt;
1982 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
1983 if (NS_FAILED(rv)) {
1984 return rv;
1987 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
1988 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
1989 if (NS_FAILED(rv)) {
1990 return rv;
1993 pendingStatement.forget(_handle);
1994 return rv;
1997 NS_IMETHODIMP
1998 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
1999 return databaseElementExists(TABLE, aTableName, _exists);
2002 NS_IMETHODIMP
2003 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2004 return databaseElementExists(INDEX, aIndexName, _exists);
2007 NS_IMETHODIMP
2008 Connection::GetTransactionInProgress(bool* _inProgress) {
2009 if (!connectionReady()) {
2010 return NS_ERROR_NOT_INITIALIZED;
2012 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2013 if (NS_FAILED(rv)) {
2014 return rv;
2017 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2018 *_inProgress = transactionInProgress(lockedScope);
2019 return NS_OK;
2022 NS_IMETHODIMP
2023 Connection::GetDefaultTransactionType(int32_t* _type) {
2024 *_type = mDefaultTransactionType;
2025 return NS_OK;
2028 NS_IMETHODIMP
2029 Connection::SetDefaultTransactionType(int32_t aType) {
2030 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2031 mDefaultTransactionType = aType;
2032 return NS_OK;
2035 NS_IMETHODIMP
2036 Connection::GetVariableLimit(int32_t* _limit) {
2037 if (!connectionReady()) {
2038 return NS_ERROR_NOT_INITIALIZED;
2040 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2041 if (limit < 0) {
2042 return NS_ERROR_UNEXPECTED;
2044 *_limit = limit;
2045 return NS_OK;
2048 NS_IMETHODIMP
2049 Connection::BeginTransaction() {
2050 if (!connectionReady()) {
2051 return NS_ERROR_NOT_INITIALIZED;
2053 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2054 if (NS_FAILED(rv)) {
2055 return rv;
2058 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2059 return beginTransactionInternal(lockedScope, mDBConn,
2060 mDefaultTransactionType);
2063 nsresult Connection::beginTransactionInternal(
2064 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2065 int32_t aTransactionType) {
2066 if (transactionInProgress(aProofOfLock)) {
2067 return NS_ERROR_FAILURE;
2069 nsresult rv;
2070 switch (aTransactionType) {
2071 case TRANSACTION_DEFERRED:
2072 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2073 break;
2074 case TRANSACTION_IMMEDIATE:
2075 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2076 break;
2077 case TRANSACTION_EXCLUSIVE:
2078 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2079 break;
2080 default:
2081 return NS_ERROR_ILLEGAL_VALUE;
2083 return rv;
2086 NS_IMETHODIMP
2087 Connection::CommitTransaction() {
2088 if (!connectionReady()) {
2089 return NS_ERROR_NOT_INITIALIZED;
2091 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2092 if (NS_FAILED(rv)) {
2093 return rv;
2096 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2097 return commitTransactionInternal(lockedScope, mDBConn);
2100 nsresult Connection::commitTransactionInternal(
2101 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2102 if (!transactionInProgress(aProofOfLock)) {
2103 return NS_ERROR_UNEXPECTED;
2105 nsresult rv =
2106 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2107 return rv;
2110 NS_IMETHODIMP
2111 Connection::RollbackTransaction() {
2112 if (!connectionReady()) {
2113 return NS_ERROR_NOT_INITIALIZED;
2115 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2116 if (NS_FAILED(rv)) {
2117 return rv;
2120 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2121 return rollbackTransactionInternal(lockedScope, mDBConn);
2124 nsresult Connection::rollbackTransactionInternal(
2125 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2126 if (!transactionInProgress(aProofOfLock)) {
2127 return NS_ERROR_UNEXPECTED;
2130 nsresult rv =
2131 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2132 return rv;
2135 NS_IMETHODIMP
2136 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2137 if (!connectionReady()) {
2138 return NS_ERROR_NOT_INITIALIZED;
2140 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2141 if (NS_FAILED(rv)) {
2142 return rv;
2145 SmprintfPointer buf =
2146 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2147 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2149 int srv = executeSql(mDBConn, buf.get());
2151 return convertResultCode(srv);
2154 NS_IMETHODIMP
2155 Connection::CreateFunction(const nsACString& aFunctionName,
2156 int32_t aNumArguments,
2157 mozIStorageFunction* aFunction) {
2158 if (!connectionReady()) {
2159 return NS_ERROR_NOT_INITIALIZED;
2161 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2162 if (NS_FAILED(rv)) {
2163 return rv;
2166 // Check to see if this function is already defined. We only check the name
2167 // because a function can be defined with the same body but different names.
2168 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2169 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2171 int srv = ::sqlite3_create_function(
2172 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2173 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2174 if (srv != SQLITE_OK) return convertResultCode(srv);
2176 FunctionInfo info = {aFunction, aNumArguments};
2177 mFunctions.InsertOrUpdate(aFunctionName, info);
2179 return NS_OK;
2182 NS_IMETHODIMP
2183 Connection::RemoveFunction(const nsACString& aFunctionName) {
2184 if (!connectionReady()) {
2185 return NS_ERROR_NOT_INITIALIZED;
2187 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2188 if (NS_FAILED(rv)) {
2189 return rv;
2192 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2193 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2195 int srv = ::sqlite3_create_function(
2196 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2197 nullptr, nullptr, nullptr, nullptr);
2198 if (srv != SQLITE_OK) return convertResultCode(srv);
2200 mFunctions.Remove(aFunctionName);
2202 return NS_OK;
2205 NS_IMETHODIMP
2206 Connection::SetProgressHandler(int32_t aGranularity,
2207 mozIStorageProgressHandler* aHandler,
2208 mozIStorageProgressHandler** _oldHandler) {
2209 if (!connectionReady()) {
2210 return NS_ERROR_NOT_INITIALIZED;
2212 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2213 if (NS_FAILED(rv)) {
2214 return rv;
2217 // Return previous one
2218 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2219 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2221 if (!aHandler || aGranularity <= 0) {
2222 aHandler = nullptr;
2223 aGranularity = 0;
2225 mProgressHandler = aHandler;
2226 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2228 return NS_OK;
2231 NS_IMETHODIMP
2232 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2233 if (!connectionReady()) {
2234 return NS_ERROR_NOT_INITIALIZED;
2236 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2237 if (NS_FAILED(rv)) {
2238 return rv;
2241 // Return previous one
2242 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2243 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2245 mProgressHandler = nullptr;
2246 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2248 return NS_OK;
2251 NS_IMETHODIMP
2252 Connection::SetGrowthIncrement(int32_t aChunkSize,
2253 const nsACString& aDatabaseName) {
2254 if (!connectionReady()) {
2255 return NS_ERROR_NOT_INITIALIZED;
2257 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2258 if (NS_FAILED(rv)) {
2259 return rv;
2262 // Bug 597215: Disk space is extremely limited on Android
2263 // so don't preallocate space. This is also not effective
2264 // on log structured file systems used by Android devices
2265 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2266 // Don't preallocate if less than 500MiB is available.
2267 int64_t bytesAvailable;
2268 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2269 NS_ENSURE_SUCCESS(rv, rv);
2270 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2271 return NS_ERROR_FILE_TOO_BIG;
2274 (void)::sqlite3_file_control(mDBConn,
2275 aDatabaseName.Length()
2276 ? nsPromiseFlatCString(aDatabaseName).get()
2277 : nullptr,
2278 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2279 #endif
2280 return NS_OK;
2283 NS_IMETHODIMP
2284 Connection::EnableModule(const nsACString& aModuleName) {
2285 if (!connectionReady()) {
2286 return NS_ERROR_NOT_INITIALIZED;
2288 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2289 if (NS_FAILED(rv)) {
2290 return rv;
2293 for (auto& gModule : gModules) {
2294 struct Module* m = &gModule;
2295 if (aModuleName.Equals(m->name)) {
2296 int srv = m->registerFunc(mDBConn, m->name);
2297 if (srv != SQLITE_OK) return convertResultCode(srv);
2299 return NS_OK;
2303 return NS_ERROR_FAILURE;
2306 // Implemented in TelemetryVFS.cpp
2307 already_AddRefed<QuotaObject> GetQuotaObjectForFile(sqlite3_file* pFile);
2309 NS_IMETHODIMP
2310 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2311 QuotaObject** aJournalQuotaObject) {
2312 MOZ_ASSERT(aDatabaseQuotaObject);
2313 MOZ_ASSERT(aJournalQuotaObject);
2315 if (!connectionReady()) {
2316 return NS_ERROR_NOT_INITIALIZED;
2318 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2319 if (NS_FAILED(rv)) {
2320 return rv;
2323 sqlite3_file* file;
2324 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2325 &file);
2326 if (srv != SQLITE_OK) {
2327 return convertResultCode(srv);
2330 RefPtr<QuotaObject> databaseQuotaObject = GetQuotaObjectForFile(file);
2331 if (NS_WARN_IF(!databaseQuotaObject)) {
2332 return NS_ERROR_FAILURE;
2335 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2336 &file);
2337 if (srv != SQLITE_OK) {
2338 return convertResultCode(srv);
2341 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObjectForFile(file);
2342 if (NS_WARN_IF(!journalQuotaObject)) {
2343 return NS_ERROR_FAILURE;
2346 databaseQuotaObject.forget(aDatabaseQuotaObject);
2347 journalQuotaObject.forget(aJournalQuotaObject);
2348 return NS_OK;
2351 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2353 uint32_t Connection::GetTransactionNestingLevel(
2354 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2355 return mTransactionNestingLevel;
2358 uint32_t Connection::IncreaseTransactionNestingLevel(
2359 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2360 return ++mTransactionNestingLevel;
2363 uint32_t Connection::DecreaseTransactionNestingLevel(
2364 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2365 return --mTransactionNestingLevel;
2368 } // namespace mozilla::storage