Bug 1531863 - part1 : implement 'GetMediaElement()' function wrapper. r=jya
authorAlastor Wu <alwu@mozilla.com>
Fri, 08 Mar 2019 18:22:24 +0000
changeset 524459 5b9bb0da7cd6044c94f2405ef2ebf4ade22f89ac
parent 524458 4b280518c7b1798e846ef99af00ba60867dac20c
child 524460 6dbda553b4783b579f0d8046407d31486f2c5403
push id2032
push userffxbld-merge
push dateMon, 13 May 2019 09:36:57 +0000
treeherdermozilla-release@455c1065dcbe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjya
bugs1531863
milestone67.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 1531863 - part1 : implement 'GetMediaElement()' function wrapper. r=jya To create a function wrapper which would automatically check whether track list exists, in order to reduce unnecessary checking for TextTrackList. Differential Revision: https://phabricator.services.mozilla.com/D21809
dom/media/TextTrack.cpp
dom/media/TextTrack.h
--- a/dom/media/TextTrack.cpp
+++ b/dom/media/TextTrack.cpp
@@ -72,33 +72,29 @@ JSObject* TextTrack::WrapObject(JSContex
   return TextTrack_Binding::Wrap(aCx, this, aGivenProto);
 }
 
 void TextTrack::SetMode(TextTrackMode aValue) {
   if (mMode != aValue) {
     mMode = aValue;
     if (aValue == TextTrackMode::Disabled) {
       // Remove all the cues in MediaElement.
-      if (mTextTrackList) {
-        HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
-        if (mediaElement) {
-          for (size_t i = 0; i < mCueList->Length(); ++i) {
-            mediaElement->NotifyCueRemoved(*(*mCueList)[i]);
-          }
+      HTMLMediaElement* mediaElement = GetMediaElement();
+      if (mediaElement) {
+        for (size_t i = 0; i < mCueList->Length(); ++i) {
+          mediaElement->NotifyCueRemoved(*(*mCueList)[i]);
         }
       }
       SetCuesInactive();
     } else {
       // Add all the cues into MediaElement.
-      if (mTextTrackList) {
-        HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
-        if (mediaElement) {
-          for (size_t i = 0; i < mCueList->Length(); ++i) {
-            mediaElement->NotifyCueAdded(*(*mCueList)[i]);
-          }
+      HTMLMediaElement* mediaElement = GetMediaElement();
+      if (mediaElement) {
+        for (size_t i = 0; i < mCueList->Length(); ++i) {
+          mediaElement->NotifyCueAdded(*(*mCueList)[i]);
         }
       }
     }
     if (mTextTrackList) {
       mTextTrackList->CreateAndDispatchChangeEvent();
     }
     // Ensure the TimeMarchesOn is called in case that the mCueList
     // is empty.
@@ -117,37 +113,33 @@ void TextTrack::GetId(nsAString& aId) co
 void TextTrack::AddCue(TextTrackCue& aCue) {
   TextTrack* oldTextTrack = aCue.GetTrack();
   if (oldTextTrack) {
     ErrorResult dummy;
     oldTextTrack->RemoveCue(aCue, dummy);
   }
   mCueList->AddCue(aCue);
   aCue.SetTrack(this);
-  if (mTextTrackList) {
-    HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
-    if (mediaElement && (mMode != TextTrackMode::Disabled)) {
-      mediaElement->NotifyCueAdded(aCue);
-    }
+  HTMLMediaElement* mediaElement = GetMediaElement();
+  if (mediaElement && (mMode != TextTrackMode::Disabled)) {
+    mediaElement->NotifyCueAdded(aCue);
   }
 }
 
 void TextTrack::RemoveCue(TextTrackCue& aCue, ErrorResult& aRv) {
   // Bug1304948, check the aCue belongs to the TextTrack.
   mCueList->RemoveCue(aCue, aRv);
   if (aRv.Failed()) {
     return;
   }
   aCue.SetActive(false);
   aCue.SetTrack(nullptr);
-  if (mTextTrackList) {
-    HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
-    if (mediaElement) {
-      mediaElement->NotifyCueRemoved(aCue);
-    }
+  HTMLMediaElement* mediaElement = GetMediaElement();
+  if (mediaElement) {
+    mediaElement->NotifyCueRemoved(aCue);
   }
 }
 
 void TextTrack::SetCuesDirty() {
   for (uint32_t i = 0; i < mCueList->Length(); i++) {
     ((*mCueList)[i])->Reset();
   }
 }
@@ -170,22 +162,17 @@ TextTrackReadyState TextTrack::ReadyStat
 void TextTrack::SetReadyState(uint32_t aReadyState) {
   if (aReadyState <= TextTrackReadyState::FailedToLoad) {
     SetReadyState(static_cast<TextTrackReadyState>(aReadyState));
   }
 }
 
 void TextTrack::SetReadyState(TextTrackReadyState aState) {
   mReadyState = aState;
-
-  if (!mTextTrackList) {
-    return;
-  }
-
-  HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
+  HTMLMediaElement* mediaElement = GetMediaElement();
   if (mediaElement && (mReadyState == TextTrackReadyState::Loaded ||
                        mReadyState == TextTrackReadyState::FailedToLoad)) {
     mediaElement->RemoveTextTrack(this, true);
     mediaElement->UpdateReadyState();
   }
 }
 
 TextTrackList* TextTrack::GetTextTrackList() { return mTextTrackList; }
@@ -199,21 +186,19 @@ HTMLTrackElement* TextTrack::GetTrackEle
 void TextTrack::SetTrackElement(HTMLTrackElement* aTrackElement) {
   mTrackElement = aTrackElement;
 }
 
 void TextTrack::SetCuesInactive() { mCueList->SetCuesInactive(); }
 
 void TextTrack::NotifyCueUpdated(TextTrackCue* aCue) {
   mCueList->NotifyCueUpdated(aCue);
-  if (mTextTrackList) {
-    HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
-    if (mediaElement) {
-      mediaElement->NotifyCueUpdated(aCue);
-    }
+  HTMLMediaElement* mediaElement = GetMediaElement();
+  if (mediaElement) {
+    mediaElement->NotifyCueUpdated(aCue);
   }
 }
 
 void TextTrack::GetLabel(nsAString& aLabel) const {
   if (mTrackElement) {
     mTrackElement->GetLabel(aLabel);
   } else {
     aLabel = mLabel;
@@ -262,21 +247,17 @@ void TextTrack::NotifyCueActiveStateChan
     mActiveCueList->AddCue(*aCue);
   } else {
     MOZ_ASSERT(mActiveCueList->IsCueExist(aCue));
     mActiveCueList->RemoveCue(*aCue);
   }
 }
 
 void TextTrack::GetCurrentCueList(RefPtr<TextTrackCueList>& aCueList) const {
-  if (!mTextTrackList) {
-    return;
-  }
-
-  const HTMLMediaElement* mediaElement = mTextTrackList->GetMediaElement();
+  const HTMLMediaElement* mediaElement = GetMediaElement();
   if (!mediaElement) {
     return;
   }
 
   // According to `time marches on` step1, current cue list contains the cues
   // whose start times are less than or equal to the current playback position
   // and whose end times are greater than the current playback position.
   // https://html.spec.whatwg.org/multipage/media.html#time-marches-on
@@ -285,10 +266,14 @@ void TextTrack::GetCurrentCueList(RefPtr
   for (uint32_t idx = 0; idx < mCueList->Length(); idx++) {
     TextTrackCue* cue = (*mCueList)[idx];
     if (cue->StartTime() <= playbackTime && cue->EndTime() > playbackTime) {
       aCueList->AddCue(*cue);
     }
   }
 }
 
+HTMLMediaElement* TextTrack::GetMediaElement() const {
+  return mTextTrackList ? mTextTrackList->GetMediaElement() : nullptr;
+}
+
 }  // namespace dom
 }  // namespace mozilla
--- a/dom/media/TextTrack.h
+++ b/dom/media/TextTrack.h
@@ -15,16 +15,17 @@
 
 namespace mozilla {
 namespace dom {
 
 class TextTrackList;
 class TextTrackCue;
 class TextTrackCueList;
 class HTMLTrackElement;
+class HTMLMediaElement;
 
 enum TextTrackSource { Track, AddTextTrack, MediaResourceSpecific };
 
 // Constants for numeric readyState property values.
 enum TextTrackReadyState {
   NotLoaded = 0U,
   Loading = 1U,
   Loaded = 2U,
@@ -105,16 +106,18 @@ class TextTrack final : public DOMEventT
   // Use this function to request current cues which start time are less than or
   // equal to the current playback position and whose end times are greater than
   // the current playback position.
   void GetCurrentCueList(RefPtr<TextTrackCueList>& aCueList) const;
 
  private:
   ~TextTrack();
 
+  HTMLMediaElement* GetMediaElement() const;
+
   RefPtr<TextTrackList> mTextTrackList;
 
   TextTrackKind mKind;
   nsString mLabel;
   nsString mLanguage;
   nsString mType;
   TextTrackMode mMode;