Bug 1678463 - Part 2: Migrate media _WITH_DESTROY refcounts to new macros, r=jya
authorNika Layzell <nika@thelayzells.com>
Mon, 14 Dec 2020 18:30:19 +0000
changeset 560652 86e06386072c4e59d1e7b473791627bdfbf41183
parent 560651 753585f3f88fb9d1fd798b76b3cef64d866f056a
child 560653 bf8f615202dd9b24f5158ab92398253af86b6c71
push id38032
push usercsabou@mozilla.com
push dateTue, 15 Dec 2020 09:29:54 +0000
treeherdermozilla-central@f805f27183c3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1678463
milestone85.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 1678463 - Part 2: Migrate media _WITH_DESTROY refcounts to new macros, r=jya These new macros provide a standard way to delegate `delete` to the main thread, and can be used to avoid using bespoke _WITH_DESTROY implementations. Differential Revision: https://phabricator.services.mozilla.com/D97826
dom/media/MediaResource.cpp
dom/media/MediaResource.h
dom/media/gmp/GMPCrashHelper.cpp
dom/media/gmp/GMPCrashHelper.h
dom/media/gmp/GMPServiceParent.cpp
dom/media/gmp/GMPServiceParent.h
dom/media/gmp/moz.build
--- a/dom/media/MediaResource.cpp
+++ b/dom/media/MediaResource.cpp
@@ -18,29 +18,16 @@ using mozilla::media::TimeUnit;
 mozilla::LazyLogModule gMediaResourceIndexLog("MediaResourceIndex");
 // Debug logging macro with object pointer and class name.
 #define ILOG(msg, ...)                                             \
   DDMOZ_LOG(gMediaResourceIndexLog, mozilla::LogLevel::Debug, msg, \
             ##__VA_ARGS__)
 
 namespace mozilla {
 
-void MediaResource::Destroy() {
-  // Ensures we only delete the MediaResource on the main thread.
-  if (NS_IsMainThread()) {
-    delete this;
-    return;
-  }
-  nsresult rv = SchedulerGroup::Dispatch(
-      TaskCategory::Other,
-      NewNonOwningRunnableMethod("MediaResource::Destroy", this,
-                                 &MediaResource::Destroy));
-  MOZ_ALWAYS_SUCCEEDS(rv);
-}
-
 static const uint32_t kMediaResourceIndexCacheSize = 8192;
 static_assert(IsPowerOfTwo(kMediaResourceIndexCacheSize),
               "kMediaResourceIndexCacheSize cache size must be a power of 2");
 
 MediaResourceIndex::MediaResourceIndex(MediaResource* aResource)
     : mResource(aResource),
       mOffset(0),
       mCacheBlockSize(
--- a/dom/media/MediaResource.h
+++ b/dom/media/MediaResource.h
@@ -49,17 +49,18 @@ DDLoggedTypeDeclName(MediaResource);
  */
 class MediaResource : public DecoderDoctorLifeLogger<MediaResource> {
  public:
   // Our refcounting is threadsafe, and when our refcount drops to zero
   // we dispatch an event to the main thread to delete the MediaResource.
   // Note that this means it's safe for references to this object to be
   // released on a non main thread, but the destructor will always run on
   // the main thread.
-  NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(MediaResource, Destroy());
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_MAIN_THREAD(
+      MediaResource)
 
   // Close the resource, stop any listeners, channels, etc.
   // Cancels any currently blocking Read request and forces that request to
   // return an error. This must be called (and resolve) before the MediaResource
   // is deleted.
   virtual RefPtr<GenericPromise> Close() {
     return GenericPromise::CreateAndResolve(true, __func__);
   }
@@ -110,19 +111,16 @@ class MediaResource : public DecoderDoct
    * Fills aRanges with MediaByteRanges representing the data which is cached
    * in the media cache. Stream should be pinned during call and while
    * aRanges is being used.
    */
   virtual nsresult GetCachedRanges(MediaByteRangeSet& aRanges) = 0;
 
  protected:
   virtual ~MediaResource() = default;
-
- private:
-  void Destroy();
 };
 
 /**
  * RAII class that handles pinning and unpinning for MediaResource and derived.
  * This should be used when making calculations that involve potentially-cached
  * MediaResource data, so that the state of the world can't change out from
  * under us.
  */
deleted file mode 100644
--- a/dom/media/gmp/GMPCrashHelper.cpp
+++ /dev/null
@@ -1,25 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/* 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/. */
-
-#include "GMPCrashHelper.h"
-#include "runnable_utils.h"
-#include "nsThreadUtils.h"
-#include "mozilla/SchedulerGroup.h"
-
-namespace mozilla {
-
-void GMPCrashHelper::Destroy() {
-  if (NS_IsMainThread()) {
-    delete this;
-  } else {
-    // Don't addref, as then we'd end up releasing after the delete runs!
-    SchedulerGroup::Dispatch(
-        TaskCategory::Other,
-        NewNonOwningRunnableMethod("GMPCrashHelper::Destroy", this,
-                                   &GMPCrashHelper::Destroy));
-  }
-}
-
-}  // namespace mozilla
--- a/dom/media/gmp/GMPCrashHelper.h
+++ b/dom/media/gmp/GMPCrashHelper.h
@@ -16,22 +16,22 @@ namespace mozilla {
 
 // For every GMP actor requested, the caller can specify a crash helper,
 // which is an object which supplies the nsPIDOMWindowInner to which we'll
 // dispatch the PluginCrashed event if the GMP crashes.
 // GMPCrashHelper has threadsafe refcounting. Its release method ensures
 // that instances are destroyed on the main thread.
 class GMPCrashHelper {
  public:
-  NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(GMPCrashHelper, Destroy());
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_MAIN_THREAD(
+      GMPCrashHelper);
 
   // Called on the main thread.
   virtual already_AddRefed<nsPIDOMWindowInner>
   GetPluginCrashedEventTarget() = 0;
 
  protected:
   virtual ~GMPCrashHelper() { MOZ_ASSERT(NS_IsMainThread()); }
-  void Destroy();
 };
 
 }  // namespace mozilla
 
 #endif  // GMPCrashHelper_h_
--- a/dom/media/gmp/GMPServiceParent.cpp
+++ b/dom/media/gmp/GMPServiceParent.cpp
@@ -1581,29 +1581,16 @@ GMPServiceParent::GMPServiceParent(Gecko
 }
 
 GMPServiceParent::~GMPServiceParent() {
   MOZ_ASSERT(NS_IsMainThread(), "Should be destroyted on the main thread");
   MOZ_ASSERT(mService);
   mService->ServiceUserDestroyed(this);
 }
 
-void GMPServiceParent::Destroy() {
-  // Ensures we only delete the GMPServiceParent on the main thread.
-  if (NS_IsMainThread()) {
-    delete this;
-    return;
-  }
-  nsresult rv = SchedulerGroup::Dispatch(
-      TaskCategory::Other,
-      NewNonOwningRunnableMethod("GMPServiceParent::Destroy", this,
-                                 &GMPServiceParent::Destroy));
-  MOZ_ALWAYS_SUCCEEDS(rv);
-}
-
 mozilla::ipc::IPCResult GMPServiceParent::RecvLaunchGMP(
     const nsCString& aNodeId, const nsCString& aAPI,
     nsTArray<nsCString>&& aTags, nsTArray<ProcessId>&& aAlreadyBridgedTo,
     uint32_t* aOutPluginId, ProcessId* aOutProcessId,
     nsCString* aOutDisplayName, Endpoint<PGMPContentParent>* aOutEndpoint,
     nsresult* aOutRv, nsCString* aOutErrorDescription) {
   if (mService->IsShuttingDown()) {
     *aOutRv = NS_ERROR_ILLEGAL_DURING_SHUTDOWN;
--- a/dom/media/gmp/GMPServiceParent.h
+++ b/dom/media/gmp/GMPServiceParent.h
@@ -217,18 +217,18 @@ class GMPServiceParent final : public PG
   // Our refcounting is thread safe, and when our refcount drops to zero
   // we dispatch an event to the main thread to delete the GMPServiceParent.
   // Note that this means it's safe for references to this object to be
   // released on a non main thread, but the destructor will always run on
   // the main thread.
 
   // Mark AddRef and Release as `final`, as they overload pure virtual
   // implementations in PGMPServiceParent.
-  NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DESTROY(GMPServiceParent,
-                                                     Destroy(), final);
+  NS_INLINE_DECL_THREADSAFE_REFCOUNTING_WITH_DELETE_ON_MAIN_THREAD(
+      GMPServiceParent, final);
 
   ipc::IPCResult RecvGetGMPNodeId(const nsString& aOrigin,
                                   const nsString& aTopLevelOrigin,
                                   const nsString& aGMPName,
                                   nsCString* aID) override;
 
   static bool Create(Endpoint<PGMPServiceParent>&& aGMPService);
 
@@ -246,17 +246,15 @@ class GMPServiceParent final : public PG
       nsTArray<nsCString>&& aTags, nsTArray<ProcessId>&& aAlreadyBridgedTo,
       uint32_t* aOutPluginId, ProcessId* aOutID, nsCString* aOutDisplayName,
       Endpoint<PGMPContentParent>* aOutEndpoint, nsresult* aOutRv,
       nsCString* aOutErrorDescription) override;
 
  private:
   ~GMPServiceParent();
 
-  void Destroy();
-
   RefPtr<GeckoMediaPluginServiceParent> mService;
 };
 
 }  // namespace gmp
 }  // namespace mozilla
 
 #endif  // GMPServiceParent_h_
--- a/dom/media/gmp/moz.build
+++ b/dom/media/gmp/moz.build
@@ -73,17 +73,16 @@ UNIFIED_SOURCES += [
     "ChromiumCDMCallbackProxy.cpp",
     "ChromiumCDMChild.cpp",
     "ChromiumCDMParent.cpp",
     "ChromiumCDMProxy.cpp",
     "DecryptJob.cpp",
     "GMPChild.cpp",
     "GMPContentChild.cpp",
     "GMPContentParent.cpp",
-    "GMPCrashHelper.cpp",
     "GMPCrashHelperHolder.cpp",
     "GMPDiskStorage.cpp",
     "GMPLoader.cpp",
     "GMPMemoryStorage.cpp",
     "GMPParent.cpp",
     "GMPPlatform.cpp",
     "GMPProcessChild.cpp",
     "GMPProcessParent.cpp",