1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
// META: title=data URL dedicated worker in data URL context
// META: script=/service-workers/service-worker/resources/test-helpers.sub.js
const mimeType = 'application/javascript';
// Tests creating a dedicated worker in a data URL iframe.
promise_test(async t => {
const nestedWorkerScriptURL =
new URL('/workers/support/post-message-on-load-worker.js', location.href);
// This code will be executed in a data URL iframe. The iframe tries to create
// a dedicated worker from |nestedWorkerScriptURL|, but that should result in
// a failure. This is because the data URL iframe has an opaque origin, and
// script fetch is handled as a cross-origin request.
const frameCode = `
<script>
try {
const worker = new Worker('${nestedWorkerScriptURL}');
worker.onmessage = e => {
window.parent.postMessage(
'Worker construction unexpectedly succeeded', '*');
};
worker.onerror = e => window.parent.postMessage('PASS', '*');
} catch (e) {
// Cross-origin request should asynchronously fail during worker script
// fetch because its request mode is 'same-origin'.
window.parent.postMessage(
'Worker construction unexpectedly synchronously failed', '*');
}
</script>
`;
const p = new Promise(r => window.onmessage = e => r(e.data));
const frame = await with_iframe(`data:text/html;base64,${btoa(frameCode)}`);
const result = await p;
assert_equals(result, 'PASS');
}, 'Create a dedicated worker in a data url frame');
// Tests creating a dedicated worker in a data URL dedicated worker (i.e.,
// nested dedicated worker).
promise_test(async t => {
const nestedWorkerScriptURL =
new URL('/workers/support/post-message-on-load-worker.js', location.href);
// This code will be executed in a data URL dedicated worker. The worker tries
// to create a nested dedicated worker from |nestedWorkerScriptURL|, but that
// should result in a failure. This is because the data URL dedicated worker
// has an opaque origin, and script fetch is handled as a cross-origin
// request.
const workerCode = `
try {
const worker = new Worker('${nestedWorkerScriptURL}');
worker.onmessage =
e => postMessage('Worker construction unexpectedly succeeded');
worker.onerror = e => postMessage('PASS');
} catch (e) {
// Cross-origin request should asynchronously fail during worker script
// fetch because its request mode is 'same-origin'.
postMessage('Worker construction unexpectedly synchronously failed');
}
`;
const result = await new Promise((resolve, reject) => {
const worker = new Worker(`data:${mimeType};base64,${btoa(workerCode)}`);
worker.onmessage = e => resolve(e.data);
worker.onerror = e => reject(e.message);
});
assert_equals(result, 'PASS');
}, 'Create a dedicated worker in a data url dedicated worker');
// Tests creating a data URL dedicated worker in a data URL iframe.
promise_test(async t => {
// This code will be executed in a data URL iframe. The iframe tries to create
// a data URL dedicated worker. Fetching a data URL from the data URL iframe
// whose origin is opaque is allowed, so the worker construction should
// succeed. The iframe posts the result to the parent frame.
const frameCode = `
<script>
const worker = new Worker('data:${mimeType},postMessage("PASS");');
worker.onmessage = e => window.parent.postMessage(e.data, '*');
worker.onerror = e => {
window.parent.postMessage('FAIL: ' + e.message, '*');
};
</script>
`;
const p = new Promise(r => window.onmessage = e => r(e.data));
const frame = await with_iframe(`data:text/html;base64,${btoa(frameCode)}`);
const result = await p;
assert_equals(result, 'PASS');
}, 'Create a data url dedicated worker in a data url frame');
// Tests creating a data URL dedicated worker in a data URL dedicated worker
// (i.e., nested dedicated worker).
promise_test(async t => {
// This code will be executed in a data URL dedicated worker. The worker tries
// to create a nested data URL dedicated worker. Fetching a data URL from the
// data URL dedicated worker is allowed, so the worker construction should
// succeed. The worker posts the result to the parent frame.
const workerCode = `
const worker = new Worker('data:${mimeType},postMessage("PASS");');
worker.onmessage = e => postMessage(e.data);
worker.onerror = e => postMessage('FAIL: ' + e.message);
`;
const result = await new Promise((resolve, reject) => {
const worker = new Worker(`data:${mimeType};base64,${btoa(workerCode)}`);
worker.onmessage = e => resolve(e.data);
worker.onerror = e => reject(e.message);
});
assert_equals(result, 'PASS');
}, 'Create a data url dedicated worker in a data url dedicated worker');
|