Bug 1434701 P8 Make ServiceWorkerRegistration own the ServiceWorker references itself and handle the descriptor update. r=asuth

This commit is contained in:
Ben Kelly 2018-02-14 14:23:16 -05:00
parent bed54ad0bb
commit b29c22e698
4 changed files with 83 additions and 111 deletions

View File

@ -65,5 +65,70 @@ ServiceWorkerRegistration::CreateForWorker(WorkerPrivate* aWorkerPrivate,
return registration.forget();
}
already_AddRefed<ServiceWorker>
ServiceWorkerRegistration::GetInstalling() const
{
RefPtr<ServiceWorker> ref = mInstallingWorker;
return ref.forget();
}
already_AddRefed<ServiceWorker>
ServiceWorkerRegistration::GetWaiting() const
{
RefPtr<ServiceWorker> ref = mWaitingWorker;
return ref.forget();
}
already_AddRefed<ServiceWorker>
ServiceWorkerRegistration::GetActive() const
{
RefPtr<ServiceWorker> ref = mActiveWorker;
return ref.forget();
}
void
ServiceWorkerRegistration::UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor)
{
MOZ_DIAGNOSTIC_ASSERT(MatchesDescriptor(aDescriptor));
mDescriptor = aDescriptor;
nsCOMPtr<nsIGlobalObject> global = GetParentObject();
if (!global) {
mInstallingWorker = nullptr;
mWaitingWorker = nullptr;
mActiveWorker = nullptr;
return;
}
Maybe<ServiceWorkerDescriptor> active = aDescriptor.GetActive();
if (active.isSome()) {
mActiveWorker = global->GetOrCreateServiceWorker(active.ref());
} else {
mActiveWorker = nullptr;
}
Maybe<ServiceWorkerDescriptor> waiting = aDescriptor.GetWaiting();
if (waiting.isSome()) {
mWaitingWorker = global->GetOrCreateServiceWorker(waiting.ref());
} else {
mWaitingWorker = nullptr;
}
Maybe<ServiceWorkerDescriptor> 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

View File

@ -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<JSObject*> aGivenProto) override;
virtual already_AddRefed<ServiceWorker>
GetInstalling() = 0;
already_AddRefed<ServiceWorker>
GetInstalling() const;
virtual already_AddRefed<ServiceWorker>
GetWaiting() = 0;
already_AddRefed<ServiceWorker>
GetWaiting() const;
virtual already_AddRefed<ServiceWorker>
GetActive() = 0;
already_AddRefed<ServiceWorker>
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<ServiceWorker> mInstallingWorker;
RefPtr<ServiceWorker> mWaitingWorker;
RefPtr<ServiceWorker> mActiveWorker;
};

View File

@ -97,30 +97,6 @@ ServiceWorkerRegistrationMainThread::StopListeningForEvents()
mListeningForEvents = false;
}
already_AddRefed<ServiceWorker>
ServiceWorkerRegistrationMainThread::GetInstalling()
{
MOZ_ASSERT(NS_IsMainThread());
RefPtr<ServiceWorker> ret = mInstallingWorker;
return ret.forget();
}
already_AddRefed<ServiceWorker>
ServiceWorkerRegistrationMainThread::GetWaiting()
{
MOZ_ASSERT(NS_IsMainThread());
RefPtr<ServiceWorker> ret = mWaitingWorker;
return ret.forget();
}
already_AddRefed<ServiceWorker>
ServiceWorkerRegistrationMainThread::GetActive()
{
MOZ_ASSERT(NS_IsMainThread());
RefPtr<ServiceWorker> 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<nsIGlobalObject> global = GetParentObject();
if (!global) {
mInstallingWorker = nullptr;
mWaitingWorker = nullptr;
mActiveWorker = nullptr;
return;
}
Maybe<ServiceWorkerDescriptor> active = aDescriptor.GetActive();
if (active.isSome()) {
mActiveWorker = global->GetOrCreateServiceWorker(active.ref());
} else {
mActiveWorker = nullptr;
}
Maybe<ServiceWorkerDescriptor> waiting = aDescriptor.GetWaiting();
if (waiting.isSome()) {
mWaitingWorker = global->GetOrCreateServiceWorker(waiting.ref());
} else {
mWaitingWorker = nullptr;
}
Maybe<ServiceWorkerDescriptor> 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<ServiceWorker>
ServiceWorkerRegistrationWorkerThread::GetInstalling()
{
// FIXME(nsm): Will be implemented after Bug 1113522.
return nullptr;
}
already_AddRefed<ServiceWorker>
ServiceWorkerRegistrationWorkerThread::GetWaiting()
{
// FIXME(nsm): Will be implemented after Bug 1113522.
return nullptr;
}
already_AddRefed<ServiceWorker>
ServiceWorkerRegistrationWorkerThread::GetActive()
{
// FIXME(nsm): Will be implemented after Bug 1113522.
return nullptr;
}
already_AddRefed<Promise>
ServiceWorkerRegistrationWorkerThread::Update(ErrorResult& aRv)
{

View File

@ -52,15 +52,6 @@ public:
GetNotifications(const GetNotificationOptions& aOptions,
ErrorResult& aRv) override;
already_AddRefed<ServiceWorker>
GetInstalling() override;
already_AddRefed<ServiceWorker>
GetWaiting() override;
already_AddRefed<ServiceWorker>
GetActive() override;
already_AddRefed<PushManager>
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<ServiceWorker> mInstallingWorker;
RefPtr<ServiceWorker> mWaitingWorker;
RefPtr<ServiceWorker> mActiveWorker;
RefPtr<PushManager> mPushManager;
};
@ -185,15 +168,6 @@ public:
GetNotifications(const GetNotificationOptions& aOptions,
ErrorResult& aRv) override;
already_AddRefed<ServiceWorker>
GetInstalling() override;
already_AddRefed<ServiceWorker>
GetWaiting() override;
already_AddRefed<ServiceWorker>
GetActive() override;
void
GetScope(nsAString& aScope) const override
{