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