Bug 1296531 - Break out ShutdownTicket and GetShutdownBarrier from MSG to MediaUtils. r?jib draft
authorAndreas Pehrson <pehrsons@gmail.com>
Tue, 16 May 2017 12:39:02 +0200
changeset 670311 1e9ef14347b4f07f3d64cf75b93c376ba1646bb9
parent 670310 c5ef2cd5cb6e60f72bb459d3da6ee9d824d0dc4b
child 670312 a0895616749089280bfbc63f843be949935e86b6
push id81598
push userbmo:apehrson@mozilla.com
push dateTue, 26 Sep 2017 09:13:19 +0000
reviewersjib
bugs1296531
milestone58.0a1
Bug 1296531 - Break out ShutdownTicket and GetShutdownBarrier from MSG to MediaUtils. r?jib MozReview-Commit-ID: HVCfuJz1zoA
dom/media/MediaStreamGraph.cpp
dom/media/MediaStreamGraphImpl.h
dom/media/systemservices/MediaUtils.cpp
dom/media/systemservices/MediaUtils.h
--- a/dom/media/MediaStreamGraph.cpp
+++ b/dom/media/MediaStreamGraph.cpp
@@ -1483,17 +1483,17 @@ MediaStreamGraphImpl::ApplyStreamUpdate(
   stream->mMainThreadFinished = aUpdate->mNextMainThreadFinished;
 
   if (stream->ShouldNotifyStreamFinished()) {
     stream->NotifyMainThreadListeners();
   }
 }
 
 void
-MediaStreamGraphImpl::ForceShutDown(ShutdownTicket* aShutdownTicket)
+MediaStreamGraphImpl::ForceShutDown(media::ShutdownTicket* aShutdownTicket)
 {
   NS_ASSERTION(NS_IsMainThread(), "Must be called on main thread");
   LOG(LogLevel::Debug, ("MediaStreamGraph %p ForceShutdown", this));
 
   MonitorAutoLock lock(mMonitor);
   if (aShutdownTicket) {
     MOZ_ASSERT(!mForceShutdownTicket);
     // Avoid waiting forever for a graph to shut down
@@ -3556,29 +3556,29 @@ MediaStreamGraph::GetInstance(MediaStrea
             "MediaStreamGraph shutdown: blocking on msg thread"))
         {}
 
         NS_IMETHOD
         BlockShutdown(nsIAsyncShutdownClient* aProfileBeforeChange) override
         {
           // Distribute the global async shutdown blocker in a ticket. If there
           // are zero graphs then shutdown is unblocked when we go out of scope.
-          RefPtr<MediaStreamGraphImpl::ShutdownTicket> ticket =
-              new MediaStreamGraphImpl::ShutdownTicket(gMediaStreamGraphShutdownBlocker.get());
+          auto ticket = MakeRefPtr<media::ShutdownTicket>(
+              gMediaStreamGraphShutdownBlocker.get());
           gMediaStreamGraphShutdownBlocker = nullptr;
 
           for (auto iter = gGraphs.Iter(); !iter.Done(); iter.Next()) {
             iter.UserData()->ForceShutDown(ticket);
           }
           return NS_OK;
         }
       };
 
       gMediaStreamGraphShutdownBlocker = new Blocker();
-      nsCOMPtr<nsIAsyncShutdownClient> barrier = MediaStreamGraphImpl::GetShutdownBarrier();
+      nsCOMPtr<nsIAsyncShutdownClient> barrier = media::GetShutdownBarrier();
       nsresult rv = barrier->
           AddBlocker(gMediaStreamGraphShutdownBlocker,
                      NS_LITERAL_STRING(__FILE__), __LINE__,
                      NS_LITERAL_STRING("MediaStreamGraph shutdown"));
       MOZ_RELEASE_ASSERT(NS_SUCCEEDED(rv));
     }
 
     AbstractThread* mainThread;
--- a/dom/media/MediaStreamGraphImpl.h
+++ b/dom/media/MediaStreamGraphImpl.h
@@ -12,25 +12,28 @@
 #include "GraphDriver.h"
 #include "Latency.h"
 #include "mozilla/Monitor.h"
 #include "mozilla/Services.h"
 #include "mozilla/TimeStamp.h"
 #include "mozilla/UniquePtr.h"
 #include "mozilla/WeakPtr.h"
 #include "nsDataHashtable.h"
-#include "nsIASyncShutdown.h"
 #include "nsIMemoryReporter.h"
 #include "nsINamed.h"
 #include "nsIRunnable.h"
 #include "nsIThread.h"
 #include "nsITimer.h"
 
 namespace mozilla {
 
+namespace media {
+class ShutdownTicket;
+}
+
 template <typename T>
 class LinkedList;
 #ifdef MOZ_WEBRTC
 class AudioOutputObserver;
 #endif
 
 /**
  * A per-stream update message passed from the media graph thread to the
@@ -152,58 +155,23 @@ public:
   void AppendMessage(UniquePtr<ControlMessage> aMessage);
 
   /**
    * Dispatches a runnable from any thread to the correct main thread for this
    * MediaStreamGraph.
    */
   void Dispatch(already_AddRefed<nsIRunnable>&& aRunnable);
 
-  // Shutdown helpers.
-
-  static already_AddRefed<nsIAsyncShutdownClient>
-  GetShutdownBarrier()
-  {
-    nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdown();
-    MOZ_RELEASE_ASSERT(svc);
-
-    nsCOMPtr<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();
-  }
-
-  class ShutdownTicket final
-  {
-  public:
-    explicit ShutdownTicket(nsIAsyncShutdownBlocker* aBlocker) : mBlocker(aBlocker) {}
-    NS_INLINE_DECL_REFCOUNTING(ShutdownTicket)
-  private:
-    ~ShutdownTicket()
-    {
-      nsCOMPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();
-      barrier->RemoveBlocker(mBlocker);
-    }
-
-    nsCOMPtr<nsIAsyncShutdownBlocker> mBlocker;
-  };
-
   /**
    * Make this MediaStreamGraph enter forced-shutdown state. This state
    * will be noticed by the media graph thread, which will shut down all streams
    * and other state controlled by the media graph thread.
    * This is called during application shutdown.
    */
-  void ForceShutDown(ShutdownTicket* aShutdownTicket);
+  void ForceShutDown(media::ShutdownTicket* aShutdownTicket);
 
   /**
    * Called before the thread runs.
    */
   void Init();
 
   /**
    * Respond to CollectReports with sizes collected on the graph thread.
@@ -762,17 +730,17 @@ public:
   /**
    * True when we need to do a forced shutdown during application shutdown.
    */
   bool mForceShutDown;
 
   /**
    * Drop this reference during shutdown to unblock shutdown.
    **/
-  RefPtr<ShutdownTicket> mForceShutdownTicket;
+  RefPtr<media::ShutdownTicket> mForceShutdownTicket;
 
   /**
    * True when we have posted an event to the main thread to run
    * RunInStableState() and the event hasn't run yet.
    */
   bool mPostedRunInStableStateEvent;
 
   // Main thread only
--- a/dom/media/systemservices/MediaUtils.cpp
+++ b/dom/media/systemservices/MediaUtils.cpp
@@ -4,12 +4,30 @@
  * 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()
+{
+  nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdown();
+  MOZ_RELEASE_ASSERT(svc);
+
+  nsCOMPtr<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();
+}
+
 NS_IMPL_ISUPPORTS(ShutdownBlocker, nsIAsyncShutdownBlocker)
 
 } // namespace media
 } // namespace mozilla
--- a/dom/media/systemservices/MediaUtils.h
+++ b/dom/media/systemservices/MediaUtils.h
@@ -2,19 +2,22 @@
 /* vim: set sw=2 ts=8 et ft=cpp : */
 /* 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_MediaUtils_h
 #define mozilla_MediaUtils_h
 
-#include "nsThreadUtils.h"
+#include "mozilla/Assertions.h"
+#include "mozilla/UniquePtr.h"
+#include "nsCOMPtr.h"
 #include "nsIAsyncShutdown.h"
-#include "mozilla/UniquePtr.h"
+#include "nsISupportsImpl.h"
+#include "nsThreadUtils.h"
 
 namespace mozilla {
 namespace media {
 
 /*
  * media::Pledge - A promise-like pattern for c++ that takes lambda functions.
  *
  * Asynchronous APIs that proxy to another thread or to the chrome process and
@@ -355,19 +358,22 @@ class Refcountable<UniquePtr<T>> : publi
 {
 public:
   explicit Refcountable<UniquePtr<T>>(T* aPtr) : UniquePtr<T>(aPtr) {}
   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Refcountable<T>)
 private:
   ~Refcountable<UniquePtr<T>>() {}
 };
 
-/* media::ShutdownBlocker - Async shutdown helper.
+/* Async shutdown helpers
  */
 
+already_AddRefed<nsIAsyncShutdownClient>
+GetShutdownBarrier();
+
 class ShutdownBlocker : public nsIAsyncShutdownBlocker
 {
 public:
   ShutdownBlocker(const nsString& aName) : mName(aName) {}
 
   NS_IMETHOD
   BlockShutdown(nsIAsyncShutdownClient* aProfileBeforeChange) override = 0;
 
@@ -384,12 +390,27 @@ public:
 
   NS_DECL_ISUPPORTS
 protected:
   virtual ~ShutdownBlocker() {}
 private:
   const nsString mName;
 };
 
+class ShutdownTicket final
+{
+public:
+  explicit ShutdownTicket(nsIAsyncShutdownBlocker* aBlocker) : mBlocker(aBlocker) {}
+  NS_INLINE_DECL_REFCOUNTING(ShutdownTicket)
+private:
+  ~ShutdownTicket()
+  {
+    nsCOMPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();
+    barrier->RemoveBlocker(mBlocker);
+  }
+
+  nsCOMPtr<nsIAsyncShutdownBlocker> mBlocker;
+};
+
 } // namespace media
 } // namespace mozilla
 
 #endif // mozilla_MediaUtils_h