Bug 1678642 [wpt PR 26591] - Fix typo in max-age attribute for Set-Cookie header...
[gecko.git] / storage / mozStorageConnection.cpp
blob3cbc5f629091c39ddc4862eda52d938895001040
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 <stdio.h>
9 #include "nsError.h"
10 #include "nsThreadUtils.h"
11 #include "nsIFile.h"
12 #include "nsIFileURL.h"
13 #include "nsIXPConnect.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"
41 #include "GeckoProfiler.h"
43 #include "mozilla/Logging.h"
44 #include "mozilla/Printf.h"
45 #include "nsProxyRelease.h"
46 #include "nsURLHelper.h"
48 #include <algorithm>
50 #define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
52 // Maximum size of the pages cache per connection.
53 #define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
55 mozilla::LazyLogModule gStorageLog("mozStorage");
57 // Checks that the protected code is running on the main-thread only if the
58 // connection was also opened on it.
59 #ifdef DEBUG
60 # define CHECK_MAINTHREAD_ABUSE() \
61 do { \
62 nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); \
63 NS_WARNING_ASSERTION( \
64 threadOpenedOn == mainThread || !NS_IsMainThread(), \
65 "Using Storage synchronous API on main-thread, but " \
66 "the connection was " \
67 "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* GetTelemetryVFSName(bool);
83 const char* GetObfuscatingVFSName();
85 namespace {
87 int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
88 if (NS_SUCCEEDED(aXPCOMResultCode)) {
89 return SQLITE_OK;
92 switch (aXPCOMResultCode) {
93 case NS_ERROR_FILE_CORRUPTED:
94 return SQLITE_CORRUPT;
95 case NS_ERROR_FILE_ACCESS_DENIED:
96 return SQLITE_CANTOPEN;
97 case NS_ERROR_STORAGE_BUSY:
98 return SQLITE_BUSY;
99 case NS_ERROR_FILE_IS_LOCKED:
100 return SQLITE_LOCKED;
101 case NS_ERROR_FILE_READ_ONLY:
102 return SQLITE_READONLY;
103 case NS_ERROR_STORAGE_IOERR:
104 return SQLITE_IOERR;
105 case NS_ERROR_FILE_NO_DEVICE_SPACE:
106 return SQLITE_FULL;
107 case NS_ERROR_OUT_OF_MEMORY:
108 return SQLITE_NOMEM;
109 case NS_ERROR_UNEXPECTED:
110 return SQLITE_MISUSE;
111 case NS_ERROR_ABORT:
112 return SQLITE_ABORT;
113 case NS_ERROR_STORAGE_CONSTRAINT:
114 return SQLITE_CONSTRAINT;
115 default:
116 return SQLITE_ERROR;
119 MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!");
122 ////////////////////////////////////////////////////////////////////////////////
123 //// Variant Specialization Functions (variantToSQLiteT)
125 int sqlite3_T_int(sqlite3_context* aCtx, int aValue) {
126 ::sqlite3_result_int(aCtx, aValue);
127 return SQLITE_OK;
130 int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
131 ::sqlite3_result_int64(aCtx, aValue);
132 return SQLITE_OK;
135 int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
136 ::sqlite3_result_double(aCtx, aValue);
137 return SQLITE_OK;
140 int sqlite3_T_text(sqlite3_context* aCtx, const nsCString& aValue) {
141 ::sqlite3_result_text(aCtx, aValue.get(), aValue.Length(), SQLITE_TRANSIENT);
142 return SQLITE_OK;
145 int sqlite3_T_text16(sqlite3_context* aCtx, const nsString& aValue) {
146 ::sqlite3_result_text16(
147 aCtx, aValue.get(),
148 aValue.Length() * sizeof(char16_t), // Number of bytes.
149 SQLITE_TRANSIENT);
150 return SQLITE_OK;
153 int sqlite3_T_null(sqlite3_context* aCtx) {
154 ::sqlite3_result_null(aCtx);
155 return SQLITE_OK;
158 int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
159 ::sqlite3_result_blob(aCtx, aData, aSize, free);
160 return SQLITE_OK;
163 #include "variantToSQLiteT_impl.h"
165 ////////////////////////////////////////////////////////////////////////////////
166 //// Modules
168 struct Module {
169 const char* name;
170 int (*registerFunc)(sqlite3*, const char*);
173 Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
175 ////////////////////////////////////////////////////////////////////////////////
176 //// Local Functions
178 int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
179 switch (aReason) {
180 case SQLITE_TRACE_STMT: {
181 // aP is a pointer to the prepared statement.
182 sqlite3_stmt* stmt = static_cast<sqlite3_stmt*>(aP);
183 // aX is a pointer to a string containing the unexpanded SQL or a comment,
184 // starting with "--"" in case of a trigger.
185 char* expanded = static_cast<char*>(aX);
186 // Simulate what sqlite_trace was doing.
187 if (!::strncmp(expanded, "--", 2)) {
188 MOZ_LOG(gStorageLog, LogLevel::Debug,
189 ("TRACE_STMT on %p: '%s'", aClosure, expanded));
190 } else {
191 char* sql = ::sqlite3_expanded_sql(stmt);
192 MOZ_LOG(gStorageLog, LogLevel::Debug,
193 ("TRACE_STMT on %p: '%s'", aClosure, sql));
194 ::sqlite3_free(sql);
196 break;
198 case SQLITE_TRACE_PROFILE: {
199 // aX is pointer to a 64bit integer containing nanoseconds it took to
200 // execute the last command.
201 sqlite_int64 time = *(static_cast<sqlite_int64*>(aX)) / 1000000;
202 if (time > 0) {
203 MOZ_LOG(gStorageLog, LogLevel::Debug,
204 ("TRACE_TIME on %p: %lldms", aClosure, time));
206 break;
209 return 0;
212 void basicFunctionHelper(sqlite3_context* aCtx, int aArgc,
213 sqlite3_value** aArgv) {
214 void* userData = ::sqlite3_user_data(aCtx);
216 mozIStorageFunction* func = static_cast<mozIStorageFunction*>(userData);
218 RefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
219 if (!arguments) return;
221 nsCOMPtr<nsIVariant> result;
222 nsresult rv = func->OnFunctionCall(arguments, getter_AddRefs(result));
223 if (NS_FAILED(rv)) {
224 nsAutoCString errorMessage;
225 GetErrorName(rv, errorMessage);
226 errorMessage.InsertLiteral("User function returned ", 0);
227 errorMessage.Append('!');
229 NS_WARNING(errorMessage.get());
231 ::sqlite3_result_error(aCtx, errorMessage.get(), -1);
232 ::sqlite3_result_error_code(aCtx, nsresultToSQLiteResult(rv));
233 return;
235 int retcode = variantToSQLiteT(aCtx, result);
236 if (retcode != SQLITE_OK) {
237 NS_WARNING("User function returned invalid data type!");
238 ::sqlite3_result_error(aCtx, "User function returned invalid data type",
239 -1);
244 * This code is heavily based on the sample at:
245 * http://www.sqlite.org/unlock_notify.html
247 class UnlockNotification {
248 public:
249 UnlockNotification()
250 : mMutex("UnlockNotification mMutex"),
251 mCondVar(mMutex, "UnlockNotification condVar"),
252 mSignaled(false) {}
254 void Wait() {
255 MutexAutoLock lock(mMutex);
256 while (!mSignaled) {
257 (void)mCondVar.Wait();
261 void Signal() {
262 MutexAutoLock lock(mMutex);
263 mSignaled = true;
264 (void)mCondVar.Notify();
267 private:
268 Mutex mMutex;
269 CondVar mCondVar;
270 bool mSignaled;
273 void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
274 for (int i = 0; i < aArgsSize; i++) {
275 UnlockNotification* notification =
276 static_cast<UnlockNotification*>(aArgs[i]);
277 notification->Signal();
281 int WaitForUnlockNotify(sqlite3* aDatabase) {
282 UnlockNotification notification;
283 int srv =
284 ::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, &notification);
285 MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
286 if (srv == SQLITE_OK) {
287 notification.Wait();
290 return srv;
293 ////////////////////////////////////////////////////////////////////////////////
294 //// Local Classes
296 class AsyncCloseConnection final : public Runnable {
297 public:
298 AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
299 nsIRunnable* aCallbackEvent)
300 : Runnable("storage::AsyncCloseConnection"),
301 mConnection(aConnection),
302 mNativeConnection(aNativeConnection),
303 mCallbackEvent(aCallbackEvent) {}
305 NS_IMETHOD Run() override {
306 // This code is executed on the background thread
307 MOZ_ASSERT(NS_GetCurrentThread() != mConnection->threadOpenedOn);
309 nsCOMPtr<nsIRunnable> event =
310 NewRunnableMethod("storage::Connection::shutdownAsyncThread",
311 mConnection, &Connection::shutdownAsyncThread);
312 MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event));
314 // Internal close.
315 (void)mConnection->internalClose(mNativeConnection);
317 // Callback
318 if (mCallbackEvent) {
319 nsCOMPtr<nsIThread> thread;
320 (void)NS_GetMainThread(getter_AddRefs(thread));
321 (void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
324 return NS_OK;
327 ~AsyncCloseConnection() override {
328 NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
329 mConnection.forget());
330 NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
331 mCallbackEvent.forget());
334 private:
335 RefPtr<Connection> mConnection;
336 sqlite3* mNativeConnection;
337 nsCOMPtr<nsIRunnable> mCallbackEvent;
341 * An event used to initialize the clone of a connection.
343 * Must be executed on the clone's async execution thread.
345 class AsyncInitializeClone final : public Runnable {
346 public:
348 * @param aConnection The connection being cloned.
349 * @param aClone The clone.
350 * @param aReadOnly If |true|, the clone is read only.
351 * @param aCallback A callback to trigger once initialization
352 * is complete. This event will be called on
353 * aClone->threadOpenedOn.
355 AsyncInitializeClone(Connection* aConnection, Connection* aClone,
356 const bool aReadOnly,
357 mozIStorageCompletionCallback* aCallback)
358 : Runnable("storage::AsyncInitializeClone"),
359 mConnection(aConnection),
360 mClone(aClone),
361 mReadOnly(aReadOnly),
362 mCallback(aCallback) {
363 MOZ_ASSERT(NS_IsMainThread());
366 NS_IMETHOD Run() override {
367 MOZ_ASSERT(!NS_IsMainThread());
368 nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
369 if (NS_FAILED(rv)) {
370 return Dispatch(rv, nullptr);
372 return Dispatch(NS_OK,
373 NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
376 private:
377 nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
378 RefPtr<CallbackComplete> event =
379 new CallbackComplete(aResult, aValue, mCallback.forget());
380 return mClone->threadOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
383 ~AsyncInitializeClone() override {
384 nsCOMPtr<nsIThread> thread;
385 DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
386 MOZ_ASSERT(NS_SUCCEEDED(rv));
388 // Handle ambiguous nsISupports inheritance.
389 NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
390 mConnection.forget());
391 NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
393 // Generally, the callback will be released by CallbackComplete.
394 // However, if for some reason Run() is not executed, we still
395 // need to ensure that it is released here.
396 NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
397 mCallback.forget());
400 RefPtr<Connection> mConnection;
401 RefPtr<Connection> mClone;
402 const bool mReadOnly;
403 nsCOMPtr<mozIStorageCompletionCallback> mCallback;
407 * A listener for async connection closing.
409 class CloseListener final : public mozIStorageCompletionCallback {
410 public:
411 NS_DECL_ISUPPORTS
412 CloseListener() : mClosed(false) {}
414 NS_IMETHOD Complete(nsresult, nsISupports*) override {
415 mClosed = true;
416 return NS_OK;
419 bool mClosed;
421 private:
422 ~CloseListener() = default;
425 NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)
427 } // namespace
429 ////////////////////////////////////////////////////////////////////////////////
430 //// Connection
432 Connection::Connection(Service* aService, int aFlags,
433 ConnectionOperation aSupportedOperations,
434 bool aIgnoreLockingMode)
435 : sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
436 sharedDBMutex("Connection::sharedDBMutex"),
437 threadOpenedOn(do_GetCurrentThread()),
438 mDBConn(nullptr),
439 mAsyncExecutionThreadShuttingDown(false),
440 mConnectionClosed(false),
441 mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
442 mDestroying(false),
443 mProgressHandler(nullptr),
444 mFlags(aFlags),
445 mIgnoreLockingMode(aIgnoreLockingMode),
446 mStorageService(aService),
447 mSupportedOperations(aSupportedOperations) {
448 MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
449 "Can't ignore locking for a non-readonly connection!");
450 mStorageService->registerConnection(this);
453 Connection::~Connection() {
454 // Failsafe Close() occurs in our custom Release method because of
455 // complications related to Close() potentially invoking AsyncClose() which
456 // will increment our refcount.
457 MOZ_ASSERT(!mAsyncExecutionThread,
458 "The async thread has not been shutdown properly!");
461 NS_IMPL_ADDREF(Connection)
463 NS_INTERFACE_MAP_BEGIN(Connection)
464 NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
465 NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
466 NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
467 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
468 NS_INTERFACE_MAP_END
470 // This is identical to what NS_IMPL_RELEASE provides, but with the
471 // extra |1 == count| case.
472 NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
473 MOZ_ASSERT(0 != mRefCnt, "dup release");
474 nsrefcnt count = --mRefCnt;
475 NS_LOG_RELEASE(this, count, "Connection");
476 if (1 == count) {
477 // If the refcount went to 1, the single reference must be from
478 // gService->mConnections (in class |Service|). And the code calling
479 // Release is either:
480 // - The "user" code that had created the connection, releasing on any
481 // thread.
482 // - One of Service's getConnections() callers had acquired a strong
483 // reference to the Connection that out-lived the last "user" reference,
484 // and now that just got dropped. Note that this reference could be
485 // getting dropped on the main thread or Connection->threadOpenedOn
486 // (because of the NewRunnableMethod used by minimizeMemory).
488 // Either way, we should now perform our failsafe Close() and unregister.
489 // However, we only want to do this once, and the reality is that our
490 // refcount could go back up above 1 and down again at any time if we are
491 // off the main thread and getConnections() gets called on the main thread,
492 // so we use an atomic here to do this exactly once.
493 if (mDestroying.compareExchange(false, true)) {
494 // Close the connection, dispatching to the opening thread if we're not
495 // on that thread already and that thread is still accepting runnables.
496 // We do this because it's possible we're on the main thread because of
497 // getConnections(), and we REALLY don't want to transfer I/O to the main
498 // thread if we can avoid it.
499 if (threadOpenedOn->IsOnCurrentThread()) {
500 // This could cause SpinningSynchronousClose() to be invoked and AddRef
501 // triggered for AsyncCloseConnection's strong ref if the conn was ever
502 // use for async purposes. (Main-thread only, though.)
503 Unused << synchronousClose();
504 } else {
505 nsCOMPtr<nsIRunnable> event =
506 NewRunnableMethod("storage::Connection::synchronousClose", this,
507 &Connection::synchronousClose);
508 if (NS_FAILED(
509 threadOpenedOn->Dispatch(event.forget(), NS_DISPATCH_NORMAL))) {
510 // The target thread was dead and so we've just leaked our runnable.
511 // This should not happen because our non-main-thread consumers should
512 // be explicitly closing their connections, not relying on us to close
513 // them for them. (It's okay to let a statement go out of scope for
514 // automatic cleanup, but not a Connection.)
515 MOZ_ASSERT(false,
516 "Leaked Connection::synchronousClose(), ownership fail.");
517 Unused << synchronousClose();
521 // This will drop its strong reference right here, right now.
522 mStorageService->unregisterConnection(this);
524 } else if (0 == count) {
525 mRefCnt = 1; /* stabilize */
526 #if 0 /* enable this to find non-threadsafe destructors: */
527 NS_ASSERT_OWNINGTHREAD(Connection);
528 #endif
529 delete (this);
530 return 0;
532 return count;
535 int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
536 int32_t* aMaxValue) {
537 MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
538 int curr = 0, max = 0;
539 DebugOnly<int> rc =
540 ::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
541 MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
542 if (aMaxValue) *aMaxValue = max;
543 return curr;
546 nsIEventTarget* Connection::getAsyncExecutionTarget() {
547 NS_ENSURE_TRUE(threadOpenedOn == NS_GetCurrentThread(), nullptr);
549 // Don't return the asynchronous thread if we are shutting down.
550 if (mAsyncExecutionThreadShuttingDown) {
551 return nullptr;
554 // Create the async thread if there's none yet.
555 if (!mAsyncExecutionThread) {
556 static nsThreadPoolNaming naming;
557 nsresult rv = NS_NewNamedThread(naming.GetNextThreadName("mozStorage"),
558 getter_AddRefs(mAsyncExecutionThread));
559 if (NS_FAILED(rv)) {
560 NS_WARNING("Failed to create async thread.");
561 return nullptr;
563 mAsyncExecutionThread->SetNameForWakeupTelemetry("mozStorage (all)"_ns);
566 return mAsyncExecutionThread;
569 void Connection::RecordOpenStatus(nsresult rv) {
570 nsCString histogramKey = mTelemetryFilename;
572 if (histogramKey.IsEmpty()) {
573 histogramKey.AssignLiteral("unknown");
576 if (NS_SUCCEEDED(rv)) {
577 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
578 return;
581 switch (rv) {
582 case NS_ERROR_FILE_CORRUPTED:
583 AccumulateCategoricalKeyed(histogramKey,
584 LABELS_SQLITE_STORE_OPEN::corrupt);
585 break;
586 case NS_ERROR_STORAGE_IOERR:
587 AccumulateCategoricalKeyed(histogramKey,
588 LABELS_SQLITE_STORE_OPEN::diskio);
589 break;
590 case NS_ERROR_FILE_ACCESS_DENIED:
591 case NS_ERROR_FILE_IS_LOCKED:
592 case NS_ERROR_FILE_READ_ONLY:
593 AccumulateCategoricalKeyed(histogramKey,
594 LABELS_SQLITE_STORE_OPEN::access);
595 break;
596 case NS_ERROR_FILE_NO_DEVICE_SPACE:
597 AccumulateCategoricalKeyed(histogramKey,
598 LABELS_SQLITE_STORE_OPEN::diskspace);
599 break;
600 default:
601 AccumulateCategoricalKeyed(histogramKey,
602 LABELS_SQLITE_STORE_OPEN::failure);
606 void Connection::RecordQueryStatus(int srv) {
607 nsCString histogramKey = mTelemetryFilename;
609 if (histogramKey.IsEmpty()) {
610 histogramKey.AssignLiteral("unknown");
613 switch (srv) {
614 case SQLITE_OK:
615 case SQLITE_ROW:
616 case SQLITE_DONE:
618 // Note that these are returned when we intentionally cancel a statement so
619 // they aren't indicating a failure.
620 case SQLITE_ABORT:
621 case SQLITE_INTERRUPT:
622 AccumulateCategoricalKeyed(histogramKey,
623 LABELS_SQLITE_STORE_QUERY::success);
624 break;
625 case SQLITE_CORRUPT:
626 case SQLITE_NOTADB:
627 AccumulateCategoricalKeyed(histogramKey,
628 LABELS_SQLITE_STORE_QUERY::corrupt);
629 break;
630 case SQLITE_PERM:
631 case SQLITE_CANTOPEN:
632 case SQLITE_LOCKED:
633 case SQLITE_READONLY:
634 AccumulateCategoricalKeyed(histogramKey,
635 LABELS_SQLITE_STORE_QUERY::access);
636 break;
637 case SQLITE_IOERR:
638 case SQLITE_NOLFS:
639 AccumulateCategoricalKeyed(histogramKey,
640 LABELS_SQLITE_STORE_QUERY::diskio);
641 break;
642 case SQLITE_FULL:
643 case SQLITE_TOOBIG:
644 AccumulateCategoricalKeyed(histogramKey,
645 LABELS_SQLITE_STORE_OPEN::diskspace);
646 break;
647 case SQLITE_CONSTRAINT:
648 case SQLITE_RANGE:
649 case SQLITE_MISMATCH:
650 case SQLITE_MISUSE:
651 AccumulateCategoricalKeyed(histogramKey,
652 LABELS_SQLITE_STORE_OPEN::misuse);
653 break;
654 case SQLITE_BUSY:
655 AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::busy);
656 break;
657 default:
658 AccumulateCategoricalKeyed(histogramKey,
659 LABELS_SQLITE_STORE_QUERY::failure);
663 nsresult Connection::initialize(const nsACString& aStorageKey,
664 const nsACString& aName) {
665 MOZ_ASSERT(aStorageKey.Equals(kMozStorageMemoryStorageKey));
666 NS_ASSERTION(!connectionReady(),
667 "Initialize called on already opened database!");
668 MOZ_ASSERT(!mIgnoreLockingMode, "Can't ignore locking on an in-memory db.");
669 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
671 mStorageKey = aStorageKey;
672 mName = aName;
674 // in memory database requested, sqlite uses a magic file name
676 const nsAutoCString path =
677 mName.IsEmpty() ? nsAutoCString(":memory:"_ns)
678 : "file:"_ns + mName + "?mode=memory&cache=shared"_ns;
680 mTelemetryFilename.AssignLiteral(":memory:");
682 int srv = ::sqlite3_open_v2(path.get(), &mDBConn, mFlags,
683 GetTelemetryVFSName(true));
684 if (srv != SQLITE_OK) {
685 mDBConn = nullptr;
686 nsresult rv = convertResultCode(srv);
687 RecordOpenStatus(rv);
688 return rv;
691 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
692 srv =
693 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
694 MOZ_ASSERT(srv == SQLITE_OK,
695 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
696 #endif
698 // Do not set mDatabaseFile or mFileURL here since this is a "memory"
699 // database.
701 nsresult rv = initializeInternal();
702 RecordOpenStatus(rv);
703 NS_ENSURE_SUCCESS(rv, rv);
705 return NS_OK;
708 nsresult Connection::initialize(nsIFile* aDatabaseFile) {
709 NS_ASSERTION(aDatabaseFile, "Passed null file!");
710 NS_ASSERTION(!connectionReady(),
711 "Initialize called on already opened database!");
712 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
714 // Do not set mFileURL here since this is database does not have an associated
715 // URL.
716 mDatabaseFile = aDatabaseFile;
717 aDatabaseFile->GetNativeLeafName(mTelemetryFilename);
719 nsAutoString path;
720 nsresult rv = aDatabaseFile->GetPath(path);
721 NS_ENSURE_SUCCESS(rv, rv);
723 #ifdef XP_WIN
724 static const char* sIgnoreLockingVFS = "win32-none";
725 #else
726 static const char* sIgnoreLockingVFS = "unix-none";
727 #endif
729 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
730 int srv;
731 if (mIgnoreLockingMode) {
732 exclusive = false;
733 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
734 sIgnoreLockingVFS);
735 } else {
736 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
737 GetTelemetryVFSName(exclusive));
738 if (exclusive && (srv == SQLITE_LOCKED || srv == SQLITE_BUSY)) {
739 // Retry without trying to get an exclusive lock.
740 exclusive = false;
741 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn,
742 mFlags, GetTelemetryVFSName(false));
745 if (srv != SQLITE_OK) {
746 mDBConn = nullptr;
747 rv = convertResultCode(srv);
748 RecordOpenStatus(rv);
749 return rv;
752 rv = initializeInternal();
753 if (exclusive &&
754 (rv == NS_ERROR_STORAGE_BUSY || rv == NS_ERROR_FILE_IS_LOCKED)) {
755 // Usually SQLite will fail to acquire an exclusive lock on opening, but in
756 // some cases it may successfully open the database and then lock on the
757 // first query execution. When initializeInternal fails it closes the
758 // connection, so we can try to restart it in non-exclusive mode.
759 srv = ::sqlite3_open_v2(NS_ConvertUTF16toUTF8(path).get(), &mDBConn, mFlags,
760 GetTelemetryVFSName(false));
761 if (srv == SQLITE_OK) {
762 rv = initializeInternal();
766 RecordOpenStatus(rv);
767 NS_ENSURE_SUCCESS(rv, rv);
769 return NS_OK;
772 nsresult Connection::initialize(nsIFileURL* aFileURL,
773 const nsACString& aTelemetryFilename) {
774 NS_ASSERTION(aFileURL, "Passed null file URL!");
775 NS_ASSERTION(!connectionReady(),
776 "Initialize called on already opened database!");
777 AUTO_PROFILER_LABEL("Connection::initialize", OTHER);
779 nsCOMPtr<nsIFile> databaseFile;
780 nsresult rv = aFileURL->GetFile(getter_AddRefs(databaseFile));
781 NS_ENSURE_SUCCESS(rv, rv);
783 // Set both mDatabaseFile and mFileURL here.
784 mFileURL = aFileURL;
785 mDatabaseFile = databaseFile;
787 if (!aTelemetryFilename.IsEmpty()) {
788 mTelemetryFilename = aTelemetryFilename;
789 } else {
790 databaseFile->GetNativeLeafName(mTelemetryFilename);
793 nsAutoCString spec;
794 rv = aFileURL->GetSpec(spec);
795 NS_ENSURE_SUCCESS(rv, rv);
797 bool exclusive = StaticPrefs::storage_sqlite_exclusiveLock_enabled();
799 // If there is a key specified, we need to use the obfuscating VFS.
800 nsAutoCString query;
801 rv = aFileURL->GetQuery(query);
802 NS_ENSURE_SUCCESS(rv, rv);
803 const char* const vfs =
804 URLParams::Parse(query,
805 [](const nsAString& aName, const nsAString& aValue) {
806 return aName.EqualsLiteral("key");
808 ? GetObfuscatingVFSName()
809 : GetTelemetryVFSName(exclusive);
810 int srv = ::sqlite3_open_v2(spec.get(), &mDBConn, mFlags, vfs);
811 if (srv != SQLITE_OK) {
812 mDBConn = nullptr;
813 rv = convertResultCode(srv);
814 RecordOpenStatus(rv);
815 return rv;
818 rv = initializeInternal();
819 RecordOpenStatus(rv);
820 NS_ENSURE_SUCCESS(rv, rv);
822 return NS_OK;
825 nsresult Connection::initializeInternal() {
826 MOZ_ASSERT(mDBConn);
827 auto guard = MakeScopeExit([&]() { initializeFailed(); });
829 mConnectionClosed = false;
831 #ifdef MOZ_SQLITE_FTS3_TOKENIZER
832 DebugOnly<int> srv2 =
833 ::sqlite3_db_config(mDBConn, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
834 MOZ_ASSERT(srv2 == SQLITE_OK,
835 "SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER should be enabled");
836 #endif
838 MOZ_ASSERT(!mTelemetryFilename.IsEmpty(),
839 "A telemetry filename should have been set by now.");
841 // Properly wrap the database handle's mutex.
842 sharedDBMutex.initWithMutex(sqlite3_db_mutex(mDBConn));
844 // SQLite tracing can slow down queries (especially long queries)
845 // significantly. Don't trace unless the user is actively monitoring SQLite.
846 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
847 ::sqlite3_trace_v2(mDBConn, SQLITE_TRACE_STMT | SQLITE_TRACE_PROFILE,
848 tracefunc, this);
850 MOZ_LOG(
851 gStorageLog, LogLevel::Debug,
852 ("Opening connection to '%s' (%p)", mTelemetryFilename.get(), this));
855 int64_t pageSize = Service::kDefaultPageSize;
857 // Set page_size to the preferred default value. This is effective only if
858 // the database has just been created, otherwise, if the database does not
859 // use WAL journal mode, a VACUUM operation will updated its page_size.
860 nsAutoCString pageSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
861 "PRAGMA page_size = ");
862 pageSizeQuery.AppendInt(pageSize);
863 int srv = executeSql(mDBConn, pageSizeQuery.get());
864 if (srv != SQLITE_OK) {
865 return convertResultCode(srv);
868 // Setting the cache_size forces the database open, verifying if it is valid
869 // or corrupt. So this is executed regardless it being actually needed.
870 // The cache_size is calculated from the actual page_size, to save memory.
871 nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
872 "PRAGMA cache_size = ");
873 cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
874 srv = executeSql(mDBConn, cacheSizeQuery.get());
875 if (srv != SQLITE_OK) {
876 return convertResultCode(srv);
879 // Register our built-in SQL functions.
880 srv = registerFunctions(mDBConn);
881 if (srv != SQLITE_OK) {
882 return convertResultCode(srv);
885 // Register our built-in SQL collating sequences.
886 srv = registerCollations(mDBConn, mStorageService);
887 if (srv != SQLITE_OK) {
888 return convertResultCode(srv);
891 // Set the default synchronous value. Each consumer can switch this
892 // accordingly to their needs.
893 #if defined(ANDROID)
894 // Android prefers synchronous = OFF for performance reasons.
895 Unused << ExecuteSimpleSQL("PRAGMA synchronous = OFF;"_ns);
896 #else
897 // Normal is the suggested value for WAL journals.
898 Unused << ExecuteSimpleSQL("PRAGMA synchronous = NORMAL;"_ns);
899 #endif
901 // Initialization succeeded, we can stop guarding for failures.
902 guard.release();
903 return NS_OK;
906 nsresult Connection::initializeOnAsyncThread(nsIFile* aStorageFile) {
907 MOZ_ASSERT(threadOpenedOn != NS_GetCurrentThread());
908 nsresult rv = aStorageFile
909 ? initialize(aStorageFile)
910 : initialize(kMozStorageMemoryStorageKey, VoidCString());
911 if (NS_FAILED(rv)) {
912 // Shutdown the async thread, since initialization failed.
913 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
914 mAsyncExecutionThreadShuttingDown = true;
915 nsCOMPtr<nsIRunnable> event =
916 NewRunnableMethod("Connection::shutdownAsyncThread", this,
917 &Connection::shutdownAsyncThread);
918 Unused << NS_DispatchToMainThread(event);
920 return rv;
923 void Connection::initializeFailed() {
925 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
926 mConnectionClosed = true;
928 MOZ_ALWAYS_TRUE(::sqlite3_close(mDBConn) == SQLITE_OK);
929 mDBConn = nullptr;
930 sharedDBMutex.destroy();
933 nsresult Connection::databaseElementExists(
934 enum DatabaseElementType aElementType, const nsACString& aElementName,
935 bool* _exists) {
936 if (!connectionReady()) {
937 return NS_ERROR_NOT_AVAILABLE;
939 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
940 if (NS_FAILED(rv)) {
941 return rv;
944 // When constructing the query, make sure to SELECT the correct db's
945 // sqlite_master if the user is prefixing the element with a specific db. ex:
946 // sample.test
947 nsCString query("SELECT name FROM (SELECT * FROM ");
948 nsDependentCSubstring element;
949 int32_t ind = aElementName.FindChar('.');
950 if (ind == kNotFound) {
951 element.Assign(aElementName);
952 } else {
953 nsDependentCSubstring db(Substring(aElementName, 0, ind + 1));
954 element.Assign(Substring(aElementName, ind + 1, aElementName.Length()));
955 query.Append(db);
957 query.AppendLiteral(
958 "sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type = "
959 "'");
961 switch (aElementType) {
962 case INDEX:
963 query.AppendLiteral("index");
964 break;
965 case TABLE:
966 query.AppendLiteral("table");
967 break;
969 query.AppendLiteral("' AND name ='");
970 query.Append(element);
971 query.Append('\'');
973 sqlite3_stmt* stmt;
974 int srv = prepareStatement(mDBConn, query, &stmt);
975 if (srv != SQLITE_OK) {
976 RecordQueryStatus(srv);
977 return convertResultCode(srv);
980 srv = stepStatement(mDBConn, stmt);
981 // we just care about the return value from step
982 (void)::sqlite3_finalize(stmt);
984 RecordQueryStatus(srv);
986 if (srv == SQLITE_ROW) {
987 *_exists = true;
988 return NS_OK;
990 if (srv == SQLITE_DONE) {
991 *_exists = false;
992 return NS_OK;
995 return convertResultCode(srv);
998 bool Connection::findFunctionByInstance(mozIStorageFunction* aInstance) {
999 sharedDBMutex.assertCurrentThreadOwns();
1001 for (auto iter = mFunctions.Iter(); !iter.Done(); iter.Next()) {
1002 if (iter.UserData().function == aInstance) {
1003 return true;
1006 return false;
1009 /* static */
1010 int Connection::sProgressHelper(void* aArg) {
1011 Connection* _this = static_cast<Connection*>(aArg);
1012 return _this->progressHandler();
1015 int Connection::progressHandler() {
1016 sharedDBMutex.assertCurrentThreadOwns();
1017 if (mProgressHandler) {
1018 bool result;
1019 nsresult rv = mProgressHandler->OnProgress(this, &result);
1020 if (NS_FAILED(rv)) return 0; // Don't break request
1021 return result ? 1 : 0;
1023 return 0;
1026 nsresult Connection::setClosedState() {
1027 // Ensure that we are on the correct thread to close the database.
1028 bool onOpenedThread;
1029 nsresult rv = threadOpenedOn->IsOnCurrentThread(&onOpenedThread);
1030 NS_ENSURE_SUCCESS(rv, rv);
1031 if (!onOpenedThread) {
1032 NS_ERROR("Must close the database on the thread that you opened it with!");
1033 return NS_ERROR_UNEXPECTED;
1036 // Flag that we are shutting down the async thread, so that
1037 // getAsyncExecutionTarget knows not to expose/create the async thread.
1039 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1040 NS_ENSURE_FALSE(mAsyncExecutionThreadShuttingDown, NS_ERROR_UNEXPECTED);
1041 mAsyncExecutionThreadShuttingDown = true;
1043 // Set the property to null before closing the connection, otherwise the
1044 // other functions in the module may try to use the connection after it is
1045 // closed.
1046 mDBConn = nullptr;
1048 return NS_OK;
1051 bool Connection::operationSupported(ConnectionOperation aOperationType) {
1052 if (aOperationType == ASYNCHRONOUS) {
1053 // Async operations are supported for all connections, on any thread.
1054 return true;
1056 // Sync operations are supported for sync connections (on any thread), and
1057 // async connections on a background thread.
1058 MOZ_ASSERT(aOperationType == SYNCHRONOUS);
1059 return mSupportedOperations == SYNCHRONOUS || !NS_IsMainThread();
1062 nsresult Connection::ensureOperationSupported(
1063 ConnectionOperation aOperationType) {
1064 if (NS_WARN_IF(!operationSupported(aOperationType))) {
1065 #ifdef DEBUG
1066 if (NS_IsMainThread()) {
1067 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1068 Unused << xpc->DebugDumpJSStack(false, false, false);
1070 #endif
1071 MOZ_ASSERT(false,
1072 "Don't use async connections synchronously on the main thread");
1073 return NS_ERROR_NOT_AVAILABLE;
1075 return NS_OK;
1078 bool Connection::isConnectionReadyOnThisThread() {
1079 MOZ_ASSERT_IF(connectionReady(), !mConnectionClosed);
1080 if (mAsyncExecutionThread && mAsyncExecutionThread->IsOnCurrentThread()) {
1081 return true;
1083 return connectionReady();
1086 bool Connection::isClosing() {
1087 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1088 return mAsyncExecutionThreadShuttingDown && !mConnectionClosed;
1091 bool Connection::isClosed() {
1092 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1093 return mConnectionClosed;
1096 bool Connection::isClosed(MutexAutoLock& lock) { return mConnectionClosed; }
1098 bool Connection::isAsyncExecutionThreadAvailable() {
1099 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1100 return mAsyncExecutionThread && !mAsyncExecutionThreadShuttingDown;
1103 void Connection::shutdownAsyncThread() {
1104 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1105 MOZ_ASSERT(mAsyncExecutionThread);
1106 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown);
1108 MOZ_ALWAYS_SUCCEEDS(mAsyncExecutionThread->Shutdown());
1109 mAsyncExecutionThread = nullptr;
1112 nsresult Connection::internalClose(sqlite3* aNativeConnection) {
1113 #ifdef DEBUG
1114 { // Make sure we have marked our async thread as shutting down.
1115 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1116 MOZ_ASSERT(mAsyncExecutionThreadShuttingDown,
1117 "Did not call setClosedState!");
1118 MOZ_ASSERT(!isClosed(lockedScope), "Unexpected closed state");
1120 #endif // DEBUG
1122 if (MOZ_LOG_TEST(gStorageLog, LogLevel::Debug)) {
1123 nsAutoCString leafName(":memory");
1124 if (mDatabaseFile) (void)mDatabaseFile->GetNativeLeafName(leafName);
1125 MOZ_LOG(gStorageLog, LogLevel::Debug,
1126 ("Closing connection to '%s'", leafName.get()));
1129 // At this stage, we may still have statements that need to be
1130 // finalized. Attempt to close the database connection. This will
1131 // always disconnect any virtual tables and cleanly finalize their
1132 // internal statements. Once this is done, closing may fail due to
1133 // unfinalized client statements, in which case we need to finalize
1134 // these statements and close again.
1136 MutexAutoLock lockedScope(sharedAsyncExecutionMutex);
1137 mConnectionClosed = true;
1140 // Nothing else needs to be done if we don't have a connection here.
1141 if (!aNativeConnection) return NS_OK;
1143 int srv = ::sqlite3_close(aNativeConnection);
1145 if (srv == SQLITE_BUSY) {
1147 // Nothing else should change the connection or statements status until we
1148 // are done here.
1149 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1150 // We still have non-finalized statements. Finalize them.
1151 sqlite3_stmt* stmt = nullptr;
1152 while ((stmt = ::sqlite3_next_stmt(aNativeConnection, stmt))) {
1153 MOZ_LOG(gStorageLog, LogLevel::Debug,
1154 ("Auto-finalizing SQL statement '%s' (%p)", ::sqlite3_sql(stmt),
1155 stmt));
1157 #ifdef DEBUG
1158 SmprintfPointer msg = ::mozilla::Smprintf(
1159 "SQL statement '%s' (%p) should have been finalized before closing "
1160 "the connection",
1161 ::sqlite3_sql(stmt), stmt);
1162 NS_WARNING(msg.get());
1163 #endif // DEBUG
1165 srv = ::sqlite3_finalize(stmt);
1167 #ifdef DEBUG
1168 if (srv != SQLITE_OK) {
1169 SmprintfPointer msg = ::mozilla::Smprintf(
1170 "Could not finalize SQL statement (%p)", stmt);
1171 NS_WARNING(msg.get());
1173 #endif // DEBUG
1175 // Ensure that the loop continues properly, whether closing has
1176 // succeeded or not.
1177 if (srv == SQLITE_OK) {
1178 stmt = nullptr;
1181 // Scope exiting will unlock the mutex before we invoke sqlite3_close()
1182 // again, since Sqlite will try to acquire it.
1185 // Now that all statements have been finalized, we
1186 // should be able to close.
1187 srv = ::sqlite3_close(aNativeConnection);
1188 MOZ_ASSERT(false,
1189 "Had to forcibly close the database connection because not all "
1190 "the statements have been finalized.");
1193 if (srv == SQLITE_OK) {
1194 sharedDBMutex.destroy();
1195 } else {
1196 MOZ_ASSERT(false,
1197 "sqlite3_close failed. There are probably outstanding "
1198 "statements that are listed above!");
1201 return convertResultCode(srv);
1204 nsCString Connection::getFilename() { return mTelemetryFilename; }
1206 int Connection::stepStatement(sqlite3* aNativeConnection,
1207 sqlite3_stmt* aStatement) {
1208 MOZ_ASSERT(aStatement);
1210 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::stepStatement", OTHER,
1211 ::sqlite3_sql(aStatement));
1213 bool checkedMainThread = false;
1214 TimeStamp startTime = TimeStamp::Now();
1216 // The connection may have been closed if the executing statement has been
1217 // created and cached after a call to asyncClose() but before the actual
1218 // sqlite3_close(). This usually happens when other tasks using cached
1219 // statements are asynchronously scheduled for execution and any of them ends
1220 // up after asyncClose. See bug 728653 for details.
1221 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1223 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1225 int srv;
1226 while ((srv = ::sqlite3_step(aStatement)) == SQLITE_LOCKED_SHAREDCACHE) {
1227 if (!checkedMainThread) {
1228 checkedMainThread = true;
1229 if (::NS_IsMainThread()) {
1230 NS_WARNING("We won't allow blocking on the main thread!");
1231 break;
1235 srv = WaitForUnlockNotify(aNativeConnection);
1236 if (srv != SQLITE_OK) {
1237 break;
1240 ::sqlite3_reset(aStatement);
1243 // Report very slow SQL statements to Telemetry
1244 TimeDuration duration = TimeStamp::Now() - startTime;
1245 const uint32_t threshold = NS_IsMainThread()
1246 ? Telemetry::kSlowSQLThresholdForMainThread
1247 : Telemetry::kSlowSQLThresholdForHelperThreads;
1248 if (duration.ToMilliseconds() >= threshold) {
1249 nsDependentCString statementString(::sqlite3_sql(aStatement));
1250 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1251 duration.ToMilliseconds());
1254 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1255 // Drop off the extended result bits of the result code.
1256 return srv & 0xFF;
1259 int Connection::prepareStatement(sqlite3* aNativeConnection,
1260 const nsCString& aSQL, sqlite3_stmt** _stmt) {
1261 // We should not even try to prepare statements after the connection has
1262 // been closed.
1263 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1265 bool checkedMainThread = false;
1267 (void)::sqlite3_extended_result_codes(aNativeConnection, 1);
1269 int srv;
1270 while ((srv = ::sqlite3_prepare_v2(aNativeConnection, aSQL.get(), -1, _stmt,
1271 nullptr)) == SQLITE_LOCKED_SHAREDCACHE) {
1272 if (!checkedMainThread) {
1273 checkedMainThread = true;
1274 if (::NS_IsMainThread()) {
1275 NS_WARNING("We won't allow blocking on the main thread!");
1276 break;
1280 srv = WaitForUnlockNotify(aNativeConnection);
1281 if (srv != SQLITE_OK) {
1282 break;
1286 if (srv != SQLITE_OK) {
1287 nsCString warnMsg;
1288 warnMsg.AppendLiteral("The SQL statement '");
1289 warnMsg.Append(aSQL);
1290 warnMsg.AppendLiteral("' could not be compiled due to an error: ");
1291 warnMsg.Append(::sqlite3_errmsg(aNativeConnection));
1293 #ifdef DEBUG
1294 NS_WARNING(warnMsg.get());
1295 #endif
1296 MOZ_LOG(gStorageLog, LogLevel::Error, ("%s", warnMsg.get()));
1299 (void)::sqlite3_extended_result_codes(aNativeConnection, 0);
1300 // Drop off the extended result bits of the result code.
1301 int rc = srv & 0xFF;
1302 // sqlite will return OK on a comment only string and set _stmt to nullptr.
1303 // The callers of this function are used to only checking the return value,
1304 // so it is safer to return an error code.
1305 if (rc == SQLITE_OK && *_stmt == nullptr) {
1306 return SQLITE_MISUSE;
1309 return rc;
1312 int Connection::executeSql(sqlite3* aNativeConnection, const char* aSqlString) {
1313 if (!isConnectionReadyOnThisThread()) return SQLITE_MISUSE;
1315 AUTO_PROFILER_LABEL_DYNAMIC_CSTR("Connection::executeSql", OTHER, aSqlString);
1317 TimeStamp startTime = TimeStamp::Now();
1318 int srv =
1319 ::sqlite3_exec(aNativeConnection, aSqlString, nullptr, nullptr, nullptr);
1320 RecordQueryStatus(srv);
1322 // Report very slow SQL statements to Telemetry
1323 TimeDuration duration = TimeStamp::Now() - startTime;
1324 const uint32_t threshold = NS_IsMainThread()
1325 ? Telemetry::kSlowSQLThresholdForMainThread
1326 : Telemetry::kSlowSQLThresholdForHelperThreads;
1327 if (duration.ToMilliseconds() >= threshold) {
1328 nsDependentCString statementString(aSqlString);
1329 Telemetry::RecordSlowSQLStatement(statementString, mTelemetryFilename,
1330 duration.ToMilliseconds());
1333 return srv;
1336 ////////////////////////////////////////////////////////////////////////////////
1337 //// nsIInterfaceRequestor
1339 NS_IMETHODIMP
1340 Connection::GetInterface(const nsIID& aIID, void** _result) {
1341 if (aIID.Equals(NS_GET_IID(nsIEventTarget))) {
1342 nsIEventTarget* background = getAsyncExecutionTarget();
1343 NS_IF_ADDREF(background);
1344 *_result = background;
1345 return NS_OK;
1347 return NS_ERROR_NO_INTERFACE;
1350 ////////////////////////////////////////////////////////////////////////////////
1351 //// mozIStorageConnection
1353 NS_IMETHODIMP
1354 Connection::Close() {
1355 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1356 if (NS_FAILED(rv)) {
1357 return rv;
1359 return synchronousClose();
1362 nsresult Connection::synchronousClose() {
1363 if (!connectionReady()) {
1364 return NS_ERROR_NOT_INITIALIZED;
1367 #ifdef DEBUG
1368 // Since we're accessing mAsyncExecutionThread, we need to be on the opener
1369 // thread. We make this check outside of debug code below in setClosedState,
1370 // but this is here to be explicit.
1371 bool onOpenerThread = false;
1372 (void)threadOpenedOn->IsOnCurrentThread(&onOpenerThread);
1373 MOZ_ASSERT(onOpenerThread);
1374 #endif // DEBUG
1376 // Make sure we have not executed any asynchronous statements.
1377 // If this fails, the mDBConn may be left open, resulting in a leak.
1378 // We'll try to finalize the pending statements and close the connection.
1379 if (isAsyncExecutionThreadAvailable()) {
1380 #ifdef DEBUG
1381 if (NS_IsMainThread()) {
1382 nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
1383 Unused << xpc->DebugDumpJSStack(false, false, false);
1385 #endif
1386 MOZ_ASSERT(false,
1387 "Close() was invoked on a connection that executed asynchronous "
1388 "statements. "
1389 "Should have used asyncClose().");
1390 // Try to close the database regardless, to free up resources.
1391 Unused << SpinningSynchronousClose();
1392 return NS_ERROR_UNEXPECTED;
1395 // setClosedState nullifies our connection pointer, so we take a raw pointer
1396 // off it, to pass it through the close procedure.
1397 sqlite3* nativeConn = mDBConn;
1398 nsresult rv = setClosedState();
1399 NS_ENSURE_SUCCESS(rv, rv);
1401 return internalClose(nativeConn);
1404 NS_IMETHODIMP
1405 Connection::SpinningSynchronousClose() {
1406 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1407 if (NS_FAILED(rv)) {
1408 return rv;
1410 if (threadOpenedOn != NS_GetCurrentThread()) {
1411 return NS_ERROR_NOT_SAME_THREAD;
1414 // As currently implemented, we can't spin to wait for an existing AsyncClose.
1415 // Our only existing caller will never have called close; assert if misused
1416 // so that no new callers assume this works after an AsyncClose.
1417 MOZ_DIAGNOSTIC_ASSERT(connectionReady());
1418 if (!connectionReady()) {
1419 return NS_ERROR_UNEXPECTED;
1422 RefPtr<CloseListener> listener = new CloseListener();
1423 rv = AsyncClose(listener);
1424 NS_ENSURE_SUCCESS(rv, rv);
1425 MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return listener->mClosed; }));
1426 MOZ_ASSERT(isClosed(), "The connection should be closed at this point");
1428 return rv;
1431 NS_IMETHODIMP
1432 Connection::AsyncClose(mozIStorageCompletionCallback* aCallback) {
1433 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1434 // Check if AsyncClose or Close were already invoked.
1435 if (!connectionReady()) {
1436 return NS_ERROR_NOT_INITIALIZED;
1438 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1439 if (NS_FAILED(rv)) {
1440 return rv;
1443 // The two relevant factors at this point are whether we have a database
1444 // connection and whether we have an async execution thread. Here's what the
1445 // states mean and how we handle them:
1447 // - (mDBConn && asyncThread): The expected case where we are either an
1448 // async connection or a sync connection that has been used asynchronously.
1449 // Either way the caller must call us and not Close(). Nothing surprising
1450 // about this. We'll dispatch AsyncCloseConnection to the already-existing
1451 // async thread.
1453 // - (mDBConn && !asyncThread): A somewhat unusual case where the caller
1454 // opened the connection synchronously and was planning to use it
1455 // asynchronously, but never got around to using it asynchronously before
1456 // needing to shutdown. This has been observed to happen for the cookie
1457 // service in a case where Firefox shuts itself down almost immediately
1458 // after startup (for unknown reasons). In the Firefox shutdown case,
1459 // we may also fail to create a new async execution thread if one does not
1460 // already exist. (nsThreadManager will refuse to create new threads when
1461 // it has already been told to shutdown.) As such, we need to handle a
1462 // failure to create the async execution thread by falling back to
1463 // synchronous Close() and also dispatching the completion callback because
1464 // at least Places likes to spin a nested event loop that depends on the
1465 // callback being invoked.
1467 // Note that we have considered not trying to spin up the async execution
1468 // thread in this case if it does not already exist, but the overhead of
1469 // thread startup (if successful) is significantly less expensive than the
1470 // worst-case potential I/O hit of synchronously closing a database when we
1471 // could close it asynchronously.
1473 // - (!mDBConn && asyncThread): This happens in some but not all cases where
1474 // OpenAsyncDatabase encountered a problem opening the database. If it
1475 // happened in all cases AsyncInitDatabase would just shut down the thread
1476 // directly and we would avoid this case. But it doesn't, so for simplicity
1477 // and consistency AsyncCloseConnection knows how to handle this and we
1478 // act like this was the (mDBConn && asyncThread) case in this method.
1480 // - (!mDBConn && !asyncThread): The database was never successfully opened or
1481 // Close() or AsyncClose() has already been called (at least) once. This is
1482 // undeniably a misuse case by the caller. We could optimize for this
1483 // case by adding an additional check of mAsyncExecutionThread without using
1484 // getAsyncExecutionTarget() to avoid wastefully creating a thread just to
1485 // shut it down. But this complicates the method for broken caller code
1486 // whereas we're still correct and safe without the special-case.
1487 nsIEventTarget* asyncThread = getAsyncExecutionTarget();
1489 // Create our callback event if we were given a callback. This will
1490 // eventually be dispatched in all cases, even if we fall back to Close() and
1491 // the database wasn't open and we return an error. The rationale is that
1492 // no existing consumer checks our return value and several of them like to
1493 // spin nested event loops until the callback fires. Given that, it seems
1494 // preferable for us to dispatch the callback in all cases. (Except the
1495 // wrong thread misuse case we bailed on up above. But that's okay because
1496 // that is statically wrong whereas these edge cases are dynamic.)
1497 nsCOMPtr<nsIRunnable> completeEvent;
1498 if (aCallback) {
1499 completeEvent = newCompletionEvent(aCallback);
1502 if (!asyncThread) {
1503 // We were unable to create an async thread, so we need to fall back to
1504 // using normal Close(). Since there is no async thread, Close() will
1505 // not complain about that. (Close() may, however, complain if the
1506 // connection is closed, but that's okay.)
1507 if (completeEvent) {
1508 // Closing the database is more important than returning an error code
1509 // about a failure to dispatch, especially because all existing native
1510 // callers ignore our return value.
1511 Unused << NS_DispatchToMainThread(completeEvent.forget());
1513 MOZ_ALWAYS_SUCCEEDS(synchronousClose());
1514 // Return a success inconditionally here, since Close() is unlikely to fail
1515 // and we want to reassure the consumer that its callback will be invoked.
1516 return NS_OK;
1519 // setClosedState nullifies our connection pointer, so we take a raw pointer
1520 // off it, to pass it through the close procedure.
1521 sqlite3* nativeConn = mDBConn;
1522 rv = setClosedState();
1523 NS_ENSURE_SUCCESS(rv, rv);
1525 // Create and dispatch our close event to the background thread.
1526 nsCOMPtr<nsIRunnable> closeEvent =
1527 new AsyncCloseConnection(this, nativeConn, completeEvent);
1528 rv = asyncThread->Dispatch(closeEvent, NS_DISPATCH_NORMAL);
1529 NS_ENSURE_SUCCESS(rv, rv);
1531 return NS_OK;
1534 NS_IMETHODIMP
1535 Connection::AsyncClone(bool aReadOnly,
1536 mozIStorageCompletionCallback* aCallback) {
1537 AUTO_PROFILER_LABEL("Connection::AsyncClone", OTHER);
1539 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1540 if (!connectionReady()) {
1541 return NS_ERROR_NOT_INITIALIZED;
1543 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1544 if (NS_FAILED(rv)) {
1545 return rv;
1547 if (!mDatabaseFile) return NS_ERROR_UNEXPECTED;
1549 int flags = mFlags;
1550 if (aReadOnly) {
1551 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1552 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1553 // Turn off SQLITE_OPEN_CREATE.
1554 flags = (~SQLITE_OPEN_CREATE & flags);
1557 // The cloned connection will still implement the synchronous API, but throw
1558 // if any synchronous methods are called on the main thread.
1559 RefPtr<Connection> clone =
1560 new Connection(mStorageService, flags, ASYNCHRONOUS);
1562 RefPtr<AsyncInitializeClone> initEvent =
1563 new AsyncInitializeClone(this, clone, aReadOnly, aCallback);
1564 // Dispatch to our async thread, since the originating connection must remain
1565 // valid and open for the whole cloning process. This also ensures we are
1566 // properly serialized with a `close` operation, rather than race with it.
1567 nsCOMPtr<nsIEventTarget> target = getAsyncExecutionTarget();
1568 if (!target) {
1569 return NS_ERROR_UNEXPECTED;
1571 return target->Dispatch(initEvent, NS_DISPATCH_NORMAL);
1574 nsresult Connection::initializeClone(Connection* aClone, bool aReadOnly) {
1575 nsresult rv;
1576 if (!mStorageKey.IsEmpty()) {
1577 rv = aClone->initialize(mStorageKey, mName);
1578 } else if (mFileURL) {
1579 rv = aClone->initialize(mFileURL, mTelemetryFilename);
1580 } else {
1581 rv = aClone->initialize(mDatabaseFile);
1583 if (NS_FAILED(rv)) {
1584 return rv;
1587 auto guard = MakeScopeExit([&]() { aClone->initializeFailed(); });
1589 rv = aClone->SetDefaultTransactionType(mDefaultTransactionType);
1590 NS_ENSURE_SUCCESS(rv, rv);
1592 // Re-attach on-disk databases that were attached to the original connection.
1594 nsCOMPtr<mozIStorageStatement> stmt;
1595 rv = CreateStatement("PRAGMA database_list"_ns, getter_AddRefs(stmt));
1596 MOZ_ASSERT(NS_SUCCEEDED(rv));
1597 bool hasResult = false;
1598 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1599 nsAutoCString name;
1600 rv = stmt->GetUTF8String(1, name);
1601 if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("main") &&
1602 !name.EqualsLiteral("temp")) {
1603 nsCString path;
1604 rv = stmt->GetUTF8String(2, path);
1605 if (NS_SUCCEEDED(rv) && !path.IsEmpty()) {
1606 nsCOMPtr<mozIStorageStatement> attachStmt;
1607 rv = aClone->CreateStatement("ATTACH DATABASE :path AS "_ns + name,
1608 getter_AddRefs(attachStmt));
1609 MOZ_ASSERT(NS_SUCCEEDED(rv));
1610 rv = attachStmt->BindUTF8StringByName("path"_ns, path);
1611 MOZ_ASSERT(NS_SUCCEEDED(rv));
1612 rv = attachStmt->Execute();
1613 MOZ_ASSERT(NS_SUCCEEDED(rv),
1614 "couldn't re-attach database to cloned connection");
1620 // Copy over pragmas from the original connection.
1621 // LIMITATION WARNING! Many of these pragmas are actually scoped to the
1622 // schema ("main" and any other attached databases), and this implmentation
1623 // fails to propagate them. This is being addressed on trunk.
1624 static const char* pragmas[] = {
1625 "cache_size", "temp_store", "foreign_keys", "journal_size_limit",
1626 "synchronous", "wal_autocheckpoint", "busy_timeout"};
1627 for (auto& pragma : pragmas) {
1628 // Read-only connections just need cache_size and temp_store pragmas.
1629 if (aReadOnly && ::strcmp(pragma, "cache_size") != 0 &&
1630 ::strcmp(pragma, "temp_store") != 0) {
1631 continue;
1634 nsAutoCString pragmaQuery("PRAGMA ");
1635 pragmaQuery.Append(pragma);
1636 nsCOMPtr<mozIStorageStatement> stmt;
1637 rv = CreateStatement(pragmaQuery, getter_AddRefs(stmt));
1638 MOZ_ASSERT(NS_SUCCEEDED(rv));
1639 bool hasResult = false;
1640 if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1641 pragmaQuery.AppendLiteral(" = ");
1642 pragmaQuery.AppendInt(stmt->AsInt32(0));
1643 rv = aClone->ExecuteSimpleSQL(pragmaQuery);
1644 MOZ_ASSERT(NS_SUCCEEDED(rv));
1648 // Copy over temporary tables, triggers, and views from the original
1649 // connections. Entities in `sqlite_temp_master` are only visible to the
1650 // connection that created them.
1651 if (!aReadOnly) {
1652 rv = aClone->ExecuteSimpleSQL("BEGIN TRANSACTION"_ns);
1653 NS_ENSURE_SUCCESS(rv, rv);
1655 nsCOMPtr<mozIStorageStatement> stmt;
1656 rv = CreateStatement(nsLiteralCString("SELECT sql FROM sqlite_temp_master "
1657 "WHERE type IN ('table', 'view', "
1658 "'index', 'trigger')"),
1659 getter_AddRefs(stmt));
1660 // Propagate errors, because failing to copy triggers might cause schema
1661 // coherency issues when writing to the database from the cloned connection.
1662 NS_ENSURE_SUCCESS(rv, rv);
1663 bool hasResult = false;
1664 while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
1665 nsAutoCString query;
1666 rv = stmt->GetUTF8String(0, query);
1667 NS_ENSURE_SUCCESS(rv, rv);
1669 // The `CREATE` SQL statements in `sqlite_temp_master` omit the `TEMP`
1670 // keyword. We need to add it back, or we'll recreate temporary entities
1671 // as persistent ones. `sqlite_temp_master` also holds `CREATE INDEX`
1672 // statements, but those don't need `TEMP` keywords.
1673 if (StringBeginsWith(query, "CREATE TABLE "_ns) ||
1674 StringBeginsWith(query, "CREATE TRIGGER "_ns) ||
1675 StringBeginsWith(query, "CREATE VIEW "_ns)) {
1676 query.Replace(0, 6, "CREATE TEMP");
1679 rv = aClone->ExecuteSimpleSQL(query);
1680 NS_ENSURE_SUCCESS(rv, rv);
1683 rv = aClone->ExecuteSimpleSQL("COMMIT"_ns);
1684 NS_ENSURE_SUCCESS(rv, rv);
1687 // Copy any functions that have been added to this connection.
1688 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
1689 for (auto iter = mFunctions.Iter(); !iter.Done(); iter.Next()) {
1690 const nsACString& key = iter.Key();
1691 Connection::FunctionInfo data = iter.UserData();
1693 rv = aClone->CreateFunction(key, data.numArgs, data.function);
1694 if (NS_FAILED(rv)) {
1695 NS_WARNING("Failed to copy function to cloned connection");
1699 guard.release();
1700 return NS_OK;
1703 NS_IMETHODIMP
1704 Connection::Clone(bool aReadOnly, mozIStorageConnection** _connection) {
1705 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1707 AUTO_PROFILER_LABEL("Connection::Clone", OTHER);
1709 if (!connectionReady()) {
1710 return NS_ERROR_NOT_INITIALIZED;
1712 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1713 if (NS_FAILED(rv)) {
1714 return rv;
1717 int flags = mFlags;
1718 if (aReadOnly) {
1719 // Turn off SQLITE_OPEN_READWRITE, and set SQLITE_OPEN_READONLY.
1720 flags = (~SQLITE_OPEN_READWRITE & flags) | SQLITE_OPEN_READONLY;
1721 // Turn off SQLITE_OPEN_CREATE.
1722 flags = (~SQLITE_OPEN_CREATE & flags);
1725 RefPtr<Connection> clone =
1726 new Connection(mStorageService, flags, mSupportedOperations);
1728 rv = initializeClone(clone, aReadOnly);
1729 if (NS_FAILED(rv)) {
1730 return rv;
1733 NS_IF_ADDREF(*_connection = clone);
1734 return NS_OK;
1737 NS_IMETHODIMP
1738 Connection::Interrupt() {
1739 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1740 if (!connectionReady()) {
1741 return NS_ERROR_NOT_INITIALIZED;
1743 if (operationSupported(SYNCHRONOUS) || !(mFlags & SQLITE_OPEN_READONLY)) {
1744 // Interrupting a synchronous connection from the same thread doesn't make
1745 // sense, and read-write connections aren't safe to interrupt.
1746 return NS_ERROR_INVALID_ARG;
1748 ::sqlite3_interrupt(mDBConn);
1749 return NS_OK;
1752 NS_IMETHODIMP
1753 Connection::GetDefaultPageSize(int32_t* _defaultPageSize) {
1754 *_defaultPageSize = Service::kDefaultPageSize;
1755 return NS_OK;
1758 NS_IMETHODIMP
1759 Connection::GetConnectionReady(bool* _ready) {
1760 MOZ_ASSERT(threadOpenedOn == NS_GetCurrentThread());
1761 *_ready = connectionReady();
1762 return NS_OK;
1765 NS_IMETHODIMP
1766 Connection::GetDatabaseFile(nsIFile** _dbFile) {
1767 if (!connectionReady()) {
1768 return NS_ERROR_NOT_INITIALIZED;
1770 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1771 if (NS_FAILED(rv)) {
1772 return rv;
1775 NS_IF_ADDREF(*_dbFile = mDatabaseFile);
1777 return NS_OK;
1780 NS_IMETHODIMP
1781 Connection::GetLastInsertRowID(int64_t* _id) {
1782 if (!connectionReady()) {
1783 return NS_ERROR_NOT_INITIALIZED;
1785 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1786 if (NS_FAILED(rv)) {
1787 return rv;
1790 sqlite_int64 id = ::sqlite3_last_insert_rowid(mDBConn);
1791 *_id = id;
1793 return NS_OK;
1796 NS_IMETHODIMP
1797 Connection::GetAffectedRows(int32_t* _rows) {
1798 if (!connectionReady()) {
1799 return NS_ERROR_NOT_INITIALIZED;
1801 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1802 if (NS_FAILED(rv)) {
1803 return rv;
1806 *_rows = ::sqlite3_changes(mDBConn);
1808 return NS_OK;
1811 NS_IMETHODIMP
1812 Connection::GetLastError(int32_t* _error) {
1813 if (!connectionReady()) {
1814 return NS_ERROR_NOT_INITIALIZED;
1816 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1817 if (NS_FAILED(rv)) {
1818 return rv;
1821 *_error = ::sqlite3_errcode(mDBConn);
1823 return NS_OK;
1826 NS_IMETHODIMP
1827 Connection::GetLastErrorString(nsACString& _errorString) {
1828 if (!connectionReady()) {
1829 return NS_ERROR_NOT_INITIALIZED;
1831 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1832 if (NS_FAILED(rv)) {
1833 return rv;
1836 const char* serr = ::sqlite3_errmsg(mDBConn);
1837 _errorString.Assign(serr);
1839 return NS_OK;
1842 NS_IMETHODIMP
1843 Connection::GetSchemaVersion(int32_t* _version) {
1844 if (!connectionReady()) {
1845 return NS_ERROR_NOT_INITIALIZED;
1847 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1848 if (NS_FAILED(rv)) {
1849 return rv;
1852 nsCOMPtr<mozIStorageStatement> stmt;
1853 (void)CreateStatement("PRAGMA user_version"_ns, getter_AddRefs(stmt));
1854 NS_ENSURE_TRUE(stmt, NS_ERROR_OUT_OF_MEMORY);
1856 *_version = 0;
1857 bool hasResult;
1858 if (NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult)
1859 *_version = stmt->AsInt32(0);
1861 return NS_OK;
1864 NS_IMETHODIMP
1865 Connection::SetSchemaVersion(int32_t aVersion) {
1866 if (!connectionReady()) {
1867 return NS_ERROR_NOT_INITIALIZED;
1869 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1870 if (NS_FAILED(rv)) {
1871 return rv;
1874 nsAutoCString stmt("PRAGMA user_version = "_ns);
1875 stmt.AppendInt(aVersion);
1877 return ExecuteSimpleSQL(stmt);
1880 NS_IMETHODIMP
1881 Connection::CreateStatement(const nsACString& aSQLStatement,
1882 mozIStorageStatement** _stmt) {
1883 NS_ENSURE_ARG_POINTER(_stmt);
1884 if (!connectionReady()) {
1885 return NS_ERROR_NOT_INITIALIZED;
1887 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1888 if (NS_FAILED(rv)) {
1889 return rv;
1892 RefPtr<Statement> statement(new Statement());
1893 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1895 rv = statement->initialize(this, mDBConn, aSQLStatement);
1896 NS_ENSURE_SUCCESS(rv, rv);
1898 Statement* rawPtr;
1899 statement.forget(&rawPtr);
1900 *_stmt = rawPtr;
1901 return NS_OK;
1904 NS_IMETHODIMP
1905 Connection::CreateAsyncStatement(const nsACString& aSQLStatement,
1906 mozIStorageAsyncStatement** _stmt) {
1907 NS_ENSURE_ARG_POINTER(_stmt);
1908 if (!connectionReady()) {
1909 return NS_ERROR_NOT_INITIALIZED;
1911 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
1912 if (NS_FAILED(rv)) {
1913 return rv;
1916 RefPtr<AsyncStatement> statement(new AsyncStatement());
1917 NS_ENSURE_TRUE(statement, NS_ERROR_OUT_OF_MEMORY);
1919 rv = statement->initialize(this, mDBConn, aSQLStatement);
1920 NS_ENSURE_SUCCESS(rv, rv);
1922 AsyncStatement* rawPtr;
1923 statement.forget(&rawPtr);
1924 *_stmt = rawPtr;
1925 return NS_OK;
1928 NS_IMETHODIMP
1929 Connection::ExecuteSimpleSQL(const nsACString& aSQLStatement) {
1930 CHECK_MAINTHREAD_ABUSE();
1931 if (!connectionReady()) {
1932 return NS_ERROR_NOT_INITIALIZED;
1934 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
1935 if (NS_FAILED(rv)) {
1936 return rv;
1939 int srv = executeSql(mDBConn, PromiseFlatCString(aSQLStatement).get());
1940 return convertResultCode(srv);
1943 NS_IMETHODIMP
1944 Connection::ExecuteAsync(
1945 const nsTArray<RefPtr<mozIStorageBaseStatement>>& aStatements,
1946 mozIStorageStatementCallback* aCallback,
1947 mozIStoragePendingStatement** _handle) {
1948 nsTArray<StatementData> stmts(aStatements.Length());
1949 for (uint32_t i = 0; i < aStatements.Length(); i++) {
1950 nsCOMPtr<StorageBaseStatementInternal> stmt =
1951 do_QueryInterface(aStatements[i]);
1952 NS_ENSURE_STATE(stmt);
1954 // Obtain our StatementData.
1955 StatementData data;
1956 nsresult rv = stmt->getAsynchronousStatementData(data);
1957 NS_ENSURE_SUCCESS(rv, rv);
1959 NS_ASSERTION(stmt->getOwner() == this,
1960 "Statement must be from this database connection!");
1962 // Now append it to our array.
1963 stmts.AppendElement(data);
1966 // Dispatch to the background
1967 return AsyncExecuteStatements::execute(std::move(stmts), this, mDBConn,
1968 aCallback, _handle);
1971 NS_IMETHODIMP
1972 Connection::ExecuteSimpleSQLAsync(const nsACString& aSQLStatement,
1973 mozIStorageStatementCallback* aCallback,
1974 mozIStoragePendingStatement** _handle) {
1975 NS_ENSURE_TRUE(NS_IsMainThread(), NS_ERROR_NOT_SAME_THREAD);
1977 nsCOMPtr<mozIStorageAsyncStatement> stmt;
1978 nsresult rv = CreateAsyncStatement(aSQLStatement, getter_AddRefs(stmt));
1979 if (NS_FAILED(rv)) {
1980 return rv;
1983 nsCOMPtr<mozIStoragePendingStatement> pendingStatement;
1984 rv = stmt->ExecuteAsync(aCallback, getter_AddRefs(pendingStatement));
1985 if (NS_FAILED(rv)) {
1986 return rv;
1989 pendingStatement.forget(_handle);
1990 return rv;
1993 NS_IMETHODIMP
1994 Connection::TableExists(const nsACString& aTableName, bool* _exists) {
1995 return databaseElementExists(TABLE, aTableName, _exists);
1998 NS_IMETHODIMP
1999 Connection::IndexExists(const nsACString& aIndexName, bool* _exists) {
2000 return databaseElementExists(INDEX, aIndexName, _exists);
2003 NS_IMETHODIMP
2004 Connection::GetTransactionInProgress(bool* _inProgress) {
2005 if (!connectionReady()) {
2006 return NS_ERROR_NOT_INITIALIZED;
2008 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2009 if (NS_FAILED(rv)) {
2010 return rv;
2013 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2014 *_inProgress = transactionInProgress(lockedScope);
2015 return NS_OK;
2018 NS_IMETHODIMP
2019 Connection::GetDefaultTransactionType(int32_t* _type) {
2020 *_type = mDefaultTransactionType;
2021 return NS_OK;
2024 NS_IMETHODIMP
2025 Connection::SetDefaultTransactionType(int32_t aType) {
2026 NS_ENSURE_ARG_RANGE(aType, TRANSACTION_DEFERRED, TRANSACTION_EXCLUSIVE);
2027 mDefaultTransactionType = aType;
2028 return NS_OK;
2031 NS_IMETHODIMP
2032 Connection::GetVariableLimit(int32_t* _limit) {
2033 if (!connectionReady()) {
2034 return NS_ERROR_NOT_INITIALIZED;
2036 int limit = ::sqlite3_limit(mDBConn, SQLITE_LIMIT_VARIABLE_NUMBER, -1);
2037 if (limit < 0) {
2038 return NS_ERROR_UNEXPECTED;
2040 *_limit = limit;
2041 return NS_OK;
2044 NS_IMETHODIMP
2045 Connection::BeginTransaction() {
2046 if (!connectionReady()) {
2047 return NS_ERROR_NOT_INITIALIZED;
2049 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2050 if (NS_FAILED(rv)) {
2051 return rv;
2054 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2055 return beginTransactionInternal(lockedScope, mDBConn,
2056 mDefaultTransactionType);
2059 nsresult Connection::beginTransactionInternal(
2060 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
2061 int32_t aTransactionType) {
2062 if (transactionInProgress(aProofOfLock)) {
2063 return NS_ERROR_FAILURE;
2065 nsresult rv;
2066 switch (aTransactionType) {
2067 case TRANSACTION_DEFERRED:
2068 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN DEFERRED"));
2069 break;
2070 case TRANSACTION_IMMEDIATE:
2071 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN IMMEDIATE"));
2072 break;
2073 case TRANSACTION_EXCLUSIVE:
2074 rv = convertResultCode(executeSql(aNativeConnection, "BEGIN EXCLUSIVE"));
2075 break;
2076 default:
2077 return NS_ERROR_ILLEGAL_VALUE;
2079 return rv;
2082 NS_IMETHODIMP
2083 Connection::CommitTransaction() {
2084 if (!connectionReady()) {
2085 return NS_ERROR_NOT_INITIALIZED;
2087 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2088 if (NS_FAILED(rv)) {
2089 return rv;
2092 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2093 return commitTransactionInternal(lockedScope, mDBConn);
2096 nsresult Connection::commitTransactionInternal(
2097 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2098 if (!transactionInProgress(aProofOfLock)) {
2099 return NS_ERROR_UNEXPECTED;
2101 nsresult rv =
2102 convertResultCode(executeSql(aNativeConnection, "COMMIT TRANSACTION"));
2103 return rv;
2106 NS_IMETHODIMP
2107 Connection::RollbackTransaction() {
2108 if (!connectionReady()) {
2109 return NS_ERROR_NOT_INITIALIZED;
2111 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2112 if (NS_FAILED(rv)) {
2113 return rv;
2116 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2117 return rollbackTransactionInternal(lockedScope, mDBConn);
2120 nsresult Connection::rollbackTransactionInternal(
2121 const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection) {
2122 if (!transactionInProgress(aProofOfLock)) {
2123 return NS_ERROR_UNEXPECTED;
2126 nsresult rv =
2127 convertResultCode(executeSql(aNativeConnection, "ROLLBACK TRANSACTION"));
2128 return rv;
2131 NS_IMETHODIMP
2132 Connection::CreateTable(const char* aTableName, const char* aTableSchema) {
2133 if (!connectionReady()) {
2134 return NS_ERROR_NOT_INITIALIZED;
2136 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2137 if (NS_FAILED(rv)) {
2138 return rv;
2141 SmprintfPointer buf =
2142 ::mozilla::Smprintf("CREATE TABLE %s (%s)", aTableName, aTableSchema);
2143 if (!buf) return NS_ERROR_OUT_OF_MEMORY;
2145 int srv = executeSql(mDBConn, buf.get());
2147 return convertResultCode(srv);
2150 NS_IMETHODIMP
2151 Connection::CreateFunction(const nsACString& aFunctionName,
2152 int32_t aNumArguments,
2153 mozIStorageFunction* aFunction) {
2154 if (!connectionReady()) {
2155 return NS_ERROR_NOT_INITIALIZED;
2157 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2158 if (NS_FAILED(rv)) {
2159 return rv;
2162 // Check to see if this function is already defined. We only check the name
2163 // because a function can be defined with the same body but different names.
2164 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2165 NS_ENSURE_FALSE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2167 int srv = ::sqlite3_create_function(
2168 mDBConn, nsPromiseFlatCString(aFunctionName).get(), aNumArguments,
2169 SQLITE_ANY, aFunction, basicFunctionHelper, nullptr, nullptr);
2170 if (srv != SQLITE_OK) return convertResultCode(srv);
2172 FunctionInfo info = {aFunction, aNumArguments};
2173 mFunctions.Put(aFunctionName, info);
2175 return NS_OK;
2178 NS_IMETHODIMP
2179 Connection::RemoveFunction(const nsACString& aFunctionName) {
2180 if (!connectionReady()) {
2181 return NS_ERROR_NOT_INITIALIZED;
2183 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2184 if (NS_FAILED(rv)) {
2185 return rv;
2188 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2189 NS_ENSURE_TRUE(mFunctions.Get(aFunctionName, nullptr), NS_ERROR_FAILURE);
2191 int srv = ::sqlite3_create_function(
2192 mDBConn, nsPromiseFlatCString(aFunctionName).get(), 0, SQLITE_ANY,
2193 nullptr, nullptr, nullptr, nullptr);
2194 if (srv != SQLITE_OK) return convertResultCode(srv);
2196 mFunctions.Remove(aFunctionName);
2198 return NS_OK;
2201 NS_IMETHODIMP
2202 Connection::SetProgressHandler(int32_t aGranularity,
2203 mozIStorageProgressHandler* aHandler,
2204 mozIStorageProgressHandler** _oldHandler) {
2205 if (!connectionReady()) {
2206 return NS_ERROR_NOT_INITIALIZED;
2208 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2209 if (NS_FAILED(rv)) {
2210 return rv;
2213 // Return previous one
2214 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2215 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2217 if (!aHandler || aGranularity <= 0) {
2218 aHandler = nullptr;
2219 aGranularity = 0;
2221 mProgressHandler = aHandler;
2222 ::sqlite3_progress_handler(mDBConn, aGranularity, sProgressHelper, this);
2224 return NS_OK;
2227 NS_IMETHODIMP
2228 Connection::RemoveProgressHandler(mozIStorageProgressHandler** _oldHandler) {
2229 if (!connectionReady()) {
2230 return NS_ERROR_NOT_INITIALIZED;
2232 nsresult rv = ensureOperationSupported(ASYNCHRONOUS);
2233 if (NS_FAILED(rv)) {
2234 return rv;
2237 // Return previous one
2238 SQLiteMutexAutoLock lockedScope(sharedDBMutex);
2239 NS_IF_ADDREF(*_oldHandler = mProgressHandler);
2241 mProgressHandler = nullptr;
2242 ::sqlite3_progress_handler(mDBConn, 0, nullptr, nullptr);
2244 return NS_OK;
2247 NS_IMETHODIMP
2248 Connection::SetGrowthIncrement(int32_t aChunkSize,
2249 const nsACString& aDatabaseName) {
2250 if (!connectionReady()) {
2251 return NS_ERROR_NOT_INITIALIZED;
2253 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2254 if (NS_FAILED(rv)) {
2255 return rv;
2258 // Bug 597215: Disk space is extremely limited on Android
2259 // so don't preallocate space. This is also not effective
2260 // on log structured file systems used by Android devices
2261 #if !defined(ANDROID) && !defined(MOZ_PLATFORM_MAEMO)
2262 // Don't preallocate if less than 500MiB is available.
2263 int64_t bytesAvailable;
2264 rv = mDatabaseFile->GetDiskSpaceAvailable(&bytesAvailable);
2265 NS_ENSURE_SUCCESS(rv, rv);
2266 if (bytesAvailable < MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH) {
2267 return NS_ERROR_FILE_TOO_BIG;
2270 (void)::sqlite3_file_control(mDBConn,
2271 aDatabaseName.Length()
2272 ? nsPromiseFlatCString(aDatabaseName).get()
2273 : nullptr,
2274 SQLITE_FCNTL_CHUNK_SIZE, &aChunkSize);
2275 #endif
2276 return NS_OK;
2279 NS_IMETHODIMP
2280 Connection::EnableModule(const nsACString& aModuleName) {
2281 if (!connectionReady()) {
2282 return NS_ERROR_NOT_INITIALIZED;
2284 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2285 if (NS_FAILED(rv)) {
2286 return rv;
2289 for (auto& gModule : gModules) {
2290 struct Module* m = &gModule;
2291 if (aModuleName.Equals(m->name)) {
2292 int srv = m->registerFunc(mDBConn, m->name);
2293 if (srv != SQLITE_OK) return convertResultCode(srv);
2295 return NS_OK;
2299 return NS_ERROR_FAILURE;
2302 // Implemented in TelemetryVFS.cpp
2303 already_AddRefed<QuotaObject> GetQuotaObjectForFile(sqlite3_file* pFile);
2305 NS_IMETHODIMP
2306 Connection::GetQuotaObjects(QuotaObject** aDatabaseQuotaObject,
2307 QuotaObject** aJournalQuotaObject) {
2308 MOZ_ASSERT(aDatabaseQuotaObject);
2309 MOZ_ASSERT(aJournalQuotaObject);
2311 if (!connectionReady()) {
2312 return NS_ERROR_NOT_INITIALIZED;
2314 nsresult rv = ensureOperationSupported(SYNCHRONOUS);
2315 if (NS_FAILED(rv)) {
2316 return rv;
2319 sqlite3_file* file;
2320 int srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_FILE_POINTER,
2321 &file);
2322 if (srv != SQLITE_OK) {
2323 return convertResultCode(srv);
2326 RefPtr<QuotaObject> databaseQuotaObject = GetQuotaObjectForFile(file);
2327 if (NS_WARN_IF(!databaseQuotaObject)) {
2328 return NS_ERROR_FAILURE;
2331 srv = ::sqlite3_file_control(mDBConn, nullptr, SQLITE_FCNTL_JOURNAL_POINTER,
2332 &file);
2333 if (srv != SQLITE_OK) {
2334 return convertResultCode(srv);
2337 RefPtr<QuotaObject> journalQuotaObject = GetQuotaObjectForFile(file);
2338 if (NS_WARN_IF(!journalQuotaObject)) {
2339 return NS_ERROR_FAILURE;
2342 databaseQuotaObject.forget(aDatabaseQuotaObject);
2343 journalQuotaObject.forget(aJournalQuotaObject);
2344 return NS_OK;
2347 } // namespace mozilla::storage