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(track->document(), 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);
67 if (m_trackElement->readyState() == HTMLTrackElement::NONE)
68 m_trackElement->scheduleLoad();
71 void LoadableTextTrack::scheduleLoad(const KURL& url)
74 // If loading of the resource from this URL is in progress, return early.
75 ASSERT(m_loader && m_trackElement);
76 if (m_loader->loadState() < TextTrackLoader::Finished)
79 // The track element might have changed its state to HTMLTrackElement::Loading
80 // waiting for a call to didCompleteLoad to continue.
81 cueLoadingCompleted(m_loader.get(), m_loader->loadState() == TextTrackLoader::Failed);
85 // 4.8.10.12.3 Sourcing out-of-band text tracks (continued)
87 // 2. Let URL be the track URL of the track element.
90 // 3. Asynchronously run the remaining steps, while continuing with whatever task
91 // was responsible for creating the text track or changing the text track mode.
92 if (!m_loadTimer.isActive())
93 m_loadTimer.startOneShot(0, FROM_HERE);
96 void LoadableTextTrack::loadTimerFired(Timer<LoadableTextTrack>*)
99 m_loader->cancelLoad();
104 // 4.8.10.12.3 Sourcing out-of-band text tracks (continued)
106 // 4. Download: If URL is not the empty string, perform a potentially CORS-enabled fetch of URL, with the
107 // mode being the state of the media element's crossorigin content attribute, the origin being the
108 // origin of the media element's Document, and the default origin behaviour set to fail.
109 m_loader = TextTrackLoader::create(*this, m_trackElement->document());
110 if (!m_loader->load(m_url, m_trackElement->mediaElementCrossOriginAttribute()))
111 m_trackElement->didCompleteLoad(HTMLTrackElement::Failure);
114 void LoadableTextTrack::newCuesAvailable(TextTrackLoader* loader)
116 ASSERT_UNUSED(loader, m_loader == loader);
118 WillBeHeapVector<RefPtrWillBeMember<VTTCue> > newCues;
119 m_loader->getNewCues(newCues);
122 m_cues = TextTrackCueList::create();
124 for (size_t i = 0; i < newCues.size(); ++i) {
125 newCues[i]->setTrack(this);
126 m_cues->add(newCues[i].release());
130 mediaElement()->textTrackAddCues(this, m_cues.get());
133 void LoadableTextTrack::cueLoadingCompleted(TextTrackLoader* loader, bool loadingFailed)
135 ASSERT_UNUSED(loader, m_loader == loader);
140 m_trackElement->didCompleteLoad(loadingFailed ? HTMLTrackElement::Failure : HTMLTrackElement::Success);
143 void LoadableTextTrack::newRegionsAvailable(TextTrackLoader* loader)
145 ASSERT_UNUSED(loader, m_loader == loader);
147 WillBeHeapVector<RefPtrWillBeMember<VTTRegion> > newRegions;
148 m_loader->getNewRegions(newRegions);
150 for (size_t i = 0; i < newRegions.size(); ++i) {
151 newRegions[i]->setTrack(this);
152 regions()->add(newRegions[i]);
156 size_t LoadableTextTrack::trackElementIndex()
158 ASSERT(m_trackElement);
159 ASSERT(m_trackElement->parentNode());
162 for (HTMLTrackElement* track = Traversal<HTMLTrackElement>::firstChild(*m_trackElement->parentNode()); track; track = Traversal<HTMLTrackElement>::nextSibling(*track)) {
163 if (!track->parentNode())
165 if (track == m_trackElement)
169 ASSERT_NOT_REACHED();
174 void LoadableTextTrack::trace(Visitor* visitor)
176 visitor->trace(m_trackElement);
177 visitor->trace(m_loader);
178 TextTrack::trace(visitor);
181 } // namespace WebCore