diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn index b4e90ed80ac2..7d359e2bef0c 100644 --- a/browser/extensions/BUILD.gn +++ b/browser/extensions/BUILD.gn @@ -32,6 +32,8 @@ source_set("extensions") { "brave_extension_provider.h", "brave_extension_service.cc", "brave_extension_service.h", + "brave_theme_event_router.cc", + "brave_theme_event_router.h", "brave_tor_client_updater.cc", "brave_tor_client_updater.h", ] diff --git a/browser/extensions/api/brave_theme_api.cc b/browser/extensions/api/brave_theme_api.cc index 3c598e876155..f29b7a38bd1c 100644 --- a/browser/extensions/api/brave_theme_api.cc +++ b/browser/extensions/api/brave_theme_api.cc @@ -13,6 +13,8 @@ #include "chrome/browser/profiles/profile.h" #include "components/prefs/pref_service.h" +using BTS = BraveThemeService; + namespace { void SetBraveThemeTypePref(Profile* profile, BraveThemeType type) { @@ -34,19 +36,6 @@ BraveThemeType GetBraveThemeTypeFromString( return BraveThemeType::BRAVE_THEME_TYPE_DEFAULT; } -std::string GetStringFromBraveThemeType( - BraveThemeType theme) { - switch (theme) { - case BraveThemeType::BRAVE_THEME_TYPE_DEFAULT: - return "Default"; - case BraveThemeType::BRAVE_THEME_TYPE_LIGHT: - return "Light"; - case BraveThemeType::BRAVE_THEME_TYPE_DARK: - return "Dark"; - default: - NOTREACHED(); - } -} } // namespace namespace extensions { @@ -65,8 +54,8 @@ ExtensionFunction::ResponseAction BraveThemeSetBraveThemeTypeFunction::Run() { ExtensionFunction::ResponseAction BraveThemeGetBraveThemeTypeFunction::Run() { Profile* profile = Profile::FromBrowserContext(browser_context()); - const std::string theme_type = GetStringFromBraveThemeType( - BraveThemeService::GetUserPreferredBraveThemeType(profile)); + const std::string theme_type = BTS::GetStringFromBraveThemeType( + BTS::GetUserPreferredBraveThemeType(profile)); return RespondNow(OneArgument(std::make_unique(theme_type))); } diff --git a/browser/extensions/api/brave_theme_api_browsertest.cc b/browser/extensions/api/brave_theme_api_browsertest.cc index 756f4898a7cb..b22d09137f96 100644 --- a/browser/extensions/api/brave_theme_api_browsertest.cc +++ b/browser/extensions/api/brave_theme_api_browsertest.cc @@ -4,6 +4,7 @@ #include "base/values.h" #include "brave/browser/extensions/api/brave_theme_api.h" +#include "brave/browser/extensions/brave_theme_event_router.h" #include "brave/browser/themes/brave_theme_service.h" #include "brave/browser/themes/theme_properties.h" #include "brave/common/pref_names.h" @@ -11,10 +12,11 @@ #include "chrome/browser/extensions/extension_function_test_utils.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/themes/theme_properties.h" -#include "chrome/browser/themes/theme_service.h" +#include "chrome/browser/themes/theme_service_factory.h" #include "chrome/browser/ui/browser.h" #include "components/prefs/pref_service.h" #include "extensions/common/extension_builder.h" +#include "testing/gmock/include/gmock/gmock.h" using extensions::api::BraveThemeGetBraveThemeTypeFunction; using extensions::api::BraveThemeSetBraveThemeTypeFunction; @@ -37,6 +39,14 @@ class BraveThemeAPIBrowserTest : public InProcessBrowserTest { }; namespace { +class MockBraveThemeEventRouter : public extensions::BraveThemeEventRouter { + public: + MockBraveThemeEventRouter() {} + ~MockBraveThemeEventRouter() override {} + + MOCK_METHOD1(OnBraveThemeTypeChanged, void(Profile*)); +}; + void SetBraveThemeType(Profile* profile, BraveThemeType type) { profile->GetPrefs()->SetInteger(kBraveThemeType, type); } @@ -83,3 +93,15 @@ IN_PROC_BROWSER_TEST_F(BraveThemeAPIBrowserTest, EXPECT_EQ(BraveThemeType::BRAVE_THEME_TYPE_LIGHT, BTS::GetUserPreferredBraveThemeType(profile)); } + +IN_PROC_BROWSER_TEST_F(BraveThemeAPIBrowserTest, + BraveThemeEventRouterTest) { + Profile* profile = browser()->profile(); + MockBraveThemeEventRouter* mock_router_ = new MockBraveThemeEventRouter; + EXPECT_CALL(*mock_router_, OnBraveThemeTypeChanged(profile)).Times(1); + + BraveThemeService* service = static_cast( + ThemeServiceFactory::GetForProfile(browser()->profile())); + service->SetBraveThemeEventRouterForTesting(mock_router_); + SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_LIGHT); +} diff --git a/browser/extensions/brave_theme_event_router.cc b/browser/extensions/brave_theme_event_router.cc new file mode 100644 index 000000000000..8ae27e7be176 --- /dev/null +++ b/browser/extensions/brave_theme_event_router.cc @@ -0,0 +1,47 @@ +/* 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/extensions/brave_theme_event_router.h" + +#include "brave/browser/themes/brave_theme_service.h" +#include "brave/common/extensions/api/brave_theme.h" +#include "chrome/browser/profiles/profile.h" +#include "extensions/browser/event_router.h" +#include "extensions/browser/extension_event_histogram_value.h" + +using BTS = BraveThemeService; + +namespace extensions { + +class BraveThemeEventRouterImpl : public BraveThemeEventRouter { + public: + BraveThemeEventRouterImpl() {} + ~BraveThemeEventRouterImpl() override {} + + void OnBraveThemeTypeChanged(Profile* profile) override; + + private: + DISALLOW_COPY_AND_ASSIGN(BraveThemeEventRouterImpl); +}; + +void BraveThemeEventRouterImpl::OnBraveThemeTypeChanged(Profile* profile) { + EventRouter* event_router = EventRouter::Get(profile); + const std::string theme_type = BTS::GetStringFromBraveThemeType( + BTS::GetActiveBraveThemeType(profile)); + + auto event = std::make_unique( + extensions::events::BRAVE_ON_BRAVE_THEME_TYPE_CHANGED, + api::brave_theme::OnBraveThemeTypeChanged::kEventName, + api::brave_theme::OnBraveThemeTypeChanged::Create(theme_type), + profile); + + event_router->BroadcastEvent(std::move(event)); +} + +// static +std::unique_ptr BraveThemeEventRouter::Create() { + return std::make_unique(); +} + +} // namespace extensions diff --git a/browser/extensions/brave_theme_event_router.h b/browser/extensions/brave_theme_event_router.h new file mode 100644 index 000000000000..efa8c8cd86f5 --- /dev/null +++ b/browser/extensions/brave_theme_event_router.h @@ -0,0 +1,25 @@ +/* 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_EXTENSIONS_BRAVE_THEME_EVENT_ROUTER_H_ +#define BRAVE_BROWSER_EXTENSIONS_BRAVE_THEME_EVENT_ROUTER_H_ + +#include + +class Profile; + +namespace extensions { + +class BraveThemeEventRouter { + public: + static std::unique_ptr Create(); + + virtual ~BraveThemeEventRouter() {} + + virtual void OnBraveThemeTypeChanged(Profile* profile) = 0; +}; + +} // namespace extensions + +#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_THEME_EVENT_ROUTER_H_ diff --git a/browser/themes/brave_theme_service.cc b/browser/themes/brave_theme_service.cc index ecb15a805fa2..720c082894b9 100644 --- a/browser/themes/brave_theme_service.cc +++ b/browser/themes/brave_theme_service.cc @@ -4,13 +4,12 @@ #include "brave/browser/themes/brave_theme_service.h" -#include - -#include "brave/common/brave_switches.h" -#include "brave/browser/themes/theme_properties.h" -#include "brave/common/pref_names.h" #include "base/command_line.h" #include "base/strings/string_util.h" +#include "brave/browser/extensions/brave_theme_event_router.h" +#include "brave/browser/themes/theme_properties.h" +#include "brave/common/brave_switches.h" +#include "brave/common/pref_names.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/themes/theme_service_factory.h" #include "chrome/common/channel_info.h" @@ -31,11 +30,13 @@ BraveThemeType BraveThemeService::GetUserPreferredBraveThemeType( const base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess(); if (command_line.HasSwitch(switches::kUiMode)) { - std::string requested_theme_value = command_line.GetSwitchValueASCII(switches::kUiMode); - std::string requested_theme_value_lower = base::ToLowerASCII(requested_theme_value); + std::string requested_theme_value = + command_line.GetSwitchValueASCII(switches::kUiMode); + std::string requested_theme_value_lower = + base::ToLowerASCII(requested_theme_value); if (requested_theme_value_lower == "light") return BraveThemeType::BRAVE_THEME_TYPE_LIGHT; - if (requested_theme_value_lower == "light") + if (requested_theme_value_lower == "dark") return BraveThemeType::BRAVE_THEME_TYPE_DARK; } // get value from preferences @@ -43,6 +44,21 @@ BraveThemeType BraveThemeService::GetUserPreferredBraveThemeType( profile->GetPrefs()->GetInteger(kBraveThemeType)); } +// static +std::string BraveThemeService::GetStringFromBraveThemeType( + BraveThemeType type) { + switch (type) { + case BraveThemeType::BRAVE_THEME_TYPE_DEFAULT: + return "Default"; + case BraveThemeType::BRAVE_THEME_TYPE_LIGHT: + return "Light"; + case BraveThemeType::BRAVE_THEME_TYPE_DARK: + return "Dark"; + default: + NOTREACHED(); + } +} + // static BraveThemeType BraveThemeService::GetActiveBraveThemeType( Profile* profile) { @@ -81,14 +97,13 @@ void BraveThemeService::Init(Profile* profile) { ThemeService::Init(profile); } - - SkColor BraveThemeService::GetDefaultColor(int id, bool incognito) const { // Brave Tor profiles are always 'incognito' (for now) if (!incognito && profile()->IsTorProfile()) incognito = true; const BraveThemeType theme = GetActiveBraveThemeType(profile()); - const base::Optional braveColor = MaybeGetDefaultColorForBraveUi(id, incognito, theme); + const base::Optional braveColor = + MaybeGetDefaultColorForBraveUi(id, incognito, theme); if (braveColor) return braveColor.value(); // make sure we fallback to chrome's dark theme (incognito) for our dark theme @@ -100,4 +115,15 @@ SkColor BraveThemeService::GetDefaultColor(int id, bool incognito) const { void BraveThemeService::OnPreferenceChanged(const std::string& pref_name) { DCHECK(pref_name == kBraveThemeType); NotifyThemeChanged(); + + if (!brave_theme_event_router_) + brave_theme_event_router_ = extensions::BraveThemeEventRouter::Create(); + + brave_theme_event_router_->OnBraveThemeTypeChanged(profile()); +} + + +void BraveThemeService::SetBraveThemeEventRouterForTesting( + extensions::BraveThemeEventRouter* mock_router) { + brave_theme_event_router_.reset(mock_router); } diff --git a/browser/themes/brave_theme_service.h b/browser/themes/brave_theme_service.h index c90d906d1653..08b4b34bca79 100644 --- a/browser/themes/brave_theme_service.h +++ b/browser/themes/brave_theme_service.h @@ -5,9 +5,16 @@ #ifndef BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_ #define BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_ +#include +#include + #include "chrome/browser/themes/theme_service.h" #include "components/prefs/pref_member.h" +namespace extensions { +class BraveThemeEventRouter; +} + namespace user_prefs { class PrefRegistrySyncable; } @@ -22,6 +29,7 @@ class BraveThemeService : public ThemeService { public: static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); static BraveThemeType GetUserPreferredBraveThemeType(Profile* profile); + static std::string GetStringFromBraveThemeType(BraveThemeType type); static BraveThemeType GetActiveBraveThemeType(Profile* profile); BraveThemeService(); @@ -35,10 +43,17 @@ class BraveThemeService : public ThemeService { SkColor GetDefaultColor(int id, bool incognito) const override; private: + FRIEND_TEST_ALL_PREFIXES(BraveThemeAPIBrowserTest, BraveThemeEventRouterTest); + // Own |mock_router|. + void SetBraveThemeEventRouterForTesting( + extensions::BraveThemeEventRouter* mock_router); + void OnPreferenceChanged(const std::string& pref_name); IntegerPrefMember brave_theme_type_pref_; + std::unique_ptr brave_theme_event_router_; + DISALLOW_COPY_AND_ASSIGN(BraveThemeService); }; diff --git a/common/extensions/api/brave_theme.json b/common/extensions/api/brave_theme.json index 73fe03feff70..c495003657bd 100644 --- a/common/extensions/api/brave_theme.json +++ b/common/extensions/api/brave_theme.json @@ -9,6 +9,20 @@ "compiler_options": { "implemented_in": "brave/browser/extensions/api/brave_theme_api.h" }, + "events": [ + { + "name": "onBraveThemeTypeChanged", + "type": "function", + "description": "Fired when brave theme is changed", + "parameters": [ + { + "name": "type", + "type": "string", + "description": "new active theme type(ex, Dark or Light)" + } + ] + } + ], "functions": [ { "name": "setBraveThemeType", diff --git a/patches/extensions-browser-extension_event_histogram_value.h.patch b/patches/extensions-browser-extension_event_histogram_value.h.patch index 775578488ff8..4303b30890a0 100644 --- a/patches/extensions-browser-extension_event_histogram_value.h.patch +++ b/patches/extensions-browser-extension_event_histogram_value.h.patch @@ -1,8 +1,8 @@ diff --git a/extensions/browser/extension_event_histogram_value.h b/extensions/browser/extension_event_histogram_value.h -index cd8295f9c966d188fc58f3b37e1b4203c24e297d..f40357ea6b5ccb2fd4a6613234eb5b3017997b48 100644 +index cd8295f9c966d188fc58f3b37e1b4203c24e297d..519fbbed3c03d92906bc0f212ac9ca93fc564d6f 100644 --- a/extensions/browser/extension_event_histogram_value.h +++ b/extensions/browser/extension_event_histogram_value.h -@@ -444,6 +444,17 @@ enum HistogramValue { +@@ -444,6 +444,18 @@ enum HistogramValue { SYSTEM_POWER_SOURCE_ONPOWERCHANGED = 423, WEB_REQUEST_ON_ACTION_IGNORED = 424, ARC_APPS_PRIVATE_ON_INSTALLED = 425, @@ -12,6 +12,7 @@ index cd8295f9c966d188fc58f3b37e1b4203c24e297d..f40357ea6b5ccb2fd4a6613234eb5b30 + BRAVE_ON_WALLET_PROPERTIES, + BRAVE_ON_PUBLISHER_DATA, + BRAVE_ON_CURRENT_REPORT, ++ BRAVE_ON_BRAVE_THEME_TYPE_CHANGED, + BRAVE_REWARDS_NOTIFICATION_ADDED, + BRAVE_REWARDS_NOTIFICATION_DELETED, + BRAVE_REWARDS_ALL_NOTIFICATIONS_DELETED, diff --git a/test/BUILD.gn b/test/BUILD.gn index 500033f7e997..390d129fdf60 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -283,6 +283,7 @@ test("brave_browser_tests") { "//ppapi/buildflags", ":brave_browser_tests_deps", ":browser_tests_runner", + "//testing/gmock", ] data_deps = [ "//ppapi:ppapi_tests",