diff --git a/dom/serviceworkers/ServiceWorkerRegistration.cpp b/dom/serviceworkers/ServiceWorkerRegistration.cpp index e1aea26affab..1d306444b0d6 100644 --- a/dom/serviceworkers/ServiceWorkerRegistration.cpp +++ b/dom/serviceworkers/ServiceWorkerRegistration.cpp @@ -296,7 +296,6 @@ ServiceWorkerRegistration::ShowNotification(JSContext* aCx, // Until we ship ServiceWorker objects on worker threads the active // worker will always be nullptr. So limit this check to main // thread for now. - MOZ_ASSERT_IF(!NS_IsMainThread(), mDescriptor.GetActive().isNothing()); if (mDescriptor.GetActive().isNothing() && NS_IsMainThread()) { aRv.ThrowTypeError(scope); return nullptr; diff --git a/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp b/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp index 4c903c7511db..78ec762daf21 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp +++ b/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp @@ -102,12 +102,6 @@ ServiceWorkerRegistrationMainThread::RegistrationRemovedInternal() StopListeningForEvents(); } -void -ServiceWorkerRegistrationMainThread::UpdateFound() -{ - mOuter->DispatchTrustedEvent(NS_LITERAL_STRING("updatefound")); -} - void ServiceWorkerRegistrationMainThread::UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor) { @@ -643,6 +637,7 @@ class WorkerListener final : public ServiceWorkerRegistrationListener { ServiceWorkerRegistrationDescriptor mDescriptor; nsMainThreadPtrHandle mInfo; + nsCOMPtr mEventTarget; bool mListeningForEvents; // Set and unset on worker thread, used on main-thread and protected by mutex. @@ -654,13 +649,16 @@ public: NS_INLINE_DECL_THREADSAFE_REFCOUNTING(WorkerListener, override) WorkerListener(ServiceWorkerRegistrationWorkerThread* aReg, - const ServiceWorkerRegistrationDescriptor& aDescriptor) + const ServiceWorkerRegistrationDescriptor& aDescriptor, + nsISerialEventTarget* aEventTarget) : mDescriptor(aDescriptor) + , mEventTarget(aEventTarget) , mListeningForEvents(false) , mRegistration(aReg) , mMutex("WorkerListener::mMutex") { MOZ_ASSERT(IsCurrentThreadRunningWorker()); + MOZ_ASSERT(mEventTarget); MOZ_ASSERT(mRegistration); } @@ -700,15 +698,30 @@ public: } // ServiceWorkerRegistrationListener - void - UpdateFound() override; - void UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor) override { MOZ_ASSERT(NS_IsMainThread()); + mDescriptor = aDescriptor; - // TODO: Not implemented + + nsCOMPtr r = + NewCancelableRunnableMethod( + "WorkerListener::UpdateState", + this, + &WorkerListener::UpdateStateOnWorkerThread, + aDescriptor); + + Unused << mEventTarget->Dispatch(r.forget(), NS_DISPATCH_NORMAL); + } + + void + UpdateStateOnWorkerThread(const ServiceWorkerRegistrationDescriptor& aDescriptor) + { + MOZ_ASSERT(IsCurrentThreadRunningWorker()); + if (mRegistration) { + mRegistration->UpdateState(aDescriptor); + } } void @@ -923,7 +936,7 @@ ServiceWorkerRegistrationWorkerThread::InitListener() return; } - mListener = new WorkerListener(this, mDescriptor); + mListener = new WorkerListener(this, mDescriptor, worker->HybridEventTarget()); nsCOMPtr r = NewRunnableMethod("dom::WorkerListener::StartListeningForEvents", @@ -944,9 +957,9 @@ ServiceWorkerRegistrationWorkerThread::ReleaseListener() mListener->ClearRegistration(); nsCOMPtr r = - NewRunnableMethod("dom::WorkerListener::StopListeningForEvents", - mListener, - &WorkerListener::StopListeningForEvents); + NewCancelableRunnableMethod("dom::WorkerListener::StopListeningForEvents", + mListener, + &WorkerListener::StopListeningForEvents); // Calling GetPrivate() is safe because this method is called when the // WorkerRef is notified. MOZ_ALWAYS_SUCCEEDS(mWorkerRef->GetPrivate()->DispatchToMainThread(r.forget())); @@ -955,47 +968,12 @@ ServiceWorkerRegistrationWorkerThread::ReleaseListener() mWorkerRef = nullptr; } -class FireUpdateFoundRunnable final : public WorkerRunnable -{ - RefPtr mListener; -public: - FireUpdateFoundRunnable(WorkerPrivate* aWorkerPrivate, - WorkerListener* aListener) - : WorkerRunnable(aWorkerPrivate) - , mListener(aListener) - { - // Need this assertion for now since runnables which modify busy count can - // only be dispatched from parent thread to worker thread and we don't deal - // with nested workers. SW threads can't be nested. - MOZ_ASSERT(aWorkerPrivate->IsServiceWorker()); - } - - bool - WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override - { - MOZ_ASSERT(aWorkerPrivate); - aWorkerPrivate->AssertIsOnWorkerThread(); - mListener->UpdateFound(); - return true; - } -}; - void -WorkerListener::UpdateFound() +ServiceWorkerRegistrationWorkerThread::UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor) { - MutexAutoLock lock(mMutex); - if (!mRegistration) { - return; + if (mOuter) { + mOuter->UpdateState(aDescriptor); } - - if (NS_IsMainThread()) { - RefPtr r = - new FireUpdateFoundRunnable(mRegistration->GetWorkerPrivate(lock), this); - Unused << NS_WARN_IF(!r->Dispatch()); - return; - } - - mRegistration->UpdateFound(); } class RegistrationRemovedWorkerRunnable final : public WorkerRunnable @@ -1043,12 +1021,6 @@ WorkerListener::RegistrationRemoved() mRegistration->RegistrationRemoved(); } -void -ServiceWorkerRegistrationWorkerThread::UpdateFound() -{ - mOuter->DispatchTrustedEvent(NS_LITERAL_STRING("updatefound")); -} - WorkerPrivate* ServiceWorkerRegistrationWorkerThread::GetWorkerPrivate(const MutexAutoLock& aProofOfLock) { diff --git a/dom/serviceworkers/ServiceWorkerRegistrationImpl.h b/dom/serviceworkers/ServiceWorkerRegistrationImpl.h index 63ef1dc711ba..3e8e3e94f011 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationImpl.h +++ b/dom/serviceworkers/ServiceWorkerRegistrationImpl.h @@ -51,9 +51,6 @@ public: ServiceWorkerFailureCallback&& aFailureCB) override; // ServiceWorkerRegistrationListener - void - UpdateFound() override; - void UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor) override; @@ -120,9 +117,6 @@ public: Unregister(ServiceWorkerBoolCallback&& aSuccessCB, ServiceWorkerFailureCallback&& aFailureCB) override; - void - UpdateFound(); - private: ~ServiceWorkerRegistrationWorkerThread(); @@ -132,6 +126,9 @@ private: void ReleaseListener(); + void + UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor); + // This can be called only by WorkerListener. WorkerPrivate* GetWorkerPrivate(const MutexAutoLock& aProofOfLock); diff --git a/dom/serviceworkers/ServiceWorkerRegistrationInfo.cpp b/dom/serviceworkers/ServiceWorkerRegistrationInfo.cpp index e63f61140484..56028f3507fb 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationInfo.cpp +++ b/dom/serviceworkers/ServiceWorkerRegistrationInfo.cpp @@ -806,16 +806,6 @@ ServiceWorkerRegistrationInfo::GetUpdateDelay() return delay; } -void -ServiceWorkerRegistrationInfo::FireUpdateFound() -{ - nsTObserverArray::ForwardIterator it(mInstanceList); - while (it.HasMore()) { - RefPtr target = it.GetNext(); - target->UpdateFound(); - } -} - void ServiceWorkerRegistrationInfo::NotifyRemoved() { diff --git a/dom/serviceworkers/ServiceWorkerRegistrationListener.h b/dom/serviceworkers/ServiceWorkerRegistrationListener.h index b1ea8c0a8ad7..27acc410b7eb 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationListener.h +++ b/dom/serviceworkers/ServiceWorkerRegistrationListener.h @@ -19,9 +19,6 @@ class ServiceWorkerRegistrationListener public: NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING - virtual void - UpdateFound() = 0; - virtual void UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor) = 0; diff --git a/dom/serviceworkers/ServiceWorkerUpdateJob.cpp b/dom/serviceworkers/ServiceWorkerUpdateJob.cpp index dadd1e1338c3..3b45264877c0 100644 --- a/dom/serviceworkers/ServiceWorkerUpdateJob.cpp +++ b/dom/serviceworkers/ServiceWorkerUpdateJob.cpp @@ -539,12 +539,9 @@ ServiceWorkerUpdateJob::Install() // The job promise cannot be rejected after this point, but the job can // still fail; e.g. if the install event handler throws, etc. - // fire the updatefound event - nsCOMPtr upr = NewRunnableMethod( - "ServiceWorkerRegistrationInfo::FireUpdateFound", - mRegistration, - &ServiceWorkerRegistrationInfo::FireUpdateFound); - NS_DispatchToMainThread(upr); + // Note, the updatefound event is fired automatically when the installing + // property is set on the ServiceWorkerRegistration binding object. This + // happens via the TransitionEvaluatingToInstalling() call above. nsMainThreadPtrHandle handle( new nsMainThreadPtrHolder(