Skip to content

Truly independent web browser

LibWeb+LibWebView: Keep a moved popup's is created by web content

Shannon Booth83551e5b6da6
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 04c32ad3fa3..87da3cfae29 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -873,6 +873,8 @@ GC::Ref<HTML::LocalNavigable> Page::begin_hosting(HTML::CrossProcessId id, HTML:
auto& browsing_context = *stand_in->active_browsing_context();
browsing_context.set_browsing_context_group_id(replicated_state.browsing_context_group_id);
browsing_context.set_is_auxiliary(replicated_state.active_browsing_context_is_auxiliary);
+ stand_in->set_is_created_by_web_content(replicated_state.top_level_is_created_by_web_content);
+ stand_in->set_session_history_entry_count(replicated_state.top_level_session_history_entry_count);
VERIFY(m_top_level_traversable.ptr() == navigable.ptr());
m_top_level_traversable = stand_in;
update_needs_beforeunload_check();
diff --git a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp
index 241fbf38dc9..c567cd3d008 100644
--- a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp
+++ b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp
@@ -66,6 +66,8 @@ ErrorOr<void> encode(Encoder& encoder, Web::HTML::ReplicatedNavigableState const
TRY(encoder.encode(state.active_document_is_fully_active));
TRY(encoder.encode(state.top_level_creation_url));
TRY(encoder.encode(state.top_level_origin));
+ TRY(encoder.encode(state.top_level_is_created_by_web_content));
+ TRY(encoder.encode(state.top_level_session_history_entry_count));
TRY(encoder.encode(state.has_cross_site_ancestor));
TRY(encoder.encode(state.browsing_context_group_id));
TRY(encoder.encode(state.opener_policy));
@@ -90,6 +92,8 @@ ErrorOr<Web::HTML::ReplicatedNavigableState> decode(Decoder& decoder)
.active_document_is_fully_active = TRY(decoder.decode<bool>()),
.top_level_creation_url = TRY(decoder.decode<URL::URL>()),
.top_level_origin = TRY(decoder.decode<URL::Origin>()),
+ .top_level_is_created_by_web_content = TRY(decoder.decode<bool>()),
+ .top_level_session_history_entry_count = TRY(decoder.decode<u64>()),
.has_cross_site_ancestor = TRY(decoder.decode<bool>()),
.browsing_context_group_id = TRY(decoder.decode<Optional<u64>>()),
.opener_policy = TRY(decoder.decode<Web::HTML::OpenerPolicy>()),
diff --git a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h
index 75a9bec81af..971e398c423 100644
--- a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h
+++ b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h
@@ -56,6 +56,8 @@ struct ReplicatedNavigableState {
bool active_document_is_fully_active { false };
URL::URL top_level_creation_url;
URL::Origin top_level_origin;
+ bool top_level_is_created_by_web_content { false };
+ u64 top_level_session_history_entry_count { 1 };
bool has_cross_site_ancestor { false };
Optional<u64> browsing_context_group_id;
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index 82801b7bf3f..4aa5614e142 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -1083,6 +1083,8 @@ Optional<Web::HTML::ReplicatedNavigableState> CanonicalNavigable::replicated_sta
.active_document_is_fully_active = hosted_state.active_document_is_fully_active,
.top_level_creation_url = traversable.active_document().creation_url(),
.top_level_origin = traversable.active_document().origin(),
+ .top_level_is_created_by_web_content = traversable.is_created_by_web_content(),
+ .top_level_session_history_entry_count = traversable.session_history().size(),
.has_cross_site_ancestor = active_document_has_cross_site_ancestor(),
.browsing_context_group_id = browsing_context_group_id,
.opener_policy = active_document().opener_policy(),
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index 0f5ca9445f9..b6d33080458 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -617,6 +617,9 @@ void CanonicalTraversable::remove_from_index(CanonicalNavigable& navigable)
void CanonicalTraversable::session_history_changed()
{
+ // The traversable's replicated state carries its session history entries's size.
+ send_replicated_state();
+
if (on_session_history_changed)
on_session_history_changed();
}
diff --git a/Libraries/LibWebView/CanonicalTraversable.h b/Libraries/LibWebView/CanonicalTraversable.h
index 545521ae683..96c77df8f99 100644
--- a/Libraries/LibWebView/CanonicalTraversable.h
+++ b/Libraries/LibWebView/CanonicalTraversable.h
@@ -138,6 +138,9 @@ public:
// Fired after every canonical session-history mutation, once the history has reached its post-mutation state.
Function<void()> on_session_history_changed;
+ bool is_created_by_web_content() const { return m_is_created_by_web_content; }
+ void set_is_created_by_web_content(bool value) { m_is_created_by_web_content = value; }
+
Web::HTML::VisibilityState system_visibility_state() const { return m_system_visibility_state; }
void set_system_visibility_state(Web::HTML::VisibilityState);
@@ -268,6 +271,9 @@ private:
HashMap<Web::HTML::CrossProcessId, PendingBeforeunloadCheck> m_pending_beforeunload_checks;
void dispatch_next_beforeunload_group(Web::HTML::CrossProcessId check_id);
+ // https://html.spec.whatwg.org/multipage/document-sequences.html#is-created-by-web-content
+ bool m_is_created_by_web_content { false };
+
// https://html.spec.whatwg.org/multipage/document-sequences.html#system-visibility-state
Web::HTML::VisibilityState m_system_visibility_state { Web::HTML::VisibilityState::Hidden };
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 1e2745c7af3..963507aa314 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2604,6 +2604,10 @@ Messages::WebContentClient::DidRequestNewWebViewResponse WebContentPage::did_req
}
traversable.active_browsing_context().set_popup_sandboxing_flag_set(popup_sandboxing_flag_set);
+ // 10. Set chosen's is created by web content to true.
+ if (!picture_in_picture_navigable.has_value())
+ traversable.set_is_created_by_web_content(true);
+
auto new_page_id = Application::the().allocate_page_id();
client().open_page_for_new_top_level_traversable(new_page_id, traversable);
diff --git a/Tests/LibWeb/Text/expected/SiteIsolation/popup-in-another-process-closes-itself.txt b/Tests/LibWeb/Text/expected/SiteIsolation/popup-in-another-process-closes-itself.txt
new file mode 100644
index 00000000000..0261aec8c4f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SiteIsolation/popup-in-another-process-closes-itself.txt
@@ -0,0 +1,4 @@
+loaded
+loaded
+loaded
+closed: true
diff --git a/Tests/LibWeb/Text/input/SiteIsolation/popup-in-another-process-closes-itself.html b/Tests/LibWeb/Text/input/SiteIsolation/popup-in-another-process-closes-itself.html
new file mode 100644
index 00000000000..ab39b71913c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SiteIsolation/popup-in-another-process-closes-itself.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ // A popup that moved to another process keeps its is created by web content there, so it can close itself after
+ // more navigations.
+ promiseTest(async () => {
+ const httpServer = httpTestServer();
+ const runId = crypto.randomUUID();
+ const nextMessage = () => new Promise(resolve => addEventListener("message", event => resolve(event.data), { once: true }));
+ const echo = async (name, body, hostname) => {
+ const url = new URL(await httpServer.createEcho("GET", `/${name}-${runId}`, {
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html><script>${body}<\/script>`,
+ }));
+ if (hostname)
+ url.hostname = hostname;
+ return url.href;
+ };
+ const loaded = `addEventListener("load", () => opener.postMessage("loaded", "*"));`;
+ const crossSiteHostname = uniqueLocalhostHostname("self-close-cross-site");
+
+ const urls = [
+ await echo("self-close-first", loaded),
+ await echo("self-close-cross-site", loaded, crossSiteHostname),
+ await echo("self-close-cross-site-again", loaded, crossSiteHostname),
+ await echo("self-close-last", `window.close(); opener.postMessage(\`closed: \${window.closed}\`, "*");`, crossSiteHostname),
+ ];
+
+ let message = nextMessage();
+ const popup = window.open(urls[0]);
+ println(await message);
+ for (const url of urls.slice(1)) {
+ message = nextMessage();
+ popup.location.href = url;
+ println(await message);
+ }
+ });
+</script>