1 | /* |
2 | * Copyright (C) 2016 Metrological Group B.V. |
3 | * Copyright (C) 2016 Igalia S.L. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above |
12 | * copyright notice, this list of conditions and the following |
13 | * disclaimer in the documentation and/or other materials provided |
14 | * with the distribution. |
15 | * |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | #include "config.h" |
30 | #include "MediaKeyStatusMap.h" |
31 | |
32 | #if ENABLE(ENCRYPTED_MEDIA) |
33 | |
34 | #include "JSMediaKeyStatusMap.h" |
35 | #include "MediaKeySession.h" |
36 | #include "SharedBuffer.h" |
37 | #include <wtf/Optional.h> |
38 | |
39 | namespace WebCore { |
40 | |
41 | MediaKeyStatusMap::MediaKeyStatusMap(const MediaKeySession& session) |
42 | : m_session(&session) |
43 | { |
44 | } |
45 | |
46 | MediaKeyStatusMap::~MediaKeyStatusMap() = default; |
47 | |
48 | void MediaKeyStatusMap::detachSession() |
49 | { |
50 | m_session = nullptr; |
51 | } |
52 | |
53 | unsigned long MediaKeyStatusMap::size() |
54 | { |
55 | if (!m_session) |
56 | return 0; |
57 | return m_session->statuses().size(); |
58 | } |
59 | |
60 | static bool keyIdsMatch(const SharedBuffer& a, const BufferSource& b) |
61 | { |
62 | auto length = a.size(); |
63 | if (!length || length != b.length()) |
64 | return false; |
65 | return !std::memcmp(a.data(), b.data(), length); |
66 | } |
67 | |
68 | bool MediaKeyStatusMap::has(const BufferSource& keyId) |
69 | { |
70 | if (!m_session) |
71 | return false; |
72 | |
73 | auto& statuses = m_session->statuses(); |
74 | return std::any_of(statuses.begin(), statuses.end(), |
75 | [&keyId] (auto& it) { return keyIdsMatch(it.first, keyId); }); |
76 | } |
77 | |
78 | JSC::JSValue MediaKeyStatusMap::get(JSC::ExecState& state, const BufferSource& keyId) |
79 | { |
80 | if (!m_session) |
81 | return JSC::jsUndefined(); |
82 | |
83 | auto& statuses = m_session->statuses(); |
84 | auto it = std::find_if(statuses.begin(), statuses.end(), |
85 | [&keyId] (auto& it) { return keyIdsMatch(it.first, keyId); }); |
86 | |
87 | if (it == statuses.end()) |
88 | return JSC::jsUndefined(); |
89 | return convertEnumerationToJS(state, it->second); |
90 | } |
91 | |
92 | MediaKeyStatusMap::Iterator::Iterator(MediaKeyStatusMap& map) |
93 | : m_map(map) |
94 | { |
95 | } |
96 | |
97 | Optional<WTF::KeyValuePair<BufferSource::VariantType, MediaKeyStatus>> MediaKeyStatusMap::Iterator::next() |
98 | { |
99 | if (!m_map->m_session) |
100 | return WTF::nullopt; |
101 | |
102 | auto& statuses = m_map->m_session->statuses(); |
103 | if (m_index >= statuses.size()) |
104 | return WTF::nullopt; |
105 | |
106 | auto& pair = statuses[m_index++]; |
107 | auto buffer = ArrayBuffer::create(pair.first->data(), pair.first->size()); |
108 | return WTF::KeyValuePair<BufferSource::VariantType, MediaKeyStatus> { RefPtr<ArrayBuffer>(WTFMove(buffer)), pair.second }; |
109 | } |
110 | |
111 | } // namespace WebCore |
112 | |
113 | #endif // ENABLE(ENCRYPTED_MEDIA) |
114 | |