Bug 1334112 - part1: Add IsLoaded functions for TextTrack, TextTrackList, TextTrackManager. r=rillian draft
authorbechen <bechen@mozilla.com>
Mon, 20 Feb 2017 15:27:06 +0800
changeset 486910 9c3e5ba8a7ac6b9e3bb99fe9ae10e554422ff268
parent 469313 ece0e0fd16a30d37249a18c41fa65143d7c01b8f
child 486911 72f2b353a7e9a465e5eb2493e2c441d6383c9aff
child 487262 4a77aa7d6e8cfd366327dd10a03ad5c228c925c0
push id46092
push userbechen@mozilla.com
push dateMon, 20 Feb 2017 07:35:29 +0000
reviewersrillian
bugs1334112
milestone54.0a1
Bug 1334112 - part1: Add IsLoaded functions for TextTrack, TextTrackList, TextTrackManager. r=rillian MozReview-Commit-ID: GBEKZvLUiPn
dom/html/TextTrackManager.cpp
dom/html/TextTrackManager.h
dom/media/TextTrack.cpp
dom/media/TextTrack.h
dom/media/TextTrackList.cpp
dom/media/TextTrackList.h
--- a/dom/html/TextTrackManager.cpp
+++ b/dom/html/TextTrackManager.cpp
@@ -839,10 +839,17 @@ TextTrackManager::ReportTelemetryForCue(
   MOZ_ASSERT(!mNewCues->IsEmpty() || !mLastActiveCues->IsEmpty());
 
   if (!mCueTelemetryReported) {
     Telemetry::Accumulate(Telemetry::WEBVTT_USED_VTT_CUES, 1);
     mCueTelemetryReported = true;
   }
 }
 
+bool
+TextTrackManager::IsLoaded()
+{
+  return mTextTracks ? mTextTracks->AreTextTracksLoaded() : true;
+}
+
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/html/TextTrackManager.h
+++ b/dom/html/TextTrackManager.h
@@ -98,16 +98,18 @@ public:
   {
     mShutdown = true;
   }
 
   void NotifyCueUpdated(TextTrackCue *aCue);
 
   void NotifyReset();
 
+  bool IsLoaded();
+
 private:
   /**
    * Converts the TextTrackCue's cuetext into a tree of DOM objects
    * and attaches it to a div on its owning TrackElement's
    * MediaElement's caption overlay.
    */
   void UpdateCueDisplay();
 
--- a/dom/media/TextTrack.cpp
+++ b/dom/media/TextTrack.cpp
@@ -332,10 +332,27 @@ TextTrack::DispatchAsyncTrustedEvent(con
   RefPtr<TextTrack> self = this;
   NS_DispatchToMainThread(
     NS_NewRunnableFunction([self, aEventName]() {
       self->DispatchTrustedEvent(aEventName);
     })
   );
 }
 
+bool
+TextTrack::IsLoaded()
+{
+  if (mMode == TextTrackMode::Disabled) {
+    return true;
+  }
+  // If the TrackElement's src is null, we can not block the
+  // MediaElement.
+  if (mTrackElement) {
+    nsAutoString src;
+    if (!(mTrackElement->GetAttr(kNameSpaceID_None, nsGkAtoms::src, src))) {
+      return true;
+    }
+  }
+  return (mReadyState >= Loaded);
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/media/TextTrack.h
+++ b/dom/media/TextTrack.h
@@ -113,16 +113,18 @@ public:
   }
 
   void SetCuesInactive();
 
   void NotifyCueUpdated(TextTrackCue *aCue);
 
   void DispatchAsyncTrustedEvent(const nsString& aEventName);
 
+  bool IsLoaded();
+
 private:
   ~TextTrack();
 
   RefPtr<TextTrackList> mTextTrackList;
 
   TextTrackKind mKind;
   nsString mLabel;
   nsString mLanguage;
--- a/dom/media/TextTrackList.cpp
+++ b/dom/media/TextTrackList.cpp
@@ -227,10 +227,22 @@ TextTrackList::SetTextTrackManager(TextT
 void
 TextTrackList::SetCuesInactive()
 {
   for (uint32_t i = 0; i < Length(); i++) {
     mTextTracks[i]->SetCuesInactive();
   }
 }
 
+
+bool TextTrackList::AreTextTracksLoaded()
+{
+  // Return false if any texttrack is not loaded.
+  for (uint32_t i = 0; i < Length(); i++) {
+    if (!mTextTracks[i]->IsLoaded()) {
+      return false;
+    }
+  }
+  return true;
+}
+
 } // namespace dom
 } // namespace mozilla
--- a/dom/media/TextTrackList.h
+++ b/dom/media/TextTrackList.h
@@ -58,16 +58,18 @@ public:
 
   HTMLMediaElement* GetMediaElement();
   void SetTextTrackManager(TextTrackManager* aTextTrackManager);
 
   nsresult DispatchTrackEvent(nsIDOMEvent* aEvent);
   void CreateAndDispatchChangeEvent();
   void SetCuesInactive();
 
+  bool AreTextTracksLoaded();
+
   IMPL_EVENT_HANDLER(change)
   IMPL_EVENT_HANDLER(addtrack)
   IMPL_EVENT_HANDLER(removetrack)
 
   bool mPendingTextTrackChange = false;
 
 private:
   ~TextTrackList();