From 231fc8a398c3ce17fc487b737781cebd41553263 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Wed, 31 Jan 2018 09:10:25 -0800 Subject: [PATCH] Bug 1434342 P1 Add ServiceWorker::Create() factory method. r=asuth --- dom/serviceworkers/ServiceWorker.cpp | 31 ++++++++++++++++++++++-- dom/serviceworkers/ServiceWorker.h | 9 ++++--- dom/serviceworkers/ServiceWorkerInfo.cpp | 3 ++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/dom/serviceworkers/ServiceWorker.cpp b/dom/serviceworkers/ServiceWorker.cpp index f884125ccf08..bcdf57ec8b35 100644 --- a/dom/serviceworkers/ServiceWorker.cpp +++ b/dom/serviceworkers/ServiceWorker.cpp @@ -38,9 +38,36 @@ ServiceWorkerVisible(JSContext* aCx, JSObject* aObj) return IS_INSTANCE_OF(ServiceWorkerGlobalScope, aObj); } -ServiceWorker::ServiceWorker(nsPIDOMWindowInner* aWindow, +// static +already_AddRefed +ServiceWorker::Create(nsIGlobalObject* aOwner, + const ServiceWorkerDescriptor& aDescriptor) +{ + RefPtr ref; + + RefPtr swm = ServiceWorkerManager::GetInstance(); + if (!swm) { + return ref.forget(); + } + + RefPtr reg = + swm->GetRegistration(aDescriptor.PrincipalInfo(), aDescriptor.Scope()); + if (!reg) { + return ref.forget(); + } + + RefPtr info = reg->GetByID(aDescriptor.Id()); + if (!info) { + return ref.forget(); + } + + ref = new ServiceWorker(aOwner, info); + return ref.forget(); +} + +ServiceWorker::ServiceWorker(nsIGlobalObject* aGlobal, ServiceWorkerInfo* aInfo) - : DOMEventTargetHelper(aWindow), + : DOMEventTargetHelper(aGlobal), mInfo(aInfo) { MOZ_ASSERT(NS_IsMainThread()); diff --git a/dom/serviceworkers/ServiceWorker.h b/dom/serviceworkers/ServiceWorker.h index d46ccc4947c2..499e403576c8 100644 --- a/dom/serviceworkers/ServiceWorker.h +++ b/dom/serviceworkers/ServiceWorker.h @@ -11,7 +11,7 @@ #include "mozilla/dom/BindingDeclarations.h" #include "mozilla/dom/ServiceWorkerBinding.h" // For ServiceWorkerState. -class nsPIDOMWindowInner; +class nsIGlobalObject; namespace mozilla { namespace dom { @@ -25,13 +25,15 @@ ServiceWorkerVisible(JSContext* aCx, JSObject* aObj); class ServiceWorker final : public DOMEventTargetHelper { - friend class ServiceWorkerInfo; public: NS_DECL_ISUPPORTS_INHERITED IMPL_EVENT_HANDLER(statechange) IMPL_EVENT_HANDLER(error) + static already_AddRefed + Create(nsIGlobalObject* aOwner, const ServiceWorkerDescriptor& aDescriptor); + virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; @@ -65,8 +67,7 @@ public: const Sequence& aTransferable, ErrorResult& aRv); private: - // This class can only be created from ServiceWorkerInfo::GetOrCreateInstance(). - ServiceWorker(nsPIDOMWindowInner* aWindow, ServiceWorkerInfo* aInfo); + ServiceWorker(nsIGlobalObject* aWindow, ServiceWorkerInfo* aInfo); // This class is reference-counted and will be destroyed from Release(). ~ServiceWorker(); diff --git a/dom/serviceworkers/ServiceWorkerInfo.cpp b/dom/serviceworkers/ServiceWorkerInfo.cpp index 63264ed6dbe9..da3900c064b7 100644 --- a/dom/serviceworkers/ServiceWorkerInfo.cpp +++ b/dom/serviceworkers/ServiceWorkerInfo.cpp @@ -277,7 +277,8 @@ ServiceWorkerInfo::GetOrCreateInstance(nsPIDOMWindowInner* aWindow) } if (!ref) { - ref = new ServiceWorker(aWindow, this); + nsCOMPtr global(do_QueryInterface(aWindow)); + ref = ServiceWorker::Create(global, mDescriptor); } return ref.forget();