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) | |
philipj_slow
2015/02/26 15:43:12
After seeing how m_firstInvalidIndex is used I und
fs
2015/02/26 16:09:34
I added some comments around the invalidate/valida
| |
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 24 matching lines...) Expand all Loading... | |
83 | 79 |
84 // Maintain text track cue order: | 80 // Maintain text track cue order: |
85 // https://html.spec.whatwg.org/#text-track-cue-order | 81 // https://html.spec.whatwg.org/#text-track-cue-order |
86 size_t index = findInsertionIndex(cue.get()); | 82 size_t index = findInsertionIndex(cue.get()); |
87 | 83 |
88 // FIXME: The cue should not exist in the list in the first place. | 84 // FIXME: The cue should not exist in the list in the first place. |
89 if (!m_list.isEmpty() && (index > 0) && (m_list[index - 1].get() == cue.get( ))) | 85 if (!m_list.isEmpty() && (index > 0) && (m_list[index - 1].get() == cue.get( ))) |
90 return false; | 86 return false; |
91 | 87 |
92 m_list.insert(index, cue); | 88 m_list.insert(index, cue); |
93 invalidateCueIndexes(index); | 89 invalidateCueIndex(index); |
94 return true; | 90 return true; |
95 } | 91 } |
96 | 92 |
97 static bool cueIsBefore(const TextTrackCue* cue, PassRefPtrWillBeRawPtr<TextTrac kCue> otherCue) | 93 static bool cueIsBefore(const TextTrackCue* cue, PassRefPtrWillBeRawPtr<TextTrac kCue> otherCue) |
98 { | 94 { |
99 if (cue->startTime() < otherCue->startTime()) | 95 if (cue->startTime() < otherCue->startTime()) |
100 return true; | 96 return true; |
101 | 97 |
102 return cue->startTime() == otherCue->startTime() && cue->endTime() > otherCu e->endTime(); | 98 return cue->startTime() == otherCue->startTime() && cue->endTime() > otherCu e->endTime(); |
103 } | 99 } |
104 | 100 |
105 size_t TextTrackCueList::findInsertionIndex(const TextTrackCue* cueToInsert) con st | 101 size_t TextTrackCueList::findInsertionIndex(const TextTrackCue* cueToInsert) con st |
106 { | 102 { |
107 auto it = std::upper_bound(m_list.begin(), m_list.end(), cueToInsert, cueIsB efore); | 103 auto it = std::upper_bound(m_list.begin(), m_list.end(), cueToInsert, cueIsB efore); |
108 size_t index = safeCast<size_t>(it - m_list.begin()); | 104 size_t index = safeCast<size_t>(it - m_list.begin()); |
109 ASSERT_WITH_SECURITY_IMPLICATION(index <= m_list.size()); | 105 ASSERT_WITH_SECURITY_IMPLICATION(index <= m_list.size()); |
110 return index; | 106 return index; |
111 } | 107 } |
112 | 108 |
113 bool TextTrackCueList::remove(TextTrackCue* cue) | 109 bool TextTrackCueList::remove(TextTrackCue* cue) |
114 { | 110 { |
115 size_t index = m_list.find(cue); | 111 size_t index = m_list.find(cue); |
116 if (index == kNotFound) | 112 if (index == kNotFound) |
117 return false; | 113 return false; |
118 | 114 |
119 m_list.remove(index); | 115 m_list.remove(index); |
120 // FIXME: While removing a cue does not invalidate the cue order, it does | 116 invalidateCueIndex(index); |
121 // make it more difficult to maintain the invariant, so should probably | |
122 // just invalidate here as well. | |
123 return true; | 117 return true; |
124 } | 118 } |
125 | 119 |
126 void TextTrackCueList::updateCueIndex(TextTrackCue* cue) | 120 void TextTrackCueList::updateCueIndex(TextTrackCue* cue) |
127 { | 121 { |
128 if (!remove(cue)) | 122 if (!remove(cue)) |
129 return; | 123 return; |
130 | 124 |
131 cue->setIsActive(false); | 125 cue->setIsActive(false); |
132 cue->removeDisplayTree(); | 126 cue->removeDisplayTree(); |
133 | 127 |
134 // FIXME: If moving the cue such that its index in list increases, then | |
135 // what happens with the cached index on cues in the range [oldIndex, | |
136 // newIndex)? (Some of the indices will be "safe", but there'll be a risk | |
137 // that the lazy update via cueIndex() yields duplicates/incorrect order.) | |
138 add(cue); | 128 add(cue); |
139 } | 129 } |
140 | 130 |
141 void TextTrackCueList::clear() | 131 void TextTrackCueList::clear() |
142 { | 132 { |
143 m_list.clear(); | 133 m_list.clear(); |
144 } | 134 } |
145 | 135 |
146 void TextTrackCueList::invalidateCueIndexes(size_t start) | 136 void TextTrackCueList::invalidateCueIndex(size_t index) |
147 { | 137 { |
148 // FIXME: When iterating cues we could as well update their cached indices t oo. | 138 m_firstInvalidIndex = std::min(m_firstInvalidIndex, index); |
149 for (size_t i = start; i < m_list.size(); ++i) | 139 } |
150 m_list[i]->invalidateCueIndex(); | 140 |
141 void TextTrackCueList::validateCueIndexes() | |
142 { | |
143 for (size_t i = m_firstInvalidIndex; i < m_list.size(); ++i) | |
144 m_list[i]->updateCueIndex(safeCast<unsigned>(i)); | |
philipj_slow
2015/02/26 15:43:12
Yeah, size_t might not be too bad for the cue inde
fs
2015/02/26 16:09:34
I suspect 2^32-2 is a difficult enough limit to re
| |
145 m_firstInvalidIndex = m_list.size(); | |
151 } | 146 } |
152 | 147 |
153 DEFINE_TRACE(TextTrackCueList) | 148 DEFINE_TRACE(TextTrackCueList) |
154 { | 149 { |
155 visitor->trace(m_list); | 150 visitor->trace(m_list); |
156 visitor->trace(m_activeCues); | 151 visitor->trace(m_activeCues); |
157 } | 152 } |
158 | 153 |
159 } // namespace blink | 154 } // namespace blink |
OLD | NEW |