Bug 1775698 - Disable browser_dynamical_window_rounding.js also on linux debug due...
[gecko.git] / storage / mozStorageConnection.cpp
blob0398124f6ad233acef7625aac81c4f1162ff7d46
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 "nsError.h"
8 #include "nsThreadUtils.h"
9 #include "nsIFile.h"
10 #include "nsIFileURL.h"
11 #include "nsIXPConnect.h"
12 #include "mozilla/AppShutdown.h"
13 #include "mozilla/CheckedInt.h"
14 #include "mozilla/Telemetry.h"
15 #include "mozilla/Mutex.h"
16 #include "mozilla/CondVar.h"
17 #include "mozilla/Attributes.h"
18 #include "mozilla/ErrorNames.h"
19 #include "mozilla/Unused.h"
20 #include "mozilla/dom/quota/QuotaObject.h"
21 #include "mozilla/ScopeExit.h"
22 #include "mozilla/SpinEventLoopUntil.h"
23 #include "mozilla/StaticPrefs_storage.h"
25 #include "mozIStorageCompletionCallback.h"
26 #include "mozIStorageFunction.h"
28 #include "mozStorageAsyncStatementExecution.h"
29 #include "mozStorageSQLFunctions.h"
30 #include "mozStorageConnection.h"
31 #include "mozStorageService.h"
32 #include "mozStorageStatement.h"
33 #include "mozStorageAsyncStatement.h"
34 #include "mozStorageArgValueArray.h"
35 #include "mozStoragePrivateHelpers.h"
36 #include "mozStorageStatementData.h"
37 #include "StorageBaseStatementInternal.h"
38 #include "SQLCollations.h"
39 #include "FileSystemModule.h"
40 #include "mozStorageHelper.h"
42 #include "mozilla/Assertions.h"
43 #include "mozilla/Logging.h"
44 #include "mozilla/Printf.h"
45 #include "mozilla/ProfilerLabels.h"
46 #include "mozilla/RefPtr.h"
47 #include "nsProxyRelease.h"
48 #include "nsStringFwd.h"
49 #include "nsURLHelper.h"
51 #define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
53 // Maximum size of the pages cache per connection.
54 #define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
56 mozilla::LazyLogModule gStorageLog("mozStorage");
58 // Checks that the protected code is running on the main-thread only if the
59 // connection was also opened on it.
60 #ifdef DEBUG
61 # define CHECK_MAINTHREAD_ABUSE() \
62 do { \
63 NS_WARNING_ASSERTION( \
64 eventTargetOpenedOn == GetMainThreadSerialEventTarget() || \
65 !NS_IsMainThread(), \
66 "Using Storage synchronous API on main-thread, but " \
67 "the connection was opened on another thread."); \
68 } while (0)
69 #else
70 # define CHECK_MAINTHREAD_ABUSE() \
71 do { /* Nothing */ \
72 } while (0)
73 #endif
75 namespace mozilla::storage {
77 using mozilla::dom::quota::QuotaObject;
78 using mozilla::Telemetry::AccumulateCategoricalKeyed;
79 using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN;
80 using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY;
82 const char* GetBaseVFSName(bool);
83 const char* GetQuotaVFSName();
84 const char* GetObfuscatingVFSName();
86 namespace {
88 int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
89 if (NS_SUCCEEDED(aXPCOMResultCode)) {
90 return SQLITE_OK;
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:
99 return SQLITE_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:
105 return SQLITE_IOERR;
106 case NS_ERROR_FILE_NO_DEVICE_SPACE:
107 return SQLITE_FULL;
108 case NS_ERROR_OUT_OF_MEMORY:
109 return SQLITE_NOMEM;
110 case NS_ERROR_UNEXPECTED:
111 return SQLITE_MISUSE;
112 case NS_ERROR_ABORT:
113 return SQLITE_ABORT;
114 case NS_ERROR_STORAGE_CONSTRAINT:
115 return SQLITE_CONSTRAINT;
116 default:
117 return SQLITE_ERROR;
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);
128 return SQLITE_OK;
131 int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
132 ::sqlite3_result_int64(aCtx, aValue);
133 return SQLITE_OK;
136 int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
137 ::sqlite3_result_double(aCtx, aValue);
138 return SQLITE_OK;
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);
147 return SQLITE_OK;
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(),
157 SQLITE_TRANSIENT);
158 return SQLITE_OK;
161 int sqlite3_T_null(sqlite3_context* aCtx) {
162 ::sqlite3_result_null(aCtx);
163 return SQLITE_OK;
166 int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
167 ::sqlite3_result_blob(aCtx, aData, aSize, free);
168 return SQLITE_OK;
171 #include "variantToSQLiteT_impl.h"
173 ////////////////////////////////////////////////////////////////////////////////
174 //// Modules
176 struct Module {
177 const char* name;
178 int (*registerFunc)(sqlite3*, const char*);
181 Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
183 ////////////////////////////////////////////////////////////////////////////////
184 //// Local Functions
186 int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
187 switch (aReason) {
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));
198 } else {
199 char* sql = ::sqlite3_expanded_sql(stmt);
200 MOZ_LOG(gStorageLog, LogLevel::Debug,
201 ("TRACE_STMT on %p: '%s'", aClosure, sql));
202 ::sqlite3_free(sql);
204 break;
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;
210 if (time > 0) {
211 MOZ_LOG(gStorageLog, LogLevel::Debug,
212 ("TRACE_TIME on %p: %lldms", aClosure, time));
214 break;
217 return 0;
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));
231 if (NS_FAILED(rv)) {
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));
241 return;
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",
247 -1);
252 * This code is heavily based on the sample at:
253 * http://www.sqlite.org/unlock_notify.html
255 class UnlockNotification {
256 public:
257 UnlockNotification()
258 : mMutex("UnlockNotification mMutex"),
259 mCondVar(mMutex, "UnlockNotification condVar"),
260 mSignaled(false) {}
262 void Wait() {
263 MutexAutoLock lock(mMutex);
264 while (!mSignaled) {
265 (void)mCondVar.Wait();
269 void Signal() {
270 MutexAutoLock lock(mMutex);
271 mSignaled = true;
272 (void)mCondVar.Notify();
275 private:
276 Mutex mMutex MOZ_UNANNOTATED;
277 CondVar mCondVar;
278 bool mSignaled;
281 void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
282 for (int i = 0; i < aArgsSize; i++) {
283 UnlockNotification* notification =
284 static_cast<UnlockNotification*>(aArgs[i]);
285 notification->Signal();
289 int WaitForUnlockNotify(sqlite3* aDatabase) {
290 UnlockNotification notification;
291 int srv =
292 ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, &notification);
293 MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
294 if (srv == SQLITE_OK) {
295 notification.Wait();
298 return srv;
301 ////////////////////////////////////////////////////////////////////////////////
302 //// Local Classes
304 class AsyncCloseConnection final : public Runnable {
305 public:
306 AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
307 nsIRunnable* aCallbackEvent)
308 : Runnable("storage::AsyncCloseConnection"),
309 mConnection(aConnection),
310 mNativeConnection(aNativeConnection),
311 mCallbackEvent(aCallbackEvent) {}
313 NS_IMETHOD Run() override {
314 // Make sure we don't dispatch to the current thread.
315 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn));
317 nsCOMPtr<nsIRunnable> event =
318 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
319 mConnection, &Connection::shutdownAsyncThread);
320 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event));
322 // Internal close.
323 (void)mConnection->internalClose(mNativeConnection);
325 // Callback
326 if (mCallbackEvent) {
327 nsCOMPtr<nsIThread> thread;
328 (void)NS_GetMainThread(getter_AddRefs(thread));
329 (void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
332 return NS_OK;
335 ~AsyncCloseConnection() override {
336 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
337 mConnection.forget());
338 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
339 mCallbackEvent.forget());
342 private:
343 RefPtr<Connection> mConnection;
344 sqlite3* mNativeConnection;
345 nsCOMPtr<nsIRunnable> mCallbackEvent;
349 * An event used to initialize the clone of a connection.
351 * Must be executed on the clone's async execution thread.
353 class AsyncInitializeClone final : public Runnable {
354 public:
356 * @param aConnection The connection being cloned.
357 * @param aClone The clone.
358 * @param aReadOnly If |true|, the clone is read only.
359 * @param aCallback A callback to trigger once initialization
360 * is complete. This event will be called on
361 * aClone->eventTargetOpenedOn.
363 AsyncInitializeClone(Connection* aConnection, Connection* aClone,
364 const bool aReadOnly,
365 mozIStorageCompletionCallback* aCallback)
366 : Runnable("storage::AsyncInitializeClone"),
367 mConnection(aConnection),
368 mClone(aClone),
369 mReadOnly(aReadOnly),
370 mCallback(aCallback) {
371 MOZ_ASSERT(NS_IsMainThread());
374 NS_IMETHOD Run() override {
375 MOZ_ASSERT(!NS_IsMainThread());
376 nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
377 if (NS_FAILED(rv)) {
378 return Dispatch(rv, nullptr);
380 return Dispatch(NS_OK,
381 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
384 private:
385 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
386 RefPtr<CallbackComplete> event =
387 new CallbackComplete(aResult, aValue, mCallback.forget());
388 return mClone->eventTargetOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
391 ~AsyncInitializeClone() override {
392 nsCOMPtr<nsIThread> thread;
393 DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
394 MOZ_ASSERT(NS_SUCCEEDED(rv));
396 // Handle ambiguous nsISupports inheritance.
397 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
398 mConnection.forget());
399 NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
401 // Generally, the callback will be released by CallbackComplete.
402 // However, if for some reason Run() is not executed, we still
403 // need to ensure that it is released here.
404 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
405 mCallback.forget());
408 RefPtr<Connection> mConnection;
409 RefPtr<Connection> mClone;
410 const bool mReadOnly;
411 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
415 * A listener for async connection closing.
417 class CloseListener final : public mozIStorageCompletionCallback {
418 public:
419 NS_DECL_ISUPPORTS
420 CloseListener() : mClosed(false) {}
422 NS_IMETHOD Complete(nsresult, nsISupports*) override {
423 mClosed = true;
424 return NS_OK;
427 bool mClosed;
429 private:
430 ~CloseListener() = default;
433 NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)
435 class AsyncVacuumEvent final : public Runnable {
436 public:
437 AsyncVacuumEvent(Connection* aConnection,
438 mozIStorageCompletionCallback* aCallback,
439 bool aUseIncremental, int32_t aSetPageSize)
440 : Runnable("storage::AsyncVacuum"),
441 mConnection(aConnection),
442 mCallback(aCallback),
443 mUseIncremental(aUseIncremental),
444 mSetPageSize(aSetPageSize),
445 mStatus(NS_ERROR_UNEXPECTED) {}
447 NS_IMETHOD Run() override {
448 // This is initially dispatched to the helper thread, then re-dispatched
449 // to the opener thread, where it will callback.
450 if (IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn)) {
451 // Send the completion event.
452 if (mCallback) {
453 mozilla::Unused << mCallback->Complete(mStatus, nullptr);
455 return NS_OK;
458 // Ensure to invoke the callback regardless of errors.
459 auto guard = MakeScopeExit([&]() {
460 mConnection->mIsStatementOnHelperThreadInterruptible = false;
461 mozilla::Unused << mConnection->eventTargetOpenedOn->Dispatch(
462 this, NS_DISPATCH_NORMAL);
465 // Get list of attached databases.
466 nsCOMPtr<mozIStorageStatement> stmt;
467 nsresult rv = mConnection->CreateStatement(MOZ_STORAGE_UNIQUIFY_QUERY_STR
468 "PRAGMA database_list"_ns,
469 getter_AddRefs(stmt));
470 NS_ENSURE_SUCCESS(rv, rv);
471 // We must accumulate names and loop through them later, otherwise VACUUM
472 // will see an ongoing statement and bail out.
473 nsTArray<nsCString> schemaNames;
474 bool hasResult = false;
475 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
476 nsAutoCString name;
477 rv = stmt->GetUTF8String(1, name);
478 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("temp")) {
479 schemaNames.AppendElement(name);
482 mStatus = NS_OK;
483 // Mark this vacuum as an interruptible operation, so it can be interrupted
484 // if the connection closes during shutdown.
485 mConnection->mIsStatementOnHelperThreadInterruptible = true;
486 for (const nsCString& schemaName : schemaNames) {
487 rv = this->Vacuum(schemaName);
488 if (NS_FAILED(rv)) {
489 // This is sub-optimal since it's only keeping the last error reason,
490 // but it will do for now.
491 mStatus = rv;
494 return mStatus;
497 nsresult Vacuum(const nsACString& aSchemaName) {
498 // Abort if we're in shutdown.
499 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
500 return NS_ERROR_ABORT;
502 int32_t removablePages = mConnection->RemovablePagesInFreeList(aSchemaName);
503 if (!removablePages) {
504 // There's no empty pages to remove, so skip this vacuum for now.
505 return NS_OK;
507 nsresult rv;
508 bool needsFullVacuum = true;
510 if (mSetPageSize) {
511 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
512 query.Append(aSchemaName);
513 query.AppendLiteral(".page_size = ");
514 query.AppendInt(mSetPageSize);
515 nsCOMPtr<mozIStorageStatement> stmt;
516 rv = mConnection->ExecuteSimpleSQL(query);
517 NS_ENSURE_SUCCESS(rv, rv);
520 // Check auto_vacuum.
522 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
523 query.Append(aSchemaName);
524 query.AppendLiteral(".auto_vacuum");
525 nsCOMPtr<mozIStorageStatement> stmt;
526 rv = mConnection->CreateStatement(query, getter_AddRefs(stmt));
527 NS_ENSURE_SUCCESS(rv, rv);
528 bool hasResult = false;
529 bool changeAutoVacuum = false;
530 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
531 bool isIncrementalVacuum = stmt->AsInt32(0) == 2;
532 changeAutoVacuum = isIncrementalVacuum != mUseIncremental;
533 if (isIncrementalVacuum && !changeAutoVacuum) {
534 needsFullVacuum = false;
537 // Changing auto_vacuum is only supported on the main schema.
538 if (aSchemaName.EqualsLiteral("main") && changeAutoVacuum) {
539 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
540 query.Append(aSchemaName);
541 query.AppendLiteral(".auto_vacuum = ");
542 query.AppendInt(mUseIncremental ? 2 : 0);
543 rv = mConnection->ExecuteSimpleSQL(query);
544 NS_ENSURE_SUCCESS(rv, rv);
548 if (needsFullVacuum) {
549 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "VACUUM ");
550 query.Append(aSchemaName);
551 rv = mConnection->ExecuteSimpleSQL(query);
552 // TODO (Bug 1818039): Report failed vacuum telemetry.
553 NS_ENSURE_SUCCESS(rv, rv);
554 } else {
555 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
556 query.Append(aSchemaName);
557 query.AppendLiteral(".incremental_vacuum(");
558 query.AppendInt(removablePages);
559 query.AppendLiteral(")");
560 rv = mConnection->ExecuteSimpleSQL(query);
561 NS_ENSURE_SUCCESS(rv, rv);
564 return NS_OK;
567 ~AsyncVacuumEvent() override {
568 NS_ReleaseOnMainThread("AsyncVacuum::mConnection", mConnection.forget());
569 NS_ReleaseOnMainThread("AsyncVacuum::mCallback", mCallback.forget());
572 private:
573 RefPtr<Connection> mConnection;
574 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
575 bool mUseIncremental;
576 int32_t mSetPageSize;
577 Atomic<nsresult> mStatus;
580 } // namespace
582 ////////////////////////////////////////////////////////////////////////////////
583 //// Connection
585 Connection::Connection(Service* aService, int aFlags,
586 ConnectionOperation aSupportedOperations,
587 const nsCString& aTelemetryFilename, bool aInterruptible,
588 bool aIgnoreLockingMode)
589 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
590 sharedDBMutex("Connection::sharedDBMutex"),
591 eventTargetOpenedOn(WrapNotNull(GetCurrentSerialEventTarget())),
592 mIsStatementOnHelperThreadInterruptible(false),
593 mDBConn(nullptr),
594 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
595 mDestroying(false),
596 mProgressHandler(nullptr),
597 mStorageService(aService),
598 mFlags(aFlags),
599 mTransactionNestingLevel(0),
600 mSupportedOperations(aSupportedOperations),
601 mInterruptible(aSupportedOperations == Connection::ASYNCHRONOUS ||
602 aInterruptible),
603 mIgnoreLockingMode(aIgnoreLockingMode),
604 mAsyncExecutionThreadShuttingDown(false),
605 mConnectionClosed(false),
606 mGrowthChunkSize(0) {
607 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
608 "Can't ignore locking for a non-readonly connection!");
609 mStorageService->registerConnection(this);
610 MOZ_ASSERT(!aTelemetryFilename.IsEmpty(),
611 "A telemetry filename should have been passed-in.");
612 mTelemetryFilename.Assign(aTelemetryFilename);
615 Connection::~Connection() {
616 // Failsafe Close() occurs in our custom Release method because of
617 // complications related to Close() potentially invoking AsyncClose() which
618 // will increment our refcount.
619 MOZ_ASSERT(!mAsyncExecutionThread,
620 "The async thread has not been shutdown properly!");
623 NS_IMPL_ADDREF(Connection)
625 NS_INTERFACE_MAP_BEGIN(Connection)
626 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
627 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
628 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
629 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
630 NS_INTERFACE_MAP_END
632 // This is identical to what NS_IMPL_RELEASE provides, but with the
633 // extra |1 == count| case.
634 NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
635 MOZ_ASSERT(0 != mRefCnt, "dup release");
636 nsrefcnt count = --mRefCnt;
637 NS_LOG_RELEASE(this, count, "Connection");
638 if (1 == count) {
639 // If the refcount went to 1, the single reference must be from
640 // gService->mConnections (in class |Service|). And the code calling
641 // Release is either:
642 // - The "user" code that had created the connection, releasing on any
643 // thread.
644 // - One of Service's getConnections() callers had acquired a strong
645 // reference to the Connection that out-lived the last "user" reference,
646 // and now that just got dropped. Note that this reference could be
647 // getting dropped on the main thread or Connection->eventTargetOpenedOn
648 // (because of the NewRunnableMethod used by minimizeMemory).
650 // Either way, we should now perform our failsafe Close() and unregister.
651 // However, we only want to do this once, and the reality is that our
652 // refcount could go back up above 1 and down again at any time if we are
653 // off the main thread and getConnections() gets called on the main thread,
654 // so we use an atomic here to do this exactly once.
655 if (mDestroying.compareExchange(false, true)) {
656 // Close the connection, dispatching to the opening event target if we're
657 // not on that event target already and that event target is still
658 // accepting runnables. We do this because it's possible we're on the main
659 // thread because of getConnections(), and we REALLY don't want to
660 // transfer I/O to the main thread if we can avoid it.
661 if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
662 // This could cause SpinningSynchronousClose() to be invoked and AddRef
663 // triggered for AsyncCloseConnection's strong ref if the conn was ever
664 // use for async purposes. (Main-thread only, though.)
665 Unused << synchronousClose();
666 } else {
667 nsCOMPtr<nsIRunnable> event =
668 NewRunnableMethod("storage::Connection::synchronousClose", this,
669 &Connection::synchronousClose);
670 if (NS_FAILED(eventTargetOpenedOn->Dispatch(event.forget(),
671 NS_DISPATCH_NORMAL))) {
672 // The event target was dead and so we've just leaked our runnable.
673 // This should not happen because our non-main-thread consumers should
674 // be explicitly closing their connections, not relying on us to close
675 // them for them. (It's okay to let a statement go out of scope for
676 // automatic cleanup, but not a Connection.)
677 MOZ_ASSERT(false,
678 "Leaked Connection::synchronousClose(), ownership fail.");
679 Unused << synchronousClose();
683 // This will drop its strong reference right here, right now.
684 mStorageService->unregisterConnection(this);
686 } else if (0 == count) {
687 mRefCnt = 1; /* stabilize */
688 #if 0 /* enable this to find non-threadsafe destructors: */
689 NS_ASSERT_OWNINGTHREAD(Connection);
690 #endif
691 delete (this);
692 return 0;
694 return count;
697 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
698 int32_t* aMaxValue) {
699 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
700 int curr = 0, max = 0;
701 DebugOnly<int> rc =
702 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
703 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
704 if (aMaxValue) *aMaxValue = max;
705 return curr;
708 nsIEventTarget* Connection::getAsyncExecutionTarget() {
709 NS_ENSURE_TRUE(IsOnCurrentSerialEventTarget(eventTargetOpenedOn), nullptr);
711 // Don't return the asynchronous event target if we are shutting down.
712 if (mAsyncExecutionThreadShuttingDown) {
713 return nullptr;
716 // Create the async event target if there's none yet.
717 if (!mAsyncExecutionThread) {
718 // Names start with "sqldb:" followed by a recognizable name, like the
719 // database file name, or a specially crafted name like "memory".
720 // This name will be surfaced on https://crash-stats.mozilla.org, so any
721 // sensitive part of the file name (e.g. an URL origin) should be replaced
722 // by passing an explicit telemetryName to openDatabaseWithFileURL.
723 nsAutoCString name("sqldb:"_ns);
724 name.Append(mTelemetryFilename);
725 static nsThreadPoolNaming naming;
726 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName(name),
727 getter_AddRefs(mAsyncExecutionThread));
728 if (NS_FAILED(rv)) {
729 NS_WARNING("Failed to create async thread.");
730 return nullptr;
732 mAsyncExecutionThread->SetNameForWakeupTelemetry("mozStorage (all)"_ns);
735 return mAsyncExecutionThread;
738 void Connection::RecordOpenStatus(nsresult rv) {
739 nsCString histogramKey = mTelemetryFilename;
741 if (histogramKey.IsEmpty()) {
742 histogramKey.AssignLiteral("unknown");
745 if (NS_SUCCEEDED(rv)) {
746 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
747 return;
750 switch (rv) {
751 case NS_ERROR_FILE_CORRUPTED:
752 AccumulateCategoricalKeyed(histogramKey,
753 LABELS_SQLITE_STORE_OPEN::corrupt);
754 break;
755 case NS_ERROR_STORAGE_IOERR:
756 AccumulateCategoricalKeyed(histogramKey,
757 LABELS_SQLITE_STORE_OPEN::diskio);
758 break;
759 case NS_ERROR_FILE_ACCESS_DENIED:
760 case NS_ERROR_FILE_IS_LOCKED:
761 case NS_ERROR_FILE_READ_ONLY:
762 AccumulateCategoricalKeyed(histogramKey,
763 LABELS_SQLITE_STORE_OPEN::access);
764 break;
765 case NS_ERROR_FILE_NO_DEVICE_SPACE:
766 AccumulateCategoricalKeyed(histogramKey,
767 LABELS_SQLITE_STORE_OPEN::diskspace);
768 break;
769 default:
770 AccumulateCategoricalKeyed(histogramKey,
771 LABELS_SQLITE_STORE_OPEN::failure);
775 void Connection::RecordQueryStatus(int srv) {
776 nsCString histogramKey = mTelemetryFilename;
778 if (histogramKey.IsEmpty()) {
779 histogramKey.AssignLiteral("unknown");
782 switch (srv) {
783 case SQLITE_OK:
784 case SQLITE_ROW:
785 case SQLITE_DONE:
787 // Note that these are returned when we intentionally cancel a statement so
788 // they aren't indicating a failure.
789 case SQLITE_ABORT:
790 case SQLITE_INTERRUPT:
791 AccumulateCategoricalKeyed(histogramKey,
792 LABELS_SQLITE_STORE_QUERY::success);
793 break;
794 case SQLITE_CORRUPT:
795 case SQLITE_NOTADB:
796 AccumulateCategoricalKeyed(histogramKey,
797 LABELS_SQLITE_STORE_QUERY::corrupt);
798 break;
799 case SQLITE_PERM:
800 case SQLITE_CANTOPEN:
801 case SQLITE_LOCKED:
802 case SQLITE_READONLY:
803 AccumulateCategoricalKeyed(histogramKey,
804 LABELS_SQLITE_STORE_QUERY::access);
805 break;
806 case SQLITE_IOERR:
807 case SQLITE_NOLFS:
808 AccumulateCategoricalKeyed(histogramKey,
809 LABELS_SQLITE_STORE_QUERY::diskio);
810 break;
811 case SQLITE_FULL:
812 case SQLITE_TOOBIG:
813 AccumulateCategoricalKeyed(histogramKey,
814 LABELS_SQLITE_STORE_QUERY::diskspace);
815 break;
816 case SQLITE_CONSTRAINT:
817 case SQLITE_RANGE:
818 case SQLITE_MISMATCH:
819 case SQLITE_MISUSE:
820 AccumulateCategoricalKeyed(histogramKey,
821 LABELS_SQLITE_STORE_QUERY::misuse);
822 break;
823 case SQLITE_BUSY:
824 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_QUERY::busy);
825 break;
826 default:
827 AccumulateCategoricalKeyed(histogramKey,
828 LABELS_SQLITE_STORE_QUERY::failure);
832 nsresult Connection::initialize(const nsACString& aStorageKey,
833 const nsACString& aName) {
834 MOZ_ASSERT(aStorageKey.Equals(kMozStorageMemoryStorageKey));
835 NS_ASSERTION(!connectionReady(),
836 "Initialize called on already opened database!");
837 MOZ_ASSERT(!mIgnoreLockingMode, "Can't ignore locking on an in-memory db.");
838 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
840 mStorageKey = aStorageKey;
841 mName = aName;
843 // in memory database requested, sqlite uses a magic file name
845 const nsAutoCString path =
846 mName.IsEmpty() ? nsAutoCString(":memory:"_ns)
847 : "file:"_ns + mName + "?mode=memory&cache=shared"_ns;
849 int srv =
850 ::sqlite3_open_v2(path.get(), &mDBConn, mFlags, GetBaseVFSName(true));
851 if (srv != SQLITE_OK) {
852 mDBConn = nullptr;
853 nsresult rv = convertResultCode(srv);
854 RecordOpenStatus(rv);
855 return rv;
858 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
859 srv =
860 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
861 MOZ_ASSERT(srv == SQLITE_OK,
862 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
863 #endif
865 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
866 // database.
868 nsresult rv = initializeInternal();
869 RecordOpenStatus(rv);
870 NS_ENSURE_SUCCESS(rv, rv);
872 return NS_OK;
875 nsresult Connection::initialize(nsIFile* aDatabaseFile) {
876 NS_ASSERTION(aDatabaseFile, "Passed null file!");
877 NS_ASSERTION(!connectionReady(),
878 "Initialize called on already opened database!");
879 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
881 // Do not set mFileURL here since this is database does not have an associated
882 // URL.
883 mDatabaseFile = aDatabaseFile;
885 nsAutoString path;
886 nsresult rv = aDatabaseFile->GetPath(path);
887 NS_ENSURE_SUCCESS(rv, rv);
889 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
890 int srv;
891 if (mIgnoreLockingMode) {
892 exclusive = false;
893 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
894 "readonly-immutable-nolock");
895 } else {
896 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
897 GetBaseVFSName(exclusive));
898 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
899 // Retry without trying to get an exclusive lock.
900 exclusive = false;
901 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
902 mFlags, GetBaseVFSName(false));
905 if (srv != SQLITE_OK) {
906 mDBConn = nullptr;
907 rv = convertResultCode(srv);
908 RecordOpenStatus(rv);
909 return rv;
912 rv = initializeInternal();
913 if (exclusive &&
914 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
915 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
916 // some cases it may successfully open the database and then lock on the
917 // first query execution. When initializeInternal fails it closes the
918 // connection, so we can try to restart it in non-exclusive mode.
919 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
920 GetBaseVFSName(false));
921 if (srv == SQLITE_OK) {
922 rv = initializeInternal();
926 RecordOpenStatus(rv);
927 NS_ENSURE_SUCCESS(rv, rv);
929 return NS_OK;
932 nsresult Connection::initialize(nsIFileURL* aFileURL) {
933 NS_ASSERTION(aFileURL, "Passed null file URL!");
934 NS_ASSERTION(!connectionReady(),
935 "Initialize called on already opened database!");
936 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
938 nsCOMPtr<nsIFile> databaseFile;
939 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
940 NS_ENSURE_SUCCESS(rv, rv);
942 // Set both mDatabaseFile and mFileURL here.
943 mFileURL = aFileURL;
944 mDatabaseFile = databaseFile;
946 nsAutoCString spec;
947 rv = aFileURL->GetSpec(spec);
948 NS_ENSURE_SUCCESS(rv, rv);
950 // If there is a key specified, we need to use the obfuscating VFS.
951 nsAutoCString query;
952 rv = aFileURL->GetQuery(query);
953 NS_ENSURE_SUCCESS(rv, rv);
955 bool hasKey = false;
956 bool hasDirectoryLockId = false;
958 MOZ_ALWAYS_TRUE(URLParams::Parse(
959 query, [&hasKey, &hasDirectoryLockId](const nsAString& aName,
960 const nsAString& aValue) {
961 if (aName.EqualsLiteral("key")) {
962 hasKey = true;
963 return true;
965 if (aName.EqualsLiteral("directoryLockId")) {
966 hasDirectoryLockId = true;
967 return true;
969 return true;
970 }));
972 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
974 const char* const vfs = hasKey ? GetObfuscatingVFSName()
975 : hasDirectoryLockId ? GetQuotaVFSName()
976 : GetBaseVFSName(exclusive);
978 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
979 if (srv != SQLITE_OK) {
980 mDBConn = nullptr;
981 rv = convertResultCode(srv);
982 RecordOpenStatus(rv);
983 return rv;
986 rv = initializeInternal();
987 RecordOpenStatus(rv);
988 NS_ENSURE_SUCCESS(rv, rv);
990 return NS_OK;
993 nsresult Connection::initializeInternal() {
994 MOZ_ASSERT(mDBConn);
995 auto guard = MakeScopeExit([&]() { initializeFailed(); });
997 mConnectionClosed = false;
999 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
1000 DebugOnly<int> srv2 =
1001 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
1002 MOZ_ASSERT(srv2 == SQLITE_OK,
1003 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
1004 #endif
1006 // Properly wrap the database handle's mutex.
1007 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
1009 // SQLite tracing can slow down queries (especially long queries)
1010 // significantly. Don't trace unless the user is actively monitoring SQLite.
1011 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1012 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
1013 tracefunc, this);
1015 MOZ_LOG(
1016 gStorageLog, LogLevel::Debug,
1017 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
1020 int64_t pageSize = Service::kDefaultPageSize;
1022 // Set page_size to the preferred default value. This is effective only if
1023 // the database has just been created, otherwise, if the database does not
1024 // use WAL journal mode, a VACUUM operation will updated its page_size.
1025 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1026 "PRAGMA page_size = ");
1027 pageSizeQuery.AppendInt(pageSize);
1028 int srv = executeSql(mDBConn, pageSizeQuery.get());
1029 if (srv != SQLITE_OK) {
1030 return convertResultCode(srv);
1033 // Setting the cache_size forces the database open, verifying if it is valid
1034 // or corrupt. So this is executed regardless it being actually needed.
1035 // The cache_size is calculated from the actual page_size, to save memory.
1036 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
1037 "PRAGMA cache_size = ");
1038 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
1039 srv = executeSql(mDBConn, cacheSizeQuery.get());
1040 if (srv != SQLITE_OK) {
1041 return convertResultCode(srv);
1044 // Register our built-in SQL functions.
1045 srv = registerFunctions(mDBConn);
1046 if (srv != SQLITE_OK) {
1047 return convertResultCode(srv);
1050 // Register our built-in SQL collating sequences.
1051 srv = registerCollations(mDBConn, mStorageService);
1052 if (srv != SQLITE_OK) {
1053 return convertResultCode(srv);
1056 // Set the default synchronous value. Each consumer can switch this
1057 // accordingly to their needs.
1058 #if defined(ANDROID)
1059 // Android prefers synchronous = OFF for performance reasons.
1060 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
1061 #else
1062 // Normal is the suggested value for WAL journals.
1063 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
1064 #endif
1066 // Initialization succeeded, we can stop guarding for failures.
1067 guard.release();
1068 return NS_OK;
1071 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
1072 MOZ_ASSERT(!IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1073 nsresult rv = aStorageFile
1074 ? initialize(aStorageFile)
1075 : initialize(kMozStorageMemoryStorageKey, VoidCString());
1076 if (NS_FAILED(rv)) {
1077 // Shutdown the async thread, since initialization failed.
1078 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1079 mAsyncExecutionThreadShuttingDown = true;
1080 nsCOMPtr<nsIRunnable> event =
1081 NewRunnableMethod("Connection::shutdownAsyncThread", this,
1082 &Connection::shutdownAsyncThread);
1083 Unused << NS_DispatchToMainThread(event);
1085 return rv;
1088 void Connection::initializeFailed() {
1090 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1091 mConnectionClosed = true;
1093 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
1094 mDBConn = nullptr;
1095 sharedDBMutex.destroy();
1098 nsresult Connection::databaseElementExists(
1099 enum DatabaseElementType aElementType, const nsACString& aElementName,
1100 bool* _exists) {
1101 if (!connectionReady()) {
1102 return NS_ERROR_NOT_AVAILABLE;
1104 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1105 if (NS_FAILED(rv)) {
1106 return rv;
1109 // When constructing the query, make sure to SELECT the correct db's
1110 // sqlite_master if the user is prefixing the element with a specific db. ex:
1111 // sample.test
1112 nsCString query("SELECT name FROM (SELECT * FROM ");
1113 nsDependentCSubstring element;
1114 int32_t ind = aElementName.FindChar('.');
1115 if (ind == kNotFound) {
1116 element.Assign(aElementName);
1117 } else {
1118 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
1119 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
1120 query.Append(db);
1122 query.AppendLiteral(
1123 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
1124 "'");
1126 switch (aElementType) {
1127 case INDEX:
1128 query.AppendLiteral("index");
1129 break;
1130 case TABLE:
1131 query.AppendLiteral("table");
1132 break;
1134 query.AppendLiteral("' AND name ='");
1135 query.Append(element);
1136 query.Append('\'');
1138 sqlite3_stmt* stmt;
1139 int srv = prepareStatement(mDBConn, query, &stmt);
1140 if (srv != SQLITE_OK) {
1141 RecordQueryStatus(srv);
1142 return convertResultCode(srv);
1145 srv = stepStatement(mDBConn, stmt);
1146 // we just care about the return value from step
1147 (void)::sqlite3_finalize(stmt);
1149 RecordQueryStatus(srv);
1151 if (srv == SQLITE_ROW) {
1152 *_exists = true;
1153 return NS_OK;
1155 if (srv == SQLITE_DONE) {
1156 *_exists = false;
1157 return NS_OK;
1160 return convertResultCode(srv);
1163 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
1164 sharedDBMutex.assertCurrentThreadOwns();
1166 for (const auto& data : mFunctions.Values()) {
1167 if (data.function == aInstance) {
1168 return true;
1171 return false;
1174 /* static */
1175 int Connection::sProgressHelper(void* aArg) {
1176 Connection* _this = static_cast<Connection*>(aArg);
1177 return _this->progressHandler();
1180 int Connection::progressHandler() {
1181 sharedDBMutex.assertCurrentThreadOwns();
1182 if (mProgressHandler) {
1183 bool result;
1184 nsresult rv = mProgressHandler->OnProgress(this, &result);
1185 if (NS_FAILED(rv)) return 0; // Don't break request
1186 return result ? 1 : 0;
1188 return 0;
1191 nsresult Connection::setClosedState() {
1192 // Flag that we are shutting down the async thread, so that
1193 // getAsyncExecutionTarget knows not to expose/create the async thread.
1194 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1195 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1197 mAsyncExecutionThreadShuttingDown = true;
1199 // Set the property to null before closing the connection, otherwise the
1200 // other functions in the module may try to use the connection after it is
1201 // closed.
1202 mDBConn = nullptr;
1204 return NS_OK;
1207 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1208 if (aOperationType == ASYNCHRONOUS) {
1209 // Async operations are supported for all connections, on any thread.
1210 return true;
1212 // Sync operations are supported for sync connections (on any thread), and
1213 // async connections on a background thread.
1214 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1215 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1218 nsresult Connection::ensureOperationSupported(
1219 ConnectionOperation aOperationType) {
1220 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1221 #ifdef DEBUG
1222 if (NS_IsMainThread()) {
1223 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1224 Unused << xpc->DebugDumpJSStack(false, false, false);
1226 #endif
1227 MOZ_ASSERT(false,
1228 "Don't use async connections synchronously on the main thread");
1229 return NS_ERROR_NOT_AVAILABLE;
1231 return NS_OK;
1234 bool Connection::isConnectionReadyOnThisThread() {
1235 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1236 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1237 return true;
1239 return connectionReady();
1242 bool Connection::isClosing() {
1243 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1244 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1247 bool Connection::isClosed() {
1248 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1249 return mConnectionClosed;
1252 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1254 bool Connection::isAsyncExecutionThreadAvailable() {
1255 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1256 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1259 void Connection::shutdownAsyncThread() {
1260 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1261 MOZ_ASSERT(mAsyncExecutionThread);
1262 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1264 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1265 mAsyncExecutionThread = nullptr;
1268 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1269 #ifdef DEBUG
1270 { // Make sure we have marked our async thread as shutting down.
1271 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1272 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1273 "Did not call setClosedState!");
1274 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1276 #endif // DEBUG
1278 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1279 nsAutoCString leafName(":memory");
1280 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1281 MOZ_LOG(gStorageLog, LogLevel::Debug,
1282 ("Closing connection to '%s'", leafName.get()));
1285 // At this stage, we may still have statements that need to be
1286 // finalized. Attempt to close the database connection. This will
1287 // always disconnect any virtual tables and cleanly finalize their
1288 // internal statements. Once this is done, closing may fail due to
1289 // unfinalized client statements, in which case we need to finalize
1290 // these statements and close again.
1292 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1293 mConnectionClosed = true;
1296 // Nothing else needs to be done if we don't have a connection here.
1297 if (!aNativeConnection) return NS_OK;
1299 int srv = ::sqlite3_close(aNativeConnection);
1301 if (srv == SQLITE_BUSY) {
1303 // Nothing else should change the connection or statements status until we
1304 // are done here.
1305 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1306 // We still have non-finalized statements. Finalize them.
1307 sqlite3_stmt* stmt = nullptr;
1308 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1309 MOZ_LOG(gStorageLog, LogLevel::Debug,
1310 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1311 stmt));
1313 #ifdef DEBUG
1314 SmprintfPointer msg = ::mozilla::Smprintf(
1315 "SQL statement '%s' (%p) should have been finalized before closing "
1316 "the connection",
1317 ::sqlite3_sql(stmt), stmt);
1318 NS_WARNING(msg.get());
1319 #endif // DEBUG
1321 srv = ::sqlite3_finalize(stmt);
1323 #ifdef DEBUG
1324 if (srv != SQLITE_OK) {
1325 SmprintfPointer msg = ::mozilla::Smprintf(
1326 "Could not finalize SQL statement (%p)", stmt);
1327 NS_WARNING(msg.get());
1329 #endif // DEBUG
1331 // Ensure that the loop continues properly, whether closing has
1332 // succeeded or not.
1333 if (srv == SQLITE_OK) {
1334 stmt = nullptr;
1337 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1338 // again, since Sqlite will try to acquire it.
1341 // Now that all statements have been finalized, we
1342 // should be able to close.
1343 srv = ::sqlite3_close(aNativeConnection);
1344 MOZ_ASSERT(false,
1345 "Had to forcibly close the database connection because not all "
1346 "the statements have been finalized.");
1349 if (srv == SQLITE_OK) {
1350 sharedDBMutex.destroy();
1351 } else {
1352 MOZ_ASSERT(false,
1353 "sqlite3_close failed. There are probably outstanding "
1354 "statements that are listed above!");
1357 return convertResultCode(srv);
1360 nsCString Connection::getFilename() { return mTelemetryFilename; }
1362 int Connection::stepStatement(sqlite3* aNativeConnection,
1363 sqlite3_stmt* aStatement) {
1364 MOZ_ASSERT(aStatement);
1366 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1367 ::sqlite3_sql(aStatement));
1369 bool checkedMainThread = false;
1370 TimeStamp startTime = TimeStamp::Now();
1372 // The connection may have been closed if the executing statement has been
1373 // created and cached after a call to asyncClose() but before the actual
1374 // sqlite3_close(). This usually happens when other tasks using cached
1375 // statements are asynchronously scheduled for execution and any of them ends
1376 // up after asyncClose. See bug 728653 for details.
1377 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1379 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1381 int srv;
1382 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1383 if (!checkedMainThread) {
1384 checkedMainThread = true;
1385 if (::NS_IsMainThread()) {
1386 NS_WARNING("We won't allow blocking on the main thread!");
1387 break;
1391 srv = WaitForUnlockNotify(aNativeConnection);
1392 if (srv != SQLITE_OK) {
1393 break;
1396 ::sqlite3_reset(aStatement);
1399 // Report very slow SQL statements to Telemetry
1400 TimeDuration duration = TimeStamp::Now() - startTime;
1401 const uint32_t threshold = NS_IsMainThread()
1402 ? Telemetry::kSlowSQLThresholdForMainThread
1403 : Telemetry::kSlowSQLThresholdForHelperThreads;
1404 if (duration.ToMilliseconds() >= threshold) {
1405 nsDependentCString statementString(::sqlite3_sql(aStatement));
1406 Telemetry::RecordSlowSQLStatement(
1407 statementString, mTelemetryFilename,
1408 static_cast<uint32_t>(duration.ToMilliseconds()));
1411 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1412 // Drop off the extended result bits of the result code.
1413 return srv & 0xFF;
1416 int Connection::prepareStatement(sqlite3* aNativeConnection,
1417 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1418 // We should not even try to prepare statements after the connection has
1419 // been closed.
1420 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1422 bool checkedMainThread = false;
1424 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1426 int srv;
1427 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1428 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1429 if (!checkedMainThread) {
1430 checkedMainThread = true;
1431 if (::NS_IsMainThread()) {
1432 NS_WARNING("We won't allow blocking on the main thread!");
1433 break;
1437 srv = WaitForUnlockNotify(aNativeConnection);
1438 if (srv != SQLITE_OK) {
1439 break;
1443 if (srv != SQLITE_OK) {
1444 nsCString warnMsg;
1445 warnMsg.AppendLiteral("The SQL statement '");
1446 warnMsg.Append(aSQL);
1447 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1448 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1450 #ifdef DEBUG
1451 NS_WARNING(warnMsg.get());
1452 #endif
1453 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1456 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1457 // Drop off the extended result bits of the result code.
1458 int rc = srv & 0xFF;
1459 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1460 // The callers of this function are used to only checking the return value,
1461 // so it is safer to return an error code.
1462 if (rc == SQLITE_OK && *_stmt == nullptr) {
1463 return SQLITE_MISUSE;
1466 return rc;
1469 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1470 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1472 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1474 TimeStamp startTime = TimeStamp::Now();
1475 int srv =
1476 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1477 RecordQueryStatus(srv);
1479 // Report very slow SQL statements to Telemetry
1480 TimeDuration duration = TimeStamp::Now() - startTime;
1481 const uint32_t threshold = NS_IsMainThread()
1482 ? Telemetry::kSlowSQLThresholdForMainThread
1483 : Telemetry::kSlowSQLThresholdForHelperThreads;
1484 if (duration.ToMilliseconds() >= threshold) {
1485 nsDependentCString statementString(aSqlString);
1486 Telemetry::RecordSlowSQLStatement(
1487 statementString, mTelemetryFilename,
1488 static_cast<uint32_t>(duration.ToMilliseconds()));
1491 return srv;
1494 ////////////////////////////////////////////////////////////////////////////////
1495 //// nsIInterfaceRequestor
1497 NS_IMETHODIMP
1498 Connection::GetInterface(const nsIID& aIID, void** _result) {
1499 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1500 nsIEventTarget* background = getAsyncExecutionTarget();
1501 NS_IF_ADDREF(background);
1502 *_result = background;
1503 return NS_OK;
1505 return NS_ERROR_NO_INTERFACE;
1508 ////////////////////////////////////////////////////////////////////////////////
1509 //// mozIStorageConnection
1511 NS_IMETHODIMP
1512 Connection::Close() {
1513 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1514 if (NS_FAILED(rv)) {
1515 return rv;
1517 return synchronousClose();
1520 nsresult Connection::synchronousClose() {
1521 if (!connectionReady()) {
1522 return NS_ERROR_NOT_INITIALIZED;
1525 #ifdef DEBUG
1526 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1527 // event target. We make this check outside of debug code below in
1528 // setClosedState, but this is here to be explicit.
1529 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1530 #endif // DEBUG
1532 // Make sure we have not executed any asynchronous statements.
1533 // If this fails, the mDBConn may be left open, resulting in a leak.
1534 // We'll try to finalize the pending statements and close the connection.
1535 if (isAsyncExecutionThreadAvailable()) {
1536 #ifdef DEBUG
1537 if (NS_IsMainThread()) {
1538 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1539 Unused << xpc->DebugDumpJSStack(false, false, false);
1541 #endif
1542 MOZ_ASSERT(false,
1543 "Close() was invoked on a connection that executed asynchronous "
1544 "statements. "
1545 "Should have used asyncClose().");
1546 // Try to close the database regardless, to free up resources.
1547 Unused << SpinningSynchronousClose();
1548 return NS_ERROR_UNEXPECTED;
1551 // setClosedState nullifies our connection pointer, so we take a raw pointer
1552 // off it, to pass it through the close procedure.
1553 sqlite3* nativeConn = mDBConn;
1554 nsresult rv = setClosedState();
1555 NS_ENSURE_SUCCESS(rv, rv);
1557 return internalClose(nativeConn);
1560 NS_IMETHODIMP
1561 Connection::SpinningSynchronousClose() {
1562 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1563 if (NS_FAILED(rv)) {
1564 return rv;
1566 if (!IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
1567 return NS_ERROR_NOT_SAME_THREAD;
1570 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1571 // Our only existing caller will never have called close; assert if misused
1572 // so that no new callers assume this works after an AsyncClose.
1573 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1574 if (!connectionReady()) {
1575 return NS_ERROR_UNEXPECTED;
1578 RefPtr<CloseListener> listener = new CloseListener();
1579 rv = AsyncClose(listener);
1580 NS_ENSURE_SUCCESS(rv, rv);
1581 MOZ_ALWAYS_TRUE(
1582 SpinEventLoopUntil("storage::Connection::SpinningSynchronousClose"_ns,
1583 [&]() { return listener->mClosed; }));
1584 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1586 return rv;
1589 NS_IMETHODIMP
1590 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1591 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1592 // Check if AsyncClose or Close were already invoked.
1593 if (!connectionReady()) {
1594 return NS_ERROR_NOT_INITIALIZED;
1596 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1597 if (NS_FAILED(rv)) {
1598 return rv;
1601 // The two relevant factors at this point are whether we have a database
1602 // connection and whether we have an async execution thread. Here's what the
1603 // states mean and how we handle them:
1605 // - (mDBConn && asyncThread): The expected case where we are either an
1606 // async connection or a sync connection that has been used asynchronously.
1607 // Either way the caller must call us and not Close(). Nothing surprising
1608 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1609 // async thread.
1611 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1612 // opened the connection synchronously and was planning to use it
1613 // asynchronously, but never got around to using it asynchronously before
1614 // needing to shutdown. This has been observed to happen for the cookie
1615 // service in a case where Firefox shuts itself down almost immediately
1616 // after startup (for unknown reasons). In the Firefox shutdown case,
1617 // we may also fail to create a new async execution thread if one does not
1618 // already exist. (nsThreadManager will refuse to create new threads when
1619 // it has already been told to shutdown.) As such, we need to handle a
1620 // failure to create the async execution thread by falling back to
1621 // synchronous Close() and also dispatching the completion callback because
1622 // at least Places likes to spin a nested event loop that depends on the
1623 // callback being invoked.
1625 // Note that we have considered not trying to spin up the async execution
1626 // thread in this case if it does not already exist, but the overhead of
1627 // thread startup (if successful) is significantly less expensive than the
1628 // worst-case potential I/O hit of synchronously closing a database when we
1629 // could close it asynchronously.
1631 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1632 // OpenAsyncDatabase encountered a problem opening the database. If it
1633 // happened in all cases AsyncInitDatabase would just shut down the thread
1634 // directly and we would avoid this case. But it doesn't, so for simplicity
1635 // and consistency AsyncCloseConnection knows how to handle this and we
1636 // act like this was the (mDBConn && asyncThread) case in this method.
1638 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1639 // Close() or AsyncClose() has already been called (at least) once. This is
1640 // undeniably a misuse case by the caller. We could optimize for this
1641 // case by adding an additional check of mAsyncExecutionThread without using
1642 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1643 // shut it down. But this complicates the method for broken caller code
1644 // whereas we're still correct and safe without the special-case.
1645 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1647 // Create our callback event if we were given a callback. This will
1648 // eventually be dispatched in all cases, even if we fall back to Close() and
1649 // the database wasn't open and we return an error. The rationale is that
1650 // no existing consumer checks our return value and several of them like to
1651 // spin nested event loops until the callback fires. Given that, it seems
1652 // preferable for us to dispatch the callback in all cases. (Except the
1653 // wrong thread misuse case we bailed on up above. But that's okay because
1654 // that is statically wrong whereas these edge cases are dynamic.)
1655 nsCOMPtr<nsIRunnable> completeEvent;
1656 if (aCallback) {
1657 completeEvent = newCompletionEvent(aCallback);
1660 if (!asyncThread) {
1661 // We were unable to create an async thread, so we need to fall back to
1662 // using normal Close(). Since there is no async thread, Close() will
1663 // not complain about that. (Close() may, however, complain if the
1664 // connection is closed, but that's okay.)
1665 if (completeEvent) {
1666 // Closing the database is more important than returning an error code
1667 // about a failure to dispatch, especially because all existing native
1668 // callers ignore our return value.
1669 Unused << NS_DispatchToMainThread(completeEvent.forget());
1671 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1672 // Return a success inconditionally here, since Close() is unlikely to fail
1673 // and we want to reassure the consumer that its callback will be invoked.
1674 return NS_OK;
1677 // If we're closing the connection during shutdown, and there is an
1678 // interruptible statement running on the helper thread, issue a
1679 // sqlite3_interrupt() to avoid crashing when that statement takes a long
1680 // time (for example a vacuum).
1681 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed) &&
1682 mInterruptible && mIsStatementOnHelperThreadInterruptible) {
1683 MOZ_ASSERT(!isClosing(), "Must not be closing, see Interrupt()");
1684 DebugOnly<nsresult> rv2 = Interrupt();
1685 MOZ_ASSERT(NS_SUCCEEDED(rv2));
1688 // setClosedState nullifies our connection pointer, so we take a raw pointer
1689 // off it, to pass it through the close procedure.
1690 sqlite3* nativeConn = mDBConn;
1691 rv = setClosedState();
1692 NS_ENSURE_SUCCESS(rv, rv);
1694 // Create and dispatch our close event to the background thread.
1695 nsCOMPtr<nsIRunnable> closeEvent =
1696 new AsyncCloseConnection(this, nativeConn, completeEvent);
1697 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1698 NS_ENSURE_SUCCESS(rv, rv);
1700 return NS_OK;
1703 NS_IMETHODIMP
1704 Connection::AsyncClone(bool aReadOnly,
1705 mozIStorageCompletionCallback* aCallback) {
1706 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1708 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1709 if (!connectionReady()) {
1710 return NS_ERROR_NOT_INITIALIZED;
1712 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1713 if (NS_FAILED(rv)) {
1714 return rv;
1716 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1718 int flags = mFlags;
1719 if (aReadOnly) {
1720 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1721 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1722 // Turn off SQLITE_OPEN_CREATE.
1723 flags = (~SQLITE_OPEN_CREATE & flags);
1726 // The cloned connection will still implement the synchronous API, but throw
1727 // if any synchronous methods are called on the main thread.
1728 RefPtr<Connection> clone =
1729 new Connection(mStorageService, flags, ASYNCHRONOUS, mTelemetryFilename);
1731 RefPtr<AsyncInitializeClone> initEvent =
1732 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1733 // Dispatch to our async thread, since the originating connection must remain
1734 // valid and open for the whole cloning process. This also ensures we are
1735 // properly serialized with a `close` operation, rather than race with it.
1736 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1737 if (!target) {
1738 return NS_ERROR_UNEXPECTED;
1740 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1743 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1744 nsresult rv;
1745 if (!mStorageKey.IsEmpty()) {
1746 rv = aClone->initialize(mStorageKey, mName);
1747 } else if (mFileURL) {
1748 rv = aClone->initialize(mFileURL);
1749 } else {
1750 rv = aClone->initialize(mDatabaseFile);
1752 if (NS_FAILED(rv)) {
1753 return rv;
1756 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1758 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1759 NS_ENSURE_SUCCESS(rv, rv);
1761 // Re-attach on-disk databases that were attached to the original connection.
1763 nsCOMPtr<mozIStorageStatement> stmt;
1764 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1765 NS_ENSURE_SUCCESS(rv, rv);
1766 bool hasResult = false;
1767 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1768 nsAutoCString name;
1769 rv = stmt->GetUTF8String(1, name);
1770 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1771 !name.EqualsLiteral("temp")) {
1772 nsCString path;
1773 rv = stmt->GetUTF8String(2, path);
1774 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1775 nsCOMPtr<mozIStorageStatement> attachStmt;
1776 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1777 getter_AddRefs(attachStmt));
1778 NS_ENSURE_SUCCESS(rv, rv);
1779 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1780 NS_ENSURE_SUCCESS(rv, rv);
1781 rv = attachStmt->Execute();
1782 NS_ENSURE_SUCCESS(rv, rv);
1788 // Copy over pragmas from the original connection.
1789 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1790 // schema ("main" and any other attached databases), and this implmentation
1791 // fails to propagate them. This is being addressed on trunk.
1792 static const char* pragmas[] = {
1793 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1794 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1795 for (auto& pragma : pragmas) {
1796 // Read-only connections just need cache_size and temp_store pragmas.
1797 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1798 ::strcmp(pragma, "temp_store") != 0) {
1799 continue;
1802 nsAutoCString pragmaQuery("PRAGMA ");
1803 pragmaQuery.Append(pragma);
1804 nsCOMPtr<mozIStorageStatement> stmt;
1805 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
1806 NS_ENSURE_SUCCESS(rv, rv);
1807 bool hasResult = false;
1808 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1809 pragmaQuery.AppendLiteral(" = ");
1810 pragmaQuery.AppendInt(stmt->AsInt32(0));
1811 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
1812 NS_ENSURE_SUCCESS(rv, rv);
1816 // Copy over temporary tables, triggers, and views from the original
1817 // connections. Entities in `sqlite_temp_master` are only visible to the
1818 // connection that created them.
1819 if (!aReadOnly) {
1820 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
1821 NS_ENSURE_SUCCESS(rv, rv);
1823 nsCOMPtr<mozIStorageStatement> stmt;
1824 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1825 "WHERE type IN ('table', 'view', "
1826 "'index', 'trigger')"),
1827 getter_AddRefs(stmt));
1828 // Propagate errors, because failing to copy triggers might cause schema
1829 // coherency issues when writing to the database from the cloned connection.
1830 NS_ENSURE_SUCCESS(rv, rv);
1831 bool hasResult = false;
1832 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1833 nsAutoCString query;
1834 rv = stmt->GetUTF8String(0, query);
1835 NS_ENSURE_SUCCESS(rv, rv);
1837 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1838 // keyword. We need to add it back, or we'll recreate temporary entities
1839 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1840 // statements, but those don't need `TEMP` keywords.
1841 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
1842 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
1843 StringBeginsWith(query, "CREATE VIEW "_ns)) {
1844 query.Replace(0, 6, "CREATE TEMP");
1847 rv = aClone->ExecuteSimpleSQL(query);
1848 NS_ENSURE_SUCCESS(rv, rv);
1851 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
1852 NS_ENSURE_SUCCESS(rv, rv);
1855 // Copy any functions that have been added to this connection.
1856 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1857 for (const auto& entry : mFunctions) {
1858 const nsACString& key = entry.GetKey();
1859 Connection::FunctionInfo data = entry.GetData();
1861 rv = aClone->CreateFunction(key, data.numArgs, data.function);
1862 if (NS_FAILED(rv)) {
1863 NS_WARNING("Failed to copy function to cloned connection");
1867 guard.release();
1868 return NS_OK;
1871 NS_IMETHODIMP
1872 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
1873 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1875 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
1877 if (!connectionReady()) {
1878 return NS_ERROR_NOT_INITIALIZED;
1880 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1881 if (NS_FAILED(rv)) {
1882 return rv;
1885 int flags = mFlags;
1886 if (aReadOnly) {
1887 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1888 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1889 // Turn off SQLITE_OPEN_CREATE.
1890 flags = (~SQLITE_OPEN_CREATE & flags);
1893 RefPtr<Connection> clone =
1894 new Connection(mStorageService, flags, mSupportedOperations,
1895 mTelemetryFilename, mInterruptible);
1897 rv = initializeClone(clone, aReadOnly);
1898 if (NS_FAILED(rv)) {
1899 return rv;
1902 NS_IF_ADDREF(*_connection = clone);
1903 return NS_OK;
1906 NS_IMETHODIMP
1907 Connection::Interrupt() {
1908 MOZ_ASSERT(mInterruptible, "Interrupt method not allowed");
1909 MOZ_ASSERT_IF(SYNCHRONOUS == mSupportedOperations,
1910 !IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1911 MOZ_ASSERT_IF(ASYNCHRONOUS == mSupportedOperations,
1912 IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1914 if (!connectionReady()) {
1915 return NS_ERROR_NOT_INITIALIZED;
1918 if (isClosing()) { // Closing already in asynchronous case
1919 return NS_OK;
1923 // As stated on https://www.sqlite.org/c3ref/interrupt.html,
1924 // it is not safe to call sqlite3_interrupt() when
1925 // database connection is closed or might close before
1926 // sqlite3_interrupt() returns.
1927 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1928 if (!isClosed(lockedScope)) {
1929 MOZ_ASSERT(mDBConn);
1930 ::sqlite3_interrupt(mDBConn);
1934 return NS_OK;
1937 NS_IMETHODIMP
1938 Connection::AsyncVacuum(mozIStorageCompletionCallback* aCallback,
1939 bool aUseIncremental, int32_t aSetPageSize) {
1940 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1941 // Abort if we're shutting down.
1942 if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
1943 return NS_ERROR_ABORT;
1945 // Check if AsyncClose or Close were already invoked.
1946 if (!connectionReady()) {
1947 return NS_ERROR_NOT_INITIALIZED;
1949 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1950 if (NS_FAILED(rv)) {
1951 return rv;
1953 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1954 if (!asyncThread) {
1955 return NS_ERROR_NOT_INITIALIZED;
1958 // Create and dispatch our vacuum event to the background thread.
1959 nsCOMPtr<nsIRunnable> vacuumEvent =
1960 new AsyncVacuumEvent(this, aCallback, aUseIncremental, aSetPageSize);
1961 rv = asyncThread->Dispatch(vacuumEvent, NS_DISPATCH_NORMAL);
1962 NS_ENSURE_SUCCESS(rv, rv);
1964 return NS_OK;
1967 NS_IMETHODIMP
1968 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
1969 *_defaultPageSize = Service::kDefaultPageSize;
1970 return NS_OK;
1973 NS_IMETHODIMP
1974 Connection::GetConnectionReady(bool* _ready) {
1975 MOZ_ASSERT(IsOnCurrentSerialEventTarget(eventTargetOpenedOn));
1976 *_ready = connectionReady();
1977 return NS_OK;
1980 NS_IMETHODIMP
1981 Connection::GetDatabaseFile(nsIFile** _dbFile) {
1982 if (!connectionReady()) {
1983 return NS_ERROR_NOT_INITIALIZED;
1985 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1986 if (NS_FAILED(rv)) {
1987 return rv;
1990 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
1992 return NS_OK;
1995 NS_IMETHODIMP
1996 Connection::GetLastInsertRowID(int64_t* _id) {
1997 if (!connectionReady()) {
1998 return NS_ERROR_NOT_INITIALIZED;
2000 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2001 if (NS_FAILED(rv)) {
2002 return rv;
2005 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
2006 *_id = id;
2008 return NS_OK;
2011 NS_IMETHODIMP
2012 Connection::GetAffectedRows(int32_t* _rows) {
2013 if (!connectionReady()) {
2014 return NS_ERROR_NOT_INITIALIZED;
2016 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2017 if (NS_FAILED(rv)) {
2018 return rv;
2021 *_rows = ::sqlite3_changes(mDBConn);
2023 return NS_OK;
2026 NS_IMETHODIMP
2027 Connection::GetLastError(int32_t* _error) {
2028 if (!connectionReady()) {
2029 return NS_ERROR_NOT_INITIALIZED;
2031 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2032 if (NS_FAILED(rv)) {
2033 return rv;
2036 *_error = ::sqlite3_errcode(mDBConn);
2038 return NS_OK;
2041 NS_IMETHODIMP
2042 Connection::GetLastErrorString(nsACString& _errorString) {
2043 if (!connectionReady()) {
2044 return NS_ERROR_NOT_INITIALIZED;
2046 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2047 if (NS_FAILED(rv)) {
2048 return rv;
2051 const char* serr = ::sqlite3_errmsg(mDBConn);
2052 _errorString.Assign(serr);
2054 return NS_OK;
2057 NS_IMETHODIMP
2058 Connection::GetSchemaVersion(int32_t* _version) {
2059 if (!connectionReady()) {
2060 return NS_ERROR_NOT_INITIALIZED;
2062 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2063 if (NS_FAILED(rv)) {
2064 return rv;
2067 nsCOMPtr<mozIStorageStatement> stmt;
2068 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
2069 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
2071 *_version = 0;
2072 bool hasResult;
2073 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2074 *_version = stmt->AsInt32(0);
2077 return NS_OK;
2080 NS_IMETHODIMP
2081 Connection::SetSchemaVersion(int32_t aVersion) {
2082 if (!connectionReady()) {
2083 return NS_ERROR_NOT_INITIALIZED;
2085 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2086 if (NS_FAILED(rv)) {
2087 return rv;
2090 nsAutoCString stmt("PRAGMA user_version = "_ns);
2091 stmt.AppendInt(aVersion);
2093 return ExecuteSimpleSQL(stmt);
2096 NS_IMETHODIMP
2097 Connection::CreateStatement(const nsACString& aSQLStatement,
2098 mozIStorageStatement** _stmt) {
2099 NS_ENSURE_ARG_POINTER(_stmt);
2100 if (!connectionReady()) {
2101 return NS_ERROR_NOT_INITIALIZED;
2103 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2104 if (NS_FAILED(rv)) {
2105 return rv;
2108 RefPtr<Statement> statement(new Statement());
2109 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2111 rv = statement->initialize(this, mDBConn, aSQLStatement);
2112 NS_ENSURE_SUCCESS(rv, rv);
2114 Statement* rawPtr;
2115 statement.forget(&rawPtr);
2116 *_stmt = rawPtr;
2117 return NS_OK;
2120 NS_IMETHODIMP
2121 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
2122 mozIStorageAsyncStatement** _stmt) {
2123 NS_ENSURE_ARG_POINTER(_stmt);
2124 if (!connectionReady()) {
2125 return NS_ERROR_NOT_INITIALIZED;
2127 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2128 if (NS_FAILED(rv)) {
2129 return rv;
2132 RefPtr<AsyncStatement> statement(new AsyncStatement());
2133 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
2135 rv = statement->initialize(this, mDBConn, aSQLStatement);
2136 NS_ENSURE_SUCCESS(rv, rv);
2138 AsyncStatement* rawPtr;
2139 statement.forget(&rawPtr);
2140 *_stmt = rawPtr;
2141 return NS_OK;
2144 NS_IMETHODIMP
2145 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
2146 CHECK_MAINTHREAD_ABUSE();
2147 if (!connectionReady()) {
2148 return NS_ERROR_NOT_INITIALIZED;
2150 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2151 if (NS_FAILED(rv)) {
2152 return rv;
2155 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
2156 return convertResultCode(srv);
2159 NS_IMETHODIMP
2160 Connection::ExecuteAsync(
2161 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
2162 mozIStorageStatementCallback* aCallback,
2163 mozIStoragePendingStatement** _handle) {
2164 nsTArray<StatementData> stmts(aStatements.Length());
2165 for (uint32_t i = 0; i < aStatements.Length(); i++) {
2166 nsCOMPtr<StorageBaseStatementInternal> stmt =
2167 do_QueryInterface(aStatements[i]);
2168 NS_ENSURE_STATE(stmt);
2170 // Obtain our StatementData.
2171 StatementData data;
2172 nsresult rv = stmt->getAsynchronousStatementData(data);
2173 NS_ENSURE_SUCCESS(rv, rv);
2175 NS_ASSERTION(stmt->getOwner() == this,
2176 "Statement must be from this database connection!");
2178 // Now append it to our array.
2179 stmts.AppendElement(data);
2182 // Dispatch to the background
2183 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
2184 aCallback, _handle);
2187 NS_IMETHODIMP
2188 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
2189 mozIStorageStatementCallback* aCallback,
2190 mozIStoragePendingStatement** _handle) {
2191 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
2193 nsCOMPtr<mozIStorageAsyncStatement> stmt;
2194 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
2195 if (NS_FAILED(rv)) {
2196 return rv;
2199 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
2200 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
2201 if (NS_FAILED(rv)) {
2202 return rv;
2205 pendingStatement.forget(_handle);
2206 return rv;
2209 NS_IMETHODIMP
2210 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
2211 return databaseElementExists(TABLE, aTableName, _exists);
2214 NS_IMETHODIMP
2215 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2216 return databaseElementExists(INDEX, aIndexName, _exists);
2219 NS_IMETHODIMP
2220 Connection::GetTransactionInProgress(bool* _inProgress) {
2221 if (!connectionReady()) {
2222 return NS_ERROR_NOT_INITIALIZED;
2224 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2225 if (NS_FAILED(rv)) {
2226 return rv;
2229 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2230 *_inProgress = transactionInProgress(lockedScope);
2231 return NS_OK;
2234 NS_IMETHODIMP
2235 Connection::GetDefaultTransactionType(int32_t* _type) {
2236 *_type = mDefaultTransactionType;
2237 return NS_OK;
2240 NS_IMETHODIMP
2241 Connection::SetDefaultTransactionType(int32_t aType) {
2242 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2243 mDefaultTransactionType = aType;
2244 return NS_OK;
2247 NS_IMETHODIMP
2248 Connection::GetVariableLimit(int32_t* _limit) {
2249 if (!connectionReady()) {
2250 return NS_ERROR_NOT_INITIALIZED;
2252 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2253 if (limit < 0) {
2254 return NS_ERROR_UNEXPECTED;
2256 *_limit = limit;
2257 return NS_OK;
2260 NS_IMETHODIMP
2261 Connection::BeginTransaction() {
2262 if (!connectionReady()) {
2263 return NS_ERROR_NOT_INITIALIZED;
2265 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2266 if (NS_FAILED(rv)) {
2267 return rv;
2270 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2271 return beginTransactionInternal(lockedScope, mDBConn,
2272 mDefaultTransactionType);
2275 nsresult Connection::beginTransactionInternal(
2276 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2277 int32_t aTransactionType) {
2278 if (transactionInProgress(aProofOfLock)) {
2279 return NS_ERROR_FAILURE;
2281 nsresult rv;
2282 switch (aTransactionType) {
2283 case TRANSACTION_DEFERRED:
2284 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2285 break;
2286 case TRANSACTION_IMMEDIATE:
2287 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2288 break;
2289 case TRANSACTION_EXCLUSIVE:
2290 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2291 break;
2292 default:
2293 return NS_ERROR_ILLEGAL_VALUE;
2295 return rv;
2298 NS_IMETHODIMP
2299 Connection::CommitTransaction() {
2300 if (!connectionReady()) {
2301 return NS_ERROR_NOT_INITIALIZED;
2303 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2304 if (NS_FAILED(rv)) {
2305 return rv;
2308 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2309 return commitTransactionInternal(lockedScope, mDBConn);
2312 nsresult Connection::commitTransactionInternal(
2313 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2314 if (!transactionInProgress(aProofOfLock)) {
2315 return NS_ERROR_UNEXPECTED;
2317 nsresult rv =
2318 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2319 return rv;
2322 NS_IMETHODIMP
2323 Connection::RollbackTransaction() {
2324 if (!connectionReady()) {
2325 return NS_ERROR_NOT_INITIALIZED;
2327 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2328 if (NS_FAILED(rv)) {
2329 return rv;
2332 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2333 return rollbackTransactionInternal(lockedScope, mDBConn);
2336 nsresult Connection::rollbackTransactionInternal(
2337 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2338 if (!transactionInProgress(aProofOfLock)) {
2339 return NS_ERROR_UNEXPECTED;
2342 nsresult rv =
2343 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2344 return rv;
2347 NS_IMETHODIMP
2348 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2349 if (!connectionReady()) {
2350 return NS_ERROR_NOT_INITIALIZED;
2352 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2353 if (NS_FAILED(rv)) {
2354 return rv;
2357 SmprintfPointer buf =
2358 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2359 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2361 int srv = executeSql(mDBConn, buf.get());
2363 return convertResultCode(srv);
2366 NS_IMETHODIMP
2367 Connection::CreateFunction(const nsACString& aFunctionName,
2368 int32_t aNumArguments,
2369 mozIStorageFunction* aFunction) {
2370 if (!connectionReady()) {
2371 return NS_ERROR_NOT_INITIALIZED;
2373 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2374 if (NS_FAILED(rv)) {
2375 return rv;
2378 // Check to see if this function is already defined. We only check the name
2379 // because a function can be defined with the same body but different names.
2380 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2381 NS_ENSURE_FALSE(mFunctions.Contains(aFunctionName), NS_ERROR_FAILURE);
2383 int srv = ::sqlite3_create_function(
2384 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2385 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2386 if (srv != SQLITE_OK) return convertResultCode(srv);
2388 FunctionInfo info = {aFunction, aNumArguments};
2389 mFunctions.InsertOrUpdate(aFunctionName, info);
2391 return NS_OK;
2394 NS_IMETHODIMP
2395 Connection::RemoveFunction(const nsACString& aFunctionName) {
2396 if (!connectionReady()) {
2397 return NS_ERROR_NOT_INITIALIZED;
2399 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2400 if (NS_FAILED(rv)) {
2401 return rv;
2404 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2405 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2407 int srv = ::sqlite3_create_function(
2408 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2409 nullptr, nullptr, nullptr, nullptr);
2410 if (srv != SQLITE_OK) return convertResultCode(srv);
2412 mFunctions.Remove(aFunctionName);
2414 return NS_OK;
2417 NS_IMETHODIMP
2418 Connection::SetProgressHandler(int32_t aGranularity,
2419 mozIStorageProgressHandler* aHandler,
2420 mozIStorageProgressHandler** _oldHandler) {
2421 if (!connectionReady()) {
2422 return NS_ERROR_NOT_INITIALIZED;
2424 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2425 if (NS_FAILED(rv)) {
2426 return rv;
2429 // Return previous one
2430 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2431 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2433 if (!aHandler || aGranularity <= 0) {
2434 aHandler = nullptr;
2435 aGranularity = 0;
2437 mProgressHandler = aHandler;
2438 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2440 return NS_OK;
2443 NS_IMETHODIMP
2444 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2445 if (!connectionReady()) {
2446 return NS_ERROR_NOT_INITIALIZED;
2448 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2449 if (NS_FAILED(rv)) {
2450 return rv;
2453 // Return previous one
2454 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2455 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2457 mProgressHandler = nullptr;
2458 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2460 return NS_OK;
2463 NS_IMETHODIMP
2464 Connection::SetGrowthIncrement(int32_t aChunkSize,
2465 const nsACString& aDatabaseName) {
2466 if (!connectionReady()) {
2467 return NS_ERROR_NOT_INITIALIZED;
2469 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2470 if (NS_FAILED(rv)) {
2471 return rv;
2474 // Bug 597215: Disk space is extremely limited on Android
2475 // so don't preallocate space. This is also not effective
2476 // on log structured file systems used by Android devices
2477 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2478 // Don't preallocate if less than 500MiB is available.
2479 int64_t bytesAvailable;
2480 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2481 NS_ENSURE_SUCCESS(rv, rv);
2482 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2483 return NS_ERROR_FILE_TOO_BIG;
2486 int srv = ::sqlite3_file_control(
2487 mDBConn,
2488 aDatabaseName.Length() ? nsPromiseFlatCString(aDatabaseName).get()
2489 : nullptr,
2490 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2491 if (srv == SQLITE_OK) {
2492 mGrowthChunkSize = aChunkSize;
2494 #endif
2495 return NS_OK;
2498 int32_t Connection::RemovablePagesInFreeList(const nsACString& aSchemaName) {
2499 int32_t freeListPagesCount = 0;
2500 if (!isConnectionReadyOnThisThread()) {
2501 MOZ_ASSERT(false, "Database connection is not ready");
2502 return freeListPagesCount;
2505 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2506 query.Append(aSchemaName);
2507 query.AppendLiteral(".freelist_count");
2508 nsCOMPtr<mozIStorageStatement> stmt;
2509 DebugOnly<nsresult> rv = CreateStatement(query, getter_AddRefs(stmt));
2510 MOZ_ASSERT(NS_SUCCEEDED(rv));
2511 bool hasResult = false;
2512 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
2513 freeListPagesCount = stmt->AsInt32(0);
2516 // If there's no chunk size set, any page is good to be removed.
2517 if (mGrowthChunkSize == 0 || freeListPagesCount == 0) {
2518 return freeListPagesCount;
2520 int32_t pageSize;
2522 nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
2523 query.Append(aSchemaName);
2524 query.AppendLiteral(".page_size");
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 pageSize = stmt->AsInt32(0);
2531 } else {
2532 MOZ_ASSERT(false, "Couldn't get page_size");
2533 return 0;
2536 return std::max(0, freeListPagesCount - (mGrowthChunkSize / pageSize));
2539 NS_IMETHODIMP
2540 Connection::EnableModule(const nsACString& aModuleName) {
2541 if (!connectionReady()) {
2542 return NS_ERROR_NOT_INITIALIZED;
2544 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2545 if (NS_FAILED(rv)) {
2546 return rv;
2549 for (auto& gModule : gModules) {
2550 struct Module* m = &gModule;
2551 if (aModuleName.Equals(m->name)) {
2552 int srv = m->registerFunc(mDBConn, m->name);
2553 if (srv != SQLITE_OK) return convertResultCode(srv);
2555 return NS_OK;
2559 return NS_ERROR_FAILURE;
2562 // Implemented in QuotaVFS.cpp
2563 already_AddRefed<QuotaObject> GetQuotaObjectForFile(sqlite3_file* pFile);
2565 NS_IMETHODIMP
2566 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2567 QuotaObject** aJournalQuotaObject) {
2568 MOZ_ASSERT(aDatabaseQuotaObject);
2569 MOZ_ASSERT(aJournalQuotaObject);
2571 if (!connectionReady()) {
2572 return NS_ERROR_NOT_INITIALIZED;
2574 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2575 if (NS_FAILED(rv)) {
2576 return rv;
2579 sqlite3_file* file;
2580 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2581 &file);
2582 if (srv != SQLITE_OK) {
2583 return convertResultCode(srv);
2586 RefPtr<QuotaObject> databaseQuotaObject = GetQuotaObjectForFile(file);
2587 if (NS_WARN_IF(!databaseQuotaObject)) {
2588 return NS_ERROR_FAILURE;
2591 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2592 &file);
2593 if (srv != SQLITE_OK) {
2594 return convertResultCode(srv);
2597 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObjectForFile(file);
2598 if (NS_WARN_IF(!journalQuotaObject)) {
2599 return NS_ERROR_FAILURE;
2602 databaseQuotaObject.forget(aDatabaseQuotaObject);
2603 journalQuotaObject.forget(aJournalQuotaObject);
2604 return NS_OK;
2607 SQLiteMutex& Connection::GetSharedDBMutex() { return sharedDBMutex; }
2609 uint32_t Connection::GetTransactionNestingLevel(
2610 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2611 return mTransactionNestingLevel;
2614 uint32_t Connection::IncreaseTransactionNestingLevel(
2615 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2616 return ++mTransactionNestingLevel;
2619 uint32_t Connection::DecreaseTransactionNestingLevel(
2620 const mozilla::storage::SQLiteMutexAutoLock& aProofOfLock) {
2621 return --mTransactionNestingLevel;
2624 } // namespace mozilla::storage