# HG changeset patch # User Ben Kelly # Date 1517418626 28800 # Wed Jan 31 09:10:26 2018 -0800 # Node ID bb38a0b7b6dd6a21ad52e8d2722891fcae520f21 # Parent a246a99f932b26f44c1c691ba741025a5d38c1d3 Bug 1434342 P2 Make ServiceWorker store and use a ServiceWorkerDescriptor internally. r=asuth diff --git a/dom/serviceworkers/ServiceWorker.cpp b/dom/serviceworkers/ServiceWorker.cpp --- a/dom/serviceworkers/ServiceWorker.cpp +++ b/dom/serviceworkers/ServiceWorker.cpp @@ -56,24 +56,25 @@ ServiceWorker::Create(nsIGlobalObject* a return ref.forget(); } RefPtr info = reg->GetByID(aDescriptor.Id()); if (!info) { return ref.forget(); } - ref = new ServiceWorker(aOwner, info); + ref = new ServiceWorker(aOwner, aDescriptor, info); return ref.forget(); } ServiceWorker::ServiceWorker(nsIGlobalObject* aGlobal, ServiceWorkerInfo* aInfo) - : DOMEventTargetHelper(aGlobal), - mInfo(aInfo) + : DOMEventTargetHelper(aGlobal) + , mDescriptor(aDescriptor) + , mInfo(aInfo) { MOZ_ASSERT(NS_IsMainThread()); MOZ_ASSERT(aInfo); // This will update our state too. mInfo->AppendWorker(this); } @@ -92,20 +93,38 @@ NS_INTERFACE_MAP_END_INHERITING(DOMEvent JSObject* ServiceWorker::WrapObject(JSContext* aCx, JS::Handle aGivenProto) { MOZ_ASSERT(NS_IsMainThread()); return ServiceWorkerBinding::Wrap(aCx, this, aGivenProto); } +ServiceWorkerState +ServiceWorker::State() const +{ + return mDescriptor.State(); +} + +void +ServiceWorker::SetState(ServiceWorkerState aState) +{ + mDescriptor.SetState(aState); +} + void ServiceWorker::GetScriptURL(nsString& aURL) const { - CopyUTF8toUTF16(mInfo->ScriptSpec(), aURL); + CopyUTF8toUTF16(mDescriptor.ScriptURL(), aURL); +} + +void +ServiceWorker::DispatchStateChange(ServiceWorkerState aState) +{ + DOMEventTargetHelper::DispatchTrustedEvent(NS_LITERAL_STRING("statechange")); } void ServiceWorker::PostMessage(JSContext* aCx, JS::Handle aMessage, const Sequence& aTransferable, ErrorResult& aRv) { if (State() == ServiceWorkerState::Redundant) { diff --git a/dom/serviceworkers/ServiceWorker.h b/dom/serviceworkers/ServiceWorker.h --- a/dom/serviceworkers/ServiceWorker.h +++ b/dom/serviceworkers/ServiceWorker.h @@ -5,16 +5,17 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef mozilla_dom_serviceworker_h__ #define mozilla_dom_serviceworker_h__ #include "mozilla/DOMEventTargetHelper.h" #include "mozilla/dom/BindingDeclarations.h" #include "mozilla/dom/ServiceWorkerBinding.h" // For ServiceWorkerState. +#include "mozilla/dom/ServiceWorkerDescriptor.h" class nsIGlobalObject; namespace mozilla { namespace dom { class ServiceWorkerInfo; class ServiceWorkerManager; @@ -33,50 +34,43 @@ public: static already_AddRefed Create(nsIGlobalObject* aOwner, const ServiceWorkerDescriptor& aDescriptor); virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; ServiceWorkerState - State() const - { - return mState; - } + State() const; void - SetState(ServiceWorkerState aState) - { - mState = aState; - } + SetState(ServiceWorkerState aState); void GetScriptURL(nsString& aURL) const; void - DispatchStateChange(ServiceWorkerState aState) - { - DOMEventTargetHelper::DispatchTrustedEvent(NS_LITERAL_STRING("statechange")); - } + DispatchStateChange(ServiceWorkerState aState); #ifdef XP_WIN #undef PostMessage #endif void PostMessage(JSContext* aCx, JS::Handle aMessage, const Sequence& aTransferable, ErrorResult& aRv); private: - ServiceWorker(nsIGlobalObject* aWindow, ServiceWorkerInfo* aInfo); + ServiceWorker(nsIGlobalObject* aWindow, + const ServiceWorkerDescriptor& aDescriptor, + ServiceWorkerInfo* aInfo); // This class is reference-counted and will be destroyed from Release(). ~ServiceWorker(); - ServiceWorkerState mState; + ServiceWorkerDescriptor mDescriptor; const RefPtr mInfo; }; } // namespace dom } // namespace mozilla #endif // mozilla_dom_serviceworker_h__