Skip to content

Commit

Permalink
WebUSB: Add exclusionFilters to USBRequestDeviceOptions
Browse files Browse the repository at this point in the history
This CL adds a new "exclusionFilters" option in
navigator.usb.requestDevice() options so that web developers can
exclude from the browser picker some devices that are known to not work
as expected.

Intent to Ship: https://groups.google.com/a/chromium.org/g/blink-dev/c/e1mgO-m8zvQ
Spec:
- WICG/webusb#233
- WICG/webusb#235
Demo: https://usb-exclusion-filters.glitch.me/

Bug: 1455392
Change-Id: I1b61d9daae3e14accedb24e493ae656316427893
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4614682
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Reilly Grant <[email protected]>
Commit-Queue: Fr <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1161953}
  • Loading branch information
beaufortfrancois authored and chromium-wpt-export-bot committed Jun 23, 2023
1 parent fe9361b commit 21f1af9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
9 changes: 5 additions & 4 deletions resources/chromium/webusb-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,11 @@ class FakeWebUsbService {
}
}

getPermission(deviceFilters) {
getPermission(options) {
return new Promise(resolve => {
if (navigator.usb.test.onrequestdevice) {
navigator.usb.test.onrequestdevice(
new USBDeviceRequestEvent(deviceFilters, resolve));
new USBDeviceRequestEvent(options, resolve));
} else {
resolve({ result: null });
}
Expand All @@ -457,8 +457,9 @@ class FakeWebUsbService {
}

class USBDeviceRequestEvent {
constructor(deviceFilters, resolve) {
this.filters = convertMojoDeviceFilters(deviceFilters);
constructor(options, resolve) {
this.filters = convertMojoDeviceFilters(options.filters);
this.exclusionFilters = convertMojoDeviceFilters(options.exclusionFilters);
this.resolveFunc_ = resolve;
}

Expand Down
16 changes: 13 additions & 3 deletions webusb/usb.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,18 @@ usb_test(() => {
assert_object_equals(event.filters[i], expectedFilters[i]);
}

assert_equals(event.exclusionFilters.length, expectedFilters.length);
for (var i = 0; i < event.exclusionFilters.length; ++i) {
assert_object_equals(event.exclusionFilters[i], expectedFilters[i]);
}

event.respondWith(null);
};

const filters = expectedFilters;
const exclusionFilters = expectedFilters;
return callWithTrustedClick(() => {
return navigator.usb.requestDevice({ filters: expectedFilters })
return navigator.usb.requestDevice({ filters, exclusionFilters })
.then(device => {
assert_unreached(
'requestDevice should reject because no device selected');
Expand All @@ -94,11 +101,14 @@ usb_test(async () => {
{ subclassCode: 5678 }, // subclassCode requires classCode
{ protocolCode: 9012 }, // protocolCode requires subclassCode
];
const badFilterOptions = ['filters', 'exclusionFilters'].flatMap(key => {
return badFilters.map(filter => ({[key]: [filter]}));
});

for (const filter of badFilters) {
for (const badFilterOption of badFilterOptions) {
await callWithTrustedClick(async () => {
try {
await navigator.usb.requestDevice({ filters: [filter] });
await navigator.usb.requestDevice(badFilterOption);
assert_unreached(
'requestDevice should reject because of invalid filters');
} catch (error) {
Expand Down

0 comments on commit 21f1af9

Please sign in to comment.