Skip to content

Commit

Permalink
Make BraveThemeService observe kBraveThemeType prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhong committed Aug 24, 2018
1 parent 450eacf commit e53cff5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 19 deletions.
28 changes: 19 additions & 9 deletions browser/themes/brave_theme_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

#include "brave/browser/themes/theme_properties.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/themes/theme_service_factory.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"

Expand All @@ -18,17 +18,22 @@ void BraveThemeService::RegisterProfilePrefs(
}

// static
int BraveThemeService::GetBraveThemeType(Profile* profile) {
return profile->GetPrefs()->GetInteger(kBraveThemeType);
BraveThemeService::BraveThemeType BraveThemeService::GetBraveThemeType(Profile* profile) {
return static_cast<BraveThemeService::BraveThemeType>(
profile->GetPrefs()->GetInteger(kBraveThemeType));
}

// static
void BraveThemeService::SetBraveThemeType(Profile* profile,
BraveThemeType type) {
profile->GetPrefs()->SetInteger(kBraveThemeType, type);
BraveThemeService::BraveThemeService() {}

static_cast<BraveThemeService*>(ThemeServiceFactory::GetForProfile(profile))
->NotifyThemeChanged();
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 {
Expand All @@ -39,3 +44,8 @@ SkColor BraveThemeService::GetDefaultColor(int id, bool incognito) const {

return ThemeService::GetDefaultColor(id, incognito);
}

void BraveThemeService::OnPreferenceChanged(const std::string& pref_name) {
DCHECK(pref_name == kBraveThemeType);
NotifyThemeChanged();
}
15 changes: 11 additions & 4 deletions browser/themes/brave_theme_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#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;
Expand All @@ -20,17 +21,23 @@ class BraveThemeService : public ThemeService {
};

static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
static int GetBraveThemeType(Profile* profile);
static void SetBraveThemeType(Profile* profile, BraveThemeType type);
static BraveThemeType GetBraveThemeType(Profile* profile);

BraveThemeService() = default;
~BraveThemeService() override = default;
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);
};

Expand Down
12 changes: 10 additions & 2 deletions browser/themes/brave_theme_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
* 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)
Expand All @@ -23,7 +31,7 @@ IN_PROC_BROWSER_TEST_F(BraveThemeServiceTest, BraveThemeChangeTest) {
EXPECT_EQ(BTS::BRAVE_THEME_TYPE_DEFAULT, BTS::GetBraveThemeType(profile));

const ui::ThemeProvider& tp = ThemeService::GetThemeProviderForProfile(profile);
BTS::SetBraveThemeType(browser()->profile(), BTS::BRAVE_THEME_TYPE_LIGHT);
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));
Expand All @@ -32,7 +40,7 @@ IN_PROC_BROWSER_TEST_F(BraveThemeServiceTest, BraveThemeChangeTest) {
EXPECT_EQ(dark_frame_color, tp.GetColor(ThemeProperties::COLOR_FRAME));
#endif

BTS::SetBraveThemeType(browser()->profile(), BTS::BRAVE_THEME_TYPE_DARK);
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));
}
2 changes: 2 additions & 0 deletions browser/themes/theme_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
#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"

namespace {
Expand Down
6 changes: 2 additions & 4 deletions patches/chrome-browser-themes-theme_service_win.h.patch
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
diff --git a/chrome/browser/themes/theme_service_win.h b/chrome/browser/themes/theme_service_win.h
index eba1cb2596d71b91e06183209503f3fc83b0e593..966a408a3080c6941db428b554ace6c66f6aead6 100644
index eba1cb2596d71b91e06183209503f3fc83b0e593..e6261a982859a6a66caa00d362f290d247ce7922 100644
--- a/chrome/browser/themes/theme_service_win.h
+++ b/chrome/browser/themes/theme_service_win.h
@@ -18,6 +18,9 @@ class ThemeServiceWin : public ThemeService {
@@ -18,6 +18,7 @@ class ThemeServiceWin : public ThemeService {
~ThemeServiceWin() override;

private:
+#if defined(BRAVE_CHROMIUM_BUILD)
+ friend class BraveThemeServiceWin;
+#endif
// ThemeService:
bool ShouldUseNativeFrame() const override;
SkColor GetDefaultColor(int id, bool incognito) const override;

0 comments on commit e53cff5

Please sign in to comment.