2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "core/html/track/LoadableTextTrack.h"
29 #include "core/dom/ElementTraversal.h"
30 #include "core/html/HTMLMediaElement.h"
31 #include "core/html/HTMLTrackElement.h"
32 #include "core/html/track/TextTrackCueList.h"
33 #include "core/html/track/vtt/VTTRegionList.h"
37 using namespace HTMLNames;
39 LoadableTextTrack::LoadableTextTrack(HTMLTrackElement* track)
40 : TextTrack(emptyAtom, emptyAtom, emptyAtom, emptyAtom, TrackElement)
41 , m_trackElement(track)
42 , m_loadTimer(this, &LoadableTextTrack::loadTimerFired)
47 LoadableTextTrack::~LoadableTextTrack()
50 ASSERT(!m_trackElement);
55 void LoadableTextTrack::clearTrackElement()
57 m_trackElement = nullptr;
61 void LoadableTextTrack::setMode(const AtomicString& mode)
63 TextTrack::setMode(mode);
69 if (m_trackElement->readyState() == HTMLTrackElement::NONE)
70 m_trackElement->scheduleLoad();
73 void LoadableTextTrack::scheduleLoad(const KURL& url)
76 // If loading of the resource from this URL is in progress, return early.
77 ASSERT(m_loader && m_trackElement);
78 if (m_loader->loadState() < TextTrackLoader::Finished)
81 // The track element might have changed its state to HTMLTrackElement::Loading
82 // waiting for a call to didCompleteLoad to continue.
83 cueLoadingCompleted(m_loader.get(), m_loader->loadState() == TextTrackLoader::Failed);
87 // 4.8.10.12.3 Sourcing out-of-band text tracks (continued)
89 // 2. Let URL be the track URL of the track element.
92 // 3. Asynchronously run the remaining steps, while continuing with whatever task
93 // was responsible for creating the text track or changing the text track mode.
94 if (!m_loadTimer.isActive())
95 m_loadTimer.startOneShot(0, FROM_HERE);
98 void LoadableTextTrack::loadTimerFired(Timer<LoadableTextTrack>*)
101 m_loader->cancelLoad();
108 // 4.8.10.12.3 Sourcing out-of-band text tracks (continued)
110 // 4. Download: If URL is not the empty string, perform a potentially CORS-enabled fetch of URL, with the
111 // mode being the state of the media element's crossorigin content attribute, the origin being the
112 // origin of the media element's Document, and the default origin behaviour set to fail.
113 m_loader = TextTrackLoader::create(*this, m_trackElement->document());
114 if (!m_loader->load(m_url, m_trackElement->mediaElementCrossOriginAttribute()))
115 m_trackElement->didCompleteLoad(HTMLTrackElement::Failure);
118 void LoadableTextTrack::newCuesAvailable(TextTrackLoader* loader)
120 ASSERT_UNUSED(loader, m_loader == loader);
122 WillBeHeapVector<RefPtrWillBeMember<VTTCue> > newCues;
123 m_loader->getNewCues(newCues);
126 m_cues = TextTrackCueList::create();
128 for (size_t i = 0; i < newCues.size(); ++i) {
129 newCues[i]->setTrack(this);
130 m_cues->add(newCues[i].release());
134 mediaElement()->textTrackAddCues(this, m_cues.get());
137 void LoadableTextTrack::cueLoadingCompleted(TextTrackLoader* loader, bool loadingFailed)
139 ASSERT_UNUSED(loader, m_loader == loader);
146 m_trackElement->didCompleteLoad(loadingFailed ? HTMLTrackElement::Failure : HTMLTrackElement::Success);
149 void LoadableTextTrack::newRegionsAvailable(TextTrackLoader* loader)
151 ASSERT_UNUSED(loader, m_loader == loader);
153 WillBeHeapVector<RefPtrWillBeMember<VTTRegion> > newRegions;
154 m_loader->getNewRegions(newRegions);
156 for (size_t i = 0; i < newRegions.size(); ++i) {
157 newRegions[i]->setTrack(this);
158 regions()->add(newRegions[i]);
162 size_t LoadableTextTrack::trackElementIndex()
164 ASSERT(m_trackElement);
165 ASSERT(m_trackElement->parentNode());
168 for (HTMLTrackElement* track = Traversal<HTMLTrackElement>::firstChild(*m_trackElement->parentNode()); track; track = Traversal<HTMLTrackElement>::nextSibling(*track)) {
169 if (!track->parentNode())
171 if (track == m_trackElement)
175 ASSERT_NOT_REACHED();
180 void LoadableTextTrack::trace(Visitor* visitor)
182 visitor->trace(m_trackElement);
183 visitor->trace(m_loader);
184 TextTrack::trace(visitor);