WICG / webusb

Connecting hardware to the web.
https://wicg.github.io/webusb/
Other
1.32k stars 132 forks source link

Exclusive filter in `requestDevice` #232

Closed yume-chan closed 1 year ago

yume-chan commented 1 year ago

When a Web app already displays the list of paired devices (from getDevices), it might be a better UX to exclude those devices in the permission prompt of requestDevice.

If the app only has one page, that includes all operations to all connected devices, selecting an already paired device in the permission prompt might be a no-op (or the developer has to implement some prompts to tell the user that the selected device is already on the page). If the user needs to pair with many devices (for example in QA), this can also save their time finding the next unpaired device in the permission prompt.

The API design might be a simple boolean option excludePaired, or another array of USBDeviceFilter named exclude to allow developers to hide certain devices matching the filters (for example using all serialNumbers from already paired devices).

The second option might also become useful to only exclude a specific old incompatible model (include all devices with vendorId 0xABCD, except productId 0x1234), instead of listing all supported productIds in filters.

beaufortfrancois commented 1 year ago

This request seems similar to what we have done for Web Bluetooth and WebHID APIs previously. See https://chromestatus.com/features#exclusionFilters

Would something similar help?

yume-chan commented 1 year ago

Looking at the WebHID spec:

dictionary HIDDeviceRequestOptions {
    required sequence<HIDDeviceFilter> filters;
    sequence<HIDDeviceFilter> exclusionFilters;
};

it's exactly what I need.

reillyeon commented 1 year ago

I think this is a good idea but I wouldn't try to use it as a workaround for not having the extension to getDevices() suggested in #166.

yume-chan commented 1 year ago

use it as a workaround for not having the extension to getDevices()

I don't think it's a workaround for #166.

This one is about excluding devices from requestDevice, #166 is about including disconnected devices in getDevices.

I don't need to know paired but disconnected device list because they won't appear in requestDevice's permission prompt anyway. The permission prompt only includes currently connected devices, so I only need to filter out paired and currently connected devices, which is what getDevices returns today.

EDIT: honestly, it's a surprise for me that while WebUSB, WebHID and Web Bluetooth specs share a very similar basic device management API, they didn't evolve together. exclusionFilters was added to the other two but not WebUSB; getDevices in WebHID also doesn't return paired but disconnected devices, so it can also be benefited by changes proposed in #166; getDevices in Web Bluetooth already returns disconnected devices (maybe because Bluetooth is wireless, so it's more common to have a device in range, but not connected), but that didn't inspire the other two specs.

beaufortfrancois commented 1 year ago

FYI I've just started https://github.com/WICG/webusb/pull/233 to address this issue.