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