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#pragma once
21
22#include "APIObject.h"
23#include <WebCore/CaptureDevice.h>
24#include <WebCore/MediaStreamRequest.h>
25#include <wtf/Vector.h>
26#include <wtf/text/WTFString.h>
27
28namespace WebCore {
29class SecurityOrigin;
30}
31
32namespace WebKit {
33
34class UserMediaPermissionRequestManagerProxy;
35
36class UserMediaPermissionRequestProxy : public API::ObjectImpl<API::Object::Type::UserMediaPermissionRequest> {
37public:
38 static Ref<UserMediaPermissionRequestProxy> create(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)
39 {
40 return adoptRef(*new UserMediaPermissionRequestProxy(manager, userMediaID, mainFrameID, frameID, WTFMove(userMediaDocumentOrigin), WTFMove(topLevelDocumentOrigin), WTFMove(audioDevices), WTFMove(videoDevices), WTFMove(request)));
41 }
42
43 void allow(const String& audioDeviceUID, const String& videoDeviceUID);
44 void allow();
45
46 enum class UserMediaAccessDenialReason { NoConstraints, UserMediaDisabled, NoCaptureDevices, InvalidConstraint, HardwareError, PermissionDenied, OtherFailure };
47 void deny(UserMediaAccessDenialReason = UserMediaAccessDenialReason::UserMediaDisabled);
48
49 void invalidate();
50 bool isPending() const { return m_manager; }
51
52 bool requiresAudioCapture() const { return m_eligibleAudioDevices.size(); }
53 bool requiresVideoCapture() const { return !requiresDisplayCapture() && m_eligibleVideoDevices.size(); }
54 bool requiresDisplayCapture() const { return m_request.type == WebCore::MediaStreamRequest::Type::DisplayMedia && m_eligibleVideoDevices.size(); }
55
56 void setEligibleVideoDeviceUIDs(Vector<WebCore::CaptureDevice>&& devices) { m_eligibleVideoDevices = WTFMove(devices); }
57 void setEligibleAudioDeviceUIDs(Vector<WebCore::CaptureDevice>&& devices) { m_eligibleAudioDevices = WTFMove(devices); }
58
59 Vector<String> videoDeviceUIDs() const;
60 Vector<String> audioDeviceUIDs() const;
61 bool hasAudioDevice() const { return !m_eligibleAudioDevices.isEmpty(); }
62 bool hasVideoDevice() const { return !m_eligibleVideoDevices.isEmpty(); }
63
64 bool hasPersistentAccess() const { return m_hasPersistentAccess; }
65 void setHasPersistentAccess() { m_hasPersistentAccess = true; }
66
67 uint64_t userMediaID() const { return m_userMediaID; }
68 uint64_t mainFrameID() const { return m_mainFrameID; }
69 uint64_t frameID() const { return m_frameID; }
70
71 WebCore::SecurityOrigin& topLevelDocumentSecurityOrigin() { return m_topLevelDocumentSecurityOrigin.get(); }
72 WebCore::SecurityOrigin& userMediaDocumentSecurityOrigin() { return m_userMediaDocumentSecurityOrigin.get(); }
73 const WebCore::SecurityOrigin& topLevelDocumentSecurityOrigin() const { return m_topLevelDocumentSecurityOrigin.get(); }
74 const WebCore::SecurityOrigin& userMediaDocumentSecurityOrigin() const { return m_userMediaDocumentSecurityOrigin.get(); }
75
76 const WebCore::MediaStreamRequest& userRequest() const { return m_request; }
77
78 WebCore::MediaStreamRequest::Type requestType() const { return m_request.type; }
79
80 void setDeviceIdentifierHashSalt(String&& salt) { m_deviceIdentifierHashSalt = WTFMove(salt); }
81 const String& deviceIdentifierHashSalt() const { return m_deviceIdentifierHashSalt; }
82
83 WebCore::CaptureDevice audioDevice() const { return m_eligibleAudioDevices.isEmpty() ? WebCore::CaptureDevice { } : m_eligibleAudioDevices[0]; }
84 WebCore::CaptureDevice videoDevice() const { return m_eligibleVideoDevices.isEmpty() ? WebCore::CaptureDevice { } : m_eligibleVideoDevices[0]; }
85
86private:
87 UserMediaPermissionRequestProxy(UserMediaPermissionRequestManagerProxy&, 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&&);
88
89 UserMediaPermissionRequestManagerProxy* m_manager;
90 uint64_t m_userMediaID;
91 uint64_t m_mainFrameID;
92 uint64_t m_frameID;
93 Ref<WebCore::SecurityOrigin> m_userMediaDocumentSecurityOrigin;
94 Ref<WebCore::SecurityOrigin> m_topLevelDocumentSecurityOrigin;
95 Vector<WebCore::CaptureDevice> m_eligibleVideoDevices;
96 Vector<WebCore::CaptureDevice> m_eligibleAudioDevices;
97 WebCore::MediaStreamRequest m_request;
98 bool m_hasPersistentAccess { false };
99 String m_deviceIdentifierHashSalt;
100};
101
102String convertEnumerationToString(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason);
103
104} // namespace WebKit
105
106namespace WTF {
107
108template<typename Type>
109struct LogArgument;
110
111template <>
112struct LogArgument<WebKit::UserMediaPermissionRequestProxy::UserMediaAccessDenialReason> {
113 static String toString(const WebKit::UserMediaPermissionRequestProxy::UserMediaAccessDenialReason type)
114 {
115 return convertEnumerationToString(type);
116 }
117};
118
119}; // namespace WTF
120
121