From bd7dd8d2bfa8505c55a59206b3c554b0ce7e9780 Mon Sep 17 00:00:00 2001 From: zenparsing Date: Wed, 3 Nov 2021 16:47:49 -0400 Subject: [PATCH] Disconnect existing external wallets when connecting a new wallet --- .../browser/rewards_service_impl.cc | 22 +++++++++++++++---- .../browser/rewards_service_impl.h | 6 +++++ .../services/bat_ledger/bat_ledger_impl.cc | 13 +++++++++++ .../services/bat_ledger/bat_ledger_impl.h | 3 +++ .../public/interfaces/bat_ledger.mojom | 2 ++ .../include/bat/ledger/ledger.h | 2 ++ .../src/bat/ledger/internal/ledger_impl.cc | 4 ++++ .../src/bat/ledger/internal/ledger_impl.h | 2 ++ 8 files changed, 50 insertions(+), 4 deletions(-) diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 2e32b3517d31..4c60fb1ce552 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -2880,10 +2880,11 @@ void RewardsServiceImpl::OnExternalWalletAuthorization( std::move(callback).Run(result, args); } -void RewardsServiceImpl::ExternalWalletAuthorization( - const std::string& wallet_type, - const base::flat_map& args, - ExternalWalletAuthorizationCallback callback) { +void RewardsServiceImpl::OnDisconnectWalletsForAuthorization( + const std::string& wallet_type, + const base::flat_map& args, + ExternalWalletAuthorizationCallback callback, + ledger::type::Result result) { if (!Connected()) { return; } @@ -2897,6 +2898,19 @@ void RewardsServiceImpl::ExternalWalletAuthorization( std::move(callback))); } +void RewardsServiceImpl::ExternalWalletAuthorization( + const std::string& wallet_type, + const base::flat_map& args, + ExternalWalletAuthorizationCallback callback) { + if (!Connected()) { + return; + } + + bat_ledger_->DisconnectExternalWallets(base::BindOnce( + &RewardsServiceImpl::OnDisconnectWalletsForAuthorization, AsWeakPtr(), + wallet_type, std::move(args), std::move(callback))); +} + void RewardsServiceImpl::OnProcessExternalWalletAuthorization( const std::string& wallet_type, const std::string& action, diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index d3b3749761c7..68be3097c891 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -498,6 +498,12 @@ class RewardsServiceImpl : public RewardsService, const ledger::type::Result result, const base::flat_map& args); + void OnDisconnectWalletsForAuthorization( + const std::string& wallet_type, + const base::flat_map& args, + ExternalWalletAuthorizationCallback callback, + ledger::type::Result result); + void OnDisconnectWallet( const std::string& wallet_type, const ledger::type::Result result); diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index 13bb15ecadf8..cbf251786954 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -887,6 +887,19 @@ void BatLedgerImpl::DisconnectWallet( _1)); } +void BatLedgerImpl::DisconnectExternalWallets( + DisconnectExternalWalletsCallback callback) { + auto weak_self = AsWeakPtr(); + auto shared_callback = + std::make_shared(std::move(callback)); + + ledger_->DisconnectExternalWallets( + [weak_self, shared_callback](ledger::type::Result result) { + if (weak_self) + std::move(*shared_callback).Run(result); + }); +} + // static void BatLedgerImpl::OnGetAnonWalletStatus( CallbackHolder* holder, diff --git a/components/services/bat_ledger/bat_ledger_impl.h b/components/services/bat_ledger/bat_ledger_impl.h index a13086dc3db8..2b5c622cc7bf 100644 --- a/components/services/bat_ledger/bat_ledger_impl.h +++ b/components/services/bat_ledger/bat_ledger_impl.h @@ -212,6 +212,9 @@ class BatLedgerImpl : const std::string& wallet_type, DisconnectWalletCallback callback) override; + void DisconnectExternalWallets( + DisconnectExternalWalletsCallback callback) override; + void GetAnonWalletStatus(GetAnonWalletStatusCallback callback) override; void GetTransactionReport( diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index cc2da17b3977..767341089a3d 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -137,6 +137,8 @@ interface BatLedger { DisconnectWallet(string wallet_type) => (ledger.mojom.Result result); + DisconnectExternalWallets() => (ledger.mojom.Result result); + GetAnonWalletStatus() => (ledger.mojom.Result result); GetTransactionReport(ledger.mojom.ActivityMonth month, int32 year) => (array list); diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index 272756f871b0..5dfb5589ef60 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -330,6 +330,8 @@ class LEDGER_EXPORT Ledger { virtual void DisconnectWallet(const std::string& wallet_type, ResultCallback callback) = 0; + virtual void DisconnectExternalWallets(ResultCallback callback) = 0; + virtual void GetAllPromotions(GetAllPromotionsCallback callback) = 0; virtual void GetAnonWalletStatus(ResultCallback callback) = 0; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index 92162f29d3c1..fa5e280c6ab3 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -805,6 +805,10 @@ void LedgerImpl::DisconnectWallet(const std::string& wallet_type, }); } +void LedgerImpl::DisconnectExternalWallets(ResultCallback callback) { + WhenReady([this, callback]() { wallet()->DisconnectAllWallets(callback); }); +} + void LedgerImpl::GetAllPromotions(GetAllPromotionsCallback callback) { WhenReady([this, callback]() { database()->GetAllPromotions(callback); }); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h index e771c1224320..6fc121d404f0 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h @@ -265,6 +265,8 @@ class LedgerImpl : public Ledger { void DisconnectWallet(const std::string& wallet_type, ResultCallback callback) override; + void DisconnectExternalWallets(ResultCallback callback) override; + void GetAllPromotions(GetAllPromotionsCallback callback) override; void GetAnonWalletStatus(ResultCallback callback) override;