[MediaStream] 'devicechange' event should not fire in frames that can't access captur...
[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/MediaCanStartListener.h>
27 #include <WebCore/MediaConstraints.h>
28 #include <WebCore/MediaDevicesEnumerationRequest.h>
29 #include <WebCore/UserMediaClient.h>
30 #include <WebCore/UserMediaRequest.h>
31 #include <wtf/HashMap.h>
32 #include <wtf/Ref.h>
33 #include <wtf/RefPtr.h>
34
35 namespace WebKit {
36
37 class WebPage;
38
39 class UserMediaPermissionRequestManager : public CanMakeWeakPtr<UserMediaPermissionRequestManager>, private WebCore::MediaCanStartListener {
40 public:
41     explicit UserMediaPermissionRequestManager(WebPage&);
42     ~UserMediaPermissionRequestManager();
43
44     void startUserMediaRequest(WebCore::UserMediaRequest&);
45     void cancelUserMediaRequest(WebCore::UserMediaRequest&);
46     void userMediaAccessWasGranted(uint64_t, WebCore::CaptureDevice&& audioDevice, WebCore::CaptureDevice&& videoDevice, String&& deviceIdentifierHashSalt);
47     void userMediaAccessWasDenied(uint64_t, WebCore::UserMediaRequest::MediaAccessDenialReason, String&&);
48
49     void enumerateMediaDevices(WebCore::MediaDevicesEnumerationRequest&);
50     void cancelMediaDevicesEnumeration(WebCore::MediaDevicesEnumerationRequest&);
51     void didCompleteMediaDeviceEnumeration(uint64_t, const Vector<WebCore::CaptureDevice>& deviceList, String&& deviceIdentifierHashSalt, bool originHasPersistentAccess);
52
53     void grantUserMediaDeviceSandboxExtensions(MediaDeviceSandboxExtensions&&);
54     void revokeUserMediaDeviceSandboxExtensions(const Vector<String>&);
55
56     WebCore::UserMediaClient::DeviceChangeObserverToken addDeviceChangeObserver(WTF::Function<void()>&&);
57     void removeDeviceChangeObserver(WebCore::UserMediaClient::DeviceChangeObserverToken);
58
59     void captureDevicesChanged();
60
61 private:
62     void sendUserMediaRequest(WebCore::UserMediaRequest&);
63
64     // WebCore::MediaCanStartListener
65     void mediaCanStart(WebCore::Document&) final;
66
67     void removeMediaRequestFromMaps(WebCore::UserMediaRequest&);
68
69     WebPage& m_page;
70
71     HashMap<uint64_t, RefPtr<WebCore::UserMediaRequest>> m_idToUserMediaRequestMap;
72     HashMap<RefPtr<WebCore::UserMediaRequest>, uint64_t> m_userMediaRequestToIDMap;
73     HashMap<RefPtr<WebCore::Document>, Vector<RefPtr<WebCore::UserMediaRequest>>> m_blockedUserMediaRequests;
74
75     HashMap<uint64_t, RefPtr<WebCore::MediaDevicesEnumerationRequest>> m_idToMediaDevicesEnumerationRequestMap;
76     HashMap<RefPtr<WebCore::MediaDevicesEnumerationRequest>, uint64_t> m_mediaDevicesEnumerationRequestToIDMap;
77
78     HashMap<String, RefPtr<SandboxExtension>> m_userMediaDeviceSandboxExtensions;
79
80     HashMap<WebCore::UserMediaClient::DeviceChangeObserverToken, WTF::Function<void()>> m_deviceChangeObserverMap;
81     bool m_monitoringDeviceChange { false };
82 };
83
84 } // namespace WebKit
85
86 namespace WTF {
87
88 } // namespace WTF
89
90 #endif // ENABLE(MEDIA_STREAM)