Bug 1799258 - Support outByIn.size()<2 in SampleOutByIn. r=bradwerth
[gecko.git] / storage / mozStorageConnection.cpp
blob0f0d76839114d22eef341190b982ad4f386909ae
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 NS_ENSURE_SUCCESS(rv, 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 NS_ENSURE_SUCCESS(rv, rv);
1594 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1595 NS_ENSURE_SUCCESS(rv, rv);
1596 rv = attachStmt->Execute();
1597 NS_ENSURE_SUCCESS(rv, rv);
1603 // Copy over pragmas from the original connection.
1604 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1605 // schema ("main" and any other attached databases), and this implmentation
1606 // fails to propagate them. This is being addressed on trunk.
1607 static const char* pragmas[] = {
1608 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1609 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1610 for (auto& pragma : pragmas) {
1611 // Read-only connections just need cache_size and temp_store pragmas.
1612 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1613 ::strcmp(pragma, "temp_store") != 0) {
1614 continue;
1617 nsAutoCString pragmaQuery("PRAGMA ");
1618 pragmaQuery.Append(pragma);
1619 nsCOMPtr<mozIStorageStatement> stmt;
1620 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
1621 NS_ENSURE_SUCCESS(rv, rv);
1622 bool hasResult = false;
1623 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1624 pragmaQuery.AppendLiteral(" = ");
1625 pragmaQuery.AppendInt(stmt->AsInt32(0));
1626 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
1627 NS_ENSURE_SUCCESS(rv, rv);
1631 // Copy over temporary tables, triggers, and views from the original
1632 // connections. Entities in `sqlite_temp_master` are only visible to the
1633 // connection that created them.
1634 if (!aReadOnly) {
1635 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
1636 NS_ENSURE_SUCCESS(rv, rv);
1638 nsCOMPtr<mozIStorageStatement> stmt;
1639 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1640 "WHERE type IN ('table', 'view', "
1641 "'index', 'trigger')"),
1642 getter_AddRefs(stmt));
1643 // Propagate errors, because failing to copy triggers might cause schema
1644 // coherency issues when writing to the database from the cloned connection.
1645 NS_ENSURE_SUCCESS(rv, rv);
1646 bool hasResult = false;
1647 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1648 nsAutoCString query;
1649 rv = stmt->GetUTF8String(0, query);
1650 NS_ENSURE_SUCCESS(rv, rv);
1652 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1653 // keyword. We need to add it back, or we'll recreate temporary entities
1654 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1655 // statements, but those don't need `TEMP` keywords.
1656 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
1657 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
1658 StringBeginsWith(query, "CREATE VIEW "_ns)) {
1659 query.Replace(0, 6, "CREATE TEMP");
1662 rv = aClone->ExecuteSimpleSQL(query);
1663 NS_ENSURE_SUCCESS(rv, rv);
1666 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
1667 NS_ENSURE_SUCCESS(rv, rv);
1670 // Copy any functions that have been added to this connection.
1671 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1672 for (const auto& entry : mFunctions) {
1673 const nsACString& key = entry.GetKey();
1674 Connection::FunctionInfo data = entry.GetData();
1676 rv = aClone->CreateFunction(key, data.numArgs, data.function);
1677 if (NS_FAILED(rv)) {
1678 NS_WARNING("Failed to copy function to cloned connection");
1682 guard.release();
1683 return NS_OK;
1686 NS_IMETHODIMP
1687 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
1688 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1690 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
1692 if (!connectionReady()) {
1693 return NS_ERROR_NOT_INITIALIZED;
1695 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1696 if (NS_FAILED(rv)) {
1697 return rv;
1700 int flags = mFlags;
1701 if (aReadOnly) {
1702 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1703 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1704 // Turn off SQLITE_OPEN_CREATE.
1705 flags = (~SQLITE_OPEN_CREATE & flags);
1708 RefPtr<Connection> clone = new Connection(
1709 mStorageService, flags, mSupportedOperations, mInterruptible);
1711 rv = initializeClone(clone, aReadOnly);
1712 if (NS_FAILED(rv)) {
1713 return rv;
1716 NS_IF_ADDREF(*_connection = clone);
1717 return NS_OK;
1720 NS_IMETHODIMP
1721 Connection::Interrupt() {
1722 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
1723 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
1724 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1725 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
1726 IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1728 if (!connectionReady()) {
1729 return NS_ERROR_NOT_INITIALIZED;
1732 if (isClosing()) { // Closing already in asynchronous case
1733 return NS_OK;
1737 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
1738 // it is not safe to call sqlite3_interrupt() when
1739 // database connection is closed or might close before
1740 // sqlite3_interrupt() returns.
1741 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1742 if (!isClosed(lockedScope)) {
1743 MOZ_ASSERT(mDBConn);
1744 ::sqlite3_interrupt(mDBConn);
1748 return NS_OK;
1751 NS_IMETHODIMP
1752 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
1753 *_defaultPageSize = Service::kDefaultPageSize;
1754 return NS_OK;
1757 NS_IMETHODIMP
1758 Connection::GetConnectionReady(bool* _ready) {
1759 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1760 *_ready = connectionReady();
1761 return NS_OK;
1764 NS_IMETHODIMP
1765 Connection::GetDatabaseFile(nsIFile** _dbFile) {
1766 if (!connectionReady()) {
1767 return NS_ERROR_NOT_INITIALIZED;
1769 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1770 if (NS_FAILED(rv)) {
1771 return rv;
1774 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
1776 return NS_OK;
1779 NS_IMETHODIMP
1780 Connection::GetLastInsertRowID(int64_t* _id) {
1781 if (!connectionReady()) {
1782 return NS_ERROR_NOT_INITIALIZED;
1784 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1785 if (NS_FAILED(rv)) {
1786 return rv;
1789 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
1790 *_id = id;
1792 return NS_OK;
1795 NS_IMETHODIMP
1796 Connection::GetAffectedRows(int32_t* _rows) {
1797 if (!connectionReady()) {
1798 return NS_ERROR_NOT_INITIALIZED;
1800 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1801 if (NS_FAILED(rv)) {
1802 return rv;
1805 *_rows = ::sqlite3_changes(mDBConn);
1807 return NS_OK;
1810 NS_IMETHODIMP
1811 Connection::GetLastError(int32_t* _error) {
1812 if (!connectionReady()) {
1813 return NS_ERROR_NOT_INITIALIZED;
1815 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1816 if (NS_FAILED(rv)) {
1817 return rv;
1820 *_error = ::sqlite3_errcode(mDBConn);
1822 return NS_OK;
1825 NS_IMETHODIMP
1826 Connection::GetLastErrorString(nsACString& _errorString) {
1827 if (!connectionReady()) {
1828 return NS_ERROR_NOT_INITIALIZED;
1830 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1831 if (NS_FAILED(rv)) {
1832 return rv;
1835 const char* serr = ::sqlite3_errmsg(mDBConn);
1836 _errorString.Assign(serr);
1838 return NS_OK;
1841 NS_IMETHODIMP
1842 Connection::GetSchemaVersion(int32_t* _version) {
1843 if (!connectionReady()) {
1844 return NS_ERROR_NOT_INITIALIZED;
1846 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1847 if (NS_FAILED(rv)) {
1848 return rv;
1851 nsCOMPtr<mozIStorageStatement> stmt;
1852 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
1853 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
1855 *_version = 0;
1856 bool hasResult;
1857 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult)
1858 *_version = stmt->AsInt32(0);
1860 return NS_OK;
1863 NS_IMETHODIMP
1864 Connection::SetSchemaVersion(int32_t aVersion) {
1865 if (!connectionReady()) {
1866 return NS_ERROR_NOT_INITIALIZED;
1868 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1869 if (NS_FAILED(rv)) {
1870 return rv;
1873 nsAutoCString stmt("PRAGMA user_version = "_ns);
1874 stmt.AppendInt(aVersion);
1876 return ExecuteSimpleSQL(stmt);
1879 NS_IMETHODIMP
1880 Connection::CreateStatement(const nsACString& aSQLStatement,
1881 mozIStorageStatement** _stmt) {
1882 NS_ENSURE_ARG_POINTER(_stmt);
1883 if (!connectionReady()) {
1884 return NS_ERROR_NOT_INITIALIZED;
1886 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1887 if (NS_FAILED(rv)) {
1888 return rv;
1891 RefPtr<Statement> statement(new Statement());
1892 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1894 rv = statement->initialize(this, mDBConn, aSQLStatement);
1895 NS_ENSURE_SUCCESS(rv, rv);
1897 Statement* rawPtr;
1898 statement.forget(&rawPtr);
1899 *_stmt = rawPtr;
1900 return NS_OK;
1903 NS_IMETHODIMP
1904 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
1905 mozIStorageAsyncStatement** _stmt) {
1906 NS_ENSURE_ARG_POINTER(_stmt);
1907 if (!connectionReady()) {
1908 return NS_ERROR_NOT_INITIALIZED;
1910 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1911 if (NS_FAILED(rv)) {
1912 return rv;
1915 RefPtr<AsyncStatement> statement(new AsyncStatement());
1916 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1918 rv = statement->initialize(this, mDBConn, aSQLStatement);
1919 NS_ENSURE_SUCCESS(rv, rv);
1921 AsyncStatement* rawPtr;
1922 statement.forget(&rawPtr);
1923 *_stmt = rawPtr;
1924 return NS_OK;
1927 NS_IMETHODIMP
1928 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
1929 CHECK_MAINTHREAD_ABUSE();
1930 if (!connectionReady()) {
1931 return NS_ERROR_NOT_INITIALIZED;
1933 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1934 if (NS_FAILED(rv)) {
1935 return rv;
1938 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
1939 return convertResultCode(srv);
1942 NS_IMETHODIMP
1943 Connection::ExecuteAsync(
1944 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
1945 mozIStorageStatementCallback* aCallback,
1946 mozIStoragePendingStatement** _handle) {
1947 nsTArray<StatementData> stmts(aStatements.Length());
1948 for (uint32_t i = 0; i < aStatements.Length(); i++) {
1949 nsCOMPtr<StorageBaseStatementInternal> stmt =
1950 do_QueryInterface(aStatements[i]);
1951 NS_ENSURE_STATE(stmt);
1953 // Obtain our StatementData.
1954 StatementData data;
1955 nsresult rv = stmt->getAsynchronousStatementData(data);
1956 NS_ENSURE_SUCCESS(rv, rv);
1958 NS_ASSERTION(stmt->getOwner() == this,
1959 "Statement must be from this database connection!");
1961 // Now append it to our array.
1962 stmts.AppendElement(data);
1965 // Dispatch to the background
1966 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
1967 aCallback, _handle);
1970 NS_IMETHODIMP
1971 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
1972 mozIStorageStatementCallback* aCallback,
1973 mozIStoragePendingStatement** _handle) {
1974 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1976 nsCOMPtr<mozIStorageAsyncStatement> stmt;
1977 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
1978 if (NS_FAILED(rv)) {
1979 return rv;
1982 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
1983 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
1984 if (NS_FAILED(rv)) {
1985 return rv;
1988 pendingStatement.forget(_handle);
1989 return rv;
1992 NS_IMETHODIMP
1993 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
1994 return databaseElementExists(TABLE, aTableName, _exists);
1997 NS_IMETHODIMP
1998 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
1999 return databaseElementExists(INDEX, aIndexName, _exists);
2002 NS_IMETHODIMP
2003 Connection::GetTransactionInProgress(bool* _inProgress) {
2004 if (!connectionReady()) {
2005 return NS_ERROR_NOT_INITIALIZED;
2007 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2008 if (NS_FAILED(rv)) {
2009 return rv;
2012 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2013 *_inProgress = transactionInProgress(lockedScope);
2014 return NS_OK;
2017 NS_IMETHODIMP
2018 Connection::GetDefaultTransactionType(int32_t* _type) {
2019 *_type = mDefaultTransactionType;
2020 return NS_OK;
2023 NS_IMETHODIMP
2024 Connection::SetDefaultTransactionType(int32_t aType) {
2025 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2026 mDefaultTransactionType = aType;
2027 return NS_OK;
2030 NS_IMETHODIMP
2031 Connection::GetVariableLimit(int32_t* _limit) {
2032 if (!connectionReady()) {
2033 return NS_ERROR_NOT_INITIALIZED;
2035 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2036 if (limit < 0) {
2037 return NS_ERROR_UNEXPECTED;
2039 *_limit = limit;
2040 return NS_OK;
2043 NS_IMETHODIMP
2044 Connection::BeginTransaction() {
2045 if (!connectionReady()) {
2046 return NS_ERROR_NOT_INITIALIZED;
2048 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2049 if (NS_FAILED(rv)) {
2050 return rv;
2053 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2054 return beginTransactionInternal(lockedScope, mDBConn,
2055 mDefaultTransactionType);
2058 nsresult Connection::beginTransactionInternal(
2059 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2060 int32_t aTransactionType) {
2061 if (transactionInProgress(aProofOfLock)) {
2062 return NS_ERROR_FAILURE;
2064 nsresult rv;
2065 switch (aTransactionType) {
2066 case TRANSACTION_DEFERRED:
2067 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2068 break;
2069 case TRANSACTION_IMMEDIATE:
2070 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2071 break;
2072 case TRANSACTION_EXCLUSIVE:
2073 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2074 break;
2075 default:
2076 return NS_ERROR_ILLEGAL_VALUE;
2078 return rv;
2081 NS_IMETHODIMP
2082 Connection::CommitTransaction() {
2083 if (!connectionReady()) {
2084 return NS_ERROR_NOT_INITIALIZED;
2086 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2087 if (NS_FAILED(rv)) {
2088 return rv;
2091 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2092 return commitTransactionInternal(lockedScope, mDBConn);
2095 nsresult Connection::commitTransactionInternal(
2096 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2097 if (!transactionInProgress(aProofOfLock)) {
2098 return NS_ERROR_UNEXPECTED;
2100 nsresult rv =
2101 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2102 return rv;
2105 NS_IMETHODIMP
2106 Connection::RollbackTransaction() {
2107 if (!connectionReady()) {
2108 return NS_ERROR_NOT_INITIALIZED;
2110 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2111 if (NS_FAILED(rv)) {
2112 return rv;
2115 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2116 return rollbackTransactionInternal(lockedScope, mDBConn);
2119 nsresult Connection::rollbackTransactionInternal(
2120 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2121 if (!transactionInProgress(aProofOfLock)) {
2122 return NS_ERROR_UNEXPECTED;
2125 nsresult rv =
2126 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2127 return rv;
2130 NS_IMETHODIMP
2131 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2132 if (!connectionReady()) {
2133 return NS_ERROR_NOT_INITIALIZED;
2135 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2136 if (NS_FAILED(rv)) {
2137 return rv;
2140 SmprintfPointer buf =
2141 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2142 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2144 int srv = executeSql(mDBConn, buf.get());
2146 return convertResultCode(srv);
2149 NS_IMETHODIMP
2150 Connection::CreateFunction(const nsACString& aFunctionName,
2151 int32_t aNumArguments,
2152 mozIStorageFunction* aFunction) {
2153 if (!connectionReady()) {
2154 return NS_ERROR_NOT_INITIALIZED;
2156 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2157 if (NS_FAILED(rv)) {
2158 return rv;
2161 // Check to see if this function is already defined. We only check the name
2162 // because a function can be defined with the same body but different names.
2163 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2164 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2166 int srv = ::sqlite3_create_function(
2167 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2168 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2169 if (srv != SQLITE_OK) return convertResultCode(srv);
2171 FunctionInfo info = {aFunction, aNumArguments};
2172 mFunctions.InsertOrUpdate(aFunctionName, info);
2174 return NS_OK;
2177 NS_IMETHODIMP
2178 Connection::RemoveFunction(const nsACString& aFunctionName) {
2179 if (!connectionReady()) {
2180 return NS_ERROR_NOT_INITIALIZED;
2182 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2183 if (NS_FAILED(rv)) {
2184 return rv;
2187 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2188 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2190 int srv = ::sqlite3_create_function(
2191 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2192 nullptr, nullptr, nullptr, nullptr);
2193 if (srv != SQLITE_OK) return convertResultCode(srv);
2195 mFunctions.Remove(aFunctionName);
2197 return NS_OK;
2200 NS_IMETHODIMP
2201 Connection::SetProgressHandler(int32_t aGranularity,
2202 mozIStorageProgressHandler* aHandler,
2203 mozIStorageProgressHandler** _oldHandler) {
2204 if (!connectionReady()) {
2205 return NS_ERROR_NOT_INITIALIZED;
2207 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2208 if (NS_FAILED(rv)) {
2209 return rv;
2212 // Return previous one
2213 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2214 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2216 if (!aHandler || aGranularity <= 0) {
2217 aHandler = nullptr;
2218 aGranularity = 0;
2220 mProgressHandler = aHandler;
2221 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2223 return NS_OK;
2226 NS_IMETHODIMP
2227 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2228 if (!connectionReady()) {
2229 return NS_ERROR_NOT_INITIALIZED;
2231 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2232 if (NS_FAILED(rv)) {
2233 return rv;
2236 // Return previous one
2237 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2238 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2240 mProgressHandler = nullptr;
2241 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2243 return NS_OK;
2246 NS_IMETHODIMP
2247 Connection::SetGrowthIncrement(int32_t aChunkSize,
2248 const nsACString& aDatabaseName) {
2249 if (!connectionReady()) {
2250 return NS_ERROR_NOT_INITIALIZED;
2252 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2253 if (NS_FAILED(rv)) {
2254 return rv;
2257 // Bug 597215: Disk space is extremely limited on Android
2258 // so don't preallocate space. This is also not effective
2259 // on log structured file systems used by Android devices
2260 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2261 // Don't preallocate if less than 500MiB is available.
2262 int64_t bytesAvailable;
2263 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2264 NS_ENSURE_SUCCESS(rv, rv);
2265 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2266 return NS_ERROR_FILE_TOO_BIG;
2269 (void)::sqlite3_file_control(mDBConn,
2270 aDatabaseName.Length()
2271 ? nsPromiseFlatCString(aDatabaseName).get()
2272 : nullptr,
2273 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2274 #endif
2275 return NS_OK;
2278 NS_IMETHODIMP
2279 Connection::EnableModule(const nsACString& aModuleName) {
2280 if (!connectionReady()) {
2281 return NS_ERROR_NOT_INITIALIZED;
2283 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2284 if (NS_FAILED(rv)) {
2285 return rv;
2288 for (auto& gModule : gModules) {
2289 struct Module* m = &gModule;
2290 if (aModuleName.Equals(m->name)) {
2291 int srv = m->registerFunc(mDBConn, m->name);
2292 if (srv != SQLITE_OK) return convertResultCode(srv);
2294 return NS_OK;
2298 return NS_ERROR_FAILURE;
2301 // Implemented in TelemetryVFS.cpp
2302 already_AddRefed<QuotaObject> GetQuotaObjectForFile(sqlite3_file* pFile);
2304 NS_IMETHODIMP
2305 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2306 QuotaObject** aJournalQuotaObject) {
2307 MOZ_ASSERT(aDatabaseQuotaObject);
2308 MOZ_ASSERT(aJournalQuotaObject);
2310 if (!connectionReady()) {
2311 return NS_ERROR_NOT_INITIALIZED;
2313 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2314 if (NS_FAILED(rv)) {
2315 return rv;
2318 sqlite3_file* file;
2319 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2320 &file);
2321 if (srv != SQLITE_OK) {
2322 return convertResultCode(srv);
2325 RefPtr<QuotaObject> databaseQuotaObject = GetQuotaObjectForFile(file);
2326 if (NS_WARN_IF(!databaseQuotaObject)) {
2327 return NS_ERROR_FAILURE;
2330 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2331 &file);
2332 if (srv != SQLITE_OK) {
2333 return convertResultCode(srv);
2336 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObjectForFile(file);
2337 if (NS_WARN_IF(!journalQuotaObject)) {
2338 return NS_ERROR_FAILURE;
2341 databaseQuotaObject.forget(aDatabaseQuotaObject);
2342 journalQuotaObject.forget(aJournalQuotaObject);
2343 return NS_OK;
2346 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2348 uint32_t Connection::GetTransactionNestingLevel(
2349 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2350 return mTransactionNestingLevel;
2353 uint32_t Connection::IncreaseTransactionNestingLevel(
2354 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2355 return ++mTransactionNestingLevel;
2358 uint32_t Connection::DecreaseTransactionNestingLevel(
2359 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2360 return --mTransactionNestingLevel;
2363 } // namespace mozilla::storage