shared-worker-in-data-url-context.window.js (2764B)
1 // META: title=data URL shared worker in data URL context 2 // META: script=/service-workers/service-worker/resources/test-helpers.sub.js 3 const mimeType = 'application/javascript'; 4 5 // Tests creating a data URL shared worker in a data URL iframe. 6 promise_test(async t => { 7 const nestedWorkerScriptURL = 8 new URL('/workers/support/post-message-on-load-worker.js', location.href); 9 10 // This code will be executed in a data URL iframe. The iframe tries to create 11 // a shared worker from |nestedWorkerScriptURL|, but that should result in a 12 // failure. This is because the data URL iframe has an opaque origin, and 13 // script fetch is handled as a cross-origin request. 14 const frameCode = ` 15 <script> 16 try { 17 const worker = new SharedWorker('${nestedWorkerScriptURL}'); 18 worker.port.onmessage = e => { 19 window.parent.postMessage( 20 'SharedWorker construction unexpectedly succeeded', '*'); 21 }; 22 worker.onerror = e => window.parent.postMessage('PASS', '*'); 23 } catch (e) { 24 // Cross-origin request should asynchronously fail during worker script 25 // fetch because its request mode is 'same-origin'. 26 window.parent.postMessage( 27 'SharedWorker construction unexpectedly synchronously failed', '*'); 28 } 29 </script> 30 `; 31 32 const p = new Promise(r => window.onmessage = e => r(e.data)); 33 const frame = await with_iframe(`data:text/html;base64,${btoa(frameCode)}`); 34 const result = await p; 35 assert_equals(result, 'PASS'); 36 }, 'Create a shared worker in a data url frame'); 37 38 // Tests creating a data URL shared worker in a data URL iframe. 39 promise_test(async t => { 40 const workerCode = `onconnect = e => e.ports[0].postMessage("PASS");`; 41 42 // This code will be executed in a data URL iframe. The iframe tries to create 43 // a data URL shared worker. Fetching a data URL from the data URL shared 44 // worker is allowed, so the worker construction should succeed. The worker 45 // posts the result to the parent frame. 46 const frameCode = ` 47 <script> 48 try { 49 const worker = new SharedWorker('data:${mimeType},${workerCode};'); 50 worker.port.onmessage = e => window.parent.postMessage(e.data, '*'); 51 worker.onerror = e => { 52 window.parent.postMessage('FAIL: ' + e.message, '*'); 53 }; 54 } catch (e) { 55 window.parent.postMessage( 56 'SharedWorker construction unexpectedly synchronously failed', '*'); 57 } 58 </script> 59 `; 60 61 const p = new Promise(r => window.onmessage = e => r(e.data)); 62 const frame = await with_iframe(`data:text/html;base64,${btoa(frameCode)}`); 63 const result = await p; 64 assert_equals(result, 'PASS'); 65 }, 'Create a data url shared worker in a data url frame');