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