8b86eac24544501a506825e53c7305ea889eaf38
[WebKit-https.git] / Source / WebKit / WebProcess / MediaStream / UserMediaPermissionRequestManager.h
1 /*
2  * Copyright (C) 2014 Igalia S.L.
3  * Copyright (C) 2016-2018 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 #if ENABLE(MEDIA_STREAM)
23
24 #include "MediaDeviceSandboxExtensions.h"
25 #include "SandboxExtension.h"
26 #include <WebCore/ActivityStateChangeObserver.h>
27 #include <WebCore/MediaCanStartListener.h>
28 #include <WebCore/MediaConstraints.h>
29 #include <WebCore/MediaDevicesEnumerationRequest.h>
30 #include <WebCore/UserMediaClient.h>
31 #include <WebCore/UserMediaRequest.h>
32 #include <wtf/HashMap.h>
33 #include <wtf/Ref.h>
34 #include <wtf/RefPtr.h>
35
36 namespace WebKit {
37
38 class WebPage;
39
40 enum class DeviceAccessState : uint8_t { NoAccess, SessionAccess, PersistentAccess };
41
42 class UserMediaPermissionRequestManager : public CanMakeWeakPtr<UserMediaPermissionRequestManager>, private WebCore::MediaCanStartListener, private WebCore::ActivityStateChangeObserver {
43 public:
44     explicit UserMediaPermissionRequestManager(WebPage&);
45     ~UserMediaPermissionRequestManager();
46
47     void startUserMediaRequest(WebCore::UserMediaRequest&);
48     void cancelUserMediaRequest(WebCore::UserMediaRequest&);
49     void userMediaAccessWasGranted(uint64_t, WebCore::CaptureDevice&& audioDevice, WebCore::CaptureDevice&& videoDevice, String&& deviceIdentifierHashSalt);
50     void userMediaAccessWasDenied(uint64_t, WebCore::UserMediaRequest::MediaAccessDenialReason, String&&);
51
52     void enumerateMediaDevices(WebCore::MediaDevicesEnumerationRequest&);
53     void cancelMediaDevicesEnumeration(WebCore::MediaDevicesEnumerationRequest&);
54     void didCompleteMediaDeviceEnumeration(uint64_t, const Vector<WebCore::CaptureDevice>& deviceList, String&& deviceIdentifierHashSalt, bool originHasPersistentAccess);
55
56     void grantUserMediaDeviceSandboxExtensions(MediaDeviceSandboxExtensions&&);
57     void revokeUserMediaDeviceSandboxExtensions(const Vector<String>&);
58
59     WebCore::UserMediaClient::DeviceChangeObserverToken addDeviceChangeObserver(WTF::Function<void()>&&);
60     void removeDeviceChangeObserver(WebCore::UserMediaClient::DeviceChangeObserverToken);
61
62     void captureDevicesChanged(DeviceAccessState);
63
64 private:
65     void sendUserMediaRequest(WebCore::UserMediaRequest&);
66
67     // WebCore::MediaCanStartListener
68     void mediaCanStart(WebCore::Document&) final;
69
70     // WebCore::ActivityStateChangeObserver
71     void activityStateDidChange(OptionSet<WebCore::ActivityState::Flag> oldActivityState, OptionSet<WebCore::ActivityState::Flag> newActivityState) final;
72
73     void removeMediaRequestFromMaps(WebCore::UserMediaRequest&);
74
75     WebPage& m_page;
76
77     HashMap<uint64_t, RefPtr<WebCore::UserMediaRequest>> m_idToUserMediaRequestMap;
78     HashMap<RefPtr<WebCore::UserMediaRequest>, uint64_t> m_userMediaRequestToIDMap;
79     HashMap<RefPtr<WebCore::Document>, Vector<RefPtr<WebCore::UserMediaRequest>>> m_blockedUserMediaRequests;
80
81     HashMap<uint64_t, RefPtr<WebCore::MediaDevicesEnumerationRequest>> m_idToMediaDevicesEnumerationRequestMap;
82     HashMap<RefPtr<WebCore::MediaDevicesEnumerationRequest>, uint64_t> m_mediaDevicesEnumerationRequestToIDMap;
83
84     HashMap<String, RefPtr<SandboxExtension>> m_userMediaDeviceSandboxExtensions;
85
86     HashMap<WebCore::UserMediaClient::DeviceChangeObserverToken, WTF::Function<void()>> m_deviceChangeObserverMap;
87     DeviceAccessState m_accessStateWhenDevicesChanged { DeviceAccessState::NoAccess };
88     bool m_monitoringDeviceChange { false };
89     bool m_pendingDeviceChangeEvent { false };
90     bool m_monitoringActivityStateChange { false };
91 };
92
93 } // namespace WebKit
94
95 namespace WTF {
96
97 template<> struct EnumTraits<WebKit::DeviceAccessState> {
98     using values = EnumValues<
99         WebKit::DeviceAccessState,
100         WebKit::DeviceAccessState::NoAccess,
101         WebKit::DeviceAccessState::SessionAccess,
102         WebKit::DeviceAccessState::PersistentAccess
103     >;
104 };
105
106 } // namespace WTF
107
108 #endif // ENABLE(MEDIA_STREAM)