From a9358d515bb89f34e339d62a7e16417e887c07d4 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Thu, 3 Mar 2016 19:37:57 -0500 Subject: [PATCH] Bug 1252055 P1 Make equivalent ServiceWorker DOM objects strictly equal in js. r=ehsan --- dom/workers/ServiceWorker.h | 4 ++-- dom/workers/ServiceWorkerManager.cpp | 27 +++++++++++++++++++++++++-- dom/workers/ServiceWorkerManager.h | 3 +++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/dom/workers/ServiceWorker.h b/dom/workers/ServiceWorker.h index 323c4c487586..e49f334e6f57 100644 --- a/dom/workers/ServiceWorker.h +++ b/dom/workers/ServiceWorker.h @@ -27,7 +27,7 @@ ServiceWorkerVisible(JSContext* aCx, JSObject* aObj); class ServiceWorker final : public DOMEventTargetHelper { - friend class ServiceWorkerManager; + friend class ServiceWorkerInfo; public: NS_DECL_ISUPPORTS_INHERITED @@ -68,7 +68,7 @@ public: ErrorResult& aRv); private: - // This class can only be created from the ServiceWorkerManager. + // This class can only be created from ServiceWorkerInfo::GetOrCreateInstance(). ServiceWorker(nsPIDOMWindowInner* aWindow, ServiceWorkerInfo* aInfo); // This class is reference-counted and will be destroyed from Release(). diff --git a/dom/workers/ServiceWorkerManager.cpp b/dom/workers/ServiceWorkerManager.cpp index 451c6baadb63..e1e64499a2cf 100644 --- a/dom/workers/ServiceWorkerManager.cpp +++ b/dom/workers/ServiceWorkerManager.cpp @@ -3566,7 +3566,7 @@ ServiceWorkerManager::GetServiceWorkerForScope(nsPIDOMWindowInner* aWindow, return NS_ERROR_DOM_NOT_FOUND_ERR; } - RefPtr serviceWorker = new ServiceWorker(aWindow, info); + RefPtr serviceWorker = info->GetOrCreateInstance(aWindow); serviceWorker->SetState(info->State()); serviceWorker.forget(aServiceWorker); @@ -3793,7 +3793,7 @@ ServiceWorkerManager::GetDocumentController(nsPIDOMWindowInner* aWindow, MOZ_ASSERT(registration->mActiveWorker); RefPtr serviceWorker = - new ServiceWorker(aWindow, registration->mActiveWorker); + registration->mActiveWorker->GetOrCreateInstance(aWindow); serviceWorker.forget(aServiceWorker); return NS_OK; @@ -5250,4 +5250,27 @@ ServiceWorkerInfo::GetNextID() const return ++gServiceWorkerInfoCurrentID; } +already_AddRefed +ServiceWorkerInfo::GetOrCreateInstance(nsPIDOMWindowInner* aWindow) +{ + AssertIsOnMainThread(); + MOZ_ASSERT(aWindow); + + RefPtr ref; + + for (uint32_t i = 0; i < mInstances.Length(); ++i) { + MOZ_ASSERT(mInstances[i]); + if (mInstances[i]->GetOwner() == aWindow) { + ref = mInstances[i]; + break; + } + } + + if (!ref) { + ref = new ServiceWorker(aWindow, this); + } + + return ref.forget(); +} + END_WORKERS_NAMESPACE diff --git a/dom/workers/ServiceWorkerManager.h b/dom/workers/ServiceWorkerManager.h index 3e3efe20d3fa..420a115d6d96 100644 --- a/dom/workers/ServiceWorkerManager.h +++ b/dom/workers/ServiceWorkerManager.h @@ -305,6 +305,9 @@ public: void RemoveWorker(ServiceWorker* aWorker); + + already_AddRefed + GetOrCreateInstance(nsPIDOMWindowInner* aWindow); }; #define NS_SERVICEWORKERMANAGER_IMPL_IID \