Bug 1471929 P3 Remove the old updatefound infrastructure in favor of always updating the registration state and auto-dispatch. r=mrbkap

This commit is contained in:
Ben Kelly 2018-07-05 13:13:48 -04:00
parent d7514a28d7
commit 4405c076b5
6 changed files with 37 additions and 85 deletions

View File

@ -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<MSG_NO_ACTIVE_WORKER>(scope);
return nullptr;

View File

@ -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<ServiceWorkerRegistrationInfo> mInfo;
nsCOMPtr<nsISerialEventTarget> 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<nsIRunnable> r =
NewCancelableRunnableMethod<ServiceWorkerRegistrationDescriptor>(
"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<nsIRunnable> r =
NewRunnableMethod("dom::WorkerListener::StartListeningForEvents",
@ -944,9 +957,9 @@ ServiceWorkerRegistrationWorkerThread::ReleaseListener()
mListener->ClearRegistration();
nsCOMPtr<nsIRunnable> 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<WorkerListener> 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<FireUpdateFoundRunnable> 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)
{

View File

@ -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);

View File

@ -806,16 +806,6 @@ ServiceWorkerRegistrationInfo::GetUpdateDelay()
return delay;
}
void
ServiceWorkerRegistrationInfo::FireUpdateFound()
{
nsTObserverArray<ServiceWorkerRegistrationListener*>::ForwardIterator it(mInstanceList);
while (it.HasMore()) {
RefPtr<ServiceWorkerRegistrationListener> target = it.GetNext();
target->UpdateFound();
}
}
void
ServiceWorkerRegistrationInfo::NotifyRemoved()
{

View File

@ -19,9 +19,6 @@ class ServiceWorkerRegistrationListener
public:
NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
virtual void
UpdateFound() = 0;
virtual void
UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor) = 0;

View File

@ -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<nsIRunnable> 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<ServiceWorkerUpdateJob> handle(
new nsMainThreadPtrHolder<ServiceWorkerUpdateJob>(