serviceworker-partitioning.tentative.https.window.js (3391B)
1 // META: script=/common/utils.js 2 3 const sw_url = location.pathname.replace(/[^/]*$/, '') + 4 "./resources/serviceworker-partitioning-helper.js"; 5 6 promise_test(async t => { 7 // Create 4 iframes (two normal and two credentialless ones) and register 8 // a serviceworker with the same scope and url in all of them. 9 // 10 // Registering the same service worker again with the same url and 11 // scope is a no-op. However, credentialless iframes get partitioned 12 // service workers, so we should have a total of 2 service workers 13 // at the end (one for the normal iframes and one for the credentialless 14 // ones). 15 let iframes = await Promise.all([ 16 { name: "normal", credentialless: false}, 17 { name: "normal_control", credentialless: false}, 18 { name: "credentialless", credentialless: true}, 19 { name: "credentialless_control", credentialless: true}, 20 ].map(async ({name, credentialless}) => { 21 22 let iframe = await new Promise(resolve => { 23 let iframe = document.createElement('iframe'); 24 iframe.onload = () => resolve(iframe); 25 iframe.src = '/common/blank.html'; 26 if (credentialless) iframe.credentialless = true; 27 document.body.append(iframe); 28 }); 29 30 let sw = await new Promise(resolve => { 31 iframe.contentWindow.navigator.serviceWorker.register(sw_url) 32 .then(r => { 33 add_completion_callback(_ => r.unregister()); 34 resolve(r.active || r.installing || r.waiting); 35 }); 36 }); 37 return { iframe: iframe, name: name, sw: sw }; 38 })); 39 40 // Setup a MessageChannel for each pair (iframe, serviceworker). 41 // Ping each serviceworker telling him which iframe it belongs to. 42 iframes.forEach((iframe, i) => { 43 iframe.channel = new MessageChannel(); 44 iframe.sw.postMessage({ from: iframe.name }, [iframe.channel.port2]); 45 }); 46 47 let msg_promises = iframes.map(iframe => new Promise(resolve => { 48 iframe.channel.port1.onmessage = event => resolve(event.data); 49 })); 50 51 // Ping each (iframe, serviceworker) asking for which messages it got. 52 iframes.map(iframe => iframe.sw.postMessage({ check: iframe.name })); 53 54 // Collect all replies. 55 let msgs = await Promise.all(msg_promises); 56 57 // The "normal" iframe serviceworker belongs to the "normal" and the 58 // "normal_control" iframes. 59 assert_true(!!msgs[0]["normal"]); 60 assert_true(!!msgs[0]["normal_control"]); 61 assert_false(!!msgs[0]["credentialless"]); 62 assert_false(!!msgs[0]["credentialless_control"]); 63 64 // The "normal_control" iframe shares the same serviceworker as the "normal" 65 // iframe. 66 assert_true(!!msgs[1]["normal"]); 67 assert_true(!!msgs[1]["normal_control"]); 68 assert_false(!!msgs[1]["credentialless"]); 69 assert_false(!!msgs[1]["credentialless_control"]); 70 71 // The "credentialless" iframe serviceworker belongs to the "credentialless" 72 // and the "credentialless_control" iframes. 73 assert_false(!!msgs[2]["normal"]); 74 assert_false(!!msgs[2]["normal_control"]); 75 assert_true(!!msgs[2]["credentialless"]); 76 assert_true(!!msgs[2]["credentialless_control"]); 77 78 // The "credentialless_control" iframe shares the same serviceworker as the 79 // "credentialless" iframe. 80 assert_false(!!msgs[3]["normal"]); 81 assert_false(!!msgs[3]["normal_control"]); 82 assert_true(!!msgs[3]["credentialless"]); 83 assert_true(!!msgs[3]["credentialless_control"]); 84 85 }, "credentialless iframes get partitioned service workers.");