W3C home > Mailing lists > Public > public-webrtc-logs@w3.org > October 2022

Re: [mediacapture-main] Why does `navigator.mediaDevices.enumerateDevices()` require that `Document` must have active keyboard focus? (#905)

From: Jan-Ivar Bruaroey via GitHub <sysbot+gh@w3.org>
Date: Thu, 27 Oct 2022 16:55:52 +0000
To: public-webrtc-logs@w3.org
Message-ID: <issue_comment.created-1293812378-1666889750-sysbot+gh@w3.org>
> there is then a bad race condition bug that can happen if the user immediately navigates to another page while the promise is processing. (so the JS code check would pass, but from browser POV visibility was no longer there)

Hmm, I think there's a bug in the [enumerateDevices](https://w3c.github.io/mediacapture-main/#dom-mediadevices-enumeratedevices) algorithm here actually: it's referencing the [Document](https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window) [in parallel](https://html.spec.whatwg.org/multipage/infrastructure.html#in-parallel), which is a no-no. I think it needs to move up ahead of the in-parallel steps, which [is what Firefox does](https://searchfox.org/mozilla-central/rev/2d1097c27f31cb15f7588b043d691cda9710d14b/dom/media/MediaDevices.cpp#159,163,177-178). I'll do a PR. cc @karlt 

With that fixed, there is no race possible, because the check is done synchronously.

I think the remaining issue is resolving https://github.com/w3c/mediacapture-main/issues/752#issuecomment-1293797299.

> ... that is a bad and brittle design pattern to implement in user code, since it creeps in/duplicates "magic" logic from the spec

I can't speak for everyone, but I think if we can make this work:
if (document.visibilityState == "visible") {
  await navigator.mediaDevices.enumerateDevices();
...then I'd be inclined to say the value of adding the following new API is negative:
try {
  navigator.mediaDevices.enumerateDevices({rejectIfUnavailable: true});
} catch (e) {
I say negative, because with web compat so poor right now, I worry exposing such a method would let apps opportunistically wait for focus only on browsers that require it. — Instead, I'd prefer for other user agents to catch up here.

GitHub Notification of comment by jan-ivar
Please view or discuss this issue at https://github.com/w3c/mediacapture-main/issues/905#issuecomment-1293812378 using your GitHub account

Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config
Received on Thursday, 27 October 2022 16:55:54 UTC

This archive was generated by hypermail 2.4.0 : Saturday, 6 May 2023 21:19:59 UTC