Bug 1407415 - Consolidate media::GetShutdownBarrier() use and return a RefPtr r=dminor
authorJan-Ivar Bruaroey <jib@mozilla.com>
Fri, 03 May 2019 21:08:42 +0000
changeset 531421 5dbc2ba75388af17e059b91bf235826ef94d6485
parent 531420 5edef21b353c2e30553f8302affa971ae10d2542
child 531422 bd790e80a2f794b60bd9e300faf0c9eeea821054
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdminor
bugs1407415
milestone68.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 1407415 - Consolidate media::GetShutdownBarrier() use and return a RefPtr r=dminor Differential Revision: https://phabricator.services.mozilla.com/D29700
dom/media/MediaManager.cpp
dom/media/MediaStreamGraph.cpp
dom/media/systemservices/MediaUtils.cpp
dom/media/systemservices/MediaUtils.h
--- a/dom/media/MediaManager.cpp
+++ b/dom/media/MediaManager.cpp
@@ -163,34 +163,16 @@ class nsMainThreadPtrHolder<
 #endif
 
   // Copy constructor and operator= not implemented. Once constructed, the
   // holder is immutable.
   Holder& operator=(const nsMainThreadPtrHolder& aOther) = delete;
   nsMainThreadPtrHolder(const nsMainThreadPtrHolder& aOther) = delete;
 };
 
-namespace {
-already_AddRefed<nsIAsyncShutdownClient> GetShutdownPhase() {
-  nsCOMPtr<nsIAsyncShutdownService> svc = mozilla::services::GetAsyncShutdown();
-  MOZ_RELEASE_ASSERT(svc);
-
-  nsCOMPtr<nsIAsyncShutdownClient> shutdownPhase;
-  nsresult rv = svc->GetProfileBeforeChange(getter_AddRefs(shutdownPhase));
-  if (!shutdownPhase) {
-    // We are probably in a content process. We need to do cleanup at
-    // XPCOM shutdown in leakchecking builds.
-    rv = svc->GetXpcomWillShutdown(getter_AddRefs(shutdownPhase));
-  }
-  MOZ_RELEASE_ASSERT(shutdownPhase);
-  MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
-  return shutdownPhase.forget();
-}
-}  // namespace
-
 namespace mozilla {
 
 #ifdef LOG
 #  undef LOG
 #endif
 
 LazyLogModule gMediaManagerLog("MediaManager");
 #define LOG(...) MOZ_LOG(gMediaManagerLog, LogLevel::Debug, (__VA_ARGS__))
@@ -2020,34 +2002,32 @@ MediaManager* MediaManager::Get() {
       prefs->AddObserver("media.ondevicechange.fakeDeviceChangeEvent.enabled",
                          sSingleton, false);
       prefs->AddObserver("media.getusermedia.channels", sSingleton, false);
 #endif
     }
 
     // Prepare async shutdown
 
-    nsCOMPtr<nsIAsyncShutdownClient> shutdownPhase = GetShutdownPhase();
-
     class Blocker : public media::ShutdownBlocker {
      public:
       Blocker()
           : media::ShutdownBlocker(
                 NS_LITERAL_STRING("Media shutdown: blocking on media thread")) {
       }
 
       NS_IMETHOD BlockShutdown(nsIAsyncShutdownClient*) override {
         MOZ_RELEASE_ASSERT(MediaManager::GetIfExists());
         MediaManager::GetIfExists()->Shutdown();
         return NS_OK;
       }
     };
 
     sSingleton->mShutdownBlocker = new Blocker();
-    nsresult rv = shutdownPhase->AddBlocker(
+    nsresult rv = media::GetShutdownBarrier()->AddBlocker(
         sSingleton->mShutdownBlocker, NS_LITERAL_STRING(__FILE__), __LINE__,
         NS_LITERAL_STRING("Media shutdown"));
     MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
   }
   return sSingleton;
 }
 
 /* static */
@@ -3646,18 +3626,18 @@ void MediaManager::Shutdown() {
   auto shutdown = MakeRefPtr<ShutdownTask>(
       this, media::NewRunnableFrom([this, self = RefPtr<MediaManager>(this)]() {
         LOG("MediaManager shutdown lambda running, releasing MediaManager "
             "singleton and thread");
         if (mMediaThread) {
           mMediaThread->Stop();
         }
         // Remove async shutdown blocker
-        nsCOMPtr<nsIAsyncShutdownClient> shutdownPhase = GetShutdownPhase();
-        shutdownPhase->RemoveBlocker(sSingleton->mShutdownBlocker);
+        media::GetShutdownBarrier()->RemoveBlocker(
+            sSingleton->mShutdownBlocker);
 
         // we hold a ref to 'self' which is the same as sSingleton
         sSingleton = nullptr;
         return NS_OK;
       }));
   mMediaThread->message_loop()->PostTask(shutdown.forget());
 }
 
--- a/dom/media/MediaStreamGraph.cpp
+++ b/dom/media/MediaStreamGraph.cpp
@@ -3288,18 +3288,17 @@ MediaStreamGraph* MediaStreamGraph::GetI
           for (auto iter = gGraphs.Iter(); !iter.Done(); iter.Next()) {
             iter.UserData()->ForceShutDown(ticket);
           }
           return NS_OK;
         }
       };
 
       gMediaStreamGraphShutdownBlocker = new Blocker();
-      nsCOMPtr<nsIAsyncShutdownClient> barrier = media::GetShutdownBarrier();
-      nsresult rv = barrier->AddBlocker(
+      nsresult rv = media::GetShutdownBarrier()->AddBlocker(
           gMediaStreamGraphShutdownBlocker, NS_LITERAL_STRING(__FILE__),
           __LINE__, NS_LITERAL_STRING("MediaStreamGraph shutdown"));
       MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
     }
 
     AbstractThread* mainThread;
     if (aWindow) {
       mainThread =
--- a/dom/media/systemservices/MediaUtils.cpp
+++ b/dom/media/systemservices/MediaUtils.cpp
@@ -4,28 +4,28 @@
  * 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/. */
 
 #include "MediaUtils.h"
 
 namespace mozilla {
 namespace media {
 
-already_AddRefed<nsIAsyncShutdownClient> GetShutdownBarrier() {
+RefPtr<nsIAsyncShutdownClient> GetShutdownBarrier() {
   nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdown();
   MOZ_RELEASE_ASSERT(svc);
 
-  nsCOMPtr<nsIAsyncShutdownClient> barrier;
+  RefPtr<nsIAsyncShutdownClient> barrier;
   nsresult rv = svc->GetProfileBeforeChange(getter_AddRefs(barrier));
   if (!barrier) {
     // We are probably in a content process. We need to do cleanup at
     // XPCOM shutdown in leakchecking builds.
     rv = svc->GetXpcomWillShutdown(getter_AddRefs(barrier));
   }
   MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
   MOZ_RELEASE_ASSERT(barrier);
-  return barrier.forget();
+  return barrier;
 }
 
 NS_IMPL_ISUPPORTS(ShutdownBlocker, nsIAsyncShutdownBlocker)
 
 }  // namespace media
 }  // namespace mozilla
--- a/dom/media/systemservices/MediaUtils.h
+++ b/dom/media/systemservices/MediaUtils.h
@@ -127,17 +127,17 @@ class Refcountable<UniquePtr<T>> : publi
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Refcountable<T>)
  private:
   ~Refcountable<UniquePtr<T>>() {}
 };
 
 /* Async shutdown helpers
  */
 
-already_AddRefed<nsIAsyncShutdownClient> GetShutdownBarrier();
+RefPtr<nsIAsyncShutdownClient> GetShutdownBarrier();
 
 class ShutdownBlocker : public nsIAsyncShutdownBlocker {
  public:
   ShutdownBlocker(const nsString& aName) : mName(aName) {}
 
   NS_IMETHOD
   BlockShutdown(nsIAsyncShutdownClient* aProfileBeforeChange) override = 0;
 
@@ -157,20 +157,17 @@ class ShutdownBlocker : public nsIAsyncS
 };
 
 class ShutdownTicket final {
  public:
   explicit ShutdownTicket(nsIAsyncShutdownBlocker* aBlocker)
       : mBlocker(aBlocker) {}
   NS_INLINE_DECL_REFCOUNTING(ShutdownTicket)
  private:
-  ~ShutdownTicket() {
-    nsCOMPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();
-    barrier->RemoveBlocker(mBlocker);
-  }
+  ~ShutdownTicket() { GetShutdownBarrier()->RemoveBlocker(mBlocker); }
 
   nsCOMPtr<nsIAsyncShutdownBlocker> mBlocker;
 };
 
 /**
  * Await convenience methods to block until the promise has been resolved or
  * rejected. The Resolve/Reject functions, while called on a different thread,
  * would be running just as on the current thread thanks to the memory barrier