Skip to content

Commit

Permalink
Add onBraveThemetypeChanged event to chrome.braveTheme
Browse files Browse the repository at this point in the history
Covered by BraveThemeAPIBrowserTest.BraveThemeEventRouterTest.
  • Loading branch information
simonhong committed Nov 6, 2018
1 parent 495a038 commit 60e15ff
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 29 deletions.
2 changes: 2 additions & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down
19 changes: 4 additions & 15 deletions browser/extensions/api/brave_theme_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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<base::Value>(theme_type)));
}

Expand Down
24 changes: 23 additions & 1 deletion browser/extensions/api/brave_theme_api_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

#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"
#include "chrome/test/base/in_process_browser_test.h"
#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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<BraveThemeService*>(
ThemeServiceFactory::GetForProfile(browser()->profile()));
service->SetBraveThemeEventRouterForTesting(mock_router_);
SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_LIGHT);
}
47 changes: 47 additions & 0 deletions browser/extensions/brave_theme_event_router.cc
Original file line number Diff line number Diff line change
@@ -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::Event>(
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> BraveThemeEventRouter::Create() {
return std::make_unique<BraveThemeEventRouterImpl>();
}

} // namespace extensions
25 changes: 25 additions & 0 deletions browser/extensions/brave_theme_event_router.h
Original file line number Diff line number Diff line change
@@ -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 <memory>

class Profile;

namespace extensions {

class BraveThemeEventRouter {
public:
static std::unique_ptr<BraveThemeEventRouter> Create();

virtual ~BraveThemeEventRouter() {}

virtual void OnBraveThemeTypeChanged(Profile* profile) = 0;
};

} // namespace extensions

#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_THEME_EVENT_ROUTER_H_
48 changes: 37 additions & 11 deletions browser/themes/brave_theme_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

#include "brave/browser/themes/brave_theme_service.h"

#include <string>

#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"
Expand All @@ -31,18 +30,35 @@ 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
return static_cast<BraveThemeType>(
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) {
Expand Down Expand Up @@ -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<SkColor> braveColor = MaybeGetDefaultColorForBraveUi(id, incognito, theme);
const base::Optional<SkColor> braveColor =
MaybeGetDefaultColorForBraveUi(id, incognito, theme);
if (braveColor)
return braveColor.value();
// make sure we fallback to chrome's dark theme (incognito) for our dark theme
Expand All @@ -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);
}
15 changes: 15 additions & 0 deletions browser/themes/brave_theme_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
#ifndef BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_
#define BRAVE_BROWSER_THEMES_BRAVE_THEME_SERVICE_H_

#include <memory>
#include <string>

#include "chrome/browser/themes/theme_service.h"
#include "components/prefs/pref_member.h"

namespace extensions {
class BraveThemeEventRouter;
}

namespace user_prefs {
class PrefRegistrySyncable;
}
Expand All @@ -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();
Expand All @@ -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<extensions::BraveThemeEventRouter> brave_theme_event_router_;

DISALLOW_COPY_AND_ASSIGN(BraveThemeService);
};

Expand Down
14 changes: 14 additions & 0 deletions common/extensions/api/brave_theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ test("brave_browser_tests") {
"//ppapi/buildflags",
":brave_browser_tests_deps",
":browser_tests_runner",
"//testing/gmock",
]
data_deps = [
"//ppapi:ppapi_tests",
Expand Down

0 comments on commit 60e15ff

Please sign in to comment.