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/TextTrackCueList.h"
     29 
     30 namespace WebCore {
     31 
     32 TextTrackCueList::TextTrackCueList()
     33 {
     34     ScriptWrappable::init(this);
     35 }
     36 
     37 unsigned long TextTrackCueList::length() const
     38 {
     39     return m_list.size();
     40 }
     41 
     42 unsigned long TextTrackCueList::getCueIndex(TextTrackCue* cue) const
     43 {
     44     return m_list.find(cue);
     45 }
     46 
     47 TextTrackCue* TextTrackCueList::item(unsigned index) const
     48 {
     49     if (index < m_list.size())
     50         return m_list[index].get();
     51     return 0;
     52 }
     53 
     54 TextTrackCue* TextTrackCueList::getCueById(const AtomicString& id) const
     55 {
     56     for (size_t i = 0; i < m_list.size(); ++i) {
     57         if (m_list[i]->id() == id)
     58             return m_list[i].get();
     59     }
     60     return 0;
     61 }
     62 
     63 TextTrackCueList* TextTrackCueList::activeCues()
     64 {
     65     if (!m_activeCues)
     66         m_activeCues = create();
     67 
     68     m_activeCues->clear();
     69     for (size_t i = 0; i < m_list.size(); ++i) {
     70         RefPtrWillBeRawPtr<TextTrackCue> cue = m_list[i];
     71         if (cue->isActive())
     72             m_activeCues->add(cue);
     73     }
     74     return m_activeCues.get();
     75 }
     76 
     77 bool TextTrackCueList::add(PassRefPtrWillBeRawPtr<TextTrackCue> cue)
     78 {
     79     ASSERT(cue->startTime() >= 0);
     80     ASSERT(cue->endTime() >= 0);
     81 
     82     return add(cue, 0, m_list.size());
     83 }
     84 
     85 bool TextTrackCueList::add(PassRefPtrWillBeRawPtr<TextTrackCue> prpCue, size_t start, size_t end)
     86 {
     87     ASSERT_WITH_SECURITY_IMPLICATION(start <= m_list.size());
     88     ASSERT_WITH_SECURITY_IMPLICATION(end <= m_list.size());
     89 
     90     // Maintain text track cue order:
     91     // http://www.whatwg.org/specs/web-apps/current-work/#text-track-cue-order
     92     RefPtrWillBeRawPtr<TextTrackCue> cue = prpCue;
     93     if (start == end) {
     94         if (!m_list.isEmpty() && (start > 0) && (m_list[start - 1].get() == cue.get()))
     95             return false;
     96 
     97         m_list.insert(start, cue);
     98         invalidateCueIndexes(start);
     99         return true;
    100     }
    101 
    102     size_t index = (start + end) / 2;
    103     if (cue->startTime() < m_list[index]->startTime() || (cue->startTime() == m_list[index]->startTime() && cue->endTime() > m_list[index]->endTime()))
    104         return add(cue.release(), start, index);
    105 
    106     return add(cue.release(), index + 1, end);
    107 }
    108 
    109 bool TextTrackCueList::remove(TextTrackCue* cue)
    110 {
    111     size_t index = m_list.find(cue);
    112     if (index == kNotFound)
    113         return false;
    114 
    115     cue->setIsActive(false);
    116     m_list.remove(index);
    117     return true;
    118 }
    119 
    120 bool TextTrackCueList::contains(TextTrackCue* cue) const
    121 {
    122     return m_list.contains(cue);
    123 }
    124 
    125 bool TextTrackCueList::updateCueIndex(TextTrackCue* cue)
    126 {
    127     if (!contains(cue))
    128         return false;
    129 
    130     remove(cue);
    131     return add(cue);
    132 }
    133 
    134 void TextTrackCueList::clear()
    135 {
    136     m_list.clear();
    137 }
    138 
    139 void TextTrackCueList::invalidateCueIndexes(size_t start)
    140 {
    141     for (size_t i = start; i < m_list.size(); ++i)
    142         m_list[i]->invalidateCueIndex();
    143 }
    144 
    145 void TextTrackCueList::trace(Visitor* visitor)
    146 {
    147     visitor->trace(m_list);
    148     visitor->trace(m_activeCues);
    149 }
    150 
    151 } // namespace WebCore
    152 
    153