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/. */
10 #include "nsThreadUtils.h"
12 #include "nsIFileURL.h"
13 #include "nsIXPConnect.h"
14 #include "mozilla/AppShutdown.h"
15 #include "mozilla/CheckedInt.h"
16 #include "mozilla/Telemetry.h"
17 #include "mozilla/Mutex.h"
18 #include "mozilla/CondVar.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/ErrorNames.h"
21 #include "mozilla/Unused.h"
22 #include "mozilla/dom/quota/QuotaObject.h"
23 #include "mozilla/ScopeExit.h"
24 #include "mozilla/SpinEventLoopUntil.h"
25 #include "mozilla/StaticPrefs_storage.h"
27 #include "mozIStorageCompletionCallback.h"
28 #include "mozIStorageFunction.h"
30 #include "mozStorageAsyncStatementExecution.h"
31 #include "mozStorageSQLFunctions.h"
32 #include "mozStorageConnection.h"
33 #include "mozStorageService.h"
34 #include "mozStorageStatement.h"
35 #include "mozStorageAsyncStatement.h"
36 #include "mozStorageArgValueArray.h"
37 #include "mozStoragePrivateHelpers.h"
38 #include "mozStorageStatementData.h"
39 #include "ObfuscatingVFS.h"
41 #include "StorageBaseStatementInternal.h"
42 #include "SQLCollations.h"
43 #include "FileSystemModule.h"
44 #include "mozStorageHelper.h"
46 #include "mozilla/Assertions.h"
47 #include "mozilla/Logging.h"
48 #include "mozilla/Printf.h"
49 #include "mozilla/ProfilerLabels.h"
50 #include "mozilla/RefPtr.h"
51 #include "nsProxyRelease.h"
52 #include "nsStringFwd.h"
53 #include "nsURLHelper.h"
55 #define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
57 // Maximum size of the pages cache per connection.
58 #define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
60 mozilla::LazyLogModule
gStorageLog("mozStorage");
62 // Checks that the protected code is running on the main-thread only if the
63 // connection was also opened on it.
65 # define CHECK_MAINTHREAD_ABUSE() \
67 NS_WARNING_ASSERTION( \
68 eventTargetOpenedOn == GetMainThreadSerialEventTarget() || \
70 "Using Storage synchronous API on main-thread, but " \
71 "the connection was opened on another thread."); \
74 # define CHECK_MAINTHREAD_ABUSE() \
79 namespace mozilla::storage
{
81 using mozilla::dom::quota::QuotaObject
;
82 using mozilla::Telemetry::AccumulateCategoricalKeyed
;
83 using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN
;
84 using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY
;
88 int nsresultToSQLiteResult(nsresult aXPCOMResultCode
) {
89 if (NS_SUCCEEDED(aXPCOMResultCode
)) {
93 switch (aXPCOMResultCode
) {
94 case NS_ERROR_FILE_CORRUPTED
:
95 return SQLITE_CORRUPT
;
96 case NS_ERROR_FILE_ACCESS_DENIED
:
97 return SQLITE_CANTOPEN
;
98 case NS_ERROR_STORAGE_BUSY
:
100 case NS_ERROR_FILE_IS_LOCKED
:
101 return SQLITE_LOCKED
;
102 case NS_ERROR_FILE_READ_ONLY
:
103 return SQLITE_READONLY
;
104 case NS_ERROR_STORAGE_IOERR
:
106 case NS_ERROR_FILE_NO_DEVICE_SPACE
:
108 case NS_ERROR_OUT_OF_MEMORY
:
110 case NS_ERROR_UNEXPECTED
:
111 return SQLITE_MISUSE
;
114 case NS_ERROR_STORAGE_CONSTRAINT
:
115 return SQLITE_CONSTRAINT
;
120 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!");
123 ////////////////////////////////////////////////////////////////////////////////
124 //// Variant Specialization Functions (variantToSQLiteT)
126 int sqlite3_T_int(sqlite3_context
* aCtx
, int aValue
) {
127 ::sqlite3_result_int(aCtx
, aValue
);
131 int sqlite3_T_int64(sqlite3_context
* aCtx
, sqlite3_int64 aValue
) {
132 ::sqlite3_result_int64(aCtx
, aValue
);
136 int sqlite3_T_double(sqlite3_context
* aCtx
, double aValue
) {
137 ::sqlite3_result_double(aCtx
, aValue
);
141 int sqlite3_T_text(sqlite3_context
* aCtx
, const nsCString
& aValue
) {
142 CheckedInt
<int32_t> length(aValue
.Length());
143 if (!length
.isValid()) {
144 return SQLITE_MISUSE
;
146 ::sqlite3_result_text(aCtx
, aValue
.get(), length
.value(), SQLITE_TRANSIENT
);
150 int sqlite3_T_text16(sqlite3_context
* aCtx
, const nsString
& aValue
) {
151 CheckedInt
<int32_t> n_bytes
=
152 CheckedInt
<int32_t>(aValue
.Length()) * sizeof(char16_t
);
153 if (!n_bytes
.isValid()) {
154 return SQLITE_MISUSE
;
156 ::sqlite3_result_text16(aCtx
, aValue
.get(), n_bytes
.value(),
161 int sqlite3_T_null(sqlite3_context
* aCtx
) {
162 ::sqlite3_result_null(aCtx
);
166 int sqlite3_T_blob(sqlite3_context
* aCtx
, const void* aData
, int aSize
) {
167 ::sqlite3_result_blob(aCtx
, aData
, aSize
, free
);
171 #include "variantToSQLiteT_impl.h"
173 ////////////////////////////////////////////////////////////////////////////////
178 int (*registerFunc
)(sqlite3
*, const char*);
181 Module gModules
[] = {{"filesystem", RegisterFileSystemModule
}};
183 ////////////////////////////////////////////////////////////////////////////////
186 int tracefunc(unsigned aReason
, void* aClosure
, void* aP
, void* aX
) {
188 case SQLITE_TRACE_STMT
: {
189 // aP is a pointer to the prepared statement.
190 sqlite3_stmt
* stmt
= static_cast<sqlite3_stmt
*>(aP
);
191 // aX is a pointer to a string containing the unexpanded SQL or a comment,
192 // starting with "--"" in case of a trigger.
193 char* expanded
= static_cast<char*>(aX
);
194 // Simulate what sqlite_trace was doing.
195 if (!::strncmp(expanded
, "--", 2)) {
196 MOZ_LOG(gStorageLog
, LogLevel::Debug
,
197 ("TRACE_STMT on %p: '%s'", aClosure
, expanded
));
199 char* sql
= ::sqlite3_expanded_sql(stmt
);
200 MOZ_LOG(gStorageLog
, LogLevel::Debug
,
201 ("TRACE_STMT on %p: '%s'", aClosure
, sql
));
206 case SQLITE_TRACE_PROFILE
: {
207 // aX is pointer to a 64bit integer containing nanoseconds it took to
208 // execute the last command.
209 sqlite_int64 time
= *(static_cast<sqlite_int64
*>(aX
)) / 1000000;
211 MOZ_LOG(gStorageLog
, LogLevel::Debug
,
212 ("TRACE_TIME on %p: %lldms", aClosure
, time
));
220 void basicFunctionHelper(sqlite3_context
* aCtx
, int aArgc
,
221 sqlite3_value
** aArgv
) {
222 void* userData
= ::sqlite3_user_data(aCtx
);
224 mozIStorageFunction
* func
= static_cast<mozIStorageFunction
*>(userData
);
226 RefPtr
<ArgValueArray
> arguments(new ArgValueArray(aArgc
, aArgv
));
227 if (!arguments
) return;
229 nsCOMPtr
<nsIVariant
> result
;
230 nsresult rv
= func
->OnFunctionCall(arguments
, getter_AddRefs(result
));
232 nsAutoCString errorMessage
;
233 GetErrorName(rv
, errorMessage
);
234 errorMessage
.InsertLiteral("User function returned ", 0);
235 errorMessage
.Append('!');
237 NS_WARNING(errorMessage
.get());
239 ::sqlite3_result_error(aCtx
, errorMessage
.get(), -1);
240 ::sqlite3_result_error_code(aCtx
, nsresultToSQLiteResult(rv
));
243 int retcode
= variantToSQLiteT(aCtx
, result
);
244 if (retcode
!= SQLITE_OK
) {
245 NS_WARNING("User function returned invalid data type!");
246 ::sqlite3_result_error(aCtx
, "User function returned invalid data type",
251 RefPtr
<QuotaObject
> GetQuotaObject(sqlite3_file
* aFile
, bool obfuscatingVFS
) {
252 return obfuscatingVFS
253 ? mozilla::storage::obfsvfs::GetQuotaObjectForFile(aFile
)
254 : mozilla::storage::quotavfs::GetQuotaObjectForFile(aFile
);
258 * This code is heavily based on the sample at:
259 * http://www.sqlite.org/unlock_notify.html
261 class UnlockNotification
{
264 : mMutex("UnlockNotification mMutex"),
265 mCondVar(mMutex
, "UnlockNotification condVar"),
269 MutexAutoLock
lock(mMutex
);
271 (void)mCondVar
.Wait();
276 MutexAutoLock
lock(mMutex
);
278 (void)mCondVar
.Notify();
282 Mutex mMutex MOZ_UNANNOTATED
;
287 void UnlockNotifyCallback(void** aArgs
, int aArgsSize
) {
288 for (int i
= 0; i
< aArgsSize
; i
++) {
289 UnlockNotification
* notification
=
290 static_cast<UnlockNotification
*>(aArgs
[i
]);
291 notification
->Signal();
295 int WaitForUnlockNotify(sqlite3
* aDatabase
) {
296 UnlockNotification notification
;
298 ::sqlite3_unlock_notify(aDatabase
, UnlockNotifyCallback
, ¬ification
);
299 MOZ_ASSERT(srv
== SQLITE_LOCKED
|| srv
== SQLITE_OK
);
300 if (srv
== SQLITE_OK
) {
307 ////////////////////////////////////////////////////////////////////////////////
310 class AsyncCloseConnection final
: public Runnable
{
312 AsyncCloseConnection(Connection
* aConnection
, sqlite3
* aNativeConnection
,
313 nsIRunnable
* aCallbackEvent
)
314 : Runnable("storage::AsyncCloseConnection"),
315 mConnection(aConnection
),
316 mNativeConnection(aNativeConnection
),
317 mCallbackEvent(aCallbackEvent
) {}
319 NS_IMETHOD
Run() override
{
320 // Make sure we don't dispatch to the current thread.
321 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(mConnection
->eventTargetOpenedOn
));
323 nsCOMPtr
<nsIRunnable
> event
=
324 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
325 mConnection
, &Connection::shutdownAsyncThread
);
326 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event
));
329 (void)mConnection
->internalClose(mNativeConnection
);
332 if (mCallbackEvent
) {
333 nsCOMPtr
<nsIThread
> thread
;
334 (void)NS_GetMainThread(getter_AddRefs(thread
));
335 (void)thread
->Dispatch(mCallbackEvent
, NS_DISPATCH_NORMAL
);
341 ~AsyncCloseConnection() override
{
342 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
343 mConnection
.forget());
344 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
345 mCallbackEvent
.forget());
349 RefPtr
<Connection
> mConnection
;
350 sqlite3
* mNativeConnection
;
351 nsCOMPtr
<nsIRunnable
> mCallbackEvent
;
355 * An event used to initialize the clone of a connection.
357 * Must be executed on the clone's async execution thread.
359 class AsyncInitializeClone final
: public Runnable
{
362 * @param aConnection The connection being cloned.
363 * @param aClone The clone.
364 * @param aReadOnly If |true|, the clone is read only.
365 * @param aCallback A callback to trigger once initialization
366 * is complete. This event will be called on
367 * aClone->eventTargetOpenedOn.
369 AsyncInitializeClone(Connection
* aConnection
, Connection
* aClone
,
370 const bool aReadOnly
,
371 mozIStorageCompletionCallback
* aCallback
)
372 : Runnable("storage::AsyncInitializeClone"),
373 mConnection(aConnection
),
375 mReadOnly(aReadOnly
),
376 mCallback(aCallback
) {
377 MOZ_ASSERT(NS_IsMainThread());
380 NS_IMETHOD
Run() override
{
381 MOZ_ASSERT(!NS_IsMainThread());
382 nsresult rv
= mConnection
->initializeClone(mClone
, mReadOnly
);
384 return Dispatch(rv
, nullptr);
386 return Dispatch(NS_OK
,
387 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection
*, mClone
));
391 nsresult
Dispatch(nsresult aResult
, nsISupports
* aValue
) {
392 RefPtr
<CallbackComplete
> event
=
393 new CallbackComplete(aResult
, aValue
, mCallback
.forget());
394 return mClone
->eventTargetOpenedOn
->Dispatch(event
, NS_DISPATCH_NORMAL
);
397 ~AsyncInitializeClone() override
{
398 nsCOMPtr
<nsIThread
> thread
;
399 DebugOnly
<nsresult
> rv
= NS_GetMainThread(getter_AddRefs(thread
));
400 MOZ_ASSERT(NS_SUCCEEDED(rv
));
402 // Handle ambiguous nsISupports inheritance.
403 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread
,
404 mConnection
.forget());
405 NS_ProxyRelease("AsyncInitializeClone::mClone", thread
, mClone
.forget());
407 // Generally, the callback will be released by CallbackComplete.
408 // However, if for some reason Run() is not executed, we still
409 // need to ensure that it is released here.
410 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread
,
414 RefPtr
<Connection
> mConnection
;
415 RefPtr
<Connection
> mClone
;
416 const bool mReadOnly
;
417 nsCOMPtr
<mozIStorageCompletionCallback
> mCallback
;
421 * A listener for async connection closing.
423 class CloseListener final
: public mozIStorageCompletionCallback
{
426 CloseListener() : mClosed(false) {}
428 NS_IMETHOD
Complete(nsresult
, nsISupports
*) override
{
436 ~CloseListener() = default;
439 NS_IMPL_ISUPPORTS(CloseListener
, mozIStorageCompletionCallback
)
441 class AsyncVacuumEvent final
: public Runnable
{
443 AsyncVacuumEvent(Connection
* aConnection
,
444 mozIStorageCompletionCallback
* aCallback
,
445 bool aUseIncremental
, int32_t aSetPageSize
)
446 : Runnable("storage::AsyncVacuum"),
447 mConnection(aConnection
),
448 mCallback(aCallback
),
449 mUseIncremental(aUseIncremental
),
450 mSetPageSize(aSetPageSize
),
451 mStatus(NS_ERROR_UNEXPECTED
) {}
453 NS_IMETHOD
Run() override
{
454 // This is initially dispatched to the helper thread, then re-dispatched
455 // to the opener thread, where it will callback.
456 if (IsOnCurrentSerialEventTarget(mConnection
->eventTargetOpenedOn
)) {
457 // Send the completion event.
459 mozilla::Unused
<< mCallback
->Complete(mStatus
, nullptr);
464 // Ensure to invoke the callback regardless of errors.
465 auto guard
= MakeScopeExit([&]() {
466 mConnection
->mIsStatementOnHelperThreadInterruptible
= false;
467 mozilla::Unused
<< mConnection
->eventTargetOpenedOn
->Dispatch(
468 this, NS_DISPATCH_NORMAL
);
471 // Get list of attached databases.
472 nsCOMPtr
<mozIStorageStatement
> stmt
;
473 nsresult rv
= mConnection
->CreateStatement(MOZ_STORAGE_UNIQUIFY_QUERY_STR
474 "PRAGMA database_list"_ns
,
475 getter_AddRefs(stmt
));
476 NS_ENSURE_SUCCESS(rv
, rv
);
477 // We must accumulate names and loop through them later, otherwise VACUUM
478 // will see an ongoing statement and bail out.
479 nsTArray
<nsCString
> schemaNames
;
480 bool hasResult
= false;
481 while (stmt
&& NS_SUCCEEDED(stmt
->ExecuteStep(&hasResult
)) && hasResult
) {
483 rv
= stmt
->GetUTF8String(1, name
);
484 if (NS_SUCCEEDED(rv
) && !name
.EqualsLiteral("temp")) {
485 schemaNames
.AppendElement(name
);
489 // Mark this vacuum as an interruptible operation, so it can be interrupted
490 // if the connection closes during shutdown.
491 mConnection
->mIsStatementOnHelperThreadInterruptible
= true;
492 for (const nsCString
& schemaName
: schemaNames
) {
493 rv
= this->Vacuum(schemaName
);
495 // This is sub-optimal since it's only keeping the last error reason,
496 // but it will do for now.
503 nsresult
Vacuum(const nsACString
& aSchemaName
) {
504 // Abort if we're in shutdown.
505 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed
)) {
506 return NS_ERROR_ABORT
;
508 int32_t removablePages
= mConnection
->RemovablePagesInFreeList(aSchemaName
);
509 if (!removablePages
) {
510 // There's no empty pages to remove, so skip this vacuum for now.
514 bool needsFullVacuum
= true;
517 nsAutoCString
query(MOZ_STORAGE_UNIQUIFY_QUERY_STR
"PRAGMA ");
518 query
.Append(aSchemaName
);
519 query
.AppendLiteral(".page_size = ");
520 query
.AppendInt(mSetPageSize
);
521 nsCOMPtr
<mozIStorageStatement
> stmt
;
522 rv
= mConnection
->ExecuteSimpleSQL(query
);
523 NS_ENSURE_SUCCESS(rv
, rv
);
526 // Check auto_vacuum.
528 nsAutoCString
query(MOZ_STORAGE_UNIQUIFY_QUERY_STR
"PRAGMA ");
529 query
.Append(aSchemaName
);
530 query
.AppendLiteral(".auto_vacuum");
531 nsCOMPtr
<mozIStorageStatement
> stmt
;
532 rv
= mConnection
->CreateStatement(query
, getter_AddRefs(stmt
));
533 NS_ENSURE_SUCCESS(rv
, rv
);
534 bool hasResult
= false;
535 bool changeAutoVacuum
= false;
536 if (stmt
&& NS_SUCCEEDED(stmt
->ExecuteStep(&hasResult
)) && hasResult
) {
537 bool isIncrementalVacuum
= stmt
->AsInt32(0) == 2;
538 changeAutoVacuum
= isIncrementalVacuum
!= mUseIncremental
;
539 if (isIncrementalVacuum
&& !changeAutoVacuum
) {
540 needsFullVacuum
= false;
543 // Changing auto_vacuum is only supported on the main schema.
544 if (aSchemaName
.EqualsLiteral("main") && changeAutoVacuum
) {
545 nsAutoCString
query(MOZ_STORAGE_UNIQUIFY_QUERY_STR
"PRAGMA ");
546 query
.Append(aSchemaName
);
547 query
.AppendLiteral(".auto_vacuum = ");
548 query
.AppendInt(mUseIncremental
? 2 : 0);
549 rv
= mConnection
->ExecuteSimpleSQL(query
);
550 NS_ENSURE_SUCCESS(rv
, rv
);
554 if (needsFullVacuum
) {
555 nsAutoCString
query(MOZ_STORAGE_UNIQUIFY_QUERY_STR
"VACUUM ");
556 query
.Append(aSchemaName
);
557 rv
= mConnection
->ExecuteSimpleSQL(query
);
558 // TODO (Bug 1818039): Report failed vacuum telemetry.
559 NS_ENSURE_SUCCESS(rv
, rv
);
561 nsAutoCString
query(MOZ_STORAGE_UNIQUIFY_QUERY_STR
"PRAGMA ");
562 query
.Append(aSchemaName
);
563 query
.AppendLiteral(".incremental_vacuum(");
564 query
.AppendInt(removablePages
);
565 query
.AppendLiteral(")");
566 rv
= mConnection
->ExecuteSimpleSQL(query
);
567 NS_ENSURE_SUCCESS(rv
, rv
);
573 ~AsyncVacuumEvent() override
{
574 NS_ReleaseOnMainThread("AsyncVacuum::mConnection", mConnection
.forget());
575 NS_ReleaseOnMainThread("AsyncVacuum::mCallback", mCallback
.forget());
579 RefPtr
<Connection
> mConnection
;
580 nsCOMPtr
<mozIStorageCompletionCallback
> mCallback
;
581 bool mUseIncremental
;
582 int32_t mSetPageSize
;
583 Atomic
<nsresult
> mStatus
;
588 ////////////////////////////////////////////////////////////////////////////////
591 Connection::Connection(Service
* aService
, int aFlags
,
592 ConnectionOperation aSupportedOperations
,
593 const nsCString
& aTelemetryFilename
, bool aInterruptible
,
594 bool aIgnoreLockingMode
)
595 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
596 sharedDBMutex("Connection::sharedDBMutex"),
597 eventTargetOpenedOn(WrapNotNull(GetCurrentSerialEventTarget())),
598 mIsStatementOnHelperThreadInterruptible(false),
600 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED
),
602 mProgressHandler(nullptr),
603 mStorageService(aService
),
605 mTransactionNestingLevel(0),
606 mSupportedOperations(aSupportedOperations
),
607 mInterruptible(aSupportedOperations
== Connection::ASYNCHRONOUS
||
609 mIgnoreLockingMode(aIgnoreLockingMode
),
610 mAsyncExecutionThreadShuttingDown(false),
611 mConnectionClosed(false),
612 mGrowthChunkSize(0) {
613 MOZ_ASSERT(!mIgnoreLockingMode
|| mFlags
& SQLITE_OPEN_READONLY
,
614 "Can't ignore locking for a non-readonly connection!");
615 mStorageService
->registerConnection(this);
616 MOZ_ASSERT(!aTelemetryFilename
.IsEmpty(),
617 "A telemetry filename should have been passed-in.");
618 mTelemetryFilename
.Assign(aTelemetryFilename
);
621 Connection::~Connection() {
622 // Failsafe Close() occurs in our custom Release method because of
623 // complications related to Close() potentially invoking AsyncClose() which
624 // will increment our refcount.
625 MOZ_ASSERT(!mAsyncExecutionThread
,
626 "The async thread has not been shutdown properly!");
629 NS_IMPL_ADDREF(Connection
)
631 NS_INTERFACE_MAP_BEGIN(Connection
)
632 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection
)
633 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor
)
634 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection
)
635 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports
, mozIStorageConnection
)
638 // This is identical to what NS_IMPL_RELEASE provides, but with the
639 // extra |1 == count| case.
640 NS_IMETHODIMP_(MozExternalRefCountType
) Connection::Release(void) {
641 MOZ_ASSERT(0 != mRefCnt
, "dup release");
642 nsrefcnt count
= --mRefCnt
;
643 NS_LOG_RELEASE(this, count
, "Connection");
645 // If the refcount went to 1, the single reference must be from
646 // gService->mConnections (in class |Service|). And the code calling
647 // Release is either:
648 // - The "user" code that had created the connection, releasing on any
650 // - One of Service's getConnections() callers had acquired a strong
651 // reference to the Connection that out-lived the last "user" reference,
652 // and now that just got dropped. Note that this reference could be
653 // getting dropped on the main thread or Connection->eventTargetOpenedOn
654 // (because of the NewRunnableMethod used by minimizeMemory).
656 // Either way, we should now perform our failsafe Close() and unregister.
657 // However, we only want to do this once, and the reality is that our
658 // refcount could go back up above 1 and down again at any time if we are
659 // off the main thread and getConnections() gets called on the main thread,
660 // so we use an atomic here to do this exactly once.
661 if (mDestroying
.compareExchange(false, true)) {
662 // Close the connection, dispatching to the opening event target if we're
663 // not on that event target already and that event target is still
664 // accepting runnables. We do this because it's possible we're on the main
665 // thread because of getConnections(), and we REALLY don't want to
666 // transfer I/O to the main thread if we can avoid it.
667 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn
)) {
668 // This could cause SpinningSynchronousClose() to be invoked and AddRef
669 // triggered for AsyncCloseConnection's strong ref if the conn was ever
670 // use for async purposes. (Main-thread only, though.)
671 Unused
<< synchronousClose();
673 nsCOMPtr
<nsIRunnable
> event
=
674 NewRunnableMethod("storage::Connection::synchronousClose", this,
675 &Connection::synchronousClose
);
676 if (NS_FAILED(eventTargetOpenedOn
->Dispatch(event
.forget(),
677 NS_DISPATCH_NORMAL
))) {
678 // The event target was dead and so we've just leaked our runnable.
679 // This should not happen because our non-main-thread consumers should
680 // be explicitly closing their connections, not relying on us to close
681 // them for them. (It's okay to let a statement go out of scope for
682 // automatic cleanup, but not a Connection.)
684 "Leaked Connection::synchronousClose(), ownership fail.");
685 Unused
<< synchronousClose();
689 // This will drop its strong reference right here, right now.
690 mStorageService
->unregisterConnection(this);
692 } else if (0 == count
) {
693 mRefCnt
= 1; /* stabilize */
694 #if 0 /* enable this to find non-threadsafe destructors: */
695 NS_ASSERT_OWNINGTHREAD(Connection
);
703 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption
,
704 int32_t* aMaxValue
) {
705 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
706 int curr
= 0, max
= 0;
708 ::sqlite3_db_status(mDBConn
, aStatusOption
, &curr
, &max
, 0);
709 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc
)));
710 if (aMaxValue
) *aMaxValue
= max
;
714 nsIEventTarget
* Connection::getAsyncExecutionTarget() {
715 NS_ENSURE_TRUE(IsOnCurrentSerialEventTarget(eventTargetOpenedOn
), nullptr);
717 // Don't return the asynchronous event target if we are shutting down.
718 if (mAsyncExecutionThreadShuttingDown
) {
722 // Create the async event target if there's none yet.
723 if (!mAsyncExecutionThread
) {
724 // Names start with "sqldb:" followed by a recognizable name, like the
725 // database file name, or a specially crafted name like "memory".
726 // This name will be surfaced on https://crash-stats.mozilla.org, so any
727 // sensitive part of the file name (e.g. an URL origin) should be replaced
728 // by passing an explicit telemetryName to openDatabaseWithFileURL.
729 nsAutoCString
name("sqldb:"_ns
);
730 name
.Append(mTelemetryFilename
);
731 static nsThreadPoolNaming naming
;
732 nsresult rv
= NS_NewNamedThread(naming
.GetNextThreadName(name
),
733 getter_AddRefs(mAsyncExecutionThread
));
735 NS_WARNING("Failed to create async thread.");
738 mAsyncExecutionThread
->SetNameForWakeupTelemetry("mozStorage (all)"_ns
);
741 return mAsyncExecutionThread
;
744 void Connection::RecordOpenStatus(nsresult rv
) {
745 nsCString histogramKey
= mTelemetryFilename
;
747 if (histogramKey
.IsEmpty()) {
748 histogramKey
.AssignLiteral("unknown");
751 if (NS_SUCCEEDED(rv
)) {
752 AccumulateCategoricalKeyed(histogramKey
, LABELS_SQLITE_STORE_OPEN::success
);
757 case NS_ERROR_FILE_CORRUPTED
:
758 AccumulateCategoricalKeyed(histogramKey
,
759 LABELS_SQLITE_STORE_OPEN::corrupt
);
761 case NS_ERROR_STORAGE_IOERR
:
762 AccumulateCategoricalKeyed(histogramKey
,
763 LABELS_SQLITE_STORE_OPEN::diskio
);
765 case NS_ERROR_FILE_ACCESS_DENIED
:
766 case NS_ERROR_FILE_IS_LOCKED
:
767 case NS_ERROR_FILE_READ_ONLY
:
768 AccumulateCategoricalKeyed(histogramKey
,
769 LABELS_SQLITE_STORE_OPEN::access
);
771 case NS_ERROR_FILE_NO_DEVICE_SPACE
:
772 AccumulateCategoricalKeyed(histogramKey
,
773 LABELS_SQLITE_STORE_OPEN::diskspace
);
776 AccumulateCategoricalKeyed(histogramKey
,
777 LABELS_SQLITE_STORE_OPEN::failure
);
781 void Connection::RecordQueryStatus(int srv
) {
782 nsCString histogramKey
= mTelemetryFilename
;
784 if (histogramKey
.IsEmpty()) {
785 histogramKey
.AssignLiteral("unknown");
793 // Note that these are returned when we intentionally cancel a statement so
794 // they aren't indicating a failure.
796 case SQLITE_INTERRUPT
:
797 AccumulateCategoricalKeyed(histogramKey
,
798 LABELS_SQLITE_STORE_QUERY::success
);
802 AccumulateCategoricalKeyed(histogramKey
,
803 LABELS_SQLITE_STORE_QUERY::corrupt
);
806 case SQLITE_CANTOPEN
:
808 case SQLITE_READONLY
:
809 AccumulateCategoricalKeyed(histogramKey
,
810 LABELS_SQLITE_STORE_QUERY::access
);
814 AccumulateCategoricalKeyed(histogramKey
,
815 LABELS_SQLITE_STORE_QUERY::diskio
);
819 AccumulateCategoricalKeyed(histogramKey
,
820 LABELS_SQLITE_STORE_QUERY::diskspace
);
822 case SQLITE_CONSTRAINT
:
824 case SQLITE_MISMATCH
:
826 AccumulateCategoricalKeyed(histogramKey
,
827 LABELS_SQLITE_STORE_QUERY::misuse
);
830 AccumulateCategoricalKeyed(histogramKey
, LABELS_SQLITE_STORE_QUERY::busy
);
833 AccumulateCategoricalKeyed(histogramKey
,
834 LABELS_SQLITE_STORE_QUERY::failure
);
838 nsresult
Connection::initialize(const nsACString
& aStorageKey
,
839 const nsACString
& aName
) {
840 MOZ_ASSERT(aStorageKey
.Equals(kMozStorageMemoryStorageKey
));
841 NS_ASSERTION(!connectionReady(),
842 "Initialize called on already opened database!");
843 MOZ_ASSERT(!mIgnoreLockingMode
, "Can't ignore locking on an in-memory db.");
844 AUTO_PROFILER_LABEL("Connection::initialize", OTHER
);
846 mStorageKey
= aStorageKey
;
849 // in memory database requested, sqlite uses a magic file name
851 const nsAutoCString path
=
852 mName
.IsEmpty() ? nsAutoCString(":memory:"_ns
)
853 : "file:"_ns
+ mName
+ "?mode=memory&cache=shared"_ns
;
855 int srv
= ::sqlite3_open_v2(path
.get(), &mDBConn
, mFlags
,
856 basevfs::GetVFSName(true));
857 if (srv
!= SQLITE_OK
) {
859 nsresult rv
= convertResultCode(srv
);
860 RecordOpenStatus(rv
);
864 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
866 ::sqlite3_db_config(mDBConn
, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER
, 1, 0);
867 MOZ_ASSERT(srv
== SQLITE_OK
,
868 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
871 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
874 nsresult rv
= initializeInternal();
875 RecordOpenStatus(rv
);
876 NS_ENSURE_SUCCESS(rv
, rv
);
881 nsresult
Connection::initialize(nsIFile
* aDatabaseFile
) {
882 NS_ASSERTION(aDatabaseFile
, "Passed null file!");
883 NS_ASSERTION(!connectionReady(),
884 "Initialize called on already opened database!");
885 AUTO_PROFILER_LABEL("Connection::initialize", OTHER
);
887 // Do not set mFileURL here since this is database does not have an associated
889 mDatabaseFile
= aDatabaseFile
;
892 nsresult rv
= aDatabaseFile
->GetPath(path
);
893 NS_ENSURE_SUCCESS(rv
, rv
);
895 bool exclusive
= StaticPrefs::storage_sqlite_exclusiveLock_enabled();
897 if (mIgnoreLockingMode
) {
899 srv
= ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path
).get(), &mDBConn
, mFlags
,
900 "readonly-immutable-nolock");
902 srv
= ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path
).get(), &mDBConn
, mFlags
,
903 basevfs::GetVFSName(exclusive
));
904 if (exclusive
&& (srv
== SQLITE_LOCKED
|| srv
== SQLITE_BUSY
)) {
905 // Retry without trying to get an exclusive lock.
907 srv
= ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path
).get(), &mDBConn
,
908 mFlags
, basevfs::GetVFSName(false));
911 if (srv
!= SQLITE_OK
) {
913 rv
= convertResultCode(srv
);
914 RecordOpenStatus(rv
);
918 rv
= initializeInternal();
920 (rv
== NS_ERROR_STORAGE_BUSY
|| rv
== NS_ERROR_FILE_IS_LOCKED
)) {
921 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
922 // some cases it may successfully open the database and then lock on the
923 // first query execution. When initializeInternal fails it closes the
924 // connection, so we can try to restart it in non-exclusive mode.
925 srv
= ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path
).get(), &mDBConn
, mFlags
,
926 basevfs::GetVFSName(false));
927 if (srv
== SQLITE_OK
) {
928 rv
= initializeInternal();
932 RecordOpenStatus(rv
);
933 NS_ENSURE_SUCCESS(rv
, rv
);
938 nsresult
Connection::initialize(nsIFileURL
* aFileURL
) {
939 NS_ASSERTION(aFileURL
, "Passed null file URL!");
940 NS_ASSERTION(!connectionReady(),
941 "Initialize called on already opened database!");
942 AUTO_PROFILER_LABEL("Connection::initialize", OTHER
);
944 nsCOMPtr
<nsIFile
> databaseFile
;
945 nsresult rv
= aFileURL
->GetFile(getter_AddRefs(databaseFile
));
946 NS_ENSURE_SUCCESS(rv
, rv
);
948 // Set both mDatabaseFile and mFileURL here.
950 mDatabaseFile
= databaseFile
;
953 rv
= aFileURL
->GetSpec(spec
);
954 NS_ENSURE_SUCCESS(rv
, rv
);
956 // If there is a key specified, we need to use the obfuscating VFS.
958 rv
= aFileURL
->GetQuery(query
);
959 NS_ENSURE_SUCCESS(rv
, rv
);
962 bool hasDirectoryLockId
= false;
964 MOZ_ALWAYS_TRUE(URLParams::Parse(
965 query
, [&hasKey
, &hasDirectoryLockId
](const nsAString
& aName
,
966 const nsAString
& aValue
) {
967 if (aName
.EqualsLiteral("key")) {
971 if (aName
.EqualsLiteral("directoryLockId")) {
972 hasDirectoryLockId
= true;
978 bool exclusive
= StaticPrefs::storage_sqlite_exclusiveLock_enabled();
980 const char* const vfs
= hasKey
? obfsvfs::GetVFSName()
981 : hasDirectoryLockId
? quotavfs::GetVFSName()
982 : basevfs::GetVFSName(exclusive
);
984 int srv
= ::sqlite3_open_v2(spec
.get(), &mDBConn
, mFlags
, vfs
);
985 if (srv
!= SQLITE_OK
) {
987 rv
= convertResultCode(srv
);
988 RecordOpenStatus(rv
);
992 rv
= initializeInternal();
993 RecordOpenStatus(rv
);
994 NS_ENSURE_SUCCESS(rv
, rv
);
999 nsresult
Connection::initializeInternal() {
1000 MOZ_ASSERT(mDBConn
);
1001 auto guard
= MakeScopeExit([&]() { initializeFailed(); });
1003 mConnectionClosed
= false;
1005 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
1006 DebugOnly
<int> srv2
=
1007 ::sqlite3_db_config(mDBConn
, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER
, 1, 0);
1008 MOZ_ASSERT(srv2
== SQLITE_OK
,
1009 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
1012 // Properly wrap the database handle's mutex.
1013 sharedDBMutex
.initWithMutex(sqlite3_db_mutex(mDBConn
));
1015 // SQLite tracing can slow down queries (especially long queries)
1016 // significantly. Don't trace unless the user is actively monitoring SQLite.
1017 if (MOZ_LOG_TEST(gStorageLog
, LogLevel::Debug
)) {
1018 ::sqlite3_trace_v2(mDBConn
, SQLITE_TRACE_STMT
| SQLITE_TRACE_PROFILE
,
1022 gStorageLog
, LogLevel::Debug
,
1023 ("Opening connection to '%s' (%p)", mTelemetryFilename
.get(), this));
1026 int64_t pageSize
= Service::kDefaultPageSize
;
1028 // Set page_size to the preferred default value. This is effective only if
1029 // the database has just been created, otherwise, if the database does not
1030 // use WAL journal mode, a VACUUM operation will updated its page_size.
1031 nsAutoCString
pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1032 "PRAGMA page_size = ");
1033 pageSizeQuery
.AppendInt(pageSize
);
1034 int srv
= executeSql(mDBConn
, pageSizeQuery
.get());
1035 if (srv
!= SQLITE_OK
) {
1036 return convertResultCode(srv
);
1039 // Setting the cache_size forces the database open, verifying if it is valid
1040 // or corrupt. So this is executed regardless it being actually needed.
1041 // The cache_size is calculated from the actual page_size, to save memory.
1042 nsAutoCString
cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1043 "PRAGMA cache_size = ");
1044 cacheSizeQuery
.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES
);
1045 srv
= executeSql(mDBConn
, cacheSizeQuery
.get());
1046 if (srv
!= SQLITE_OK
) {
1047 return convertResultCode(srv
);
1050 // Register our built-in SQL functions.
1051 srv
= registerFunctions(mDBConn
);
1052 if (srv
!= SQLITE_OK
) {
1053 return convertResultCode(srv
);
1056 // Register our built-in SQL collating sequences.
1057 srv
= registerCollations(mDBConn
, mStorageService
);
1058 if (srv
!= SQLITE_OK
) {
1059 return convertResultCode(srv
);
1062 // Set the default synchronous value. Each consumer can switch this
1063 // accordingly to their needs.
1064 #if defined(ANDROID)
1065 // Android prefers synchronous = OFF for performance reasons.
1066 Unused
<< ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns
);
1068 // Normal is the suggested value for WAL journals.
1069 Unused
<< ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns
);
1072 // Initialization succeeded, we can stop guarding for failures.
1077 nsresult
Connection::initializeOnAsyncThread(nsIFile
* aStorageFile
) {
1078 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn
));
1079 nsresult rv
= aStorageFile
1080 ? initialize(aStorageFile
)
1081 : initialize(kMozStorageMemoryStorageKey
, VoidCString());
1082 if (NS_FAILED(rv
)) {
1083 // Shutdown the async thread, since initialization failed.
1084 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
1085 mAsyncExecutionThreadShuttingDown
= true;
1086 nsCOMPtr
<nsIRunnable
> event
=
1087 NewRunnableMethod("Connection::shutdownAsyncThread", this,
1088 &Connection::shutdownAsyncThread
);
1089 Unused
<< NS_DispatchToMainThread(event
);
1094 void Connection::initializeFailed() {
1096 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
1097 mConnectionClosed
= true;
1099 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn
) == SQLITE_OK
);
1101 sharedDBMutex
.destroy();
1104 nsresult
Connection::databaseElementExists(
1105 enum DatabaseElementType aElementType
, const nsACString
& aElementName
,
1107 if (!connectionReady()) {
1108 return NS_ERROR_NOT_AVAILABLE
;
1110 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
1111 if (NS_FAILED(rv
)) {
1115 // When constructing the query, make sure to SELECT the correct db's
1116 // sqlite_master if the user is prefixing the element with a specific db. ex:
1118 nsCString
query("SELECT name FROM (SELECT * FROM ");
1119 nsDependentCSubstring element
;
1120 int32_t ind
= aElementName
.FindChar('.');
1121 if (ind
== kNotFound
) {
1122 element
.Assign(aElementName
);
1124 nsDependentCSubstring
db(Substring(aElementName
, 0, ind
+ 1));
1125 element
.Assign(Substring(aElementName
, ind
+ 1, aElementName
.Length()));
1128 query
.AppendLiteral(
1129 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
1132 switch (aElementType
) {
1134 query
.AppendLiteral("index");
1137 query
.AppendLiteral("table");
1140 query
.AppendLiteral("' AND name ='");
1141 query
.Append(element
);
1145 int srv
= prepareStatement(mDBConn
, query
, &stmt
);
1146 if (srv
!= SQLITE_OK
) {
1147 RecordQueryStatus(srv
);
1148 return convertResultCode(srv
);
1151 srv
= stepStatement(mDBConn
, stmt
);
1152 // we just care about the return value from step
1153 (void)::sqlite3_finalize(stmt
);
1155 RecordQueryStatus(srv
);
1157 if (srv
== SQLITE_ROW
) {
1161 if (srv
== SQLITE_DONE
) {
1166 return convertResultCode(srv
);
1169 bool Connection::findFunctionByInstance(mozIStorageFunction
* aInstance
) {
1170 sharedDBMutex
.assertCurrentThreadOwns();
1172 for (const auto& data
: mFunctions
.Values()) {
1173 if (data
.function
== aInstance
) {
1181 int Connection::sProgressHelper(void* aArg
) {
1182 Connection
* _this
= static_cast<Connection
*>(aArg
);
1183 return _this
->progressHandler();
1186 int Connection::progressHandler() {
1187 sharedDBMutex
.assertCurrentThreadOwns();
1188 if (mProgressHandler
) {
1190 nsresult rv
= mProgressHandler
->OnProgress(this, &result
);
1191 if (NS_FAILED(rv
)) return 0; // Don't break request
1192 return result
? 1 : 0;
1197 nsresult
Connection::setClosedState() {
1198 // Flag that we are shutting down the async thread, so that
1199 // getAsyncExecutionTarget knows not to expose/create the async thread.
1200 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
1201 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown
, NS_ERROR_UNEXPECTED
);
1203 mAsyncExecutionThreadShuttingDown
= true;
1205 // Set the property to null before closing the connection, otherwise the
1206 // other functions in the module may try to use the connection after it is
1213 bool Connection::operationSupported(ConnectionOperation aOperationType
) {
1214 if (aOperationType
== ASYNCHRONOUS
) {
1215 // Async operations are supported for all connections, on any thread.
1218 // Sync operations are supported for sync connections (on any thread), and
1219 // async connections on a background thread.
1220 MOZ_ASSERT(aOperationType
== SYNCHRONOUS
);
1221 return mSupportedOperations
== SYNCHRONOUS
|| !NS_IsMainThread();
1224 nsresult
Connection::ensureOperationSupported(
1225 ConnectionOperation aOperationType
) {
1226 if (NS_WARN_IF(!operationSupported(aOperationType
))) {
1228 if (NS_IsMainThread()) {
1229 nsCOMPtr
<nsIXPConnect
> xpc
= nsIXPConnect::XPConnect();
1230 Unused
<< xpc
->DebugDumpJSStack(false, false, false);
1234 "Don't use async connections synchronously on the main thread");
1235 return NS_ERROR_NOT_AVAILABLE
;
1240 bool Connection::isConnectionReadyOnThisThread() {
1241 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed
);
1242 if (mAsyncExecutionThread
&& mAsyncExecutionThread
->IsOnCurrentThread()) {
1245 return connectionReady();
1248 bool Connection::isClosing() {
1249 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
1250 return mAsyncExecutionThreadShuttingDown
&& !mConnectionClosed
;
1253 bool Connection::isClosed() {
1254 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
1255 return mConnectionClosed
;
1258 bool Connection::isClosed(MutexAutoLock
& lock
) { return mConnectionClosed
; }
1260 bool Connection::isAsyncExecutionThreadAvailable() {
1261 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn
));
1262 return mAsyncExecutionThread
&& !mAsyncExecutionThreadShuttingDown
;
1265 void Connection::shutdownAsyncThread() {
1266 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn
));
1267 MOZ_ASSERT(mAsyncExecutionThread
);
1268 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown
);
1270 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread
->Shutdown());
1271 mAsyncExecutionThread
= nullptr;
1274 nsresult
Connection::internalClose(sqlite3
* aNativeConnection
) {
1276 { // Make sure we have marked our async thread as shutting down.
1277 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
1278 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown
,
1279 "Did not call setClosedState!");
1280 MOZ_ASSERT(!isClosed(lockedScope
), "Unexpected closed state");
1284 if (MOZ_LOG_TEST(gStorageLog
, LogLevel::Debug
)) {
1285 nsAutoCString
leafName(":memory");
1286 if (mDatabaseFile
) (void)mDatabaseFile
->GetNativeLeafName(leafName
);
1287 MOZ_LOG(gStorageLog
, LogLevel::Debug
,
1288 ("Closing connection to '%s'", leafName
.get()));
1291 // At this stage, we may still have statements that need to be
1292 // finalized. Attempt to close the database connection. This will
1293 // always disconnect any virtual tables and cleanly finalize their
1294 // internal statements. Once this is done, closing may fail due to
1295 // unfinalized client statements, in which case we need to finalize
1296 // these statements and close again.
1298 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
1299 mConnectionClosed
= true;
1302 // Nothing else needs to be done if we don't have a connection here.
1303 if (!aNativeConnection
) return NS_OK
;
1305 int srv
= ::sqlite3_close(aNativeConnection
);
1307 if (srv
== SQLITE_BUSY
) {
1309 // Nothing else should change the connection or statements status until we
1311 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
1312 // We still have non-finalized statements. Finalize them.
1313 sqlite3_stmt
* stmt
= nullptr;
1314 while ((stmt
= ::sqlite3_next_stmt(aNativeConnection
, stmt
))) {
1315 MOZ_LOG(gStorageLog
, LogLevel::Debug
,
1316 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt
),
1320 SmprintfPointer msg
= ::mozilla::Smprintf(
1321 "SQL statement '%s' (%p) should have been finalized before closing "
1323 ::sqlite3_sql(stmt
), stmt
);
1324 NS_WARNING(msg
.get());
1327 srv
= ::sqlite3_finalize(stmt
);
1330 if (srv
!= SQLITE_OK
) {
1331 SmprintfPointer msg
= ::mozilla::Smprintf(
1332 "Could not finalize SQL statement (%p)", stmt
);
1333 NS_WARNING(msg
.get());
1337 // Ensure that the loop continues properly, whether closing has
1338 // succeeded or not.
1339 if (srv
== SQLITE_OK
) {
1343 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1344 // again, since Sqlite will try to acquire it.
1347 // Now that all statements have been finalized, we
1348 // should be able to close.
1349 srv
= ::sqlite3_close(aNativeConnection
);
1351 "Had to forcibly close the database connection because not all "
1352 "the statements have been finalized.");
1355 if (srv
== SQLITE_OK
) {
1356 sharedDBMutex
.destroy();
1359 "sqlite3_close failed. There are probably outstanding "
1360 "statements that are listed above!");
1363 return convertResultCode(srv
);
1366 nsCString
Connection::getFilename() { return mTelemetryFilename
; }
1368 int Connection::stepStatement(sqlite3
* aNativeConnection
,
1369 sqlite3_stmt
* aStatement
) {
1370 MOZ_ASSERT(aStatement
);
1372 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER
,
1373 ::sqlite3_sql(aStatement
));
1375 bool checkedMainThread
= false;
1376 TimeStamp startTime
= TimeStamp::Now();
1378 // The connection may have been closed if the executing statement has been
1379 // created and cached after a call to asyncClose() but before the actual
1380 // sqlite3_close(). This usually happens when other tasks using cached
1381 // statements are asynchronously scheduled for execution and any of them ends
1382 // up after asyncClose. See bug 728653 for details.
1383 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE
;
1385 (void)::sqlite3_extended_result_codes(aNativeConnection
, 1);
1388 while ((srv
= ::sqlite3_step(aStatement
)) == SQLITE_LOCKED_SHAREDCACHE
) {
1389 if (!checkedMainThread
) {
1390 checkedMainThread
= true;
1391 if (::NS_IsMainThread()) {
1392 NS_WARNING("We won't allow blocking on the main thread!");
1397 srv
= WaitForUnlockNotify(aNativeConnection
);
1398 if (srv
!= SQLITE_OK
) {
1402 ::sqlite3_reset(aStatement
);
1405 // Report very slow SQL statements to Telemetry
1406 TimeDuration duration
= TimeStamp::Now() - startTime
;
1407 const uint32_t threshold
= NS_IsMainThread()
1408 ? Telemetry::kSlowSQLThresholdForMainThread
1409 : Telemetry::kSlowSQLThresholdForHelperThreads
;
1410 if (duration
.ToMilliseconds() >= threshold
) {
1411 nsDependentCString
statementString(::sqlite3_sql(aStatement
));
1412 Telemetry::RecordSlowSQLStatement(
1413 statementString
, mTelemetryFilename
,
1414 static_cast<uint32_t>(duration
.ToMilliseconds()));
1417 (void)::sqlite3_extended_result_codes(aNativeConnection
, 0);
1418 // Drop off the extended result bits of the result code.
1422 int Connection::prepareStatement(sqlite3
* aNativeConnection
,
1423 const nsCString
& aSQL
, sqlite3_stmt
** _stmt
) {
1424 // We should not even try to prepare statements after the connection has
1426 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE
;
1428 bool checkedMainThread
= false;
1430 (void)::sqlite3_extended_result_codes(aNativeConnection
, 1);
1433 while ((srv
= ::sqlite3_prepare_v2(aNativeConnection
, aSQL
.get(), -1, _stmt
,
1434 nullptr)) == SQLITE_LOCKED_SHAREDCACHE
) {
1435 if (!checkedMainThread
) {
1436 checkedMainThread
= true;
1437 if (::NS_IsMainThread()) {
1438 NS_WARNING("We won't allow blocking on the main thread!");
1443 srv
= WaitForUnlockNotify(aNativeConnection
);
1444 if (srv
!= SQLITE_OK
) {
1449 if (srv
!= SQLITE_OK
) {
1451 warnMsg
.AppendLiteral("The SQL statement '");
1452 warnMsg
.Append(aSQL
);
1453 warnMsg
.AppendLiteral("' could not be compiled due to an error: ");
1454 warnMsg
.Append(::sqlite3_errmsg(aNativeConnection
));
1457 NS_WARNING(warnMsg
.get());
1459 MOZ_LOG(gStorageLog
, LogLevel::Error
, ("%s", warnMsg
.get()));
1462 (void)::sqlite3_extended_result_codes(aNativeConnection
, 0);
1463 // Drop off the extended result bits of the result code.
1464 int rc
= srv
& 0xFF;
1465 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1466 // The callers of this function are used to only checking the return value,
1467 // so it is safer to return an error code.
1468 if (rc
== SQLITE_OK
&& *_stmt
== nullptr) {
1469 return SQLITE_MISUSE
;
1475 int Connection::executeSql(sqlite3
* aNativeConnection
, const char* aSqlString
) {
1476 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE
;
1478 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER
, aSqlString
);
1480 TimeStamp startTime
= TimeStamp::Now();
1482 ::sqlite3_exec(aNativeConnection
, aSqlString
, nullptr, nullptr, nullptr);
1483 RecordQueryStatus(srv
);
1485 // Report very slow SQL statements to Telemetry
1486 TimeDuration duration
= TimeStamp::Now() - startTime
;
1487 const uint32_t threshold
= NS_IsMainThread()
1488 ? Telemetry::kSlowSQLThresholdForMainThread
1489 : Telemetry::kSlowSQLThresholdForHelperThreads
;
1490 if (duration
.ToMilliseconds() >= threshold
) {
1491 nsDependentCString
statementString(aSqlString
);
1492 Telemetry::RecordSlowSQLStatement(
1493 statementString
, mTelemetryFilename
,
1494 static_cast<uint32_t>(duration
.ToMilliseconds()));
1500 ////////////////////////////////////////////////////////////////////////////////
1501 //// nsIInterfaceRequestor
1504 Connection::GetInterface(const nsIID
& aIID
, void** _result
) {
1505 if (aIID
.Equals(NS_GET_IID(nsIEventTarget
))) {
1506 nsIEventTarget
* background
= getAsyncExecutionTarget();
1507 NS_IF_ADDREF(background
);
1508 *_result
= background
;
1511 return NS_ERROR_NO_INTERFACE
;
1514 ////////////////////////////////////////////////////////////////////////////////
1515 //// mozIStorageConnection
1518 Connection::Close() {
1519 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
1520 if (NS_FAILED(rv
)) {
1523 return synchronousClose();
1526 nsresult
Connection::synchronousClose() {
1527 if (!connectionReady()) {
1528 return NS_ERROR_NOT_INITIALIZED
;
1532 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1533 // event target. We make this check outside of debug code below in
1534 // setClosedState, but this is here to be explicit.
1535 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn
));
1538 // Make sure we have not executed any asynchronous statements.
1539 // If this fails, the mDBConn may be left open, resulting in a leak.
1540 // We'll try to finalize the pending statements and close the connection.
1541 if (isAsyncExecutionThreadAvailable()) {
1543 if (NS_IsMainThread()) {
1544 nsCOMPtr
<nsIXPConnect
> xpc
= nsIXPConnect::XPConnect();
1545 Unused
<< xpc
->DebugDumpJSStack(false, false, false);
1549 "Close() was invoked on a connection that executed asynchronous "
1551 "Should have used asyncClose().");
1552 // Try to close the database regardless, to free up resources.
1553 Unused
<< SpinningSynchronousClose();
1554 return NS_ERROR_UNEXPECTED
;
1557 // setClosedState nullifies our connection pointer, so we take a raw pointer
1558 // off it, to pass it through the close procedure.
1559 sqlite3
* nativeConn
= mDBConn
;
1560 nsresult rv
= setClosedState();
1561 NS_ENSURE_SUCCESS(rv
, rv
);
1563 return internalClose(nativeConn
);
1567 Connection::SpinningSynchronousClose() {
1568 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
1569 if (NS_FAILED(rv
)) {
1572 if (!IsOnCurrentSerialEventTarget(eventTargetOpenedOn
)) {
1573 return NS_ERROR_NOT_SAME_THREAD
;
1576 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1577 // Our only existing caller will never have called close; assert if misused
1578 // so that no new callers assume this works after an AsyncClose.
1579 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1580 if (!connectionReady()) {
1581 return NS_ERROR_UNEXPECTED
;
1584 RefPtr
<CloseListener
> listener
= new CloseListener();
1585 rv
= AsyncClose(listener
);
1586 NS_ENSURE_SUCCESS(rv
, rv
);
1588 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns
,
1589 [&]() { return listener
->mClosed
; }));
1590 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1596 Connection::AsyncClose(mozIStorageCompletionCallback
* aCallback
) {
1597 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD
);
1598 // Check if AsyncClose or Close were already invoked.
1599 if (!connectionReady()) {
1600 return NS_ERROR_NOT_INITIALIZED
;
1602 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
1603 if (NS_FAILED(rv
)) {
1607 // The two relevant factors at this point are whether we have a database
1608 // connection and whether we have an async execution thread. Here's what the
1609 // states mean and how we handle them:
1611 // - (mDBConn && asyncThread): The expected case where we are either an
1612 // async connection or a sync connection that has been used asynchronously.
1613 // Either way the caller must call us and not Close(). Nothing surprising
1614 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1617 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1618 // opened the connection synchronously and was planning to use it
1619 // asynchronously, but never got around to using it asynchronously before
1620 // needing to shutdown. This has been observed to happen for the cookie
1621 // service in a case where Firefox shuts itself down almost immediately
1622 // after startup (for unknown reasons). In the Firefox shutdown case,
1623 // we may also fail to create a new async execution thread if one does not
1624 // already exist. (nsThreadManager will refuse to create new threads when
1625 // it has already been told to shutdown.) As such, we need to handle a
1626 // failure to create the async execution thread by falling back to
1627 // synchronous Close() and also dispatching the completion callback because
1628 // at least Places likes to spin a nested event loop that depends on the
1629 // callback being invoked.
1631 // Note that we have considered not trying to spin up the async execution
1632 // thread in this case if it does not already exist, but the overhead of
1633 // thread startup (if successful) is significantly less expensive than the
1634 // worst-case potential I/O hit of synchronously closing a database when we
1635 // could close it asynchronously.
1637 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1638 // OpenAsyncDatabase encountered a problem opening the database. If it
1639 // happened in all cases AsyncInitDatabase would just shut down the thread
1640 // directly and we would avoid this case. But it doesn't, so for simplicity
1641 // and consistency AsyncCloseConnection knows how to handle this and we
1642 // act like this was the (mDBConn && asyncThread) case in this method.
1644 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1645 // Close() or AsyncClose() has already been called (at least) once. This is
1646 // undeniably a misuse case by the caller. We could optimize for this
1647 // case by adding an additional check of mAsyncExecutionThread without using
1648 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1649 // shut it down. But this complicates the method for broken caller code
1650 // whereas we're still correct and safe without the special-case.
1651 nsIEventTarget
* asyncThread
= getAsyncExecutionTarget();
1653 // Create our callback event if we were given a callback. This will
1654 // eventually be dispatched in all cases, even if we fall back to Close() and
1655 // the database wasn't open and we return an error. The rationale is that
1656 // no existing consumer checks our return value and several of them like to
1657 // spin nested event loops until the callback fires. Given that, it seems
1658 // preferable for us to dispatch the callback in all cases. (Except the
1659 // wrong thread misuse case we bailed on up above. But that's okay because
1660 // that is statically wrong whereas these edge cases are dynamic.)
1661 nsCOMPtr
<nsIRunnable
> completeEvent
;
1663 completeEvent
= newCompletionEvent(aCallback
);
1667 // We were unable to create an async thread, so we need to fall back to
1668 // using normal Close(). Since there is no async thread, Close() will
1669 // not complain about that. (Close() may, however, complain if the
1670 // connection is closed, but that's okay.)
1671 if (completeEvent
) {
1672 // Closing the database is more important than returning an error code
1673 // about a failure to dispatch, especially because all existing native
1674 // callers ignore our return value.
1675 Unused
<< NS_DispatchToMainThread(completeEvent
.forget());
1677 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1678 // Return a success inconditionally here, since Close() is unlikely to fail
1679 // and we want to reassure the consumer that its callback will be invoked.
1683 // If we're closing the connection during shutdown, and there is an
1684 // interruptible statement running on the helper thread, issue a
1685 // sqlite3_interrupt() to avoid crashing when that statement takes a long
1686 // time (for example a vacuum).
1687 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed
) &&
1688 mInterruptible
&& mIsStatementOnHelperThreadInterruptible
) {
1689 MOZ_ASSERT(!isClosing(), "Must not be closing, see Interrupt()");
1690 DebugOnly
<nsresult
> rv2
= Interrupt();
1691 MOZ_ASSERT(NS_SUCCEEDED(rv2
));
1694 // setClosedState nullifies our connection pointer, so we take a raw pointer
1695 // off it, to pass it through the close procedure.
1696 sqlite3
* nativeConn
= mDBConn
;
1697 rv
= setClosedState();
1698 NS_ENSURE_SUCCESS(rv
, rv
);
1700 // Create and dispatch our close event to the background thread.
1701 nsCOMPtr
<nsIRunnable
> closeEvent
=
1702 new AsyncCloseConnection(this, nativeConn
, completeEvent
);
1703 rv
= asyncThread
->Dispatch(closeEvent
, NS_DISPATCH_NORMAL
);
1704 NS_ENSURE_SUCCESS(rv
, rv
);
1710 Connection::AsyncClone(bool aReadOnly
,
1711 mozIStorageCompletionCallback
* aCallback
) {
1712 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER
);
1714 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD
);
1715 if (!connectionReady()) {
1716 return NS_ERROR_NOT_INITIALIZED
;
1718 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
1719 if (NS_FAILED(rv
)) {
1722 if (!mDatabaseFile
) return NS_ERROR_UNEXPECTED
;
1726 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1727 flags
= (~SQLITE_OPEN_READWRITE
& flags
) | SQLITE_OPEN_READONLY
;
1728 // Turn off SQLITE_OPEN_CREATE.
1729 flags
= (~SQLITE_OPEN_CREATE
& flags
);
1732 // The cloned connection will still implement the synchronous API, but throw
1733 // if any synchronous methods are called on the main thread.
1734 RefPtr
<Connection
> clone
=
1735 new Connection(mStorageService
, flags
, ASYNCHRONOUS
, mTelemetryFilename
);
1737 RefPtr
<AsyncInitializeClone
> initEvent
=
1738 new AsyncInitializeClone(this, clone
, aReadOnly
, aCallback
);
1739 // Dispatch to our async thread, since the originating connection must remain
1740 // valid and open for the whole cloning process. This also ensures we are
1741 // properly serialized with a `close` operation, rather than race with it.
1742 nsCOMPtr
<nsIEventTarget
> target
= getAsyncExecutionTarget();
1744 return NS_ERROR_UNEXPECTED
;
1746 return target
->Dispatch(initEvent
, NS_DISPATCH_NORMAL
);
1749 nsresult
Connection::initializeClone(Connection
* aClone
, bool aReadOnly
) {
1751 if (!mStorageKey
.IsEmpty()) {
1752 rv
= aClone
->initialize(mStorageKey
, mName
);
1753 } else if (mFileURL
) {
1754 rv
= aClone
->initialize(mFileURL
);
1756 rv
= aClone
->initialize(mDatabaseFile
);
1758 if (NS_FAILED(rv
)) {
1762 auto guard
= MakeScopeExit([&]() { aClone
->initializeFailed(); });
1764 rv
= aClone
->SetDefaultTransactionType(mDefaultTransactionType
);
1765 NS_ENSURE_SUCCESS(rv
, rv
);
1767 // Re-attach on-disk databases that were attached to the original connection.
1769 nsCOMPtr
<mozIStorageStatement
> stmt
;
1770 rv
= CreateStatement("PRAGMA database_list"_ns
, getter_AddRefs(stmt
));
1771 NS_ENSURE_SUCCESS(rv
, rv
);
1772 bool hasResult
= false;
1773 while (stmt
&& NS_SUCCEEDED(stmt
->ExecuteStep(&hasResult
)) && hasResult
) {
1775 rv
= stmt
->GetUTF8String(1, name
);
1776 if (NS_SUCCEEDED(rv
) && !name
.EqualsLiteral("main") &&
1777 !name
.EqualsLiteral("temp")) {
1779 rv
= stmt
->GetUTF8String(2, path
);
1780 if (NS_SUCCEEDED(rv
) && !path
.IsEmpty()) {
1781 nsCOMPtr
<mozIStorageStatement
> attachStmt
;
1782 rv
= aClone
->CreateStatement("ATTACH DATABASE :path AS "_ns
+ name
,
1783 getter_AddRefs(attachStmt
));
1784 NS_ENSURE_SUCCESS(rv
, rv
);
1785 rv
= attachStmt
->BindUTF8StringByName("path"_ns
, path
);
1786 NS_ENSURE_SUCCESS(rv
, rv
);
1787 rv
= attachStmt
->Execute();
1788 NS_ENSURE_SUCCESS(rv
, rv
);
1794 // Copy over pragmas from the original connection.
1795 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1796 // schema ("main" and any other attached databases), and this implmentation
1797 // fails to propagate them. This is being addressed on trunk.
1798 static const char* pragmas
[] = {
1799 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1800 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1801 for (auto& pragma
: pragmas
) {
1802 // Read-only connections just need cache_size and temp_store pragmas.
1803 if (aReadOnly
&& ::strcmp(pragma
, "cache_size") != 0 &&
1804 ::strcmp(pragma
, "temp_store") != 0) {
1808 nsAutoCString
pragmaQuery("PRAGMA ");
1809 pragmaQuery
.Append(pragma
);
1810 nsCOMPtr
<mozIStorageStatement
> stmt
;
1811 rv
= CreateStatement(pragmaQuery
, getter_AddRefs(stmt
));
1812 NS_ENSURE_SUCCESS(rv
, rv
);
1813 bool hasResult
= false;
1814 if (stmt
&& NS_SUCCEEDED(stmt
->ExecuteStep(&hasResult
)) && hasResult
) {
1815 pragmaQuery
.AppendLiteral(" = ");
1816 pragmaQuery
.AppendInt(stmt
->AsInt32(0));
1817 rv
= aClone
->ExecuteSimpleSQL(pragmaQuery
);
1818 NS_ENSURE_SUCCESS(rv
, rv
);
1822 // Copy over temporary tables, triggers, and views from the original
1823 // connections. Entities in `sqlite_temp_master` are only visible to the
1824 // connection that created them.
1826 rv
= aClone
->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns
);
1827 NS_ENSURE_SUCCESS(rv
, rv
);
1829 nsCOMPtr
<mozIStorageStatement
> stmt
;
1830 rv
= CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1831 "WHERE type IN ('table', 'view', "
1832 "'index', 'trigger')"),
1833 getter_AddRefs(stmt
));
1834 // Propagate errors, because failing to copy triggers might cause schema
1835 // coherency issues when writing to the database from the cloned connection.
1836 NS_ENSURE_SUCCESS(rv
, rv
);
1837 bool hasResult
= false;
1838 while (stmt
&& NS_SUCCEEDED(stmt
->ExecuteStep(&hasResult
)) && hasResult
) {
1839 nsAutoCString query
;
1840 rv
= stmt
->GetUTF8String(0, query
);
1841 NS_ENSURE_SUCCESS(rv
, rv
);
1843 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1844 // keyword. We need to add it back, or we'll recreate temporary entities
1845 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1846 // statements, but those don't need `TEMP` keywords.
1847 if (StringBeginsWith(query
, "CREATE TABLE "_ns
) ||
1848 StringBeginsWith(query
, "CREATE TRIGGER "_ns
) ||
1849 StringBeginsWith(query
, "CREATE VIEW "_ns
)) {
1850 query
.Replace(0, 6, "CREATE TEMP");
1853 rv
= aClone
->ExecuteSimpleSQL(query
);
1854 NS_ENSURE_SUCCESS(rv
, rv
);
1857 rv
= aClone
->ExecuteSimpleSQL("COMMIT"_ns
);
1858 NS_ENSURE_SUCCESS(rv
, rv
);
1861 // Copy any functions that have been added to this connection.
1862 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
1863 for (const auto& entry
: mFunctions
) {
1864 const nsACString
& key
= entry
.GetKey();
1865 Connection::FunctionInfo data
= entry
.GetData();
1867 rv
= aClone
->CreateFunction(key
, data
.numArgs
, data
.function
);
1868 if (NS_FAILED(rv
)) {
1869 NS_WARNING("Failed to copy function to cloned connection");
1873 // Load SQLite extensions that were on this connection.
1874 // Copy into an array rather than holding the mutex while we load extensions.
1875 nsTArray
<nsCString
> loadedExtensions
;
1877 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
1878 AppendToArray(loadedExtensions
, mLoadedExtensions
);
1880 for (const auto& extension
: loadedExtensions
) {
1881 (void)aClone
->LoadExtension(extension
, nullptr);
1889 Connection::Clone(bool aReadOnly
, mozIStorageConnection
** _connection
) {
1890 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn
));
1892 AUTO_PROFILER_LABEL("Connection::Clone", OTHER
);
1894 if (!connectionReady()) {
1895 return NS_ERROR_NOT_INITIALIZED
;
1897 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
1898 if (NS_FAILED(rv
)) {
1904 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1905 flags
= (~SQLITE_OPEN_READWRITE
& flags
) | SQLITE_OPEN_READONLY
;
1906 // Turn off SQLITE_OPEN_CREATE.
1907 flags
= (~SQLITE_OPEN_CREATE
& flags
);
1910 RefPtr
<Connection
> clone
=
1911 new Connection(mStorageService
, flags
, mSupportedOperations
,
1912 mTelemetryFilename
, mInterruptible
);
1914 rv
= initializeClone(clone
, aReadOnly
);
1915 if (NS_FAILED(rv
)) {
1919 NS_IF_ADDREF(*_connection
= clone
);
1924 Connection::Interrupt() {
1925 MOZ_ASSERT(mInterruptible
, "Interrupt method not allowed");
1926 MOZ_ASSERT_IF(SYNCHRONOUS
== mSupportedOperations
,
1927 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn
));
1928 MOZ_ASSERT_IF(ASYNCHRONOUS
== mSupportedOperations
,
1929 IsOnCurrentSerialEventTarget(eventTargetOpenedOn
));
1931 if (!connectionReady()) {
1932 return NS_ERROR_NOT_INITIALIZED
;
1935 if (isClosing()) { // Closing already in asynchronous case
1940 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
1941 // it is not safe to call sqlite3_interrupt() when
1942 // database connection is closed or might close before
1943 // sqlite3_interrupt() returns.
1944 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
1945 if (!isClosed(lockedScope
)) {
1946 MOZ_ASSERT(mDBConn
);
1947 ::sqlite3_interrupt(mDBConn
);
1955 Connection::AsyncVacuum(mozIStorageCompletionCallback
* aCallback
,
1956 bool aUseIncremental
, int32_t aSetPageSize
) {
1957 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD
);
1958 // Abort if we're shutting down.
1959 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed
)) {
1960 return NS_ERROR_ABORT
;
1962 // Check if AsyncClose or Close were already invoked.
1963 if (!connectionReady()) {
1964 return NS_ERROR_NOT_INITIALIZED
;
1966 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
1967 if (NS_FAILED(rv
)) {
1970 nsIEventTarget
* asyncThread
= getAsyncExecutionTarget();
1972 return NS_ERROR_NOT_INITIALIZED
;
1975 // Create and dispatch our vacuum event to the background thread.
1976 nsCOMPtr
<nsIRunnable
> vacuumEvent
=
1977 new AsyncVacuumEvent(this, aCallback
, aUseIncremental
, aSetPageSize
);
1978 rv
= asyncThread
->Dispatch(vacuumEvent
, NS_DISPATCH_NORMAL
);
1979 NS_ENSURE_SUCCESS(rv
, rv
);
1985 Connection::GetDefaultPageSize(int32_t* _defaultPageSize
) {
1986 *_defaultPageSize
= Service::kDefaultPageSize
;
1991 Connection::GetConnectionReady(bool* _ready
) {
1992 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn
));
1993 *_ready
= connectionReady();
1998 Connection::GetDatabaseFile(nsIFile
** _dbFile
) {
1999 if (!connectionReady()) {
2000 return NS_ERROR_NOT_INITIALIZED
;
2002 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
2003 if (NS_FAILED(rv
)) {
2007 NS_IF_ADDREF(*_dbFile
= mDatabaseFile
);
2013 Connection::GetLastInsertRowID(int64_t* _id
) {
2014 if (!connectionReady()) {
2015 return NS_ERROR_NOT_INITIALIZED
;
2017 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2018 if (NS_FAILED(rv
)) {
2022 sqlite_int64 id
= ::sqlite3_last_insert_rowid(mDBConn
);
2029 Connection::GetAffectedRows(int32_t* _rows
) {
2030 if (!connectionReady()) {
2031 return NS_ERROR_NOT_INITIALIZED
;
2033 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2034 if (NS_FAILED(rv
)) {
2038 *_rows
= ::sqlite3_changes(mDBConn
);
2044 Connection::GetLastError(int32_t* _error
) {
2045 if (!connectionReady()) {
2046 return NS_ERROR_NOT_INITIALIZED
;
2048 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2049 if (NS_FAILED(rv
)) {
2053 *_error
= ::sqlite3_errcode(mDBConn
);
2059 Connection::GetLastErrorString(nsACString
& _errorString
) {
2060 if (!connectionReady()) {
2061 return NS_ERROR_NOT_INITIALIZED
;
2063 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2064 if (NS_FAILED(rv
)) {
2068 const char* serr
= ::sqlite3_errmsg(mDBConn
);
2069 _errorString
.Assign(serr
);
2075 Connection::GetSchemaVersion(int32_t* _version
) {
2076 if (!connectionReady()) {
2077 return NS_ERROR_NOT_INITIALIZED
;
2079 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2080 if (NS_FAILED(rv
)) {
2084 nsCOMPtr
<mozIStorageStatement
> stmt
;
2085 (void)CreateStatement("PRAGMA user_version"_ns
, getter_AddRefs(stmt
));
2086 NS_ENSURE_TRUE(stmt
, NS_ERROR_OUT_OF_MEMORY
);
2090 if (NS_SUCCEEDED(stmt
->ExecuteStep(&hasResult
)) && hasResult
) {
2091 *_version
= stmt
->AsInt32(0);
2098 Connection::SetSchemaVersion(int32_t aVersion
) {
2099 if (!connectionReady()) {
2100 return NS_ERROR_NOT_INITIALIZED
;
2102 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2103 if (NS_FAILED(rv
)) {
2107 nsAutoCString
stmt("PRAGMA user_version = "_ns
);
2108 stmt
.AppendInt(aVersion
);
2110 return ExecuteSimpleSQL(stmt
);
2114 Connection::CreateStatement(const nsACString
& aSQLStatement
,
2115 mozIStorageStatement
** _stmt
) {
2116 NS_ENSURE_ARG_POINTER(_stmt
);
2117 if (!connectionReady()) {
2118 return NS_ERROR_NOT_INITIALIZED
;
2120 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2121 if (NS_FAILED(rv
)) {
2125 RefPtr
<Statement
> statement(new Statement());
2126 NS_ENSURE_TRUE(statement
, NS_ERROR_OUT_OF_MEMORY
);
2128 rv
= statement
->initialize(this, mDBConn
, aSQLStatement
);
2129 NS_ENSURE_SUCCESS(rv
, rv
);
2132 statement
.forget(&rawPtr
);
2138 Connection::CreateAsyncStatement(const nsACString
& aSQLStatement
,
2139 mozIStorageAsyncStatement
** _stmt
) {
2140 NS_ENSURE_ARG_POINTER(_stmt
);
2141 if (!connectionReady()) {
2142 return NS_ERROR_NOT_INITIALIZED
;
2144 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
2145 if (NS_FAILED(rv
)) {
2149 RefPtr
<AsyncStatement
> statement(new AsyncStatement());
2150 NS_ENSURE_TRUE(statement
, NS_ERROR_OUT_OF_MEMORY
);
2152 rv
= statement
->initialize(this, mDBConn
, aSQLStatement
);
2153 NS_ENSURE_SUCCESS(rv
, rv
);
2155 AsyncStatement
* rawPtr
;
2156 statement
.forget(&rawPtr
);
2162 Connection::ExecuteSimpleSQL(const nsACString
& aSQLStatement
) {
2163 CHECK_MAINTHREAD_ABUSE();
2164 if (!connectionReady()) {
2165 return NS_ERROR_NOT_INITIALIZED
;
2167 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2168 if (NS_FAILED(rv
)) {
2172 int srv
= executeSql(mDBConn
, PromiseFlatCString(aSQLStatement
).get());
2173 return convertResultCode(srv
);
2177 Connection::ExecuteAsync(
2178 const nsTArray
<RefPtr
<mozIStorageBaseStatement
>>& aStatements
,
2179 mozIStorageStatementCallback
* aCallback
,
2180 mozIStoragePendingStatement
** _handle
) {
2181 nsTArray
<StatementData
> stmts(aStatements
.Length());
2182 for (uint32_t i
= 0; i
< aStatements
.Length(); i
++) {
2183 nsCOMPtr
<StorageBaseStatementInternal
> stmt
=
2184 do_QueryInterface(aStatements
[i
]);
2185 NS_ENSURE_STATE(stmt
);
2187 // Obtain our StatementData.
2189 nsresult rv
= stmt
->getAsynchronousStatementData(data
);
2190 NS_ENSURE_SUCCESS(rv
, rv
);
2192 NS_ASSERTION(stmt
->getOwner() == this,
2193 "Statement must be from this database connection!");
2195 // Now append it to our array.
2196 stmts
.AppendElement(data
);
2199 // Dispatch to the background
2200 return AsyncExecuteStatements::execute(std::move(stmts
), this, mDBConn
,
2201 aCallback
, _handle
);
2205 Connection::ExecuteSimpleSQLAsync(const nsACString
& aSQLStatement
,
2206 mozIStorageStatementCallback
* aCallback
,
2207 mozIStoragePendingStatement
** _handle
) {
2208 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD
);
2210 nsCOMPtr
<mozIStorageAsyncStatement
> stmt
;
2211 nsresult rv
= CreateAsyncStatement(aSQLStatement
, getter_AddRefs(stmt
));
2212 if (NS_FAILED(rv
)) {
2216 nsCOMPtr
<mozIStoragePendingStatement
> pendingStatement
;
2217 rv
= stmt
->ExecuteAsync(aCallback
, getter_AddRefs(pendingStatement
));
2218 if (NS_FAILED(rv
)) {
2222 pendingStatement
.forget(_handle
);
2227 Connection::TableExists(const nsACString
& aTableName
, bool* _exists
) {
2228 return databaseElementExists(TABLE
, aTableName
, _exists
);
2232 Connection::IndexExists(const nsACString
& aIndexName
, bool* _exists
) {
2233 return databaseElementExists(INDEX
, aIndexName
, _exists
);
2237 Connection::GetTransactionInProgress(bool* _inProgress
) {
2238 if (!connectionReady()) {
2239 return NS_ERROR_NOT_INITIALIZED
;
2241 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
2242 if (NS_FAILED(rv
)) {
2246 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
2247 *_inProgress
= transactionInProgress(lockedScope
);
2252 Connection::GetDefaultTransactionType(int32_t* _type
) {
2253 *_type
= mDefaultTransactionType
;
2258 Connection::SetDefaultTransactionType(int32_t aType
) {
2259 NS_ENSURE_ARG_RANGE(aType
, TRANSACTION_DEFERRED
, TRANSACTION_EXCLUSIVE
);
2260 mDefaultTransactionType
= aType
;
2265 Connection::GetVariableLimit(int32_t* _limit
) {
2266 if (!connectionReady()) {
2267 return NS_ERROR_NOT_INITIALIZED
;
2269 int limit
= ::sqlite3_limit(mDBConn
, SQLITE_LIMIT_VARIABLE_NUMBER
, -1);
2271 return NS_ERROR_UNEXPECTED
;
2278 Connection::BeginTransaction() {
2279 if (!connectionReady()) {
2280 return NS_ERROR_NOT_INITIALIZED
;
2282 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2283 if (NS_FAILED(rv
)) {
2287 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
2288 return beginTransactionInternal(lockedScope
, mDBConn
,
2289 mDefaultTransactionType
);
2292 nsresult
Connection::beginTransactionInternal(
2293 const SQLiteMutexAutoLock
& aProofOfLock
, sqlite3
* aNativeConnection
,
2294 int32_t aTransactionType
) {
2295 if (transactionInProgress(aProofOfLock
)) {
2296 return NS_ERROR_FAILURE
;
2299 switch (aTransactionType
) {
2300 case TRANSACTION_DEFERRED
:
2301 rv
= convertResultCode(executeSql(aNativeConnection
, "BEGIN DEFERRED"));
2303 case TRANSACTION_IMMEDIATE
:
2304 rv
= convertResultCode(executeSql(aNativeConnection
, "BEGIN IMMEDIATE"));
2306 case TRANSACTION_EXCLUSIVE
:
2307 rv
= convertResultCode(executeSql(aNativeConnection
, "BEGIN EXCLUSIVE"));
2310 return NS_ERROR_ILLEGAL_VALUE
;
2316 Connection::CommitTransaction() {
2317 if (!connectionReady()) {
2318 return NS_ERROR_NOT_INITIALIZED
;
2320 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2321 if (NS_FAILED(rv
)) {
2325 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
2326 return commitTransactionInternal(lockedScope
, mDBConn
);
2329 nsresult
Connection::commitTransactionInternal(
2330 const SQLiteMutexAutoLock
& aProofOfLock
, sqlite3
* aNativeConnection
) {
2331 if (!transactionInProgress(aProofOfLock
)) {
2332 return NS_ERROR_UNEXPECTED
;
2335 convertResultCode(executeSql(aNativeConnection
, "COMMIT TRANSACTION"));
2340 Connection::RollbackTransaction() {
2341 if (!connectionReady()) {
2342 return NS_ERROR_NOT_INITIALIZED
;
2344 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2345 if (NS_FAILED(rv
)) {
2349 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
2350 return rollbackTransactionInternal(lockedScope
, mDBConn
);
2353 nsresult
Connection::rollbackTransactionInternal(
2354 const SQLiteMutexAutoLock
& aProofOfLock
, sqlite3
* aNativeConnection
) {
2355 if (!transactionInProgress(aProofOfLock
)) {
2356 return NS_ERROR_UNEXPECTED
;
2360 convertResultCode(executeSql(aNativeConnection
, "ROLLBACK TRANSACTION"));
2365 Connection::CreateTable(const char* aTableName
, const char* aTableSchema
) {
2366 if (!connectionReady()) {
2367 return NS_ERROR_NOT_INITIALIZED
;
2369 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2370 if (NS_FAILED(rv
)) {
2374 SmprintfPointer buf
=
2375 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName
, aTableSchema
);
2376 if (!buf
) return NS_ERROR_OUT_OF_MEMORY
;
2378 int srv
= executeSql(mDBConn
, buf
.get());
2380 return convertResultCode(srv
);
2384 Connection::CreateFunction(const nsACString
& aFunctionName
,
2385 int32_t aNumArguments
,
2386 mozIStorageFunction
* aFunction
) {
2387 if (!connectionReady()) {
2388 return NS_ERROR_NOT_INITIALIZED
;
2390 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
2391 if (NS_FAILED(rv
)) {
2395 // Check to see if this function is already defined. We only check the name
2396 // because a function can be defined with the same body but different names.
2397 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
2398 NS_ENSURE_FALSE(mFunctions
.Contains(aFunctionName
), NS_ERROR_FAILURE
);
2400 int srv
= ::sqlite3_create_function(
2401 mDBConn
, nsPromiseFlatCString(aFunctionName
).get(), aNumArguments
,
2402 SQLITE_ANY
, aFunction
, basicFunctionHelper
, nullptr, nullptr);
2403 if (srv
!= SQLITE_OK
) return convertResultCode(srv
);
2405 FunctionInfo info
= {aFunction
, aNumArguments
};
2406 mFunctions
.InsertOrUpdate(aFunctionName
, info
);
2412 Connection::RemoveFunction(const nsACString
& aFunctionName
) {
2413 if (!connectionReady()) {
2414 return NS_ERROR_NOT_INITIALIZED
;
2416 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
2417 if (NS_FAILED(rv
)) {
2421 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
2422 NS_ENSURE_TRUE(mFunctions
.Get(aFunctionName
, nullptr), NS_ERROR_FAILURE
);
2424 int srv
= ::sqlite3_create_function(
2425 mDBConn
, nsPromiseFlatCString(aFunctionName
).get(), 0, SQLITE_ANY
,
2426 nullptr, nullptr, nullptr, nullptr);
2427 if (srv
!= SQLITE_OK
) return convertResultCode(srv
);
2429 mFunctions
.Remove(aFunctionName
);
2435 Connection::SetProgressHandler(int32_t aGranularity
,
2436 mozIStorageProgressHandler
* aHandler
,
2437 mozIStorageProgressHandler
** _oldHandler
) {
2438 if (!connectionReady()) {
2439 return NS_ERROR_NOT_INITIALIZED
;
2441 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
2442 if (NS_FAILED(rv
)) {
2446 // Return previous one
2447 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
2448 NS_IF_ADDREF(*_oldHandler
= mProgressHandler
);
2450 if (!aHandler
|| aGranularity
<= 0) {
2454 mProgressHandler
= aHandler
;
2455 ::sqlite3_progress_handler(mDBConn
, aGranularity
, sProgressHelper
, this);
2461 Connection::RemoveProgressHandler(mozIStorageProgressHandler
** _oldHandler
) {
2462 if (!connectionReady()) {
2463 return NS_ERROR_NOT_INITIALIZED
;
2465 nsresult rv
= ensureOperationSupported(ASYNCHRONOUS
);
2466 if (NS_FAILED(rv
)) {
2470 // Return previous one
2471 SQLiteMutexAutoLock
lockedScope(sharedDBMutex
);
2472 NS_IF_ADDREF(*_oldHandler
= mProgressHandler
);
2474 mProgressHandler
= nullptr;
2475 ::sqlite3_progress_handler(mDBConn
, 0, nullptr, nullptr);
2481 Connection::SetGrowthIncrement(int32_t aChunkSize
,
2482 const nsACString
& aDatabaseName
) {
2483 if (!connectionReady()) {
2484 return NS_ERROR_NOT_INITIALIZED
;
2486 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2487 if (NS_FAILED(rv
)) {
2491 // Bug 597215: Disk space is extremely limited on Android
2492 // so don't preallocate space. This is also not effective
2493 // on log structured file systems used by Android devices
2494 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2495 // Don't preallocate if less than 500MiB is available.
2496 int64_t bytesAvailable
;
2497 rv
= mDatabaseFile
->GetDiskSpaceAvailable(&bytesAvailable
);
2498 NS_ENSURE_SUCCESS(rv
, rv
);
2499 if (bytesAvailable
< MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH
) {
2500 return NS_ERROR_FILE_TOO_BIG
;
2503 int srv
= ::sqlite3_file_control(
2505 aDatabaseName
.Length() ? nsPromiseFlatCString(aDatabaseName
).get()
2507 SQLITE_FCNTL_CHUNK_SIZE
, &aChunkSize
);
2508 if (srv
== SQLITE_OK
) {
2509 mGrowthChunkSize
= aChunkSize
;
2515 int32_t Connection::RemovablePagesInFreeList(const nsACString
& aSchemaName
) {
2516 int32_t freeListPagesCount
= 0;
2517 if (!isConnectionReadyOnThisThread()) {
2518 MOZ_ASSERT(false, "Database connection is not ready");
2519 return freeListPagesCount
;
2522 nsAutoCString
query(MOZ_STORAGE_UNIQUIFY_QUERY_STR
"PRAGMA ");
2523 query
.Append(aSchemaName
);
2524 query
.AppendLiteral(".freelist_count");
2525 nsCOMPtr
<mozIStorageStatement
> stmt
;
2526 DebugOnly
<nsresult
> rv
= CreateStatement(query
, getter_AddRefs(stmt
));
2527 MOZ_ASSERT(NS_SUCCEEDED(rv
));
2528 bool hasResult
= false;
2529 if (stmt
&& NS_SUCCEEDED(stmt
->ExecuteStep(&hasResult
)) && hasResult
) {
2530 freeListPagesCount
= stmt
->AsInt32(0);
2533 // If there's no chunk size set, any page is good to be removed.
2534 if (mGrowthChunkSize
== 0 || freeListPagesCount
== 0) {
2535 return freeListPagesCount
;
2539 nsAutoCString
query(MOZ_STORAGE_UNIQUIFY_QUERY_STR
"PRAGMA ");
2540 query
.Append(aSchemaName
);
2541 query
.AppendLiteral(".page_size");
2542 nsCOMPtr
<mozIStorageStatement
> stmt
;
2543 DebugOnly
<nsresult
> rv
= CreateStatement(query
, getter_AddRefs(stmt
));
2544 MOZ_ASSERT(NS_SUCCEEDED(rv
));
2545 bool hasResult
= false;
2546 if (stmt
&& NS_SUCCEEDED(stmt
->ExecuteStep(&hasResult
)) && hasResult
) {
2547 pageSize
= stmt
->AsInt32(0);
2549 MOZ_ASSERT(false, "Couldn't get page_size");
2553 return std::max(0, freeListPagesCount
- (mGrowthChunkSize
/ pageSize
));
2557 Connection::LoadExtension(const nsACString
& aExtensionName
,
2558 mozIStorageCompletionCallback
* aCallback
) {
2559 AUTO_PROFILER_LABEL("Connection::LoadExtension", OTHER
);
2561 // This is a static list of extensions we can load.
2562 // Please use lowercase ASCII names and keep this list alphabetically ordered.
2563 static constexpr nsLiteralCString sSupportedExtensions
[] = {
2568 if (std::find(std::begin(sSupportedExtensions
),
2569 std::end(sSupportedExtensions
),
2570 aExtensionName
) == std::end(sSupportedExtensions
)) {
2571 return NS_ERROR_INVALID_ARG
;
2574 if (!connectionReady()) {
2575 return NS_ERROR_NOT_INITIALIZED
;
2578 int srv
= ::sqlite3_db_config(mDBConn
, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION
,
2580 if (srv
!= SQLITE_OK
) {
2581 return NS_ERROR_UNEXPECTED
;
2584 // Track the loaded extension for later connection cloning operations.
2586 MutexAutoLock
lockedScope(sharedAsyncExecutionMutex
);
2587 if (!mLoadedExtensions
.EnsureInserted(aExtensionName
)) {
2588 // Already loaded, bail out but issue a warning.
2589 NS_WARNING(nsPrintfCString(
2590 "Tried to register '%s' SQLite extension multiple times!",
2591 PromiseFlatCString(aExtensionName
).get())
2597 nsAutoCString
entryPoint("sqlite3_");
2598 entryPoint
.Append(aExtensionName
);
2599 entryPoint
.AppendLiteral("_init");
2601 RefPtr
<Runnable
> loadTask
= NS_NewRunnableFunction(
2602 "mozStorageConnection::LoadExtension",
2603 [this, self
= RefPtr(this), entryPoint
,
2604 callback
= RefPtr(aCallback
)]() mutable {
2606 !NS_IsMainThread() ||
2607 (operationSupported(Connection::SYNCHRONOUS
) &&
2608 eventTargetOpenedOn
== GetMainThreadSerialEventTarget()),
2609 "Should happen on main-thread only for synchronous connections "
2610 "opened on the main thread");
2611 #ifdef MOZ_FOLD_LIBS
2612 int srv
= ::sqlite3_load_extension(mDBConn
,
2613 MOZ_DLL_PREFIX
"nss3" MOZ_DLL_SUFFIX
,
2614 entryPoint
.get(), nullptr);
2616 int srv
= ::sqlite3_load_extension(
2617 mDBConn
, MOZ_DLL_PREFIX
"mozsqlite3" MOZ_DLL_SUFFIX
,
2618 entryPoint
.get(), nullptr);
2623 RefPtr
<Runnable
> callbackTask
= NS_NewRunnableFunction(
2624 "mozStorageConnection::LoadExtension_callback",
2625 [callback
= std::move(callback
), srv
]() {
2626 (void)callback
->Complete(convertResultCode(srv
), nullptr);
2628 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn
)) {
2629 MOZ_ALWAYS_SUCCEEDS(callbackTask
->Run());
2631 // Redispatch the callback to the calling thread.
2632 MOZ_ALWAYS_SUCCEEDS(eventTargetOpenedOn
->Dispatch(
2633 callbackTask
.forget(), NS_DISPATCH_NORMAL
));
2637 if (NS_IsMainThread() && !operationSupported(Connection::SYNCHRONOUS
)) {
2638 // This is a main-thread call to an async-only connection, thus we should
2639 // load the library in the helper thread.
2640 nsIEventTarget
* helperThread
= getAsyncExecutionTarget();
2641 if (!helperThread
) {
2642 return NS_ERROR_NOT_INITIALIZED
;
2644 MOZ_ALWAYS_SUCCEEDS(
2645 helperThread
->Dispatch(loadTask
.forget(), NS_DISPATCH_NORMAL
));
2647 // In any other case we just load the extension on the current thread.
2648 MOZ_ALWAYS_SUCCEEDS(loadTask
->Run());
2654 Connection::EnableModule(const nsACString
& aModuleName
) {
2655 if (!connectionReady()) {
2656 return NS_ERROR_NOT_INITIALIZED
;
2658 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2659 if (NS_FAILED(rv
)) {
2663 for (auto& gModule
: gModules
) {
2664 struct Module
* m
= &gModule
;
2665 if (aModuleName
.Equals(m
->name
)) {
2666 int srv
= m
->registerFunc(mDBConn
, m
->name
);
2667 if (srv
!= SQLITE_OK
) return convertResultCode(srv
);
2673 return NS_ERROR_FAILURE
;
2677 Connection::GetQuotaObjects(QuotaObject
** aDatabaseQuotaObject
,
2678 QuotaObject
** aJournalQuotaObject
) {
2679 MOZ_ASSERT(aDatabaseQuotaObject
);
2680 MOZ_ASSERT(aJournalQuotaObject
);
2682 if (!connectionReady()) {
2683 return NS_ERROR_NOT_INITIALIZED
;
2685 nsresult rv
= ensureOperationSupported(SYNCHRONOUS
);
2686 if (NS_FAILED(rv
)) {
2691 int srv
= ::sqlite3_file_control(mDBConn
, nullptr, SQLITE_FCNTL_FILE_POINTER
,
2693 if (srv
!= SQLITE_OK
) {
2694 return convertResultCode(srv
);
2699 ::sqlite3_file_control(mDBConn
, nullptr, SQLITE_FCNTL_VFS_POINTER
, &vfs
);
2700 if (srv
!= SQLITE_OK
) {
2701 return convertResultCode(srv
);
2704 bool obfusactingVFS
= false;
2707 const nsDependentCString vfsName
{vfs
->zName
};
2709 if (vfsName
== obfsvfs::GetVFSName()) {
2710 obfusactingVFS
= true;
2711 } else if (vfsName
!= quotavfs::GetVFSName()) {
2712 NS_WARNING("Got unexpected vfs");
2713 return NS_ERROR_FAILURE
;
2717 RefPtr
<QuotaObject
> databaseQuotaObject
=
2718 GetQuotaObject(file
, obfusactingVFS
);
2719 if (NS_WARN_IF(!databaseQuotaObject
)) {
2720 return NS_ERROR_FAILURE
;
2723 srv
= ::sqlite3_file_control(mDBConn
, nullptr, SQLITE_FCNTL_JOURNAL_POINTER
,
2725 if (srv
!= SQLITE_OK
) {
2726 return convertResultCode(srv
);
2729 RefPtr
<QuotaObject
> journalQuotaObject
= GetQuotaObject(file
, obfusactingVFS
);
2730 if (NS_WARN_IF(!journalQuotaObject
)) {
2731 return NS_ERROR_FAILURE
;
2734 databaseQuotaObject
.forget(aDatabaseQuotaObject
);
2735 journalQuotaObject
.forget(aJournalQuotaObject
);
2739 SQLiteMutex
& Connection::GetSharedDBMutex() { return sharedDBMutex
; }
2741 uint32_t Connection::GetTransactionNestingLevel(
2742 const mozilla::storage::SQLiteMutexAutoLock
& aProofOfLock
) {
2743 return mTransactionNestingLevel
;
2746 uint32_t Connection::IncreaseTransactionNestingLevel(
2747 const mozilla::storage::SQLiteMutexAutoLock
& aProofOfLock
) {
2748 return ++mTransactionNestingLevel
;
2751 uint32_t Connection::DecreaseTransactionNestingLevel(
2752 const mozilla::storage::SQLiteMutexAutoLock
& aProofOfLock
) {
2753 return --mTransactionNestingLevel
;
2756 } // namespace mozilla::storage