Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Ionel Popescu | 237e8e3 | 2019-08-07 19:50:54 | [diff] [blame] | 2 | // 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 Sanders | de533fce | 2021-12-15 14:59:43 | [diff] [blame] | 7 | #include "base/no_destructor.h" |
Giovanni Ortuño Urquidi | 943f590 | 2023-10-16 01:17:19 | [diff] [blame] | 8 | #include "build/build_config.h" |
Ionel Popescu | 237e8e3 | 2019-08-07 19:50:54 | [diff] [blame] | 9 | #include "content/browser/renderer_host/render_process_host_impl.h" |
| 10 | #include "content/common/renderer.mojom.h" |
Ionel Popescu | 237e8e3 | 2019-08-07 19:50:54 | [diff] [blame] | 11 | |
| 12 | namespace content { |
| 13 | |
| 14 | // static |
| 15 | ThemeHelper* ThemeHelper::GetInstance() { |
| 16 | static base::NoDestructor<ThemeHelper> s_theme_helper; |
| 17 | return s_theme_helper.get(); |
| 18 | } |
| 19 | |
Sigurdur Asgeirsson | 103e2fb | 2020-11-10 00:50:16 | [diff] [blame] | 20 | ThemeHelper::ThemeHelper() : theme_observation_(this) { |
| 21 | theme_observation_.Observe(ui::NativeTheme::GetInstanceForWeb()); |
Ionel Popescu | 237e8e3 | 2019-08-07 19:50:54 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | ThemeHelper::~ThemeHelper() {} |
| 25 | |
| 26 | mojom::UpdateSystemColorInfoParamsPtr MakeUpdateSystemColorInfoParams( |
| 27 | ui::NativeTheme* native_theme) { |
| 28 | mojom::UpdateSystemColorInfoParamsPtr params = |
| 29 | mojom::UpdateSystemColorInfoParams::New(); |
Tau Gärtli | 4e1c63a | 2025-08-11 18:41:52 | [diff] [blame] | 30 | #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) |
Giovanni Ortuño Urquidi | 943f590 | 2023-10-16 01:17:19 | [diff] [blame] | 31 | params->accent_color = native_theme->user_color(); |
| 32 | #endif |
| 33 | |
Ionel Popescu | 237e8e3 | 2019-08-07 19:50:54 | [diff] [blame] | 34 | return params; |
| 35 | } |
| 36 | |
| 37 | void ThemeHelper::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) { |
Sigurdur Asgeirsson | 103e2fb | 2020-11-10 00:50:16 | [diff] [blame] | 38 | DCHECK(theme_observation_.IsObservingSource(observed_theme)); |
Ionel Popescu | 237e8e3 | 2019-08-07 19:50:54 | [diff] [blame] | 39 | |
| 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 | |
| 51 | void 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 |