0

Modularize webui_util.h

Several desktop-only methods are moved to webui_util_desktop. These
methods are marked as deprecated, as they use disallowed methods.

Bug: 339497734
Change-Id: I5f58ab882b8325581ec66290f69c98131fb3b6af
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5528814
Commit-Queue: Erik Chen <erikchen@chromium.org>
Reviewed-by: Demetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1299545}
This commit is contained in:
Erik Chen
2024-05-10 21:52:01 +00:00
committed by Chromium LUCI CQ
parent ef59fe6aeb
commit a3bca21ae7
19 changed files with 192 additions and 142 deletions

@ -33,6 +33,7 @@ source_set("test_support") {
"//chrome/browser/ash/system_web_apps/types", "//chrome/browser/ash/system_web_apps/types",
"//chrome/browser/profiles:profile", "//chrome/browser/profiles:profile",
"//chrome/browser/ui", "//chrome/browser/ui",
"//chrome/browser/ui/webui",
"//chrome/browser/web_applications", "//chrome/browser/web_applications",
"//chrome/browser/web_applications:web_applications_test_support", "//chrome/browser/web_applications:web_applications_test_support",
"//components/crx_file", "//components/crx_file",

@ -376,8 +376,6 @@ static_library("ui") {
"webui/version/version_ui.h", "webui/version/version_ui.h",
"webui/webui_load_timer.cc", "webui/webui_load_timer.cc",
"webui/webui_load_timer.h", "webui/webui_load_timer.h",
"webui/webui_util.cc",
"webui/webui_util.h",
"zoom/chrome_zoom_level_otr_delegate.cc", "zoom/chrome_zoom_level_otr_delegate.cc",
"zoom/chrome_zoom_level_otr_delegate.h", "zoom/chrome_zoom_level_otr_delegate.h",
"zoom/chrome_zoom_level_prefs.cc", "zoom/chrome_zoom_level_prefs.cc",
@ -469,6 +467,7 @@ static_library("ui") {
"//chrome/browser/storage_access_api", "//chrome/browser/storage_access_api",
"//chrome/browser/themes", "//chrome/browser/themes",
"//chrome/browser/ui/side_panel:side_panel_enums", "//chrome/browser/ui/side_panel:side_panel_enums",
"//chrome/browser/ui/webui",
"//chrome/browser/ui/webui/data_sharing_internals:mojo_bindings", "//chrome/browser/ui/webui/data_sharing_internals:mojo_bindings",
"//chrome/browser/ui/webui/location_internals:mojo_bindings", "//chrome/browser/ui/webui/location_internals:mojo_bindings",
"//chrome/browser/ui/webui/omnibox:mojo_bindings", "//chrome/browser/ui/webui/omnibox:mojo_bindings",
@ -2045,6 +2044,8 @@ static_library("ui") {
"webui/web_app_internals/web_app_internals_ui.h", "webui/web_app_internals/web_app_internals_ui.h",
"webui/webui_gallery/webui_gallery_ui.cc", "webui/webui_gallery/webui_gallery_ui.cc",
"webui/webui_gallery/webui_gallery_ui.h", "webui/webui_gallery/webui_gallery_ui.h",
"webui/webui_util_desktop.cc",
"webui/webui_util_desktop.h",
"webui/whats_new/whats_new_handler.cc", "webui/whats_new/whats_new_handler.cc",
"webui/whats_new/whats_new_handler.h", "webui/whats_new/whats_new_handler.h",
"webui/whats_new/whats_new_ui.cc", "webui/whats_new/whats_new_ui.cc",

@ -65,3 +65,21 @@ source_set("configs") {
} }
} }
} }
source_set("webui") {
sources = [
"webui_util.cc",
"webui_util.h",
]
deps = [
"//base",
"//content/public/browser",
"//content/public/common",
"//services/network/public/mojom",
"//ui/base",
"//ui/resources",
]
if (is_chromeos_lacros) {
deps += [ "//chromeos/startup" ]
}
}

@ -16,7 +16,7 @@
#include "chrome/browser/history/top_sites_factory.h" #include "chrome/browser/history/top_sites_factory.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/instant_service.h" #include "chrome/browser/search/instant_service.h"
#include "chrome/browser/ui/webui/webui_util.h" #include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "chrome/common/webui_url_constants.h" #include "chrome/common/webui_url_constants.h"
#include "components/favicon/core/history_ui_favicon_request_handler.h" #include "components/favicon/core/history_ui_favicon_request_handler.h"
@ -218,7 +218,7 @@ bool FaviconSource::ShouldServiceRequest(
ui::NativeTheme* FaviconSource::GetNativeTheme( ui::NativeTheme* FaviconSource::GetNativeTheme(
const content::WebContents::Getter& wc_getter) { const content::WebContents::Getter& wc_getter) {
return webui::GetNativeTheme(wc_getter.Run()); return webui::GetNativeThemeDeprecated(wc_getter.Run());
} }
void FaviconSource::OnFaviconDataAvailable( void FaviconSource::OnFaviconDataAvailable(

@ -54,7 +54,7 @@
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_utils.h" #include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_utils.h"
#include "chrome/browser/ui/webui/new_tab_page/ntp_pref_names.h" #include "chrome/browser/ui/webui/new_tab_page/ntp_pref_names.h"
#include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_chrome_section.h" #include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_chrome_section.h"
#include "chrome/browser/ui/webui/webui_util.h" #include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
@ -474,7 +474,7 @@ NewTabPageHandler::NewTabPageHandler(
NtpBackgroundServiceFactory::GetForProfile(profile)), NtpBackgroundServiceFactory::GetForProfile(profile)),
ntp_custom_background_service_(ntp_custom_background_service), ntp_custom_background_service_(ntp_custom_background_service),
logo_service_(logo_service), logo_service_(logo_service),
theme_provider_(webui::GetThemeProvider(web_contents)), theme_provider_(webui::GetThemeProviderDeprecated(web_contents)),
theme_service_(theme_service), theme_service_(theme_service),
profile_(profile), profile_(profile),
web_contents_(web_contents), web_contents_(web_contents),

@ -40,7 +40,7 @@
#include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.h" #include "chrome/browser/ui/side_panel/customize_chrome/customize_chrome_tab_helper.h"
#include "chrome/browser/ui/webui/new_tab_page/new_tab_page.mojom.h" #include "chrome/browser/ui/webui/new_tab_page/new_tab_page.mojom.h"
#include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_chrome_section.h" #include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_chrome_section.h"
#include "chrome/browser/ui/webui/webui_util.h" #include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h" #include "chrome/common/webui_url_constants.h"
@ -319,7 +319,7 @@ class NewTabPageHandlerTest : public testing::Test {
VerifyCustomBackgroundImageURL) VerifyCustomBackgroundImageURL)
.Times(1); .Times(1);
} }
webui::SetThemeProviderForTesting(&mock_theme_provider_); webui::SetThemeProviderForTestingDeprecated(&mock_theme_provider_);
web_contents_->SetColorProviderSource(&mock_color_provider_source_); web_contents_->SetColorProviderSource(&mock_color_provider_source_);
EXPECT_FALSE( EXPECT_FALSE(

@ -11,7 +11,7 @@
#include "chrome/browser/themes/theme_service_factory.h" #include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/color/chrome_color_id.h" #include "chrome/browser/ui/color/chrome_color_id.h"
#include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
#include "chrome/browser/ui/webui/webui_util.h" #include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/grit/theme_resources.h" #include "chrome/grit/theme_resources.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
@ -55,7 +55,7 @@ void NewTabPageThirdPartyHandler::NotifyAboutTheme() {
auto theme = new_tab_page_third_party::mojom::Theme::New(); auto theme = new_tab_page_third_party::mojom::Theme::New();
auto most_visited = most_visited::mojom::MostVisitedTheme::New(); auto most_visited = most_visited::mojom::MostVisitedTheme::New();
const ui::ThemeProvider* theme_provider = const ui::ThemeProvider* theme_provider =
webui::GetThemeProvider(web_contents_); webui::GetThemeProviderDeprecated(web_contents_);
DCHECK(theme_provider); DCHECK(theme_provider);
const ui::ColorProvider& color_provider = web_contents_->GetColorProvider(); const ui::ColorProvider& color_provider = web_contents_->GetColorProvider();
most_visited->background_color = most_visited->background_color =
@ -65,8 +65,8 @@ void NewTabPageThirdPartyHandler::NotifyAboutTheme() {
theme->text_color = color_provider.GetColor(kColorNewTabPageText); theme->text_color = color_provider.GetColor(kColorNewTabPageText);
most_visited->is_dark = !color_utils::IsDark(theme->text_color); most_visited->is_dark = !color_utils::IsDark(theme->text_color);
theme->color_background = color_utils::SkColorToRgbaString(GetThemeColor( theme->color_background = color_utils::SkColorToRgbaString(GetThemeColor(
webui::GetNativeTheme(web_contents_), web_contents_->GetColorProvider(), webui::GetNativeThemeDeprecated(web_contents_),
kColorNewTabPageBackground)); web_contents_->GetColorProvider(), kColorNewTabPageBackground));
if (theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { if (theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
theme->background_tiling = GetNewTabBackgroundTilingCSS(*theme_provider); theme->background_tiling = GetNewTabBackgroundTilingCSS(*theme_provider);
theme->background_position = theme->background_position =

@ -19,6 +19,7 @@
#include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
#include "chrome/browser/ui/webui/theme_source.h" #include "chrome/browser/ui/webui/theme_source.h"
#include "chrome/browser/ui/webui/webui_util.h" #include "chrome/browser/ui/webui/webui_util.h"
#include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "chrome/common/webui_url_constants.h" #include "chrome/common/webui_url_constants.h"
@ -63,7 +64,7 @@ void CreateAndAddNewTabPageThirdPartyUiHtmlSource(Profile* profile,
source->AddLocalizedStrings(kStrings); source->AddLocalizedStrings(kStrings);
const ui::ThemeProvider* theme_provider = const ui::ThemeProvider* theme_provider =
webui::GetThemeProvider(web_contents); webui::GetThemeProviderDeprecated(web_contents);
// TODO(crbug.com/40823895): Always mock theme provider in tests so that // TODO(crbug.com/40823895): Always mock theme provider in tests so that
// `theme_provider` is never nullptr. // `theme_provider` is never nullptr.
if (theme_provider) { if (theme_provider) {
@ -74,8 +75,8 @@ void CreateAndAddNewTabPageThirdPartyUiHtmlSource(Profile* profile,
GetNewTabBackgroundTilingCSS(*theme_provider)); GetNewTabBackgroundTilingCSS(*theme_provider));
source->AddString("colorBackground", source->AddString("colorBackground",
color_utils::SkColorToRgbaString(GetThemeColor( color_utils::SkColorToRgbaString(GetThemeColor(
webui::GetNativeTheme(web_contents), color_provider, webui::GetNativeThemeDeprecated(web_contents),
kColorNewTabPageBackground))); color_provider, kColorNewTabPageBackground)));
// TODO(crbug.com/40120448): don't get theme id from profile. // TODO(crbug.com/40120448): don't get theme id from profile.
source->AddString("themeId", source->AddString("themeId",
profile->GetPrefs()->GetString(prefs::kCurrentThemeID)); profile->GetPrefs()->GetString(prefs::kCurrentThemeID));

@ -29,7 +29,7 @@
#include "chrome/browser/ui/cookie_controls/cookie_controls_service_factory.h" #include "chrome/browser/ui/cookie_controls/cookie_controls_service_factory.h"
#include "chrome/browser/ui/layout_constants.h" #include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/webui/ntp/cookie_controls_handler.h" #include "chrome/browser/ui/webui/ntp/cookie_controls_handler.h"
#include "chrome/browser/ui/webui/webui_util.h" #include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/common/buildflags.h" #include "chrome/common/buildflags.h"
#include "chrome/common/chrome_features.h" #include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h" #include "chrome/common/pref_names.h"
@ -401,7 +401,8 @@ void NTPResourceCache::CreateNewTabGuestHTML() {
void NTPResourceCache::CreateNewTabIncognitoCSS( void NTPResourceCache::CreateNewTabIncognitoCSS(
const content::WebContents::Getter& wc_getter) { const content::WebContents::Getter& wc_getter) {
auto* web_contents = wc_getter.Run(); auto* web_contents = wc_getter.Run();
const ui::NativeTheme* native_theme = webui::GetNativeTheme(web_contents); const ui::NativeTheme* native_theme =
webui::GetNativeThemeDeprecated(web_contents);
DCHECK(native_theme); DCHECK(native_theme);
const ui::ThemeProvider& tp = ThemeService::GetThemeProviderForProfile( const ui::ThemeProvider& tp = ThemeService::GetThemeProviderForProfile(
@ -434,7 +435,8 @@ void NTPResourceCache::CreateNewTabIncognitoCSS(
void NTPResourceCache::CreateNewTabCSS( void NTPResourceCache::CreateNewTabCSS(
const content::WebContents::Getter& wc_getter) { const content::WebContents::Getter& wc_getter) {
auto* web_contents = wc_getter.Run(); auto* web_contents = wc_getter.Run();
const ui::NativeTheme* native_theme = webui::GetNativeTheme(web_contents); const ui::NativeTheme* native_theme =
webui::GetNativeThemeDeprecated(web_contents);
DCHECK(native_theme); DCHECK(native_theme);
const ui::ThemeProvider& tp = const ui::ThemeProvider& tp =

@ -39,7 +39,7 @@
#include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_util.h" #include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_util.h"
#include "chrome/browser/ui/webui/theme_source.h" #include "chrome/browser/ui/webui/theme_source.h"
#include "chrome/browser/ui/webui/util/image_util.h" #include "chrome/browser/ui/webui/util/image_util.h"
#include "chrome/browser/ui/webui/webui_util.h" #include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/grit/generated_resources.h" #include "chrome/grit/generated_resources.h"
#include "components/tab_groups/tab_group_color.h" #include "components/tab_groups/tab_group_color.h"
#include "components/tab_groups/tab_group_id.h" #include "components/tab_groups/tab_group_id.h"
@ -191,7 +191,8 @@ TabStripPageHandler::TabStripPageHandler(
ThemeServiceFactory::GetForProfile(browser_->profile())->AddObserver(this); ThemeServiceFactory::GetForProfile(browser_->profile())->AddObserver(this);
// Or native theme change. // Or native theme change.
theme_observation_.Observe(webui::GetNativeTheme(web_ui_->GetWebContents())); theme_observation_.Observe(
webui::GetNativeThemeDeprecated(web_ui_->GetWebContents()));
} }
void TabStripPageHandler::NotifyLayoutChanged() { void TabStripPageHandler::NotifyLayoutChanged() {
@ -886,7 +887,7 @@ void TabStripPageHandler::OnNativeThemeUpdated(
// switch between light/dark mode. b) A different theme is enabled. e.g. // switch between light/dark mode. b) A different theme is enabled. e.g.
// switch between GTK and classic theme on Linux. Reset observer in case b). // switch between GTK and classic theme on Linux. Reset observer in case b).
ui::NativeTheme* current_theme = ui::NativeTheme* current_theme =
webui::GetNativeTheme(web_ui_->GetWebContents()); webui::GetNativeThemeDeprecated(web_ui_->GetWebContents());
if (observed_theme != current_theme) { if (observed_theme != current_theme) {
theme_observation_.Reset(); theme_observation_.Reset();
theme_observation_.Observe(current_theme); theme_observation_.Observe(current_theme);

@ -15,7 +15,7 @@
#include "chrome/browser/ui/webui/tab_strip/tab_strip_ui.h" #include "chrome/browser/ui/webui/tab_strip/tab_strip_ui.h"
#include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_embedder.h" #include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_embedder.h"
#include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_layout.h" #include "chrome/browser/ui/webui/tab_strip/tab_strip_ui_layout.h"
#include "chrome/browser/ui/webui/webui_util.h" #include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/test/base/browser_with_test_window_test.h" #include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_profile_manager.h" #include "chrome/test/base/testing_profile_manager.h"
#include "components/tab_groups/tab_group_color.h" #include "components/tab_groups/tab_group_color.h"
@ -702,7 +702,7 @@ TEST_F(TabStripPageHandlerTest, PreventsInvalidGroupDrags) {
} }
TEST_F(TabStripPageHandlerTest, OnThemeChanged) { TEST_F(TabStripPageHandlerTest, OnThemeChanged) {
webui::GetNativeTheme(web_ui()->GetWebContents()) webui::GetNativeThemeDeprecated(web_ui()->GetWebContents())
->NotifyOnNativeThemeUpdated(); ->NotifyOnNativeThemeUpdated();
EXPECT_CALL(page_, ThemeChanged()); EXPECT_CALL(page_, ThemeChanged());
} }

@ -12,7 +12,7 @@
#include "chrome/browser/themes/theme_service.h" #include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h" #include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/webui/theme_source.h" #include "chrome/browser/ui/webui/theme_source.h"
#include "chrome/browser/ui/webui/webui_util.h" #include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/grit/theme_resources.h" #include "chrome/grit/theme_resources.h"
#include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui.h"
@ -43,7 +43,7 @@ void ThemeHandler::OnJavascriptAllowed() {
// Or native theme change. // Or native theme change.
if (web_ui()) { if (web_ui()) {
theme_observation_.Observe( theme_observation_.Observe(
webui::GetNativeTheme(web_ui()->GetWebContents())); webui::GetNativeThemeDeprecated(web_ui()->GetWebContents()));
} }
} }
@ -61,7 +61,7 @@ void ThemeHandler::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
// switch between light/dark mode. b) A different theme is enabled. e.g. // switch between light/dark mode. b) A different theme is enabled. e.g.
// switch between GTK and classic theme on Linux. Reset observer in case b). // switch between GTK and classic theme on Linux. Reset observer in case b).
ui::NativeTheme* current_theme = ui::NativeTheme* current_theme =
webui::GetNativeTheme(web_ui()->GetWebContents()); webui::GetNativeThemeDeprecated(web_ui()->GetWebContents());
if (observed_theme != current_theme) { if (observed_theme != current_theme) {
theme_observation_.Reset(); theme_observation_.Reset();
theme_observation_.Observe(current_theme); theme_observation_.Observe(current_theme);

@ -2,7 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/ui/webui/theme_source.h"
#include <stddef.h> #include <stddef.h>
#include <vector> #include <vector>
#include "base/functional/bind.h" #include "base/functional/bind.h"
@ -11,8 +14,7 @@
#include "base/strings/strcat.h" #include "base/strings/strcat.h"
#include "base/test/bind.h" #include "base/test/bind.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/theme_source.h" #include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/browser/ui/webui/webui_util.h"
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "chrome/grit/theme_resources.h" #include "chrome/grit/theme_resources.h"
#include "chrome/test/base/test_theme_provider.h" #include "chrome/test/base/test_theme_provider.h"
@ -43,7 +45,7 @@ class WebUISourcesTest : public testing::Test {
private: private:
void SetUp() override { void SetUp() override {
webui::SetThemeProviderForTesting(&test_theme_provider_); webui::SetThemeProviderForTestingDeprecated(&test_theme_provider_);
profile_ = std::make_unique<TestingProfile>(); profile_ = std::make_unique<TestingProfile>();
theme_source_ = std::make_unique<ThemeSource>(profile_.get()); theme_source_ = std::make_unique<ThemeSource>(profile_.get());
test_web_contents_ = content::WebContentsTester::CreateTestWebContents( test_web_contents_ = content::WebContentsTester::CreateTestWebContents(
@ -57,7 +59,7 @@ class WebUISourcesTest : public testing::Test {
test_web_contents_.reset(); test_web_contents_.reset();
test_web_contents_getter_ = content::WebContents::Getter(); test_web_contents_getter_ = content::WebContents::Getter();
profile_.reset(); profile_.reset();
webui::SetThemeProviderForTesting(nullptr); webui::SetThemeProviderForTestingDeprecated(nullptr);
} }
void SendResponse(scoped_refptr<base::RefCountedMemory> data) { void SendResponse(scoped_refptr<base::RefCountedMemory> data) {

@ -18,26 +18,12 @@
#include "ui/base/webui/web_ui_util.h" #include "ui/base/webui/web_ui_util.h"
#include "ui/resources/grit/webui_resources.h" #include "ui/resources/grit/webui_resources.h"
#if BUILDFLAG(IS_CHROMEOS_ASH) #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
#include "chrome/browser/ash/policy/core/browser_policy_connector_ash.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#elif BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
#include "base/enterprise_util.h" #include "base/enterprise_util.h"
#elif BUILDFLAG(IS_CHROMEOS_LACROS) #elif BUILDFLAG(IS_CHROMEOS_LACROS)
#include "chromeos/startup/browser_params_proxy.h" #include "chromeos/startup/browser_params_proxy.h"
#endif #endif
#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/widget/widget.h"
#endif // defined(TOOLKIT_VIEWS)
namespace webui { namespace webui {
void SetJSModuleDefaults(content::WebUIDataSource* source) { void SetJSModuleDefaults(content::WebUIDataSource* source) {
@ -134,80 +120,5 @@ void AddLocalizedString(content::WebUIDataSource* source,
std::erase(str, '&'); std::erase(str, '&');
source->AddString(message, str); source->AddString(message, str);
} }
#if defined(TOOLKIT_VIEWS)
namespace {
const ui::ThemeProvider* g_theme_provider_for_testing = nullptr;
} // namespace
ui::NativeTheme* GetNativeTheme(content::WebContents* web_contents) {
ui::NativeTheme* native_theme = nullptr;
if (web_contents) {
Browser* browser = chrome::FindBrowserWithTab(web_contents);
if (browser) {
// Find for WebContents hosted in a tab.
native_theme = browser->window()->GetNativeTheme();
}
if (!native_theme) {
// Find for WebContents hosted in a widget, but not directly in a
// Browser. e.g. Tab Search, Read Later.
views::Widget* widget = views::Widget::GetTopLevelWidgetForNativeView(
web_contents->GetContentNativeView());
if (widget)
native_theme = widget->GetNativeTheme();
}
}
if (!native_theme) {
// Find for isolated WebContents, e.g. in tests.
// Or when |web_contents| is nullptr, because the renderer is not ready.
// TODO(crbug.com/40677117): Remove global accessor to NativeTheme.
native_theme = ui::NativeTheme::GetInstanceForNativeUi();
}
return native_theme;
}
const ui::ThemeProvider* GetThemeProvider(content::WebContents* web_contents) {
if (g_theme_provider_for_testing)
return g_theme_provider_for_testing;
auto* browser_window =
BrowserWindow::FindBrowserWindowWithWebContents(web_contents);
if (browser_window)
return browser_window->GetThemeProvider();
// Fallback 1: get the theme provider from the profile's associated browser.
// This is used in newly created tabs, e.g. NewTabPageUI, where theming is
// required before the WebContents is attached to a browser window.
// TODO(crbug.com/40823135): Remove this fallback by associating the
// WebContents during navigation.
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
const Browser* browser = chrome::FindBrowserWithProfile(profile);
if (browser)
return browser->window()->GetThemeProvider();
// Fallback 2: get the theme provider from the last created browser.
// This is used in ChromeOS, where under multi-signin a browser window can
// be sent to another profile.
// TODO(crbug.com/40823135): Remove this fallback by associating the
// WebContents during navigation.
BrowserList* browser_list = BrowserList::GetInstance();
browser = browser_list->empty()
? nullptr
: *std::prev(BrowserList::GetInstance()->end());
return browser ? browser->window()->GetThemeProvider() : nullptr;
}
void SetThemeProviderForTesting(const ui::ThemeProvider* theme_provider) {
g_theme_provider_for_testing = theme_provider;
}
#endif // defined(TOOLKIT_VIEWS)
} // namespace webui } // namespace webui

@ -11,15 +11,9 @@
#include "ui/base/webui/resource_path.h" #include "ui/base/webui/resource_path.h"
namespace content { namespace content {
class WebContents;
class WebUIDataSource; class WebUIDataSource;
} }
namespace ui {
class NativeTheme;
class ThemeProvider;
}
namespace webui { namespace webui {
// Performs common setup steps for a |source| using JS modules: enable i18n // Performs common setup steps for a |source| using JS modules: enable i18n
@ -44,20 +38,6 @@ void EnableTrustedTypesCSP(content::WebUIDataSource* source);
void AddLocalizedString(content::WebUIDataSource* source, void AddLocalizedString(content::WebUIDataSource* source,
const std::string& message, const std::string& message,
int id); int id);
#if defined(TOOLKIT_VIEWS)
// Returns whether WebContents should use dark mode colors depending on the
// theme.
ui::NativeTheme* GetNativeTheme(content::WebContents* web_contents);
// Returns the ThemeProvider instance associated with the given web contents.
const ui::ThemeProvider* GetThemeProvider(content::WebContents* web_contents);
// Sets a global theme provider that will be returned when calling
// webui::GetThemeProvider(). Used only for testing.
void SetThemeProviderForTesting(const ui::ThemeProvider* theme_provider);
#endif // defined(TOOLKIT_VIEWS)
} // namespace webui } // namespace webui

@ -0,0 +1,95 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/webui_util_desktop.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/widget/widget.h"
namespace webui {
namespace {
const ui::ThemeProvider* g_theme_provider_for_testing = nullptr;
} // namespace
ui::NativeTheme* GetNativeThemeDeprecated(content::WebContents* web_contents) {
ui::NativeTheme* native_theme = nullptr;
if (web_contents) {
Browser* browser = chrome::FindBrowserWithTab(web_contents);
if (browser) {
// Find for WebContents hosted in a tab.
native_theme = browser->window()->GetNativeTheme();
}
if (!native_theme) {
// Find for WebContents hosted in a widget, but not directly in a
// Browser. e.g. Tab Search, Read Later.
views::Widget* widget = views::Widget::GetTopLevelWidgetForNativeView(
web_contents->GetContentNativeView());
if (widget) {
native_theme = widget->GetNativeTheme();
}
}
}
if (!native_theme) {
// Find for isolated WebContents, e.g. in tests.
// Or when |web_contents| is nullptr, because the renderer is not ready.
// TODO(crbug.com/40677117): Remove global accessor to NativeTheme.
native_theme = ui::NativeTheme::GetInstanceForNativeUi();
}
return native_theme;
}
const ui::ThemeProvider* GetThemeProviderDeprecated(
content::WebContents* web_contents) {
if (g_theme_provider_for_testing) {
return g_theme_provider_for_testing;
}
auto* browser_window =
BrowserWindow::FindBrowserWindowWithWebContents(web_contents);
if (browser_window) {
return browser_window->GetThemeProvider();
}
// Fallback 1: get the theme provider from the profile's associated browser.
// This is used in newly created tabs, e.g. NewTabPageUI, where theming is
// required before the WebContents is attached to a browser window.
// TODO(crbug.com/40823135): Remove this fallback by associating the
// WebContents during navigation.
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
const Browser* browser = chrome::FindBrowserWithProfile(profile);
if (browser) {
return browser->window()->GetThemeProvider();
}
// Fallback 2: get the theme provider from the last created browser.
// This is used in ChromeOS, where under multi-signin a browser window can
// be sent to another profile.
// TODO(crbug.com/40823135): Remove this fallback by associating the
// WebContents during navigation.
BrowserList* browser_list = BrowserList::GetInstance();
browser = browser_list->empty()
? nullptr
: *std::prev(BrowserList::GetInstance()->end());
return browser ? browser->window()->GetThemeProvider() : nullptr;
}
void SetThemeProviderForTestingDeprecated(
const ui::ThemeProvider* theme_provider) {
g_theme_provider_for_testing = theme_provider;
}
} // namespace webui

@ -0,0 +1,37 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_WEBUI_WEBUI_UTIL_DESKTOP_H_
#define CHROME_BROWSER_UI_WEBUI_WEBUI_UTIL_DESKTOP_H_
namespace content {
class WebContents;
}
namespace ui {
class NativeTheme;
class ThemeProvider;
} // namespace ui
namespace webui {
// These methods should not be used. Instead, browser-related state should be
// passed from the owner of the WebUI instance to the WebUIController instance.
// See LensOverlayController for one example of how to do this.
// Returns whether WebContents should use dark mode colors depending on the
// theme.
ui::NativeTheme* GetNativeThemeDeprecated(content::WebContents* web_contents);
// Returns the ThemeProvider instance associated with the given web contents.
const ui::ThemeProvider* GetThemeProviderDeprecated(
content::WebContents* web_contents);
// Sets a global theme provider that will be returned when calling
// webui::GetThemeProviderDeprecated(). Used only for testing.
void SetThemeProviderForTestingDeprecated(
const ui::ThemeProvider* theme_provider);
} // namespace webui
#endif // CHROME_BROWSER_UI_WEBUI_WEBUI_UTIL_DESKTOP_H_

@ -10674,6 +10674,7 @@ if (!is_android) {
"//chrome/browser/devtools", "//chrome/browser/devtools",
"//chrome/browser/profiles:profile", "//chrome/browser/profiles:profile",
"//chrome/browser/ui", "//chrome/browser/ui",
"//chrome/browser/ui/webui",
"//chrome/browser/web_applications", "//chrome/browser/web_applications",
"//chrome/browser/web_applications:web_applications_test_support", "//chrome/browser/web_applications:web_applications_test_support",
"//chrome/common", "//chrome/common",

@ -689,13 +689,13 @@ const ui::ThemeProvider* GetThemeProvider(content::WebContents* web_contents);
void NTPResourceCache::CreateNewTabIncognitoCSS( void NTPResourceCache::CreateNewTabIncognitoCSS(
const content::WebContents::Getter& wc_getter) { const content::WebContents::Getter& wc_getter) {
auto* web_contents = wc_getter.Run(); auto* web_contents = wc_getter.Run();
const ui::NativeTheme* native_theme = webui::GetNativeTheme(web_contents); const ui::NativeTheme* native_theme = webui::GetNativeThemeDeprecated(web_contents);
DCHECK(native_theme); DCHECK(native_theme);
// Requesting the incognito CSS is only done from within incognito browser // Requesting the incognito CSS is only done from within incognito browser
// windows. The ThemeProvider associated with the requesting WebContents will // windows. The ThemeProvider associated with the requesting WebContents will
// wrap the relevant incognito bits. // wrap the relevant incognito bits.
const ui::ThemeProvider* tp = webui::GetThemeProvider(web_contents); const ui::ThemeProvider* tp = webui::GetThemeProviderDeprecated(web_contents);
DCHECK(tp); DCHECK(tp);
... ...