Bug 1438211 P4 Actually set the worker global on ServiceWorkerRegistration. r=asuth
authorBen Kelly <ben@wanderview.com>
Fri, 02 Mar 2018 13:02:50 -0800
changeset 407226 ee87211590b5
parent 407225 658d8694715a
child 407227 98cfdc9a7826
push id33596
push userncsoregi@mozilla.com
push dateFri, 09 Mar 2018 00:18:11 +0000
treeherdermozilla-central@31a33fc61956 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersasuth
bugs1438211
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1438211 P4 Actually set the worker global on ServiceWorkerRegistration. r=asuth
dom/serviceworkers/ServiceWorkerRegistration.cpp
dom/serviceworkers/ServiceWorkerRegistration.h
dom/workers/WorkerScope.cpp
--- a/dom/serviceworkers/ServiceWorkerRegistration.cpp
+++ b/dom/serviceworkers/ServiceWorkerRegistration.cpp
@@ -71,27 +71,28 @@ ServiceWorkerRegistration::CreateForMain
   RefPtr<ServiceWorkerRegistration> registration =
     new ServiceWorkerRegistration(aWindow->AsGlobal(), aDescriptor, inner);
 
   return registration.forget();
 }
 
 /* static */ already_AddRefed<ServiceWorkerRegistration>
 ServiceWorkerRegistration::CreateForWorker(WorkerPrivate* aWorkerPrivate,
+                                           nsIGlobalObject* aGlobal,
                                            const ServiceWorkerRegistrationDescriptor& aDescriptor)
 {
-  MOZ_ASSERT(aWorkerPrivate);
+  MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
+  MOZ_DIAGNOSTIC_ASSERT(aGlobal);
   aWorkerPrivate->AssertIsOnWorkerThread();
 
   RefPtr<Inner> inner =
     new ServiceWorkerRegistrationWorkerThread(aWorkerPrivate, aDescriptor);
 
   RefPtr<ServiceWorkerRegistration> registration =
-    new ServiceWorkerRegistration(aWorkerPrivate->GlobalScope(), aDescriptor,
-                                  inner);
+    new ServiceWorkerRegistration(aGlobal, aDescriptor, inner);
 
   return registration.forget();
 }
 
 void
 ServiceWorkerRegistration::DisconnectFromOwner()
 {
   mInner->ClearServiceWorkerRegistration(this);
@@ -123,17 +124,22 @@ ServiceWorkerRegistration::GetActive() c
 void
 ServiceWorkerRegistration::UpdateState(const ServiceWorkerRegistrationDescriptor& aDescriptor)
 {
   MOZ_DIAGNOSTIC_ASSERT(MatchesDescriptor(aDescriptor));
 
   mDescriptor = aDescriptor;
 
   nsCOMPtr<nsIGlobalObject> global = GetParentObject();
-  if (!global) {
+
+  // Clear all workers if the registration has been detached from the global.
+  // Also, we cannot expose ServiceWorker objects on worker threads yet, so
+  // do the same on when off-main-thread.  This main thread check should be
+  // removed as part of bug 1113522.
+  if (!global || !NS_IsMainThread()) {
     mInstallingWorker = nullptr;
     mWaitingWorker = nullptr;
     mActiveWorker = nullptr;
     return;
   }
 
   Maybe<ServiceWorkerDescriptor> active = aDescriptor.GetActive();
   if (active.isSome()) {
--- a/dom/serviceworkers/ServiceWorkerRegistration.h
+++ b/dom/serviceworkers/ServiceWorkerRegistration.h
@@ -70,16 +70,17 @@ public:
   IMPL_EVENT_HANDLER(updatefound)
 
   static already_AddRefed<ServiceWorkerRegistration>
   CreateForMainThread(nsPIDOMWindowInner* aWindow,
                       const ServiceWorkerRegistrationDescriptor& aDescriptor);
 
   static already_AddRefed<ServiceWorkerRegistration>
   CreateForWorker(WorkerPrivate* aWorkerPrivate,
+                  nsIGlobalObject* aGlobal,
                   const ServiceWorkerRegistrationDescriptor& aDescriptor);
 
   JSObject*
   WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
 
   void DisconnectFromOwner() override;
 
   already_AddRefed<ServiceWorker>
--- a/dom/workers/WorkerScope.cpp
+++ b/dom/workers/WorkerScope.cpp
@@ -570,17 +570,18 @@ WorkerGlobalScope::GetOrCreateServiceWor
       return;
     }
 
     ref = swr.forget();
     *aDoneOut = true;
   });
 
   if (!ref) {
-    ref = ServiceWorkerRegistration::CreateForWorker(mWorkerPrivate, aDescriptor);
+    ref = ServiceWorkerRegistration::CreateForWorker(mWorkerPrivate, this,
+                                                     aDescriptor);
   }
 
   return ref.forget();
 }
 
 DedicatedWorkerGlobalScope::DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate,
                                                        const nsString& aName)
   : WorkerGlobalScope(aWorkerPrivate)
@@ -680,16 +681,17 @@ ServiceWorkerGlobalScope::ServiceWorkerG
                                                    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,
+                                                             this,
                                                              aRegistrationDescriptor))
 {
 }
 
 ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope()
 {
 }