Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OS(MacOS/Windows) theme controls brave theme #1805

Merged
merged 6 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,28 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_BRAVE_SYNC_LINK_LABEL" desc="Brave Sync link label">
Access Sync via
</message>
<!-- Brave Theme -->
<message name="IDS_BRAVE_THEME_TYPE_LIGHT" desc="Text for light theme type">
Light
</message>
<message name="IDS_BRAVE_THEME_TYPE_DARK" desc="Text for dark theme type">
Dark
</message>
<if expr="is_linux">
<message name="IDS_BRAVE_THEME_TYPE_SYSTEM" desc="Text for system theme type">
Same as Linux
</message>
</if>
<if expr="is_win">
<message name="IDS_BRAVE_THEME_TYPE_SYSTEM" desc="Text for system theme type">
Same as Windows
</message>
</if>
<if expr="is_macosx">
<message name="IDS_BRAVE_THEME_TYPE_SYSTEM" desc="Text for system theme type">
Same as MacOS
</message>
</if>
<!-- Brave Default Extensions -->
<message name="IDS_SETTINGS_BRAVE_DEFAULT_EXTENSIONS_TITLE" desc="The title for Brave default extensions in settings">
Extensions
Expand Down
54 changes: 9 additions & 45 deletions browser/extensions/api/brave_theme_api.cc
Original file line number Diff line number Diff line change
@@ -1,62 +1,26 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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/api/brave_theme_api.h"

#include <memory>
#include <string>

#include "base/json/json_writer.h"
#include "base/values.h"
#include "brave/browser/themes/brave_theme_service.h"
#include "brave/common/extensions/api/brave_theme.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_service.h"

using BTS = BraveThemeService;

namespace {
void SetBraveThemeTypePref(Profile* profile,
BraveThemeType type) {
profile->GetPrefs()->SetInteger(kBraveThemeType, type);
}

BraveThemeType GetBraveThemeTypeFromString(
base::StringPiece theme) {
if (theme == "Default")
return BraveThemeType::BRAVE_THEME_TYPE_DEFAULT;

if (theme == "Light")
return BraveThemeType::BRAVE_THEME_TYPE_LIGHT;

if (theme == "Dark")
return BraveThemeType::BRAVE_THEME_TYPE_DARK;

NOTREACHED();
return BraveThemeType::BRAVE_THEME_TYPE_DEFAULT;
}

} // namespace

namespace extensions {
namespace api {

ExtensionFunction::ResponseAction BraveThemeSetBraveThemeTypeFunction::Run() {
std::unique_ptr<brave_theme::SetBraveThemeType::Params> params(
brave_theme::SetBraveThemeType::Params::Create(*args_));
EXTENSION_FUNCTION_VALIDATE(params.get());

Profile* profile = Profile::FromBrowserContext(browser_context());
SetBraveThemeTypePref(profile, GetBraveThemeTypeFromString(params->type));

return RespondNow(NoArguments());
}

ExtensionFunction::ResponseAction BraveThemeGetBraveThemeTypeFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
const std::string theme_type = BTS::GetStringFromBraveThemeType(
BTS::GetActiveBraveThemeType(profile));
return RespondNow(OneArgument(std::make_unique<base::Value>(theme_type)));
ExtensionFunction::ResponseAction BraveThemeGetBraveThemeListFunction::Run() {
std::string json_string;
base::JSONWriter::Write(BraveThemeService::GetBraveThemeList(),
&json_string);
return RespondNow(OneArgument(std::make_unique<base::Value>(json_string)));
}

} // namespace api
Expand Down
19 changes: 5 additions & 14 deletions browser/extensions/api/brave_theme_api.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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/. */

Expand All @@ -10,26 +11,16 @@
namespace extensions {
namespace api {

class BraveThemeSetBraveThemeTypeFunction : public UIThreadExtensionFunction {
class BraveThemeGetBraveThemeListFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveTheme.setBraveThemeType", UNKNOWN)
DECLARE_EXTENSION_FUNCTION("braveTheme.getBraveThemeList", UNKNOWN)

protected:
~BraveThemeSetBraveThemeTypeFunction() override {}
~BraveThemeGetBraveThemeListFunction() override {}

ResponseAction Run() override;
};

class BraveThemeGetBraveThemeTypeFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveTheme.getBraveThemeType", UNKNOWN)

protected:
~BraveThemeGetBraveThemeTypeFunction() override {}

ResponseAction Run() override;
};

} // namespace api
} // namespace extensions

Expand Down
68 changes: 12 additions & 56 deletions browser/extensions/api/brave_theme_api_browsertest.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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/. */

Expand All @@ -18,24 +19,21 @@
#include "extensions/common/extension_builder.h"
#include "testing/gmock/include/gmock/gmock.h"

using extensions::api::BraveThemeGetBraveThemeTypeFunction;
using extensions::api::BraveThemeSetBraveThemeTypeFunction;
using extension_function_test_utils::RunFunctionAndReturnSingleResult;
using BTS = BraveThemeService;

class BraveThemeAPIBrowserTest : public InProcessBrowserTest {
public:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
extension_ = extensions::ExtensionBuilder("Test").Build();
}
public:
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
extension_ = extensions::ExtensionBuilder("Test").Build();
}

scoped_refptr<const extensions::Extension> extension() {
return extension_;
}
scoped_refptr<const extensions::Extension> extension() {
return extension_;
}

private:
scoped_refptr<const extensions::Extension> extension_;
private:
scoped_refptr<const extensions::Extension> extension_;
};

namespace {
Expand All @@ -52,48 +50,6 @@ void SetBraveThemeType(Profile* profile, BraveThemeType type) {
}
} // namespace

IN_PROC_BROWSER_TEST_F(BraveThemeAPIBrowserTest,
BraveThemeGetBraveThemeTypeTest) {
Profile* profile = browser()->profile();

// Check default type is set initially.
EXPECT_EQ(BraveThemeType::BRAVE_THEME_TYPE_DEFAULT,
BTS::GetUserPreferredBraveThemeType(profile));

// Change to Light type and check it from api.
SetBraveThemeType(profile, BraveThemeType::BRAVE_THEME_TYPE_LIGHT);
EXPECT_EQ(BraveThemeType::BRAVE_THEME_TYPE_LIGHT,
BTS::GetUserPreferredBraveThemeType(profile));
scoped_refptr<BraveThemeGetBraveThemeTypeFunction> get_function(
new BraveThemeGetBraveThemeTypeFunction());
get_function->set_extension(extension().get());
std::unique_ptr<base::Value> value;
value.reset(RunFunctionAndReturnSingleResult(get_function.get(),
std::string("[]"),
browser()));
EXPECT_EQ(value->GetString(), "Light");
}

IN_PROC_BROWSER_TEST_F(BraveThemeAPIBrowserTest,
BraveThemeSetBraveThemeTypeTest) {
Profile* profile = browser()->profile();

// Check default type is set initially.
EXPECT_EQ(BraveThemeType::BRAVE_THEME_TYPE_DEFAULT,
BTS::GetUserPreferredBraveThemeType(profile));

// Change theme type to Light via api and check it.
scoped_refptr<BraveThemeSetBraveThemeTypeFunction> set_function(
new BraveThemeSetBraveThemeTypeFunction());
set_function->set_extension(extension().get());
RunFunctionAndReturnSingleResult(set_function.get(),
std::string("[\"Light\"]"),
browser());

EXPECT_EQ(BraveThemeType::BRAVE_THEME_TYPE_LIGHT,
BTS::GetUserPreferredBraveThemeType(profile));
}

IN_PROC_BROWSER_TEST_F(BraveThemeAPIBrowserTest,
BraveThemeEventRouterTest) {
Profile* profile = browser()->profile();
Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() {
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kHideBraveRewardsButton] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
(*s_brave_whitelist)[kBraveThemeType] =
settings_api::PrefType::PREF_TYPE_NUMBER;
// Clear browsing data on exit prefs.
(*s_brave_whitelist)[browsing_data::prefs::kDeleteBrowsingHistoryOnExit] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
Expand Down
25 changes: 21 additions & 4 deletions browser/extensions/brave_theme_event_router.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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 <utility>
#include <string>

#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 {
std::string GetStringFromBraveThemeType(
BraveThemeType type) {
switch (type) {
case BraveThemeType::BRAVE_THEME_TYPE_LIGHT:
return "Light";
case BraveThemeType::BRAVE_THEME_TYPE_DARK:
return "Dark";
default:
NOTREACHED();
return "Default";
}
}
} // namespace

namespace extensions {

Expand All @@ -27,8 +44,8 @@ class BraveThemeEventRouterImpl : public BraveThemeEventRouter {

void BraveThemeEventRouterImpl::OnBraveThemeTypeChanged(Profile* profile) {
EventRouter* event_router = EventRouter::Get(profile);
const std::string theme_type = BTS::GetStringFromBraveThemeType(
BTS::GetActiveBraveThemeType(profile));
const std::string theme_type = GetStringFromBraveThemeType(
BraveThemeService::GetActiveBraveThemeType(profile));

auto event = std::make_unique<extensions::Event>(
extensions::events::BRAVE_ON_BRAVE_THEME_TYPE_CHANGED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,16 @@ cr.define('settings', function() {
/**
* @return {!Promise<string>}
*/
getBraveThemeType() {}
/**
* @param {string} theme name.
*/
setBraveThemeType(theme) {}
getBraveThemeList() {}
}

/**
* @implements {settings.BraveAppearanceBrowserProxy}
*/
class BraveAppearanceBrowserProxyImpl {
/** @override */
getBraveThemeType() {
return new Promise(resolve => chrome.braveTheme.getBraveThemeType(resolve))
}

/** @override */
setBraveThemeType(theme) {
chrome.braveTheme.setBraveThemeType(theme);
getBraveThemeList() {
return new Promise(resolve => chrome.braveTheme.getBraveThemeList(resolve))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@
</style>
<div class="settings-box">
<div class="start">$i18n{appearanceSettingsBraveTheme}</div>
<select id="braveThemeType" class="md-select"
on-change="onBraveThemeTypeChange_">
<template is="dom-repeat" items="[[braveThemeTypes_]]">
<option value="[[item]]"
selected="[[braveThemeTypeEqual_(item, braveThemeType_)]]">
[[item]]
</option>
</template>
</select>
<settings-dropdown-menu id="braveThemeType"
label="$i18n{appearanceSettingsBraveTheme}"
pref="{{prefs.brave.theme.type}}"
menu-options="[[braveThemeList_]]">
</settings-dropdown-menu>
</div>
</template>
<script src="brave_appearance_page.js"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ Polymer({
is: 'settings-brave-appearance-theme',

properties: {
braveThemeTypes_: {
readOnly: true,
type: Array,
value: [
'Light',
'Dark',
],
},
braveThemeType_: String,
braveThemeList_: [],
},

/** @private {?settings.BraveAppearanceBrowserProxy} */
Expand All @@ -30,24 +22,10 @@ Polymer({

/** @override */
ready: function() {
this.browserProxy_.getBraveThemeType().then(theme => {
this.braveThemeType_ = theme;
this.browserProxy_.getBraveThemeList().then(list => {
this.braveThemeList_ = JSON.parse(list);
});
},

/**
* @param {string} theme1
* @param {string} theme2
* @return {boolean}
* @private
*/
braveThemeTypeEqual_: function(theme1, theme2) {
return theme1 === theme2;
},

onBraveThemeTypeChange_: function() {
this.browserProxy_.setBraveThemeType(this.$.braveThemeType.value);
},
});

/**
Expand Down
4 changes: 4 additions & 0 deletions browser/themes/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ source_set("themes") {
"brave_theme_service.h",
"brave_theme_service_win.cc",
"brave_theme_service_win.h",
"brave_theme_utils.h",
"brave_theme_utils_linux.cc",
"brave_theme_utils_mac.mm",
"brave_theme_utils_win.cc",
"theme_properties.cc",
"theme_properties.h",
]
Expand Down
Loading