diff --git a/dom/serviceworkers/ServiceWorkerRegistration.cpp b/dom/serviceworkers/ServiceWorkerRegistration.cpp index 6d620818d856..731543135fac 100644 --- a/dom/serviceworkers/ServiceWorkerRegistration.cpp +++ b/dom/serviceworkers/ServiceWorkerRegistration.cpp @@ -65,5 +65,70 @@ ServiceWorkerRegistration::CreateForWorker(WorkerPrivate* aWorkerPrivate, return registration.forget(); } +already_AddRefed +ServiceWorkerRegistration::GetInstalling() const +{ + RefPtr ref = mInstallingWorker; + return ref.forget(); +} + +already_AddRefed +ServiceWorkerRegistration::GetWaiting() const +{ + RefPtr ref = mWaitingWorker; + return ref.forget(); +} + +already_AddRefed +ServiceWorkerRegistration::GetActive() const +{ + RefPtr ref = mActiveWorker; + return ref.forget(); +} + +void +ServiceWorkerRegistration::UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor) +{ + MOZ_DIAGNOSTIC_ASSERT(MatchesDescriptor(aDescriptor)); + + mDescriptor = aDescriptor; + + nsCOMPtr global = GetParentObject(); + if (!global) { + mInstallingWorker = nullptr; + mWaitingWorker = nullptr; + mActiveWorker = nullptr; + return; + } + + Maybe active = aDescriptor.GetActive(); + if (active.isSome()) { + mActiveWorker = global->GetOrCreateServiceWorker(active.ref()); + } else { + mActiveWorker = nullptr; + } + + Maybe waiting = aDescriptor.GetWaiting(); + if (waiting.isSome()) { + mWaitingWorker = global->GetOrCreateServiceWorker(waiting.ref()); + } else { + mWaitingWorker = nullptr; + } + + Maybe installing = aDescriptor.GetInstalling(); + if (installing.isSome()) { + mInstallingWorker = global->GetOrCreateServiceWorker(installing.ref()); + } else { + mInstallingWorker = nullptr; + } +} + +bool +ServiceWorkerRegistration::MatchesDescriptor(const ServiceWorkerRegistrationDescriptor& aDescriptor) const +{ + return aDescriptor.PrincipalInfo() == mDescriptor.PrincipalInfo() && + aDescriptor.Scope() == mDescriptor.Scope(); +} + } // dom namespace } // mozilla namespace diff --git a/dom/serviceworkers/ServiceWorkerRegistration.h b/dom/serviceworkers/ServiceWorkerRegistration.h index e7d1378d6ae2..39ff2a40f34c 100644 --- a/dom/serviceworkers/ServiceWorkerRegistration.h +++ b/dom/serviceworkers/ServiceWorkerRegistration.h @@ -9,6 +9,7 @@ #include "mozilla/DOMEventTargetHelper.h" #include "mozilla/dom/DOMPrefs.h" +#include "mozilla/dom/ServiceWorker.h" #include "mozilla/dom/ServiceWorkerBinding.h" #include "mozilla/dom/ServiceWorkerRegistrationBinding.h" #include "mozilla/dom/ServiceWorkerRegistrationDescriptor.h" @@ -43,14 +44,20 @@ public: JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; - virtual already_AddRefed - GetInstalling() = 0; + already_AddRefed + GetInstalling() const; - virtual already_AddRefed - GetWaiting() = 0; + already_AddRefed + GetWaiting() const; - virtual already_AddRefed - GetActive() = 0; + already_AddRefed + GetActive() const; + + void + UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor); + + bool + MatchesDescriptor(const ServiceWorkerRegistrationDescriptor& aDescriptor) const; virtual void GetScope(nsAString& aScope) const = 0; @@ -85,6 +92,9 @@ protected: { } ServiceWorkerRegistrationDescriptor mDescriptor; + RefPtr mInstallingWorker; + RefPtr mWaitingWorker; + RefPtr mActiveWorker; }; diff --git a/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp b/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp index 280a83bb118f..f339757fc3cd 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp +++ b/dom/serviceworkers/ServiceWorkerRegistrationImpl.cpp @@ -97,30 +97,6 @@ ServiceWorkerRegistrationMainThread::StopListeningForEvents() mListeningForEvents = false; } -already_AddRefed -ServiceWorkerRegistrationMainThread::GetInstalling() -{ - MOZ_ASSERT(NS_IsMainThread()); - RefPtr ret = mInstallingWorker; - return ret.forget(); -} - -already_AddRefed -ServiceWorkerRegistrationMainThread::GetWaiting() -{ - MOZ_ASSERT(NS_IsMainThread()); - RefPtr ret = mWaitingWorker; - return ret.forget(); -} - -already_AddRefed -ServiceWorkerRegistrationMainThread::GetActive() -{ - MOZ_ASSERT(NS_IsMainThread()); - RefPtr ret = mActiveWorker; - return ret.forget(); -} - void ServiceWorkerRegistrationMainThread::UpdateFound() { @@ -130,38 +106,7 @@ ServiceWorkerRegistrationMainThread::UpdateFound() void ServiceWorkerRegistrationMainThread::UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor) { - MOZ_DIAGNOSTIC_ASSERT(MatchesDescriptor(aDescriptor)); - - mDescriptor = aDescriptor; - - nsCOMPtr global = GetParentObject(); - if (!global) { - mInstallingWorker = nullptr; - mWaitingWorker = nullptr; - mActiveWorker = nullptr; - return; - } - - Maybe active = aDescriptor.GetActive(); - if (active.isSome()) { - mActiveWorker = global->GetOrCreateServiceWorker(active.ref()); - } else { - mActiveWorker = nullptr; - } - - Maybe waiting = aDescriptor.GetWaiting(); - if (waiting.isSome()) { - mWaitingWorker = global->GetOrCreateServiceWorker(waiting.ref()); - } else { - mWaitingWorker = nullptr; - } - - Maybe installing = aDescriptor.GetInstalling(); - if (installing.isSome()) { - mInstallingWorker = global->GetOrCreateServiceWorker(installing.ref()); - } else { - mInstallingWorker = nullptr; - } + ServiceWorkerRegistration::UpdateState(aDescriptor); } void @@ -178,8 +123,7 @@ ServiceWorkerRegistrationMainThread::RegistrationRemoved() bool ServiceWorkerRegistrationMainThread::MatchesDescriptor(const ServiceWorkerRegistrationDescriptor& aDescriptor) { - return aDescriptor.PrincipalInfo() == mDescriptor.PrincipalInfo() && - aDescriptor.Scope() == mDescriptor.Scope(); + return ServiceWorkerRegistration::MatchesDescriptor(aDescriptor); } namespace { @@ -883,27 +827,6 @@ ServiceWorkerRegistrationWorkerThread::~ServiceWorkerRegistrationWorkerThread() MOZ_ASSERT(!mListener); } -already_AddRefed -ServiceWorkerRegistrationWorkerThread::GetInstalling() -{ - // FIXME(nsm): Will be implemented after Bug 1113522. - return nullptr; -} - -already_AddRefed -ServiceWorkerRegistrationWorkerThread::GetWaiting() -{ - // FIXME(nsm): Will be implemented after Bug 1113522. - return nullptr; -} - -already_AddRefed -ServiceWorkerRegistrationWorkerThread::GetActive() -{ - // FIXME(nsm): Will be implemented after Bug 1113522. - return nullptr; -} - already_AddRefed ServiceWorkerRegistrationWorkerThread::Update(ErrorResult& aRv) { diff --git a/dom/serviceworkers/ServiceWorkerRegistrationImpl.h b/dom/serviceworkers/ServiceWorkerRegistrationImpl.h index 35c38cba3da4..8a0a54ee9085 100644 --- a/dom/serviceworkers/ServiceWorkerRegistrationImpl.h +++ b/dom/serviceworkers/ServiceWorkerRegistrationImpl.h @@ -52,15 +52,6 @@ public: GetNotifications(const GetNotificationOptions& aOptions, ErrorResult& aRv) override; - already_AddRefed - GetInstalling() override; - - already_AddRefed - GetWaiting() override; - - already_AddRefed - GetActive() override; - already_AddRefed GetPushManager(JSContext* aCx, ErrorResult& aRv) override; @@ -141,14 +132,6 @@ private: const nsString mScope; bool mListeningForEvents; - // The following properties are cached here to ensure JS equality is satisfied - // instead of acquiring a new worker instance from the ServiceWorkerManager - // for every access. A null value is considered a cache miss. - // These three may change to a new worker at any time. - RefPtr mInstallingWorker; - RefPtr mWaitingWorker; - RefPtr mActiveWorker; - RefPtr mPushManager; }; @@ -185,15 +168,6 @@ public: GetNotifications(const GetNotificationOptions& aOptions, ErrorResult& aRv) override; - already_AddRefed - GetInstalling() override; - - already_AddRefed - GetWaiting() override; - - already_AddRefed - GetActive() override; - void GetScope(nsAString& aScope) const override {