| 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 "core/dom/DOMArrayBuffer.h" | 8 #include "core/dom/DOMArrayBuffer.h" |
| 9 #include "core/dom/DOMArrayPiece.h" | 9 #include "core/dom/DOMArrayPiece.h" |
| 10 #include "public/platform/WebData.h" | 10 #include "public/platform/WebData.h" |
| 11 #include "wtf/text/WTFString.h" | 11 #include "wtf/text/WTFString.h" |
| 12 | 12 |
| 13 #include <algorithm> |
| 14 |
| 13 namespace blink { | 15 namespace blink { |
| 14 | 16 |
| 15 // Represents the key ID and associated status. | 17 // Represents the key ID and associated status. |
| 16 class MediaKeyStatusMap::MapEntry final : public GarbageCollectedFinalized<Media
KeyStatusMap::MapEntry> { | 18 class MediaKeyStatusMap::MapEntry final : public GarbageCollectedFinalized<Media
KeyStatusMap::MapEntry> { |
| 17 public: | 19 public: |
| 18 static MapEntry* create(WebData keyId, const String& status) | 20 static MapEntry* create(WebData keyId, const String& status) |
| 19 { | 21 { |
| 20 return new MapEntry(keyId, status); | 22 return new MapEntry(keyId, status); |
| 21 } | 23 } |
| 22 | 24 |
| 23 virtual ~MapEntry() { } | 25 virtual ~MapEntry() { } |
| 24 | 26 |
| 25 DOMArrayBuffer* keyId() const | 27 DOMArrayBuffer* keyId() const |
| 26 { | 28 { |
| 27 return m_keyId.get(); | 29 return m_keyId.get(); |
| 28 } | 30 } |
| 29 | 31 |
| 30 const String& status() const | 32 const String& status() const |
| 31 { | 33 { |
| 32 return m_status; | 34 return m_status; |
| 33 } | 35 } |
| 34 | 36 |
| 37 static bool compareLessThan(MapEntry* a, MapEntry* b) |
| 38 { |
| 39 // Compare the keyIds of 2 different MapEntries. Assume that |a| and |b| |
| 40 // are not null. KeyIds are compared byte by byte. |
| 41 |
| 42 // Handle null cases first (which shouldn't happen). |
| 43 // |aKeyId| |bKeyId| result |
| 44 // null null == (false) |
| 45 // null not-null < (true) |
| 46 // not-null null > (false) |
| 47 if (!a->keyId() || !b->keyId()) |
| 48 return b->keyId(); |
| 49 |
| 50 // Compare the bytes. |
| 51 int result = memcmp(a->keyId()->data(), b->keyId()->data(), |
| 52 std::min(a->keyId()->byteLength(), b->keyId()->byteLength())); |
| 53 if (result != 0) |
| 54 return result < 0; |
| 55 |
| 56 // KeyIds are equal to the shared length, so the shorter string is <. |
| 57 return a->keyId()->byteLength() <= b->keyId()->byteLength(); |
| 58 } |
| 59 |
| 35 DEFINE_INLINE_VIRTUAL_TRACE() | 60 DEFINE_INLINE_VIRTUAL_TRACE() |
| 36 { | 61 { |
| 37 } | 62 } |
| 38 | 63 |
| 39 private: | 64 private: |
| 40 MapEntry(WebData keyId, const String& status) | 65 MapEntry(WebData keyId, const String& status) |
| 41 : m_keyId(DOMArrayBuffer::create(keyId.data(), keyId.size())) | 66 : m_keyId(DOMArrayBuffer::create(keyId.data(), keyId.size())) |
| 42 , m_status(status) | 67 , m_status(status) |
| 43 { | 68 { |
| 44 } | 69 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 }; | 108 }; |
| 84 | 109 |
| 85 void MediaKeyStatusMap::clear() | 110 void MediaKeyStatusMap::clear() |
| 86 { | 111 { |
| 87 m_entries.clear(); | 112 m_entries.clear(); |
| 88 } | 113 } |
| 89 | 114 |
| 90 void MediaKeyStatusMap::addEntry(WebData keyId, const String& status) | 115 void MediaKeyStatusMap::addEntry(WebData keyId, const String& status) |
| 91 { | 116 { |
| 92 m_entries.append(MapEntry::create(keyId, status)); | 117 m_entries.append(MapEntry::create(keyId, status)); |
| 118 |
| 119 // No need to do any sorting for the first entry. |
| 120 if (m_entries.size() == 1) |
| 121 return; |
| 122 |
| 123 // Sort the entries. |
| 124 std::sort(m_entries.begin(), m_entries.end(), MapEntry::compareLessThan); |
| 93 } | 125 } |
| 94 | 126 |
| 95 const MediaKeyStatusMap::MapEntry& MediaKeyStatusMap::at(size_t index) const | 127 const MediaKeyStatusMap::MapEntry& MediaKeyStatusMap::at(size_t index) const |
| 96 { | 128 { |
| 97 BLINK_ASSERT(index < m_entries.size()); | 129 BLINK_ASSERT(index < m_entries.size()); |
| 98 return *m_entries.at(index); | 130 return *m_entries.at(index); |
| 99 } | 131 } |
| 100 | 132 |
| 101 size_t MediaKeyStatusMap::indexOf(const DOMArrayPiece& key) const | 133 size_t MediaKeyStatusMap::indexOf(const DOMArrayPiece& key) const |
| 102 { | 134 { |
| 103 for (size_t index = 0; index < m_entries.size(); ++index) { | 135 for (size_t index = 0; index < m_entries.size(); ++index) { |
| 104 const auto& current = m_entries.at(index)->keyId(); | 136 const auto& current = m_entries.at(index)->keyId(); |
| 105 if (key == *current) | 137 if (key == *current) |
| 106 return index; | 138 return index; |
| 107 } | 139 } |
| 108 | 140 |
| 109 // Not found, so return an index outside the valid range. | 141 // Not found, so return an index outside the valid range. |
| 110 return m_entries.size(); | 142 return m_entries.size(); |
| 111 } | 143 } |
| 112 | 144 |
| 145 bool MediaKeyStatusMap::has(const ArrayBufferOrArrayBufferView& keyId) |
| 146 { |
| 147 size_t index = indexOf(keyId); |
| 148 return index < m_entries.size(); |
| 149 } |
| 150 |
| 151 String MediaKeyStatusMap::get(const ArrayBufferOrArrayBufferView& keyId) |
| 152 { |
| 153 size_t index = indexOf(keyId); |
| 154 if (index >= m_entries.size()) |
| 155 return String(); |
| 156 return at(index).status(); |
| 157 } |
| 158 |
| 113 PairIterable<ArrayBufferOrArrayBufferView, String>::IterationSource* MediaKeySta
tusMap::startIteration(ScriptState*, ExceptionState&) | 159 PairIterable<ArrayBufferOrArrayBufferView, String>::IterationSource* MediaKeySta
tusMap::startIteration(ScriptState*, ExceptionState&) |
| 114 { | 160 { |
| 115 return new MapIterationSource(this); | 161 return new MapIterationSource(this); |
| 116 } | 162 } |
| 117 | 163 |
| 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 | |
| 128 DEFINE_TRACE(MediaKeyStatusMap) | 164 DEFINE_TRACE(MediaKeyStatusMap) |
| 129 { | 165 { |
| 130 visitor->trace(m_entries); | 166 visitor->trace(m_entries); |
| 131 } | 167 } |
| 132 | 168 |
| 133 } // namespace blink | 169 } // namespace blink |
| OLD | NEW |