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 | #if ENABLE(MEDIA_STREAM) |
47 | static inline void setDeviceAsFirst(Vector<CaptureDevice>& devices, const String& deviceID) |
48 | { |
49 | size_t index = devices.findMatching([&deviceID](const auto& device) { |
50 | return device.persistentId() == deviceID; |
51 | }); |
52 | ASSERT(index != notFound); |
53 | |
54 | if (index) { |
55 | auto device = devices[index]; |
56 | ASSERT(device.enabled()); |
57 | |
58 | devices.remove(index); |
59 | devices.insert(0, WTFMove(device)); |
60 | } |
61 | } |
62 | #endif |
63 | |
64 | void UserMediaPermissionRequestProxy::allow(const String& audioDeviceUID, const String& videoDeviceUID) |
65 | { |
66 | #if ENABLE(MEDIA_STREAM) |
67 | if (!audioDeviceUID.isEmpty()) |
68 | setDeviceAsFirst(m_eligibleAudioDevices, audioDeviceUID); |
69 | if (!videoDeviceUID.isEmpty()) |
70 | setDeviceAsFirst(m_eligibleVideoDevices, videoDeviceUID); |
71 | #else |
72 | UNUSED_PARAM(audioDeviceUID); |
73 | UNUSED_PARAM(videoDeviceUID); |
74 | #endif |
75 | |
76 | allow(); |
77 | } |
78 | |
79 | void UserMediaPermissionRequestProxy::allow() |
80 | { |
81 | ASSERT(m_manager); |
82 | if (!m_manager) |
83 | return; |
84 | |
85 | m_manager->grantRequest(*this); |
86 | invalidate(); |
87 | } |
88 | |
89 | void UserMediaPermissionRequestProxy::deny(UserMediaAccessDenialReason reason) |
90 | { |
91 | if (!m_manager) |
92 | return; |
93 | |
94 | m_manager->denyRequest(*this, reason); |
95 | invalidate(); |
96 | } |
97 | |
98 | void UserMediaPermissionRequestProxy::invalidate() |
99 | { |
100 | m_manager = nullptr; |
101 | } |
102 | |
103 | Vector<String> UserMediaPermissionRequestProxy::videoDeviceUIDs() const |
104 | { |
105 | return WTF::map(m_eligibleVideoDevices, [] (auto& device) { |
106 | return device.persistentId(); |
107 | }); |
108 | } |
109 | |
110 | Vector<String> UserMediaPermissionRequestProxy::audioDeviceUIDs() const |
111 | { |
112 | return WTF::map(m_eligibleAudioDevices, [] (auto& device) { |
113 | return device.persistentId(); |
114 | }); |
115 | } |
116 | |
117 | String convertEnumerationToString(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason enumerationValue) |
118 | { |
119 | static const NeverDestroyed<String> values[] = { |
120 | MAKE_STATIC_STRING_IMPL("NoConstraints" ), |
121 | MAKE_STATIC_STRING_IMPL("UserMediaDisabled" ), |
122 | MAKE_STATIC_STRING_IMPL("NoCaptureDevices" ), |
123 | MAKE_STATIC_STRING_IMPL("InvalidConstraint" ), |
124 | MAKE_STATIC_STRING_IMPL("HardwareError" ), |
125 | MAKE_STATIC_STRING_IMPL("PermissionDenied" ), |
126 | MAKE_STATIC_STRING_IMPL("OtherFailure" ), |
127 | }; |
128 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoConstraints) == 0, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoConstraints is not 0 as expected" ); |
129 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::UserMediaDisabled) == 1, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::UserMediaDisabled is not 1 as expected" ); |
130 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoCaptureDevices) == 2, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoCaptureDevices is not 2 as expected" ); |
131 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::InvalidConstraint) == 3, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::InvalidConstraint is not 3 as expected" ); |
132 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::HardwareError) == 4, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::HardwareError is not 4 as expected" ); |
133 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied) == 5, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied is not 5 as expected" ); |
134 | static_assert(static_cast<size_t>(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::OtherFailure) == 6, "UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::OtherFailure is not 6 as expected" ); |
135 | ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values)); |
136 | return values[static_cast<size_t>(enumerationValue)]; |
137 | } |
138 | |
139 | } // namespace WebKit |
140 | |