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