Unreviewed, rolling out r241167.
[WebKit-https.git] / Source / WebKit / UIProcess / UserMediaPermissionRequestManagerProxy.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 #include "UserMediaPermissionCheckProxy.h"
23 #include "UserMediaPermissionRequestProxy.h"
24 #include <WebCore/MediaProducer.h>
25 #include <WebCore/SecurityOrigin.h>
26 #include <wtf/HashMap.h>
27 #include <wtf/RunLoop.h>
28 #include <wtf/Seconds.h>
29 #include <wtf/WeakPtr.h>
30
31 namespace WebCore {
32 class CaptureDevice;
33 struct MediaConstraints;
34 struct MediaStreamRequest;
35 class SecurityOrigin;
36 };
37
38 namespace WebKit {
39
40 class WebPageProxy;
41
42 class UserMediaPermissionRequestManagerProxy : public CanMakeWeakPtr<UserMediaPermissionRequestManagerProxy> {
43 public:
44     explicit UserMediaPermissionRequestManagerProxy(WebPageProxy&);
45     ~UserMediaPermissionRequestManagerProxy();
46
47     WebPageProxy& page() const { return m_page; }
48
49     void invalidatePendingRequests();
50
51     void requestUserMediaPermissionForFrame(uint64_t userMediaID, uint64_t frameID, Ref<WebCore::SecurityOrigin>&&  userMediaDocumentOrigin, Ref<WebCore::SecurityOrigin>&& topLevelDocumentOrigin, const WebCore::MediaStreamRequest&);
52
53     void resetAccess(uint64_t mainFrameID);
54     void viewIsBecomingVisible();
55
56     void userMediaAccessWasGranted(uint64_t, WebCore::CaptureDevice&& audioDevice, WebCore::CaptureDevice&& videoDevice);
57     void userMediaAccessWasDenied(uint64_t, UserMediaPermissionRequestProxy::UserMediaAccessDenialReason);
58
59     void enumerateMediaDevicesForFrame(uint64_t userMediaID, uint64_t frameID, Ref<WebCore::SecurityOrigin>&& userMediaDocumentOrigin, Ref<WebCore::SecurityOrigin>&& topLevelDocumentOrigin);
60
61     void stopCapture();
62     void scheduleNextRejection();
63     void rejectionTimerFired();
64     void clearCachedState();
65     void captureDevicesChanged();
66
67     void captureStateChanged(WebCore::MediaProducer::MediaStateFlags oldState, WebCore::MediaProducer::MediaStateFlags newState);
68     void syncWithWebCorePrefs() const;
69
70 private:
71     Ref<UserMediaPermissionRequestProxy> createPermissionRequest(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&&);
72     void denyRequest(uint64_t userMediaID, UserMediaPermissionRequestProxy::UserMediaAccessDenialReason, const String& invalidConstraint);
73 #if ENABLE(MEDIA_STREAM)
74     bool grantAccess(uint64_t userMediaID, const WebCore::CaptureDevice audioDevice, const WebCore::CaptureDevice videoDevice, const String& deviceIdentifierHashSalt);
75
76     const UserMediaPermissionRequestProxy* searchForGrantedRequest(uint64_t frameID, const WebCore::SecurityOrigin& userMediaDocumentOrigin, const WebCore::SecurityOrigin& topLevelDocumentOrigin, bool needsAudio, bool needsVideo) const;
77     bool wasRequestDenied(uint64_t mainFrameID, const WebCore::SecurityOrigin& userMediaDocumentOrigin, const WebCore::SecurityOrigin& topLevelDocumentOrigin, bool needsAudio, bool needsVideo, bool needsScreenCapture);
78
79     void getUserMediaPermissionInfo(uint64_t requestID, uint64_t frameID, UserMediaPermissionCheckProxy::CompletionHandler&&, Ref<WebCore::SecurityOrigin>&& userMediaDocumentOrigin, Ref<WebCore::SecurityOrigin>&& topLevelDocumentOrigin);
80
81     enum class RequestAction {
82         Deny,
83         Grant,
84         Prompt
85     };
86     RequestAction getRequestAction(uint64_t frameID, WebCore::SecurityOrigin& userMediaDocumentOrigin, WebCore::SecurityOrigin& topLevelDocumentOrigin, const WebCore::MediaStreamRequest&, Vector<WebCore::CaptureDevice>& audioDevices, Vector<WebCore::CaptureDevice>& videoDevices);
87
88     bool wasGrantedVideoOrAudioAccess(uint64_t, const WebCore::SecurityOrigin& userMediaDocumentOrigin, const WebCore::SecurityOrigin& topLevelDocumentOrigin);
89 #endif
90
91     void watchdogTimerFired();
92
93     HashMap<uint64_t, RefPtr<UserMediaPermissionRequestProxy>> m_pendingUserMediaRequests;
94     HashMap<uint64_t, Ref<UserMediaPermissionCheckProxy>> m_pendingDeviceRequests;
95
96     WebPageProxy& m_page;
97
98     RunLoop::Timer<UserMediaPermissionRequestManagerProxy> m_rejectionTimer;
99     Vector<uint64_t> m_pendingRejections;
100
101     Vector<Ref<UserMediaPermissionRequestProxy>> m_pregrantedRequests;
102     Vector<Ref<UserMediaPermissionRequestProxy>> m_grantedRequests;
103
104     struct DeniedRequest {
105         uint64_t mainFrameID;
106         Ref<WebCore::SecurityOrigin> userMediaDocumentOrigin;
107         Ref<WebCore::SecurityOrigin> topLevelDocumentOrigin;
108         bool isAudioDenied;
109         bool isVideoDenied;
110         bool isScreenCaptureDenied;
111     };
112     Vector<DeniedRequest> m_deniedRequests;
113
114     WebCore::MediaProducer::MediaStateFlags m_captureState { WebCore::MediaProducer::IsNotPlaying };
115     RunLoop::Timer<UserMediaPermissionRequestManagerProxy> m_watchdogTimer;
116     Seconds m_currentWatchdogInterval;
117     bool m_hasFilteredDeviceList { false };
118 };
119
120 } // namespace WebKit
121