Skip to content

Truly independent web browser

2.6 KB
Raw Blame
<!DOCTYPE html>
<script src="../../include.js"></script>
<script>
    // A grandchild same origin with this page, under an isolated iframe, is hosted in this page's process. The first
    // script here to reach the isolated iframe's WindowProxy is the grandchild's, through parent. Once the grandchild
    // navigates to another document, neither that WindowProxy nor the cross-origin property descriptors the grandchild
    // read through it may keep the grandchild's first document alive.
    // This page is served over HTTP so that the grandchild can be same origin with it.
    promiseTest(async () => {
        const httpServer = httpTestServer();

        const secondGrandchildUrl = await httpServer.createEcho("GET", "/document-reaching-a-remote-parent-second", {
            status: 200,
            headers: { "Content-Type": "text/html" },
            body: `<!DOCTYPE html><script>top.postMessage("second grandchild", "*");<\/script>`,
        });

        // The WeakRef comes from this page's realm, so that it does not keep the grandchild's realm alive itself.
        const firstGrandchildUrl = await httpServer.createEcho("GET", "/document-reaching-a-remote-parent-first", {
            status: 200,
            headers: { "Content-Type": "text/html" },
            body: `<!DOCTYPE html><script>
                document.preserveWrapper = true;
                top.weakFirstGrandchildDocument = new top.WeakRef(document);
                parent.postMessage("first grandchild", "*");
                location.replace(${JSON.stringify(secondGrandchildUrl)});
            <\/script>`,
        });

        const childUrl = new URL(
            await httpServer.createEcho("GET", "/document-reaching-a-remote-parent-child", {
                status: 200,
                headers: { "Content-Type": "text/html" },
                body: `<!DOCTYPE html><iframe src="${firstGrandchildUrl}"></iframe>`,
            })
        );
        childUrl.hostname = uniqueLocalhostHostname("document-reaching-a-remote-parent");

        const secondGrandchildLoaded = new Promise(resolve => {
            addEventListener("message", event => {
                if (event.data === "second grandchild") resolve();
            });
        });
        const iframe = document.createElement("iframe");
        iframe.src = childUrl;
        document.body.appendChild(iframe);
        await secondGrandchildLoaded;

        println(internals.dumpSiteIsolationProcessTree().trimEnd());
        await collectGarbageUntilCleared([weakFirstGrandchildDocument]);
        println(`first grandchild's document collected: ${weakFirstGrandchildDocument.deref() === undefined}`);
    });
</script>