Bug 1329989 - Simplify the ServiceWorkerRegistrationWorkerThread runnables. r=baku
authorBen Kelly <ben@wanderview.com>
Thu, 12 Jan 2017 16:46:24 -0800
changeset 374224 17a962c65f48643180d586790f1340093d658b32
parent 374223 0f37bb9f5af0e2e67fe348c814aea5f2884f4765
child 374225 27d973fbf0268e0a00f07c7b6f17d022022b4d0b
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1329989
milestone53.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 1329989 - Simplify the ServiceWorkerRegistrationWorkerThread runnables. r=baku MozReview-Commit-ID: MPxbX3vNpT
dom/workers/ServiceWorkerRegistration.cpp
--- a/dom/workers/ServiceWorkerRegistration.cpp
+++ b/dom/workers/ServiceWorkerRegistration.cpp
@@ -918,29 +918,23 @@ public:
 
   bool
   Notify(Status aStatus) override;
 
   already_AddRefed<PushManager>
   GetPushManager(JSContext* aCx, ErrorResult& aRv) override;
 
 private:
-  enum Reason
-  {
-    RegistrationIsGoingAway = 0,
-    WorkerIsGoingAway,
-  };
-
   ~ServiceWorkerRegistrationWorkerThread();
 
   void
   InitListener();
 
   void
-  ReleaseListener(Reason aReason);
+  ReleaseListener();
 
   WorkerPrivate* mWorkerPrivate;
   RefPtr<WorkerListener> mListener;
 
   RefPtr<PushManager> mPushManager;
 };
 
 class WorkerListener final : public ServiceWorkerRegistrationListener
@@ -1071,30 +1065,30 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_
                                                   ServiceWorkerRegistration)
   NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPushManager)
 
 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ServiceWorkerRegistrationWorkerThread,
                                                 ServiceWorkerRegistration)
   NS_IMPL_CYCLE_COLLECTION_UNLINK(mPushManager)
-  tmp->ReleaseListener(RegistrationIsGoingAway);
+  tmp->ReleaseListener();
 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
 
 ServiceWorkerRegistrationWorkerThread::ServiceWorkerRegistrationWorkerThread(WorkerPrivate* aWorkerPrivate,
                                                                              const nsAString& aScope)
   : ServiceWorkerRegistration(nullptr, aScope)
   , mWorkerPrivate(aWorkerPrivate)
 {
   InitListener();
 }
 
 ServiceWorkerRegistrationWorkerThread::~ServiceWorkerRegistrationWorkerThread()
 {
-  ReleaseListener(RegistrationIsGoingAway);
+  ReleaseListener();
   MOZ_ASSERT(!mListener);
 }
 
 already_AddRefed<workers::ServiceWorker>
 ServiceWorkerRegistrationWorkerThread::GetInstalling()
 {
   // FIXME(nsm): Will be implemented after Bug 1113522.
   return nullptr;
@@ -1173,129 +1167,65 @@ ServiceWorkerRegistrationWorkerThread::U
   }
 
   RefPtr<StartUnregisterRunnable> r = new StartUnregisterRunnable(proxy, mScope);
   MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(r.forget()));
 
   return promise.forget();
 }
 
-class StartListeningRunnable final : public Runnable
-{
-  RefPtr<WorkerListener> mListener;
-public:
-  explicit StartListeningRunnable(WorkerListener* aListener)
-    : mListener(aListener)
-  {}
-
-  NS_IMETHOD
-  Run() override
-  {
-    mListener->StartListeningForEvents();
-    return NS_OK;
-  }
-};
-
 void
 ServiceWorkerRegistrationWorkerThread::InitListener()
 {
   MOZ_ASSERT(!mListener);
   WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
   MOZ_ASSERT(worker);
   worker->AssertIsOnWorkerThread();
 
   mListener = new WorkerListener(worker, this);
   if (!HoldWorker(worker, Closing)) {
     mListener = nullptr;
     NS_WARNING("Could not add feature");
     return;
   }
 
-  RefPtr<StartListeningRunnable> r =
-    new StartListeningRunnable(mListener);
+  nsCOMPtr<nsIRunnable> r =
+    NewRunnableMethod(mListener, &WorkerListener::StartListeningForEvents);
   MOZ_ALWAYS_SUCCEEDS(worker->DispatchToMainThread(r.forget()));
 }
 
-class AsyncStopListeningRunnable final : public Runnable
-{
-  RefPtr<WorkerListener> mListener;
-public:
-  explicit AsyncStopListeningRunnable(WorkerListener* aListener)
-    : mListener(aListener)
-  {}
-
-  NS_IMETHOD
-  Run() override
-  {
-    mListener->StopListeningForEvents();
-    return NS_OK;
-  }
-};
-
-class SyncStopListeningRunnable final : public WorkerMainThreadRunnable
-{
-  RefPtr<WorkerListener> mListener;
-public:
-  SyncStopListeningRunnable(WorkerPrivate* aWorkerPrivate,
-                            WorkerListener* aListener)
-    : WorkerMainThreadRunnable(aWorkerPrivate,
-                               NS_LITERAL_CSTRING("ServiceWorkerRegistration :: StopListening"))
-    , mListener(aListener)
-  {}
-
-  bool
-  MainThreadRun() override
-  {
-    mListener->StopListeningForEvents();
-    return true;
-  }
-};
-
 void
-ServiceWorkerRegistrationWorkerThread::ReleaseListener(Reason aReason)
+ServiceWorkerRegistrationWorkerThread::ReleaseListener()
 {
   if (!mListener) {
     return;
   }
 
   // We can assert worker here, because:
   // 1) We always HoldWorker, so if the worker has shutdown already, we'll
   //    have received Notify and removed it. If HoldWorker had failed,
   //    mListener will be null and we won't reach here.
   // 2) Otherwise, worker is still around even if we are going away.
   mWorkerPrivate->AssertIsOnWorkerThread();
   ReleaseWorker();
 
   mListener->ClearRegistration();
 
-  if (aReason == RegistrationIsGoingAway) {
-    RefPtr<AsyncStopListeningRunnable> r =
-      new AsyncStopListeningRunnable(mListener);
-    MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(r.forget()));
-  } else if (aReason == WorkerIsGoingAway) {
-    RefPtr<SyncStopListeningRunnable> r =
-      new SyncStopListeningRunnable(mWorkerPrivate, mListener);
-    ErrorResult rv;
-    r->Dispatch(Killing, rv);
-    if (rv.Failed()) {
-      NS_ERROR("Failed to dispatch stop listening runnable!");
-      // And now what?
-      rv.SuppressException();
-    }
-  } else {
-    MOZ_CRASH("Bad reason");
-  }
+  nsCOMPtr<nsIRunnable> r =
+    NewRunnableMethod(mListener, &WorkerListener::StopListeningForEvents);
+  MOZ_ALWAYS_SUCCEEDS(mWorkerPrivate->DispatchToMainThread(r.forget()));
+
   mListener = nullptr;
   mWorkerPrivate = nullptr;
 }
 
 bool
 ServiceWorkerRegistrationWorkerThread::Notify(Status aStatus)
 {
-  ReleaseListener(WorkerIsGoingAway);
+  ReleaseListener();
   return true;
 }
 
 class FireUpdateFoundRunnable final : public WorkerRunnable
 {
   RefPtr<WorkerListener> mListener;
 public:
   FireUpdateFoundRunnable(WorkerPrivate* aWorkerPrivate,