diff --git a/ipc/glue/BackgroundImpl.cpp b/ipc/glue/BackgroundImpl.cpp index 99b3b4a0bf57..a374d54767a9 100644 --- a/ipc/glue/BackgroundImpl.cpp +++ b/ipc/glue/BackgroundImpl.cpp @@ -95,7 +95,6 @@ class ParentImpl final : public BackgroundParentImpl { private: class ShutdownObserver; - class ForceCloseBackgroundActorsRunnable; class ConnectActorRunnable; class CreateActorHelper; @@ -547,25 +546,6 @@ class ParentImpl::ShutdownObserver final : public nsIObserver { ~ShutdownObserver() { AssertIsOnMainThread(); } }; -class ParentImpl::ForceCloseBackgroundActorsRunnable final : public Runnable { - nsTArray* mActorArray; - - public: - explicit ForceCloseBackgroundActorsRunnable( - nsTArray* aActorArray) - : Runnable("Background::ParentImpl::ForceCloseBackgroundActorsRunnable"), - mActorArray(aActorArray) { - AssertIsInMainOrSocketProcess(); - AssertIsOnMainThread(); - MOZ_ASSERT(aActorArray); - } - - private: - ~ForceCloseBackgroundActorsRunnable() = default; - - NS_DECL_NSIRUNNABLE -}; - class ParentImpl::ConnectActorRunnable final : public Runnable { RefPtr mActor; Endpoint mEndpoint; @@ -1367,10 +1347,24 @@ void ParentImpl::ShutdownTimerCallback(nsITimer* aTimer, void* aClosure) { // finished. sLiveActorCount++; - nsCOMPtr forceCloseRunnable = - new ForceCloseBackgroundActorsRunnable(closure->mLiveActors); - MOZ_ALWAYS_SUCCEEDS( - closure->mThread->Dispatch(forceCloseRunnable, NS_DISPATCH_NORMAL)); + InvokeAsync(closure->mThread, __func__, + [liveActors = closure->mLiveActors]() { + MOZ_ASSERT(liveActors); + + if (!liveActors->IsEmpty()) { + // Copy the array since calling Close() could mutate the + // actual array. + nsTArray actorsToClose(liveActors->Clone()); + for (ParentImpl* actor : actorsToClose) { + actor->Close(); + } + } + return GenericPromise::CreateAndResolve(true, __func__); + }) + ->Then(GetCurrentThreadSerialEventTarget(), __func__, []() { + MOZ_ASSERT(sLiveActorCount); + sLiveActorCount--; + }); } void ParentImpl::Destroy() { @@ -1445,33 +1439,6 @@ ParentImpl::ShutdownObserver::Observe(nsISupports* aSubject, const char* aTopic, return NS_OK; } -NS_IMETHODIMP -ParentImpl::ForceCloseBackgroundActorsRunnable::Run() { - AssertIsInMainOrSocketProcess(); - MOZ_ASSERT(mActorArray); - - if (NS_IsMainThread()) { - MOZ_ASSERT(sLiveActorCount); - sLiveActorCount--; - return NS_OK; - } - - AssertIsOnBackgroundThread(); - - if (!mActorArray->IsEmpty()) { - // Copy the array since calling Close() could mutate the actual array. - nsTArray actorsToClose(mActorArray->Clone()); - - for (uint32_t index = 0; index < actorsToClose.Length(); index++) { - actorsToClose[index]->Close(); - } - } - - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(this)); - - return NS_OK; -} - NS_IMETHODIMP ParentImpl::ConnectActorRunnable::Run() { AssertIsInMainOrSocketProcess();