-
Notifications
You must be signed in to change notification settings - Fork 889
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
50 changed files
with
1,058 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import("//brave/components/brave_wallet/browser/buildflags/buildflags.gni") | ||
|
||
assert(brave_wallet_enabled) | ||
|
||
source_set("brave_wallet") { | ||
sources = [ | ||
"brave_wallet_service_factory.h", | ||
"brave_wallet_service_factory.cc", | ||
"brave_wallet_utils.cc", | ||
"brave_wallet_utils.h", | ||
] | ||
deps = [ | ||
"//components/prefs", | ||
"//extensions/browser", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* 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/widevine/widevine_utils.h" | ||
|
||
#include "brave/common/brave_wallet_constants.h" | ||
#include "brave/common/extensions/extension_constants.h" | ||
#include "brave/common/pref_names.h" | ||
#include "chrome/browser/profiles/profile.h" | ||
#include "components/prefs/pref_registry_simple.h" | ||
#include "extensions/browser/extension_prefs.h" | ||
|
||
namespace brave_wallet { | ||
|
||
void MigrateBraveWalletPrefs(Profile* profile) { | ||
LOG(ERROR) << "===checking if should do migration of prefs"; | ||
PrefService* prefs = profile->GetPrefs(); | ||
if (!prefs->HasPrefPath(kBraveWalletEnabledDeprecated)) { | ||
LOG(ERROR) << "===Skipping brave wallet enabled check because it doesn't exist"; | ||
return; | ||
} | ||
LOG(ERROR) << "====proceeding with brave wallet enabled migration"; | ||
bool wallet_enabled = prefs->GetBoolean(kBraveWalletEnabledDeprecated); | ||
bool has_crypto_wallets = extensions::ExtensionPrefs::Get(profile)-> | ||
HasPrefForExtension(ethereum_remote_client_extension_id); | ||
bool has_metamask = extensions::ExtensionPrefs::Get(profile)-> | ||
HasPrefForExtension(metamask_extension_id); | ||
|
||
BraveWalletWeb3ProviderTypes provider = | ||
BraveWalletWeb3ProviderTypes::ASK; | ||
if (!wallet_enabled && has_metamask) { | ||
// If Crypto Wallets was disabled and MetaMask is installed, set to MetaMask | ||
provider = BraveWalletWeb3ProviderTypes::METAMASK; | ||
} else if (!wallet_enabled && !has_metamask) { | ||
// If Crypto Wallets is diabled, and MetaMask not installed, set None | ||
provider = BraveWalletWeb3ProviderTypes::NONE; | ||
} else if (wallet_enabled && has_metamask) { | ||
// If Crypto Wallets is enabled, and MetaMask is installed, set | ||
// to Crypto Wallets | ||
provider = BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS; | ||
} else if (has_crypto_wallets && wallet_enabled) { | ||
// If CryptoWallets is enabled and installed, but MetaMask is not | ||
// installed, set Crypto Wallets. | ||
provider = BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS; | ||
} else if (!has_crypto_wallets && wallet_enabled) { | ||
// If CryptoWallets is enabled and not installed yet, and MetaMask is not | ||
// installed, set Ask | ||
provider = BraveWalletWeb3ProviderTypes::ASK; | ||
} | ||
prefs->SetInteger(kBraveWalletWeb3Provider, static_cast<int>(provider)); | ||
prefs->ClearPref(kBraveWalletEnabledDeprecated); | ||
} | ||
|
||
} // brave_wallet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* 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/. */ | ||
|
||
#ifndef BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_ | ||
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_ | ||
|
||
class Profile; | ||
|
||
namespace brave_wallet { | ||
|
||
void MigrateBraveWalletPrefs(Profile* profile); | ||
|
||
} // namespace brave_wallet | ||
|
||
#endif // BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* 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/brave_wallet/brave_wallet_utils.h" | ||
|
||
#include "brave/common/brave_wallet_constants.h" | ||
#include "brave/common/pref_names.h" | ||
#include "brave/common/extensions/extension_constants.h" | ||
#include "chrome/browser/prefs/browser_prefs.h" | ||
#include "chrome/test/base/testing_profile.h" | ||
#include "components/sync_preferences/testing_pref_service_syncable.h" | ||
#include "content/public/test/browser_task_environment.h" | ||
#include "extensions/browser/extension_prefs.h" | ||
#include "extensions/browser/extension_registry.h" | ||
#include "extensions/common/extension_builder.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
class BraveWalletUtilsUnitTest : public testing::Test { | ||
public: | ||
void SetUp() override { | ||
profile_ = CreateProfile(); | ||
} | ||
|
||
Profile* profile() { | ||
return profile_.get(); | ||
} | ||
|
||
void AddCryptoWallets() { | ||
AddExtension(ethereum_remote_client_extension_id, crypto_wallets_extension); | ||
} | ||
|
||
void AddMetaMask() { | ||
AddExtension(metamask_extension_id, metamask_extension); | ||
} | ||
|
||
private: | ||
void AddExtension(const std::string& extension_id, | ||
scoped_refptr<const extensions::Extension>& extension) { | ||
extensions::DictionaryBuilder manifest; | ||
manifest.Set("name", "ext") | ||
.Set("version", "0.1") | ||
.Set("manifest_version", 2); | ||
extension = extensions::ExtensionBuilder() | ||
.SetManifest(manifest.Build()) | ||
.SetID(extension_id) | ||
.Build(); | ||
ASSERT_TRUE(extension); | ||
extensions::ExtensionPrefs::Get(profile())->UpdateExtensionPref(extension_id, | ||
"test", std::make_unique<base::Value>("")); | ||
extensions::ExtensionRegistry::Get(profile())->AddEnabled(extension.get()); | ||
} | ||
|
||
std::unique_ptr<TestingProfile> CreateProfile() { | ||
TestingProfile::Builder builder; | ||
auto prefs = | ||
std::make_unique<sync_preferences::TestingPrefServiceSyncable>(); | ||
RegisterUserProfilePrefs(prefs->registry()); | ||
prefs->registry()->RegisterBooleanPref(kBraveWalletEnabledDeprecated, true); | ||
builder.SetPrefService(std::move(prefs)); | ||
return builder.Build(); | ||
} | ||
|
||
content::BrowserTaskEnvironment task_environment_; | ||
std::unique_ptr<TestingProfile> profile_; | ||
scoped_refptr<const extensions::Extension> metamask_extension; | ||
scoped_refptr<const extensions::Extension> crypto_wallets_extension; | ||
}; | ||
|
||
// If Crypto Wallets was disabled and MetaMask is installed, set to MetaMask | ||
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationMMCryptoWalletsDisabled) { | ||
AddMetaMask(); | ||
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, false); | ||
|
||
brave_wallet::MigrateBraveWalletPrefs(profile()); | ||
|
||
auto provider = static_cast<BraveWalletWeb3ProviderTypes>( | ||
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider)); | ||
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::METAMASK); | ||
} | ||
|
||
// If Crypto Wallets is diabled, and MetaMask not installed, set None | ||
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationCryptoWalletsDisabled) { | ||
AddCryptoWallets(); | ||
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, false); | ||
|
||
brave_wallet::MigrateBraveWalletPrefs(profile()); | ||
|
||
auto provider = static_cast<BraveWalletWeb3ProviderTypes>( | ||
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider)); | ||
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::NONE); | ||
} | ||
|
||
// If Crypto Wallets is enabled, and MetaMask is installed, set | ||
// to Crypto Wallets | ||
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationCryptoWalletsAndMMInstalled) { | ||
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, true); | ||
AddCryptoWallets(); | ||
AddMetaMask(); | ||
|
||
brave_wallet::MigrateBraveWalletPrefs(profile()); | ||
|
||
auto provider = static_cast<BraveWalletWeb3ProviderTypes>( | ||
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider)); | ||
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS); | ||
} | ||
|
||
// If CryptoWallets is enabled and installed, but MetaMask is not | ||
// installed, set Crypto Wallets. | ||
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationCryptoWalletsInstalled) { | ||
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, true); | ||
AddCryptoWallets(); | ||
|
||
brave_wallet::MigrateBraveWalletPrefs(profile()); | ||
|
||
auto provider = static_cast<BraveWalletWeb3ProviderTypes>( | ||
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider)); | ||
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS); | ||
} | ||
|
||
// If CryptoWallets is enabled and not installed yet, and MetaMask is not | ||
// installed, set Ask | ||
TEST_F(BraveWalletUtilsUnitTest, TestPrefMigrationNothingInstalled) { | ||
profile()->GetPrefs()->SetBoolean(kBraveWalletEnabledDeprecated, true); | ||
|
||
brave_wallet::MigrateBraveWalletPrefs(profile()); | ||
|
||
auto provider = static_cast<BraveWalletWeb3ProviderTypes>( | ||
profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider)); | ||
ASSERT_EQ(provider, BraveWalletWeb3ProviderTypes::ASK); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.