Truly independent web browser
WebContent: Stop a page shown elsewhere from keeping its document alive
Shannon Boothc1b6aeba601b
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cppindex bc3cddfa471..8fdd591c096 100644--- a/Services/WebContent/PageClient.cpp+++ b/Services/WebContent/PageClient.cpp@@ -191,7 +191,6 @@ void PageClient::visit_edges(JS::Cell::Visitor& visitor)visitor.visit(m_page);for (auto& command : m_webdriver_input_commands)visitor.visit(command.value);- visitor.visit(m_top_level_document_console_client);for (auto& range : m_pending_text_fragment_generation_ranges)visitor.visit(range.value);for (auto& promise : m_pending_delete_all_cookies_promises)@@ -1042,7 +1041,7 @@ void PageClient::page_did_change_active_document_in_top_level_browsing_context(Wm_web_ui.clear();// The output that DevTools gets once it asks belongs to the document that the tab shows.- if (m_top_level_document_console_client && document.console_client() != m_top_level_document_console_client)+ if (!document.console_client() || document.console_client().ptr() != m_top_level_document_console_client.ptr().ptr())m_console_output_buffer = {};if (auto console_client = attach_js_console(document))@@ -2660,15 +2659,16 @@ ErrorOr<WebView::DebuggerObjectProperties> PageClient::console_object_propertiesvoid PageClient::js_console_input(StringView js_source){- if (m_top_level_document_console_client)- m_top_level_document_console_client->handle_input(js_source);+ if (auto console_client = m_top_level_document_console_client.ptr())+ console_client->handle_input(js_source);}Optional<Web::HTML::ConsoleInputCompletion> PageClient::complete_js_console_input(StringView input){- if (!m_top_level_document_console_client)+ auto console_client = m_top_level_document_console_client.ptr();+ if (!console_client)return {};- return m_top_level_document_console_client->complete_input(input);+ return console_client->complete_input(input);}void PageClient::run_javascript(StringView js_source)diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.hindex 53397632d9c..a7379ad4373 100644--- a/Services/WebContent/PageClient.h+++ b/Services/WebContent/PageClient.h@@ -12,6 +12,7 @@#include <AK/HashMap.h>#include <AK/HashTable.h>#include <AK/Utf16String.h>+#include <LibGC/Weak.h>#include <LibGfx/Rect.h>#include <LibJS/Forward.h>#include <LibWeb/CSS/StyleSheetIdentifier.h>@@ -483,7 +484,9 @@ private:u64 m_next_webdriver_mouse_event_request_id { 0 };RefPtr<WebUIConnection> m_web_ui;- GC::Ptr<WebContentConsoleClient> m_top_level_document_console_client;+ // Weak, since the page can stop showing a document here without showing another, when another process hosts the+ // tab's next document.+ GC::Weak<WebContentConsoleClient> m_top_level_document_console_client;RefPtr<Core::Timer> m_frame_timer;FrameTimerPurpose m_frame_timer_purpose { FrameTimerPurpose::Inactive };diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/document-of-popup-gone-to-another-process-is-collectable.txt b/Tests/LibWeb/Text/expected/SiteIsolation/document-of-popup-gone-to-another-process-is-collectable.txtnew file mode 100644index 00000000000..dde05781c08--- /dev/null+++ b/Tests/LibWeb/Text/expected/SiteIsolation/document-of-popup-gone-to-another-process-is-collectable.txt@@ -0,0 +1,4 @@+message: same origin+message: cross-site+document here collected: true+popup.closed: falsediff --git a/Tests/LibWeb/Text/input/SiteIsolation/document-of-popup-gone-to-another-process-is-collectable.html b/Tests/LibWeb/Text/input/SiteIsolation/document-of-popup-gone-to-another-process-is-collectable.htmlnew file mode 100644index 00000000000..de2f03f9c68--- /dev/null+++ b/Tests/LibWeb/Text/input/SiteIsolation/document-of-popup-gone-to-another-process-is-collectable.html@@ -0,0 +1,55 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // A popup that navigates cross-site leaves its opener's process, whose page for the popup stays. Neither that page+ // nor the WindowProxy the opener keeps may keep the document the popup displayed here alive.+ // This page is served over HTTP so that the popup's first document can be same origin with it.+ promiseTest(async () => {+ const httpServer = httpTestServer();++ const crossSiteUrl = new URL(+ await httpServer.createEcho("GET", "/document-of-popup-gone-to-another-process-cross-site", {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>opener.postMessage("cross-site", "*");<\/script>`,+ })+ );+ crossSiteUrl.hostname = uniqueLocalhostHostname("document-of-popup-gone-to-another-process");++ const sameOriginUrl = await httpServer.createEcho("GET", "/document-of-popup-gone-to-another-process-same-origin", {+ status: 200,+ headers: { "Content-Type": "text/html" },+ body: `<!DOCTYPE html><script>opener.postMessage("same origin", "*");<\/script>`,+ });++ const nextMessage = () => new Promise(resolve => addEventListener("message", event => resolve(event.data), { once: true }));++ let message = nextMessage();+ const popup = window.open(sameOriginUrl);+ println(`message: ${await message}`);++ // An expando keeps the document's wrapper alive for as long as the document lives. No local of this function+ // holds the document.+ const weakDocument = (() => {+ const documentHere = popup.document;+ documentHere.preserveWrapper = true;+ return new WeakRef(documentHere);+ })();++ message = nextMessage();+ popup.location = crossSiteUrl.href;+ println(`message: ${await message}`);++ // The popup's message can arrive before this process stops hosting the popup, after which the popup is+ // cross-origin here.+ if (!(await waitForCondition(() => Object.getPrototypeOf(popup) === null))) {+ println("popup did not leave this process");+ return;+ }++ await collectGarbageUntilCleared([weakDocument]);+ println(`document here collected: ${weakDocument.deref() === undefined}`);+ println(`popup.closed: ${popup.closed}`);+ popup.close();+ });+</script>diff --git a/Tests/LibWeb/Text/input/SiteIsolation/document-of-popup-gone-to-another-process-is-collectable.html.headers b/Tests/LibWeb/Text/input/SiteIsolation/document-of-popup-gone-to-another-process-is-collectable.html.headersnew file mode 100644index 00000000000..156209f9c81--- /dev/null+++ b/Tests/LibWeb/Text/input/SiteIsolation/document-of-popup-gone-to-another-process-is-collectable.html.headers@@ -0,0 +1 @@+Content-Type: text/html