1 /* 2 * Copyright (C) 2011 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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. 12 * 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. 24 */ 25 26 #include "config.h" 27 #include "core/html/track/LoadableTextTrack.h" 28 29 #include "core/html/HTMLTrackElement.h" 30 #include "core/html/track/TextTrackCueList.h" 31 #include "core/html/track/vtt/VTTRegionList.h" 32 33 namespace WebCore { 34 35 LoadableTextTrack::LoadableTextTrack(HTMLTrackElement* track) 36 : TextTrack(track->document(), track, emptyAtom, emptyAtom, emptyAtom, emptyAtom, TrackElement) 37 , m_trackElement(track) 38 , m_loadTimer(this, &LoadableTextTrack::loadTimerFired) 39 , m_isDefault(false) 40 { 41 } 42 43 LoadableTextTrack::~LoadableTextTrack() 44 { 45 } 46 47 void LoadableTextTrack::clearClient() 48 { 49 m_trackElement = 0; 50 TextTrack::clearClient(); 51 } 52 53 void LoadableTextTrack::scheduleLoad(const KURL& url) 54 { 55 if (url == m_url) { 56 // If loading of the resource from this URL is in progress, return early. 57 ASSERT(m_loader && m_trackElement); 58 if (m_loader->loadState() < TextTrackLoader::Finished) 59 return; 60 61 // The track element might have changed its state to HTMLTrackElement::Loading 62 // waiting for a call to didCompleteLoad to continue. 63 cueLoadingCompleted(m_loader.get(), m_loader->loadState() == TextTrackLoader::Failed); 64 return; 65 } 66 67 // 4.8.10.12.3 Sourcing out-of-band text tracks (continued) 68 69 // 2. Let URL be the track URL of the track element. 70 m_url = url; 71 72 // 3. Asynchronously run the remaining steps, while continuing with whatever task 73 // was responsible for creating the text track or changing the text track mode. 74 if (!m_loadTimer.isActive()) 75 m_loadTimer.startOneShot(0); 76 } 77 78 void LoadableTextTrack::loadTimerFired(Timer<LoadableTextTrack>*) 79 { 80 if (m_loader) 81 m_loader->cancelLoad(); 82 83 if (!m_trackElement) 84 return; 85 86 // 4.8.10.12.3 Sourcing out-of-band text tracks (continued) 87 88 // 4. Download: If URL is not the empty string, perform a potentially CORS-enabled fetch of URL, with the 89 // mode being the state of the media element's crossorigin content attribute, the origin being the 90 // origin of the media element's Document, and the default origin behaviour set to fail. 91 m_loader = TextTrackLoader::create(*this, m_trackElement->document()); 92 if (!m_loader->load(m_url, m_trackElement->mediaElementCrossOriginAttribute())) 93 m_trackElement->didCompleteLoad(HTMLTrackElement::Failure); 94 } 95 96 void LoadableTextTrack::newCuesAvailable(TextTrackLoader* loader) 97 { 98 ASSERT_UNUSED(loader, m_loader == loader); 99 100 Vector<RefPtr<VTTCue> > newCues; 101 m_loader->getNewCues(newCues); 102 103 if (!m_cues) 104 m_cues = TextTrackCueList::create(); 105 106 for (size_t i = 0; i < newCues.size(); ++i) { 107 newCues[i]->setTrack(this); 108 m_cues->add(newCues[i]); 109 } 110 111 if (client()) 112 client()->textTrackAddCues(this, m_cues.get()); 113 } 114 115 void LoadableTextTrack::cueLoadingCompleted(TextTrackLoader* loader, bool loadingFailed) 116 { 117 ASSERT_UNUSED(loader, m_loader == loader); 118 119 if (!m_trackElement) 120 return; 121 122 m_trackElement->didCompleteLoad(loadingFailed ? HTMLTrackElement::Failure : HTMLTrackElement::Success); 123 } 124 125 void LoadableTextTrack::newRegionsAvailable(TextTrackLoader* loader) 126 { 127 ASSERT_UNUSED(loader, m_loader == loader); 128 129 Vector<RefPtr<VTTRegion> > newRegions; 130 m_loader->getNewRegions(newRegions); 131 132 for (size_t i = 0; i < newRegions.size(); ++i) { 133 newRegions[i]->setTrack(this); 134 regions()->add(newRegions[i]); 135 } 136 } 137 138 size_t LoadableTextTrack::trackElementIndex() 139 { 140 ASSERT(m_trackElement); 141 ASSERT(m_trackElement->parentNode()); 142 143 size_t index = 0; 144 for (Node* node = m_trackElement->parentNode()->firstChild(); node; node = node->nextSibling()) { 145 if (!node->hasTagName(trackTag) || !node->parentNode()) 146 continue; 147 if (node == m_trackElement) 148 return index; 149 ++index; 150 } 151 ASSERT_NOT_REACHED(); 152 153 return 0; 154 } 155 156 } // namespace WebCore 157 158