Skip to content

Commit

Permalink
Merge pull request #11754 from brave/reset-wallet-2
Browse files Browse the repository at this point in the history
Refactor Reset to be under BraveWalletService
  • Loading branch information
bbondy authored Jan 6, 2022
2 parents bbec9d5 + 51e38c3 commit 2bb7659
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 85 deletions.
2 changes: 0 additions & 2 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ source_set("brave_wallet") {
"asset_ratio_service_factory.h",
"brave_wallet_context_utils.cc",
"brave_wallet_context_utils.h",
"brave_wallet_reset.cc",
"brave_wallet_reset.h",
"brave_wallet_service_factory.cc",
"brave_wallet_service_factory.h",
"erc_token_images_source.cc",
Expand Down
8 changes: 6 additions & 2 deletions browser/brave_wallet/android/wallet_native_utils_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

#include "base/android/jni_android.h"
#include "base/logging.h"
#include "brave/browser/brave_wallet/brave_wallet_reset.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/build/android/jni_headers/WalletNativeUtils_jni.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"

Expand All @@ -17,7 +18,10 @@ static void JNI_WalletNativeUtils_ResetWallet(JNIEnv* env) {
auto* profile = ProfileManager::GetActiveUserProfile();
DCHECK(profile);

brave_wallet::ResetWallet(profile);
auto* brave_wallet_service =
brave_wallet::BraveWalletServiceFactory::GetServiceForContext(profile);
if (brave_wallet_service)
brave_wallet_service->Reset();
}

} // namespace android
Expand Down
39 changes: 0 additions & 39 deletions browser/brave_wallet/brave_wallet_reset.cc

This file was deleted.

22 changes: 0 additions & 22 deletions browser/brave_wallet/brave_wallet_reset.h

This file was deleted.

4 changes: 4 additions & 0 deletions browser/brave_wallet/brave_wallet_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <utility>

#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/brave_wallet/eth_tx_service_factory.h"
#include "brave/browser/brave_wallet/json_rpc_service_factory.h"
#include "brave/browser/brave_wallet/keyring_service_factory.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service_delegate.h"
Expand Down Expand Up @@ -70,6 +72,8 @@ KeyedService* BraveWalletServiceFactory::BuildServiceInstanceFor(
return new BraveWalletService(
BraveWalletServiceDelegate::Create(context),
KeyringServiceFactory::GetServiceForContext(context),
JsonRpcServiceFactory::GetServiceForContext(context),
EthTxServiceFactory::GetServiceForContext(context),
user_prefs::UserPrefs::Get(context));
}

Expand Down
11 changes: 10 additions & 1 deletion browser/brave_wallet/brave_wallet_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "brave/browser/brave_wallet/eth_tx_service_factory.h"
#include "brave/browser/brave_wallet/json_rpc_service_factory.h"
#include "brave/browser/brave_wallet/keyring_service_factory.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service_delegate.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/erc_token_list_parser.h"
#include "brave/components/brave_wallet/browser/erc_token_registry.h"
#include "brave/components/brave_wallet/browser/eth_tx_service.h"
#include "brave/components/brave_wallet/browser/json_rpc_service.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/pref_names.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
Expand Down Expand Up @@ -141,9 +145,12 @@ class BraveWalletServiceUnitTest : public testing::Test {
histogram_tester_.reset(new base::HistogramTester);
keyring_service_ =
KeyringServiceFactory::GetServiceForContext(profile_.get());
json_rpc_service_ =
JsonRpcServiceFactory::GetServiceForContext(profile_.get());
eth_tx_service_ = EthTxServiceFactory::GetServiceForContext(profile_.get());
service_.reset(new BraveWalletService(
BraveWalletServiceDelegate::Create(profile_.get()), keyring_service_,
GetPrefs()));
json_rpc_service_, eth_tx_service_, GetPrefs()));
observer_.reset(new TestBraveWalletServiceObserver());
service_->AddObserver(observer_->GetReceiver());

Expand Down Expand Up @@ -491,6 +498,8 @@ class BraveWalletServiceUnitTest : public testing::Test {
std::unique_ptr<base::HistogramTester> histogram_tester_;
std::unique_ptr<BraveWalletService> service_;
KeyringService* keyring_service_;
JsonRpcService* json_rpc_service_;
EthTxService* eth_tx_service_;
std::unique_ptr<TestBraveWalletServiceObserver> observer_;
base::test::ScopedFeatureList scoped_feature_list_;

Expand Down
15 changes: 12 additions & 3 deletions browser/ui/webui/settings/brave_default_extensions_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
#include "base/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "brave/browser/brave_wallet/brave_wallet_reset.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/eth_tx_service_factory.h"
#include "brave/browser/extensions/brave_component_loader.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/eth_tx_service.h"
#include "brave/components/brave_webtorrent/grit/brave_webtorrent_resources.h"
#include "brave/components/decentralized_dns/buildflags/buildflags.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
Expand Down Expand Up @@ -210,12 +213,18 @@ void BraveDefaultExtensionsHandler::GetRestartNeeded(

void BraveDefaultExtensionsHandler::ResetWallet(
base::Value::ConstListView args) {
brave_wallet::ResetWallet(profile_);
auto* brave_wallet_service =
brave_wallet::BraveWalletServiceFactory::GetServiceForContext(profile_);
if (brave_wallet_service)
brave_wallet_service->Reset();
}

void BraveDefaultExtensionsHandler::ResetTransactionInfo(
base::Value::ConstListView args) {
brave_wallet::ResetTransactionInfo(profile_);
auto* eth_tx_service =
brave_wallet::EthTxServiceFactory::GetServiceForContext(profile_);
if (eth_tx_service)
eth_tx_service->Reset();
}

void BraveDefaultExtensionsHandler::SetWebTorrentEnabled(
Expand Down
15 changes: 15 additions & 0 deletions components/brave_wallet/browser/brave_wallet_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include "brave/components/brave_wallet/browser/brave_wallet_prefs.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/erc_token_registry.h"
#include "brave/components/brave_wallet/browser/eth_tx_service.h"
#include "brave/components/brave_wallet/browser/json_rpc_service.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/pref_names.h"
#include "brave/components/brave_wallet/common/eth_address.h"
Expand Down Expand Up @@ -110,9 +112,13 @@ namespace brave_wallet {
BraveWalletService::BraveWalletService(
std::unique_ptr<BraveWalletServiceDelegate> delegate,
KeyringService* keyring_service,
JsonRpcService* json_rpc_service,
EthTxService* eth_tx_service,
PrefService* prefs)
: delegate_(std::move(delegate)),
keyring_service_(keyring_service),
json_rpc_service_(json_rpc_service),
eth_tx_service_(eth_tx_service),
prefs_(prefs),
weak_ptr_factory_(this) {
if (delegate_)
Expand Down Expand Up @@ -780,9 +786,18 @@ void BraveWalletService::OnNetworkChanged() {
}

void BraveWalletService::Reset() {
if (eth_tx_service_)
eth_tx_service_->Reset();
if (json_rpc_service_)
json_rpc_service_->Reset();

// Clear BraveWalletService
ClearBraveWalletServicePrefs(prefs_);
CancelAllSuggestedTokenCallbacks();
CancelAllSignMessageCallbacks();

if (keyring_service_)
keyring_service_->Reset();
}

} // namespace brave_wallet
6 changes: 6 additions & 0 deletions components/brave_wallet/browser/brave_wallet_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ constexpr char kBraveWalletWeeklyHistogramName[] = "Brave.Wallet.UsageWeekly";
constexpr char kBraveWalletMonthlyHistogramName[] = "Brave.Wallet.UsageMonthly";

class KeyringService;
class JsonRpcService;
class EthTxService;

class BraveWalletService : public KeyedService,
public mojom::BraveWalletService,
Expand All @@ -46,6 +48,8 @@ class BraveWalletService : public KeyedService,
explicit BraveWalletService(
std::unique_ptr<BraveWalletServiceDelegate> delegate,
KeyringService* keyring_service,
JsonRpcService* json_rpc_service,
EthTxService* eth_tx_service,
PrefService* prefs);
~BraveWalletService() override;

Expand Down Expand Up @@ -181,6 +185,8 @@ class BraveWalletService : public KeyedService,
mojo::RemoteSet<mojom::BraveWalletServiceObserver> observers_;
std::unique_ptr<BraveWalletServiceDelegate> delegate_;
KeyringService* keyring_service_;
JsonRpcService* json_rpc_service_;
EthTxService* eth_tx_service_;
PrefService* prefs_;
mojo::ReceiverSet<mojom::BraveWalletService> receivers_;
PrefChangeRegistrar pref_change_registrar_;
Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/json_rpc_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ class JsonRpcService : public KeyedService, public mojom::JsonRpcService {

// Resets things back to the original state of BraveWalletService.
// To be used when the Wallet is reset / erased
void Reset() override;
void Reset();

using GetSupportsInterfaceCallback =
base::OnceCallback<void(bool is_supported,
Expand Down
10 changes: 6 additions & 4 deletions components/brave_wallet/browser/keyring_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ HDKeyring* KeyringService::RestoreKeyring(const std::string& keyring_id,
} else {
// We have no way to check if new mnemonic is same as current mnemonic so
// we need to clear all prefs for fresh start
Reset();
Reset(false);
}
}

Expand Down Expand Up @@ -1232,15 +1232,17 @@ void KeyringService::IsLocked(IsLockedCallback callback) {
std::move(callback).Run(IsLocked());
}

void KeyringService::Reset() {
void KeyringService::Reset(bool notify_observer) {
StopAutoLockTimer();
encryptor_.reset();
default_keyring_.reset();

ClearKeyringServiceProfilePrefs(prefs_);

for (const auto& observer : observers_) {
observer->KeyringReset();
if (notify_observer) {
for (const auto& observer : observers_) {
observer->KeyringReset();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion components/brave_wallet/browser/keyring_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ class KeyringService : public KeyedService, public mojom::KeyringService {
void GetDefaultKeyringInfo(GetDefaultKeyringInfoCallback callback) override;
void GetKeyringInfo(const std::string& keyring_id,
GetKeyringInfoCallback callback) override;
void Reset() override;
void SetDefaultKeyringHardwareAccountName(
const std::string& address,
const std::string& name,
Expand All @@ -159,6 +158,7 @@ class KeyringService : public KeyedService, public mojom::KeyringService {
const std::string& name,
SetDefaultKeyringImportedAccountNameCallback callback) override;

void Reset(bool notify_observer = true);
bool IsKeyringCreated(const std::string& keyring_id);
bool IsHardwareAccount(const std::string& account) const;
void SignTransactionByDefaultKeyring(const std::string& address,
Expand Down
6 changes: 0 additions & 6 deletions components/brave_wallet/common/brave_wallet.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,6 @@ interface KeyringService {
// Obtains the keyring information
GetKeyringInfo(string keyringId) => (KeyringInfo keyring);

// Resets the keyring and the related preferences
Reset();

// Adds a KeyringServiceObserver
AddObserver(pending_remote<KeyringServiceObserver> observer);

Expand Down Expand Up @@ -608,9 +605,6 @@ interface JsonRpcService {
// Balance is 0x1 if current selected account is the same as token owner's
// address, otherwise balance is 0x0.
GetERC721TokenBalance(string contract_address, string token_id, string account_address) => (string balance, ProviderError error, string error_message);

// Resets the keyring and the related preferences
Reset();
};

enum TransactionStatus {
Expand Down
11 changes: 8 additions & 3 deletions ios/browser/brave_wallet/brave_wallet_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service_delegate.h"
#include "brave/ios/browser/brave_wallet/eth_tx_service_factory.h"
#include "brave/ios/browser/brave_wallet/json_rpc_service_factory.h"
#include "brave/ios/browser/brave_wallet/keyring_service_factory.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/keyed_service/ios/browser_state_dependency_manager.h"
Expand Down Expand Up @@ -48,9 +50,12 @@ BraveWalletServiceFactory::BuildServiceInstanceFor(
auto* browser_state = ChromeBrowserState::FromBrowserState(context);
auto* keyring_service =
KeyringServiceFactory::GetServiceForState(browser_state);
std::unique_ptr<BraveWalletService> service(
new BraveWalletService(std::make_unique<BraveWalletServiceDelegate>(),
keyring_service, browser_state->GetPrefs()));
auto* json_rpc_service =
JsonRpcServiceFactory::GetServiceForState(browser_state);
auto* eth_tx_service = EthTxServiceFactory::GetServiceForState(browser_state);
std::unique_ptr<BraveWalletService> service(new BraveWalletService(
std::make_unique<BraveWalletServiceDelegate>(), keyring_service,
json_rpc_service, eth_tx_service, browser_state->GetPrefs()));
return service;
}

Expand Down
8 changes: 7 additions & 1 deletion ios/browser/brave_wallet/eth_tx_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

#include <utility>

#include "brave/components/brave_wallet/browser/eth_tx_service.h"
#include "brave/components/brave_wallet/browser/pref_names.h"
#include "brave/components/brave_wallet/factory/eth_tx_service_factory_helper.h"
#include "brave/ios/browser/brave_wallet/asset_ratio_service_factory.h"
Expand All @@ -28,6 +27,13 @@ EthTxServiceFactory::GetForBrowserState(ChromeBrowserState* browser_state) {
->MakeRemote();
}

// static
EthTxService* EthTxServiceFactory::GetServiceForState(
ChromeBrowserState* browser_state) {
return static_cast<EthTxService*>(
GetInstance()->GetServiceForBrowserState(browser_state, true));
}

// static
EthTxServiceFactory* EthTxServiceFactory::GetInstance() {
return base::Singleton<EthTxServiceFactory>::get();
Expand Down
2 changes: 2 additions & 0 deletions ios/browser/brave_wallet/eth_tx_service_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <memory>

#include "base/memory/singleton.h"
#include "brave/components/brave_wallet/browser/eth_tx_service.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
Expand All @@ -27,6 +28,7 @@ class EthTxServiceFactory : public BrowserStateKeyedServiceFactory {
// Creates the service if it doesn't exist already for |browser_state|.
static mojo::PendingRemote<mojom::EthTxService> GetForBrowserState(
ChromeBrowserState* browser_state);
static EthTxService* GetServiceForState(ChromeBrowserState* browser_state);

static EthTxServiceFactory* GetInstance();

Expand Down

0 comments on commit 2bb7659

Please sign in to comment.