partitioned-estimate-usage-details-service-workers-helper-frame.html (2285B)
1 <!DOCTYPE html> 2 <meta name=help href="https://privacycg.github.io/storage-partitioning/"> 3 <title>Helper frame</title> 4 5 <!-- This resource file is used in the WPT at: 6 wpt/storage/partitioned-estimate-usage-details-service-workers.tentative.https.sub.html 7 A full list of the test steps referenced here are defined in that WPT --> 8 9 <script> 10 // Helper function to obtain service worker usage details for both the same- 11 // partition iframe and cross-partition iframe. 12 const usageDetails = async () => { 13 return (await navigator.storage.estimate()) 14 .usageDetails.serviceWorkerRegistrations || 0 15 } 16 17 // Define test variables. 18 let details = {}; 19 20 // Step 5: Our same-partition iframe intercepts the "get-details" 21 // postMessage, obtains the service worker usage details available to the 22 // iframe, and postMessages the usage back to the test window. 23 window.addEventListener("message", async event => { 24 if (event.data === "get-details") { 25 details.source = "same-site"; 26 details.init = await usageDetails(); 27 event.source.postMessage(details, event.source.origin); 28 } 29 }); 30 31 // Step 8: Once created and loaded, our cross-partition iframe has an 32 // on-load listener that is triggered. To check that our script is executing 33 // in the cross-partition iframe (and not the same-partition iframe), we 34 // check that our parent has a valid opener value. 35 window.addEventListener("load", async () => { 36 if (parent.opener) { 37 // Step 9: Then, our cross-partition iframe obtains the service worker 38 // usage details available to it and postMessages the usage back 39 // to the test window. 40 details.source = "cross-site"; 41 details.init = await usageDetails(); 42 parent.opener.postMessage(details, parent.opener.origin); 43 } 44 }); 45 46 // Step 1: Notify our parent test window that the same-partition iframe 47 // has been created. (Note: this script is shared between both the same- 48 // partition iframe and the cross-partition iframe, but is only 49 // intercepted by the test window in the case of the same-partition iframe). 50 // This postMessage alerts our test window that it can begin recording 51 // service worker usage. 52 window.parent.postMessage("iframe-is-ready", window.parent.origin); 53 </script>