Skip to content

Commit 79341a4

Browse files
odejesushchromium-wpt-export-bot
authored andcommitted
Add WebUSB FeaturePolicy WPT for Workers
This change adds Feature Policy tests for Dedicated Workers. Bug: 841510 Change-Id: I117f8a5bb61d5783b6867e7edcc9d99aff41a497 Reviewed-on: https://chromium-review.googlesource.com/1093379 Commit-Queue: Ovidio Henriquez <[email protected]> Reviewed-by: Reilly Grant <[email protected]> Cr-Commit-Position: refs/heads/master@{#568170}
1 parent 3f54308 commit 79341a4

8 files changed

+145
-19
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
'use strict';
3+
4+
let worker = new Worker('feature-policy-usb-worker.js');
5+
6+
worker.onmessage = event => {
7+
window.parent.postMessage(event.data, '*');
8+
};
9+
worker.postMessage({ type: 'ready' });
10+
</script>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
// Dedicated worker
4+
if (typeof postMessage === 'function') {
5+
onmessage = event => {
6+
switch(event.data.type) {
7+
case 'ready':
8+
navigator.usb.getDevices().then(
9+
() => postMessage({ enabled: true }),
10+
error => postMessage ({ enabled: false }));
11+
break;
12+
}
13+
};
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
importScripts('/resources/testharness.js');
4+
5+
let workerType;
6+
7+
if (typeof postMessage === 'function') {
8+
workerType = 'dedicated';
9+
}
10+
11+
promise_test(() => navigator.usb.getDevices(),
12+
`Inherited header feature policy allows ${workerType} workers.`);
13+
14+
done();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
importScripts('/resources/testharness.js');
4+
5+
const header = 'Feature-Policy header {"usb" : []}';
6+
let workerType;
7+
8+
if (typeof postMessage === 'function') {
9+
workerType = 'dedicated';
10+
}
11+
12+
promise_test(() => navigator.usb.getDevices().then(
13+
() => assert_unreached('expected promise to reject with SecurityError'),
14+
error => assert_equals(error.name, 'SecurityError')),
15+
`Inherited ${header} disallows ${workerType} workers.`);
16+
17+
done();

webusb/usb-allowed-by-feature-policy-attribute-redirect-on-load.https.sub.html

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,40 @@
55
<script src=/feature-policy/resources/featurepolicy.js></script>
66
<script>
77
'use strict';
8-
var relative_path = '/feature-policy/resources/feature-policy-usb.html';
9-
var base_src = '/feature-policy/resources/redirect-on-load.html#';
10-
var same_origin_src = base_src + relative_path;
11-
var cross_origin_src = base_src + 'https://{{domains[www]}}:{{ports[https][0]}}' +
12-
relative_path;
13-
var header = 'Feature-Policy allow="usb"';
8+
const relative_path = '/feature-policy/resources/feature-policy-usb.html';
9+
const base_src = '/feature-policy/resources/redirect-on-load.html#';
10+
const relative_worker_frame_path =
11+
'/feature-policy/resources/feature-policy-usb-worker.html';
12+
const sub = 'https://{{domains[www]}}:{{ports[https][0]}}';
13+
const same_origin_src = base_src + relative_path;
14+
const cross_origin_src = base_src + sub + relative_path;
15+
const same_origin_worker_frame_src = base_src + relative_worker_frame_path;
16+
const cross_origin_worker_frame_src = base_src + sub +
17+
relative_worker_frame_path;
18+
const header = 'Feature-Policy allow="usb"';
1419

1520
async_test(t => {
1621
test_feature_availability(
1722
'usb.getDevices()', t, same_origin_src,
1823
expect_feature_available_default, 'usb');
1924
}, header + ' allows same-origin relocation.');
2025

26+
async_test(t => {
27+
test_feature_availability(
28+
'usb.getDevices()', t, same_origin_worker_frame_src,
29+
expect_feature_available_default, 'usb');
30+
}, header + ' allows workers in same-origin relocation.');
31+
2132
async_test(t => {
2233
test_feature_availability(
2334
'usb.getDevices()', t, cross_origin_src,
2435
expect_feature_unavailable_default, 'usb');
2536
}, header + ' disallows cross-origin relocation.');
37+
38+
async_test(t => {
39+
test_feature_availability(
40+
'usb.getDevices()', t, cross_origin_worker_frame_src,
41+
expect_feature_unavailable_default, 'usb');
42+
}, header + ' disallows workers in cross-origin relocation.');
2643
</script>
2744
</body>

webusb/usb-allowed-by-feature-policy-attribute.https.sub.html

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,42 @@
55
<script src=/feature-policy/resources/featurepolicy.js></script>
66
<script>
77
'use strict';
8-
var same_origin_src = '/feature-policy/resources/feature-policy-usb.html';
9-
var cross_origin_src = 'https://{{domains[www]}}:{{ports[https][0]}}' +
10-
same_origin_src;
11-
var feature_name = 'Feature policy "usb"';
12-
var header = 'allow="usb" attribute';
8+
const sub = 'https://{{domains[www]}}:{{ports[https][0]}}';
9+
const same_origin_src = '/feature-policy/resources/feature-policy-usb.html';
10+
const cross_origin_src = sub + same_origin_src;
11+
const same_origin_worker_frame_src =
12+
'/feature-policy/resources/feature-policy-usb-worker.html';
13+
const cross_origin_worker_frame_src = sub + same_origin_worker_frame_src;
14+
const feature_name = 'Feature policy "usb"';
15+
const header = 'allow="usb" attribute';
1316

1417
async_test(t => {
1518
test_feature_availability(
1619
'usb.getDevices()', t, same_origin_src,
1720
expect_feature_available_default, 'usb');
1821
}, feature_name + ' can be enabled in same-origin iframe using ' + header);
1922

23+
async_test(t => {
24+
test_feature_availability(
25+
'usb.getDevices()', t, same_origin_worker_frame_src,
26+
expect_feature_available_default, 'usb');
27+
}, feature_name + ' can be enabled in a worker in same-origin iframe using ' +
28+
header);
29+
2030
async_test(t => {
2131
test_feature_availability(
2232
'usb.getDevices()', t, cross_origin_src,
2333
expect_feature_available_default, 'usb');
2434
}, feature_name + ' can be enabled in cross-origin iframe using ' + header);
35+
36+
async_test(t => {
37+
test_feature_availability(
38+
'usb.getDevices()', t, cross_origin_worker_frame_src,
39+
expect_feature_available_default, 'usb');
40+
}, feature_name + ' can be enabled in a worker in cross-origin iframe using ' +
41+
header);
42+
43+
fetch_tests_from_worker(new Worker(
44+
'/webusb/resources/usb-allowed-by-feature-policy-worker.js'));
2545
</script>
2646
</body>

webusb/usb-allowed-by-feature-policy.https.sub.html

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
<script src=/feature-policy/resources/featurepolicy.js></script>
66
<script>
77
'use strict';
8-
var same_origin_src = '/feature-policy/resources/feature-policy-usb.html';
9-
var cross_origin_src = 'https://{{domains[www]}}:{{ports[https][0]}}' +
10-
same_origin_src;
11-
var header = 'Feature-Policy header {"usb" : ["*"]}';
8+
const sub = 'https://{{domains[www]}}:{{ports[https][0]}}';
9+
const same_origin_src = '/feature-policy/resources/feature-policy-usb.html';
10+
const cross_origin_src = sub + same_origin_src;
11+
const same_origin_worker_frame_src =
12+
'/feature-policy/resources/feature-policy-usb-worker.html';
13+
const cross_origin_worker_frame_src = sub + same_origin_worker_frame_src;
14+
const header = 'Feature-Policy header {"usb" : ["*"]}';
1215

1316
promise_test(
1417
() => navigator.usb.getDevices(),
@@ -19,9 +22,23 @@
1922
expect_feature_available_default);
2023
}, header + ' allows same-origin iframes.');
2124

25+
async_test(t => {
26+
test_feature_availability('usb.getDevices()', t, same_origin_worker_frame_src,
27+
expect_feature_available_default);
28+
}, header + ' allows workers in same-origin iframes.');
29+
2230
async_test(t => {
2331
test_feature_availability('usb.getDevices()', t, cross_origin_src,
2432
expect_feature_available_default);
2533
}, header + ' allows cross-origin iframes.');
34+
35+
async_test(t => {
36+
test_feature_availability('usb.getDevices()', t,
37+
cross_origin_worker_frame_src,
38+
expect_feature_available_default);
39+
}, header + ' allows workers in cross-origin iframes.');
40+
41+
fetch_tests_from_worker(new Worker(
42+
'/webusb/resources/usb-allowed-by-feature-policy-worker.js'));
2643
</script>
2744
</body>

webusb/usb-disabled-by-feature-policy.https.sub.html

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
<script src=/feature-policy/resources/featurepolicy.js></script>
66
<script>
77
'use strict';
8-
var same_origin_src = '/feature-policy/resources/feature-policy-usb.html';
9-
var cross_origin_src = 'https://{{domains[www]}}:{{ports[https][0]}}' +
10-
same_origin_src;
11-
var header = 'Feature-Policy header {"usb" : []}';
8+
const sub = 'https://{{domains[www]}}:{{ports[https][0]}}';
9+
const same_origin_src = '/feature-policy/resources/feature-policy-usb.html';
10+
const cross_origin_src = sub + same_origin_src;
11+
const same_origin_worker_frame_src =
12+
'/feature-policy/resources/feature-policy-usb-worker.html';
13+
const cross_origin_worker_frame_src = sub + same_origin_worker_frame_src;
14+
const header = 'Feature-Policy header {"usb" : []}';
1215

1316
promise_test(() => {
1417
return navigator.usb.getDevices().then(() => {
@@ -23,9 +26,23 @@
2326
expect_feature_unavailable_default);
2427
}, header + ' disallows same-origin iframes.');
2528

29+
async_test(t => {
30+
test_feature_availability('usb.getDevices()', t, same_origin_worker_frame_src,
31+
expect_feature_unavailable_default);
32+
}, header + ' disallows workers in same-origin iframes.');
33+
2634
async_test(t => {
2735
test_feature_availability('usb.getDevices()', t, cross_origin_src,
2836
expect_feature_unavailable_default);
2937
}, header + ' disallows cross-origin iframes.');
38+
39+
async_test(t => {
40+
test_feature_availability('usb.getDevices()', t,
41+
cross_origin_worker_frame_src,
42+
expect_feature_unavailable_default);
43+
}, header + ' disallows workers in cross-origin iframes.');
44+
45+
fetch_tests_from_worker(new Worker(
46+
'/webusb/resources/usb-disabled-by-feature-policy-worker.js'));
3047
</script>
3148
</body>

0 commit comments

Comments
 (0)