| 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" | 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "core/dom/DOMArrayBuffer.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 : m_keyId(DOMArrayBuffer::create(keyId.data(), keyId.size())) | 43 : m_keyId(DOMArrayBuffer::create(keyId.data(), keyId.size())) |
| 44 , m_status(status) | 44 , m_status(status) |
| 45 { | 45 { |
| 46 } | 46 } |
| 47 | 47 |
| 48 RefPtr<DOMArrayBuffer> m_keyId; | 48 RefPtr<DOMArrayBuffer> m_keyId; |
| 49 const String m_status; | 49 const String m_status; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 // Represents an Iterator that loops through the set of MapEntrys. | 52 // Represents an Iterator that loops through the set of MapEntrys. |
| 53 class MapIterationSource final : public PairIterable<DOMArrayBuffer*, String>::I
terationSource { | 53 class MapIterationSource final : public PairIterable<ArrayBufferOrArrayBufferVie
w, String>::IterationSource { |
| 54 public: | 54 public: |
| 55 MapIterationSource(MediaKeyStatusMap* map) | 55 MapIterationSource(MediaKeyStatusMap* map) |
| 56 : m_map(map) | 56 : m_map(map) |
| 57 , m_current(0) | 57 , m_current(0) |
| 58 { | 58 { |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool next(ScriptState* scriptState, DOMArrayBuffer*& key, String& value, Exc
eptionState&) override | 61 bool next(ScriptState* scriptState, ArrayBufferOrArrayBufferView& key, Strin
g& value, ExceptionState&) override |
| 62 { | 62 { |
| 63 // This simply advances an index and returns the next value if any, | 63 // This simply advances an index and returns the next value if any, |
| 64 // so if the iterated object is mutated values may be skipped. | 64 // so if the iterated object is mutated values may be skipped. |
| 65 if (m_current >= m_map->size()) | 65 if (m_current >= m_map->size()) |
| 66 return false; | 66 return false; |
| 67 | 67 |
| 68 const auto& entry = m_map->at(m_current++); | 68 const auto& entry = m_map->at(m_current++); |
| 69 key = entry.keyId(); | 69 key.setArrayBuffer(entry.keyId()); |
| 70 value = entry.status(); | 70 value = entry.status(); |
| 71 return true; | 71 return true; |
| 72 } | 72 } |
| 73 | 73 |
| 74 virtual void trace(Visitor* visitor) override | 74 void trace(Visitor* visitor) override |
| 75 { | 75 { |
| 76 visitor->trace(m_map); | 76 visitor->trace(m_map); |
| 77 PairIterable<DOMArrayBuffer*, String>::IterationSource::trace(visitor); | 77 PairIterable<ArrayBufferOrArrayBufferView, String>::IterationSource::tra
ce(visitor); |
| 78 } | 78 } |
| 79 | 79 |
| 80 private: | 80 private: |
| 81 // m_map is stored just for keeping it alive. It needs to be kept | 81 // m_map is stored just for keeping it alive. It needs to be kept |
| 82 // alive while JavaScript holds the iterator to it. | 82 // alive while JavaScript holds the iterator to it. |
| 83 const Member<const MediaKeyStatusMap> m_map; | 83 const Member<const MediaKeyStatusMap> m_map; |
| 84 size_t m_current; | 84 size_t m_current; |
| 85 }; | 85 }; |
| 86 | 86 |
| 87 void MediaKeyStatusMap::clear() | 87 void MediaKeyStatusMap::clear() |
| 88 { | 88 { |
| 89 m_entries.clear(); | 89 m_entries.clear(); |
| 90 } | 90 } |
| 91 | 91 |
| 92 void MediaKeyStatusMap::addEntry(WebData keyId, const String& status) | 92 void MediaKeyStatusMap::addEntry(WebData keyId, const String& status) |
| 93 { | 93 { |
| 94 m_entries.append(MapEntry::create(keyId, status)); | 94 m_entries.append(MapEntry::create(keyId, status)); |
| 95 } | 95 } |
| 96 | 96 |
| 97 const MediaKeyStatusMap::MapEntry& MediaKeyStatusMap::at(size_t index) const | 97 const MediaKeyStatusMap::MapEntry& MediaKeyStatusMap::at(size_t index) const |
| 98 { | 98 { |
| 99 BLINK_ASSERT(index < m_entries.size()); | 99 BLINK_ASSERT(index < m_entries.size()); |
| 100 return *m_entries.at(index); | 100 return *m_entries.at(index); |
| 101 } | 101 } |
| 102 | 102 |
| 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 | 103 size_t MediaKeyStatusMap::indexOf(const DOMArrayPiece& key) const |
| 117 { | 104 { |
| 118 for (size_t index = 0; index < m_entries.size(); ++index) { | 105 for (size_t index = 0; index < m_entries.size(); ++index) { |
| 119 const auto& current = m_entries.at(index)->keyId(); | 106 const auto& current = m_entries.at(index)->keyId(); |
| 120 if (key == *current) | 107 if (key == *current) |
| 121 return index; | 108 return index; |
| 122 } | 109 } |
| 123 | 110 |
| 124 // Not found, so return an index outside the valid range. | 111 // Not found, so return an index outside the valid range. |
| 125 return m_entries.size(); | 112 return m_entries.size(); |
| 126 } | 113 } |
| 127 | 114 |
| 128 PairIterable<DOMArrayBuffer*, String>::IterationSource* MediaKeyStatusMap::start
Iteration(ScriptState*, ExceptionState&) | 115 PairIterable<ArrayBufferOrArrayBufferView, String>::IterationSource* MediaKeySta
tusMap::startIteration(ScriptState*, ExceptionState&) |
| 129 { | 116 { |
| 130 return new MapIterationSource(this); | 117 return new MapIterationSource(this); |
| 131 } | 118 } |
| 132 | 119 |
| 120 bool MediaKeyStatusMap::getMapEntry(ScriptState*, const ArrayBufferOrArrayBuffer
View& key, String& value, ExceptionState&) |
| 121 { |
| 122 size_t index = indexOf(key); |
| 123 if (index < m_entries.size()) { |
| 124 value = at(index).status(); |
| 125 return true; |
| 126 } |
| 127 return false; |
| 128 } |
| 129 |
| 133 void MediaKeyStatusMap::trace(Visitor* visitor) | 130 void MediaKeyStatusMap::trace(Visitor* visitor) |
| 134 { | 131 { |
| 135 visitor->trace(m_entries); | 132 visitor->trace(m_entries); |
| 136 } | 133 } |
| 137 | 134 |
| 138 } // namespace blink | 135 } // namespace blink |
| OLD | NEW |