7b8c6141edc93fdb55d2c8b6cec7ae8e2dd527af
[WebKit-https.git] / Source / WebKit / UIProcess / UserMediaPermissionRequestManagerProxy.cpp
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 #include "config.h"
21 #include "UserMediaPermissionRequestManagerProxy.h"
22
23 #include "APISecurityOrigin.h"
24 #include "APIUIClient.h"
25 #include "DeviceIdHashSaltStorage.h"
26 #include "UserMediaPermissionRequestManager.h"
27 #include "UserMediaProcessManager.h"
28 #include "WebAutomationSession.h"
29 #include "WebPageMessages.h"
30 #include "WebPageProxy.h"
31 #include "WebProcess.h"
32 #include "WebProcessPool.h"
33 #include "WebProcessProxy.h"
34 #include "WebsiteDataStore.h"
35 #include <WebCore/MediaConstraints.h>
36 #include <WebCore/MockRealtimeMediaSourceCenter.h>
37 #include <WebCore/RealtimeMediaSource.h>
38 #include <WebCore/SecurityOriginData.h>
39 #include <WebCore/UserMediaRequest.h>
40
41 namespace WebKit {
42 using namespace WebCore;
43
44 #if ENABLE(MEDIA_STREAM)
45 static const MediaProducer::MediaStateFlags activeCaptureMask = MediaProducer::HasActiveAudioCaptureDevice | MediaProducer::HasActiveVideoCaptureDevice;
46
47 static uint64_t generateRequestID()
48 {
49     static uint64_t uniqueRequestID = 1;
50     return uniqueRequestID++;
51 }
52 #endif
53
54 UserMediaPermissionRequestManagerProxy::UserMediaPermissionRequestManagerProxy(WebPageProxy& page)
55     : m_page(page)
56     , m_rejectionTimer(RunLoop::main(), this, &UserMediaPermissionRequestManagerProxy::rejectionTimerFired)
57     , m_watchdogTimer(RunLoop::main(), this, &UserMediaPermissionRequestManagerProxy::watchdogTimerFired)
58 {
59 #if ENABLE(MEDIA_STREAM)
60     UserMediaProcessManager::singleton().addUserMediaPermissionRequestManagerProxy(*this);
61 #endif
62 }
63
64 UserMediaPermissionRequestManagerProxy::~UserMediaPermissionRequestManagerProxy()
65 {
66 #if ENABLE(MEDIA_STREAM)
67     UserMediaProcessManager::singleton().removeUserMediaPermissionRequestManagerProxy(*this);
68 #endif
69     invalidatePendingRequests();
70 }
71
72 void UserMediaPermissionRequestManagerProxy::invalidatePendingRequests()
73 {
74     for (auto& request : m_pendingUserMediaRequests.values())
75         request->invalidate();
76     m_pendingUserMediaRequests.clear();
77
78     m_pendingDeviceRequests.clear();
79 }
80
81 void UserMediaPermissionRequestManagerProxy::stopCapture()
82 {
83     invalidatePendingRequests();
84     m_page.stopMediaCapture();
85 }
86
87 void UserMediaPermissionRequestManagerProxy::captureDevicesChanged()
88 {
89 #if ENABLE(MEDIA_STREAM)
90     if (!m_page.isValid() || !m_page.mainFrame())
91         return;
92
93     auto handler = [this](Optional<bool> originHasPersistentAccess) mutable {
94         if (!originHasPersistentAccess || !m_page.isValid())
95             return;
96
97         if (m_grantedRequests.isEmpty() && !*originHasPersistentAccess)
98             return;
99
100         m_page.process().send(Messages::WebPage::CaptureDevicesChanged(), m_page.pageID());
101     };
102
103     auto origin = WebCore::SecurityOrigin::create(m_page.mainFrame()->url());
104     getUserMediaPermissionInfo(m_page.mainFrame()->frameID(), origin.get(), WTFMove(origin), WTFMove(handler));
105 #endif
106 }
107
108 void UserMediaPermissionRequestManagerProxy::clearCachedState()
109 {
110     invalidatePendingRequests();
111 }
112
113 #if ENABLE(MEDIA_STREAM)
114 static uint64_t toWebCore(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason reason)
115 {
116     switch (reason) {
117     case UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoConstraints:
118         return static_cast<uint64_t>(UserMediaRequest::MediaAccessDenialReason::NoConstraints);
119     case UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::UserMediaDisabled:
120         return static_cast<uint64_t>(UserMediaRequest::MediaAccessDenialReason::UserMediaDisabled);
121     case UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoCaptureDevices:
122         return static_cast<uint64_t>(UserMediaRequest::MediaAccessDenialReason::NoCaptureDevices);
123     case UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::InvalidConstraint:
124         return static_cast<uint64_t>(UserMediaRequest::MediaAccessDenialReason::InvalidConstraint);
125     case UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::HardwareError:
126         return static_cast<uint64_t>(UserMediaRequest::MediaAccessDenialReason::HardwareError);
127     case UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied:
128         return static_cast<uint64_t>(UserMediaRequest::MediaAccessDenialReason::PermissionDenied);
129     case UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::OtherFailure:
130         return static_cast<uint64_t>(UserMediaRequest::MediaAccessDenialReason::OtherFailure);
131     }
132
133     ASSERT_NOT_REACHED();
134     return static_cast<uint64_t>(UserMediaRequest::MediaAccessDenialReason::OtherFailure);
135 }
136 #endif
137
138 void UserMediaPermissionRequestManagerProxy::userMediaAccessWasDenied(uint64_t userMediaID, UserMediaPermissionRequestProxy::UserMediaAccessDenialReason reason)
139 {
140     if (!m_page.isValid())
141         return;
142
143     auto request = m_pendingUserMediaRequests.take(userMediaID);
144     if (!request)
145         return;
146
147     if (reason == UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied)
148         m_deniedRequests.append(DeniedRequest { request->mainFrameID(), request->userMediaDocumentSecurityOrigin(), request->topLevelDocumentSecurityOrigin(), request->requiresAudioCapture(), request->requiresVideoCapture(), request->requiresDisplayCapture() });
149
150     denyRequest(userMediaID, reason, emptyString());
151 }
152
153 void UserMediaPermissionRequestManagerProxy::denyRequest(uint64_t userMediaID, UserMediaPermissionRequestProxy::UserMediaAccessDenialReason reason, const String& invalidConstraint)
154 {
155     ASSERT(m_page.isValid());
156
157 #if ENABLE(MEDIA_STREAM)
158     m_page.process().send(Messages::WebPage::UserMediaAccessWasDenied(userMediaID, toWebCore(reason), invalidConstraint), m_page.pageID());
159 #else
160     UNUSED_PARAM(reason);
161     UNUSED_PARAM(invalidConstraint);
162 #endif
163 }
164
165 void UserMediaPermissionRequestManagerProxy::userMediaAccessWasGranted(uint64_t userMediaID, CaptureDevice&& audioDevice, CaptureDevice&& videoDevice)
166 {
167     ASSERT(audioDevice || videoDevice);
168
169     if (!m_page.isValid())
170         return;
171
172 #if ENABLE(MEDIA_STREAM)
173     auto request = m_pendingUserMediaRequests.take(userMediaID);
174     if (!request)
175         return;
176
177     auto& userMediaDocumentSecurityOrigin = request->userMediaDocumentSecurityOrigin();
178     auto& topLevelDocumentSecurityOrigin = request->topLevelDocumentSecurityOrigin();
179     m_page.websiteDataStore().deviceIdHashSaltStorage().deviceIdHashSaltForOrigin(userMediaDocumentSecurityOrigin, topLevelDocumentSecurityOrigin, [this, weakThis = makeWeakPtr(*this), request = request.releaseNonNull()] (String&& deviceIDHashSalt) mutable {
180         if (!weakThis)
181             return;
182         if (!grantAccess(request))
183             return;
184
185         m_grantedRequests.append(WTFMove(request));
186         if (m_hasFilteredDeviceList)
187             captureDevicesChanged();
188         m_hasFilteredDeviceList = false;
189     });
190 #else
191     UNUSED_PARAM(userMediaID);
192     UNUSED_PARAM(audioDevice);
193     UNUSED_PARAM(videoDevice);
194 #endif
195 }
196
197 #if ENABLE(MEDIA_STREAM)
198 void UserMediaPermissionRequestManagerProxy::resetAccess(uint64_t frameID)
199 {
200     m_grantedRequests.removeAllMatching([frameID](const auto& grantedRequest) {
201         return grantedRequest->mainFrameID() == frameID;
202     });
203     m_pregrantedRequests.clear();
204     m_deniedRequests.clear();
205     m_hasFilteredDeviceList = false;
206 }
207
208 const UserMediaPermissionRequestProxy* UserMediaPermissionRequestManagerProxy::searchForGrantedRequest(uint64_t frameID, const SecurityOrigin& userMediaDocumentOrigin, const SecurityOrigin& topLevelDocumentOrigin, bool needsAudio, bool needsVideo) const
209 {
210     if (m_page.isMediaStreamCaptureMuted())
211         return nullptr;
212
213     bool checkForAudio = needsAudio;
214     bool checkForVideo = needsVideo;
215     for (const auto& grantedRequest : m_grantedRequests) {
216         if (grantedRequest->requiresDisplayCapture())
217             continue;
218         if (!grantedRequest->userMediaDocumentSecurityOrigin().isSameSchemeHostPort(userMediaDocumentOrigin))
219             continue;
220         if (!grantedRequest->topLevelDocumentSecurityOrigin().isSameSchemeHostPort(topLevelDocumentOrigin))
221             continue;
222         if (grantedRequest->frameID() != frameID)
223             continue;
224
225         if (grantedRequest->requiresVideoCapture())
226             checkForVideo = false;
227
228         if (grantedRequest->requiresAudioCapture())
229             checkForAudio = false;
230
231         if (checkForVideo || checkForAudio)
232             continue;
233
234         return grantedRequest.ptr();
235     }
236     return nullptr;
237 }
238
239 bool UserMediaPermissionRequestManagerProxy::wasRequestDenied(uint64_t mainFrameID, const SecurityOrigin& userMediaDocumentOrigin, const SecurityOrigin& topLevelDocumentOrigin, bool needsAudio, bool needsVideo, bool needsScreenCapture)
240 {
241     for (const auto& deniedRequest : m_deniedRequests) {
242         if (!deniedRequest.userMediaDocumentOrigin->isSameSchemeHostPort(userMediaDocumentOrigin))
243             continue;
244         if (!deniedRequest.topLevelDocumentOrigin->isSameSchemeHostPort(topLevelDocumentOrigin))
245             continue;
246         if (deniedRequest.mainFrameID != mainFrameID)
247             continue;
248         if (deniedRequest.isAudioDenied && needsAudio)
249             return true;
250         if (deniedRequest.isVideoDenied && needsVideo)
251             return true;
252         if (deniedRequest.isScreenCaptureDenied && needsScreenCapture)
253             return true;
254     }
255     return false;
256 }
257
258 bool UserMediaPermissionRequestManagerProxy::grantAccess(const UserMediaPermissionRequestProxy& request)
259 {
260     if (!UserMediaProcessManager::singleton().willCreateMediaStream(*this, request.hasAudioDevice(), request.hasVideoDevice())) {
261         denyRequest(request.userMediaID(), UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::OtherFailure, "Unable to extend sandbox.");
262         return false;
263     }
264
265     m_page.process().send(Messages::WebPage::UserMediaAccessWasGranted(request.userMediaID(), request.audioDevice(), request.videoDevice(), request.deviceIdentifierHashSalt()), m_page.pageID());
266     return true;
267 }
268 #endif
269
270 void UserMediaPermissionRequestManagerProxy::rejectionTimerFired()
271 {
272     uint64_t userMediaID = m_pendingRejections[0];
273     m_pendingRejections.remove(0);
274
275     denyRequest(userMediaID, UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied, emptyString());
276     if (!m_pendingRejections.isEmpty())
277         scheduleNextRejection();
278 }
279
280 void UserMediaPermissionRequestManagerProxy::scheduleNextRejection()
281 {
282     const double mimimumDelayBeforeReplying = .25;
283     if (!m_rejectionTimer.isActive())
284         m_rejectionTimer.startOneShot(Seconds(mimimumDelayBeforeReplying + randomNumber()));
285 }
286
287 #if ENABLE(MEDIA_STREAM)
288 UserMediaPermissionRequestManagerProxy::RequestAction UserMediaPermissionRequestManagerProxy::getRequestAction(const UserMediaPermissionRequestProxy& request)
289 {
290     bool requestingScreenCapture = request.requestType() == MediaStreamRequest::Type::DisplayMedia;
291     bool requestingCamera = !requestingScreenCapture && request.hasVideoDevice();
292     bool requestingMicrophone = request.hasAudioDevice();
293
294     ASSERT(!(requestingScreenCapture && !request.hasVideoDevice()));
295     ASSERT(!(requestingScreenCapture && requestingMicrophone));
296
297     if (wasRequestDenied(request.frameID(), request.userMediaDocumentSecurityOrigin(), request.topLevelDocumentSecurityOrigin(), requestingMicrophone, requestingCamera, requestingScreenCapture))
298         return RequestAction::Deny;
299
300     if (request.requestType() == MediaStreamRequest::Type::DisplayMedia)
301         return RequestAction::Prompt;
302
303     return searchForGrantedRequest(request.frameID(), request.userMediaDocumentSecurityOrigin(), request.topLevelDocumentSecurityOrigin(), requestingMicrophone, requestingCamera) ? RequestAction::Grant : RequestAction::Prompt;
304 }
305 #endif
306
307 void UserMediaPermissionRequestManagerProxy::requestUserMediaPermissionForFrame(uint64_t userMediaID, uint64_t frameID, Ref<SecurityOrigin>&& userMediaDocumentOrigin, Ref<SecurityOrigin>&& topLevelDocumentOrigin, MediaStreamRequest&& userRequest)
308 {
309 #if ENABLE(MEDIA_STREAM)
310     if (!UserMediaProcessManager::singleton().captureEnabled()) {
311         m_pendingRejections.append(userMediaID);
312         scheduleNextRejection();
313         return;
314     }
315
316     if (!m_page.isValid())
317         return;
318
319     auto userMediaOrigin = API::SecurityOrigin::create(userMediaDocumentOrigin.get());
320     auto topLevelOrigin = API::SecurityOrigin::create(topLevelDocumentOrigin.get());
321
322     auto request = m_pendingUserMediaRequests.add(userMediaID, UserMediaPermissionRequestProxy::create(*this, userMediaID, m_page.mainFrame()->frameID(), frameID, WTFMove(userMediaDocumentOrigin), WTFMove(topLevelDocumentOrigin), { }, { }, WTFMove(userRequest))).iterator->value.copyRef();
323
324     getUserMediaPermissionInfo(frameID, request->userMediaDocumentSecurityOrigin(), request->topLevelDocumentSecurityOrigin(), [this, request = request.releaseNonNull()](Optional<bool> hasPersistentAccess) mutable {
325         if (!request->isPending())
326             return;
327
328         if (!hasPersistentAccess) {
329             request->deny(UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::OtherFailure);
330             return;
331         }
332
333         processUserMediaPermissionRequest(WTFMove(request), *hasPersistentAccess);
334     });
335 }
336
337 void UserMediaPermissionRequestManagerProxy::processUserMediaPermissionRequest(Ref<UserMediaPermissionRequestProxy>&& request, bool hasPersistentAccess)
338 {
339     if (hasPersistentAccess)
340         request->setHasPersistentAccess();
341
342     auto& userMediaDocumentSecurityOrigin = request->userMediaDocumentSecurityOrigin();
343     auto& topLevelDocumentSecurityOrigin = request->topLevelDocumentSecurityOrigin();
344     m_page.websiteDataStore().deviceIdHashSaltStorage().deviceIdHashSaltForOrigin(userMediaDocumentSecurityOrigin, topLevelDocumentSecurityOrigin, [this, request = WTFMove(request)] (String&& deviceIDHashSalt) mutable {
345         if (!request->isPending())
346             return;
347
348         RealtimeMediaSourceCenter::InvalidConstraintsHandler invalidHandler = [this, request = request.copyRef()](const String& invalidConstraint) {
349             if (!request->isPending())
350                 return;
351
352             if (!m_page.isValid())
353                 return;
354
355             processUserMediaPermissionInvalidRequest(request.get(), invalidConstraint);
356         };
357
358         auto validHandler = [this, request = request.copyRef()](Vector<CaptureDevice>&& audioDevices, Vector<CaptureDevice>&& videoDevices, String&& deviceIdentifierHashSalt) mutable {
359             if (!request->isPending())
360                 return;
361
362             if (!m_page.isValid() || !m_page.mainFrame())
363                 return;
364
365             processUserMediaPermissionValidRequest(WTFMove(request), WTFMove(audioDevices), WTFMove(videoDevices), WTFMove(deviceIdentifierHashSalt));
366         };
367
368         syncWithWebCorePrefs();
369
370         RealtimeMediaSourceCenter::singleton().validateRequestConstraints(WTFMove(validHandler), WTFMove(invalidHandler), request->userRequest(), WTFMove(deviceIDHashSalt));
371     });
372 #else
373     UNUSED_PARAM(userMediaID);
374     UNUSED_PARAM(frameID);
375     UNUSED_PARAM(userMediaDocumentOrigin);
376     UNUSED_PARAM(topLevelDocumentOrigin);
377     UNUSED_PARAM(userRequest);
378 #endif
379 }
380
381 #if ENABLE(MEDIA_STREAM)
382 void UserMediaPermissionRequestManagerProxy::processUserMediaPermissionInvalidRequest(const UserMediaPermissionRequestProxy& request, const String& invalidConstraint)
383 {
384     bool filterConstraint = !request.hasPersistentAccess() && !wasGrantedVideoOrAudioAccess(request.frameID(), request.userMediaDocumentSecurityOrigin(), request.topLevelDocumentSecurityOrigin());
385
386     denyRequest(request.userMediaID(), UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::InvalidConstraint, filterConstraint ? String { } : invalidConstraint);
387 }
388
389 void UserMediaPermissionRequestManagerProxy::processUserMediaPermissionValidRequest(Ref<UserMediaPermissionRequestProxy>&& request, Vector<CaptureDevice>&& audioDevices, Vector<CaptureDevice>&& videoDevices, String&& deviceIdentifierHashSalt)
390 {
391     if (videoDevices.isEmpty() && audioDevices.isEmpty()) {
392         denyRequest(request->userMediaID(), UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoConstraints, emptyString());
393         return;
394     }
395
396     request->setDeviceIdentifierHashSalt(WTFMove(deviceIdentifierHashSalt));
397     request->setEligibleVideoDeviceUIDs(WTFMove(videoDevices));
398     request->setEligibleAudioDeviceUIDs(WTFMove(audioDevices));
399
400     auto action = getRequestAction(request);
401     if (action == RequestAction::Deny) {
402         denyRequest(request->userMediaID(), UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied, emptyString());
403         return;
404     }
405
406     if (action == RequestAction::Grant) {
407         ASSERT(request->requestType() != MediaStreamRequest::Type::DisplayMedia);
408
409         if (m_page.isViewVisible())
410             grantAccess(request);
411         else
412             m_pregrantedRequests.append(WTFMove(request));
413
414         return;
415     }
416
417     if (m_page.isControlledByAutomation()) {
418         if (WebAutomationSession* automationSession = m_page.process().processPool().automationSession()) {
419             if (automationSession->shouldAllowGetUserMediaForPage(m_page))
420                 request->allow();
421             else
422                 userMediaAccessWasDenied(request->userMediaID(), UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::PermissionDenied);
423
424             return;
425         }
426     }
427
428     if (m_page.preferences().mockCaptureDevicesEnabled() && !m_page.preferences().mockCaptureDevicesPromptEnabled()) {
429         request->allow();
430         return;
431     }
432
433     // If page navigated, there is no need to call the page client for authorization.
434     auto* webFrame = m_page.process().webFrame(request->frameID());
435
436     if (!webFrame || !SecurityOrigin::createFromString(m_page.pageLoadState().activeURL())->isSameSchemeHostPort(request->topLevelDocumentSecurityOrigin())) {
437         denyRequest(request->userMediaID(), UserMediaPermissionRequestProxy::UserMediaAccessDenialReason::NoConstraints, emptyString());
438         return;
439     }
440
441     // FIXME: Remove webFrame, userMediaOrigin and topLevelOrigin from this uiClient API call.
442     auto userMediaOrigin = API::SecurityOrigin::create(request->userMediaDocumentSecurityOrigin());
443     auto topLevelOrigin = API::SecurityOrigin::create(request->topLevelDocumentSecurityOrigin());
444     m_page.uiClient().decidePolicyForUserMediaPermissionRequest(m_page, *webFrame, WTFMove(userMediaOrigin), WTFMove(topLevelOrigin), request);
445 }
446
447 void UserMediaPermissionRequestManagerProxy::getUserMediaPermissionInfo(uint64_t frameID, Ref<SecurityOrigin>&& userMediaDocumentOrigin, Ref<SecurityOrigin>&& topLevelDocumentOrigin, CompletionHandler<void(Optional<bool>)>&& handler)
448 {
449     auto* webFrame = m_page.process().webFrame(frameID);
450     if (!webFrame || !SecurityOrigin::createFromString(m_page.pageLoadState().activeURL())->isSameSchemeHostPort(topLevelDocumentOrigin.get())) {
451         handler({ });
452         return;
453     }
454
455     auto userMediaOrigin = API::SecurityOrigin::create(userMediaDocumentOrigin.get());
456     auto topLevelOrigin = API::SecurityOrigin::create(topLevelDocumentOrigin.get());
457
458     auto requestID = generateRequestID();
459     m_pendingDeviceRequests.add(requestID);
460
461     auto request = UserMediaPermissionCheckProxy::create(frameID, [this, weakThis = makeWeakPtr(*this), requestID, handler = WTFMove(handler)](Optional<bool> allowed) mutable {
462         if (!weakThis || !m_pendingDeviceRequests.remove(requestID) || !allowed) {
463             handler({ });
464             return;
465         }
466         handler(*allowed);
467     }, WTFMove(userMediaDocumentOrigin), WTFMove(topLevelDocumentOrigin));
468
469     // FIXME: Remove webFrame, userMediaOrigin and topLevelOrigin from this uiClient API call.
470     m_page.uiClient().checkUserMediaPermissionForOrigin(m_page, *webFrame, userMediaOrigin.get(), topLevelOrigin.get(), request.get());
471 }
472
473 bool UserMediaPermissionRequestManagerProxy::wasGrantedVideoOrAudioAccess(uint64_t frameID, const SecurityOrigin& userMediaDocumentOrigin, const SecurityOrigin& topLevelDocumentOrigin)
474 {
475     for (const auto& grantedRequest : m_grantedRequests) {
476         if (grantedRequest->requiresDisplayCapture())
477             continue;
478         if (!grantedRequest->userMediaDocumentSecurityOrigin().isSameSchemeHostPort(userMediaDocumentOrigin))
479             continue;
480         if (!grantedRequest->topLevelDocumentSecurityOrigin().isSameSchemeHostPort(topLevelDocumentOrigin))
481             continue;
482         if (grantedRequest->frameID() != frameID)
483             continue;
484
485         if (grantedRequest->requiresVideoCapture() || grantedRequest->requiresAudioCapture())
486             return true;
487     }
488
489     return false;
490 }
491
492 Vector<CaptureDevice> UserMediaPermissionRequestManagerProxy::computeFilteredDeviceList(bool revealIdsAndLabels, const String& deviceIDHashSalt)
493 {
494 #if PLATFORM(IOS_FAMILY)
495     static const int defaultMaximumCameraCount = 2;
496 #else
497     static const int defaultMaximumCameraCount = 1;
498 #endif
499     static const int defaultMaximumMicrophoneCount = 1;
500
501     auto devices = RealtimeMediaSourceCenter::singleton().getMediaStreamDevices();
502     int cameraCount = 0;
503     int microphoneCount = 0;
504
505     Vector<CaptureDevice> filteredDevices;
506     for (const auto& device : devices) {
507         if (!device.enabled() || (device.type() != WebCore::CaptureDevice::DeviceType::Camera && device.type() != WebCore::CaptureDevice::DeviceType::Microphone))
508             continue;
509
510         if (!revealIdsAndLabels) {
511             if (device.type() == WebCore::CaptureDevice::DeviceType::Camera && ++cameraCount > defaultMaximumCameraCount)
512                 continue;
513             if (device.type() == WebCore::CaptureDevice::DeviceType::Microphone && ++microphoneCount > defaultMaximumMicrophoneCount)
514                 continue;
515         }
516
517         auto label = emptyString();
518         auto id = emptyString();
519         auto groupId = emptyString();
520         if (revealIdsAndLabels) {
521             label = device.label();
522             id = RealtimeMediaSourceCenter::singleton().hashStringWithSalt(device.persistentId(), deviceIDHashSalt);
523             groupId = RealtimeMediaSourceCenter::singleton().hashStringWithSalt(device.groupId(), deviceIDHashSalt);
524         }
525
526         filteredDevices.append(CaptureDevice(id, device.type(), label, groupId));
527     }
528
529     m_hasFilteredDeviceList = !revealIdsAndLabels;
530     return filteredDevices;
531 }
532 #endif
533
534 void UserMediaPermissionRequestManagerProxy::enumerateMediaDevicesForFrame(uint64_t userMediaID, uint64_t frameID, Ref<SecurityOrigin>&& userMediaDocumentOrigin, Ref<SecurityOrigin>&& topLevelDocumentOrigin)
535 {
536 #if ENABLE(MEDIA_STREAM)
537     auto completionHandler = [this, userMediaID, frameID, userMediaDocumentOrigin = userMediaDocumentOrigin.copyRef(), topLevelDocumentOrigin = topLevelDocumentOrigin.copyRef()](Optional<bool> originHasPersistentAccess) mutable {
538         if (!originHasPersistentAccess)
539             return;
540
541         if (!m_page.isValid())
542             return;
543
544         auto requestID = generateRequestID();
545         m_pendingDeviceRequests.add(requestID);
546
547         auto& requestOrigin = userMediaDocumentOrigin.get();
548         auto& topOrigin = topLevelDocumentOrigin.get();
549         m_page.websiteDataStore().deviceIdHashSaltStorage().deviceIdHashSaltForOrigin(requestOrigin, topOrigin, [this, weakThis = makeWeakPtr(*this), requestID, frameID, userMediaID, userMediaDocumentOrigin = WTFMove(userMediaDocumentOrigin), topLevelDocumentOrigin = WTFMove(topLevelDocumentOrigin), originHasPersistentAccess = *originHasPersistentAccess] (String&& deviceIDHashSalt) {
550             if (!weakThis || !m_pendingDeviceRequests.remove(requestID))
551                 return;
552
553             if (!m_page.isValid())
554                 return;
555
556             syncWithWebCorePrefs();
557
558             bool revealIdsAndLabels = originHasPersistentAccess || wasGrantedVideoOrAudioAccess(frameID, userMediaDocumentOrigin.get(), topLevelDocumentOrigin.get());
559
560             m_page.process().send(Messages::WebPage::DidCompleteMediaDeviceEnumeration { userMediaID, computeFilteredDeviceList(revealIdsAndLabels, deviceIDHashSalt), deviceIDHashSalt, originHasPersistentAccess }, m_page.pageID());
561         });
562     };
563
564     getUserMediaPermissionInfo(frameID, WTFMove(userMediaDocumentOrigin), WTFMove(topLevelDocumentOrigin), WTFMove(completionHandler));
565 #else
566     UNUSED_PARAM(userMediaID);
567     UNUSED_PARAM(frameID);
568     UNUSED_PARAM(userMediaDocumentOrigin);
569     UNUSED_PARAM(topLevelDocumentOrigin);
570 #endif
571 }
572
573 void UserMediaPermissionRequestManagerProxy::syncWithWebCorePrefs() const
574 {
575 #if ENABLE(MEDIA_STREAM)
576     // Enable/disable the mock capture devices for the UI process as per the WebCore preferences. Note that
577     // this is a noop if the preference hasn't changed since the last time this was called.
578     bool mockDevicesEnabled = m_page.preferences().mockCaptureDevicesEnabled();
579     MockRealtimeMediaSourceCenter::setMockRealtimeMediaSourceCenterEnabled(mockDevicesEnabled);
580 #endif
581 }
582
583 void UserMediaPermissionRequestManagerProxy::captureStateChanged(MediaProducer::MediaStateFlags oldState, MediaProducer::MediaStateFlags newState)
584 {
585     if (!m_page.isValid())
586         return;
587
588 #if ENABLE(MEDIA_STREAM)
589     bool wasCapturingAudio = oldState & MediaProducer::AudioCaptureMask;
590     bool wasCapturingVideo = oldState & MediaProducer::VideoCaptureMask;
591     bool isCapturingAudio = newState & MediaProducer::AudioCaptureMask;
592     bool isCapturingVideo = newState & MediaProducer::VideoCaptureMask;
593
594     if ((wasCapturingAudio && !isCapturingAudio) || (wasCapturingVideo && !isCapturingVideo))
595         UserMediaProcessManager::singleton().endedCaptureSession(*this);
596     if ((!wasCapturingAudio && isCapturingAudio) || (!wasCapturingVideo && isCapturingVideo))
597         UserMediaProcessManager::singleton().startedCaptureSession(*this);
598
599     if (m_captureState == (newState & activeCaptureMask))
600         return;
601
602     m_captureState = newState & activeCaptureMask;
603
604     Seconds interval;
605     if (m_captureState & activeCaptureMask)
606         interval = Seconds::fromHours(m_page.preferences().longRunningMediaCaptureStreamRepromptIntervalInHours());
607     else
608         interval = Seconds::fromMinutes(m_page.preferences().inactiveMediaCaptureSteamRepromptIntervalInMinutes());
609
610     if (interval == m_currentWatchdogInterval)
611         return;
612
613     m_currentWatchdogInterval = interval;
614     m_watchdogTimer.startOneShot(m_currentWatchdogInterval);
615 #endif
616 }
617
618 void UserMediaPermissionRequestManagerProxy::viewIsBecomingVisible()
619 {
620     for (auto& request : m_pregrantedRequests)
621         request->allow();
622     m_pregrantedRequests.clear();
623 }
624
625 void UserMediaPermissionRequestManagerProxy::watchdogTimerFired()
626 {
627     m_grantedRequests.clear();
628     m_pregrantedRequests.clear();
629     m_currentWatchdogInterval = 0_s;
630     m_hasFilteredDeviceList = false;
631 }
632
633 } // namespace WebKit