Ionel Popescu | 237e8e3 | 2019-08-07 19:50:54 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 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 | |
| 7 | #include "content/browser/renderer_host/render_process_host_impl.h" |
| 8 | #include "content/common/renderer.mojom.h" |
| 9 | |
| 10 | namespace content { |
| 11 | |
| 12 | // static |
| 13 | ThemeHelper* ThemeHelper::GetInstance() { |
| 14 | static base::NoDestructor<ThemeHelper> s_theme_helper; |
| 15 | return s_theme_helper.get(); |
| 16 | } |
| 17 | |
| 18 | ThemeHelper::ThemeHelper() : theme_observer_(this) { |
| 19 | theme_observer_.Add(ui::NativeTheme::GetInstanceForWeb()); |
| 20 | } |
| 21 | |
| 22 | ThemeHelper::~ThemeHelper() {} |
| 23 | |
| 24 | mojom::UpdateSystemColorInfoParamsPtr MakeUpdateSystemColorInfoParams( |
| 25 | ui::NativeTheme* native_theme) { |
| 26 | mojom::UpdateSystemColorInfoParamsPtr params = |
| 27 | mojom::UpdateSystemColorInfoParams::New(); |
| 28 | params->is_dark_mode = native_theme->ShouldUseDarkColors(); |
| 29 | params->is_high_contrast = native_theme->UsesHighContrastColors(); |
| 30 | params->preferred_color_scheme = native_theme->GetPreferredColorScheme(); |
| 31 | const auto& colors = native_theme->GetSystemColors(); |
| 32 | params->colors.insert(colors.begin(), colors.end()); |
| 33 | |
| 34 | return params; |
| 35 | } |
| 36 | |
| 37 | void ThemeHelper::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) { |
| 38 | DCHECK(theme_observer_.IsObserving(observed_theme)); |
| 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 |