Bug 1605894 reduce the proliferation of DefaultLoopbackTone to only AudioStreamFlowin...
[gecko.git] / storage / mozStorageConnection.cpp
blob755f6d55a7bcaa7e192c4431469e2003d78a7937
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 "BaseVFS.h"
8 #include "nsError.h"
9 #include "nsThreadUtils.h"
10 #include "nsIFile.h"
11 #include "nsIFileURL.h"
12 #include "nsIXPConnect.h"
13 #include "mozilla/AppShutdown.h"
14 #include "mozilla/CheckedInt.h"
15 #include "mozilla/Telemetry.h"
16 #include "mozilla/Mutex.h"
17 #include "mozilla/CondVar.h"
18 #include "mozilla/Attributes.h"
19 #include "mozilla/ErrorNames.h"
20 #include "mozilla/Unused.h"
21 #include "mozilla/dom/quota/QuotaObject.h"
22 #include "mozilla/ScopeExit.h"
23 #include "mozilla/SpinEventLoopUntil.h"
24 #include "mozilla/StaticPrefs_storage.h"
26 #include "mozIStorageCompletionCallback.h"
27 #include "mozIStorageFunction.h"
29 #include "mozStorageAsyncStatementExecution.h"
30 #include "mozStorageSQLFunctions.h"
31 #include "mozStorageConnection.h"
32 #include "mozStorageService.h"
33 #include "mozStorageStatement.h"
34 #include "mozStorageAsyncStatement.h"
35 #include "mozStorageArgValueArray.h"
36 #include "mozStoragePrivateHelpers.h"
37 #include "mozStorageStatementData.h"
38 #include "ObfuscatingVFS.h"
39 #include "QuotaVFS.h"
40 #include "StorageBaseStatementInternal.h"
41 #include "SQLCollations.h"
42 #include "FileSystemModule.h"
43 #include "mozStorageHelper.h"
45 #include "mozilla/Assertions.h"
46 #include "mozilla/Logging.h"
47 #include "mozilla/Printf.h"
48 #include "mozilla/ProfilerLabels.h"
49 #include "mozilla/RefPtr.h"
50 #include "nsProxyRelease.h"
51 #include "nsStringFwd.h"
52 #include "nsURLHelper.h"
54 #define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
56 // Maximum size of the pages cache per connection.
57 #define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
59 mozilla::LazyLogModule gStorageLog("mozStorage");
61 // Checks that the protected code is running on the main-thread only if the
62 // connection was also opened on it.
63 #ifdef DEBUG
64 # define CHECK_MAINTHREAD_ABUSE() \
65 do { \
66 NS_WARNING_ASSERTION( \
67 eventTargetOpenedOn == GetMainThreadSerialEventTarget() || \
68 !NS_IsMainThread(), \
69 "Using Storage synchronous API on main-thread, but " \
70 "the connection was opened on another thread."); \
71 } while (0)
72 #else
73 # define CHECK_MAINTHREAD_ABUSE() \
74 do { /* Nothing */ \
75 } while (0)
76 #endif
78 namespace mozilla::storage {
80 using mozilla::dom::quota::QuotaObject;
81 using mozilla::Telemetry::AccumulateCategoricalKeyed;
82 using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN;
83 using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY;
85 namespace {
87 int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
88 if (NS_SUCCEEDED(aXPCOMResultCode)) {
89 return SQLITE_OK;
92 switch (aXPCOMResultCode) {
93 case NS_ERROR_FILE_CORRUPTED:
94 return SQLITE_CORRUPT;
95 case NS_ERROR_FILE_ACCESS_DENIED:
96 return SQLITE_CANTOPEN;
97 case NS_ERROR_STORAGE_BUSY:
98 return SQLITE_BUSY;
99 case NS_ERROR_FILE_IS_LOCKED:
100 return SQLITE_LOCKED;
101 case NS_ERROR_FILE_READ_ONLY:
102 return SQLITE_READONLY;
103 case NS_ERROR_STORAGE_IOERR:
104 return SQLITE_IOERR;
105 case NS_ERROR_FILE_NO_DEVICE_SPACE:
106 return SQLITE_FULL;
107 case NS_ERROR_OUT_OF_MEMORY:
108 return SQLITE_NOMEM;
109 case NS_ERROR_UNEXPECTED:
110 return SQLITE_MISUSE;
111 case NS_ERROR_ABORT:
112 return SQLITE_ABORT;
113 case NS_ERROR_STORAGE_CONSTRAINT:
114 return SQLITE_CONSTRAINT;
115 default:
116 return SQLITE_ERROR;
119 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!");
122 ////////////////////////////////////////////////////////////////////////////////
123 //// Variant Specialization Functions (variantToSQLiteT)
125 int sqlite3_T_int(sqlite3_context* aCtx, int aValue) {
126 ::sqlite3_result_int(aCtx, aValue);
127 return SQLITE_OK;
130 int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
131 ::sqlite3_result_int64(aCtx, aValue);
132 return SQLITE_OK;
135 int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
136 ::sqlite3_result_double(aCtx, aValue);
137 return SQLITE_OK;
140 int sqlite3_T_text(sqlite3_context* aCtx, const nsCString& aValue) {
141 CheckedInt<int32_t> length(aValue.Length());
142 if (!length.isValid()) {
143 return SQLITE_MISUSE;
145 ::sqlite3_result_text(aCtx, aValue.get(), length.value(), SQLITE_TRANSIENT);
146 return SQLITE_OK;
149 int sqlite3_T_text16(sqlite3_context* aCtx, const nsString& aValue) {
150 CheckedInt<int32_t> n_bytes =
151 CheckedInt<int32_t>(aValue.Length()) * sizeof(char16_t);
152 if (!n_bytes.isValid()) {
153 return SQLITE_MISUSE;
155 ::sqlite3_result_text16(aCtx, aValue.get(), n_bytes.value(),
156 SQLITE_TRANSIENT);
157 return SQLITE_OK;
160 int sqlite3_T_null(sqlite3_context* aCtx) {
161 ::sqlite3_result_null(aCtx);
162 return SQLITE_OK;
165 int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
166 ::sqlite3_result_blob(aCtx, aData, aSize, free);
167 return SQLITE_OK;
170 #include "variantToSQLiteT_impl.h"
172 ////////////////////////////////////////////////////////////////////////////////
173 //// Modules
175 struct Module {
176 const char* name;
177 int (*registerFunc)(sqlite3*, const char*);
180 Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
182 ////////////////////////////////////////////////////////////////////////////////
183 //// Local Functions
185 int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
186 switch (aReason) {
187 case SQLITE_TRACE_STMT: {
188 // aP is a pointer to the prepared statement.
189 sqlite3_stmt* stmt = static_cast<sqlite3_stmt*>(aP);
190 // aX is a pointer to a string containing the unexpanded SQL or a comment,
191 // starting with "--"" in case of a trigger.
192 char* expanded = static_cast<char*>(aX);
193 // Simulate what sqlite_trace was doing.
194 if (!::strncmp(expanded, "--", 2)) {
195 MOZ_LOG(gStorageLog, LogLevel::Debug,
196 ("TRACE_STMT on %p: '%s'", aClosure, expanded));
197 } else {
198 char* sql = ::sqlite3_expanded_sql(stmt);
199 MOZ_LOG(gStorageLog, LogLevel::Debug,
200 ("TRACE_STMT on %p: '%s'", aClosure, sql));
201 ::sqlite3_free(sql);
203 break;
205 case SQLITE_TRACE_PROFILE: {
206 // aX is pointer to a 64bit integer containing nanoseconds it took to
207 // execute the last command.
208 sqlite_int64 time = *(static_cast<sqlite_int64*>(aX)) / 1000000;
209 if (time > 0) {
210 MOZ_LOG(gStorageLog, LogLevel::Debug,
211 ("TRACE_TIME on %p: %lldms", aClosure, time));
213 break;
216 return 0;
219 void basicFunctionHelper(sqlite3_context* aCtx, int aArgc,
220 sqlite3_value** aArgv) {
221 void* userData = ::sqlite3_user_data(aCtx);
223 mozIStorageFunction* func = static_cast<mozIStorageFunction*>(userData);
225 RefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
226 if (!arguments) return;
228 nsCOMPtr<nsIVariant> result;
229 nsresult rv = func->OnFunctionCall(arguments, getter_AddRefs(result));
230 if (NS_FAILED(rv)) {
231 nsAutoCString errorMessage;
232 GetErrorName(rv, errorMessage);
233 errorMessage.InsertLiteral("User function returned ", 0);
234 errorMessage.Append('!');
236 NS_WARNING(errorMessage.get());
238 ::sqlite3_result_error(aCtx, errorMessage.get(), -1);
239 ::sqlite3_result_error_code(aCtx, nsresultToSQLiteResult(rv));
240 return;
242 int retcode = variantToSQLiteT(aCtx, result);
243 if (retcode != SQLITE_OK) {
244 NS_WARNING("User function returned invalid data type!");
245 ::sqlite3_result_error(aCtx, "User function returned invalid data type",
246 -1);
250 RefPtr<QuotaObject> GetQuotaObject(sqlite3_file* aFile, bool obfuscatingVFS) {
251 return obfuscatingVFS
252 ? mozilla::storage::obfsvfs::GetQuotaObjectForFile(aFile)
253 : mozilla::storage::quotavfs::GetQuotaObjectForFile(aFile);
257 * This code is heavily based on the sample at:
258 * http://www.sqlite.org/unlock_notify.html
260 class UnlockNotification {
261 public:
262 UnlockNotification()
263 : mMutex("UnlockNotification mMutex"),
264 mCondVar(mMutex, "UnlockNotification condVar"),
265 mSignaled(false) {}
267 void Wait() {
268 MutexAutoLock lock(mMutex);
269 while (!mSignaled) {
270 (void)mCondVar.Wait();
274 void Signal() {
275 MutexAutoLock lock(mMutex);
276 mSignaled = true;
277 (void)mCondVar.Notify();
280 private:
281 Mutex mMutex MOZ_UNANNOTATED;
282 CondVar mCondVar;
283 bool mSignaled;
286 void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
287 for (int i = 0; i < aArgsSize; i++) {
288 UnlockNotification* notification =
289 static_cast<UnlockNotification*>(aArgs[i]);
290 notification->Signal();
294 int WaitForUnlockNotify(sqlite3* aDatabase) {
295 UnlockNotification notification;
296 int srv =
297 ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, &notification);
298 MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
299 if (srv == SQLITE_OK) {
300 notification.Wait();
303 return srv;
306 ////////////////////////////////////////////////////////////////////////////////
307 //// Local Classes
309 class AsyncCloseConnection final : public Runnable {
310 public:
311 AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
312 nsIRunnable* aCallbackEvent)
313 : Runnable("storage::AsyncCloseConnection"),
314 mConnection(aConnection),
315 mNativeConnection(aNativeConnection),
316 mCallbackEvent(aCallbackEvent) {}
318 NS_IMETHOD Run() override {
319 // Make sure we don't dispatch to the current thread.
320 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn));
322 nsCOMPtr<nsIRunnable> event =
323 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
324 mConnection, &Connection::shutdownAsyncThread);
325 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event));
327 // Internal close.
328 (void)mConnection->internalClose(mNativeConnection);
330 // Callback
331 if (mCallbackEvent) {
332 nsCOMPtr<nsIThread> thread;
333 (void)NS_GetMainThread(getter_AddRefs(thread));
334 (void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
337 return NS_OK;
340 ~AsyncCloseConnection() override {
341 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
342 mConnection.forget());
343 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
344 mCallbackEvent.forget());
347 private:
348 RefPtr<Connection> mConnection;
349 sqlite3* mNativeConnection;
350 nsCOMPtr<nsIRunnable> mCallbackEvent;
354 * An event used to initialize the clone of a connection.
356 * Must be executed on the clone's async execution thread.
358 class AsyncInitializeClone final : public Runnable {
359 public:
361 * @param aConnection The connection being cloned.
362 * @param aClone The clone.
363 * @param aReadOnly If |true|, the clone is read only.
364 * @param aCallback A callback to trigger once initialization
365 * is complete. This event will be called on
366 * aClone->eventTargetOpenedOn.
368 AsyncInitializeClone(Connection* aConnection, Connection* aClone,
369 const bool aReadOnly,
370 mozIStorageCompletionCallback* aCallback)
371 : Runnable("storage::AsyncInitializeClone"),
372 mConnection(aConnection),
373 mClone(aClone),
374 mReadOnly(aReadOnly),
375 mCallback(aCallback) {
376 MOZ_ASSERT(NS_IsMainThread());
379 NS_IMETHOD Run() override {
380 MOZ_ASSERT(!NS_IsMainThread());
381 nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
382 if (NS_FAILED(rv)) {
383 return Dispatch(rv, nullptr);
385 return Dispatch(NS_OK,
386 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
389 private:
390 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
391 RefPtr<CallbackComplete> event =
392 new CallbackComplete(aResult, aValue, mCallback.forget());
393 return mClone->eventTargetOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
396 ~AsyncInitializeClone() override {
397 nsCOMPtr<nsIThread> thread;
398 DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
399 MOZ_ASSERT(NS_SUCCEEDED(rv));
401 // Handle ambiguous nsISupports inheritance.
402 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
403 mConnection.forget());
404 NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
406 // Generally, the callback will be released by CallbackComplete.
407 // However, if for some reason Run() is not executed, we still
408 // need to ensure that it is released here.
409 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
410 mCallback.forget());
413 RefPtr<Connection> mConnection;
414 RefPtr<Connection> mClone;
415 const bool mReadOnly;
416 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
420 * A listener for async connection closing.
422 class CloseListener final : public mozIStorageCompletionCallback {
423 public:
424 NS_DECL_ISUPPORTS
425 CloseListener() : mClosed(false) {}
427 NS_IMETHOD Complete(nsresult, nsISupports*) override {
428 mClosed = true;
429 return NS_OK;
432 bool mClosed;
434 private:
435 ~CloseListener() = default;
438 NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)
440 class AsyncVacuumEvent final : public Runnable {
441 public:
442 AsyncVacuumEvent(Connection* aConnection,
443 mozIStorageCompletionCallback* aCallback,
444 bool aUseIncremental, int32_t aSetPageSize)
445 : Runnable("storage::AsyncVacuum"),
446 mConnection(aConnection),
447 mCallback(aCallback),
448 mUseIncremental(aUseIncremental),
449 mSetPageSize(aSetPageSize),
450 mStatus(NS_ERROR_UNEXPECTED) {}
452 NS_IMETHOD Run() override {
453 // This is initially dispatched to the helper thread, then re-dispatched
454 // to the opener thread, where it will callback.
455 if (IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn)) {
456 // Send the completion event.
457 if (mCallback) {
458 mozilla::Unused << mCallback->Complete(mStatus, nullptr);
460 return NS_OK;
463 // Ensure to invoke the callback regardless of errors.
464 auto guard = MakeScopeExit([&]() {
465 mConnection->mIsStatementOnHelperThreadInterruptible = false;
466 mozilla::Unused << mConnection->eventTargetOpenedOn->Dispatch(
467 this, NS_DISPATCH_NORMAL);
470 // Get list of attached databases.
471 nsCOMPtr<mozIStorageStatement> stmt;
472 nsresult rv = mConnection->CreateStatement(MOZ_STORAGE_UNIQUIFY_QUERY_STR
473 "PRAGMA database_list"_ns,
474 getter_AddRefs(stmt));
475 NS_ENSURE_SUCCESS(rv, rv);
476 // We must accumulate names and loop through them later, otherwise VACUUM
477 // will see an ongoing statement and bail out.
478 nsTArray<nsCString> schemaNames;
479 bool hasResult = false;
480 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
481 nsAutoCString name;
482 rv = stmt->GetUTF8String(1, name);
483 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("temp")) {
484 schemaNames.AppendElement(name);
487 mStatus = NS_OK;
488 // Mark this vacuum as an interruptible operation, so it can be interrupted
489 // if the connection closes during shutdown.
490 mConnection->mIsStatementOnHelperThreadInterruptible = true;
491 for (const nsCString& schemaName : schemaNames) {
492 rv = this->Vacuum(schemaName);
493 if (NS_FAILED(rv)) {
494 // This is sub-optimal since it's only keeping the last error reason,
495 // but it will do for now.
496 mStatus = rv;
499 return mStatus;
502 nsresult Vacuum(const nsACString& aSchemaName) {
503 // Abort if we're in shutdown.
504 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
505 return NS_ERROR_ABORT;
507 int32_t removablePages = mConnection->RemovablePagesInFreeList(aSchemaName);
508 if (!removablePages) {
509 // There's no empty pages to remove, so skip this vacuum for now.
510 return NS_OK;
512 nsresult rv;
513 bool needsFullVacuum = true;
515 if (mSetPageSize) {
516 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
517 query.Append(aSchemaName);
518 query.AppendLiteral(".page_size = ");
519 query.AppendInt(mSetPageSize);
520 nsCOMPtr<mozIStorageStatement> stmt;
521 rv = mConnection->ExecuteSimpleSQL(query);
522 NS_ENSURE_SUCCESS(rv, rv);
525 // Check auto_vacuum.
527 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
528 query.Append(aSchemaName);
529 query.AppendLiteral(".auto_vacuum");
530 nsCOMPtr<mozIStorageStatement> stmt;
531 rv = mConnection->CreateStatement(query, getter_AddRefs(stmt));
532 NS_ENSURE_SUCCESS(rv, rv);
533 bool hasResult = false;
534 bool changeAutoVacuum = false;
535 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
536 bool isIncrementalVacuum = stmt->AsInt32(0) == 2;
537 changeAutoVacuum = isIncrementalVacuum != mUseIncremental;
538 if (isIncrementalVacuum && !changeAutoVacuum) {
539 needsFullVacuum = false;
542 // Changing auto_vacuum is only supported on the main schema.
543 if (aSchemaName.EqualsLiteral("main") && changeAutoVacuum) {
544 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
545 query.Append(aSchemaName);
546 query.AppendLiteral(".auto_vacuum = ");
547 query.AppendInt(mUseIncremental ? 2 : 0);
548 rv = mConnection->ExecuteSimpleSQL(query);
549 NS_ENSURE_SUCCESS(rv, rv);
553 if (needsFullVacuum) {
554 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "VACUUM ");
555 query.Append(aSchemaName);
556 rv = mConnection->ExecuteSimpleSQL(query);
557 // TODO (Bug 1818039): Report failed vacuum telemetry.
558 NS_ENSURE_SUCCESS(rv, rv);
559 } else {
560 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
561 query.Append(aSchemaName);
562 query.AppendLiteral(".incremental_vacuum(");
563 query.AppendInt(removablePages);
564 query.AppendLiteral(")");
565 rv = mConnection->ExecuteSimpleSQL(query);
566 NS_ENSURE_SUCCESS(rv, rv);
569 return NS_OK;
572 ~AsyncVacuumEvent() override {
573 NS_ReleaseOnMainThread("AsyncVacuum::mConnection", mConnection.forget());
574 NS_ReleaseOnMainThread("AsyncVacuum::mCallback", mCallback.forget());
577 private:
578 RefPtr<Connection> mConnection;
579 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
580 bool mUseIncremental;
581 int32_t mSetPageSize;
582 Atomic<nsresult> mStatus;
585 } // namespace
587 ////////////////////////////////////////////////////////////////////////////////
588 //// Connection
590 Connection::Connection(Service* aService, int aFlags,
591 ConnectionOperation aSupportedOperations,
592 const nsCString& aTelemetryFilename, bool aInterruptible,
593 bool aIgnoreLockingMode)
594 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
595 sharedDBMutex("Connection::sharedDBMutex"),
596 eventTargetOpenedOn(WrapNotNull(GetCurrentSerialEventTarget())),
597 mIsStatementOnHelperThreadInterruptible(false),
598 mDBConn(nullptr),
599 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
600 mDestroying(false),
601 mProgressHandler(nullptr),
602 mStorageService(aService),
603 mFlags(aFlags),
604 mTransactionNestingLevel(0),
605 mSupportedOperations(aSupportedOperations),
606 mInterruptible(aSupportedOperations == Connection::ASYNCHRONOUS ||
607 aInterruptible),
608 mIgnoreLockingMode(aIgnoreLockingMode),
609 mAsyncExecutionThreadShuttingDown(false),
610 mConnectionClosed(false),
611 mGrowthChunkSize(0) {
612 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
613 "Can't ignore locking for a non-readonly connection!");
614 mStorageService->registerConnection(this);
615 MOZ_ASSERT(!aTelemetryFilename.IsEmpty(),
616 "A telemetry filename should have been passed-in.");
617 mTelemetryFilename.Assign(aTelemetryFilename);
620 Connection::~Connection() {
621 // Failsafe Close() occurs in our custom Release method because of
622 // complications related to Close() potentially invoking AsyncClose() which
623 // will increment our refcount.
624 MOZ_ASSERT(!mAsyncExecutionThread,
625 "The async thread has not been shutdown properly!");
628 NS_IMPL_ADDREF(Connection)
630 NS_INTERFACE_MAP_BEGIN(Connection)
631 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
632 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
633 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
634 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
635 NS_INTERFACE_MAP_END
637 // This is identical to what NS_IMPL_RELEASE provides, but with the
638 // extra |1 == count| case.
639 NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
640 MOZ_ASSERT(0 != mRefCnt, "dup release");
641 nsrefcnt count = --mRefCnt;
642 NS_LOG_RELEASE(this, count, "Connection");
643 if (1 == count) {
644 // If the refcount went to 1, the single reference must be from
645 // gService->mConnections (in class |Service|). And the code calling
646 // Release is either:
647 // - The "user" code that had created the connection, releasing on any
648 // thread.
649 // - One of Service's getConnections() callers had acquired a strong
650 // reference to the Connection that out-lived the last "user" reference,
651 // and now that just got dropped. Note that this reference could be
652 // getting dropped on the main thread or Connection->eventTargetOpenedOn
653 // (because of the NewRunnableMethod used by minimizeMemory).
655 // Either way, we should now perform our failsafe Close() and unregister.
656 // However, we only want to do this once, and the reality is that our
657 // refcount could go back up above 1 and down again at any time if we are
658 // off the main thread and getConnections() gets called on the main thread,
659 // so we use an atomic here to do this exactly once.
660 if (mDestroying.compareExchange(false, true)) {
661 // Close the connection, dispatching to the opening event target if we're
662 // not on that event target already and that event target is still
663 // accepting runnables. We do this because it's possible we're on the main
664 // thread because of getConnections(), and we REALLY don't want to
665 // transfer I/O to the main thread if we can avoid it.
666 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
667 // This could cause SpinningSynchronousClose() to be invoked and AddRef
668 // triggered for AsyncCloseConnection's strong ref if the conn was ever
669 // use for async purposes. (Main-thread only, though.)
670 Unused << synchronousClose();
671 } else {
672 nsCOMPtr<nsIRunnable> event =
673 NewRunnableMethod("storage::Connection::synchronousClose", this,
674 &Connection::synchronousClose);
675 if (NS_FAILED(eventTargetOpenedOn->Dispatch(event.forget(),
676 NS_DISPATCH_NORMAL))) {
677 // The event target was dead and so we've just leaked our runnable.
678 // This should not happen because our non-main-thread consumers should
679 // be explicitly closing their connections, not relying on us to close
680 // them for them. (It's okay to let a statement go out of scope for
681 // automatic cleanup, but not a Connection.)
682 MOZ_ASSERT(false,
683 "Leaked Connection::synchronousClose(), ownership fail.");
684 Unused << synchronousClose();
688 // This will drop its strong reference right here, right now.
689 mStorageService->unregisterConnection(this);
691 } else if (0 == count) {
692 mRefCnt = 1; /* stabilize */
693 #if 0 /* enable this to find non-threadsafe destructors: */
694 NS_ASSERT_OWNINGTHREAD(Connection);
695 #endif
696 delete (this);
697 return 0;
699 return count;
702 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
703 int32_t* aMaxValue) {
704 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
705 int curr = 0, max = 0;
706 DebugOnly<int> rc =
707 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
708 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
709 if (aMaxValue) *aMaxValue = max;
710 return curr;
713 nsIEventTarget* Connection::getAsyncExecutionTarget() {
714 NS_ENSURE_TRUE(IsOnCurrentSerialEventTarget(eventTargetOpenedOn), nullptr);
716 // Don't return the asynchronous event target if we are shutting down.
717 if (mAsyncExecutionThreadShuttingDown) {
718 return nullptr;
721 // Create the async event target if there's none yet.
722 if (!mAsyncExecutionThread) {
723 // Names start with "sqldb:" followed by a recognizable name, like the
724 // database file name, or a specially crafted name like "memory".
725 // This name will be surfaced on https://crash-stats.mozilla.org, so any
726 // sensitive part of the file name (e.g. an URL origin) should be replaced
727 // by passing an explicit telemetryName to openDatabaseWithFileURL.
728 nsAutoCString name("sqldb:"_ns);
729 name.Append(mTelemetryFilename);
730 static nsThreadPoolNaming naming;
731 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName(name),
732 getter_AddRefs(mAsyncExecutionThread));
733 if (NS_FAILED(rv)) {
734 NS_WARNING("Failed to create async thread.");
735 return nullptr;
737 mAsyncExecutionThread->SetNameForWakeupTelemetry("mozStorage (all)"_ns);
740 return mAsyncExecutionThread;
743 void Connection::RecordOpenStatus(nsresult rv) {
744 nsCString histogramKey = mTelemetryFilename;
746 if (histogramKey.IsEmpty()) {
747 histogramKey.AssignLiteral("unknown");
750 if (NS_SUCCEEDED(rv)) {
751 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
752 return;
755 switch (rv) {
756 case NS_ERROR_FILE_CORRUPTED:
757 AccumulateCategoricalKeyed(histogramKey,
758 LABELS_SQLITE_STORE_OPEN::corrupt);
759 break;
760 case NS_ERROR_STORAGE_IOERR:
761 AccumulateCategoricalKeyed(histogramKey,
762 LABELS_SQLITE_STORE_OPEN::diskio);
763 break;
764 case NS_ERROR_FILE_ACCESS_DENIED:
765 case NS_ERROR_FILE_IS_LOCKED:
766 case NS_ERROR_FILE_READ_ONLY:
767 AccumulateCategoricalKeyed(histogramKey,
768 LABELS_SQLITE_STORE_OPEN::access);
769 break;
770 case NS_ERROR_FILE_NO_DEVICE_SPACE:
771 AccumulateCategoricalKeyed(histogramKey,
772 LABELS_SQLITE_STORE_OPEN::diskspace);
773 break;
774 default:
775 AccumulateCategoricalKeyed(histogramKey,
776 LABELS_SQLITE_STORE_OPEN::failure);
780 void Connection::RecordQueryStatus(int srv) {
781 nsCString histogramKey = mTelemetryFilename;
783 if (histogramKey.IsEmpty()) {
784 histogramKey.AssignLiteral("unknown");
787 switch (srv) {
788 case SQLITE_OK:
789 case SQLITE_ROW:
790 case SQLITE_DONE:
792 // Note that these are returned when we intentionally cancel a statement so
793 // they aren't indicating a failure.
794 case SQLITE_ABORT:
795 case SQLITE_INTERRUPT:
796 AccumulateCategoricalKeyed(histogramKey,
797 LABELS_SQLITE_STORE_QUERY::success);
798 break;
799 case SQLITE_CORRUPT:
800 case SQLITE_NOTADB:
801 AccumulateCategoricalKeyed(histogramKey,
802 LABELS_SQLITE_STORE_QUERY::corrupt);
803 break;
804 case SQLITE_PERM:
805 case SQLITE_CANTOPEN:
806 case SQLITE_LOCKED:
807 case SQLITE_READONLY:
808 AccumulateCategoricalKeyed(histogramKey,
809 LABELS_SQLITE_STORE_QUERY::access);
810 break;
811 case SQLITE_IOERR:
812 case SQLITE_NOLFS:
813 AccumulateCategoricalKeyed(histogramKey,
814 LABELS_SQLITE_STORE_QUERY::diskio);
815 break;
816 case SQLITE_FULL:
817 case SQLITE_TOOBIG:
818 AccumulateCategoricalKeyed(histogramKey,
819 LABELS_SQLITE_STORE_QUERY::diskspace);
820 break;
821 case SQLITE_CONSTRAINT:
822 case SQLITE_RANGE:
823 case SQLITE_MISMATCH:
824 case SQLITE_MISUSE:
825 AccumulateCategoricalKeyed(histogramKey,
826 LABELS_SQLITE_STORE_QUERY::misuse);
827 break;
828 case SQLITE_BUSY:
829 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_QUERY::busy);
830 break;
831 default:
832 AccumulateCategoricalKeyed(histogramKey,
833 LABELS_SQLITE_STORE_QUERY::failure);
837 nsresult Connection::initialize(const nsACString& aStorageKey,
838 const nsACString& aName) {
839 MOZ_ASSERT(aStorageKey.Equals(kMozStorageMemoryStorageKey));
840 NS_ASSERTION(!connectionReady(),
841 "Initialize called on already opened database!");
842 MOZ_ASSERT(!mIgnoreLockingMode, "Can't ignore locking on an in-memory db.");
843 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
845 mStorageKey = aStorageKey;
846 mName = aName;
848 // in memory database requested, sqlite uses a magic file name
850 const nsAutoCString path =
851 mName.IsEmpty() ? nsAutoCString(":memory:"_ns)
852 : "file:"_ns + mName + "?mode=memory&cache=shared"_ns;
854 int srv = ::sqlite3_open_v2(path.get(), &mDBConn, mFlags,
855 basevfs::GetVFSName(true));
856 if (srv != SQLITE_OK) {
857 mDBConn = nullptr;
858 nsresult rv = convertResultCode(srv);
859 RecordOpenStatus(rv);
860 return rv;
863 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
864 srv =
865 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
866 MOZ_ASSERT(srv == SQLITE_OK,
867 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
868 #endif
870 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
871 // database.
873 nsresult rv = initializeInternal();
874 RecordOpenStatus(rv);
875 NS_ENSURE_SUCCESS(rv, rv);
877 return NS_OK;
880 nsresult Connection::initialize(nsIFile* aDatabaseFile) {
881 NS_ASSERTION(aDatabaseFile, "Passed null file!");
882 NS_ASSERTION(!connectionReady(),
883 "Initialize called on already opened database!");
884 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
886 // Do not set mFileURL here since this is database does not have an associated
887 // URL.
888 mDatabaseFile = aDatabaseFile;
890 nsAutoString path;
891 nsresult rv = aDatabaseFile->GetPath(path);
892 NS_ENSURE_SUCCESS(rv, rv);
894 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
895 int srv;
896 if (mIgnoreLockingMode) {
897 exclusive = false;
898 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
899 "readonly-immutable-nolock");
900 } else {
901 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
902 basevfs::GetVFSName(exclusive));
903 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
904 // Retry without trying to get an exclusive lock.
905 exclusive = false;
906 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
907 mFlags, basevfs::GetVFSName(false));
910 if (srv != SQLITE_OK) {
911 mDBConn = nullptr;
912 rv = convertResultCode(srv);
913 RecordOpenStatus(rv);
914 return rv;
917 rv = initializeInternal();
918 if (exclusive &&
919 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
920 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
921 // some cases it may successfully open the database and then lock on the
922 // first query execution. When initializeInternal fails it closes the
923 // connection, so we can try to restart it in non-exclusive mode.
924 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
925 basevfs::GetVFSName(false));
926 if (srv == SQLITE_OK) {
927 rv = initializeInternal();
931 RecordOpenStatus(rv);
932 NS_ENSURE_SUCCESS(rv, rv);
934 return NS_OK;
937 nsresult Connection::initialize(nsIFileURL* aFileURL) {
938 NS_ASSERTION(aFileURL, "Passed null file URL!");
939 NS_ASSERTION(!connectionReady(),
940 "Initialize called on already opened database!");
941 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
943 nsCOMPtr<nsIFile> databaseFile;
944 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
945 NS_ENSURE_SUCCESS(rv, rv);
947 // Set both mDatabaseFile and mFileURL here.
948 mFileURL = aFileURL;
949 mDatabaseFile = databaseFile;
951 nsAutoCString spec;
952 rv = aFileURL->GetSpec(spec);
953 NS_ENSURE_SUCCESS(rv, rv);
955 // If there is a key specified, we need to use the obfuscating VFS.
956 nsAutoCString query;
957 rv = aFileURL->GetQuery(query);
958 NS_ENSURE_SUCCESS(rv, rv);
960 bool hasKey = false;
961 bool hasDirectoryLockId = false;
963 MOZ_ALWAYS_TRUE(URLParams::Parse(
964 query, [&hasKey, &hasDirectoryLockId](const nsAString& aName,
965 const nsAString& aValue) {
966 if (aName.EqualsLiteral("key")) {
967 hasKey = true;
968 return true;
970 if (aName.EqualsLiteral("directoryLockId")) {
971 hasDirectoryLockId = true;
972 return true;
974 return true;
975 }));
977 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
979 const char* const vfs = hasKey ? obfsvfs::GetVFSName()
980 : hasDirectoryLockId ? quotavfs::GetVFSName()
981 : basevfs::GetVFSName(exclusive);
983 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
984 if (srv != SQLITE_OK) {
985 mDBConn = nullptr;
986 rv = convertResultCode(srv);
987 RecordOpenStatus(rv);
988 return rv;
991 rv = initializeInternal();
992 RecordOpenStatus(rv);
993 NS_ENSURE_SUCCESS(rv, rv);
995 return NS_OK;
998 nsresult Connection::initializeInternal() {
999 MOZ_ASSERT(mDBConn);
1000 auto guard = MakeScopeExit([&]() { initializeFailed(); });
1002 mConnectionClosed = false;
1004 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
1005 DebugOnly<int> srv2 =
1006 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
1007 MOZ_ASSERT(srv2 == SQLITE_OK,
1008 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
1009 #endif
1011 // Properly wrap the database handle's mutex.
1012 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
1014 // SQLite tracing can slow down queries (especially long queries)
1015 // significantly. Don't trace unless the user is actively monitoring SQLite.
1016 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1017 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
1018 tracefunc, this);
1020 MOZ_LOG(
1021 gStorageLog, LogLevel::Debug,
1022 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
1025 int64_t pageSize = Service::kDefaultPageSize;
1027 // Set page_size to the preferred default value. This is effective only if
1028 // the database has just been created, otherwise, if the database does not
1029 // use WAL journal mode, a VACUUM operation will updated its page_size.
1030 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1031 "PRAGMA page_size = ");
1032 pageSizeQuery.AppendInt(pageSize);
1033 int srv = executeSql(mDBConn, pageSizeQuery.get());
1034 if (srv != SQLITE_OK) {
1035 return convertResultCode(srv);
1038 // Setting the cache_size forces the database open, verifying if it is valid
1039 // or corrupt. So this is executed regardless it being actually needed.
1040 // The cache_size is calculated from the actual page_size, to save memory.
1041 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1042 "PRAGMA cache_size = ");
1043 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
1044 srv = executeSql(mDBConn, cacheSizeQuery.get());
1045 if (srv != SQLITE_OK) {
1046 return convertResultCode(srv);
1049 // Register our built-in SQL functions.
1050 srv = registerFunctions(mDBConn);
1051 if (srv != SQLITE_OK) {
1052 return convertResultCode(srv);
1055 // Register our built-in SQL collating sequences.
1056 srv = registerCollations(mDBConn, mStorageService);
1057 if (srv != SQLITE_OK) {
1058 return convertResultCode(srv);
1061 // Set the default synchronous value. Each consumer can switch this
1062 // accordingly to their needs.
1063 #if defined(ANDROID)
1064 // Android prefers synchronous = OFF for performance reasons.
1065 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
1066 #else
1067 // Normal is the suggested value for WAL journals.
1068 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
1069 #endif
1071 // Initialization succeeded, we can stop guarding for failures.
1072 guard.release();
1073 return NS_OK;
1076 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
1077 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1078 nsresult rv = aStorageFile
1079 ? initialize(aStorageFile)
1080 : initialize(kMozStorageMemoryStorageKey, VoidCString());
1081 if (NS_FAILED(rv)) {
1082 // Shutdown the async thread, since initialization failed.
1083 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1084 mAsyncExecutionThreadShuttingDown = true;
1085 nsCOMPtr<nsIRunnable> event =
1086 NewRunnableMethod("Connection::shutdownAsyncThread", this,
1087 &Connection::shutdownAsyncThread);
1088 Unused << NS_DispatchToMainThread(event);
1090 return rv;
1093 void Connection::initializeFailed() {
1095 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1096 mConnectionClosed = true;
1098 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
1099 mDBConn = nullptr;
1100 sharedDBMutex.destroy();
1103 nsresult Connection::databaseElementExists(
1104 enum DatabaseElementType aElementType, const nsACString& aElementName,
1105 bool* _exists) {
1106 if (!connectionReady()) {
1107 return NS_ERROR_NOT_AVAILABLE;
1109 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1110 if (NS_FAILED(rv)) {
1111 return rv;
1114 // When constructing the query, make sure to SELECT the correct db's
1115 // sqlite_master if the user is prefixing the element with a specific db. ex:
1116 // sample.test
1117 nsCString query("SELECT name FROM (SELECT * FROM ");
1118 nsDependentCSubstring element;
1119 int32_t ind = aElementName.FindChar('.');
1120 if (ind == kNotFound) {
1121 element.Assign(aElementName);
1122 } else {
1123 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
1124 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
1125 query.Append(db);
1127 query.AppendLiteral(
1128 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
1129 "'");
1131 switch (aElementType) {
1132 case INDEX:
1133 query.AppendLiteral("index");
1134 break;
1135 case TABLE:
1136 query.AppendLiteral("table");
1137 break;
1139 query.AppendLiteral("' AND name ='");
1140 query.Append(element);
1141 query.Append('\'');
1143 sqlite3_stmt* stmt;
1144 int srv = prepareStatement(mDBConn, query, &stmt);
1145 if (srv != SQLITE_OK) {
1146 RecordQueryStatus(srv);
1147 return convertResultCode(srv);
1150 srv = stepStatement(mDBConn, stmt);
1151 // we just care about the return value from step
1152 (void)::sqlite3_finalize(stmt);
1154 RecordQueryStatus(srv);
1156 if (srv == SQLITE_ROW) {
1157 *_exists = true;
1158 return NS_OK;
1160 if (srv == SQLITE_DONE) {
1161 *_exists = false;
1162 return NS_OK;
1165 return convertResultCode(srv);
1168 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
1169 sharedDBMutex.assertCurrentThreadOwns();
1171 for (const auto& data : mFunctions.Values()) {
1172 if (data.function == aInstance) {
1173 return true;
1176 return false;
1179 /* static */
1180 int Connection::sProgressHelper(void* aArg) {
1181 Connection* _this = static_cast<Connection*>(aArg);
1182 return _this->progressHandler();
1185 int Connection::progressHandler() {
1186 sharedDBMutex.assertCurrentThreadOwns();
1187 if (mProgressHandler) {
1188 bool result;
1189 nsresult rv = mProgressHandler->OnProgress(this, &result);
1190 if (NS_FAILED(rv)) return 0; // Don't break request
1191 return result ? 1 : 0;
1193 return 0;
1196 nsresult Connection::setClosedState() {
1197 // Flag that we are shutting down the async thread, so that
1198 // getAsyncExecutionTarget knows not to expose/create the async thread.
1199 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1200 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1202 mAsyncExecutionThreadShuttingDown = true;
1204 // Set the property to null before closing the connection, otherwise the
1205 // other functions in the module may try to use the connection after it is
1206 // closed.
1207 mDBConn = nullptr;
1209 return NS_OK;
1212 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1213 if (aOperationType == ASYNCHRONOUS) {
1214 // Async operations are supported for all connections, on any thread.
1215 return true;
1217 // Sync operations are supported for sync connections (on any thread), and
1218 // async connections on a background thread.
1219 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1220 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1223 nsresult Connection::ensureOperationSupported(
1224 ConnectionOperation aOperationType) {
1225 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1226 #ifdef DEBUG
1227 if (NS_IsMainThread()) {
1228 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1229 Unused << xpc->DebugDumpJSStack(false, false, false);
1231 #endif
1232 MOZ_ASSERT(false,
1233 "Don't use async connections synchronously on the main thread");
1234 return NS_ERROR_NOT_AVAILABLE;
1236 return NS_OK;
1239 bool Connection::isConnectionReadyOnThisThread() {
1240 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1241 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1242 return true;
1244 return connectionReady();
1247 bool Connection::isClosing() {
1248 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1249 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1252 bool Connection::isClosed() {
1253 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1254 return mConnectionClosed;
1257 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1259 bool Connection::isAsyncExecutionThreadAvailable() {
1260 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1261 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1264 void Connection::shutdownAsyncThread() {
1265 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1266 MOZ_ASSERT(mAsyncExecutionThread);
1267 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1269 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1270 mAsyncExecutionThread = nullptr;
1273 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1274 #ifdef DEBUG
1275 { // Make sure we have marked our async thread as shutting down.
1276 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1277 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1278 "Did not call setClosedState!");
1279 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1281 #endif // DEBUG
1283 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1284 nsAutoCString leafName(":memory");
1285 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1286 MOZ_LOG(gStorageLog, LogLevel::Debug,
1287 ("Closing connection to '%s'", leafName.get()));
1290 // At this stage, we may still have statements that need to be
1291 // finalized. Attempt to close the database connection. This will
1292 // always disconnect any virtual tables and cleanly finalize their
1293 // internal statements. Once this is done, closing may fail due to
1294 // unfinalized client statements, in which case we need to finalize
1295 // these statements and close again.
1297 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1298 mConnectionClosed = true;
1301 // Nothing else needs to be done if we don't have a connection here.
1302 if (!aNativeConnection) return NS_OK;
1304 int srv = ::sqlite3_close(aNativeConnection);
1306 if (srv == SQLITE_BUSY) {
1308 // Nothing else should change the connection or statements status until we
1309 // are done here.
1310 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1311 // We still have non-finalized statements. Finalize them.
1312 sqlite3_stmt* stmt = nullptr;
1313 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1314 MOZ_LOG(gStorageLog, LogLevel::Debug,
1315 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1316 stmt));
1318 #ifdef DEBUG
1319 SmprintfPointer msg = ::mozilla::Smprintf(
1320 "SQL statement '%s' (%p) should have been finalized before closing "
1321 "the connection",
1322 ::sqlite3_sql(stmt), stmt);
1323 NS_WARNING(msg.get());
1324 #endif // DEBUG
1326 srv = ::sqlite3_finalize(stmt);
1328 #ifdef DEBUG
1329 if (srv != SQLITE_OK) {
1330 SmprintfPointer msg = ::mozilla::Smprintf(
1331 "Could not finalize SQL statement (%p)", stmt);
1332 NS_WARNING(msg.get());
1334 #endif // DEBUG
1336 // Ensure that the loop continues properly, whether closing has
1337 // succeeded or not.
1338 if (srv == SQLITE_OK) {
1339 stmt = nullptr;
1342 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1343 // again, since Sqlite will try to acquire it.
1346 // Now that all statements have been finalized, we
1347 // should be able to close.
1348 srv = ::sqlite3_close(aNativeConnection);
1349 MOZ_ASSERT(false,
1350 "Had to forcibly close the database connection because not all "
1351 "the statements have been finalized.");
1354 if (srv == SQLITE_OK) {
1355 sharedDBMutex.destroy();
1356 } else {
1357 MOZ_ASSERT(false,
1358 "sqlite3_close failed. There are probably outstanding "
1359 "statements that are listed above!");
1362 return convertResultCode(srv);
1365 nsCString Connection::getFilename() { return mTelemetryFilename; }
1367 int Connection::stepStatement(sqlite3* aNativeConnection,
1368 sqlite3_stmt* aStatement) {
1369 MOZ_ASSERT(aStatement);
1371 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1372 ::sqlite3_sql(aStatement));
1374 bool checkedMainThread = false;
1375 TimeStamp startTime = TimeStamp::Now();
1377 // The connection may have been closed if the executing statement has been
1378 // created and cached after a call to asyncClose() but before the actual
1379 // sqlite3_close(). This usually happens when other tasks using cached
1380 // statements are asynchronously scheduled for execution and any of them ends
1381 // up after asyncClose. See bug 728653 for details.
1382 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1384 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1386 int srv;
1387 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1388 if (!checkedMainThread) {
1389 checkedMainThread = true;
1390 if (::NS_IsMainThread()) {
1391 NS_WARNING("We won't allow blocking on the main thread!");
1392 break;
1396 srv = WaitForUnlockNotify(aNativeConnection);
1397 if (srv != SQLITE_OK) {
1398 break;
1401 ::sqlite3_reset(aStatement);
1404 // Report very slow SQL statements to Telemetry
1405 TimeDuration duration = TimeStamp::Now() - startTime;
1406 const uint32_t threshold = NS_IsMainThread()
1407 ? Telemetry::kSlowSQLThresholdForMainThread
1408 : Telemetry::kSlowSQLThresholdForHelperThreads;
1409 if (duration.ToMilliseconds() >= threshold) {
1410 nsDependentCString statementString(::sqlite3_sql(aStatement));
1411 Telemetry::RecordSlowSQLStatement(
1412 statementString, mTelemetryFilename,
1413 static_cast<uint32_t>(duration.ToMilliseconds()));
1416 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1417 // Drop off the extended result bits of the result code.
1418 return srv & 0xFF;
1421 int Connection::prepareStatement(sqlite3* aNativeConnection,
1422 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1423 // We should not even try to prepare statements after the connection has
1424 // been closed.
1425 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1427 bool checkedMainThread = false;
1429 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1431 int srv;
1432 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1433 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1434 if (!checkedMainThread) {
1435 checkedMainThread = true;
1436 if (::NS_IsMainThread()) {
1437 NS_WARNING("We won't allow blocking on the main thread!");
1438 break;
1442 srv = WaitForUnlockNotify(aNativeConnection);
1443 if (srv != SQLITE_OK) {
1444 break;
1448 if (srv != SQLITE_OK) {
1449 nsCString warnMsg;
1450 warnMsg.AppendLiteral("The SQL statement '");
1451 warnMsg.Append(aSQL);
1452 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1453 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1455 #ifdef DEBUG
1456 NS_WARNING(warnMsg.get());
1457 #endif
1458 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1461 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1462 // Drop off the extended result bits of the result code.
1463 int rc = srv & 0xFF;
1464 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1465 // The callers of this function are used to only checking the return value,
1466 // so it is safer to return an error code.
1467 if (rc == SQLITE_OK && *_stmt == nullptr) {
1468 return SQLITE_MISUSE;
1471 return rc;
1474 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1475 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1477 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1479 TimeStamp startTime = TimeStamp::Now();
1480 int srv =
1481 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1482 RecordQueryStatus(srv);
1484 // Report very slow SQL statements to Telemetry
1485 TimeDuration duration = TimeStamp::Now() - startTime;
1486 const uint32_t threshold = NS_IsMainThread()
1487 ? Telemetry::kSlowSQLThresholdForMainThread
1488 : Telemetry::kSlowSQLThresholdForHelperThreads;
1489 if (duration.ToMilliseconds() >= threshold) {
1490 nsDependentCString statementString(aSqlString);
1491 Telemetry::RecordSlowSQLStatement(
1492 statementString, mTelemetryFilename,
1493 static_cast<uint32_t>(duration.ToMilliseconds()));
1496 return srv;
1499 ////////////////////////////////////////////////////////////////////////////////
1500 //// nsIInterfaceRequestor
1502 NS_IMETHODIMP
1503 Connection::GetInterface(const nsIID& aIID, void** _result) {
1504 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1505 nsIEventTarget* background = getAsyncExecutionTarget();
1506 NS_IF_ADDREF(background);
1507 *_result = background;
1508 return NS_OK;
1510 return NS_ERROR_NO_INTERFACE;
1513 ////////////////////////////////////////////////////////////////////////////////
1514 //// mozIStorageConnection
1516 NS_IMETHODIMP
1517 Connection::Close() {
1518 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1519 if (NS_FAILED(rv)) {
1520 return rv;
1522 return synchronousClose();
1525 nsresult Connection::synchronousClose() {
1526 if (!connectionReady()) {
1527 return NS_ERROR_NOT_INITIALIZED;
1530 #ifdef DEBUG
1531 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1532 // event target. We make this check outside of debug code below in
1533 // setClosedState, but this is here to be explicit.
1534 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1535 #endif // DEBUG
1537 // Make sure we have not executed any asynchronous statements.
1538 // If this fails, the mDBConn may be left open, resulting in a leak.
1539 // We'll try to finalize the pending statements and close the connection.
1540 if (isAsyncExecutionThreadAvailable()) {
1541 #ifdef DEBUG
1542 if (NS_IsMainThread()) {
1543 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1544 Unused << xpc->DebugDumpJSStack(false, false, false);
1546 #endif
1547 MOZ_ASSERT(false,
1548 "Close() was invoked on a connection that executed asynchronous "
1549 "statements. "
1550 "Should have used asyncClose().");
1551 // Try to close the database regardless, to free up resources.
1552 Unused << SpinningSynchronousClose();
1553 return NS_ERROR_UNEXPECTED;
1556 // setClosedState nullifies our connection pointer, so we take a raw pointer
1557 // off it, to pass it through the close procedure.
1558 sqlite3* nativeConn = mDBConn;
1559 nsresult rv = setClosedState();
1560 NS_ENSURE_SUCCESS(rv, rv);
1562 return internalClose(nativeConn);
1565 NS_IMETHODIMP
1566 Connection::SpinningSynchronousClose() {
1567 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1568 if (NS_FAILED(rv)) {
1569 return rv;
1571 if (!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
1572 return NS_ERROR_NOT_SAME_THREAD;
1575 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1576 // Our only existing caller will never have called close; assert if misused
1577 // so that no new callers assume this works after an AsyncClose.
1578 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1579 if (!connectionReady()) {
1580 return NS_ERROR_UNEXPECTED;
1583 RefPtr<CloseListener> listener = new CloseListener();
1584 rv = AsyncClose(listener);
1585 NS_ENSURE_SUCCESS(rv, rv);
1586 MOZ_ALWAYS_TRUE(
1587 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,
1588 [&]() { return listener->mClosed; }));
1589 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1591 return rv;
1594 NS_IMETHODIMP
1595 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1596 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1597 // Check if AsyncClose or Close were already invoked.
1598 if (!connectionReady()) {
1599 return NS_ERROR_NOT_INITIALIZED;
1601 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1602 if (NS_FAILED(rv)) {
1603 return rv;
1606 // The two relevant factors at this point are whether we have a database
1607 // connection and whether we have an async execution thread. Here's what the
1608 // states mean and how we handle them:
1610 // - (mDBConn && asyncThread): The expected case where we are either an
1611 // async connection or a sync connection that has been used asynchronously.
1612 // Either way the caller must call us and not Close(). Nothing surprising
1613 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1614 // async thread.
1616 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1617 // opened the connection synchronously and was planning to use it
1618 // asynchronously, but never got around to using it asynchronously before
1619 // needing to shutdown. This has been observed to happen for the cookie
1620 // service in a case where Firefox shuts itself down almost immediately
1621 // after startup (for unknown reasons). In the Firefox shutdown case,
1622 // we may also fail to create a new async execution thread if one does not
1623 // already exist. (nsThreadManager will refuse to create new threads when
1624 // it has already been told to shutdown.) As such, we need to handle a
1625 // failure to create the async execution thread by falling back to
1626 // synchronous Close() and also dispatching the completion callback because
1627 // at least Places likes to spin a nested event loop that depends on the
1628 // callback being invoked.
1630 // Note that we have considered not trying to spin up the async execution
1631 // thread in this case if it does not already exist, but the overhead of
1632 // thread startup (if successful) is significantly less expensive than the
1633 // worst-case potential I/O hit of synchronously closing a database when we
1634 // could close it asynchronously.
1636 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1637 // OpenAsyncDatabase encountered a problem opening the database. If it
1638 // happened in all cases AsyncInitDatabase would just shut down the thread
1639 // directly and we would avoid this case. But it doesn't, so for simplicity
1640 // and consistency AsyncCloseConnection knows how to handle this and we
1641 // act like this was the (mDBConn && asyncThread) case in this method.
1643 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1644 // Close() or AsyncClose() has already been called (at least) once. This is
1645 // undeniably a misuse case by the caller. We could optimize for this
1646 // case by adding an additional check of mAsyncExecutionThread without using
1647 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1648 // shut it down. But this complicates the method for broken caller code
1649 // whereas we're still correct and safe without the special-case.
1650 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1652 // Create our callback event if we were given a callback. This will
1653 // eventually be dispatched in all cases, even if we fall back to Close() and
1654 // the database wasn't open and we return an error. The rationale is that
1655 // no existing consumer checks our return value and several of them like to
1656 // spin nested event loops until the callback fires. Given that, it seems
1657 // preferable for us to dispatch the callback in all cases. (Except the
1658 // wrong thread misuse case we bailed on up above. But that's okay because
1659 // that is statically wrong whereas these edge cases are dynamic.)
1660 nsCOMPtr<nsIRunnable> completeEvent;
1661 if (aCallback) {
1662 completeEvent = newCompletionEvent(aCallback);
1665 if (!asyncThread) {
1666 // We were unable to create an async thread, so we need to fall back to
1667 // using normal Close(). Since there is no async thread, Close() will
1668 // not complain about that. (Close() may, however, complain if the
1669 // connection is closed, but that's okay.)
1670 if (completeEvent) {
1671 // Closing the database is more important than returning an error code
1672 // about a failure to dispatch, especially because all existing native
1673 // callers ignore our return value.
1674 Unused << NS_DispatchToMainThread(completeEvent.forget());
1676 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1677 // Return a success inconditionally here, since Close() is unlikely to fail
1678 // and we want to reassure the consumer that its callback will be invoked.
1679 return NS_OK;
1682 // If we're closing the connection during shutdown, and there is an
1683 // interruptible statement running on the helper thread, issue a
1684 // sqlite3_interrupt() to avoid crashing when that statement takes a long
1685 // time (for example a vacuum).
1686 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed) &&
1687 mInterruptible && mIsStatementOnHelperThreadInterruptible) {
1688 MOZ_ASSERT(!isClosing(), "Must not be closing, see Interrupt()");
1689 DebugOnly<nsresult> rv2 = Interrupt();
1690 MOZ_ASSERT(NS_SUCCEEDED(rv2));
1693 // setClosedState nullifies our connection pointer, so we take a raw pointer
1694 // off it, to pass it through the close procedure.
1695 sqlite3* nativeConn = mDBConn;
1696 rv = setClosedState();
1697 NS_ENSURE_SUCCESS(rv, rv);
1699 // Create and dispatch our close event to the background thread.
1700 nsCOMPtr<nsIRunnable> closeEvent =
1701 new AsyncCloseConnection(this, nativeConn, completeEvent);
1702 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1703 NS_ENSURE_SUCCESS(rv, rv);
1705 return NS_OK;
1708 NS_IMETHODIMP
1709 Connection::AsyncClone(bool aReadOnly,
1710 mozIStorageCompletionCallback* aCallback) {
1711 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1713 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1714 if (!connectionReady()) {
1715 return NS_ERROR_NOT_INITIALIZED;
1717 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1718 if (NS_FAILED(rv)) {
1719 return rv;
1721 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1723 int flags = mFlags;
1724 if (aReadOnly) {
1725 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1726 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1727 // Turn off SQLITE_OPEN_CREATE.
1728 flags = (~SQLITE_OPEN_CREATE & flags);
1731 // The cloned connection will still implement the synchronous API, but throw
1732 // if any synchronous methods are called on the main thread.
1733 RefPtr<Connection> clone =
1734 new Connection(mStorageService, flags, ASYNCHRONOUS, mTelemetryFilename);
1736 RefPtr<AsyncInitializeClone> initEvent =
1737 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1738 // Dispatch to our async thread, since the originating connection must remain
1739 // valid and open for the whole cloning process. This also ensures we are
1740 // properly serialized with a `close` operation, rather than race with it.
1741 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1742 if (!target) {
1743 return NS_ERROR_UNEXPECTED;
1745 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1748 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1749 nsresult rv;
1750 if (!mStorageKey.IsEmpty()) {
1751 rv = aClone->initialize(mStorageKey, mName);
1752 } else if (mFileURL) {
1753 rv = aClone->initialize(mFileURL);
1754 } else {
1755 rv = aClone->initialize(mDatabaseFile);
1757 if (NS_FAILED(rv)) {
1758 return rv;
1761 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1763 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1764 NS_ENSURE_SUCCESS(rv, rv);
1766 // Re-attach on-disk databases that were attached to the original connection.
1768 nsCOMPtr<mozIStorageStatement> stmt;
1769 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1770 NS_ENSURE_SUCCESS(rv, rv);
1771 bool hasResult = false;
1772 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1773 nsAutoCString name;
1774 rv = stmt->GetUTF8String(1, name);
1775 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1776 !name.EqualsLiteral("temp")) {
1777 nsCString path;
1778 rv = stmt->GetUTF8String(2, path);
1779 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1780 nsCOMPtr<mozIStorageStatement> attachStmt;
1781 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1782 getter_AddRefs(attachStmt));
1783 NS_ENSURE_SUCCESS(rv, rv);
1784 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1785 NS_ENSURE_SUCCESS(rv, rv);
1786 rv = attachStmt->Execute();
1787 NS_ENSURE_SUCCESS(rv, rv);
1793 // Copy over pragmas from the original connection.
1794 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1795 // schema ("main" and any other attached databases), and this implmentation
1796 // fails to propagate them. This is being addressed on trunk.
1797 static const char* pragmas[] = {
1798 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1799 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1800 for (auto& pragma : pragmas) {
1801 // Read-only connections just need cache_size and temp_store pragmas.
1802 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1803 ::strcmp(pragma, "temp_store") != 0) {
1804 continue;
1807 nsAutoCString pragmaQuery("PRAGMA ");
1808 pragmaQuery.Append(pragma);
1809 nsCOMPtr<mozIStorageStatement> stmt;
1810 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
1811 NS_ENSURE_SUCCESS(rv, rv);
1812 bool hasResult = false;
1813 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1814 pragmaQuery.AppendLiteral(" = ");
1815 pragmaQuery.AppendInt(stmt->AsInt32(0));
1816 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
1817 NS_ENSURE_SUCCESS(rv, rv);
1821 // Copy over temporary tables, triggers, and views from the original
1822 // connections. Entities in `sqlite_temp_master` are only visible to the
1823 // connection that created them.
1824 if (!aReadOnly) {
1825 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
1826 NS_ENSURE_SUCCESS(rv, rv);
1828 nsCOMPtr<mozIStorageStatement> stmt;
1829 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1830 "WHERE type IN ('table', 'view', "
1831 "'index', 'trigger')"),
1832 getter_AddRefs(stmt));
1833 // Propagate errors, because failing to copy triggers might cause schema
1834 // coherency issues when writing to the database from the cloned connection.
1835 NS_ENSURE_SUCCESS(rv, rv);
1836 bool hasResult = false;
1837 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1838 nsAutoCString query;
1839 rv = stmt->GetUTF8String(0, query);
1840 NS_ENSURE_SUCCESS(rv, rv);
1842 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1843 // keyword. We need to add it back, or we'll recreate temporary entities
1844 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1845 // statements, but those don't need `TEMP` keywords.
1846 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
1847 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
1848 StringBeginsWith(query, "CREATE VIEW "_ns)) {
1849 query.Replace(0, 6, "CREATE TEMP");
1852 rv = aClone->ExecuteSimpleSQL(query);
1853 NS_ENSURE_SUCCESS(rv, rv);
1856 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
1857 NS_ENSURE_SUCCESS(rv, rv);
1860 // Copy any functions that have been added to this connection.
1861 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1862 for (const auto& entry : mFunctions) {
1863 const nsACString& key = entry.GetKey();
1864 Connection::FunctionInfo data = entry.GetData();
1866 rv = aClone->CreateFunction(key, data.numArgs, data.function);
1867 if (NS_FAILED(rv)) {
1868 NS_WARNING("Failed to copy function to cloned connection");
1872 guard.release();
1873 return NS_OK;
1876 NS_IMETHODIMP
1877 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
1878 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1880 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
1882 if (!connectionReady()) {
1883 return NS_ERROR_NOT_INITIALIZED;
1885 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1886 if (NS_FAILED(rv)) {
1887 return rv;
1890 int flags = mFlags;
1891 if (aReadOnly) {
1892 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1893 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1894 // Turn off SQLITE_OPEN_CREATE.
1895 flags = (~SQLITE_OPEN_CREATE & flags);
1898 RefPtr<Connection> clone =
1899 new Connection(mStorageService, flags, mSupportedOperations,
1900 mTelemetryFilename, mInterruptible);
1902 rv = initializeClone(clone, aReadOnly);
1903 if (NS_FAILED(rv)) {
1904 return rv;
1907 NS_IF_ADDREF(*_connection = clone);
1908 return NS_OK;
1911 NS_IMETHODIMP
1912 Connection::Interrupt() {
1913 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
1914 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
1915 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1916 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
1917 IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1919 if (!connectionReady()) {
1920 return NS_ERROR_NOT_INITIALIZED;
1923 if (isClosing()) { // Closing already in asynchronous case
1924 return NS_OK;
1928 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
1929 // it is not safe to call sqlite3_interrupt() when
1930 // database connection is closed or might close before
1931 // sqlite3_interrupt() returns.
1932 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1933 if (!isClosed(lockedScope)) {
1934 MOZ_ASSERT(mDBConn);
1935 ::sqlite3_interrupt(mDBConn);
1939 return NS_OK;
1942 NS_IMETHODIMP
1943 Connection::AsyncVacuum(mozIStorageCompletionCallback* aCallback,
1944 bool aUseIncremental, int32_t aSetPageSize) {
1945 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1946 // Abort if we're shutting down.
1947 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
1948 return NS_ERROR_ABORT;
1950 // Check if AsyncClose or Close were already invoked.
1951 if (!connectionReady()) {
1952 return NS_ERROR_NOT_INITIALIZED;
1954 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1955 if (NS_FAILED(rv)) {
1956 return rv;
1958 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1959 if (!asyncThread) {
1960 return NS_ERROR_NOT_INITIALIZED;
1963 // Create and dispatch our vacuum event to the background thread.
1964 nsCOMPtr<nsIRunnable> vacuumEvent =
1965 new AsyncVacuumEvent(this, aCallback, aUseIncremental, aSetPageSize);
1966 rv = asyncThread->Dispatch(vacuumEvent, NS_DISPATCH_NORMAL);
1967 NS_ENSURE_SUCCESS(rv, rv);
1969 return NS_OK;
1972 NS_IMETHODIMP
1973 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
1974 *_defaultPageSize = Service::kDefaultPageSize;
1975 return NS_OK;
1978 NS_IMETHODIMP
1979 Connection::GetConnectionReady(bool* _ready) {
1980 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1981 *_ready = connectionReady();
1982 return NS_OK;
1985 NS_IMETHODIMP
1986 Connection::GetDatabaseFile(nsIFile** _dbFile) {
1987 if (!connectionReady()) {
1988 return NS_ERROR_NOT_INITIALIZED;
1990 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1991 if (NS_FAILED(rv)) {
1992 return rv;
1995 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
1997 return NS_OK;
2000 NS_IMETHODIMP
2001 Connection::GetLastInsertRowID(int64_t* _id) {
2002 if (!connectionReady()) {
2003 return NS_ERROR_NOT_INITIALIZED;
2005 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2006 if (NS_FAILED(rv)) {
2007 return rv;
2010 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
2011 *_id = id;
2013 return NS_OK;
2016 NS_IMETHODIMP
2017 Connection::GetAffectedRows(int32_t* _rows) {
2018 if (!connectionReady()) {
2019 return NS_ERROR_NOT_INITIALIZED;
2021 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2022 if (NS_FAILED(rv)) {
2023 return rv;
2026 *_rows = ::sqlite3_changes(mDBConn);
2028 return NS_OK;
2031 NS_IMETHODIMP
2032 Connection::GetLastError(int32_t* _error) {
2033 if (!connectionReady()) {
2034 return NS_ERROR_NOT_INITIALIZED;
2036 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2037 if (NS_FAILED(rv)) {
2038 return rv;
2041 *_error = ::sqlite3_errcode(mDBConn);
2043 return NS_OK;
2046 NS_IMETHODIMP
2047 Connection::GetLastErrorString(nsACString& _errorString) {
2048 if (!connectionReady()) {
2049 return NS_ERROR_NOT_INITIALIZED;
2051 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2052 if (NS_FAILED(rv)) {
2053 return rv;
2056 const char* serr = ::sqlite3_errmsg(mDBConn);
2057 _errorString.Assign(serr);
2059 return NS_OK;
2062 NS_IMETHODIMP
2063 Connection::GetSchemaVersion(int32_t* _version) {
2064 if (!connectionReady()) {
2065 return NS_ERROR_NOT_INITIALIZED;
2067 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2068 if (NS_FAILED(rv)) {
2069 return rv;
2072 nsCOMPtr<mozIStorageStatement> stmt;
2073 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
2074 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
2076 *_version = 0;
2077 bool hasResult;
2078 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2079 *_version = stmt->AsInt32(0);
2082 return NS_OK;
2085 NS_IMETHODIMP
2086 Connection::SetSchemaVersion(int32_t aVersion) {
2087 if (!connectionReady()) {
2088 return NS_ERROR_NOT_INITIALIZED;
2090 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2091 if (NS_FAILED(rv)) {
2092 return rv;
2095 nsAutoCString stmt("PRAGMA user_version = "_ns);
2096 stmt.AppendInt(aVersion);
2098 return ExecuteSimpleSQL(stmt);
2101 NS_IMETHODIMP
2102 Connection::CreateStatement(const nsACString& aSQLStatement,
2103 mozIStorageStatement** _stmt) {
2104 NS_ENSURE_ARG_POINTER(_stmt);
2105 if (!connectionReady()) {
2106 return NS_ERROR_NOT_INITIALIZED;
2108 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2109 if (NS_FAILED(rv)) {
2110 return rv;
2113 RefPtr<Statement> statement(new Statement());
2114 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2116 rv = statement->initialize(this, mDBConn, aSQLStatement);
2117 NS_ENSURE_SUCCESS(rv, rv);
2119 Statement* rawPtr;
2120 statement.forget(&rawPtr);
2121 *_stmt = rawPtr;
2122 return NS_OK;
2125 NS_IMETHODIMP
2126 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
2127 mozIStorageAsyncStatement** _stmt) {
2128 NS_ENSURE_ARG_POINTER(_stmt);
2129 if (!connectionReady()) {
2130 return NS_ERROR_NOT_INITIALIZED;
2132 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2133 if (NS_FAILED(rv)) {
2134 return rv;
2137 RefPtr<AsyncStatement> statement(new AsyncStatement());
2138 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2140 rv = statement->initialize(this, mDBConn, aSQLStatement);
2141 NS_ENSURE_SUCCESS(rv, rv);
2143 AsyncStatement* rawPtr;
2144 statement.forget(&rawPtr);
2145 *_stmt = rawPtr;
2146 return NS_OK;
2149 NS_IMETHODIMP
2150 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
2151 CHECK_MAINTHREAD_ABUSE();
2152 if (!connectionReady()) {
2153 return NS_ERROR_NOT_INITIALIZED;
2155 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2156 if (NS_FAILED(rv)) {
2157 return rv;
2160 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
2161 return convertResultCode(srv);
2164 NS_IMETHODIMP
2165 Connection::ExecuteAsync(
2166 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
2167 mozIStorageStatementCallback* aCallback,
2168 mozIStoragePendingStatement** _handle) {
2169 nsTArray<StatementData> stmts(aStatements.Length());
2170 for (uint32_t i = 0; i < aStatements.Length(); i++) {
2171 nsCOMPtr<StorageBaseStatementInternal> stmt =
2172 do_QueryInterface(aStatements[i]);
2173 NS_ENSURE_STATE(stmt);
2175 // Obtain our StatementData.
2176 StatementData data;
2177 nsresult rv = stmt->getAsynchronousStatementData(data);
2178 NS_ENSURE_SUCCESS(rv, rv);
2180 NS_ASSERTION(stmt->getOwner() == this,
2181 "Statement must be from this database connection!");
2183 // Now append it to our array.
2184 stmts.AppendElement(data);
2187 // Dispatch to the background
2188 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
2189 aCallback, _handle);
2192 NS_IMETHODIMP
2193 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
2194 mozIStorageStatementCallback* aCallback,
2195 mozIStoragePendingStatement** _handle) {
2196 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
2198 nsCOMPtr<mozIStorageAsyncStatement> stmt;
2199 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
2200 if (NS_FAILED(rv)) {
2201 return rv;
2204 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
2205 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
2206 if (NS_FAILED(rv)) {
2207 return rv;
2210 pendingStatement.forget(_handle);
2211 return rv;
2214 NS_IMETHODIMP
2215 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
2216 return databaseElementExists(TABLE, aTableName, _exists);
2219 NS_IMETHODIMP
2220 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2221 return databaseElementExists(INDEX, aIndexName, _exists);
2224 NS_IMETHODIMP
2225 Connection::GetTransactionInProgress(bool* _inProgress) {
2226 if (!connectionReady()) {
2227 return NS_ERROR_NOT_INITIALIZED;
2229 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2230 if (NS_FAILED(rv)) {
2231 return rv;
2234 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2235 *_inProgress = transactionInProgress(lockedScope);
2236 return NS_OK;
2239 NS_IMETHODIMP
2240 Connection::GetDefaultTransactionType(int32_t* _type) {
2241 *_type = mDefaultTransactionType;
2242 return NS_OK;
2245 NS_IMETHODIMP
2246 Connection::SetDefaultTransactionType(int32_t aType) {
2247 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2248 mDefaultTransactionType = aType;
2249 return NS_OK;
2252 NS_IMETHODIMP
2253 Connection::GetVariableLimit(int32_t* _limit) {
2254 if (!connectionReady()) {
2255 return NS_ERROR_NOT_INITIALIZED;
2257 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2258 if (limit < 0) {
2259 return NS_ERROR_UNEXPECTED;
2261 *_limit = limit;
2262 return NS_OK;
2265 NS_IMETHODIMP
2266 Connection::BeginTransaction() {
2267 if (!connectionReady()) {
2268 return NS_ERROR_NOT_INITIALIZED;
2270 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2271 if (NS_FAILED(rv)) {
2272 return rv;
2275 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2276 return beginTransactionInternal(lockedScope, mDBConn,
2277 mDefaultTransactionType);
2280 nsresult Connection::beginTransactionInternal(
2281 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2282 int32_t aTransactionType) {
2283 if (transactionInProgress(aProofOfLock)) {
2284 return NS_ERROR_FAILURE;
2286 nsresult rv;
2287 switch (aTransactionType) {
2288 case TRANSACTION_DEFERRED:
2289 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2290 break;
2291 case TRANSACTION_IMMEDIATE:
2292 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2293 break;
2294 case TRANSACTION_EXCLUSIVE:
2295 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2296 break;
2297 default:
2298 return NS_ERROR_ILLEGAL_VALUE;
2300 return rv;
2303 NS_IMETHODIMP
2304 Connection::CommitTransaction() {
2305 if (!connectionReady()) {
2306 return NS_ERROR_NOT_INITIALIZED;
2308 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2309 if (NS_FAILED(rv)) {
2310 return rv;
2313 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2314 return commitTransactionInternal(lockedScope, mDBConn);
2317 nsresult Connection::commitTransactionInternal(
2318 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2319 if (!transactionInProgress(aProofOfLock)) {
2320 return NS_ERROR_UNEXPECTED;
2322 nsresult rv =
2323 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2324 return rv;
2327 NS_IMETHODIMP
2328 Connection::RollbackTransaction() {
2329 if (!connectionReady()) {
2330 return NS_ERROR_NOT_INITIALIZED;
2332 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2333 if (NS_FAILED(rv)) {
2334 return rv;
2337 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2338 return rollbackTransactionInternal(lockedScope, mDBConn);
2341 nsresult Connection::rollbackTransactionInternal(
2342 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2343 if (!transactionInProgress(aProofOfLock)) {
2344 return NS_ERROR_UNEXPECTED;
2347 nsresult rv =
2348 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2349 return rv;
2352 NS_IMETHODIMP
2353 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2354 if (!connectionReady()) {
2355 return NS_ERROR_NOT_INITIALIZED;
2357 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2358 if (NS_FAILED(rv)) {
2359 return rv;
2362 SmprintfPointer buf =
2363 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2364 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2366 int srv = executeSql(mDBConn, buf.get());
2368 return convertResultCode(srv);
2371 NS_IMETHODIMP
2372 Connection::CreateFunction(const nsACString& aFunctionName,
2373 int32_t aNumArguments,
2374 mozIStorageFunction* aFunction) {
2375 if (!connectionReady()) {
2376 return NS_ERROR_NOT_INITIALIZED;
2378 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2379 if (NS_FAILED(rv)) {
2380 return rv;
2383 // Check to see if this function is already defined. We only check the name
2384 // because a function can be defined with the same body but different names.
2385 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2386 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2388 int srv = ::sqlite3_create_function(
2389 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2390 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2391 if (srv != SQLITE_OK) return convertResultCode(srv);
2393 FunctionInfo info = {aFunction, aNumArguments};
2394 mFunctions.InsertOrUpdate(aFunctionName, info);
2396 return NS_OK;
2399 NS_IMETHODIMP
2400 Connection::RemoveFunction(const nsACString& aFunctionName) {
2401 if (!connectionReady()) {
2402 return NS_ERROR_NOT_INITIALIZED;
2404 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2405 if (NS_FAILED(rv)) {
2406 return rv;
2409 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2410 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2412 int srv = ::sqlite3_create_function(
2413 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2414 nullptr, nullptr, nullptr, nullptr);
2415 if (srv != SQLITE_OK) return convertResultCode(srv);
2417 mFunctions.Remove(aFunctionName);
2419 return NS_OK;
2422 NS_IMETHODIMP
2423 Connection::SetProgressHandler(int32_t aGranularity,
2424 mozIStorageProgressHandler* aHandler,
2425 mozIStorageProgressHandler** _oldHandler) {
2426 if (!connectionReady()) {
2427 return NS_ERROR_NOT_INITIALIZED;
2429 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2430 if (NS_FAILED(rv)) {
2431 return rv;
2434 // Return previous one
2435 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2436 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2438 if (!aHandler || aGranularity <= 0) {
2439 aHandler = nullptr;
2440 aGranularity = 0;
2442 mProgressHandler = aHandler;
2443 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2445 return NS_OK;
2448 NS_IMETHODIMP
2449 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2450 if (!connectionReady()) {
2451 return NS_ERROR_NOT_INITIALIZED;
2453 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2454 if (NS_FAILED(rv)) {
2455 return rv;
2458 // Return previous one
2459 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2460 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2462 mProgressHandler = nullptr;
2463 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2465 return NS_OK;
2468 NS_IMETHODIMP
2469 Connection::SetGrowthIncrement(int32_t aChunkSize,
2470 const nsACString& aDatabaseName) {
2471 if (!connectionReady()) {
2472 return NS_ERROR_NOT_INITIALIZED;
2474 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2475 if (NS_FAILED(rv)) {
2476 return rv;
2479 // Bug 597215: Disk space is extremely limited on Android
2480 // so don't preallocate space. This is also not effective
2481 // on log structured file systems used by Android devices
2482 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2483 // Don't preallocate if less than 500MiB is available.
2484 int64_t bytesAvailable;
2485 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2486 NS_ENSURE_SUCCESS(rv, rv);
2487 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2488 return NS_ERROR_FILE_TOO_BIG;
2491 int srv = ::sqlite3_file_control(
2492 mDBConn,
2493 aDatabaseName.Length() ? nsPromiseFlatCString(aDatabaseName).get()
2494 : nullptr,
2495 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2496 if (srv == SQLITE_OK) {
2497 mGrowthChunkSize = aChunkSize;
2499 #endif
2500 return NS_OK;
2503 int32_t Connection::RemovablePagesInFreeList(const nsACString& aSchemaName) {
2504 int32_t freeListPagesCount = 0;
2505 if (!isConnectionReadyOnThisThread()) {
2506 MOZ_ASSERT(false, "Database connection is not ready");
2507 return freeListPagesCount;
2510 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2511 query.Append(aSchemaName);
2512 query.AppendLiteral(".freelist_count");
2513 nsCOMPtr<mozIStorageStatement> stmt;
2514 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2515 MOZ_ASSERT(NS_SUCCEEDED(rv));
2516 bool hasResult = false;
2517 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2518 freeListPagesCount = stmt->AsInt32(0);
2521 // If there's no chunk size set, any page is good to be removed.
2522 if (mGrowthChunkSize == 0 || freeListPagesCount == 0) {
2523 return freeListPagesCount;
2525 int32_t pageSize;
2527 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2528 query.Append(aSchemaName);
2529 query.AppendLiteral(".page_size");
2530 nsCOMPtr<mozIStorageStatement> stmt;
2531 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2532 MOZ_ASSERT(NS_SUCCEEDED(rv));
2533 bool hasResult = false;
2534 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2535 pageSize = stmt->AsInt32(0);
2536 } else {
2537 MOZ_ASSERT(false, "Couldn't get page_size");
2538 return 0;
2541 return std::max(0, freeListPagesCount - (mGrowthChunkSize / pageSize));
2544 NS_IMETHODIMP
2545 Connection::EnableModule(const nsACString& aModuleName) {
2546 if (!connectionReady()) {
2547 return NS_ERROR_NOT_INITIALIZED;
2549 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2550 if (NS_FAILED(rv)) {
2551 return rv;
2554 for (auto& gModule : gModules) {
2555 struct Module* m = &gModule;
2556 if (aModuleName.Equals(m->name)) {
2557 int srv = m->registerFunc(mDBConn, m->name);
2558 if (srv != SQLITE_OK) return convertResultCode(srv);
2560 return NS_OK;
2564 return NS_ERROR_FAILURE;
2567 NS_IMETHODIMP
2568 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2569 QuotaObject** aJournalQuotaObject) {
2570 MOZ_ASSERT(aDatabaseQuotaObject);
2571 MOZ_ASSERT(aJournalQuotaObject);
2573 if (!connectionReady()) {
2574 return NS_ERROR_NOT_INITIALIZED;
2576 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2577 if (NS_FAILED(rv)) {
2578 return rv;
2581 sqlite3_file* file;
2582 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2583 &file);
2584 if (srv != SQLITE_OK) {
2585 return convertResultCode(srv);
2588 sqlite3_vfs* vfs;
2589 srv =
2590 ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_VFS_POINTER, &vfs);
2591 if (srv != SQLITE_OK) {
2592 return convertResultCode(srv);
2595 bool obfusactingVFS = false;
2598 const nsDependentCString vfsName{vfs->zName};
2600 if (vfsName == obfsvfs::GetVFSName()) {
2601 obfusactingVFS = true;
2602 } else if (vfsName != quotavfs::GetVFSName()) {
2603 NS_WARNING("Got unexpected vfs");
2604 return NS_ERROR_FAILURE;
2608 RefPtr<QuotaObject> databaseQuotaObject =
2609 GetQuotaObject(file, obfusactingVFS);
2610 if (NS_WARN_IF(!databaseQuotaObject)) {
2611 return NS_ERROR_FAILURE;
2614 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2615 &file);
2616 if (srv != SQLITE_OK) {
2617 return convertResultCode(srv);
2620 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObject(file, obfusactingVFS);
2621 if (NS_WARN_IF(!journalQuotaObject)) {
2622 return NS_ERROR_FAILURE;
2625 databaseQuotaObject.forget(aDatabaseQuotaObject);
2626 journalQuotaObject.forget(aJournalQuotaObject);
2627 return NS_OK;
2630 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2632 uint32_t Connection::GetTransactionNestingLevel(
2633 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2634 return mTransactionNestingLevel;
2637 uint32_t Connection::IncreaseTransactionNestingLevel(
2638 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2639 return ++mTransactionNestingLevel;
2642 uint32_t Connection::DecreaseTransactionNestingLevel(
2643 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2644 return --mTransactionNestingLevel;
2647 } // namespace mozilla::storage