Skip to content

Truly independent web browser

LibWeb+LibWebView+WebContent: Apply force dark in cross-site iframes

sideshowbarker2ebfe7ef89b9
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 1c4aee8169d..fc72d8f0eba 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -4775,6 +4775,7 @@ GC::Ref<LocalNavigable> LocalNavigable::create_stand_in(Badge<Page> badge, Remot
} else {
navigable->m_root_container_state = remote_navigable.replicated_state().container;
navigable->set_parent_compositor_context(as<RemoteNavigable>(*parent_navigable).compositor_context_id());
+ navigable->m_force_dark_enabled = page.force_dark_for_local_roots();
}
// The UI process orders any history step for the navigable after the update for its creation.
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 5ea99315df4..2412cdabbec 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -421,7 +421,7 @@ CSS::PreferredColorScheme Page::preferred_color_scheme() const
// Force-dark presents a dark preference, the way Android WebView's force-dark does (Chrome itself leaves the
// preference alone and darkens per element): a page that can style itself dark does, the color-scheme opt-out keeps
// the filter away from it, and only pages with no dark support get filtered.
- if (has_local_traversable() && local_traversable()->force_dark_enabled())
+ if (has_local_traversable() ? local_traversable()->force_dark_enabled() : m_force_dark_for_local_roots)
return CSS::PreferredColorScheme::Dark;
auto preferred_color_scheme = m_client->preferred_color_scheme();
@@ -834,6 +834,22 @@ GC::Ref<HTML::LocalNavigable> Page::local_traversable() const
return as<HTML::LocalNavigable>(*m_top_level_traversable);
}
+void Page::set_force_dark_for_local_roots(bool enabled)
+{
+ if (m_force_dark_for_local_roots == enabled)
+ return;
+ m_force_dark_for_local_roots = enabled;
+ // NB: A local root can still be a stand-in that its remote parent doesn't list as a child yet, which local_roots()
+ // wouldn't find, so this sets it on every local navigable of the page.
+ GC::RootVector<GC::Ref<HTML::LocalNavigable>> navigables;
+ for (auto navigable : HTML::all_local_navigables()) {
+ if (&navigable->page() == this && !navigable->has_been_destroyed())
+ navigables.append(*navigable);
+ }
+ for (auto const& navigable : navigables)
+ navigable->set_force_dark_enabled(enabled);
+}
+
GC::RootVector<GC::Ref<HTML::LocalNavigable>> Page::local_roots() const
{
GC::RootVector<GC::Ref<HTML::LocalNavigable>> roots;
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index cd1b5f9445a..74376e9098b 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -478,6 +478,11 @@ public:
void invalidate_user_style();
void invalidate_style_for_preference_change();
+ // Whether force-dark applies to the local roots of a page that embeds out-of-process iframes of a tab, which takes it
+ // from the UI process rather than from a local traversable of its own.
+ bool force_dark_for_local_roots() const { return m_force_dark_for_local_roots; }
+ void set_force_dark_for_local_roots(bool);
+
bool content_blocking_enabled_for_url(Optional<URL::URL> const&) const;
void set_content_blocking_policy(bool enabled, ReadonlySpan<String> disabled_sites);
@@ -693,6 +698,7 @@ private:
bool m_listen_for_dom_mutations { false };
bool m_accessibility_interested { false };
Optional<CSS::PreferredColorScheme> m_preferred_color_scheme_override_for_testing;
+ bool m_force_dark_for_local_roots { false };
// The chain of containers above a document leaves this process at a hosted root, and the process holding the
// container above runs the steps for the rest of it.
diff --git a/Libraries/LibWebView/CanonicalTraversable.cpp b/Libraries/LibWebView/CanonicalTraversable.cpp
index ae7408255cd..daee5abb6d8 100644
--- a/Libraries/LibWebView/CanonicalTraversable.cpp
+++ b/Libraries/LibWebView/CanonicalTraversable.cpp
@@ -1741,6 +1741,7 @@ void CanonicalTraversable::did_activate_history_entry(HistoryOperation& operatio
// NB: The address bar can already show a pending navigation's URL while the old document
// is still visible. Only apply the destination's zoom after its document is activated.
view->apply_zoom_for_current_host();
+ view->send_force_dark_to_embedded_pages();
view->m_external_url_request_policy.clear_page_request_allowance();
if (view->on_top_level_navigation_commit)
view->on_top_level_navigation_commit();
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 5b7577b2e2b..4972d05eea7 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -4511,6 +4511,22 @@ void ViewImplementation::content_settings_changed()
// The page displaying the tab, and any page chosen to host its next document, host the tab's traversable.
send_force_dark(page());
traversable().for_each_pending_host(send_force_dark);
+ send_force_dark_to_embedded_pages();
+}
+
+// A page embedding out-of-process iframes of the tab has no local traversable to resolve force-dark for, so it takes the
+// tab's own: what applies to the host of the tab's top-level document.
+void ViewImplementation::send_force_dark_to_embedded_page(WebContentPage& page) const
+{
+ page.async_set_force_dark_for_local_roots(Application::settings().force_dark_for_host(current_host_for_settings()));
+}
+
+void ViewImplementation::send_force_dark_to_embedded_pages() const
+{
+ traversable().for_each_hosting_page([&](WebContentPage& page) {
+ if (&page != &this->page())
+ send_force_dark_to_embedded_page(page);
+ });
}
void ViewImplementation::browsing_behavior_changed()
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 0970b541227..07af4abb8fa 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -838,6 +838,8 @@ protected:
// The view to show a tab or window opened from this view in, or null for the application to open one of its own.
virtual ViewImplementation* create_view_for_new_tab_or_window(IsPrivate) { return nullptr; }
void apply_zoom_for_current_host();
+ void send_force_dark_to_embedded_page(WebContentPage&) const;
+ void send_force_dark_to_embedded_pages() const;
void handle_resize();
void fail_pending_debugger_requests();
diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cpp
index 97b90eccbf6..34a9789a2ba 100644
--- a/Libraries/LibWebView/WebContentClient.cpp
+++ b/Libraries/LibWebView/WebContentClient.cpp
@@ -390,6 +390,7 @@ void WebContentClient::register_embedded_page(Web::PageId page_id, CanonicalTrav
Application::process_manager().cancel_forced_exit(child_id());
page.view().send_preferences_to_page(page);
+ page.view().send_force_dark_to_embedded_page(page);
if (Application::browser_options().webdriver_browser_endpoint.has_value())
Application::the().push_webdriver_session_config(page);
page.async_set_has_focus(traversable.has_system_focus());
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index 8a0e0cf3e80..fd1c3555edd 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -3406,6 +3406,12 @@ void ConnectionFromClient::set_force_dark_settings(Web::PageId page_id, bool ena
page->page().local_traversable()->set_force_dark_settings(enabled, move(per_host));
}
+void ConnectionFromClient::set_force_dark_for_local_roots(Web::PageId page_id, bool enabled)
+{
+ if (auto page = this->page(page_id); page.has_value() && !page->page().has_local_traversable())
+ page->page().set_force_dark_for_local_roots(enabled);
+}
+
void ConnectionFromClient::set_autoplay_settings(Web::PageId, Web::HTML::AutoplayPolicy policy, OrderedHashMap<String, Web::HTML::AutoplayPolicy> site_policies)
{
Web::HTML::AutoplaySettings::the().set_policy(policy, site_policies);
diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h
index 3dd0b443f4b..2bb745057a4 100644
--- a/Services/WebContent/ConnectionFromClient.h
+++ b/Services/WebContent/ConnectionFromClient.h
@@ -252,6 +252,7 @@ private:
virtual void set_content_blockers(Core::AnonymousBuffer patterns) override;
virtual void set_force_dark_settings(Web::PageId page_id, bool enabled, HashMap<String, bool> per_host) override;
+ virtual void set_force_dark_for_local_roots(Web::PageId page_id, bool enabled) override;
virtual void set_autoplay_settings(Web::PageId page_id, Web::HTML::AutoplayPolicy policy, OrderedHashMap<String, Web::HTML::AutoplayPolicy> site_policies) override;
virtual void set_preferred_color_scheme(Web::PageId page_id, Web::CSS::PreferredColorScheme) override;
virtual void set_preferred_contrast(Web::PageId page_id, Web::CSS::PreferredContrast) override;
diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc
index 7ee6c79f6b7..11746fbd16e 100644
--- a/Services/WebContent/WebContentServer.ipc
+++ b/Services/WebContent/WebContentServer.ipc
@@ -261,6 +261,7 @@ endpoint WebContentServer routed by Web::PageId page_id
set_content_blockers(Core::AnonymousBuffer patterns) =|
set_force_dark_settings(Web::PageId page_id, bool enabled, HashMap<String, bool> per_host) =|
+ set_force_dark_for_local_roots(Web::PageId page_id, bool enabled) =|
set_autoplay_settings(Web::PageId page_id, Web::HTML::AutoplayPolicy policy, OrderedHashMap<String, Web::HTML::AutoplayPolicy> site_policies) =|
set_preferred_color_scheme(Web::PageId page_id, Web::CSS::PreferredColorScheme color_scheme) =|
set_preferred_contrast(Web::PageId page_id, Web::CSS::PreferredContrast contrast) =|
diff --git a/Tests/LibWebView/test-webdriver-force-dark.py b/Tests/LibWebView/test-webdriver-force-dark.py
index 5c2c4d026fa..0a918c41395 100755
--- a/Tests/LibWebView/test-webdriver-force-dark.py
+++ b/Tests/LibWebView/test-webdriver-force-dark.py
@@ -30,10 +30,17 @@ class Handler(http.server.BaseHTTPRequestHandler):
self.send_header("Content-Type", "text/html")
self.send_header("Cache-Control", "no-store")
self.end_headers()
+ if self.path == "/frame":
+ self.wfile.write(b"<!doctype html><body style='background: white'>")
+ return
+ # The second iframe is from the other host, so it is cross-site and loads in a process of its own.
+ host, port = self.headers["Host"].rsplit(":", 1)
+ other_host = "localhost" if host == "127.0.0.1" else "127.0.0.1"
self.wfile.write(
b"<!doctype html><title>Force-dark test</title><body style='background: white'>"
b"<script>window.initialDark = matchMedia('(prefers-color-scheme: dark)').matches;</script>"
b"<iframe srcdoc=\"<body style='background:white'>\" style='position:absolute;left:100px;top:100px;border:0'></iframe>"
+ + f"<iframe src='http://{other_host}:{port}/frame' style='position:absolute;left:400px;top:100px;border:0'></iframe>".encode()
)
def log_message(self, format, *args):
@@ -129,8 +136,13 @@ def run_test(default):
screenshot = base64.b64decode(payload["value"])
color = png_pixel(screenshot, 10, 10)
iframe_color = png_pixel(screenshot, 110, 110)
+ cross_site_iframe_color = png_pixel(screenshot, 410, 110)
assert (max(color) < 128) == expected, f"{description} expected dark={expected}, got {color}"
assert (max(iframe_color) < 128) == expected, f"{description} iframe painted {iframe_color}"
+ # The tab's top-level host decides for the cross-site iframe too, whatever its own host's setting.
+ assert (max(cross_site_iframe_color) < 128) == expected, (
+ f"{description} cross-site iframe painted {cross_site_iframe_color}"
+ )
status, payload, raw = command(
"POST",
"execute/sync",