From 478a75705e2d46a5bf4e62b30e8acf61075ff626 Mon Sep 17 00:00:00 2001 From: alwu Date: Tue, 11 Feb 2020 00:41:14 +0000 Subject: [PATCH] Bug 1613329 - part1 : append text track to media element when binding track element to DOM tree. r=bzbarsky This change ensures that we always remember to add a text track to media element's track list. Differential Revision: https://phabricator.services.mozilla.com/D61666 --HG-- extra : moz-landing-system : lando --- dom/html/HTMLTrackElement.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/dom/html/HTMLTrackElement.cpp b/dom/html/HTMLTrackElement.cpp index 182eaeec80dd..d374811154f8 100644 --- a/dom/html/HTMLTrackElement.cpp +++ b/dom/html/HTMLTrackElement.cpp @@ -23,6 +23,7 @@ #include "mozilla/dom/Document.h" #include "nsILoadGroup.h" #include "nsIObserver.h" +#include "nsIScriptError.h" #include "nsISupportsImpl.h" #include "nsISupportsPrimitives.h" #include "nsMappedAttributes.h" @@ -158,11 +159,20 @@ TextTrack* HTMLTrackElement::GetTrack() { if (!mTrack) { CreateTextTrack(); } - return mTrack; } void HTMLTrackElement::CreateTextTrack() { + nsISupports* parentObject = OwnerDoc()->GetParentObject(); + nsCOMPtr window = do_QueryInterface(parentObject); + if (!parentObject) { + nsContentUtils::ReportToConsole( + nsIScriptError::errorFlag, NS_LITERAL_CSTRING("Media"), OwnerDoc(), + nsContentUtils::eDOM_PROPERTIES, + "Using track element in non-window context"); + return; + } + nsString label, srcLang; GetSrclang(srcLang); GetLabel(label); @@ -174,19 +184,11 @@ void HTMLTrackElement::CreateTextTrack() { kind = TextTrackKind::Subtitles; } - nsISupports* parentObject = OwnerDoc()->GetParentObject(); - - NS_ENSURE_TRUE_VOID(parentObject); - - nsCOMPtr window = do_QueryInterface(parentObject); + MOZ_ASSERT(!mTrack, "No need to recreate a text track!"); mTrack = new TextTrack(window, kind, label, srcLang, TextTrackMode::Disabled, TextTrackReadyState::NotLoaded, TextTrackSource::Track); mTrack->SetTrackElement(this); - - if (mMediaParent) { - mMediaParent->AddTextTrack(mTrack); - } } bool HTMLTrackElement::ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute, @@ -392,6 +394,11 @@ nsresult HTMLTrackElement::BindToTree(BindContext& aContext, nsINode& aParent) { if (!mTrack) { CreateTextTrack(); } + // As `CreateTextTrack()` might fail, so we have to check it again. + if (mTrack) { + LOG("Add text track to media parent"); + mMediaParent->AddTextTrack(mTrack); + } MaybeDispatchLoadResource(); }