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