Bug 1329989 - Simplify the ServiceWorkerRegistrationWorkerThread runnables. r=baku a=abillings FIREFOX_51_0b14_BUILD1 FIREFOX_51_0b14_RELEASE
authorBen Kelly <ben@wanderview.com>
Thu, 12 Jan 2017 16:46:24 -0800
changeset 359375 09142d07fd735e375fc1ae46886a52d6aef43b60
parent 359374 1fd589b5962b86e4df07fcffa98d44b75e4561a7
child 359376 200135ebbfaf69596efa3f645bf40430af02a72b
push id1324
push usermtabara@mozilla.com
push dateMon, 16 Jan 2017 13:07:44 +0000
treeherdermozilla-release@a01c49833940 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku, abillings
bugs1329989
milestone51.0
Bug 1329989 - Simplify the ServiceWorkerRegistrationWorkerThread runnables. r=baku a=abillings MozReview-Commit-ID: MPxbX3vNpT
dom/workers/ServiceWorkerRegistration.cpp
--- a/dom/workers/ServiceWorkerRegistration.cpp
+++ b/dom/workers/ServiceWorkerRegistration.cpp
@@ -894,29 +894,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
@@ -1040,30 +1034,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;
@@ -1142,129 +1136,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(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,