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