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: TODO
Spec: WICG/webusb#233
Demo: https://usb-exclusion-filters.glitch.me/

Bug: 1455392
Change-Id: I1b61d9daae3e14accedb24e493ae656316427893
  • Loading branch information
beaufortfrancois authored and chromium-wpt-export-bot committed Jun 19, 2023
1 parent d8b86fb commit e011e17
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
12 changes: 8 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,12 @@ class FakeWebUsbService {
}

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

Expand Down
35 changes: 27 additions & 8 deletions webusb/usb.https.window.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,25 @@ 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);
};

return callWithTrustedClick(() => {
return navigator.usb.requestDevice({ filters: expectedFilters })
.then(device => {
assert_unreached(
'requestDevice should reject because no device selected');
})
.catch(error => {
assert_equals(error.code, DOMException.NOT_FOUND_ERR);
});
return navigator.usb
.requestDevice(
{filters: expectedFilters, exclusionFilters: expectedFilters})
.then(device => {
assert_unreached(
'requestDevice should reject because no device selected');
})
.catch(error => {
assert_equals(error.code, DOMException.NOT_FOUND_ERR);
});
});
}, 'filters are sent correctly');

Expand All @@ -108,6 +115,18 @@ usb_test(async () => {
}
}, 'requestDevice rejects on invalid filters');

usb_test(() => {
return callWithTrustedClick(
() => navigator.usb.requestDevice({filters: [], exclusionFilters: []})
.then(device => {
assert_unreachable(
'requestDevice should reject if empty exclusion filters');
})
.catch(error => {
assert_equals(error.name, 'TypeError');
}));
}, 'requestDevice rejects on empty exclusion filters');

usb_test(() => {
return getFakeDevice().then(({ device, fakeDevice }) => {
navigator.usb.test.onrequestdevice = event => {
Expand Down

0 comments on commit e011e17

Please sign in to comment.