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.
28 #include "core/html/track/TextTrackCueList.h"
32 TextTrackCueList::TextTrackCueList()
34 ScriptWrappable::init(this);
37 unsigned long TextTrackCueList::length() const
42 unsigned long TextTrackCueList::getCueIndex(TextTrackCue* cue) const
44 return m_list.find(cue);
47 TextTrackCue* TextTrackCueList::item(unsigned index) const
49 if (index < m_list.size())
50 return m_list[index].get();
54 TextTrackCue* TextTrackCueList::getCueById(const AtomicString& id) const
56 for (size_t i = 0; i < m_list.size(); ++i) {
57 if (m_list[i]->id() == id)
58 return m_list[i].get();
63 TextTrackCueList* TextTrackCueList::activeCues()
66 m_activeCues = create();
68 m_activeCues->clear();
69 for (size_t i = 0; i < m_list.size(); ++i) {
70 RefPtr<TextTrackCue> cue = m_list[i];
72 m_activeCues->add(cue);
74 return m_activeCues.get();
77 bool TextTrackCueList::add(PassRefPtr<TextTrackCue> cue)
79 ASSERT(cue->startTime() >= 0);
80 ASSERT(cue->endTime() >= 0);
82 return add(cue, 0, m_list.size());
85 bool TextTrackCueList::add(PassRefPtr<TextTrackCue> prpCue, size_t start, size_t end)
87 ASSERT_WITH_SECURITY_IMPLICATION(start <= m_list.size());
88 ASSERT_WITH_SECURITY_IMPLICATION(end <= m_list.size());
90 // Maintain text track cue order:
91 // http://www.whatwg.org/specs/web-apps/current-work/#text-track-cue-order
92 RefPtr<TextTrackCue> cue = prpCue;
94 if (!m_list.isEmpty() && (start > 0) && (m_list[start - 1].get() == cue.get()))
97 m_list.insert(start, cue);
98 invalidateCueIndexes(start);
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);
106 return add(cue.release(), index + 1, end);
109 bool TextTrackCueList::remove(TextTrackCue* cue)
111 size_t index = m_list.find(cue);
112 if (index == kNotFound)
115 cue->setIsActive(false);
116 m_list.remove(index);
120 bool TextTrackCueList::contains(TextTrackCue* cue) const
122 return m_list.contains(cue);
125 bool TextTrackCueList::updateCueIndex(TextTrackCue* cue)
134 void TextTrackCueList::clear()
139 void TextTrackCueList::invalidateCueIndexes(size_t start)
141 for (size_t i = start; i < m_list.size(); ++i)
142 m_list[i]->invalidateCueIndex();
145 } // namespace WebCore