Skip to content

Commit

Permalink
SelectedCoin pref for KeyringService (#13261)
Browse files Browse the repository at this point in the history
  • Loading branch information
supermassive committed May 16, 2022
1 parent ca0909b commit eb78e7f
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions browser/brave_prefs_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ IN_PROC_BROWSER_TEST_F(BraveProfilePrefsBrowserTest, MiscBravePrefs) {
kShowWalletIconOnToolbar));
EXPECT_FALSE(chrome_test_utils::GetProfile(this)->GetPrefs()->GetBoolean(
kBraveWalletBackupComplete));
EXPECT_EQ(static_cast<int>(brave_wallet::mojom::CoinType::ETH),
chrome_test_utils::GetProfile(this)->GetPrefs()->GetInteger(
kBraveWalletSelectedCoin));
EXPECT_FALSE(chrome_test_utils::GetProfile(this)->GetPrefs()->GetBoolean(
kMRUCyclingEnabled));
#if !BUILDFLAG(USE_GCM_FROM_PLATFORM)
Expand Down
23 changes: 23 additions & 0 deletions browser/brave_wallet/brave_wallet_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,18 @@ class BraveWalletServiceUnitTest : public testing::Test {
return default_cryptocurrency;
}

mojom::CoinType GetSelectedCoin() {
base::RunLoop run_loop;
mojom::CoinType selected_coin;
service_->GetSelectedCoin(
base::BindLambdaForTesting([&](mojom::CoinType coin_type) {
selected_coin = coin_type;
run_loop.Quit();
}));
run_loop.Run();
return selected_coin;
}

void SimulateOnGetImportInfo(const std::string& new_password,
bool result,
const ImportInfo& info,
Expand Down Expand Up @@ -1081,6 +1093,17 @@ TEST_F(BraveWalletServiceUnitTest, GetAndSetDefaultBaseCryptocurrency) {
EXPECT_EQ(GetDefaultBaseCryptocurrency(), "ETH");
}

TEST_F(BraveWalletServiceUnitTest, SelectedCoin) {
EXPECT_EQ(static_cast<int>(mojom::CoinType::ETH),
GetPrefs()->GetInteger(kBraveWalletSelectedCoin));
EXPECT_EQ(mojom::CoinType::ETH, GetSelectedCoin());

service_->SetSelectedCoin(mojom::CoinType::SOL);
EXPECT_EQ(static_cast<int>(mojom::CoinType::SOL),
GetPrefs()->GetInteger(kBraveWalletSelectedCoin));
EXPECT_EQ(mojom::CoinType::SOL, GetSelectedCoin());
}

TEST_F(BraveWalletServiceUnitTest, EthAddRemoveSetUserAssetVisible) {
mojom::BlockchainTokenPtr eth_0x4_token = GetEthToken();
eth_0x4_token->chain_id = "0x4";
Expand Down
4 changes: 4 additions & 0 deletions components/brave_wallet/browser/brave_wallet_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterStringPref(kDefaultBaseCurrency, "USD");
registry->RegisterStringPref(kDefaultBaseCryptocurrency, "BTC");
registry->RegisterBooleanPref(kShowWalletIconOnToolbar, true);
registry->RegisterBooleanPref(kShowWalletTestNetworks, false);
registry->RegisterIntegerPref(
kBraveWalletSelectedCoin,
static_cast<int>(brave_wallet::mojom::CoinType::ETH));
registry->RegisterDictionaryPref(kBraveWalletTransactions);
registry->RegisterTimePref(kBraveWalletLastUnlockTime, base::Time());
registry->RegisterTimePref(kBraveWalletP3ALastReportTime, base::Time());
Expand Down
8 changes: 8 additions & 0 deletions components/brave_wallet/browser/brave_wallet_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,14 @@ void BraveWalletService::SetDefaultBaseCryptocurrency(
}
}

void BraveWalletService::GetSelectedCoin(GetSelectedCoinCallback callback) {
std::move(callback).Run(::brave_wallet::GetSelectedCoin(prefs_));
}

void BraveWalletService::SetSelectedCoin(mojom::CoinType coin) {
::brave_wallet::SetSelectedCoin(prefs_, coin);
}

void BraveWalletService::OnDefaultWalletChanged() {
auto default_wallet = ::brave_wallet::GetDefaultWallet(prefs_);
for (const auto& observer : observers_) {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/brave_wallet_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class BraveWalletService : public KeyedService,
void GetDefaultBaseCryptocurrency(
GetDefaultBaseCryptocurrencyCallback callback) override;
void SetDefaultBaseCryptocurrency(const std::string& cryptocurrency) override;
void GetSelectedCoin(GetSelectedCoinCallback callback) override;
void SetSelectedCoin(mojom::CoinType coin) override;
void AddEthereumPermission(const url::Origin& origin,
const std::string& account,
AddEthereumPermissionCallback callback) override;
Expand Down
9 changes: 9 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,15 @@ void SetDefaultBaseCryptocurrency(PrefService* prefs,
prefs->SetString(kDefaultBaseCryptocurrency, cryptocurrency);
}

mojom::CoinType GetSelectedCoin(PrefService* prefs) {
return static_cast<brave_wallet::mojom::CoinType>(
prefs->GetInteger(kBraveWalletSelectedCoin));
}

void SetSelectedCoin(PrefService* prefs, mojom::CoinType coin) {
prefs->SetInteger(kBraveWalletSelectedCoin, static_cast<int>(coin));
}

std::string GetDefaultBaseCryptocurrency(PrefService* prefs) {
return prefs->GetString(kDefaultBaseCryptocurrency);
}
Expand Down
2 changes: 2 additions & 0 deletions components/brave_wallet/browser/brave_wallet_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ void SetDefaultBaseCurrency(PrefService* prefs, const std::string& currency);
std::string GetDefaultBaseCurrency(PrefService* prefs);
void SetDefaultBaseCryptocurrency(PrefService* prefs,
const std::string& cryptocurrency);
void SetSelectedCoin(PrefService* prefs, mojom::CoinType coin);
mojom::CoinType GetSelectedCoin(PrefService* prefs);
std::string GetDefaultBaseCryptocurrency(PrefService* prefs);
std::vector<std::string> GetAllKnownEthNetworkIds();
std::vector<std::string> GetAllKnownSolNetworkIds();
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const char kDefaultBaseCryptocurrency[] =
const char kBraveWalletTransactions[] = "brave.wallet.transactions";
const char kShowWalletIconOnToolbar[] =
"brave.wallet.show_wallet_icon_on_toolbar";
const char kBraveWalletSelectedCoin[] = "brave.wallet.selected_coin";
const char kBraveWalletLastUnlockTime[] =
"brave.wallet.wallet_last_unlock_time_v2";
const char kBraveWalletPingReportedUnlockTime[] =
Expand Down
1 change: 1 addition & 0 deletions components/brave_wallet/browser/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ extern const char kDefaultBaseCurrency[];
extern const char kDefaultBaseCryptocurrency[];
extern const char kBraveWalletTransactions[];
extern const char kShowWalletIconOnToolbar[];
extern const char kBraveWalletSelectedCoin[];
extern const char kBraveWalletLastUnlockTime[];
extern const char kBraveWalletPingReportedUnlockTime[];
extern const char kBraveWalletP3ALastReportTime[];
Expand Down
4 changes: 4 additions & 0 deletions components/brave_wallet/common/brave_wallet.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,10 @@ interface BraveWalletService {
// Checks if the Ethereum permission is allowed for the origin on the account
HasEthereumPermission(url.mojom.Origin origin, string account) => (bool success, bool has_permission);

// Controls last selected coin pref.
GetSelectedCoin() => (CoinType coin);
SetSelectedCoin(CoinType coin);

// Resets the Ethereum permission for the account
ResetEthereumPermission(url.mojom.Origin origin, string account) => (bool success);

Expand Down

0 comments on commit eb78e7f

Please sign in to comment.