diff --git a/dom/cache/Context.cpp b/dom/cache/Context.cpp index de69e4cc9363..2cf550c22b44 100644 --- a/dom/cache/Context.cpp +++ b/dom/cache/Context.cpp @@ -131,11 +131,13 @@ class Context::QuotaInitRunnable final : public nsIRunnable public: QuotaInitRunnable(Context* aContext, Manager* aManager, - Action* aQuotaIOThreadAction) + nsIThread* aTarget, + Action* aInitAction) : mContext(aContext) , mThreadsafeHandle(aContext->CreateThreadsafeHandle()) , mManager(aManager) - , mQuotaIOThreadAction(aQuotaIOThreadAction) + , mTarget(aTarget) + , mInitAction(aInitAction) , mInitiatingThread(NS_GetCurrentThread()) , mResult(NS_OK) , mState(STATE_INIT) @@ -144,6 +146,7 @@ public: { MOZ_ASSERT(mContext); MOZ_ASSERT(mManager); + MOZ_ASSERT(mTarget); MOZ_ASSERT(mInitiatingThread); } @@ -166,7 +169,7 @@ public: NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable); MOZ_ASSERT(!mCanceled); mCanceled = true; - mQuotaIOThreadAction->CancelOnInitiatingThread(); + mInitAction->CancelOnInitiatingThread(); } private: @@ -202,7 +205,7 @@ private: { MOZ_ASSERT(mState == STATE_COMPLETE); MOZ_ASSERT(!mContext); - MOZ_ASSERT(!mQuotaIOThreadAction); + MOZ_ASSERT(!mInitAction); } enum State @@ -211,6 +214,7 @@ private: STATE_CALL_WAIT_FOR_OPEN_ALLOWED, STATE_WAIT_FOR_OPEN_ALLOWED, STATE_ENSURE_ORIGIN_INITIALIZED, + STATE_RUN_ON_TARGET, STATE_RUNNING, STATE_COMPLETING, STATE_COMPLETE @@ -222,13 +226,14 @@ private: MOZ_ASSERT(mContext); mContext = nullptr; mManager = nullptr; - mQuotaIOThreadAction = nullptr; + mInitAction = nullptr; } nsRefPtr mContext; nsRefPtr mThreadsafeHandle; nsRefPtr mManager; - nsRefPtr mQuotaIOThreadAction; + nsCOMPtr mTarget; + nsRefPtr mInitAction; nsCOMPtr mInitiatingThread; nsresult mResult; QuotaInfo mQuotaInfo; @@ -266,9 +271,14 @@ NS_IMPL_ISUPPORTS(mozilla::dom::cache::Context::QuotaInitRunnable, nsIRunnable); // | (Quota IO Thread) +----------------+ // +----------+------------+ | // | | +// +----------v------------+ | +// | RunOnTarget | Resolve(error) | +// | (Target Thread) +----------------+ +// +----------+------------+ | +// | | // +---------v---------+ +------v------+ // | Running | | Completing | -// | (Quota IO Thread) +------------>(Orig Thread)| +// | (Target Thread) +------------>(Orig Thread)| // +-------------------+ +------+------+ // | // +-----v----+ @@ -384,16 +394,27 @@ Context::QuotaInitRunnable::Run() break; } - mState = STATE_RUNNING; - - if (!mQuotaIOThreadAction) { + if (!mInitAction) { resolver->Resolve(NS_OK); break; } + mState = STATE_RUN_ON_TARGET; + + MOZ_ALWAYS_TRUE(NS_SUCCEEDED( + mTarget->Dispatch(this, nsIThread::DISPATCH_NORMAL))); + break; + } + // ------------------- + case STATE_RUN_ON_TARGET: + { + MOZ_ASSERT(NS_GetCurrentThread() == mTarget); + + mState = STATE_RUNNING; + // Execute the provided initialization Action. The Action must Resolve() // before returning. - mQuotaIOThreadAction->RunOnTarget(resolver, mQuotaInfo, nullptr); + mInitAction->RunOnTarget(resolver, mQuotaInfo, nullptr); MOZ_ASSERT(resolver->Resolved()); break; @@ -402,8 +423,8 @@ Context::QuotaInitRunnable::Run() case STATE_COMPLETING: { NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable); - if (mQuotaIOThreadAction) { - mQuotaIOThreadAction->CompleteOnInitiatingThread(mResult); + if (mInitAction) { + mInitAction->CompleteOnInitiatingThread(mResult); } mContext->OnQuotaInit(mResult, mQuotaInfo, mOfflineStorage); mState = STATE_COMPLETE; @@ -776,14 +797,14 @@ Context::ThreadsafeHandle::ContextDestroyed(Context* aContext) // static already_AddRefed Context::Create(Manager* aManager, nsIThread* aTarget, - Action* aQuotaIOThreadAction, Context* aOldContext) + Action* aInitAction, Context* aOldContext) { nsRefPtr context = new Context(aManager, aTarget); // Do this here to avoid doing an AddRef() in the constructor // TODO: pass context->mData to allow connetion sharing with init context->mInitRunnable = new QuotaInitRunnable(context, aManager, - aQuotaIOThreadAction); + aTarget, aInitAction); if (aOldContext) { aOldContext->SetNextContext(context); diff --git a/dom/cache/Context.h b/dom/cache/Context.h index 50e705f380eb..3b010ca87364 100644 --- a/dom/cache/Context.h +++ b/dom/cache/Context.h @@ -112,7 +112,7 @@ public: // be execute synchronously. static already_AddRefed Create(Manager* aManager, nsIThread* aTarget, - Action* aQuotaIOThreadAction, Context* aOldContext); + Action* aInitAction, Context* aOldContext); // Execute given action on the target once the quota manager has been // initialized.