From 1a8803ebd568b46784c21b7eebb8c74af93c9c87 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Wed, 27 Jul 2016 20:36:10 -0400 Subject: [PATCH] Bug 1170543 P3 Expose ServiceWorker idle thread state to ServiceWorkerManager. r=asuth --- dom/workers/ServiceWorkerManager.cpp | 7 +++++++ dom/workers/ServiceWorkerManager.h | 3 +++ dom/workers/ServiceWorkerPrivate.cpp | 12 ++++++++++++ dom/workers/ServiceWorkerPrivate.h | 3 +++ 4 files changed, 25 insertions(+) diff --git a/dom/workers/ServiceWorkerManager.cpp b/dom/workers/ServiceWorkerManager.cpp index 09441f5866cc..39c5a17c9c13 100644 --- a/dom/workers/ServiceWorkerManager.cpp +++ b/dom/workers/ServiceWorkerManager.cpp @@ -1292,6 +1292,13 @@ ServiceWorkerManager::NotifyUnregister(nsIPrincipal* aPrincipal, return NS_OK; } +void +ServiceWorkerManager::WorkerIsIdle(ServiceWorkerInfo* aWorker) +{ + AssertIsOnMainThread(); + MOZ_ASSERT(aWorker); +} + already_AddRefed ServiceWorkerManager::GetOrCreateJobQueue(const nsACString& aKey, const nsACString& aScope) diff --git a/dom/workers/ServiceWorkerManager.h b/dom/workers/ServiceWorkerManager.h index ed9867fb8193..d9d2732f68bf 100644 --- a/dom/workers/ServiceWorkerManager.h +++ b/dom/workers/ServiceWorkerManager.h @@ -294,6 +294,9 @@ public: nsresult NotifyUnregister(nsIPrincipal* aPrincipal, const nsAString& aScope); + void + WorkerIsIdle(ServiceWorkerInfo* aWorker); + private: ServiceWorkerManager(); ~ServiceWorkerManager(); diff --git a/dom/workers/ServiceWorkerPrivate.cpp b/dom/workers/ServiceWorkerPrivate.cpp index ac95f0526eb6..de34f85a5b61 100644 --- a/dom/workers/ServiceWorkerPrivate.cpp +++ b/dom/workers/ServiceWorkerPrivate.cpp @@ -1899,6 +1899,13 @@ ServiceWorkerPrivate::DetachDebugger() return NS_OK; } +bool +ServiceWorkerPrivate::IsIdle() const +{ + AssertIsOnMainThread(); + return mTokenCount == 0 || (mTokenCount == 1 && mIdleKeepAliveToken); +} + /* static */ void ServiceWorkerPrivate::NoteIdleWorkerCallback(nsITimer* aTimer, void* aPrivate) { @@ -1994,6 +2001,11 @@ ServiceWorkerPrivate::ReleaseToken() --mTokenCount; if (!mTokenCount) { TerminateWorker(); + } else if (IsIdle()) { + RefPtr swm = ServiceWorkerManager::GetInstance(); + if (swm) { + swm->WorkerIsIdle(mInfo); + } } } diff --git a/dom/workers/ServiceWorkerPrivate.h b/dom/workers/ServiceWorkerPrivate.h index 2ba83823653e..127c2ee2e63d 100644 --- a/dom/workers/ServiceWorkerPrivate.h +++ b/dom/workers/ServiceWorkerPrivate.h @@ -146,6 +146,9 @@ public: nsresult DetachDebugger(); + bool + IsIdle() const; + private: enum WakeUpReason { FetchEvent = 0,