0

Use in_forced_colors web setting in Blink

This CL uses the `in_forced_colors` web setting introduced in
CL:5324688 within Blink. The web setting was introduced to remove the
reliance on the global NativeTheme web instance for the state of forced
colors in Blink.

More specifically, this change:
1.) replaces all calls to the WebThemeEngine::GetForcedColors with the
web setting
2.) copies the setting in components that require it such as svg_image
and web_page_popup
3.) updates the kForcedHighContrast switch to override the web setting
for the content shell and web tests

Bug: 1231644, 41489514
Change-Id: I04ee8c3d9e55f271728f27ed915da6f2255f87bb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5368321
Reviewed-by: danakj <danakj@chromium.org>
Commit-Queue: Sam Davis Omekara <samomekarajr@microsoft.com>
Reviewed-by: Alison Maher <almaher@microsoft.com>
Cr-Commit-Position: refs/heads/main@{#1277687}
This commit is contained in:
Sam Davis Omekara
2024-03-25 16:08:19 +00:00
committed by Chromium LUCI CQ
parent 470b1f55df
commit 79a90bd7ef
16 changed files with 50 additions and 73 deletions

@ -560,8 +560,10 @@ void ShellContentBrowserClient::OverrideWebkitPrefs(
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kForceHighContrast)) {
prefs->in_forced_colors = true;
prefs->preferred_contrast = blink::mojom::PreferredContrast::kMore;
} else {
prefs->in_forced_colors = false;
prefs->preferred_contrast = blink::mojom::PreferredContrast::kNoPreference;
}

@ -816,8 +816,10 @@ void WebTestControlHost::OverrideWebkitPrefs(
if (base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kForceHighContrast)) {
prefs->in_forced_colors = true;
prefs->preferred_contrast = blink::mojom::PreferredContrast::kMore;
} else {
prefs->in_forced_colors = false;
prefs->preferred_contrast = blink::mojom::PreferredContrast::kNoPreference;
}
}

@ -5,7 +5,6 @@
#include "third_party/blink/renderer/core/css/media_values.h"
#include "third_party/blink/public/common/css/scripting.h"
#include "third_party/blink/public/platform/web_theme_engine.h"
#include "third_party/blink/renderer/core/css/css_resolution_units.h"
#include "third_party/blink/renderer/core/css/css_to_length_conversion_data.h"
#include "third_party/blink/renderer/core/css/media_feature_overrides.h"
@ -29,7 +28,6 @@
#include "third_party/blink/renderer/core/preferences/preference_overrides.h"
#include "third_party/blink/renderer/platform/graphics/color_space_gamut.h"
#include "third_party/blink/renderer/platform/network/network_state_notifier.h"
#include "third_party/blink/renderer/platform/theme/web_theme_engine_helper.h"
#include "third_party/blink/renderer/platform/widget/frame_widget.h"
#include "ui/display/screen_info.h"
@ -497,7 +495,7 @@ ForcedColors MediaValues::CalculateForcedColors(LocalFrame* frame) {
std::optional<ForcedColors> override_value =
overrides ? overrides->GetForcedColors() : std::nullopt;
return override_value.value_or(
WebThemeEngineHelper::GetNativeThemeEngine()->GetForcedColors());
frame->GetDocument()->GetStyleEngine().GetForcedColors());
}
NavigationControls MediaValues::CalculateNavigationControls(LocalFrame* frame) {

@ -35,7 +35,6 @@
#include "base/ranges/algorithm.h"
#include "third_party/blink/public/mojom/frame/color_scheme.mojom-blink.h"
#include "third_party/blink/public/mojom/timing/resource_timing.mojom-blink.h"
#include "third_party/blink/public/platform/web_theme_engine.h"
#include "third_party/blink/renderer/core/css/cascade_layer_map.h"
#include "third_party/blink/renderer/core/css/check_pseudo_has_cache_scope.h"
#include "third_party/blink/renderer/core/css/container_query_data.h"
@ -117,7 +116,6 @@
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/instrumentation/histogram.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/theme/web_theme_engine_helper.h"
#include "third_party/blink/renderer/platform/weborigin/security_origin.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"
#include "third_party/blink/renderer/platform/wtf/wtf_size_t.h"
@ -242,11 +240,12 @@ StyleEngine::StyleEngine(Document& document)
document.GetSettings()->GetForceDarkModeEnabled();
UpdateColorSchemeMetrics();
forced_colors_ = document.GetSettings()->GetInForcedColors()
? ForcedColors::kActive
: ForcedColors::kNone;
UpdateForcedBackgroundColor();
}
forced_colors_ =
WebThemeEngineHelper::GetNativeThemeEngine()->GetForcedColors();
UpdateColorScheme();
// Mostly for the benefit of unit tests.
@ -3941,13 +3940,13 @@ bool StyleEngine::SupportsDarkColorScheme() {
void StyleEngine::UpdateColorScheme() {
auto* settings = GetDocument().GetSettings();
auto* web_theme_engine = WebThemeEngineHelper::GetNativeThemeEngine();
if (!settings || !web_theme_engine) {
if (!settings) {
return;
}
ForcedColors old_forced_colors = forced_colors_;
forced_colors_ = web_theme_engine->GetForcedColors();
forced_colors_ = settings->GetInForcedColors() ? ForcedColors::kActive
: ForcedColors::kNone;
mojom::blink::PreferredColorScheme old_preferred_color_scheme =
preferred_color_scheme_;

@ -1965,7 +1965,7 @@ TEST_F(StyleEngineTest, MediaQueriesChangeForcedColors) {
GetCSSPropertyColor()));
ColorSchemeHelper color_scheme_helper(GetDocument());
color_scheme_helper.SetForcedColors(GetDocument(), ForcedColors::kActive);
color_scheme_helper.SetInForcedColors(/*in_forced_colors=*/true);
UpdateAllLifecyclePhases();
EXPECT_EQ(Color::FromRGB(0, 128, 0),
GetDocument().body()->GetComputedStyle()->VisitedDependentColor(
@ -1995,9 +1995,9 @@ TEST_F(StyleEngineTest, MediaQueriesChangeForcedColorsAndPreferredColorScheme) {
<body></body>
)HTML");
// ForcedColors = kNone, PreferredColorScheme = kLight
// InForcedColors = false, PreferredColorScheme = kLight
ColorSchemeHelper color_scheme_helper(GetDocument());
color_scheme_helper.SetForcedColors(GetDocument(), ForcedColors::kNone);
color_scheme_helper.SetInForcedColors(/*in_forced_colors=*/false);
color_scheme_helper.SetPreferredColorScheme(
mojom::blink::PreferredColorScheme::kLight);
UpdateAllLifecyclePhases();
@ -2013,14 +2013,14 @@ TEST_F(StyleEngineTest, MediaQueriesChangeForcedColorsAndPreferredColorScheme) {
GetDocument().body()->GetComputedStyle()->VisitedDependentColor(
GetCSSPropertyColor()));
// ForcedColors = kActive, PreferredColorScheme = kDark
color_scheme_helper.SetForcedColors(GetDocument(), ForcedColors::kActive);
// InForcedColors = true, PreferredColorScheme = kDark
color_scheme_helper.SetInForcedColors(/*in_forced_colors=*/true);
UpdateAllLifecyclePhases();
EXPECT_EQ(Color::FromRGB(255, 165, 0),
GetDocument().body()->GetComputedStyle()->VisitedDependentColor(
GetCSSPropertyColor()));
// ForcedColors = kActive, PreferredColorScheme = kLight
// InForcedColors = true, PreferredColorScheme = kLight
color_scheme_helper.SetPreferredColorScheme(
mojom::blink::PreferredColorScheme::kLight);
UpdateAllLifecyclePhases();
@ -2904,7 +2904,7 @@ TEST_F(StyleEngineTest, ColorSchemeBaseBackgroundChange) {
EXPECT_EQ(Color(0x12, 0x12, 0x12),
GetDocument().View()->BaseBackgroundColor());
color_scheme_helper.SetForcedColors(GetDocument(), ForcedColors::kActive);
color_scheme_helper.SetInForcedColors(/*in_forced_colors=*/true);
UpdateAllLifecyclePhases();
mojom::blink::ColorScheme color_scheme = mojom::blink::ColorScheme::kLight;
Color system_background_color = LayoutTheme::GetTheme().SystemColor(
@ -3517,7 +3517,7 @@ TEST_F(StyleEngineTest, NoRevertUseCountForForcedColors) {
EXPECT_EQ("rgb(255, 0, 0)", ComputedValue(elem, "color")->CssText());
ColorSchemeHelper color_scheme_helper(GetDocument());
color_scheme_helper.SetForcedColors(GetDocument(), ForcedColors::kActive);
color_scheme_helper.SetInForcedColors(/*in_forced_colors=*/true);
UpdateAllLifecyclePhases();
EXPECT_EQ(ComputedValue(ref, "color")->CssText(),
ComputedValue(elem, "color")->CssText());
@ -7426,7 +7426,7 @@ TEST_F(StyleEngineTest, EnsureAppRegionTriggersRelayout) {
TEST_F(StyleEngineTest, ForcedColorsLightDark) {
ScopedForcedColorsForTest scoped_feature(true);
ColorSchemeHelper color_scheme_helper(GetDocument());
color_scheme_helper.SetForcedColors(GetDocument(), ForcedColors::kActive);
color_scheme_helper.SetInForcedColors(/*in_forced_colors=*/true);
GetDocument().body()->setInnerHTML(R"HTML(
<style>
:root { color-scheme: light only; }

@ -75,7 +75,6 @@
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/public/platform/web_content_settings_client.h"
#include "third_party/blink/public/platform/web_theme_engine.h"
#include "third_party/blink/public/web/web_link_preview_triggerer.h"
#include "third_party/blink/public/web/web_print_page_description.h"
#include "third_party/blink/renderer/bindings/core/v8/frozen_array.h"
@ -366,7 +365,6 @@
#include "third_party/blink/renderer/platform/scheduler/public/frame_or_worker_scheduler.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/text/platform_locale.h"
#include "third_party/blink/renderer/platform/theme/web_theme_engine_helper.h"
#include "third_party/blink/renderer/platform/web_test_support.h"
#include "third_party/blink/renderer/platform/weborigin/origin_access_entry.h"
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
@ -9420,11 +9418,10 @@ void Document::VisionDeficiencyChanged() {
}
void Document::UpdateForcedColors() {
ForcedColors forced_colors =
RuntimeEnabledFeatures::ForcedColorsEnabled()
? WebThemeEngineHelper::GetNativeThemeEngine()->GetForcedColors()
: ForcedColors::kNone;
in_forced_colors_mode_ = forced_colors != ForcedColors::kNone;
Settings* settings = GetSettings();
if (RuntimeEnabledFeatures::ForcedColorsEnabled() && settings) {
in_forced_colors_mode_ = settings->GetInForcedColors();
}
if (in_forced_colors_mode_)
GetStyleEngine().EnsureUAStyleForForcedColors();
}
@ -9445,12 +9442,8 @@ const ui::ColorProvider* Document::GetColorProviderForPainting(
return nullptr;
}
// TODO(crbug.com/1516529): This should be changed to use
// `in_forced_colors_mode_` once forced colors becomes a web setting in Blink.
return GetPage()->GetColorProviderForPainting(
color_scheme,
WebThemeEngineHelper::GetNativeThemeEngine()->GetForcedColors() !=
ForcedColors::kNone);
return GetPage()->GetColorProviderForPainting(color_scheme,
in_forced_colors_mode_);
}
void Document::CountUse(mojom::WebFeature feature) const {

@ -140,6 +140,7 @@ Page* CreatePage(ChromeClient& chrome_client, WebViewImpl& opener_web_view) {
main_settings.GetPreferredColorScheme());
page->GetSettings().SetForceDarkModeEnabled(
main_settings.GetForceDarkModeEnabled());
page->GetSettings().SetInForcedColors(main_settings.GetInForcedColors());
const MediaFeatureOverrides* media_feature_overrides =
opener_web_view.GetPage()->GetMediaFeatureOverrides();

@ -608,8 +608,7 @@ TEST_F(WebViewTest, SetBaseBackgroundColorWithColorScheme) {
web_view->SetPageBaseBackgroundColor(SK_ColorBLUE);
EXPECT_EQ(Color(0x12, 0x12, 0x12), frame_view->BaseBackgroundColor());
color_scheme_helper.SetForcedColors(*(web_view->GetPage()),
ForcedColors::kActive);
color_scheme_helper.SetInForcedColors(/*in_forced_colors=*/true);
UpdateAllLifecyclePhases();
mojom::blink::ColorScheme color_scheme = mojom::blink::ColorScheme::kLight;
@ -619,8 +618,7 @@ TEST_F(WebViewTest, SetBaseBackgroundColorWithColorScheme) {
color_scheme, /*in_forced_colors=*/true));
EXPECT_EQ(system_background_color, frame_view->BaseBackgroundColor());
color_scheme_helper.SetForcedColors(*(web_view->GetPage()),
ForcedColors::kNone);
color_scheme_helper.SetInForcedColors(/*in_forced_colors=*/false);
UpdateAllLifecyclePhases();
EXPECT_EQ(Color(0x12, 0x12, 0x12), frame_view->BaseBackgroundColor());

@ -131,7 +131,7 @@ std::optional<Color> DefaultForegroundColor(
color_scheme);
case kPseudoIdTargetText:
return LayoutTheme::GetTheme().PlatformTextSearchColor(
false /* active match */, color_scheme,
false /* active match */, document.InForcedColorsMode(), color_scheme,
document.GetColorProviderForPainting(color_scheme));
case kPseudoIdSpellingError:
case kPseudoIdGrammarError:

@ -147,7 +147,6 @@ LayoutTheme& LayoutTheme::GetTheme() {
}
LayoutTheme::LayoutTheme() : has_custom_focus_ring_color_(false) {
UpdateForcedColorsState();
}
ControlPart LayoutTheme::AdjustAppearanceWithAuthorStyle(
@ -508,7 +507,6 @@ void LayoutTheme::AdjustSearchFieldCancelButtonStyle(
ComputedStyleBuilder&) const {}
void LayoutTheme::PlatformColorsDidChange() {
UpdateForcedColorsState();
Page::PlatformColorsChanged();
}
@ -726,12 +724,14 @@ Color LayoutTheme::SystemColorFromColorProvider(
Color LayoutTheme::PlatformTextSearchHighlightColor(
bool active_match,
bool in_forced_colors,
mojom::blink::ColorScheme color_scheme,
const ui::ColorProvider* color_provider) const {
if (active_match) {
if (InForcedColorsMode())
if (in_forced_colors) {
return GetTheme().SystemColor(CSSValueID::kHighlight, color_scheme,
color_provider);
}
return Color(255, 150, 50); // Orange.
}
return Color(255, 255, 0); // Yellow.
@ -739,11 +739,13 @@ Color LayoutTheme::PlatformTextSearchHighlightColor(
Color LayoutTheme::PlatformTextSearchColor(
bool active_match,
bool in_forced_colors,
mojom::blink::ColorScheme color_scheme,
const ui::ColorProvider* color_provider) const {
if (InForcedColorsMode() && active_match)
if (in_forced_colors && active_match) {
return GetTheme().SystemColor(CSSValueID::kHighlighttext, color_scheme,
color_provider);
}
return Color::kBlack;
}
@ -813,12 +815,6 @@ Color LayoutTheme::GetCustomFocusRingColor() const {
return custom_focus_ring_color_;
}
void LayoutTheme::UpdateForcedColorsState() {
in_forced_colors_mode_ =
WebThemeEngineHelper::GetNativeThemeEngine()->GetForcedColors() !=
ForcedColors::kNone;
}
bool LayoutTheme::IsAccentColorCustomized(
mojom::blink::ColorScheme color_scheme) const {
if (!RuntimeEnabledFeatures::CSSSystemAccentColorEnabled()) {

@ -119,9 +119,11 @@ class CORE_EXPORT LayoutTheme : public RefCounted<LayoutTheme> {
// Highlight and text colors for TextMatches.
Color PlatformTextSearchHighlightColor(
bool active_match,
bool in_forced_colors,
mojom::blink::ColorScheme color_scheme,
const ui::ColorProvider* color_provider) const;
Color PlatformTextSearchColor(bool active_match,
bool in_forced_colors,
mojom::blink::ColorScheme color_scheme,
const ui::ColorProvider* color_provider) const;
@ -198,8 +200,6 @@ class CORE_EXPORT LayoutTheme : public RefCounted<LayoutTheme> {
// rendered with enough contrast on the result of GetAccentColorOrDefault.
Color GetAccentColorText(mojom::blink::ColorScheme color_scheme) const;
bool InForcedColorsMode() const { return in_forced_colors_mode_; }
protected:
// The platform selection color.
virtual Color PlatformActiveSelectionBackgroundColor(
@ -263,7 +263,6 @@ class CORE_EXPORT LayoutTheme : public RefCounted<LayoutTheme> {
base::TimeDelta caret_blink_interval_ = base::Milliseconds(500);
bool delegates_menu_list_rendering_ = false;
bool in_forced_colors_mode_ = false;
// This color is expected to be drawn on a semi-transparent overlay,
// making it more transparent than its alpha value indicates.

@ -188,7 +188,7 @@ TextPaintStyle TextPaintStyleForTextMatch(const TextMatchMarker& marker,
const mojom::blink::ColorScheme color_scheme = style.UsedColorScheme();
const Color platform_text_color =
LayoutTheme::GetTheme().PlatformTextSearchColor(
marker.IsActiveMatch(), color_scheme,
marker.IsActiveMatch(), document.InForcedColorsMode(), color_scheme,
document.GetColorProviderForPainting(color_scheme));
// Comparing against the value of the 'color' property doesn't always make
// sense (for example for SVG <text> which paints using 'fill' and 'stroke').
@ -464,6 +464,7 @@ void HighlightPainter::Paint(Phase phase) {
Color color =
LayoutTheme::GetTheme().PlatformTextSearchHighlightColor(
text_match_marker.IsActiveMatch(),
document.InForcedColorsMode(),
originating_style_.UsedColorScheme(),
document.GetColorProviderForPainting(
originating_style_.UsedColorScheme()));

@ -776,6 +776,8 @@ Image::SizeAvailability SVGImage::DataChanged(bool all_data_received) {
// dark/light color schemes.
page->GetSettings().SetPreferredColorScheme(
default_settings.GetPreferredColorScheme());
page->GetSettings().SetInForcedColors(
default_settings.GetInForcedColors());
}
chrome_client_->InitAnimationTimer(page->GetPageScheduler()
->GetAgentGroupScheduler()

@ -4,28 +4,24 @@
#include "third_party/blink/renderer/core/testing/color_scheme_helper.h"
#include "third_party/blink/public/platform/web_theme_engine.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/settings.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/platform/theme/web_theme_engine_helper.h"
namespace blink {
ColorSchemeHelper::ColorSchemeHelper(Document& document)
: settings_(*document.GetSettings()) {
web_theme_engine_ = WebThemeEngineHelper::GetNativeThemeEngine();
default_preferred_color_scheme_ = settings_.GetPreferredColorScheme();
default_preferred_contrast_ = settings_.GetPreferredContrast();
default_forced_colors_ = web_theme_engine_->GetForcedColors();
default_in_forced_colors_ = settings_.GetInForcedColors();
}
ColorSchemeHelper::ColorSchemeHelper(Page& page)
: settings_(page.GetSettings()) {
web_theme_engine_ = WebThemeEngineHelper::GetNativeThemeEngine();
default_preferred_color_scheme_ = settings_.GetPreferredColorScheme();
default_preferred_contrast_ = settings_.GetPreferredContrast();
default_forced_colors_ = web_theme_engine_->GetForcedColors();
default_in_forced_colors_ = settings_.GetInForcedColors();
}
ColorSchemeHelper::~ColorSchemeHelper() {
@ -33,7 +29,7 @@ ColorSchemeHelper::~ColorSchemeHelper() {
// original values.
settings_.SetPreferredColorScheme(default_preferred_color_scheme_);
settings_.SetPreferredContrast(default_preferred_contrast_);
web_theme_engine_->SetForcedColors(default_forced_colors_);
settings_.SetInForcedColors(default_in_forced_colors_);
}
void ColorSchemeHelper::SetPreferredColorScheme(
@ -46,16 +42,8 @@ void ColorSchemeHelper::SetPreferredContrast(
settings_.SetPreferredContrast(preferred_contrast);
}
void ColorSchemeHelper::SetForcedColors(Document& document,
ForcedColors forced_colors) {
web_theme_engine_->SetForcedColors(forced_colors);
document.ColorSchemeChanged();
}
void ColorSchemeHelper::SetForcedColors(Page& page,
ForcedColors forced_colors) {
web_theme_engine_->SetForcedColors(forced_colors);
page.ColorSchemeChanged();
void ColorSchemeHelper::SetInForcedColors(bool in_forced_colors) {
settings_.SetInForcedColors(in_forced_colors);
}
void ColorSchemeHelper::SetEmulatedForcedColors(Document& document,

@ -5,7 +5,6 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_COLOR_SCHEME_HELPER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_COLOR_SCHEME_HELPER_H_
#include "third_party/blink/public/common/css/forced_colors.h"
#include "third_party/blink/public/mojom/css/preferred_color_scheme.mojom-shared.h"
#include "third_party/blink/public/mojom/css/preferred_contrast.mojom-shared.h"
@ -29,8 +28,7 @@ class ColorSchemeHelper {
void SetPreferredColorScheme(
mojom::PreferredColorScheme preferred_color_scheme);
void SetPreferredContrast(mojom::PreferredContrast preferred_contrast);
void SetForcedColors(Document& document, ForcedColors forced_colors);
void SetForcedColors(Page& page, ForcedColors forced_colors);
void SetInForcedColors(bool in_forced_colors);
void SetEmulatedForcedColors(Document& document, bool is_dark_theme);
private:
@ -40,7 +38,7 @@ class ColorSchemeHelper {
mojom::PreferredColorScheme::kLight;
mojom::PreferredContrast default_preferred_contrast_ =
mojom::PreferredContrast::kNoPreference;
ForcedColors default_forced_colors_ = ForcedColors::kNone;
bool default_in_forced_colors_ = false;
};
} // namespace blink

@ -3399,7 +3399,7 @@ void Internals::setForcedColorsAndDarkPreferredColorScheme(Document* document) {
color_scheme_helper_.emplace(*document);
color_scheme_helper_->SetPreferredColorScheme(
mojom::blink::PreferredColorScheme::kDark);
color_scheme_helper_->SetForcedColors(*document, ForcedColors::kActive);
color_scheme_helper_->SetInForcedColors(/*in_forced_colors=*/true);
color_scheme_helper_->SetEmulatedForcedColors(*document,
/*is_dark_theme=*/false);
}