diff --git a/dom/workers/ServiceWorkerClient.cpp b/dom/workers/ServiceWorkerClient.cpp index 13a4d9d9b8f0..f7bc5b961924 100644 --- a/dom/workers/ServiceWorkerClient.cpp +++ b/dom/workers/ServiceWorkerClient.cpp @@ -231,7 +231,7 @@ ServiceWorkerClient::PostMessage(JSContext* aCx, JS::Handle aMessage, return; } - aRv = NS_DispatchToMainThread(runnable); + aRv = workerPrivate->DispatchToMainThread(runnable.forget()); if (NS_WARN_IF(aRv.Failed())) { return; } diff --git a/dom/workers/ServiceWorkerClients.cpp b/dom/workers/ServiceWorkerClients.cpp index 408bbef0b798..ba8aca79a6d5 100644 --- a/dom/workers/ServiceWorkerClients.cpp +++ b/dom/workers/ServiceWorkerClients.cpp @@ -675,7 +675,7 @@ ServiceWorkerClients::Get(const nsAString& aClientId, ErrorResult& aRv) RefPtr r = new GetRunnable(promiseProxy, aClientId); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); + MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); return promise.forget(); } @@ -711,7 +711,7 @@ ServiceWorkerClients::MatchAll(const ClientQueryOptions& aOptions, new MatchAllRunnable(promiseProxy, NS_ConvertUTF16toUTF8(scope), aOptions.mIncludeUncontrolled); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); + MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); return promise.forget(); } @@ -752,7 +752,7 @@ ServiceWorkerClients::OpenWindow(const nsAString& aUrl, RefPtr r = new OpenWindowRunnable(promiseProxy, aUrl, scope); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); + MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); return promise.forget(); } @@ -781,6 +781,6 @@ ServiceWorkerClients::Claim(ErrorResult& aRv) RefPtr runnable = new ClaimRunnable(promiseProxy, NS_ConvertUTF16toUTF8(scope)); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable)); + MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(runnable.forget())); return promise.forget(); } diff --git a/dom/workers/ServiceWorkerEvents.cpp b/dom/workers/ServiceWorkerEvents.cpp index fee9069deec1..df78bd1f57e0 100644 --- a/dom/workers/ServiceWorkerEvents.cpp +++ b/dom/workers/ServiceWorkerEvents.cpp @@ -394,7 +394,13 @@ void RespondWithCopyComplete(void* aClosure, nsresult aStatus) data->mScriptSpec, data->mResponseURLSpec); } - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event)); + // In theory this can happen after the worker thread is terminated. + WorkerPrivate* worker = GetCurrentThreadWorkerPrivate(); + if (worker) { + MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(event.forget())); + } else { + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(event.forget())); + } } namespace { @@ -724,7 +730,13 @@ RespondWithHandler::CancelRequest(nsresult aStatus) { nsCOMPtr runnable = new CancelChannelRunnable(mInterceptedChannel, mRegistration, aStatus); - NS_DispatchToMainThread(runnable); + // Note, this may run off the worker thread during worker termination. + WorkerPrivate* worker = GetCurrentThreadWorkerPrivate(); + if (worker) { + MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(runnable.forget())); + } else { + MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable.forget())); + } mRequestWasHandled = true; } @@ -857,8 +869,8 @@ public: mColumn = column; } - MOZ_ALWAYS_SUCCEEDS( - NS_DispatchToMainThread(NewRunnableMethod(this, &WaitUntilHandler::ReportOnMainThread))); + MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread( + NewRunnableMethod(this, &WaitUntilHandler::ReportOnMainThread))); } void diff --git a/dom/workers/ServiceWorkerPrivate.cpp b/dom/workers/ServiceWorkerPrivate.cpp index 0682aa4c0a03..d24bc11a35b9 100644 --- a/dom/workers/ServiceWorkerPrivate.cpp +++ b/dom/workers/ServiceWorkerPrivate.cpp @@ -212,7 +212,7 @@ private: mDone = true; #endif mCallback->SetResult(aResult); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(mCallback)); + MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(mCallback)); } }; @@ -495,7 +495,7 @@ public: { nsCOMPtr runnable = new RegistrationUpdateRunnable(mRegistration, true /* time check */); - NS_DispatchToMainThread(runnable.forget()); + aWorkerPrivate->DispatchToMainThread(runnable.forget()); ExtendableEventWorkerRunnable::PostRun(aCx, aWorkerPrivate, aRunResult); } @@ -535,7 +535,7 @@ public: Cancel() override { mCallback->SetResult(false); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(mCallback)); + MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(mCallback)); return WorkerRunnable::Cancel(); } @@ -631,7 +631,7 @@ public: mDone = true; mCallback->SetResult(aResult); - nsresult rv = NS_DispatchToMainThread(mCallback); + nsresult rv = mWorkerPrivate->DispatchToMainThread(mCallback); if (NS_WARN_IF(NS_FAILED(rv))) { NS_RUNTIMEABORT("Failed to dispatch life cycle event handler."); } @@ -762,6 +762,7 @@ public: void Report(uint16_t aReason = nsIPushErrorReporter::DELIVERY_INTERNAL_ERROR) { + WorkerPrivate* workerPrivate = mWorkerPrivate; mWorkerPrivate->AssertIsOnWorkerThread(); mWorkerPrivate = nullptr; @@ -773,7 +774,7 @@ public: NewRunnableMethod(this, &PushErrorReporter::ReportOnMainThread, aReason); MOZ_ALWAYS_TRUE(NS_SUCCEEDED( - NS_DispatchToMainThread(runnable.forget()))); + workerPrivate->DispatchToMainThread(runnable.forget()))); } void ReportOnMainThread(uint16_t aReason) @@ -1420,7 +1421,7 @@ public: Cancel() override { nsCOMPtr runnable = new ResumeRequest(mInterceptedChannel); - if (NS_FAILED(NS_DispatchToMainThread(runnable))) { + if (NS_FAILED(mWorkerPrivate->DispatchToMainThread(runnable))) { NS_WARNING("Failed to resume channel on FetchEventRunnable::Cancel()!\n"); } WorkerRunnable::Cancel(); @@ -1534,7 +1535,7 @@ private: NS_ERROR_INTERCEPTION_FAILED); } - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable)); + MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(runnable.forget())); } RefPtr waitUntilPromise = event->GetPromise(); diff --git a/dom/workers/ServiceWorkerRegistration.cpp b/dom/workers/ServiceWorkerRegistration.cpp index 1f619c42cf73..9c9d2b57162f 100644 --- a/dom/workers/ServiceWorkerRegistration.cpp +++ b/dom/workers/ServiceWorkerRegistration.cpp @@ -1110,7 +1110,7 @@ ServiceWorkerRegistrationWorkerThread::Update(ErrorResult& aRv) } RefPtr r = new UpdateRunnable(proxy, mScope); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); + MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(r.forget())); return promise.forget(); } @@ -1142,7 +1142,7 @@ ServiceWorkerRegistrationWorkerThread::Unregister(ErrorResult& aRv) } RefPtr r = new StartUnregisterRunnable(proxy, mScope); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); + MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(r.forget())); return promise.forget(); } @@ -1180,7 +1180,7 @@ ServiceWorkerRegistrationWorkerThread::InitListener() RefPtr r = new StartListeningRunnable(mListener); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); + MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(r.forget())); } class AsyncStopListeningRunnable final : public Runnable @@ -1238,7 +1238,7 @@ ServiceWorkerRegistrationWorkerThread::ReleaseListener(Reason aReason) if (aReason == RegistrationIsGoingAway) { RefPtr r = new AsyncStopListeningRunnable(mListener); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); + MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(r.forget())); } else if (aReason == WorkerIsGoingAway) { RefPtr r = new SyncStopListeningRunnable(mWorkerPrivate, mListener); diff --git a/dom/workers/ServiceWorkerWindowClient.cpp b/dom/workers/ServiceWorkerWindowClient.cpp index 9dd42ee0415f..7cc1eb6c5688 100644 --- a/dom/workers/ServiceWorkerWindowClient.cpp +++ b/dom/workers/ServiceWorkerWindowClient.cpp @@ -183,7 +183,7 @@ ServiceWorkerWindowClient::Focus(ErrorResult& aRv) const if (promiseProxy) { RefPtr r = new ClientFocusRunnable(mWindowId, promiseProxy); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); + MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); } else { promise->MaybeReject(NS_ERROR_DOM_ABORT_ERR); } @@ -516,7 +516,7 @@ ServiceWorkerWindowClient::Navigate(const nsAString& aUrl, ErrorResult& aRv) if (promiseProxy) { RefPtr r = new ClientNavigateRunnable(mWindowId, aUrl, promiseProxy); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r)); + MOZ_ALWAYS_SUCCEEDS(workerPrivate->DispatchToMainThread(r.forget())); } else { promise->MaybeReject(NS_ERROR_DOM_ABORT_ERR); } diff --git a/dom/workers/WorkerScope.cpp b/dom/workers/WorkerScope.cpp index 480fddc8d58b..332b2687255a 100644 --- a/dom/workers/WorkerScope.cpp +++ b/dom/workers/WorkerScope.cpp @@ -697,7 +697,7 @@ ServiceWorkerGlobalScope::SkipWaiting(ErrorResult& aRv) new WorkerScopeSkipWaitingRunnable(promiseProxy, NS_ConvertUTF16toUTF8(mScope)); - MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable)); + MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(runnable.forget())); return promise.forget(); }