Bug 1559919 - Finish the WorkerHolder cleanup - part 5 - IPCWorkerRef in ServiceWorkerRegistrationChild, r?asuth draft
authorpulselistener
Tue, 18 Jun 2019 14:21:44 +0000
changeset 2102901 493b5208fbb68ec84dcb9b33301755945c5301eb
parent 2102900 36cd3813189eaffb7893871aa53e4759aaa8afe5
child 2102902 55dc898adae936302bde19553f55f1243fccb225
push id378950
push userreviewbot
push dateTue, 18 Jun 2019 14:22:47 +0000
treeherdertry@526aa335c8be [default view] [failures only]
reviewersasuth
bugs1559919
milestone69.0a1
Bug 1559919 - Finish the WorkerHolder cleanup - part 5 - IPCWorkerRef in ServiceWorkerRegistrationChild, r?asuth Differential Diff: PHID-DIFF-p2rcl5wfw5br6vi2dfhw
dom/serviceworkers/RemoteServiceWorkerRegistrationImpl.cpp
dom/serviceworkers/ServiceWorkerRegistrationChild.cpp
dom/serviceworkers/ServiceWorkerRegistrationChild.h
--- a/dom/serviceworkers/RemoteServiceWorkerRegistrationImpl.cpp
+++ b/dom/serviceworkers/RemoteServiceWorkerRegistrationImpl.cpp
@@ -109,32 +109,23 @@ RemoteServiceWorkerRegistrationImpl::Rem
     : mActor(nullptr), mOuter(nullptr), mShutdown(false) {
   PBackgroundChild* parentActor =
       BackgroundChild::GetOrCreateForCurrentThread();
   if (NS_WARN_IF(!parentActor)) {
     Shutdown();
     return;
   }
 
-  RefPtr<WorkerHolderToken> workerHolderToken;
-  if (!NS_IsMainThread()) {
-    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
-    MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
-
-    workerHolderToken = WorkerHolderToken::Create(
-        workerPrivate, Canceling, WorkerHolderToken::AllowIdleShutdownStart);
-
-    if (NS_WARN_IF(!workerHolderToken)) {
-      Shutdown();
-      return;
-    }
+  ServiceWorkerRegistrationChild* actor =
+      ServiceWorkerRegistrationChild::Create();
+  if (NS_WARN_IF(!actor)) {
+    Shutdown();
+    return;
   }
 
-  ServiceWorkerRegistrationChild* actor =
-      new ServiceWorkerRegistrationChild(workerHolderToken);
   PServiceWorkerRegistrationChild* sentActor =
       parentActor->SendPServiceWorkerRegistrationConstructor(
           actor, aDescriptor.ToIPC());
   if (NS_WARN_IF(!sentActor)) {
     Shutdown();
     return;
   }
   MOZ_DIAGNOSTIC_ASSERT(sentActor == actor);
--- a/dom/serviceworkers/ServiceWorkerRegistrationChild.cpp
+++ b/dom/serviceworkers/ServiceWorkerRegistrationChild.cpp
@@ -9,20 +9,17 @@
 #include "RemoteServiceWorkerRegistrationImpl.h"
 
 namespace mozilla {
 namespace dom {
 
 using mozilla::ipc::IPCResult;
 
 void ServiceWorkerRegistrationChild::ActorDestroy(ActorDestroyReason aReason) {
-  if (mWorkerHolderToken) {
-    mWorkerHolderToken->RemoveListener(this);
-    mWorkerHolderToken = nullptr;
-  }
+  mIPCWorkerRef = nullptr;
 
   if (mOwner) {
     mOwner->RevokeActor(this);
     MOZ_DIAGNOSTIC_ASSERT(!mOwner);
   }
 }
 
 IPCResult ServiceWorkerRegistrationChild::RecvUpdateState(
@@ -35,29 +32,39 @@ IPCResult ServiceWorkerRegistrationChild
 
 IPCResult ServiceWorkerRegistrationChild::RecvFireUpdateFound() {
   if (mOwner) {
     mOwner->FireUpdateFound();
   }
   return IPC_OK();
 }
 
-void ServiceWorkerRegistrationChild::WorkerShuttingDown() {
-  MaybeStartTeardown();
+// static
+ServiceWorkerRegistrationChild* ServiceWorkerRegistrationChild::Create() {
+  ServiceWorkerRegistrationChild* actor = new ServiceWorkerRegistrationChild();
+
+  if (!NS_IsMainThread()) {
+    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
+    MOZ_DIAGNOSTIC_ASSERT(workerPrivate);
+
+    actor->mIPCWorkerRef =
+        IPCWorkerRef::Create(workerPrivate, "ServiceWorkerRegistrationChild",
+                             [&] { actor->MaybeStartTeardown(); });
+
+    if (NS_WARN_IF(!actor->mIPCWorkerRef)) {
+      delete actor;
+      return nullptr;
+    }
+  }
+
+  return actor;
 }
 
-ServiceWorkerRegistrationChild::ServiceWorkerRegistrationChild(
-    WorkerHolderToken* aWorkerHolderToken)
-    : mWorkerHolderToken(aWorkerHolderToken),
-      mOwner(nullptr),
-      mTeardownStarted(false) {
-  if (mWorkerHolderToken) {
-    mWorkerHolderToken->AddListener(this);
-  }
-}
+ServiceWorkerRegistrationChild::ServiceWorkerRegistrationChild()
+    : mOwner(nullptr), mTeardownStarted(false) {}
 
 void ServiceWorkerRegistrationChild::SetOwner(
     RemoteServiceWorkerRegistrationImpl* aOwner) {
   MOZ_DIAGNOSTIC_ASSERT(!mOwner);
   MOZ_DIAGNOSTIC_ASSERT(aOwner);
   mOwner = aOwner;
 }
 
--- a/dom/serviceworkers/ServiceWorkerRegistrationChild.h
+++ b/dom/serviceworkers/ServiceWorkerRegistrationChild.h
@@ -3,44 +3,42 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_serviceworkerregistrationchild_h__
 #define mozilla_dom_serviceworkerregistrationchild_h__
 
 #include "mozilla/dom/PServiceWorkerRegistrationChild.h"
-#include "mozilla/dom/WorkerHolderToken.h"
 
 namespace mozilla {
 namespace dom {
 
+class IPCWorkerRef;
 class RemoteServiceWorkerRegistrationImpl;
 
 class ServiceWorkerRegistrationChild final
-    : public PServiceWorkerRegistrationChild,
-      public WorkerHolderToken::Listener {
-  RefPtr<WorkerHolderToken> mWorkerHolderToken;
+    : public PServiceWorkerRegistrationChild {
+  RefPtr<IPCWorkerRef> mIPCWorkerRef;
   RemoteServiceWorkerRegistrationImpl* mOwner;
   bool mTeardownStarted;
 
+  ServiceWorkerRegistrationChild();
+
   // PServiceWorkerRegistrationChild
   void ActorDestroy(ActorDestroyReason aReason) override;
 
   mozilla::ipc::IPCResult RecvUpdateState(
       const IPCServiceWorkerRegistrationDescriptor& aDescriptor) override;
 
   mozilla::ipc::IPCResult RecvFireUpdateFound() override;
 
-  // WorkerHolderToken::Listener
-  void WorkerShuttingDown() override;
+ public:
+  static ServiceWorkerRegistrationChild* Create();
 
- public:
-  explicit ServiceWorkerRegistrationChild(
-      WorkerHolderToken* aWorkerHolderToken);
   ~ServiceWorkerRegistrationChild() = default;
 
   void SetOwner(RemoteServiceWorkerRegistrationImpl* aOwner);
 
   void RevokeOwner(RemoteServiceWorkerRegistrationImpl* aOwner);
 
   void MaybeStartTeardown();
 };