Skip to content

Truly independent web browser

LibWeb: Replicate the used color scheme of a navigable's container

Andreas Klinga685df315a81
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index f907d1ae79b..4ef2bfaf908 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -1567,7 +1567,14 @@ ReplicatedContainerState LocalNavigable::container_state() const
void LocalNavigable::set_root_container_state(ReplicatedContainerState state)
{
VERIFY(is_local_root() && parent());
+ auto used_color_scheme_changed = m_root_container_state.used_color_scheme != state.used_color_scheme;
m_root_container_state = move(state);
+
+ // The container's used color scheme decides whether the canvas is opaque.
+ if (used_color_scheme_changed && m_active_document) {
+ m_needs_to_record_display_list = true;
+ m_active_document->set_needs_repaint(Badge<HTML::LocalNavigable> {}, InvalidateDisplayList::No);
+ }
}
void LocalNavigable::set_closing(bool value)
diff --git a/Libraries/LibWeb/HTML/NavigableContainer.cpp b/Libraries/LibWeb/HTML/NavigableContainer.cpp
index d7f06f706ac..5cb3f059906 100644
--- a/Libraries/LibWeb/HTML/NavigableContainer.cpp
+++ b/Libraries/LibWeb/HTML/NavigableContainer.cpp
@@ -548,6 +548,8 @@ void NavigableContainer::report_content_navigable_viewport_rect(Layout::BegunRea
if (!document().layout_is_up_to_date())
return;
auto const* layout_node = this->layout_node(read);
+ if (layout_node)
+ report_used_color_scheme(layout_node->color_scheme());
if (!layout_node || !Painting::is_navigable_container_viewport_paintable(*layout_node)) {
if (Core::StatsLog::the())
report_stats_displayed(false);
@@ -611,6 +613,18 @@ void NavigableContainer::report_content_navigable_viewport_rect(Layout::BegunRea
page.client().page_did_update_child_frame_viewport(m_content_navigable->id(), reported.rect, reported.intersection);
}
+// A content navigable whose document another process hosts decides whether its canvas is opaque by the container's
+// used color scheme, which it reads through its replicated state.
+void NavigableContainer::report_used_color_scheme(CSS::PreferredColorScheme color_scheme)
+{
+ if (color_scheme == CSS::PreferredColorScheme::Auto)
+ color_scheme = CSS::PreferredColorScheme::Light;
+ if (m_used_color_scheme == color_scheme)
+ return;
+ m_used_color_scheme = color_scheme;
+ document().page().client().page_did_change_navigable_container_state(m_content_navigable->id(), replicated_container_state());
+}
+
ReplicatedContainerState NavigableContainer::replicated_container_state()
{
ReplicatedContainerState state;
@@ -626,6 +640,7 @@ ReplicatedContainerState NavigableContainer::replicated_container_state()
auto document_policy_container = document().policy_container();
state.document_insecure_requests_policy = document_policy_container->insecure_requests_policy();
state.document_upgrade_insecure_navigations_set = document_policy_container->upgrade_insecure_navigations_set();
+ state.used_color_scheme = m_used_color_scheme;
return state;
}
diff --git a/Libraries/LibWeb/HTML/NavigableContainer.h b/Libraries/LibWeb/HTML/NavigableContainer.h
index 90e7ff68b0c..0b7b5c60dc1 100644
--- a/Libraries/LibWeb/HTML/NavigableContainer.h
+++ b/Libraries/LibWeb/HTML/NavigableContainer.h
@@ -99,6 +99,9 @@ private:
bool operator==(ReportedContentNavigableViewport const&) const = default;
};
Optional<ReportedContentNavigableViewport> m_reported_content_navigable_viewport;
+
+ void report_used_color_scheme(CSS::PreferredColorScheme);
+ CSS::PreferredColorScheme m_used_color_scheme { CSS::PreferredColorScheme::Light };
};
}
diff --git a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
index f79a66b7076..609cf92c07a 100644
--- a/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
+++ b/Libraries/LibWeb/Painting/PaintingRustBridge.cpp
@@ -642,6 +642,9 @@ Optional<DisplayListRecording> start_rust_display_list_recording(Layout::BegunRe
container_scheme = CSS::PreferredColorScheme::Light;
opaque_canvas = container_scheme != color_scheme;
}
+ } else if (document.navigable()->parent()) {
+ // NB: The container of a local root lives in another process, which replicates its used color scheme.
+ opaque_canvas = document.navigable()->container_state().used_color_scheme != color_scheme;
}
inputs.canvas_fill_rect = config.canvas_fill_rect;
inputs.canvas_color = CSS::SystemColor::canvas(color_scheme);
diff --git a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp
index a5c42d75959..e7df3f90b02 100644
--- a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp
+++ b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.cpp
@@ -24,9 +24,19 @@ ErrorOr<void> encode(Encoder& encoder, Web::HTML::ReplicatedContainerState const
TRY(Web::PermissionsPolicy::encode_policy_directive(encoder, state.container_policy));
TRY(encoder.encode(state.document_insecure_requests_policy));
TRY(encoder.encode(state.document_upgrade_insecure_navigations_set));
+ TRY(encoder.encode(to_underlying(state.used_color_scheme)));
return {};
}
+// A container's used color scheme is light or dark, never auto.
+static ErrorOr<Web::CSS::PreferredColorScheme> decode_used_color_scheme(Decoder& decoder)
+{
+ auto value = static_cast<Web::CSS::PreferredColorScheme>(TRY(decoder.decode<UnderlyingType<Web::CSS::PreferredColorScheme>>()));
+ if (value != Web::CSS::PreferredColorScheme::Light && value != Web::CSS::PreferredColorScheme::Dark)
+ return Error::from_string_literal("Invalid used color scheme");
+ return value;
+}
+
template<>
ErrorOr<Web::HTML::ReplicatedContainerState> decode(Decoder& decoder)
{
@@ -41,6 +51,7 @@ ErrorOr<Web::HTML::ReplicatedContainerState> decode(Decoder& decoder)
.container_policy = TRY(Web::PermissionsPolicy::decode_policy_directive(decoder)),
.document_insecure_requests_policy = TRY(decoder.decode<Web::UpgradeInsecureRequests::InsecureRequestsPolicy>()),
.document_upgrade_insecure_navigations_set = TRY(decoder.decode<Web::UpgradeInsecureRequests::UpgradeInsecureNavigationsSet>()),
+ .used_color_scheme = TRY(decode_used_color_scheme(decoder)),
};
}
diff --git a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h
index ce454bbfd1b..be24d9c6556 100644
--- a/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h
+++ b/Libraries/LibWebCommon/HTML/ReplicatedNavigableState.h
@@ -12,6 +12,7 @@
#include <LibIPC/Forward.h>
#include <LibURL/Origin.h>
#include <LibURL/URL.h>
+#include <LibWebCommon/CSS/PreferredColorScheme.h>
#include <LibWebCommon/Export.h>
#include <LibWebCommon/HTML/CrossOrigin/OpenerPolicy.h>
#include <LibWebCommon/HTML/CrossProcessId.h>
@@ -60,6 +61,10 @@ struct ReplicatedContainerState {
// https://w3c.github.io/webappsec-upgrade-insecure-requests/#upgrade-insecure-navigations-set, of the container's
// node document's policy container.
UpgradeInsecureRequests::UpgradeInsecureNavigationsSet document_upgrade_insecure_navigations_set {};
+
+ // https://drafts.csswg.org/css-color-adjust-1/#color-scheme-effect
+ // The container's used color scheme, which decides whether the canvas of the navigable's document is opaque.
+ CSS::PreferredColorScheme used_color_scheme { CSS::PreferredColorScheme::Light };
};
// What the process hosting a navigable's active document reports of it to the UI process, which composes the
diff --git a/Libraries/LibWebView/CanonicalNavigable.cpp b/Libraries/LibWebView/CanonicalNavigable.cpp
index 57b2bf42c55..10dae66c194 100644
--- a/Libraries/LibWebView/CanonicalNavigable.cpp
+++ b/Libraries/LibWebView/CanonicalNavigable.cpp
@@ -1092,6 +1092,9 @@ void CanonicalNavigable::abandon_populated_document(NonnullRefPtr<CanonicalDocum
void CanonicalNavigable::place_populated_document(CanonicalDocument& document, WebContentPage& page)
{
document.set_host(page);
+ // A provisional host snapshots its container before population. Refresh it before giving that host a viewport.
+ if (parent() && m_hosted_state.has_value())
+ page.async_update_local_root_container_state(id(), m_hosted_state->container);
if (auto* bidi = WebDriverBiDi::the())
bidi->push_session_state_for_client(page.client());
send_viewport_to_host();
@@ -1212,8 +1215,14 @@ void CanonicalNavigable::update_container_state(Web::HTML::ReplicatedContainerSt
state.document_permissions_policy = m_container_document->permissions_policy();
}
m_hosted_state->container = state;
- if (has_remote_host())
- remote_host().async_update_local_root_container_state(id(), move(state));
+ RefPtr<WebContentPage> active_host = has_remote_host() ? &remote_host() : nullptr;
+ if (active_host)
+ active_host->async_update_local_root_container_state(id(), state);
+ // Viewport updates also reach provisional hosts. Keep their container snapshot current before they activate.
+ for_each_pending_host([&](WebContentPage& host) {
+ if (&host != active_host.ptr())
+ host.async_update_local_root_container_state(id(), state);
+ });
send_replicated_state();
}
diff --git a/Tests/LibWeb/Ref/expected/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas-ref.html b/Tests/LibWeb/Ref/expected/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas-ref.html
new file mode 100644
index 00000000000..93478d201b8
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas-ref.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; background: green; }
+ div { width: 100px; height: 100px; background: white; }
+</style>
+<div></div>
diff --git a/Tests/LibWeb/Ref/input/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas.html b/Tests/LibWeb/Ref/input/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas.html
new file mode 100644
index 00000000000..c7f0fabd666
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas.html
@@ -0,0 +1,54 @@
+<!doctype html>
+<html class="reftest-wait">
+ <link rel="match" href="../expected/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas-ref.html" />
+ <style>
+ body {
+ margin: 0;
+ background: green;
+ }
+ iframe {
+ color-scheme: dark;
+ display: block;
+ width: 100px;
+ height: 100px;
+ border: 0;
+ }
+ </style>
+ <script>
+ // The frame's document is cross-site, so another process hosts it. Its used color scheme is light while the
+ // frame's is dark, so its canvas is opaque in the light Canvas color. The frame's process learns the frame's
+ // used color scheme from a rendering update of this page, so the frame is told to report only after one.
+ const request = new XMLHttpRequest();
+ const path = `/echo/isolated-iframe-opaque-canvas-${crypto.randomUUID()}`;
+ request.open("POST", "/echo", false);
+ request.setRequestHeader("Content-Type", "application/json");
+ request.send(
+ JSON.stringify({
+ method: "GET",
+ path,
+ status: 200,
+ headers: { "Content-Type": "text/html" },
+ body: `<!DOCTYPE html><style>html, body { margin: 0; height: 100%; }</style>
+ <script>window.addEventListener("message", () => requestAnimationFrame(() => requestAnimationFrame(async () => {
+ const nextTask = () => new Promise(resolve => {
+ const channel = new MessageChannel();
+ channel.port1.onmessage = resolve;
+ channel.port2.postMessage(null);
+ });
+ do await nextTask();
+ while (internals.frameSchedulerState() !== "idle");
+ internals.synchronizeCompositor();
+ parent.postMessage("painted", "*");
+ })));<\/script>`,
+ })
+ );
+
+ const host = `oop-iframe-opaque-canvas-${crypto.randomUUID()}.localhost`;
+ window.addEventListener("message", () => document.documentElement.classList.remove("reftest-wait"));
+ document.write(`<iframe src="http://${host}:${location.port}${path}"></iframe>`);
+ const iframe = document.querySelector("iframe");
+ iframe.addEventListener("load", () => {
+ requestAnimationFrame(() => requestAnimationFrame(() => iframe.contentWindow.postMessage("paint", "*")));
+ });
+ </script>
+</html>
diff --git a/Tests/LibWeb/Ref/input/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas.html.headers b/Tests/LibWeb/Ref/input/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas.html.headers
new file mode 100644
index 00000000000..156209f9c81
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/isolated-iframe-with-a-different-color-scheme-has-an-opaque-canvas.html.headers
@@ -0,0 +1 @@
+Content-Type: text/html