Skip to content

Truly independent web browser

LibWebView: Keep headless windows open when their opener closes

Tim Ledbetter304850c95544
diff --git a/Libraries/LibWebView/HeadlessWebView.cpp b/Libraries/LibWebView/HeadlessWebView.cpp
index efcbc47f181..b7b5aa72267 100644
--- a/Libraries/LibWebView/HeadlessWebView.cpp
+++ b/Libraries/LibWebView/HeadlessWebView.cpp
@@ -212,6 +212,10 @@ void HeadlessWebView::propagate_web_content_crash(WebContentCrashReason crash_re
HeadlessWebView& HeadlessWebView::adopt_child_web_view(NonnullOwnPtr<HeadlessWebView> web_view)
{
+ // A window outlives the window that opened it, so the root view owns every window opened from its windows.
+ if (m_parent_web_view)
+ return m_parent_web_view->adopt_child_web_view(move(web_view));
+
auto* child_web_view = web_view.ptr();
auto weak_this = make_weak_ptr<HeadlessWebView>();
web_view->m_parent_web_view = weak_this;
diff --git a/Libraries/LibWebView/HeadlessWebView.h b/Libraries/LibWebView/HeadlessWebView.h
index 9600fd87b5a..fc5eddcf54e 100644
--- a/Libraries/LibWebView/HeadlessWebView.h
+++ b/Libraries/LibWebView/HeadlessWebView.h
@@ -34,7 +34,6 @@ public:
void close_child_web_views()
{
for (auto& child : m_child_web_views) {
- child->close_child_web_views();
// Children sharing a crashed WebContent process are discarded by their pending crash callbacks.
if (!child->handle().is_empty() && child->page().is_open()) {
child->request_close();
@@ -48,10 +47,8 @@ public:
// Disconnect crash handlers so child crashes don't propagate to parent.
// We don't destroy the children because there may be pending deferred_invokes
// that would cause use-after-free.
- for (auto& child : m_child_web_views) {
+ for (auto& child : m_child_web_views)
child->m_propagate_crashes_to_parent = false;
- child->disconnect_child_crash_handlers();
- }
}
protected:
diff --git a/Tests/LibWeb/Text/expected/HTML/popup-outlives-the-popup-that-opened-it.txt b/Tests/LibWeb/Text/expected/HTML/popup-outlives-the-popup-that-opened-it.txt
new file mode 100644
index 00000000000..5dbf64dc995
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/popup-outlives-the-popup-that-opened-it.txt
@@ -0,0 +1 @@
+Second popup navigated: true
diff --git a/Tests/LibWeb/Text/input/HTML/popup-outlives-the-popup-that-opened-it.html b/Tests/LibWeb/Text/input/HTML/popup-outlives-the-popup-that-opened-it.html
new file mode 100644
index 00000000000..c186b9105a8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/popup-outlives-the-popup-that-opened-it.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // Closing a popup should leave the popups it opened working. Here the second popup navigates to a page that closes
+ // it, which only happens if it can still navigate after the first popup is gone.
+ promiseTest(async () => {
+ const url = await httpTestServer().createEcho("GET", `/popup-outlives-its-opener-${crypto.randomUUID()}`, {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: "<script>close();<\/script>",
+ });
+
+ const firstPopup = open("about:blank");
+ firstPopup.internals.grantTransientActivation();
+ const script = firstPopup.document.createElement("script");
+ script.textContent = `window.secondPopup = open("about:blank");`;
+ firstPopup.document.body.appendChild(script);
+ const secondPopup = firstPopup.secondPopup;
+
+ firstPopup.close();
+ secondPopup.location.href = url;
+ println(`Second popup navigated: ${await waitForCondition(() => secondPopup.closed)}`);
+ });
+</script>