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

> 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:
```js
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:
```js
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