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

Short answer: trackers. `enumerateDevices` is still called by [8% of pages](https://chromestatus.com/metrics/feature/timeline/popularity/1119), dwarfing `getUserMedia` at [0.6%](https://chromestatus.com/metrics/feature/timeline/popularity/1402). It's why the spec has significantly reduced fingerprinting down to 2 bits ahead actual camera/mic use, but not all browsers have caught up yet ([crbug 1101860](https://crbug.com/1101860), [bug 1528042](https://bugzil.la/1528042)). Once they do, it's still 2 bits, so my bet is tracking libraries will continue to call it.

Additionally, users unplugging or inserting a USB device may be time-correlated to uniquely identify them across origins, even if browsers time-fuzz (they don't) the [devicechange](https://w3c.github.io/mediacapture-main/#event-mediadevices-devicechange) event that fires then. You'll find the [steps that fire the devicechange event](https://w3c.github.io/mediacapture-main/#dfn-device-change-notification-steps) contains similar "focus" language for that reason. Since `enumerateDevices` can be called in a loop until its result differs to emulate the devicechange event, it makes sense for it to have the same restriction.

> navigator.mediaDevices.enumerateDevices({rejectIfUnavailable: true});

It might be simpler to do this:
```js
if (document.visibilityState == "visible") {
  await navigator.mediaDevices.enumerateDevices();
}
```
Longer answer: one might think `document.hasFocus()` should be used, but that would require focus of iframes. See https://github.com/w3c/mediacapture-main/issues/752 for the complicated reasons.

> That way JS pages would not be left hanging

Promises have replaced callbacks, but they're still just a mechanism to trigger callbacks. As such they don't interfere with garbage collection, and no resources are "left hanging" in the traditional sense just because callbacks aren't called. `async`/`await` is syntactic sugar (sweet sugar but sugar nonetheless). I hope this answers your questions.

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


-- 
Sent via github-notify-ml as configured in https://github.com/w3c/github-notify-ml-config

Received on Monday, 10 October 2022 22:52:09 UTC