blob: 935002e9621c7a791f0a25c87b6fd571a22607ed [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2019 The Chromium Authors
Ionel Popescu237e8e32019-08-07 19:50:542// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/theme_helper.h"
6
David Sandersde533fce2021-12-15 14:59:437#include "base/no_destructor.h"
Giovanni Ortuño Urquidi943f5902023-10-16 01:17:198#include "build/build_config.h"
Ionel Popescu237e8e32019-08-07 19:50:549#include "content/browser/renderer_host/render_process_host_impl.h"
10#include "content/common/renderer.mojom.h"
Ionel Popescu237e8e32019-08-07 19:50:5411
12namespace content {
13
14// static
15ThemeHelper* ThemeHelper::GetInstance() {
16 static base::NoDestructor<ThemeHelper> s_theme_helper;
17 return s_theme_helper.get();
18}
19
Sigurdur Asgeirsson103e2fb2020-11-10 00:50:1620ThemeHelper::ThemeHelper() : theme_observation_(this) {
21 theme_observation_.Observe(ui::NativeTheme::GetInstanceForWeb());
Ionel Popescu237e8e32019-08-07 19:50:5422}
23
24ThemeHelper::~ThemeHelper() {}
25
26mojom::UpdateSystemColorInfoParamsPtr MakeUpdateSystemColorInfoParams(
27 ui::NativeTheme* native_theme) {
28 mojom::UpdateSystemColorInfoParamsPtr params =
29 mojom::UpdateSystemColorInfoParams::New();
Tau Gärtli4e1c63a2025-08-11 18:41:5230#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
Giovanni Ortuño Urquidi943f5902023-10-16 01:17:1931 params->accent_color = native_theme->user_color();
32#endif
33
Ionel Popescu237e8e32019-08-07 19:50:5434 return params;
35}
36
37void ThemeHelper::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
Sigurdur Asgeirsson103e2fb2020-11-10 00:50:1638 DCHECK(theme_observation_.IsObservingSource(observed_theme));
Ionel Popescu237e8e32019-08-07 19:50:5439
40 mojom::UpdateSystemColorInfoParamsPtr params =
41 MakeUpdateSystemColorInfoParams(observed_theme);
42 for (auto iter = RenderProcessHost::AllHostsIterator(); !iter.IsAtEnd();
43 iter.Advance()) {
44 if (iter.GetCurrentValue()->IsInitializedAndNotDead()) {
45 iter.GetCurrentValue()->GetRendererInterface()->UpdateSystemColorInfo(
46 params->Clone());
47 }
48 }
49}
50
51void ThemeHelper::SendSystemColorInfo(mojom::Renderer* renderer) const {
52 mojom::UpdateSystemColorInfoParamsPtr params =
53 MakeUpdateSystemColorInfoParams(ui::NativeTheme::GetInstanceForWeb());
54 renderer->UpdateSystemColorInfo(std::move(params));
55}
56
57} // namespace content