OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "config.h" | 5 #include "config.h" |
6 #include "modules/encryptedmedia/MediaKeyStatusMap.h" | 6 #include "modules/encryptedmedia/MediaKeyStatusMap.h" |
7 | 7 |
8 #include "bindings/core/v8/ExceptionState.h" | |
9 #include "bindings/core/v8/ScriptState.h" | |
10 #include "core/dom/DOMArrayBuffer.h" | 8 #include "core/dom/DOMArrayBuffer.h" |
11 #include "core/dom/DOMArrayPiece.h" | 9 #include "core/dom/DOMArrayPiece.h" |
12 #include "public/platform/WebData.h" | 10 #include "public/platform/WebData.h" |
13 #include "wtf/text/WTFString.h" | 11 #include "wtf/text/WTFString.h" |
14 | 12 |
15 namespace blink { | 13 namespace blink { |
16 | 14 |
17 // Represents the key ID and associated status. | 15 // Represents the key ID and associated status. |
18 class MediaKeyStatusMap::MapEntry final : public GarbageCollectedFinalized<Media
KeyStatusMap::MapEntry> { | 16 class MediaKeyStatusMap::MapEntry final : public GarbageCollectedFinalized<Media
KeyStatusMap::MapEntry> { |
19 public: | 17 public: |
(...skipping 23 matching lines...) Expand all Loading... |
43 : m_keyId(DOMArrayBuffer::create(keyId.data(), keyId.size())) | 41 : m_keyId(DOMArrayBuffer::create(keyId.data(), keyId.size())) |
44 , m_status(status) | 42 , m_status(status) |
45 { | 43 { |
46 } | 44 } |
47 | 45 |
48 RefPtr<DOMArrayBuffer> m_keyId; | 46 RefPtr<DOMArrayBuffer> m_keyId; |
49 const String m_status; | 47 const String m_status; |
50 }; | 48 }; |
51 | 49 |
52 // Represents an Iterator that loops through the set of MapEntrys. | 50 // Represents an Iterator that loops through the set of MapEntrys. |
53 class MapIterationSource final : public PairIterable<DOMArrayBuffer*, String>::I
terationSource { | 51 class MapIterationSource final : public PairIterable<ArrayBufferOrArrayBufferVie
w, String>::IterationSource { |
54 public: | 52 public: |
55 MapIterationSource(MediaKeyStatusMap* map) | 53 MapIterationSource(MediaKeyStatusMap* map) |
56 : m_map(map) | 54 : m_map(map) |
57 , m_current(0) | 55 , m_current(0) |
58 { | 56 { |
59 } | 57 } |
60 | 58 |
61 bool next(ScriptState* scriptState, DOMArrayBuffer*& key, String& value, Exc
eptionState&) override | 59 bool next(ScriptState* scriptState, ArrayBufferOrArrayBufferView& key, Strin
g& value, ExceptionState&) override |
62 { | 60 { |
63 // This simply advances an index and returns the next value if any, | 61 // This simply advances an index and returns the next value if any, |
64 // so if the iterated object is mutated values may be skipped. | 62 // so if the iterated object is mutated values may be skipped. |
65 if (m_current >= m_map->size()) | 63 if (m_current >= m_map->size()) |
66 return false; | 64 return false; |
67 | 65 |
68 const auto& entry = m_map->at(m_current++); | 66 const auto& entry = m_map->at(m_current++); |
69 key = entry.keyId(); | 67 key.setArrayBuffer(entry.keyId()); |
70 value = entry.status(); | 68 value = entry.status(); |
71 return true; | 69 return true; |
72 } | 70 } |
73 | 71 |
74 virtual void trace(Visitor* visitor) override | 72 void trace(Visitor* visitor) override |
75 { | 73 { |
76 visitor->trace(m_map); | 74 visitor->trace(m_map); |
77 PairIterable<DOMArrayBuffer*, String>::IterationSource::trace(visitor); | 75 PairIterable<ArrayBufferOrArrayBufferView, String>::IterationSource::tra
ce(visitor); |
78 } | 76 } |
79 | 77 |
80 private: | 78 private: |
81 // m_map is stored just for keeping it alive. It needs to be kept | 79 // m_map is stored just for keeping it alive. It needs to be kept |
82 // alive while JavaScript holds the iterator to it. | 80 // alive while JavaScript holds the iterator to it. |
83 const Member<const MediaKeyStatusMap> m_map; | 81 const Member<const MediaKeyStatusMap> m_map; |
84 size_t m_current; | 82 size_t m_current; |
85 }; | 83 }; |
86 | 84 |
87 void MediaKeyStatusMap::clear() | 85 void MediaKeyStatusMap::clear() |
88 { | 86 { |
89 m_entries.clear(); | 87 m_entries.clear(); |
90 } | 88 } |
91 | 89 |
92 void MediaKeyStatusMap::addEntry(WebData keyId, const String& status) | 90 void MediaKeyStatusMap::addEntry(WebData keyId, const String& status) |
93 { | 91 { |
94 m_entries.append(MapEntry::create(keyId, status)); | 92 m_entries.append(MapEntry::create(keyId, status)); |
95 } | 93 } |
96 | 94 |
97 const MediaKeyStatusMap::MapEntry& MediaKeyStatusMap::at(size_t index) const | 95 const MediaKeyStatusMap::MapEntry& MediaKeyStatusMap::at(size_t index) const |
98 { | 96 { |
99 BLINK_ASSERT(index < m_entries.size()); | 97 BLINK_ASSERT(index < m_entries.size()); |
100 return *m_entries.at(index); | 98 return *m_entries.at(index); |
101 } | 99 } |
102 | 100 |
103 String MediaKeyStatusMap::get(const DOMArrayPiece& key) | |
104 { | |
105 // If |key| is not found this returns the null string. However, it shows | |
106 // up in JavaScript as "". | |
107 size_t index = indexOf(key); | |
108 return (index < m_entries.size()) ? at(index).status() : String(); | |
109 } | |
110 | |
111 bool MediaKeyStatusMap::has(const DOMArrayPiece& key) const | |
112 { | |
113 return indexOf(key) < m_entries.size(); | |
114 } | |
115 | |
116 size_t MediaKeyStatusMap::indexOf(const DOMArrayPiece& key) const | 101 size_t MediaKeyStatusMap::indexOf(const DOMArrayPiece& key) const |
117 { | 102 { |
118 for (size_t index = 0; index < m_entries.size(); ++index) { | 103 for (size_t index = 0; index < m_entries.size(); ++index) { |
119 const auto& current = m_entries.at(index)->keyId(); | 104 const auto& current = m_entries.at(index)->keyId(); |
120 if (key == *current) | 105 if (key == *current) |
121 return index; | 106 return index; |
122 } | 107 } |
123 | 108 |
124 // Not found, so return an index outside the valid range. | 109 // Not found, so return an index outside the valid range. |
125 return m_entries.size(); | 110 return m_entries.size(); |
126 } | 111 } |
127 | 112 |
128 PairIterable<DOMArrayBuffer*, String>::IterationSource* MediaKeyStatusMap::start
Iteration(ScriptState*, ExceptionState&) | 113 PairIterable<ArrayBufferOrArrayBufferView, String>::IterationSource* MediaKeySta
tusMap::startIteration(ScriptState*, ExceptionState&) |
129 { | 114 { |
130 return new MapIterationSource(this); | 115 return new MapIterationSource(this); |
131 } | 116 } |
132 | 117 |
| 118 bool MediaKeyStatusMap::getMapEntry(ScriptState*, const ArrayBufferOrArrayBuffer
View& key, String& value, ExceptionState&) |
| 119 { |
| 120 size_t index = indexOf(key); |
| 121 if (index < m_entries.size()) { |
| 122 value = at(index).status(); |
| 123 return true; |
| 124 } |
| 125 return false; |
| 126 } |
| 127 |
133 void MediaKeyStatusMap::trace(Visitor* visitor) | 128 void MediaKeyStatusMap::trace(Visitor* visitor) |
134 { | 129 { |
135 visitor->trace(m_entries); | 130 visitor->trace(m_entries); |
136 } | 131 } |
137 | 132 |
138 } // namespace blink | 133 } // namespace blink |
OLD | NEW |