Bug 1266926 - Move CaptureStreamTrackSource cleanup to Destroy method. r=jesup, a=al
authorAndreas Pehrson <pehrsons@gmail.com>
Mon, 09 May 2016 11:52:53 +0200
changeset 318710 8fd6c0a620ca78d14b446a16282895812fb1f511
parent 318709 5e345d6d359c93ccb70aeaf6cccb3713f217a3ab
child 318711 d1e8949f9e790141748fe1ceba2a76bc6b9fb72d
push id9540
push usercbook@mozilla.com
push dateTue, 10 May 2016 10:25:42 +0000
treeherdermozilla-aurora@4c5959b81fd5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjesup, al
bugs1266926
milestone48.0a2
Bug 1266926 - Move CaptureStreamTrackSource cleanup to Destroy method. r=jesup, a=al
dom/html/HTMLMediaElement.cpp
dom/media/MediaStreamTrack.cpp
dom/media/MediaStreamTrack.h
--- a/dom/html/HTMLMediaElement.cpp
+++ b/dom/html/HTMLMediaElement.cpp
@@ -1911,16 +1911,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();
@@ -1947,21 +1955,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; }