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](, dwarfing `getUserMedia` at [0.6%]( 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](, [bug 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]( event that fires then. You'll find the [steps that fire the devicechange event]( 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:
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 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 using your GitHub account

Sent via github-notify-ml as configured in

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