Bug 1266926 - Move CaptureStreamTrackSource cleanup to Destroy method. r=jesup
authorAndreas Pehrson <pehrsons@gmail.com>
Mon, 09 May 2016 11:52:53 +0200
changeset 321109 a59c7993e3745dbef1a2efc6ec07c804d052af59
parent 321108 907c72cf22c11fe1b6186b66aa49cdbdaa0b2838
child 321110 999dad3716412d4a6c7ad408a3c17d6819d1fcba
push id9671
push userraliiev@mozilla.com
push dateMon, 06 Jun 2016 20:27:52 +0000
treeherdermozilla-aurora@cea65ca3d0bd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup
bugs1266926
milestone49.0a1
Bug 1266926 - Move CaptureStreamTrackSource cleanup to Destroy method. r=jesup
dom/html/HTMLMediaElement.cpp
dom/media/MediaStreamTrack.cpp
dom/media/MediaStreamTrack.h
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -1903,16 +1903,24 @@ public:
                              true,
                              nsString())
     , mElement(aElement)
   {
     MOZ_ASSERT(mElement);
     mElement->AddDecoderPrincipalChangeObserver(this);
   }
 
+  void Destroy() override
+  {
+    MOZ_ASSERT(mElement);
+    DebugOnly<bool> res = mElement->RemoveDecoderPrincipalChangeObserver(this);
+    NS_ASSERTION(res, "Removing decoder principal changed observer failed. "
+                      "Had it already been removed?");
+  }
+
   MediaSourceEnum GetMediaSource() const override
   {
     return MediaSourceEnum::Other;
   }
 
   CORSMode GetCORSMode() const override
   {
     return mElement->GetCORSMode();
@@ -1939,21 +1947,16 @@ public:
     if (nsContentUtils::CombineResourcePrincipals(&mPrincipal, newPrincipal)) {
       PrincipalChanged();
     }
   }
 
 protected:
   virtual ~CaptureStreamTrackSource()
   {
-    if (mElement) {
-      DebugOnly<bool> res = mElement->RemoveDecoderPrincipalChangeObserver(this);
-      NS_ASSERTION(res, "Removing decoder principal changed observer failed. "
-                        "Had it already been removed?");
-    }
   }
 
   RefPtr<HTMLMediaElement> mElement;
 };
 
 NS_IMPL_ADDREF_INHERITED(HTMLMediaElement::CaptureStreamTrackSource,
                          MediaStreamTrackSource)
 NS_IMPL_RELEASE_INHERITED(HTMLMediaElement::CaptureStreamTrackSource,
--- a/dom/media/MediaStreamTrack.cpp
+++ b/dom/media/MediaStreamTrack.cpp
@@ -20,17 +20,27 @@ static PRLogModuleInfo* gMediaStreamTrac
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(MediaStreamTrackSource)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(MediaStreamTrackSource)
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(MediaStreamTrackSource)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
-NS_IMPL_CYCLE_COLLECTION(MediaStreamTrackSource, mPrincipal)
+
+NS_IMPL_CYCLE_COLLECTION_CLASS(MediaStreamTrackSource)
+
+NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(MediaStreamTrackSource)
+  tmp->Destroy();
+  NS_IMPL_CYCLE_COLLECTION_UNLINK(mPrincipal)
+NS_IMPL_CYCLE_COLLECTION_UNLINK_END
+
+NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(MediaStreamTrackSource)
+  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPrincipal)
+NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
 
 already_AddRefed<Promise>
 MediaStreamTrackSource::ApplyConstraints(nsPIDOMWindowInner* aWindow,
                                          const dom::MediaTrackConstraints& aConstraints,
                                          ErrorResult &aRv)
 {
   nsCOMPtr<nsIGlobalObject> go = do_QueryInterface(aWindow);
   RefPtr<Promise> promise = Promise::Create(go, aRv);
--- a/dom/media/MediaStreamTrack.h
+++ b/dom/media/MediaStreamTrack.h
@@ -62,16 +62,23 @@ public:
       mIsRemote(aIsRemote),
       mLabel(aLabel),
       mStopped(false)
   {
     MOZ_COUNT_CTOR(MediaStreamTrackSource);
   }
 
   /**
+   * Use to clean up any resources that have to be cleaned before the
+   * destructor is called. It is often too late in the destructor because
+   * of garbage collection having removed the members already.
+   */
+  virtual void Destroy() {}
+
+  /**
    * Gets the source's MediaSourceEnum for usage by PeerConnections.
    */
   virtual MediaSourceEnum GetMediaSource() const = 0;
 
   /**
    * Get this TrackSource's principal.
    */
   nsIPrincipal* GetPrincipal() const { return mPrincipal; }