diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index 54b56e16f245..53c0e72467e7 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -5,6 +5,7 @@ #include "brave/browser/brave_profile_prefs.h" #include "brave/browser/alternate_private_search_engine_util.h" +#include "brave/browser/themes/brave_theme_service.h" #include "brave/common/pref_names.h" #include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h" #include "chrome/browser/net/prediction_options.h" @@ -24,6 +25,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { RegisterAlternatePrivateSearchEngineProfilePrefs(registry); + BraveThemeService::RegisterProfilePrefs(registry); + registry->RegisterBooleanPref(kWidevineOptedIn, false); // No sign into Brave functionality diff --git a/browser/themes/BUILD.gn b/browser/themes/BUILD.gn index 620522bb232f..8194ec3b7715 100644 --- a/browser/themes/BUILD.gn +++ b/browser/themes/BUILD.gn @@ -1,9 +1,20 @@ source_set("themes") { sources = [ + "brave_theme_service.cc", + "brave_theme_service.h", + "brave_theme_service_win.cc", + "brave_theme_service_win.h", "theme_properties.cc", "theme_properties.h", ] + deps = [ + "//base", + "//brave/common", + "//chrome/common", + "//components/prefs", + "//components/pref_registry", + "//components/version_info", "//skia", ] } diff --git a/browser/themes/brave_theme_service.cc b/browser/themes/brave_theme_service.cc new file mode 100644 index 000000000000..74c173957a3a --- /dev/null +++ b/browser/themes/brave_theme_service.cc @@ -0,0 +1,51 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/themes/brave_theme_service.h" + +#include "brave/browser/themes/theme_properties.h" +#include "brave/common/pref_names.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/themes/theme_service_factory.h" +#include "components/pref_registry/pref_registry_syncable.h" +#include "components/prefs/pref_service.h" + +// static +void BraveThemeService::RegisterProfilePrefs( + user_prefs::PrefRegistrySyncable* registry) { + registry->RegisterIntegerPref(kBraveThemeType, BRAVE_THEME_TYPE_DEFAULT); +} + +// static +BraveThemeService::BraveThemeType BraveThemeService::GetBraveThemeType(Profile* profile) { + return static_cast( + profile->GetPrefs()->GetInteger(kBraveThemeType)); +} + +BraveThemeService::BraveThemeService() {} + +BraveThemeService::~BraveThemeService() {} + +void BraveThemeService::Init(Profile* profile) { + brave_theme_type_pref_.Init( + kBraveThemeType, + profile->GetPrefs(), + base::Bind(&BraveThemeService::OnPreferenceChanged, + base::Unretained(this))); + ThemeService::Init(profile); +} + +SkColor BraveThemeService::GetDefaultColor(int id, bool incognito) const { + const base::Optional braveColor = + MaybeGetDefaultColorForBraveUi(id, incognito, profile()); + if (braveColor) + return braveColor.value(); + + return ThemeService::GetDefaultColor(id, incognito); +} + +void BraveThemeService::OnPreferenceChanged(const std::string& pref_name) { + DCHECK(pref_name == kBraveThemeType); + NotifyThemeChanged(); +} diff --git a/browser/themes/brave_theme_service.h b/browser/themes/brave_theme_service.h new file mode 100644 index 000000000000..8edd55d14e19 --- /dev/null +++ b/browser/themes/brave_theme_service.h @@ -0,0 +1,44 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_ +#define BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_ + +#include "chrome/browser/themes/theme_service.h" +#include "components/prefs/pref_member.h" + +namespace user_prefs { +class PrefRegistrySyncable; +} + +class BraveThemeService : public ThemeService { + public: + enum BraveThemeType { + BRAVE_THEME_TYPE_DEFAULT, // Choose theme by channel + BRAVE_THEME_TYPE_DARK, // Use dark theme regardless of channel + BRAVE_THEME_TYPE_LIGHT, // Use light theme regardless of channel + }; + + static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); + static BraveThemeType GetBraveThemeType(Profile* profile); + + BraveThemeService(); + ~BraveThemeService() override; + + // ThemeService overrides: + void Init(Profile* profile) override; + + protected: + // ThemeService overrides: + SkColor GetDefaultColor(int id, bool incognito) const override; + + private: + void OnPreferenceChanged(const std::string& pref_name); + + IntegerPrefMember brave_theme_type_pref_; + + DISALLOW_COPY_AND_ASSIGN(BraveThemeService); +}; + +#endif // BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_ diff --git a/browser/themes/brave_theme_service_browsertest.cc b/browser/themes/brave_theme_service_browsertest.cc new file mode 100644 index 000000000000..a45df5042ab3 --- /dev/null +++ b/browser/themes/brave_theme_service_browsertest.cc @@ -0,0 +1,46 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/themes/brave_theme_service.h" +#include "brave/common/pref_names.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/themes/theme_properties.h" +#include "chrome/browser/themes/theme_service.h" +#include "chrome/browser/ui/browser.h" +#include "components/prefs/pref_service.h" + +using BraveThemeServiceTest = InProcessBrowserTest; +using BTS = BraveThemeService; + +namespace { +void SetBraveThemeType(Profile* profile, BTS::BraveThemeType type) { + profile->GetPrefs()->SetInteger(kBraveThemeType, type); +} +} // namespace + +IN_PROC_BROWSER_TEST_F(BraveThemeServiceTest, BraveThemeChangeTest) { + Profile* profile = browser()->profile(); +#if defined(OFFICIAL_BUILD) + const SkColor light_frame_color = SkColorSetRGB(0xD8, 0xDE, 0xE1); +#endif + const SkColor dark_frame_color = SkColorSetRGB(0x58, 0x5B, 0x5E); + + // Check default type is set initially. + EXPECT_EQ(BTS::BRAVE_THEME_TYPE_DEFAULT, BTS::GetBraveThemeType(profile)); + + const ui::ThemeProvider& tp = ThemeService::GetThemeProviderForProfile(profile); + SetBraveThemeType(browser()->profile(), BTS::BRAVE_THEME_TYPE_LIGHT); + EXPECT_EQ(BTS::BRAVE_THEME_TYPE_LIGHT, BTS::GetBraveThemeType(profile)); +#if defined(OFFICIAL_BUILD) + EXPECT_EQ(light_frame_color, tp.GetColor(ThemeProperties::COLOR_FRAME)); +#else + // Non-official build always uses dark theme. + EXPECT_EQ(dark_frame_color, tp.GetColor(ThemeProperties::COLOR_FRAME)); +#endif + + SetBraveThemeType(browser()->profile(), BTS::BRAVE_THEME_TYPE_DARK); + EXPECT_EQ(BTS::BRAVE_THEME_TYPE_DARK, BTS::GetBraveThemeType(profile)); + EXPECT_EQ(dark_frame_color, tp.GetColor(ThemeProperties::COLOR_FRAME)); +} diff --git a/browser/themes/brave_theme_service_win.cc b/browser/themes/brave_theme_service_win.cc new file mode 100644 index 000000000000..27e68f31807d --- /dev/null +++ b/browser/themes/brave_theme_service_win.cc @@ -0,0 +1,16 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/themes/brave_theme_service_win.h" + +#include "chrome/browser/themes/theme_properties.h" + +SkColor BraveThemeServiceWin::GetDefaultColor(int id, bool incognito) const { + // Prevent dcheck in chrome/browser/themes/theme_properties.cc(384) + // It assumes this id handled in theme service. + if (DwmColorsAllowed() && id == ThemeProperties::COLOR_ACCENT_BORDER) + return dwm_accent_border_color_; + // Skip ThemeServiceWin::GetDefaultColor() to prevent using dwm frame color. + return BraveThemeService::GetDefaultColor(id, incognito); +} diff --git a/browser/themes/brave_theme_service_win.h b/browser/themes/brave_theme_service_win.h new file mode 100644 index 000000000000..c7489dc61835 --- /dev/null +++ b/browser/themes/brave_theme_service_win.h @@ -0,0 +1,22 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_WIN_H_ +#define BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_WIN_H_ + +#include "chrome/browser/themes/theme_service_win.h" + +class BraveThemeServiceWin : public ThemeServiceWin { + public: + BraveThemeServiceWin() = default; + ~BraveThemeServiceWin() override = default; + + private: + // ThemeServiceWin overrides: + SkColor GetDefaultColor(int id, bool incognito) const override; + + DISALLOW_COPY_AND_ASSIGN(BraveThemeServiceWin); +}; + +#endif // BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_WIN_H_ diff --git a/browser/themes/theme_properties.cc b/browser/themes/theme_properties.cc index 4155a676c1f3..6b7b94bc87b1 100644 --- a/browser/themes/theme_properties.cc +++ b/browser/themes/theme_properties.cc @@ -4,10 +4,13 @@ #include "brave/browser/themes/theme_properties.h" +#include "brave/browser/themes/brave_theme_service.h" +#include "brave/common/pref_names.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/browser/themes/theme_properties.h" #include "chrome/common/channel_info.h" +#include "components/prefs/pref_service.h" #include "components/version_info/channel.h" -#include "ui/gfx/color_palette.h" namespace { @@ -70,22 +73,29 @@ base::Optional MaybeGetDefaultColorForBraveUiDevChannel(int id, bool in } // namespace // Returns a |nullopt| if the UI color is not handled by Brave. -base::Optional MaybeGetDefaultColorForBraveUi(int id, bool incognito) { - if (id == BRAVE_COLOR_FOR_TEST) { - return SkColorSetRGB(11, 13, 17); - } +base::Optional MaybeGetDefaultColorForBraveUi(int id, bool incognito, Profile* profile) { #if !defined(OFFICIAL_BUILD) return MaybeGetDefaultColorForBraveUiDevChannel(id, incognito); #else - switch (chrome::GetChannel()) { - case version_info::Channel::STABLE: - case version_info::Channel::BETA: + switch (BraveThemeService::GetBraveThemeType(profile)) { + case BraveThemeService::BRAVE_THEME_TYPE_DEFAULT: + switch (chrome::GetChannel()) { + case version_info::Channel::STABLE: + case version_info::Channel::BETA: + return MaybeGetDefaultColorForBraveUiReleaseChannel(id, incognito); + case version_info::Channel::DEV: + case version_info::Channel::CANARY: + case version_info::Channel::UNKNOWN: + default: + return MaybeGetDefaultColorForBraveUiDevChannel(id, incognito); + } + case BraveThemeService::BRAVE_THEME_TYPE_LIGHT: return MaybeGetDefaultColorForBraveUiReleaseChannel(id, incognito); - case version_info::Channel::DEV: - case version_info::Channel::CANARY: - case version_info::Channel::UNKNOWN: - default: + case BraveThemeService::BRAVE_THEME_TYPE_DARK: return MaybeGetDefaultColorForBraveUiDevChannel(id, incognito); + default: + NOTREACHED(); + } #endif return base::nullopt; diff --git a/browser/themes/theme_properties.h b/browser/themes/theme_properties.h index f8da9d48485c..7dd7a7bbad7d 100644 --- a/browser/themes/theme_properties.h +++ b/browser/themes/theme_properties.h @@ -8,12 +8,10 @@ #include "base/optional.h" #include "third_party/skia/include/core/SkColor.h" -#define BRAVE_COLOR_FOR_TEST 0x7FFFFFFF +class Profile; -base::Optional MaybeGetDefaultColorForBraveUi(int id, bool incognito); +#define BRAVE_COLOR_FOR_TEST 0x7FFFFFFF -#define MAYBE_OVERRIDE_DEFAULT_COLOR_FOR_BRAVE(id, incognito) \ - const base::Optional braveColor = MaybeGetDefaultColorForBraveUi(id, incognito); \ - if (braveColor) return braveColor.value(); +base::Optional MaybeGetDefaultColorForBraveUi(int id, bool incognito, Profile* profile); #endif // BRAVE_BROWSER_THEMES_THEME_PROPERTIES_H_ diff --git a/browser/themes/theme_properties_unittest.cc b/browser/themes/theme_properties_unittest.cc deleted file mode 100644 index 914f4c942593..000000000000 --- a/browser/themes/theme_properties_unittest.cc +++ /dev/null @@ -1,13 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public -+ * License, v. 2.0. If a copy of the MPL was not distributed with this file, -+ * You can obtain one at http://mozilla.org/MPL/2.0/. */ - -#include "brave/browser/themes/theme_properties.h" -#include "chrome/browser/themes/theme_properties.h" -#include "testing/gtest/include/gtest/gtest.h" - -TEST(BraveThemeTest, ObtainsBraveOverrideColors) { - SkColor actualColor = ThemeProperties::GetDefaultColor(BRAVE_COLOR_FOR_TEST, false); - SkColor expectedColor = SkColorSetRGB(11, 13, 17); - ASSERT_EQ(actualColor, expectedColor); -} diff --git a/chromium_src/chrome/browser/themes/theme_service_aurax11.h b/chromium_src/chrome/browser/themes/theme_service_aurax11.h new file mode 100644 index 000000000000..5330250f8900 --- /dev/null +++ b/chromium_src/chrome/browser/themes/theme_service_aurax11.h @@ -0,0 +1,10 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/themes/brave_theme_service.h" + +#undef ThemeService +#define ThemeService BraveThemeService +#include "../../../../../chrome/browser/themes/theme_service_aurax11.h" +#undef ThemeService diff --git a/chromium_src/chrome/browser/themes/theme_service_factory.cc b/chromium_src/chrome/browser/themes/theme_service_factory.cc new file mode 100644 index 000000000000..0fb350c60bd4 --- /dev/null +++ b/chromium_src/chrome/browser/themes/theme_service_factory.cc @@ -0,0 +1,18 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "build/build_config.h" +#include "chrome/browser/themes/theme_service.h" + +#if defined(OS_WIN) +#include "brave/browser/themes/brave_theme_service_win.h" +#undef ThemeServiceWin +#define ThemeServiceWin BraveThemeServiceWin +#elif !defined(USE_X11) +#include "brave/browser/themes/brave_theme_service.h" +#undef ThemeService +#define ThemeService BraveThemeService +#endif + +#include "../../../../../chrome/browser/themes/theme_service_factory.cc" diff --git a/chromium_src/chrome/browser/themes/theme_service_win.h b/chromium_src/chrome/browser/themes/theme_service_win.h new file mode 100644 index 000000000000..0e9cd4b50109 --- /dev/null +++ b/chromium_src/chrome/browser/themes/theme_service_win.h @@ -0,0 +1,13 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/themes/brave_theme_service.h" + +#undef ThemeService +#define ThemeService BraveThemeService + +#include "../../../../../chrome/browser/themes/theme_service_win.h" + +#undef ThemeService +#define ThemeService ThemeService diff --git a/common/pref_names.cc b/common/pref_names.cc index d386f68ffdcc..9205048e4e24 100644 --- a/common/pref_names.cc +++ b/common/pref_names.cc @@ -18,3 +18,4 @@ const char kAdBlockCurrentRegion[] = "brave.ad_block.current_region"; const char kWidevineOptedIn[] = "brave.widevine_opted_in"; const char kUseAlternatePrivateSearchEngine[] = "brave.use_alternate_private_search_engine"; +const char kBraveThemeType[] = "brave.theme.type"; diff --git a/common/pref_names.h b/common/pref_names.h index 1740bb9ec239..98b1d7baa35c 100644 --- a/common/pref_names.h +++ b/common/pref_names.h @@ -18,5 +18,6 @@ extern const char kWeekOfInstallation[]; extern const char kAdBlockCurrentRegion[]; extern const char kWidevineOptedIn[]; extern const char kUseAlternatePrivateSearchEngine[]; +extern const char kBraveThemeType[]; -#endif +#endif // BRAVE_COMMON_PREF_NAMES_H_ diff --git a/patches/chrome-browser-themes-theme_properties.cc.patch b/patches/chrome-browser-themes-theme_properties.cc.patch deleted file mode 100644 index a8e6c7f15ffb..000000000000 --- a/patches/chrome-browser-themes-theme_properties.cc.patch +++ /dev/null @@ -1,20 +0,0 @@ -diff --git a/chrome/browser/themes/theme_properties.cc b/chrome/browser/themes/theme_properties.cc -index 733d3521fb2ecb8dd47c68cc9ca451752bdec04e..e52c5cecb77044d2b33bb2917e850da1fbd4a058 100644 ---- a/chrome/browser/themes/theme_properties.cc -+++ b/chrome/browser/themes/theme_properties.cc -@@ -10,6 +10,7 @@ - #include "base/optional.h" - #include "base/strings/string_split.h" - #include "base/strings/string_util.h" -+#include "brave/browser/themes/theme_properties.h" - #include "build/build_config.h" - #include "chrome/browser/themes/browser_theme_pack.h" - #include "ui/base/material_design/material_design_controller.h" -@@ -290,6 +291,7 @@ color_utils::HSL ThemeProperties::GetDefaultTint(int id, bool incognito) { - - // static - SkColor ThemeProperties::GetDefaultColor(int id, bool incognito) { -+ MAYBE_OVERRIDE_DEFAULT_COLOR_FOR_BRAVE(id, incognito) - const base::Optional color = - MaybeGetDefaultColorForNewerMaterialUi(id, incognito); - if (color) diff --git a/patches/chrome-browser-themes-theme_service_win.h.patch b/patches/chrome-browser-themes-theme_service_win.h.patch new file mode 100644 index 000000000000..70bb9e90baec --- /dev/null +++ b/patches/chrome-browser-themes-theme_service_win.h.patch @@ -0,0 +1,12 @@ +diff --git a/chrome/browser/themes/theme_service_win.h b/chrome/browser/themes/theme_service_win.h +index eba1cb2596d71b91e06183209503f3fc83b0e593..e6261a982859a6a66caa00d362f290d247ce7922 100644 +--- a/chrome/browser/themes/theme_service_win.h ++++ b/chrome/browser/themes/theme_service_win.h +@@ -18,6 +18,7 @@ class ThemeServiceWin : public ThemeService { + ~ThemeServiceWin() override; + + private: ++ friend class BraveThemeServiceWin; + // ThemeService: + bool ShouldUseNativeFrame() const override; + SkColor GetDefaultColor(int id, bool incognito) const override; diff --git a/test/BUILD.gn b/test/BUILD.gn index 2dbfe4e09340..e414b166e4de 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -32,7 +32,6 @@ test("brave_unit_tests") { "//brave/browser/brave_stats_updater_unittest.cc", "//brave/browser/net/brave_site_hacks_network_delegate_helper_unittest.cc", "//brave/browser/net/brave_static_redirect_network_delegate_helper_unittest.cc", - "//brave/browser/themes/theme_properties_unittest.cc", "//brave/chromium_src/chrome/browser/signin/account_consistency_disabled_unittest.cc", "//brave/chromium_src/components/version_info/brave_version_info_unittest.cc", "//brave/common/importer/brave_mock_importer_bridge.cc", @@ -170,6 +169,7 @@ test("brave_browser_tests") { "//brave/browser/extensions/brave_extension_provider_browsertest.cc", "//brave/renderer/brave_content_settings_observer_browsertest.cc", "//brave/renderer/brave_content_settings_observer_flash_browsertest.cc", + "//brave/browser/themes/brave_theme_service_browsertest.cc", "//chrome/browser/extensions/browsertest_util.cc", "//chrome/browser/extensions/browsertest_util.h", "//chrome/browser/extensions/extension_browsertest.cc",