Reland "Set forced colors based on NativeTheme"
This is a reland of d1b4afb89b58ab8d606cae268d511a03549ebda2
Original change :
https://chromium-review.googlesource.com/c/chromium/src/+/1653408/18
Revert of original change :
https://chromium-review.googlesource.com/c/chromium/src/+/1682277
---
The original change was reverted due to a compilation error in the
following files:
- local_ntp_backgrounds_browsertest.cc
- local_ntp_browsertest.cc
The reason being that a recent change to these files had added calls
to NativeTheme::SetDarkModeParent(), which was a function that had been
removed by this change.
Since the two changes were submitted around the same time, this
compilation error was not picked up until after the change was merged.
---
In order to fix the compilation errors,
NativeTheme::SetDarkModeParent() was re-added to NativeTheme. A follow-
up change will be made to remove SetDarkModeParent() and
DarkModeObserver.
As per additional feedback, NativeTheme was updated to have-an
observer rather than is-an observer. This involved removing
the implementation of NativeThemeObserver directly by NativeTheme
and adding an inner-class (ColorSchemeNativeThemeObserver) to
implement NativeThemeObserver. This new inner-class now handles
both dark mode and high contrast changes.
---
Original change's description:
> Set forced colors based on NativeTheme
>
> Pass the state of high contrast from NativeTheme to the renderer and
> update the forced colors enum based on its value dynamically. This
> is used to evaluate the forced-colors media query.
>
> This change also removes the use of DarkModeObserver in WebContentsImpl
> and NativeTheme in preference to NativeThemeObserver. WebContentsImpl
> and NativeTheme now implement NativeThemeObserver to track changes
> to both dark mode and high contrast.
>
> Bug: 970285
> Change-Id: Id83abb902358916f514cf972876e1798e56b72ad
> Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1653408
> Commit-Queue: Alison Maher <[email protected]>
> Reviewed-by: Kinuko Yasuda <[email protected]>
> Reviewed-by: Peter Kasting <[email protected]>
> Reviewed-by: Kevin Babbitt <[email protected]>
> Reviewed-by: Rune Lillesveen <[email protected]>
> Cr-Commit-Position: refs/heads/master@{#673531}
[email protected],[email protected]
Bug: 970285
Change-Id: I816cb440cae33baa5492a91c2ed20c50f0c10a66
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1684132
Reviewed-by: Kevin Babbitt <[email protected]>
Reviewed-by: Kinuko Yasuda <[email protected]>
Reviewed-by: Elly Fong-Jones <[email protected]>
Commit-Queue: Alison Maher <[email protected]>
Cr-Commit-Position: refs/heads/master@{#675295}
diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
index 37e9397c..2cc8cae9 100644
--- a/content/browser/web_contents/web_contents_impl.cc
+++ b/content/browser/web_contents/web_contents_impl.cc
@@ -591,6 +591,7 @@
is_overlay_content_(false),
showing_context_menu_(false),
text_autosizer_page_info_({0, 0, 1.f}),
+ native_theme_observer_(this),
had_inner_webcontents_(false),
loading_weak_factory_(this),
weak_factory_(this) {
@@ -613,7 +614,10 @@
registry_.AddInterface(base::BindRepeating(
&WebContentsImpl::OnColorChooserFactoryRequest, base::Unretained(this)));
- dark_mode_observer_.Start();
+ ui::NativeTheme* native_theme = ui::NativeTheme::GetInstanceForWeb();
+ native_theme_observer_.Add(native_theme);
+ in_high_contrast_ = native_theme->UsesHighContrastColors();
+ in_dark_mode_ = native_theme->SystemDarkModeEnabled();
}
WebContentsImpl::~WebContentsImpl() {
@@ -7226,8 +7230,24 @@
GetMainFrame()->SetVisibilityForChildViews(visible);
}
-void WebContentsImpl::OnDarkModeChanged(bool dark_mode) {
- NotifyPreferencesChanged();
+void WebContentsImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
+ DCHECK(native_theme_observer_.IsObserving(observed_theme));
+
+ bool in_dark_mode = observed_theme->SystemDarkModeEnabled();
+ bool in_high_contrast = observed_theme->UsesHighContrastColors();
+ bool preferences_changed = false;
+
+ if (in_dark_mode_ != in_dark_mode) {
+ in_dark_mode_ = in_dark_mode;
+ preferences_changed = true;
+ }
+ if (in_high_contrast_ != in_high_contrast) {
+ in_high_contrast_ = in_high_contrast;
+ preferences_changed = true;
+ }
+
+ if (preferences_changed)
+ NotifyPreferencesChanged();
}
} // namespace content