From 7506d483068357c96e11911696e248c9ceb58ef4 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Wed, 14 Feb 2018 14:23:15 -0500 Subject: [PATCH] Bug 1434701 P3 Pass ServiceWorkerRegistrationDescriptor to ServiceWorkerRegistration::CreateForWorker(). r=asuth --- dom/serviceworkers/ServiceWorkerPrivate.cpp | 12 ++++++++---- .../ServiceWorkerRegistration.cpp | 8 +++++--- dom/serviceworkers/ServiceWorkerRegistration.h | 2 +- dom/workers/WorkerLoadInfo.cpp | 1 + dom/workers/WorkerLoadInfo.h | 2 ++ dom/workers/WorkerPrivate.cpp | 4 +++- dom/workers/WorkerPrivate.h | 8 ++++++++ dom/workers/WorkerScope.cpp | 17 +++++++++-------- dom/workers/WorkerScope.h | 3 ++- 9 files changed, 39 insertions(+), 18 deletions(-) diff --git a/dom/serviceworkers/ServiceWorkerPrivate.cpp b/dom/serviceworkers/ServiceWorkerPrivate.cpp index c5fa5494253c..904494b6895e 100644 --- a/dom/serviceworkers/ServiceWorkerPrivate.cpp +++ b/dom/serviceworkers/ServiceWorkerPrivate.cpp @@ -1833,6 +1833,13 @@ ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy, return NS_ERROR_FAILURE; } + RefPtr swm = ServiceWorkerManager::GetInstance(); + NS_ENSURE_TRUE(swm, NS_ERROR_FAILURE); + + RefPtr reg = + swm->GetRegistration(mInfo->Principal(), mInfo->Scope()); + NS_ENSURE_TRUE(reg, NS_ERROR_FAILURE); + // TODO(catalinb): Bug 1192138 - Add telemetry for service worker wake-ups. // Ensure that the IndexedDatabaseManager is initialized @@ -1850,11 +1857,8 @@ ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy, MOZ_ASSERT(!mInfo->CacheName().IsEmpty()); info.mServiceWorkerCacheName = mInfo->CacheName(); - PrincipalInfo principalInfo; - rv = PrincipalToPrincipalInfo(mInfo->Principal(), &principalInfo); - NS_ENSURE_SUCCESS(rv, rv); - info.mServiceWorkerDescriptor.emplace(mInfo->Descriptor()); + info.mServiceWorkerRegistrationDescriptor.emplace(reg->Descriptor()); info.mLoadGroup = aLoadGroup; info.mLoadFailedAsyncRunnable = aLoadFailedRunnable; diff --git a/dom/serviceworkers/ServiceWorkerRegistration.cpp b/dom/serviceworkers/ServiceWorkerRegistration.cpp index 905d5f7dc46a..217ad773b7a4 100644 --- a/dom/serviceworkers/ServiceWorkerRegistration.cpp +++ b/dom/serviceworkers/ServiceWorkerRegistration.cpp @@ -1025,7 +1025,7 @@ public: TransitionWorker(WhichServiceWorker aWhichOne) override { MOZ_ASSERT(NS_IsMainThread()); - NS_WARNING("FIXME: Not implemented!"); + // TODO: Not implemented } void @@ -1367,13 +1367,15 @@ ServiceWorkerRegistration::CreateForMainThread(nsPIDOMWindowInner* aWindow, /* static */ already_AddRefed ServiceWorkerRegistration::CreateForWorker(WorkerPrivate* aWorkerPrivate, - const nsAString& aScope) + const ServiceWorkerRegistrationDescriptor& aDescriptor) { MOZ_ASSERT(aWorkerPrivate); aWorkerPrivate->AssertIsOnWorkerThread(); + NS_ConvertUTF8toUTF16 scope(aDescriptor.Scope()); + RefPtr registration = - new ServiceWorkerRegistrationWorkerThread(aWorkerPrivate, aScope); + new ServiceWorkerRegistrationWorkerThread(aWorkerPrivate, scope); return registration.forget(); } diff --git a/dom/serviceworkers/ServiceWorkerRegistration.h b/dom/serviceworkers/ServiceWorkerRegistration.h index e3309dd29d7e..d42ba4a92723 100644 --- a/dom/serviceworkers/ServiceWorkerRegistration.h +++ b/dom/serviceworkers/ServiceWorkerRegistration.h @@ -63,7 +63,7 @@ public: static already_AddRefed CreateForWorker(WorkerPrivate* aWorkerPrivate, - const nsAString& aScope); + const ServiceWorkerRegistrationDescriptor& aDescriptor); JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; diff --git a/dom/workers/WorkerLoadInfo.cpp b/dom/workers/WorkerLoadInfo.cpp index f9fe6b5ce778..1f64ccba4088 100644 --- a/dom/workers/WorkerLoadInfo.cpp +++ b/dom/workers/WorkerLoadInfo.cpp @@ -146,6 +146,7 @@ WorkerLoadInfo::StealFrom(WorkerLoadInfo& aOther) mOrigin = aOther.mOrigin; mServiceWorkerCacheName = aOther.mServiceWorkerCacheName; mServiceWorkerDescriptor = aOther.mServiceWorkerDescriptor; + mServiceWorkerRegistrationDescriptor = aOther.mServiceWorkerRegistrationDescriptor; mLoadFlags = aOther.mLoadFlags; mWindowID = aOther.mWindowID; mReferrerPolicy = aOther.mReferrerPolicy; diff --git a/dom/workers/WorkerLoadInfo.h b/dom/workers/WorkerLoadInfo.h index 3569c5513b23..d1aa3e3a602f 100644 --- a/dom/workers/WorkerLoadInfo.h +++ b/dom/workers/WorkerLoadInfo.h @@ -8,6 +8,7 @@ #define mozilla_dom_workers_WorkerLoadInfo_h #include "mozilla/dom/ChannelInfo.h" +#include "mozilla/dom/ServiceWorkerRegistrationDescriptor.h" #include "mozilla/dom/WorkerCommon.h" #include "mozilla/net/ReferrerPolicy.h" #include "nsIInterfaceRequestor.h" @@ -92,6 +93,7 @@ struct WorkerLoadInfo nsString mServiceWorkerCacheName; Maybe mServiceWorkerDescriptor; + Maybe mServiceWorkerRegistrationDescriptor; Maybe mParentController; diff --git a/dom/workers/WorkerPrivate.cpp b/dom/workers/WorkerPrivate.cpp index 171370368501..be8b6f7ef4dc 100644 --- a/dom/workers/WorkerPrivate.cpp +++ b/dom/workers/WorkerPrivate.cpp @@ -5140,7 +5140,9 @@ WorkerPrivate::GetOrCreateGlobalScope(JSContext* aCx) if (IsSharedWorker()) { globalScope = new SharedWorkerGlobalScope(this, WorkerName()); } else if (IsServiceWorker()) { - globalScope = new ServiceWorkerGlobalScope(this, ServiceWorkerScope()); + globalScope = + new ServiceWorkerGlobalScope(this, + GetServiceWorkerRegistrationDescriptor()); } else { globalScope = new DedicatedWorkerGlobalScope(this, WorkerName()); } diff --git a/dom/workers/WorkerPrivate.h b/dom/workers/WorkerPrivate.h index 619af6b2d117..17114cd6d2ed 100644 --- a/dom/workers/WorkerPrivate.h +++ b/dom/workers/WorkerPrivate.h @@ -842,6 +842,14 @@ public: return mLoadInfo.mServiceWorkerDescriptor.ref(); } + const ServiceWorkerRegistrationDescriptor& + GetServiceWorkerRegistrationDescriptor() const + { + MOZ_DIAGNOSTIC_ASSERT(IsServiceWorker()); + MOZ_DIAGNOSTIC_ASSERT(mLoadInfo.mServiceWorkerRegistrationDescriptor.isSome()); + return mLoadInfo.mServiceWorkerRegistrationDescriptor.ref(); + } + void UpdateServiceWorkerState(ServiceWorkerState aState) { diff --git a/dom/workers/WorkerScope.cpp b/dom/workers/WorkerScope.cpp index 51c2dbad6694..b465c0900c90 100644 --- a/dom/workers/WorkerScope.cpp +++ b/dom/workers/WorkerScope.cpp @@ -633,9 +633,15 @@ NS_IMPL_ADDREF_INHERITED(ServiceWorkerGlobalScope, WorkerGlobalScope) NS_IMPL_RELEASE_INHERITED(ServiceWorkerGlobalScope, WorkerGlobalScope) ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(WorkerPrivate* aWorkerPrivate, - const nsACString& aScope) - : WorkerGlobalScope(aWorkerPrivate), - mScope(NS_ConvertUTF8toUTF16(aScope)) + const ServiceWorkerRegistrationDescriptor& aRegistrationDescriptor) + : WorkerGlobalScope(aWorkerPrivate) + , mScope(NS_ConvertUTF8toUTF16(aRegistrationDescriptor.Scope())) + + // Eagerly create the registration because we will need to receive updates + // about the state of the registration. We can't wait until first access + // to start receiving these. + , mRegistration(ServiceWorkerRegistration::CreateForWorker(aWorkerPrivate, + aRegistrationDescriptor)) { } @@ -672,11 +678,6 @@ ServiceWorkerGlobalScope::GetClients() ServiceWorkerRegistration* ServiceWorkerGlobalScope::Registration() { - if (!mRegistration) { - mRegistration = - ServiceWorkerRegistration::CreateForWorker(mWorkerPrivate, mScope); - } - return mRegistration; } diff --git a/dom/workers/WorkerScope.h b/dom/workers/WorkerScope.h index fbc4aacf30ab..5fca1bde8413 100644 --- a/dom/workers/WorkerScope.h +++ b/dom/workers/WorkerScope.h @@ -303,7 +303,8 @@ public: IMPL_EVENT_HANDLER(notificationclick) IMPL_EVENT_HANDLER(notificationclose) - ServiceWorkerGlobalScope(WorkerPrivate* aWorkerPrivate, const nsACString& aScope); + ServiceWorkerGlobalScope(WorkerPrivate* aWorkerPrivate, + const ServiceWorkerRegistrationDescriptor& aRegistrationDescriptor); virtual bool WrapGlobalObject(JSContext* aCx,