Source code
Revision control
Copy as Markdown
Other Tools
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
#include "BaseVFS.h"
#include "ErrorList.h"
#include "nsError.h"
#include "nsThreadUtils.h"
#include "nsIFile.h"
#include "nsIFileURL.h"
#include "nsIXPConnect.h"
#include "mozilla/AppShutdown.h"
#include "mozilla/CheckedInt.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Mutex.h"
#include "mozilla/CondVar.h"
#include "mozilla/Attributes.h"
#include "mozilla/ErrorNames.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/quota/QuotaObject.h"
#include "mozilla/ScopeExit.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "mozilla/StaticPrefs_storage.h"
#include "mozIStorageCompletionCallback.h"
#include "mozIStorageFunction.h"
#include "mozStorageAsyncStatementExecution.h"
#include "mozStorageSQLFunctions.h"
#include "mozStorageConnection.h"
#include "mozStorageService.h"
#include "mozStorageStatement.h"
#include "mozStorageAsyncStatement.h"
#include "mozStorageArgValueArray.h"
#include "mozStoragePrivateHelpers.h"
#include "mozStorageStatementData.h"
#include "ObfuscatingVFS.h"
#include "QuotaVFS.h"
#include "StorageBaseStatementInternal.h"
#include "SQLCollations.h"
#include "FileSystemModule.h"
#include "mozStorageHelper.h"
#include "mozilla/Assertions.h"
#include "mozilla/Logging.h"
#include "mozilla/Printf.h"
#include "mozilla/ProfilerLabels.h"
#include "mozilla/RefPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsProxyRelease.h"
#include "nsStringFwd.h"
#include "nsURLHelper.h"
#define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB
// Maximum size of the pages cache per connection.
#define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB
mozilla::LazyLogModule gStorageLog("mozStorage");
// Checks that the protected code is running on the main-thread only if the
// connection was also opened on it.
#ifdef DEBUG
# define CHECK_MAINTHREAD_ABUSE() \
do { \
NS_WARNING_ASSERTION( \
eventTargetOpenedOn == GetMainThreadSerialEventTarget() || \
!NS_IsMainThread(), \
"Using Storage synchronous API on main-thread, but " \
"the connection was opened on another thread."); \
} while (0)
#else
# define CHECK_MAINTHREAD_ABUSE() \
do { /* Nothing */ \
} while (0)
#endif
namespace mozilla::storage {
using mozilla::dom::quota::QuotaObject;
using mozilla::Telemetry::AccumulateCategoricalKeyed;
using mozilla::Telemetry::LABELS_SQLITE_STORE_OPEN;
using mozilla::Telemetry::LABELS_SQLITE_STORE_QUERY;
namespace {
int nsresultToSQLiteResult(nsresult aXPCOMResultCode) {
if (NS_SUCCEEDED(aXPCOMResultCode)) {
return SQLITE_OK;
}
switch (aXPCOMResultCode) {
case NS_ERROR_FILE_CORRUPTED:
return SQLITE_CORRUPT;
case NS_ERROR_FILE_ACCESS_DENIED:
return SQLITE_CANTOPEN;
case NS_ERROR_STORAGE_BUSY:
return SQLITE_BUSY;
case NS_ERROR_FILE_IS_LOCKED:
return SQLITE_LOCKED;
case NS_ERROR_FILE_READ_ONLY:
return SQLITE_READONLY;
case NS_ERROR_STORAGE_IOERR:
return SQLITE_IOERR;
case NS_ERROR_FILE_NO_DEVICE_SPACE:
return SQLITE_FULL;
case NS_ERROR_OUT_OF_MEMORY:
return SQLITE_NOMEM;
case NS_ERROR_UNEXPECTED:
return SQLITE_MISUSE;
case NS_ERROR_ABORT:
return SQLITE_ABORT;
case NS_ERROR_STORAGE_CONSTRAINT:
return SQLITE_CONSTRAINT;
default:
return SQLITE_ERROR;
}
MOZ_MAKE_COMPILER_ASSUME_IS_UNREACHABLE("Must return in switch above!");
}
////////////////////////////////////////////////////////////////////////////////
//// Variant Specialization Functions (variantToSQLiteT)
int sqlite3_T_int(sqlite3_context* aCtx, int aValue) {
::sqlite3_result_int(aCtx, aValue);
return SQLITE_OK;
}
int sqlite3_T_int64(sqlite3_context* aCtx, sqlite3_int64 aValue) {
::sqlite3_result_int64(aCtx, aValue);
return SQLITE_OK;
}
int sqlite3_T_double(sqlite3_context* aCtx, double aValue) {
::sqlite3_result_double(aCtx, aValue);
return SQLITE_OK;
}
int sqlite3_T_text(sqlite3_context* aCtx, const nsCString& aValue) {
CheckedInt<int32_t> length(aValue.Length());
if (!length.isValid()) {
return SQLITE_MISUSE;
}
::sqlite3_result_text(aCtx, aValue.get(), length.value(), SQLITE_TRANSIENT);
return SQLITE_OK;
}
int sqlite3_T_text16(sqlite3_context* aCtx, const nsString& aValue) {
CheckedInt<int32_t> n_bytes =
CheckedInt<int32_t>(aValue.Length()) * sizeof(char16_t);
if (!n_bytes.isValid()) {
return SQLITE_MISUSE;
}
::sqlite3_result_text16(aCtx, aValue.get(), n_bytes.value(),
SQLITE_TRANSIENT);
return SQLITE_OK;
}
int sqlite3_T_null(sqlite3_context* aCtx) {
::sqlite3_result_null(aCtx);
return SQLITE_OK;
}
int sqlite3_T_blob(sqlite3_context* aCtx, const void* aData, int aSize) {
::sqlite3_result_blob(aCtx, aData, aSize, free);
return SQLITE_OK;
}
#include "variantToSQLiteT_impl.h"
////////////////////////////////////////////////////////////////////////////////
//// Modules
struct Module {
const char* name;
int (*registerFunc)(sqlite3*, const char*);
};
Module gModules[] = {{"filesystem", RegisterFileSystemModule}};
////////////////////////////////////////////////////////////////////////////////
//// Local Functions
int tracefunc(unsigned aReason, void* aClosure, void* aP, void* aX) {
switch (aReason) {
case SQLITE_TRACE_STMT: {
// aP is a pointer to the prepared statement.
sqlite3_stmt* stmt = static_cast<sqlite3_stmt*>(aP);
// aX is a pointer to a string containing the unexpanded SQL or a comment,
// starting with "--"" in case of a trigger.
char* expanded = static_cast<char*>(aX);
// Simulate what sqlite_trace was doing.
if (!::strncmp(expanded, "--", 2)) {
MOZ_LOG(gStorageLog, LogLevel::Debug,
("TRACE_STMT on %p: '%s'", aClosure, expanded));
} else {
char* sql = ::sqlite3_expanded_sql(stmt);
MOZ_LOG(gStorageLog, LogLevel::Debug,
("TRACE_STMT on %p: '%s'", aClosure, sql));
::sqlite3_free(sql);
}
break;
}
case SQLITE_TRACE_PROFILE: {
// aX is pointer to a 64bit integer containing nanoseconds it took to
// execute the last command.
sqlite_int64 time = *(static_cast<sqlite_int64*>(aX)) / 1000000;
if (time > 0) {
MOZ_LOG(gStorageLog, LogLevel::Debug,
("TRACE_TIME on %p: %lldms", aClosure, time));
}
break;
}
}
return 0;
}
void basicFunctionHelper(sqlite3_context* aCtx, int aArgc,
sqlite3_value** aArgv) {
void* userData = ::sqlite3_user_data(aCtx);
mozIStorageFunction* func = static_cast<mozIStorageFunction*>(userData);
RefPtr<ArgValueArray> arguments(new ArgValueArray(aArgc, aArgv));
if (!arguments) return;
nsCOMPtr<nsIVariant> result;
nsresult rv = func->OnFunctionCall(arguments, getter_AddRefs(result));
if (NS_FAILED(rv)) {
nsAutoCString errorMessage;
GetErrorName(rv, errorMessage);
errorMessage.InsertLiteral("User function returned ", 0);
errorMessage.Append('!');
NS_WARNING(errorMessage.get());
::sqlite3_result_error(aCtx, errorMessage.get(), -1);
::sqlite3_result_error_code(aCtx, nsresultToSQLiteResult(rv));
return;
}
int retcode = variantToSQLiteT(aCtx, result);
if (retcode != SQLITE_OK) {
NS_WARNING("User function returned invalid data type!");
::sqlite3_result_error(aCtx, "User function returned invalid data type",
-1);
}
}
RefPtr<QuotaObject> GetQuotaObject(sqlite3_file* aFile, bool obfuscatingVFS) {
return obfuscatingVFS
? mozilla::storage::obfsvfs::GetQuotaObjectForFile(aFile)
: mozilla::storage::quotavfs::GetQuotaObjectForFile(aFile);
}
/**
* This code is heavily based on the sample at:
*/
class UnlockNotification {
public:
UnlockNotification()
: mMutex("UnlockNotification mMutex"),
mCondVar(mMutex, "UnlockNotification condVar"),
mSignaled(false) {}
void Wait() {
MutexAutoLock lock(mMutex);
while (!mSignaled) {
(void)mCondVar.Wait();
}
}
void Signal() {
MutexAutoLock lock(mMutex);
mSignaled = true;
(void)mCondVar.Notify();
}
private:
Mutex mMutex MOZ_UNANNOTATED;
CondVar mCondVar;
bool mSignaled;
};
void UnlockNotifyCallback(void** aArgs, int aArgsSize) {
for (int i = 0; i < aArgsSize; i++) {
UnlockNotification* notification =
static_cast<UnlockNotification*>(aArgs[i]);
notification->Signal();
}
}
int WaitForUnlockNotify(sqlite3* aDatabase) {
UnlockNotification notification;
int srv =
::sqlite3_unlock_notify(aDatabase, UnlockNotifyCallback, ¬ification);
MOZ_ASSERT(srv == SQLITE_LOCKED || srv == SQLITE_OK);
if (srv == SQLITE_OK) {
notification.Wait();
}
return srv;
}
////////////////////////////////////////////////////////////////////////////////
//// Local Classes
class AsyncCloseConnection final : public Runnable {
public:
AsyncCloseConnection(Connection* aConnection, sqlite3* aNativeConnection,
nsIRunnable* aCallbackEvent)
: Runnable("storage::AsyncCloseConnection"),
mConnection(aConnection),
mNativeConnection(aNativeConnection),
mCallbackEvent(aCallbackEvent) {}
NS_IMETHOD Run() override {
// Make sure we don't dispatch to the current thread.
MOZ_ASSERT(!IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn));
nsCOMPtr<nsIRunnable> event =
NewRunnableMethod("storage::Connection::shutdownAsyncThread",
mConnection, &Connection::shutdownAsyncThread);
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event));
// Internal close.
(void)mConnection->internalClose(mNativeConnection);
// Callback
if (mCallbackEvent) {
nsCOMPtr<nsIThread> thread;
(void)NS_GetMainThread(getter_AddRefs(thread));
(void)thread->Dispatch(mCallbackEvent, NS_DISPATCH_NORMAL);
}
return NS_OK;
}
~AsyncCloseConnection() override {
NS_ReleaseOnMainThread("AsyncCloseConnection::mConnection",
mConnection.forget());
NS_ReleaseOnMainThread("AsyncCloseConnection::mCallbackEvent",
mCallbackEvent.forget());
}
private:
RefPtr<Connection> mConnection;
sqlite3* mNativeConnection;
nsCOMPtr<nsIRunnable> mCallbackEvent;
};
/**
* An event used to initialize the clone of a connection.
*
* Must be executed on the clone's async execution thread.
*/
class AsyncInitializeClone final : public Runnable {
public:
/**
* @param aConnection The connection being cloned.
* @param aClone The clone.
* @param aReadOnly If |true|, the clone is read only.
* @param aCallback A callback to trigger once initialization
* is complete. This event will be called on
* aClone->eventTargetOpenedOn.
*/
AsyncInitializeClone(Connection* aConnection, Connection* aClone,
const bool aReadOnly,
mozIStorageCompletionCallback* aCallback)
: Runnable("storage::AsyncInitializeClone"),
mConnection(aConnection),
mClone(aClone),
mReadOnly(aReadOnly),
mCallback(aCallback) {
MOZ_ASSERT(NS_IsMainThread());
}
NS_IMETHOD Run() override {
MOZ_ASSERT(!NS_IsMainThread());
nsresult rv = mConnection->initializeClone(mClone, mReadOnly);
if (NS_FAILED(rv)) {
return Dispatch(rv, nullptr);
}
return Dispatch(NS_OK,
NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, mClone));
}
private:
nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
RefPtr<CallbackComplete> event =
new CallbackComplete(aResult, aValue, mCallback.forget());
return mClone->eventTargetOpenedOn->Dispatch(event, NS_DISPATCH_NORMAL);
}
~AsyncInitializeClone() override {
nsCOMPtr<nsIThread> thread;
DebugOnly<nsresult> rv = NS_GetMainThread(getter_AddRefs(thread));
MOZ_ASSERT(NS_SUCCEEDED(rv));
// Handle ambiguous nsISupports inheritance.
NS_ProxyRelease("AsyncInitializeClone::mConnection", thread,
mConnection.forget());
NS_ProxyRelease("AsyncInitializeClone::mClone", thread, mClone.forget());
// Generally, the callback will be released by CallbackComplete.
// However, if for some reason Run() is not executed, we still
// need to ensure that it is released here.
NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
mCallback.forget());
}
RefPtr<Connection> mConnection;
RefPtr<Connection> mClone;
const bool mReadOnly;
nsCOMPtr<mozIStorageCompletionCallback> mCallback;
};
/**
* A listener for async connection closing.
*/
class CloseListener final : public mozIStorageCompletionCallback {
public:
NS_DECL_ISUPPORTS
CloseListener() : mClosed(false) {}
NS_IMETHOD Complete(nsresult, nsISupports*) override {
mClosed = true;
return NS_OK;
}
bool mClosed;
private:
~CloseListener() = default;
};
NS_IMPL_ISUPPORTS(CloseListener, mozIStorageCompletionCallback)
class AsyncVacuumEvent final : public Runnable {
public:
AsyncVacuumEvent(Connection* aConnection,
mozIStorageCompletionCallback* aCallback,
bool aUseIncremental, int32_t aSetPageSize)
: Runnable("storage::AsyncVacuum"),
mConnection(aConnection),
mCallback(aCallback),
mUseIncremental(aUseIncremental),
mSetPageSize(aSetPageSize),
mStatus(NS_ERROR_UNEXPECTED) {}
NS_IMETHOD Run() override {
// This is initially dispatched to the helper thread, then re-dispatched
// to the opener thread, where it will callback.
if (IsOnCurrentSerialEventTarget(mConnection->eventTargetOpenedOn)) {
// Send the completion event.
if (mCallback) {
mozilla::Unused << mCallback->Complete(mStatus, nullptr);
}
return NS_OK;
}
// Ensure to invoke the callback regardless of errors.
auto guard = MakeScopeExit([&]() {
mConnection->mIsStatementOnHelperThreadInterruptible = false;
mozilla::Unused << mConnection->eventTargetOpenedOn->Dispatch(
this, NS_DISPATCH_NORMAL);
});
// Get list of attached databases.
nsCOMPtr<mozIStorageStatement> stmt;
nsresult rv = mConnection->CreateStatement(MOZ_STORAGE_UNIQUIFY_QUERY_STR
"PRAGMA database_list"_ns,
getter_AddRefs(stmt));
NS_ENSURE_SUCCESS(rv, rv);
// We must accumulate names and loop through them later, otherwise VACUUM
// will see an ongoing statement and bail out.
nsTArray<nsCString> schemaNames;
bool hasResult = false;
while (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
nsAutoCString name;
rv = stmt->GetUTF8String(1, name);
if (NS_SUCCEEDED(rv) && !name.EqualsLiteral("temp")) {
schemaNames.AppendElement(name);
}
}
mStatus = NS_OK;
// Mark this vacuum as an interruptible operation, so it can be interrupted
// if the connection closes during shutdown.
mConnection->mIsStatementOnHelperThreadInterruptible = true;
for (const nsCString& schemaName : schemaNames) {
rv = this->Vacuum(schemaName);
if (NS_FAILED(rv)) {
// This is sub-optimal since it's only keeping the last error reason,
// but it will do for now.
mStatus = rv;
}
}
return mStatus;
}
nsresult Vacuum(const nsACString& aSchemaName) {
// Abort if we're in shutdown.
if (AppShutdown::IsInOrBeyond(ShutdownPhase::AppShutdownConfirmed)) {
return NS_ERROR_ABORT;
}
int32_t removablePages = mConnection->RemovablePagesInFreeList(aSchemaName);
if (!removablePages) {
// There's no empty pages to remove, so skip this vacuum for now.
return NS_OK;
}
nsresult rv;
bool needsFullVacuum = true;
if (mSetPageSize) {
nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
query.Append(aSchemaName);
query.AppendLiteral(".page_size = ");
query.AppendInt(mSetPageSize);
nsCOMPtr<mozIStorageStatement> stmt;
rv = mConnection->ExecuteSimpleSQL(query);
NS_ENSURE_SUCCESS(rv, rv);
}
// Check auto_vacuum.
{
nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
query.Append(aSchemaName);
query.AppendLiteral(".auto_vacuum");
nsCOMPtr<mozIStorageStatement> stmt;
rv = mConnection->CreateStatement(query, getter_AddRefs(stmt));
NS_ENSURE_SUCCESS(rv, rv);
bool hasResult = false;
bool changeAutoVacuum = false;
if (stmt && NS_SUCCEEDED(stmt->ExecuteStep(&hasResult)) && hasResult) {
bool isIncrementalVacuum = stmt->AsInt32(0) == 2;
changeAutoVacuum = isIncrementalVacuum != mUseIncremental;
if (isIncrementalVacuum && !changeAutoVacuum) {
needsFullVacuum = false;
}
}
// Changing auto_vacuum is only supported on the main schema.
if (aSchemaName.EqualsLiteral("main") && changeAutoVacuum) {
nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
query.Append(aSchemaName);
query.AppendLiteral(".auto_vacuum = ");
query.AppendInt(mUseIncremental ? 2 : 0);
rv = mConnection->ExecuteSimpleSQL(query);
NS_ENSURE_SUCCESS(rv, rv);
}
}
if (needsFullVacuum) {
nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "VACUUM ");
query.Append(aSchemaName);
rv = mConnection->ExecuteSimpleSQL(query);
NS_ENSURE_SUCCESS(rv, rv);
} else {
nsAutoCString query(MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA ");
query.Append(aSchemaName);
query.AppendLiteral(".incremental_vacuum(");
query.AppendInt(removablePages);
query.AppendLiteral(")");
rv = mConnection->ExecuteSimpleSQL(query);
NS_ENSURE_SUCCESS(rv, rv);
}
return NS_OK;
}
~AsyncVacuumEvent() override {
NS_ReleaseOnMainThread("AsyncVacuum::mConnection", mConnection.forget());
NS_ReleaseOnMainThread("AsyncVacuum::mCallback", mCallback.forget());
}
private:
RefPtr<Connection> mConnection;
nsCOMPtr<mozIStorageCompletionCallback> mCallback;
bool mUseIncremental;
int32_t mSetPageSize;
Atomic<nsresult> mStatus;
};
/**
* A runnable to perform an SQLite database backup when there may be one or more
* open connections on that database.
*/
class AsyncBackupDatabaseFile final : public Runnable, public nsITimerCallback {
public:
NS_DECL_ISUPPORTS_INHERITED
/**
* @param aConnection The connection to the database being backed up.
* @param aNativeConnection The native connection to the database being backed
* up.
* @param aDestinationFile The destination file for the created backup.
* @param aCallback A callback to trigger once the backup process has
* completed. The callback will be supplied with an nsresult
* indicating whether or not the backup was successfully
* created. This callback will be called on the
* mConnection->eventTargetOpenedOn thread.
* @throws
*/
AsyncBackupDatabaseFile(Connection* aConnection, sqlite3* aNativeConnection,
nsIFile* aDestinationFile,
mozIStorageCompletionCallback* aCallback,
int32_t aPagesPerStep, uint32_t aStepDelayMs)
: Runnable("storage::AsyncBackupDatabaseFile"),
mConnection(aConnection),
mNativeConnection(aNativeConnection),
mDestinationFile(aDestinationFile),
mCallback(aCallback),
mPagesPerStep(aPagesPerStep),
mStepDelayMs(aStepDelayMs),
mBackupFile(nullptr),
mBackupHandle(nullptr) {
MOZ_ASSERT(NS_IsMainThread());
}
NS_IMETHOD Run() override {
MOZ_ASSERT(!NS_IsMainThread());
nsAutoString path;
nsresult rv = mDestinationFile->GetPath(path);
if (NS_FAILED(rv)) {
return Dispatch(rv, nullptr);
}
// Put a .tmp on the end of the destination while the backup is underway.
// This extension will be stripped off after the backup successfully
// completes.
path.AppendLiteral(".tmp");
int srv = ::sqlite3_open(NS_ConvertUTF16toUTF8(path).get(), &mBackupFile);
if (srv != SQLITE_OK) {
return Dispatch(NS_ERROR_FAILURE, nullptr);
}
static const char* mainDBName = "main";
mBackupHandle = ::sqlite3_backup_init(mBackupFile, mainDBName,
mNativeConnection, mainDBName);
if (!mBackupHandle) {
MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK);
return Dispatch(NS_ERROR_FAILURE, nullptr);
}
return DoStep();
}
NS_IMETHOD
Notify(nsITimer* aTimer) override { return DoStep(); }
private:
nsresult DoStep() {
#define DISPATCH_AND_RETURN_IF_FAILED(rv) \
if (NS_FAILED(rv)) { \
return Dispatch(rv, nullptr); \
}
// This guard is used to close the backup database in the event of
// some failure throughout this process. We release the exit guard
// only if we complete the backup successfully, or defer to another
// later call to DoStep.
auto guard = MakeScopeExit([&]() {
MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK);
mBackupFile = nullptr;
});
MOZ_ASSERT(!NS_IsMainThread());
nsAutoString originalPath;
nsresult rv = mDestinationFile->GetPath(originalPath);
DISPATCH_AND_RETURN_IF_FAILED(rv);
nsAutoString tempPath = originalPath;
tempPath.AppendLiteral(".tmp");
nsCOMPtr<nsIFile> file =
do_CreateInstance("@mozilla.org/file/local;1", &rv);
DISPATCH_AND_RETURN_IF_FAILED(rv);
rv = file->InitWithPath(tempPath);
DISPATCH_AND_RETURN_IF_FAILED(rv);
int srv = ::sqlite3_backup_step(mBackupHandle, mPagesPerStep);
if (srv == SQLITE_OK || srv == SQLITE_BUSY || srv == SQLITE_LOCKED) {
// We're continuing the backup later. Release the guard to avoid closing
// the database.
guard.release();
// Queue up the next step
return NS_NewTimerWithCallback(getter_AddRefs(mTimer), this, mStepDelayMs,
nsITimer::TYPE_ONE_SHOT,
GetCurrentSerialEventTarget());
}
#ifdef DEBUG
if (srv != SQLITE_DONE) {
nsCString warnMsg;
warnMsg.AppendLiteral(
"The SQLite database copy could not be completed due to an error: ");
warnMsg.Append(::sqlite3_errmsg(mBackupFile));
NS_WARNING(warnMsg.get());
}
#endif
(void)::sqlite3_backup_finish(mBackupHandle);
MOZ_ALWAYS_TRUE(::sqlite3_close(mBackupFile) == SQLITE_OK);
mBackupFile = nullptr;
// The database is already closed, so we can release this guard now.
guard.release();
if (srv != SQLITE_DONE) {
NS_WARNING("Failed to create database copy.");
// The partially created database file is not useful. Let's remove it.
rv = file->Remove(false);
if (NS_FAILED(rv)) {
NS_WARNING(
"Removing a partially backed up SQLite database file failed.");
}
return Dispatch(convertResultCode(srv), nullptr);
}
// Now that we've successfully created the copy, we'll strip off the .tmp
// extension.
nsAutoString leafName;
rv = mDestinationFile->GetLeafName(leafName);
DISPATCH_AND_RETURN_IF_FAILED(rv);
rv = file->RenameTo(nullptr, leafName);
DISPATCH_AND_RETURN_IF_FAILED(rv);
#undef DISPATCH_AND_RETURN_IF_FAILED
return Dispatch(NS_OK, nullptr);
}
nsresult Dispatch(nsresult aResult, nsISupports* aValue) {
RefPtr<CallbackComplete> event =
new CallbackComplete(aResult, aValue, mCallback.forget());
return mConnection->eventTargetOpenedOn->Dispatch(event,
NS_DISPATCH_NORMAL);
}
~AsyncBackupDatabaseFile() override {
nsresult rv;
nsCOMPtr<nsIThread> thread =
do_QueryInterface(mConnection->eventTargetOpenedOn, &rv);
MOZ_ASSERT(NS_SUCCEEDED(rv));
// Handle ambiguous nsISupports inheritance.
NS_ProxyRelease("AsyncBackupDatabaseFile::mConnection", thread,
mConnection.forget());
NS_ProxyRelease("AsyncBackupDatabaseFile::mDestinationFile", thread,
mDestinationFile.forget());
// Generally, the callback will be released by CallbackComplete.
// However, if for some reason Run() is not executed, we still
// need to ensure that it is released here.
NS_ProxyRelease("AsyncInitializeClone::mCallback", thread,
mCallback.forget());
}
RefPtr<Connection> mConnection;
sqlite3* mNativeConnection;
nsCOMPtr<nsITimer> mTimer;
nsCOMPtr<nsIFile> mDestinationFile;
nsCOMPtr<mozIStorageCompletionCallback> mCallback;
int32_t mPagesPerStep;
uint32_t mStepDelayMs;
sqlite3* mBackupFile;
sqlite3_backup* mBackupHandle;
};
NS_IMPL_ISUPPORTS_INHERITED(AsyncBackupDatabaseFile, Runnable, nsITimerCallback)
} // namespace
////////////////////////////////////////////////////////////////////////////////
//// Connection
Connection::Connection(Service* aService, int aFlags,
ConnectionOperation aSupportedOperations,
const nsCString& aTelemetryFilename, bool aInterruptible,
bool aIgnoreLockingMode, bool aOpenNotExclusive)
: sharedAsyncExecutionMutex("Connection::sharedAsyncExecutionMutex"),
sharedDBMutex("Connection::sharedDBMutex"),
eventTargetOpenedOn(WrapNotNull(GetCurrentSerialEventTarget())),
mIsStatementOnHelperThreadInterruptible(false),
mDBConn(nullptr),
mDefaultTransactionType(mozIStorageConnection::TRANSACTION_DEFERRED),
mDestroying(false),
mProgressHandler(nullptr),
mStorageService(aService),
mFlags(aFlags),
mTransactionNestingLevel(0),
mSupportedOperations(aSupportedOperations),
mInterruptible(aSupportedOperations == Connection::ASYNCHRONOUS ||
aInterruptible),
mIgnoreLockingMode(aIgnoreLockingMode),
mOpenNotExclusive(aOpenNotExclusive),
mAsyncExecutionThreadShuttingDown(false),
mConnectionClosed(false),
mGrowthChunkSize(0) {
MOZ_ASSERT(!mIgnoreLockingMode || mFlags & SQLITE_OPEN_READONLY,
"Can't ignore locking for a non-readonly connection!");
mStorageService->registerConnection(this);
MOZ_ASSERT(!aTelemetryFilename.IsEmpty(),
"A telemetry filename should have been passed-in.");
mTelemetryFilename.Assign(aTelemetryFilename);
}
Connection::~Connection() {
// Failsafe Close() occurs in our custom Release method because of
// complications related to Close() potentially invoking AsyncClose() which
// will increment our refcount.
MOZ_ASSERT(!mAsyncExecutionThread,
"The async thread has not been shutdown properly!");
}
NS_IMPL_ADDREF(Connection)
NS_INTERFACE_MAP_BEGIN(Connection)
NS_INTERFACE_MAP_ENTRY(mozIStorageAsyncConnection)
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
NS_INTERFACE_MAP_ENTRY(mozIStorageConnection)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozIStorageConnection)
NS_INTERFACE_MAP_END
// This is identical to what NS_IMPL_RELEASE provides, but with the
// extra |1 == count| case.
NS_IMETHODIMP_(MozExternalRefCountType) Connection::Release(void) {
MOZ_ASSERT(0 != mRefCnt, "dup release");
nsrefcnt count = --mRefCnt;
NS_LOG_RELEASE(this, count, "Connection");
if (1 == count) {
// If the refcount went to 1, the single reference must be from
// gService->mConnections (in class |Service|). And the code calling
// Release is either:
// - The "user" code that had created the connection, releasing on any
// thread.
// - One of Service's getConnections() callers had acquired a strong
// reference to the Connection that out-lived the last "user" reference,
// and now that just got dropped. Note that this reference could be
// getting dropped on the main thread or Connection->eventTargetOpenedOn
// (because of the NewRunnableMethod used by minimizeMemory).
//
// Either way, we should now perform our failsafe Close() and unregister.
// However, we only want to do this once, and the reality is that our
// refcount could go back up above 1 and down again at any time if we are
// off the main thread and getConnections() gets called on the main thread,
// so we use an atomic here to do this exactly once.
if (mDestroying.compareExchange(false, true)) {
// Close the connection, dispatching to the opening event target if we're
// not on that event target already and that event target is still
// accepting runnables. We do this because it's possible we're on the main
// thread because of getConnections(), and we REALLY don't want to
// transfer I/O to the main thread if we can avoid it.
if (IsOnCurrentSerialEventTarget(eventTargetOpenedOn)) {
// This could cause SpinningSynchronousClose() to be invoked and AddRef
// triggered for AsyncCloseConnection's strong ref if the conn was ever
// use for async purposes. (Main-thread only, though.)
Unused << synchronousClose();
} else {
nsCOMPtr<nsIRunnable> event =
NewRunnableMethod("storage::Connection::synchronousClose", this,
&Connection::synchronousClose);
if (NS_FAILED(eventTargetOpenedOn->Dispatch(event.forget(),
NS_DISPATCH_NORMAL))) {
// The event target was dead and so we've just leaked our runnable.
// This should not happen because our non-main-thread consumers should
// be explicitly closing their connections, not relying on us to close
// them for them. (It's okay to let a statement go out of scope for
// automatic cleanup, but not a Connection.)
MOZ_ASSERT(false,
"Leaked Connection::synchronousClose(), ownership fail.");
Unused << synchronousClose();
}
}
// This will drop its strong reference right here, right now.
mStorageService->unregisterConnection(this);
}
} else if (0 == count) {
mRefCnt = 1; /* stabilize */
#if 0 /* enable this to find non-threadsafe destructors: */
NS_ASSERT_OWNINGTHREAD(Connection);
#endif
delete (this);
return 0;
}
return count;
}
int32_t Connection::getSqliteRuntimeStatus(int32_t aStatusOption,
int32_t* aMaxValue) {
MOZ_ASSERT(connectionReady(), "A connection must exist at this point");
int curr = 0, max = 0;
DebugOnly<int> rc =
::sqlite3_db_status(mDBConn, aStatusOption, &curr, &max, 0);
MOZ_ASSERT(NS_SUCCEEDED(convertResultCode(rc)));
if (aMaxValue) *aMaxValue = max;
return curr;
}
nsIEventTarget* Connection::getAsyncExecutionTarget() {
NS_ENSURE_TRUE(IsOnCurrentSerialEventTarget(eventTargetOpenedOn), nullptr);
// Don't return the asynchronous event target if we are shutting down.
if (mAsyncExecutionThreadShuttingDown) {
return nullptr;
}
// Create the async event target if there's none yet.
if (!mAsyncExecutionThread) {
// Names start with "sqldb:" followed by a recognizable name, like the
// database file name, or a specially crafted name like "memory".
// sensitive part of the file name (e.g. an URL origin) should be replaced
// by passing an explicit telemetryName to openDatabaseWithFileURL.
nsAutoCString name("sqldb:"_ns);
name.Append(mTelemetryFilename);
static nsThreadPoolNaming naming;
nsresult rv = NS_NewNamedThread(naming.GetNextThreadName(name),
getter_AddRefs(mAsyncExecutionThread));
if (NS_FAILED(rv)) {
NS_WARNING("Failed to create async thread.");
return nullptr;
}
}
return mAsyncExecutionThread;
}
void Connection::RecordOpenStatus(nsresult rv) {
nsCString histogramKey = mTelemetryFilename;
if (histogramKey.IsEmpty()) {
histogramKey.AssignLiteral("unknown");
}
if (NS_SUCCEEDED(rv)) {
AccumulateCategoricalKeyed(histogramKey, LABELS_SQLITE_STORE_OPEN::success);
return;
}
switch (rv) {
case NS_ERROR_FILE_CORRUPTED:
AccumulateCategoricalKeyed(histogramKey,
LABELS_SQLITE_STORE_OPEN::corrupt);
break;
case NS_ERROR_STORAGE_IOERR:
AccumulateCategoricalKeyed(histogramKey,
LABELS_SQLITE_STORE_OPEN::diskio);
break;
case NS_ERROR_FILE_ACCESS_DENIED:
case NS_ERROR_FILE_IS_LOCKED:
case NS_ERROR_FILE_READ_ONLY:
AccumulateCategoricalKeyed(histogramKey,
LABELS_SQLITE_STORE_OPEN::access);
break;
case NS_ERROR_FILE_NO_DEVICE_SPACE:
AccumulateCategoricalKeyed(histogramKey,
LABELS_SQLITE_STORE_OPEN::diskspace);
break;
default:
AccumulateCategoricalKeyed(histogramKey,
LABELS_SQLITE_STORE_OPEN::failure);
}
}
void Connection::RecordQueryStatus(int srv) {
nsCString histogramKey = mTelemetryFilename;
if (histogramKey.IsEmpty()) {
histogramKey.AssignLiteral("unknown");
}
switch (srv) {
case SQLITE_OK:
case SQLITE_ROW:
case SQLITE_DONE: