Skip to content

Commit

Permalink
Bug 1919972 [wpt PR 48244] - WebHID: Add support for dedicated worker…
Browse files Browse the repository at this point in the history
…s, a=testonly

Automatic update from web-platform-tests
WebHID: Add support for dedicated workers

This CL exposes the WebHID API to dedicated workers behind the blink
runtime feature WebHIDOnDedicatedWorkers.

Intent to prototype: https://groups.google.com/a/chromium.org/g/blink-dev/c/y__BOYfZWzI
Spec PR: WICG/webhid#121
Spec issue: WICG/webhid#120
Demo: https://webhid-worker.glitch.me/

Change-Id: If145f34faf9211a54c8b4a15fbe7e903411b8d1d
Bug: 365932453
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5841991
Reviewed-by: Arthur Sonzogni <[email protected]>
Reviewed-by: Yoav Weiss (@Shopify) <[email protected]>
Reviewed-by: Matt Reynolds <[email protected]>
Commit-Queue: Fr <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1357046}

--

wpt-commits: f3a05bb7fe21b6e6d8fbf5923d53abfc7c04fd66
wpt-pr: 48244
  • Loading branch information
beaufortfrancois authored and moz-wptsync-bot committed Sep 25, 2024
1 parent 5babe57 commit efffaab
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 6 deletions.
10 changes: 5 additions & 5 deletions testing/web-platform/tests/interfaces/webhid.idl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
[SameObject] readonly attribute HID hid;
};

[Exposed=ServiceWorker, SecureContext]
[Exposed=(DedicatedWorker,ServiceWorker), SecureContext]
partial interface WorkerNavigator {
[SameObject] readonly attribute HID hid;
};

[Exposed=(Window,ServiceWorker), SecureContext]
[Exposed=(DedicatedWorker,ServiceWorker,Window), SecureContext]
interface HID : EventTarget {
attribute EventHandler onconnect;
attribute EventHandler ondisconnect;
Expand All @@ -33,7 +33,7 @@ dictionary HIDDeviceFilter {
unsigned short usage;
};

[Exposed=Window, SecureContext]
[Exposed=(DedicatedWorker,ServiceWorker,Window), SecureContext]
interface HIDDevice : EventTarget {
attribute EventHandler oninputreport;
readonly attribute boolean opened;
Expand All @@ -51,7 +51,7 @@ interface HIDDevice : EventTarget {
Promise<DataView> receiveFeatureReport([EnforceRange] octet reportId);
};

[Exposed=Window, SecureContext]
[Exposed=(DedicatedWorker,ServiceWorker,Window), SecureContext]
interface HIDConnectionEvent : Event {
constructor(DOMString type, HIDConnectionEventInit eventInitDict);
[SameObject] readonly attribute HIDDevice device;
Expand All @@ -61,7 +61,7 @@ dictionary HIDConnectionEventInit : EventInit {
required HIDDevice device;
};

[Exposed=Window, SecureContext]
[Exposed=(DedicatedWorker,ServiceWorker,Window), SecureContext]
interface HIDInputReportEvent : Event {
constructor(DOMString type, HIDInputReportEventInit eventInitDict);
[SameObject] readonly attribute HIDDevice device;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
'use strict';

let worker = new Worker('permissions-policy-hid-worker.js');

worker.onmessage = event => {
window.parent.postMessage(event.data, '*');
};
worker.postMessage({ type: 'ready' });
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

// Dedicated worker
if (typeof postMessage === 'function') {
onmessage = event => {
switch(event.data.type) {
case 'ready':
navigator.hid.getDevices().then(
() => postMessage({ type: 'availability-result', enabled: true }),
error => postMessage ({ type: 'availability-result', enabled: false }));
break;
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>
'use strict';

Promise.resolve().then(() => navigator.hid.getDevices()).then(devices => {
window.parent.postMessage({ type: 'availability-result', enabled: true }, '*');
}, error => {
window.parent.postMessage({ type: 'availability-result', enabled: false }, '*');
});
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<body>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/permissions-policy/resources/permissions-policy.js></script>
<script>
'use strict';
const relative_path = '/permissions-policy/resources/permissions-policy-hid.html';
const base_src = '/permissions-policy/resources/redirect-on-load.html#';
const relative_worker_frame_path =
'/permissions-policy/resources/permissions-policy-hid-worker.html';
const sub = 'https://{{domains[www]}}:{{ports[https][0]}}';
const same_origin_src = base_src + relative_path;
const cross_origin_src = base_src + sub + relative_path;
const same_origin_worker_frame_src = base_src + relative_worker_frame_path;
const cross_origin_worker_frame_src = base_src + sub +
relative_worker_frame_path;
const header = 'Permissions-Policy allow="hid"';

async_test(t => {
test_feature_availability(
'hid.getDevices()', t, same_origin_src,
expect_feature_available_default, 'hid');
}, header + ' allows same-origin relocation.');

async_test(t => {
test_feature_availability(
'hid.getDevices()', t, same_origin_worker_frame_src,
expect_feature_available_default, 'hid');
}, header + ' allows workers in same-origin relocation.');

async_test(t => {
test_feature_availability(
'hid.getDevices()', t, cross_origin_src,
expect_feature_unavailable_default, 'hid');
}, header + ' disallows cross-origin relocation.');

async_test(t => {
test_feature_availability(
'hid.getDevices()', t, cross_origin_worker_frame_src,
expect_feature_unavailable_default, 'hid');
}, header + ' disallows workers in cross-origin relocation.');
</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<body>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/permissions-policy/resources/permissions-policy.js></script>
<script>
'use strict';
const sub = 'https://{{domains[www]}}:{{ports[https][0]}}';
const same_origin_src = '/permissions-policy/resources/permissions-policy-hid.html';
const cross_origin_src = sub + same_origin_src;
const same_origin_worker_frame_src =
'/permissions-policy/resources/permissions-policy-hid-worker.html';
const cross_origin_worker_frame_src = sub + same_origin_worker_frame_src;
const feature_name = 'Permissions policy "hid"';
const header = 'allow="hid" attribute';

async_test(t => {
test_feature_availability(
'hid.getDevices()', t, same_origin_src,
expect_feature_available_default, 'hid');
}, feature_name + ' can be enabled in same-origin iframe using ' + header);

async_test(t => {
test_feature_availability(
'hid.getDevices()', t, same_origin_worker_frame_src,
expect_feature_available_default, 'hid');
}, feature_name + ' can be enabled in a worker in same-origin iframe using ' +
header);

async_test(t => {
test_feature_availability(
'hid.getDevices()', t, cross_origin_src,
expect_feature_available_default, 'hid');
}, feature_name + ' can be enabled in cross-origin iframe using ' + header);

async_test(t => {
test_feature_availability(
'hid.getDevices()', t, cross_origin_worker_frame_src,
expect_feature_available_default, 'hid');
}, feature_name + ' can be enabled in a worker in cross-origin iframe using ' +
header);

fetch_tests_from_worker(new Worker(
'/webhid/resources/hid-allowed-by-permissions-policy-worker.js'));
</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<body>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/permissions-policy/resources/permissions-policy.js></script>
<script>
'use strict';
const sub = 'https://{{domains[www]}}:{{ports[https][0]}}';
const same_origin_src = '/permissions-policy/resources/permissions-policy-hid.html';
const cross_origin_src = sub + same_origin_src;
const same_origin_worker_frame_src =
'/permissions-policy/resources/permissions-policy-hid-worker.html';
const cross_origin_worker_frame_src = sub + same_origin_worker_frame_src;
const header = 'Permissions-Policy header hid=*';

promise_test(
() => navigator.hid.getDevices(),
header + ' allows the top-level document.');

async_test(t => {
test_feature_availability('hid.getDevices()', t, same_origin_src,
expect_feature_available_default);
}, header + ' allows same-origin iframes.');

async_test(t => {
test_feature_availability('hid.getDevices()', t, same_origin_worker_frame_src,
expect_feature_available_default);
}, header + ' allows workers in same-origin iframes.');

// Set allow="hid" on iframe element to delegate 'hid' to cross origin subframe.
async_test(t => {
test_feature_availability('hid.getDevices()', t, cross_origin_src,
expect_feature_available_default, 'hid');
}, header + ' allows cross-origin iframes.');

// Set allow="hid" on iframe element to delegate 'hid' to cross origin subframe.
async_test(t => {
test_feature_availability('hid.getDevices()', t,
cross_origin_worker_frame_src,
expect_feature_available_default, 'hid');
}, header + ' allows workers in cross-origin iframes.');

fetch_tests_from_worker(new Worker(
'/webhid/resources/hid-allowed-by-permissions-policy-worker.js'));
</script>
</body>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Permissions-Policy: hid=*
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ idl_test(
['webhid'],
['html', 'dom'],
idl_array => {
if (self.GLOBAL.isWindow()) {
idl_array.add_objects({ Navigator: ['navigator'] });
} else if (self.GLOBAL.isWorker()) {
idl_array.add_objects({ WorkerNavigator: ['navigator'] });
}

idl_array.add_objects({
HID: ['navigator.hid'],
Navigator: ['navigator'],
// TODO: HIDConnectionEvent
// TODO: HIDInputReportEvent
// TODO: HIDDevice
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

importScripts('/resources/testharness.js');

let workerType;

if (typeof postMessage === 'function') {
workerType = 'dedicated';
}

promise_test(() => navigator.hid.getDevices(),
`Inherited header permissions policy allows ${workerType} workers.`);

done();
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

importScripts('/resources/testharness.js');

const header = 'Permissions-Policy header hid=()';
let workerType;

if (typeof postMessage === 'function') {
workerType = 'dedicated';
}

promise_test(() => navigator.hid.getDevices().then(
() => assert_unreached('expected promise to reject with SecurityError'),
error => assert_equals(error.name, 'SecurityError')),
`Inherited ${header} disallows ${workerType} workers.`);

done();

0 comments on commit efffaab

Please sign in to comment.