1 | /* |
2 | * Copyright (C) 2014 Igalia S.L. |
3 | * Copyright (C) 2016-2019 Apple Inc. All rights reserved. |
4 | * |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This library is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the Free Software |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
18 | */ |
19 | |
20 | #include "config.h" |
21 | #include "UserMediaPermissionRequestProxy.h" |
22 | |
23 | #include "UserMediaPermissionRequestManagerProxy.h" |
24 | #include <WebCore/CaptureDeviceManager.h> |
25 | #include <WebCore/RealtimeMediaSourceCenter.h> |
26 | #include <WebCore/SecurityOrigin.h> |
27 | #include <WebCore/SecurityOriginData.h> |
28 | #include <wtf/text/StringHash.h> |
29 | |
30 | namespace WebKit { |
31 | using namespace WebCore; |
32 | |
33 | UserMediaPermissionRequestProxy::UserMediaPermissionRequestProxy(UserMediaPermissionRequestManagerProxy& manager, uint64_t userMediaID, uint64_t mainFrameID, uint64_t frameID, Ref<WebCore::SecurityOrigin>&& userMediaDocumentOrigin, Ref<WebCore::SecurityOrigin>&& topLevelDocumentOrigin, Vector<WebCore::CaptureDevice>&& audioDevices, Vector<WebCore::CaptureDevice>&& videoDevices, WebCore::MediaStreamRequest&& request) |
34 | : m_manager(&manager) |
35 | , m_userMediaID(userMediaID) |
36 | , m_mainFrameID(mainFrameID) |
37 | , m_frameID(frameID) |
38 | , m_userMediaDocumentSecurityOrigin(WTFMove(userMediaDocumentOrigin)) |
39 | , m_topLevelDocumentSecurityOrigin(WTFMove(topLevelDocumentOrigin)) |
40 | , m_eligibleVideoDevices(WTFMove(videoDevices)) |
41 | , m_eligibleAudioDevices(WTFMove(audioDevices)) |
42 | , m_request(WTFMove(request)) |
43 | { |
44 | } |
45 | |
46 | void UserMediaPermissionRequestProxy::allow(const String& audioDeviceUID, const String& videoDeviceUID) |
47 | { |
48 | ASSERT(m_manager); |
49 | if (!m_manager) |
50 | return; |
51 | |
52 | #if ENABLE(MEDIA_STREAM) |
53 | CaptureDevice audioDevice; |
54 | if (!audioDeviceUID.isEmpty()) { |
55 | size_t index = m_eligibleAudioDevices.findMatching([&](const auto& device) { |
56 | return device.persistentId() == audioDeviceUID; |
57 | }); |
58 | ASSERT(index != notFound); |
59 | |
60 | if (index != notFound) |
61 | audioDevice = m_eligibleAudioDevices[index]; |
62 | |
63 | ASSERT(audioDevice.enabled()); |
64 | } |
65 | |
66 | CaptureDevice videoDevice; |
67 | if (!videoDeviceUID.isEmpty()) { |
68 | size_t index = m_eligibleVideoDevices.findMatching([&](const auto& device) { |
69 | return device.persistentId() == videoDeviceUID; |
70 | }); |
71 | ASSERT(index != notFound); |
72 | |
73 | if (index != notFound) |
74 | videoDevice = m_eligibleVideoDevices[index]; |
75 | |
76 | ASSERT(videoDevice.enabled()); |
77 | } |
78 | |
79 | m_manager->userMediaAccessWasGranted(m_userMediaID, WTFMove(audioDevice), WTFMove(videoDevice)); |
80 | #else |
81 | UNUSED_PARAM(audioDeviceUID); |
82 | UNUSED_PARAM(videoDeviceUID); |
83 | #endif |
84 | |
85 | invalidate(); |
86 | } |
87 | |
88 | void UserMediaPermissionRequestProxy::allow(WebCore::CaptureDevice&& audioDevice, WebCore::CaptureDevice&& videoDevice) |
89 | { |
90 | ASSERT(m_manager); |
91 | if (!m_manager) |
92 | return; |
93 | |
94 | m_manager->userMediaAccessWasGranted(m_userMediaID, WTFMove(audioDevice), WTFMove(videoDevice)); |
95 | invalidate(); |
96 | } |
97 | |
98 | void UserMediaPermissionRequestProxy::allow() |
99 | { |
100 | ASSERT(m_manager); |
101 | if (!m_manager) |
102 | return; |
103 | |
104 | auto audioDevice = !m_eligibleAudioDevices.isEmpty() ? m_eligibleAudioDevices[0] : CaptureDevice(); |
105 | auto videoDevice = !m_eligibleVideoDevices.isEmpty() ? m_eligibleVideoDevices[0] : CaptureDevice(); |
106 | |
107 | m_manager->userMediaAccessWasGranted(m_userMediaID, WTFMove(audioDevice), WTFMove(videoDevice)); |
108 | invalidate(); |
109 | } |
110 | |
111 | void UserMediaPermissionRequestProxy::deny(UserMediaAccessDenialReason reason) |
112 | { |
113 | if (!m_manager) |
114 | return; |
115 | |
116 | m_manager->userMediaAccessWasDenied(m_userMediaID, reason); |
117 | invalidate(); |
118 | } |
119 | |
120 | void UserMediaPermissionRequestProxy::invalidate() |
121 | { |
122 | m_manager = nullptr; |
123 | } |
124 | |
125 | Vector<String> UserMediaPermissionRequestProxy::videoDeviceUIDs() const |
126 | { |
127 | return WTF::map(m_eligibleVideoDevices, [] (auto& device) { |
128 | return device.persistentId(); |
129 | }); |
130 | } |
131 | |
132 | Vector<String> UserMediaPermissionRequestProxy::audioDeviceUIDs() const |
133 | { |
134 | return WTF::map(m_eligibleAudioDevices, [] (auto& device) { |
135 | return device.persistentId(); |
136 | }); |
137 | } |
138 | |
139 | String convertEnumerationToString(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason enumerationValue) |
140 | { |
141 | static const NeverDestroyed<String> values[] = { |
142 | MAKE_STATIC_STRING_IMPL("NoConstraints" ), |
143 | MAKE_STATIC_STRING_IMPL("UserMediaDisabled" ), |
144 | MAKE_STATIC_STRING_IMPL("NoCaptureDevices" ), |
145 | MAKE_STATIC_STRING_IMPL("InvalidConstraint" ), |
146 | MAKE_STATIC_STRING_IMPL("HardwareError" ), |
147 | MAKE_STATIC_STRING_IMPL("PermissionDenied" ), |
148 | MAKE_STATIC_STRING_IMPL("OtherFailure" ), |
149 | }; |
150 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoConstraints) == 0, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoConstraints is not 0 as expected" ); |
151 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::UserMediaDisabled) == 1, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::UserMediaDisabled is not 1 as expected" ); |
152 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoCaptureDevices) == 2, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoCaptureDevices is not 2 as expected" ); |
153 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::InvalidConstraint) == 3, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::InvalidConstraint is not 3 as expected" ); |
154 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::HardwareError) == 4, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::HardwareError is not 4 as expected" ); |
155 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied) == 5, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied is not 5 as expected" ); |
156 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::OtherFailure) == 6, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::OtherFailure is not 6 as expected" ); |
157 | ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values)); |
158 | return values[static_cast<size_t>(enumerationValue)]; |
159 | } |
160 | |
161 | } // namespace WebKit |
162 | |