Source code
Revision control
Copy as Markdown
Other Tools
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 "mozilla/dom/cache/Context.h"
#include "CacheCommon.h"
#include "mozilla/AutoRestore.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/dom/SafeRefPtr.h"
#include "mozilla/dom/cache/Action.h"
#include "mozilla/dom/cache/FileUtils.h"
#include "mozilla/dom/cache/Manager.h"
#include "mozilla/dom/cache/ManagerId.h"
#include "mozilla/dom/quota/Assertions.h"
#include "mozilla/dom/quota/ClientDirectoryLock.h"
#include "mozilla/dom/quota/DirectoryLock.h"
#include "mozilla/dom/quota/DirectoryLockInlines.h"
#include "mozilla/dom/quota/PrincipalUtils.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/dom/quota/ResultExtensions.h"
#include "mozilla/dom/quota/ThreadUtils.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"
#include "mozilla/Maybe.h"
#include "mozIStorageConnection.h"
#include "NotifyUtils.h"
#include "nsIPrincipal.h"
#include "nsIRunnable.h"
#include "nsIThread.h"
#include "nsThreadUtils.h"
#include "QuotaClientImpl.h"
namespace {
using mozilla::dom::cache::Action;
using mozilla::dom::cache::CacheDirectoryMetadata;
class NullAction final : public Action {
public:
NullAction() = default;
virtual void RunOnTarget(mozilla::SafeRefPtr<Resolver> aResolver,
const mozilla::Maybe<CacheDirectoryMetadata>&, Data*,
const mozilla::Maybe<mozilla::dom::cache::CipherKey>&
/* aMaybeCipherKey */) override {
// Resolve success immediately. This Action does no actual work.
MOZ_DIAGNOSTIC_ASSERT(aResolver);
aResolver->Resolve(NS_OK);
}
};
} // namespace
namespace mozilla::dom::cache {
using mozilla::dom::quota::AssertIsOnIOThread;
using mozilla::dom::quota::ClientDirectoryLock;
using mozilla::dom::quota::PERSISTENCE_TYPE_DEFAULT;
using mozilla::dom::quota::PersistenceType;
using mozilla::dom::quota::QuotaManager;
using mozilla::dom::quota::SleepIfEnabled;
class Context::Data final : public Action::Data {
public:
explicit Data(nsISerialEventTarget* aTarget) : mTarget(aTarget) {
MOZ_DIAGNOSTIC_ASSERT(mTarget);
}
virtual mozIStorageConnection* GetConnection() const override {
MOZ_ASSERT(mTarget->IsOnCurrentThread());
return mConnection;
}
virtual void SetConnection(mozIStorageConnection* aConn) override {
MOZ_ASSERT(mTarget->IsOnCurrentThread());
MOZ_DIAGNOSTIC_ASSERT(!mConnection);
mConnection = aConn;
MOZ_DIAGNOSTIC_ASSERT(mConnection);
}
private:
~Data() {
// We could proxy release our data here, but instead just assert. The
// Context code should guarantee that we are destroyed on the target
// thread once the connection is initialized. If we're not, then
// QuotaManager might race and try to clear the origin out from under us.
MOZ_ASSERT_IF(mConnection, mTarget->IsOnCurrentThread());
}
nsCOMPtr<nsISerialEventTarget> mTarget;
nsCOMPtr<mozIStorageConnection> mConnection;
// Threadsafe counting because we're created on the PBackground thread
// and destroyed on the target IO thread.
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Context::Data)
};
// Executed to perform the complicated dance of steps necessary to initialize
// the QuotaManager. This must be performed for each origin before any disk
// IO occurrs.
class Context::QuotaInitRunnable final : public nsIRunnable {
public:
QuotaInitRunnable(SafeRefPtr<Context> aContext, SafeRefPtr<Manager> aManager,
Data* aData, nsISerialEventTarget* aTarget,
SafeRefPtr<Action> aInitAction)
: mContext(std::move(aContext)),
mThreadsafeHandle(mContext->CreateThreadsafeHandle()),
mManager(std::move(aManager)),
mData(aData),
mTarget(aTarget),
mInitAction(std::move(aInitAction)),
mInitiatingEventTarget(GetCurrentSerialEventTarget()),
mResult(NS_OK),
mState(STATE_INIT),
mCanceled(false) {
MOZ_DIAGNOSTIC_ASSERT(mContext);
MOZ_DIAGNOSTIC_ASSERT(mManager);
MOZ_DIAGNOSTIC_ASSERT(mData);
MOZ_DIAGNOSTIC_ASSERT(mTarget);
MOZ_DIAGNOSTIC_ASSERT(mInitiatingEventTarget);
MOZ_DIAGNOSTIC_ASSERT(mInitAction);
}
Maybe<ClientDirectoryLock&> MaybeDirectoryLockRef() const {
NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable);
return ToMaybeRef(mDirectoryLock.get());
}
nsresult Dispatch() {
NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable);
MOZ_DIAGNOSTIC_ASSERT(mState == STATE_INIT);
mState = STATE_GET_INFO;
nsresult rv = NS_DispatchToMainThread(this, nsIThread::DISPATCH_NORMAL);
if (NS_WARN_IF(NS_FAILED(rv))) {
mState = STATE_COMPLETE;
Clear();
}
return rv;
}
void Cancel() {
NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable);
MOZ_DIAGNOSTIC_ASSERT(!mCanceled);
mCanceled = true;
mInitAction->CancelOnInitiatingThread();
}
void DirectoryLockAcquired(ClientDirectoryLock* aLock);
void DirectoryLockFailed();
private:
class SyncResolver final : public Action::Resolver {
public:
SyncResolver() : mResolved(false), mResult(NS_OK) {}
virtual void Resolve(nsresult aRv) override {
MOZ_DIAGNOSTIC_ASSERT(!mResolved);
mResolved = true;
mResult = aRv;
};
bool Resolved() const { return mResolved; }
nsresult Result() const { return mResult; }
private:
~SyncResolver() = default;
bool mResolved;
nsresult mResult;
NS_INLINE_DECL_REFCOUNTING(Context::QuotaInitRunnable::SyncResolver,
override)
};
~QuotaInitRunnable() {
MOZ_DIAGNOSTIC_ASSERT(mState == STATE_COMPLETE);
MOZ_DIAGNOSTIC_ASSERT(!mContext);
MOZ_DIAGNOSTIC_ASSERT(!mInitAction);
}
enum State {
STATE_INIT,
STATE_GET_INFO,
STATE_CREATE_QUOTA_MANAGER,
STATE_WAIT_FOR_DIRECTORY_LOCK,
STATE_ENSURE_ORIGIN_INITIALIZED,
STATE_RUN_ON_TARGET,
STATE_RUNNING,
STATE_COMPLETING,
STATE_COMPLETE
};
void Complete(nsresult aResult) {
MOZ_DIAGNOSTIC_ASSERT(mState == STATE_RUNNING || NS_FAILED(aResult));
MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(mResult));
mResult = aResult;
mState = STATE_COMPLETING;
MOZ_ALWAYS_SUCCEEDS(
mInitiatingEventTarget->Dispatch(this, nsIThread::DISPATCH_NORMAL));
}
void Clear() {
NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable);
MOZ_DIAGNOSTIC_ASSERT(mContext);
mContext = nullptr;
mManager = nullptr;
mInitAction = nullptr;
}
SafeRefPtr<Context> mContext;
SafeRefPtr<ThreadsafeHandle> mThreadsafeHandle;
SafeRefPtr<Manager> mManager;
RefPtr<Data> mData;
nsCOMPtr<nsISerialEventTarget> mTarget;
SafeRefPtr<Action> mInitAction;
nsCOMPtr<nsIEventTarget> mInitiatingEventTarget;
nsresult mResult;
Maybe<mozilla::ipc::PrincipalInfo> mPrincipalInfo;
Maybe<CacheDirectoryMetadata> mDirectoryMetadata;
RefPtr<ClientDirectoryLock> mDirectoryLock;
RefPtr<CipherKeyManager> mCipherKeyManager;
State mState;
Atomic<bool> mCanceled;
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIRUNNABLE
};
void Context::QuotaInitRunnable::DirectoryLockAcquired(
ClientDirectoryLock* aLock) {
NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable);
MOZ_DIAGNOSTIC_ASSERT(aLock);
MOZ_DIAGNOSTIC_ASSERT(mState == STATE_WAIT_FOR_DIRECTORY_LOCK);
MOZ_DIAGNOSTIC_ASSERT(!mDirectoryLock);
mDirectoryLock = aLock;
MOZ_DIAGNOSTIC_ASSERT(mDirectoryLock->Id() >= 0);
mDirectoryMetadata->mDirectoryLockId = mDirectoryLock->Id();
if (mCanceled || mDirectoryLock->Invalidated()) {
Complete(NS_ERROR_ABORT);
return;
}
QuotaManager* qm = QuotaManager::Get();
MOZ_DIAGNOSTIC_ASSERT(qm);
mState = STATE_ENSURE_ORIGIN_INITIALIZED;
nsresult rv = qm->IOThread()->Dispatch(this, nsIThread::DISPATCH_NORMAL);
if (NS_WARN_IF(NS_FAILED(rv))) {
Complete(rv);
return;
}
NotifyDatabaseWorkStarted();
}
void Context::QuotaInitRunnable::DirectoryLockFailed() {
NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable);
MOZ_DIAGNOSTIC_ASSERT(mState == STATE_WAIT_FOR_DIRECTORY_LOCK);
MOZ_DIAGNOSTIC_ASSERT(!mDirectoryLock);
NS_WARNING("Failed to acquire a directory lock!");
Complete(NS_ERROR_FAILURE);
}
NS_IMPL_ISUPPORTS(mozilla::dom::cache::Context::QuotaInitRunnable, nsIRunnable);
// The QuotaManager init state machine is represented in the following diagram:
//
// +---------------+
// | Start | Resolve(error)
// | (Orig Thread) +---------------------+
// +-------+-------+ |
// | |
// +----------v-----------+ |
// | GetInfo | Resolve(error) |
// | (Main Thread) +-----------------+
// +----------+-----------+ |
// | |
// +----------v-----------+ |
// | CreateQuotaManager | Resolve(error) |
// | (Orig Thread) +-----------------+
// +----------+-----------+ |
// | |
// +----------v-----------+ |
// | WaitForDirectoryLock | Resolve(error) |
// | (Orig Thread) +-----------------+
// +----------+-----------+ |
// | |
// +----------v------------+ |
// |EnsureOriginInitialized| Resolve(error) |
// | (Quota IO Thread) +----------------+
// +----------+------------+ |
// | |
// +----------v------------+ |
// | RunOnTarget | Resolve(error) |
// | (Target Thread) +----------------+
// +----------+------------+ |
// | |
// +---------v---------+ +------v------+
// | Running | | Completing |
// | (Target Thread) +------------>(Orig Thread)|
// +-------------------+ +------+------+
// |
// +-----v----+
// | Complete |
// +----------+
//
// The initialization process proceeds through the main states. If an error
// occurs, then we transition to Completing state back on the original thread.
NS_IMETHODIMP
Context::QuotaInitRunnable::Run() {
// May run on different threads depending on the state. See individual
// state cases for thread assertions.
SafeRefPtr<SyncResolver> resolver = MakeSafeRefPtr<SyncResolver>();
switch (mState) {
// -----------------------------------
case STATE_GET_INFO: {
MOZ_ASSERT(NS_IsMainThread());
auto res = [this]() -> Result<Ok, nsresult> {
if (mCanceled) {
return Err(NS_ERROR_ABORT);
}
nsCOMPtr<nsIPrincipal> principal = mManager->GetManagerId().Principal();
mozilla::ipc::PrincipalInfo principalInfo;
QM_TRY(
MOZ_TO_RESULT(PrincipalToPrincipalInfo(principal, &principalInfo)));
mPrincipalInfo.emplace(std::move(principalInfo));
mState = STATE_CREATE_QUOTA_MANAGER;
MOZ_ALWAYS_SUCCEEDS(
mInitiatingEventTarget->Dispatch(this, nsIThread::DISPATCH_NORMAL));
return Ok{};
}();
if (res.isErr()) {
resolver->Resolve(res.inspectErr());
}
break;
}
// ----------------------------------
case STATE_CREATE_QUOTA_MANAGER: {
NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable);
if (mCanceled || QuotaManager::IsShuttingDown()) {
resolver->Resolve(NS_ERROR_ABORT);
break;
}
QM_TRY(QuotaManager::EnsureCreated(), QM_PROPAGATE,
[&resolver](const auto rv) { resolver->Resolve(rv); });
auto* const quotaManager = QuotaManager::Get();
MOZ_DIAGNOSTIC_ASSERT(quotaManager);
QM_TRY_UNWRAP(auto principalMetadata,
quota::GetInfoFromValidatedPrincipalInfo(*quotaManager,
*mPrincipalInfo));
mDirectoryMetadata.emplace(std::move(principalMetadata));
mState = STATE_WAIT_FOR_DIRECTORY_LOCK;
quotaManager
->OpenClientDirectory({*mDirectoryMetadata, quota::Client::DOMCACHE})
->Then(
GetCurrentSerialEventTarget(), __func__,
[self = RefPtr(this)](
const quota::ClientDirectoryLockPromise::ResolveOrRejectValue&
aValue) {
if (aValue.IsResolve()) {
self->DirectoryLockAcquired(aValue.ResolveValue());
} else {
self->DirectoryLockFailed();
}
});
break;
}
// ----------------------------------
case STATE_ENSURE_ORIGIN_INITIALIZED: {
AssertIsOnIOThread();
auto res = [this]() -> Result<Ok, nsresult> {
if (mCanceled) {
return Err(NS_ERROR_ABORT);
}
QuotaManager* quotaManager = QuotaManager::Get();
MOZ_DIAGNOSTIC_ASSERT(quotaManager);
QM_TRY_UNWRAP(mDirectoryMetadata->mDir,
quotaManager->GetOrCreateTemporaryOriginDirectory(
*mDirectoryMetadata));
auto* cacheQuotaClient = CacheQuotaClient::Get();
MOZ_DIAGNOSTIC_ASSERT(cacheQuotaClient);
mCipherKeyManager =
cacheQuotaClient->GetOrCreateCipherKeyManager(*mDirectoryMetadata);
SleepIfEnabled(
StaticPrefs::dom_cache_databaseInitialization_pauseOnIOThreadMs());
mState = STATE_RUN_ON_TARGET;
MOZ_ALWAYS_SUCCEEDS(
mTarget->Dispatch(this, nsIThread::DISPATCH_NORMAL));
return Ok{};
}();
if (res.isErr()) {
resolver->Resolve(res.inspectErr());
}
break;
}
// -------------------
case STATE_RUN_ON_TARGET: {
MOZ_ASSERT(mTarget->IsOnCurrentThread());
mState = STATE_RUNNING;
// Execute the provided initialization Action. The Action must Resolve()
// before returning.
mInitAction->RunOnTarget(
resolver.clonePtr(), mDirectoryMetadata, mData,
mCipherKeyManager ? Some(mCipherKeyManager->Ensure()) : Nothing{});
MOZ_DIAGNOSTIC_ASSERT(resolver->Resolved());
mData = nullptr;
// If the database was opened, then we should always succeed when creating
// the marker file. If it wasn't opened successfully, then no need to
// create a marker file anyway.
if (NS_SUCCEEDED(resolver->Result())) {
MOZ_ALWAYS_SUCCEEDS(CreateMarkerFile(*mDirectoryMetadata));
}
break;
}
// -------------------
case STATE_COMPLETING: {
NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable);
mInitAction->CompleteOnInitiatingThread(mResult);
mContext->OnQuotaInit(mResult, mDirectoryMetadata,
std::move(mDirectoryLock),
std::move(mCipherKeyManager));
mState = STATE_COMPLETE;
// Explicitly cleanup here as the destructor could fire on any of
// the threads we have bounced through.
Clear();
break;
}
// -----
case STATE_WAIT_FOR_DIRECTORY_LOCK:
default: {
MOZ_CRASH("unexpected state in QuotaInitRunnable");
}
}
if (resolver->Resolved()) {
Complete(resolver->Result());
}
return NS_OK;
}
// Runnable wrapper around Action objects dispatched on the Context. This
// runnable executes the Action on the appropriate threads while the Context
// is initialized.
class Context::ActionRunnable final : public nsIRunnable,
public Action::Resolver,
public Context::Activity {
public:
ActionRunnable(SafeRefPtr<Context> aContext, Data* aData,
nsISerialEventTarget* aTarget, SafeRefPtr<Action> aAction,
const Maybe<CacheDirectoryMetadata>& aDirectoryMetadata,
RefPtr<CipherKeyManager> aCipherKeyManager)
: mContext(std::move(aContext)),
mData(aData),
mTarget(aTarget),
mAction(std::move(aAction)),
mDirectoryMetadata(aDirectoryMetadata),
mCipherKeyManager(std::move(aCipherKeyManager)),
mInitiatingThread(GetCurrentSerialEventTarget()),
mState(STATE_INIT),
mResult(NS_OK),
mExecutingRunOnTarget(false) {
MOZ_DIAGNOSTIC_ASSERT(mContext);
// mData may be nullptr
MOZ_DIAGNOSTIC_ASSERT(mTarget);
MOZ_DIAGNOSTIC_ASSERT(mAction);
// mDirectoryMetadata.mDir may be nullptr if QuotaInitRunnable failed
MOZ_DIAGNOSTIC_ASSERT(mInitiatingThread);
}
nsresult Dispatch() {
NS_ASSERT_OWNINGTHREAD(ActionRunnable);
MOZ_DIAGNOSTIC_ASSERT(mState == STATE_INIT);
mState = STATE_RUN_ON_TARGET;
nsresult rv = mTarget->Dispatch(this, nsIEventTarget::DISPATCH_NORMAL);
if (NS_WARN_IF(NS_FAILED(rv))) {
mState = STATE_COMPLETE;
Clear();
}
return rv;
}
virtual bool MatchesCacheId(CacheId aCacheId) const override {
NS_ASSERT_OWNINGTHREAD(ActionRunnable);
return mAction->MatchesCacheId(aCacheId);
}
virtual void Cancel() override {
NS_ASSERT_OWNINGTHREAD(ActionRunnable);
mAction->CancelOnInitiatingThread();
}
virtual void Resolve(nsresult aRv) override {
MOZ_ASSERT(mTarget->IsOnCurrentThread());
MOZ_DIAGNOSTIC_ASSERT(mState == STATE_RUNNING);
mResult = aRv;
// We ultimately must complete on the initiating thread, but bounce through
// the current thread again to ensure that we don't destroy objects and
// state out from under the currently running action's stack.
mState = STATE_RESOLVING;
// If we were resolved synchronously within Action::RunOnTarget() then we
// can avoid a thread bounce and just resolve once RunOnTarget() returns.
// The Run() method will handle this by looking at mState after
// RunOnTarget() returns.
if (mExecutingRunOnTarget) {
return;
}
// Otherwise we are in an asynchronous resolve. And must perform a thread
// bounce to run on the target thread again.
MOZ_ALWAYS_SUCCEEDS(mTarget->Dispatch(this, nsIThread::DISPATCH_NORMAL));
}
private:
~ActionRunnable() {
MOZ_DIAGNOSTIC_ASSERT(mState == STATE_COMPLETE);
MOZ_DIAGNOSTIC_ASSERT(!mContext);
MOZ_DIAGNOSTIC_ASSERT(!mAction);
}
void DoStringify(nsACString& aData) override {
aData.Append("ActionRunnable ("_ns +
//
"State:"_ns + IntToCString(mState) + kStringifyDelimiter +
//
"Action:"_ns + IntToCString(static_cast<bool>(mAction)) +
kStringifyDelimiter +
// TODO: We might want to have Action::Stringify, too.
//
"Context:"_ns + IntToCString(static_cast<bool>(mContext)) +
kStringifyDelimiter +
// We do not print out mContext as we most probably were called
// by its Stringify.
")"_ns);
}
void Clear() {
NS_ASSERT_OWNINGTHREAD(ActionRunnable);
MOZ_DIAGNOSTIC_ASSERT(mContext);
MOZ_DIAGNOSTIC_ASSERT(mAction);
mContext->RemoveActivity(*this);
mContext = nullptr;
mAction = nullptr;
}
enum State {
STATE_INIT,
STATE_RUN_ON_TARGET,
STATE_RUNNING,
STATE_RESOLVING,
STATE_COMPLETING,
STATE_COMPLETE
};
SafeRefPtr<Context> mContext;
RefPtr<Data> mData;
nsCOMPtr<nsISerialEventTarget> mTarget;
SafeRefPtr<Action> mAction;
const Maybe<CacheDirectoryMetadata> mDirectoryMetadata;
RefPtr<CipherKeyManager> mCipherKeyManager;
nsCOMPtr<nsIEventTarget> mInitiatingThread;
State mState;
nsresult mResult;
// Only accessible on target thread;
bool mExecutingRunOnTarget;
public:
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIRUNNABLE
};