OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 13 matching lines...) Expand all Loading... |
24 */ | 24 */ |
25 | 25 |
26 #include "config.h" | 26 #include "config.h" |
27 #include "core/html/track/TextTrackCueList.h" | 27 #include "core/html/track/TextTrackCueList.h" |
28 | 28 |
29 #include "wtf/StdLibExtras.h" | 29 #include "wtf/StdLibExtras.h" |
30 | 30 |
31 namespace blink { | 31 namespace blink { |
32 | 32 |
33 TextTrackCueList::TextTrackCueList() | 33 TextTrackCueList::TextTrackCueList() |
| 34 : m_firstInvalidIndex(0) |
34 { | 35 { |
35 } | 36 } |
36 | 37 |
37 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(TextTrackCueList); | 38 DEFINE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(TextTrackCueList); |
38 | 39 |
39 unsigned long TextTrackCueList::length() const | 40 unsigned long TextTrackCueList::length() const |
40 { | 41 { |
41 return m_list.size(); | 42 return m_list.size(); |
42 } | 43 } |
43 | 44 |
44 unsigned long TextTrackCueList::getCueIndex(TextTrackCue* cue) const | |
45 { | |
46 return m_list.find(cue); | |
47 } | |
48 | |
49 TextTrackCue* TextTrackCueList::item(unsigned index) const | 45 TextTrackCue* TextTrackCueList::item(unsigned index) const |
50 { | 46 { |
51 if (index < m_list.size()) | 47 if (index < m_list.size()) |
52 return m_list[index].get(); | 48 return m_list[index].get(); |
53 return nullptr; | 49 return nullptr; |
54 } | 50 } |
55 | 51 |
56 TextTrackCue* TextTrackCueList::getCueById(const AtomicString& id) const | 52 TextTrackCue* TextTrackCueList::getCueById(const AtomicString& id) const |
57 { | 53 { |
58 for (size_t i = 0; i < m_list.size(); ++i) { | 54 for (size_t i = 0; i < m_list.size(); ++i) { |
(...skipping 19 matching lines...) Expand all Loading... |
78 | 74 |
79 // Maintain text track cue order: | 75 // Maintain text track cue order: |
80 // https://html.spec.whatwg.org/#text-track-cue-order | 76 // https://html.spec.whatwg.org/#text-track-cue-order |
81 size_t index = findInsertionIndex(cue.get()); | 77 size_t index = findInsertionIndex(cue.get()); |
82 | 78 |
83 // FIXME: The cue should not exist in the list in the first place. | 79 // FIXME: The cue should not exist in the list in the first place. |
84 if (!m_list.isEmpty() && (index > 0) && (m_list[index - 1].get() == cue.get(
))) | 80 if (!m_list.isEmpty() && (index > 0) && (m_list[index - 1].get() == cue.get(
))) |
85 return false; | 81 return false; |
86 | 82 |
87 m_list.insert(index, cue); | 83 m_list.insert(index, cue); |
88 invalidateCueIndexes(index); | 84 invalidateCueIndex(index); |
89 return true; | 85 return true; |
90 } | 86 } |
91 | 87 |
92 static bool cueIsBefore(const TextTrackCue* cue, PassRefPtrWillBeRawPtr<TextTrac
kCue> otherCue) | 88 static bool cueIsBefore(const TextTrackCue* cue, PassRefPtrWillBeRawPtr<TextTrac
kCue> otherCue) |
93 { | 89 { |
94 if (cue->startTime() < otherCue->startTime()) | 90 if (cue->startTime() < otherCue->startTime()) |
95 return true; | 91 return true; |
96 | 92 |
97 return cue->startTime() == otherCue->startTime() && cue->endTime() > otherCu
e->endTime(); | 93 return cue->startTime() == otherCue->startTime() && cue->endTime() > otherCu
e->endTime(); |
98 } | 94 } |
99 | 95 |
100 size_t TextTrackCueList::findInsertionIndex(const TextTrackCue* cueToInsert) con
st | 96 size_t TextTrackCueList::findInsertionIndex(const TextTrackCue* cueToInsert) con
st |
101 { | 97 { |
102 auto it = std::upper_bound(m_list.begin(), m_list.end(), cueToInsert, cueIsB
efore); | 98 auto it = std::upper_bound(m_list.begin(), m_list.end(), cueToInsert, cueIsB
efore); |
103 size_t index = safeCast<size_t>(it - m_list.begin()); | 99 size_t index = safeCast<size_t>(it - m_list.begin()); |
104 ASSERT_WITH_SECURITY_IMPLICATION(index <= m_list.size()); | 100 ASSERT_WITH_SECURITY_IMPLICATION(index <= m_list.size()); |
105 return index; | 101 return index; |
106 } | 102 } |
107 | 103 |
108 bool TextTrackCueList::remove(TextTrackCue* cue) | 104 bool TextTrackCueList::remove(TextTrackCue* cue) |
109 { | 105 { |
110 size_t index = m_list.find(cue); | 106 size_t index = m_list.find(cue); |
111 if (index == kNotFound) | 107 if (index == kNotFound) |
112 return false; | 108 return false; |
113 | 109 |
114 m_list.remove(index); | 110 m_list.remove(index); |
115 // FIXME: While removing a cue does not invalidate the cue order, it does | 111 invalidateCueIndex(index); |
116 // make it more difficult to maintain the invariant, so should probably | 112 cue->invalidateCueIndex(); |
117 // just invalidate here as well. | |
118 return true; | 113 return true; |
119 } | 114 } |
120 | 115 |
121 void TextTrackCueList::updateCueIndex(TextTrackCue* cue) | 116 void TextTrackCueList::updateCueIndex(TextTrackCue* cue) |
122 { | 117 { |
123 if (!remove(cue)) | 118 if (!remove(cue)) |
124 return; | 119 return; |
125 | |
126 // FIXME: If moving the cue such that its index in list increases, then | |
127 // what happens with the cached index on cues in the range [oldIndex, | |
128 // newIndex)? (Some of the indices will be "safe", but there'll be a risk | |
129 // that the lazy update via cueIndex() yields duplicates/incorrect order.) | |
130 add(cue); | 120 add(cue); |
131 } | 121 } |
132 | 122 |
133 void TextTrackCueList::clear() | 123 void TextTrackCueList::clear() |
134 { | 124 { |
135 m_list.clear(); | 125 m_list.clear(); |
136 } | 126 } |
137 | 127 |
138 void TextTrackCueList::invalidateCueIndexes(size_t start) | 128 void TextTrackCueList::invalidateCueIndex(size_t index) |
139 { | 129 { |
140 // FIXME: When iterating cues we could as well update their cached indices t
oo. | 130 // Store the smallest (first) index that we know has a cue that does not |
141 for (size_t i = start; i < m_list.size(); ++i) | 131 // meet the criteria: |
142 m_list[i]->invalidateCueIndex(); | 132 // cueIndex(list[index-1]) + 1 == cueIndex(list[index]) [index > 0] |
| 133 // This is a stronger requirement than we need, but it's easier to maintain. |
| 134 // We can then check if a cue's index is valid by comparing it with |
| 135 // |m_firstInvalidIndex| - if it's strictly less it is valid. |
| 136 m_firstInvalidIndex = std::min(m_firstInvalidIndex, index); |
| 137 } |
| 138 |
| 139 void TextTrackCueList::validateCueIndexes() |
| 140 { |
| 141 // Compute new index values for the cues starting at |
| 142 // |m_firstInvalidIndex|. If said index is beyond the end of the list, no |
| 143 // cues will need to be updated. |
| 144 for (size_t i = m_firstInvalidIndex; i < m_list.size(); ++i) |
| 145 m_list[i]->updateCueIndex(safeCast<unsigned>(i)); |
| 146 m_firstInvalidIndex = m_list.size(); |
143 } | 147 } |
144 | 148 |
145 DEFINE_TRACE(TextTrackCueList) | 149 DEFINE_TRACE(TextTrackCueList) |
146 { | 150 { |
147 visitor->trace(m_list); | 151 visitor->trace(m_list); |
148 } | 152 } |
149 | 153 |
150 } // namespace blink | 154 } // namespace blink |
OLD | NEW |