Skip to content

Commit

Permalink
Merge pull request #20840 from brave/reset_wallet_permissions
Browse files Browse the repository at this point in the history
Reset wallet permissions when wallet is being reset
  • Loading branch information
yrliou authored Nov 6, 2023
2 parents 99fc33e + 4d6e92d commit 6aaecf9
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 103 deletions.
2 changes: 2 additions & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ source_set("brave_wallet_delegate") {
sources = [
"brave_wallet_provider_delegate_impl.cc",
"brave_wallet_provider_delegate_impl.h",
"brave_wallet_service_delegate_base.cc",
"brave_wallet_service_delegate_base.h",
"brave_wallet_service_delegate_helper.cc",
]
deps = [
Expand Down
63 changes: 63 additions & 0 deletions browser/brave_wallet/brave_wallet_service_delegate_base.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */

#include "brave/browser/brave_wallet/brave_wallet_service_delegate_base.h"

#include "brave/components/brave_wallet/browser/permission_utils.h"
#include "brave/components/permissions/contexts/brave_wallet_permission_context.h"
#include "content/public/browser/browser_context.h"

namespace brave_wallet {

BraveWalletServiceDelegateBase::BraveWalletServiceDelegateBase(
content::BrowserContext* context)
: context_(context) {}

BraveWalletServiceDelegateBase::~BraveWalletServiceDelegateBase() = default;

bool BraveWalletServiceDelegateBase::HasPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) {
bool has_permission = false;
auto type = CoinTypeToPermissionType(coin);
if (!type) {
return false;
}

bool success = permissions::BraveWalletPermissionContext::HasPermission(
*type, context_, origin, account, &has_permission);
return success && has_permission;
}

bool BraveWalletServiceDelegateBase::ResetPermission(
mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) {
auto type = CoinTypeToPermissionType(coin);
if (!type) {
return false;
}

return permissions::BraveWalletPermissionContext::ResetPermission(
*type, context_, origin, account);
}

bool BraveWalletServiceDelegateBase::IsPermissionDenied(
mojom::CoinType coin,
const url::Origin& origin) {
auto type = CoinTypeToPermissionType(coin);
if (!type) {
return false;
}

return permissions::BraveWalletPermissionContext::IsPermissionDenied(
*type, context_, origin);
}

void BraveWalletServiceDelegateBase::ResetAllPermissions() {
permissions::BraveWalletPermissionContext::ResetAllPermissions(context_);
}

} // namespace brave_wallet
51 changes: 51 additions & 0 deletions browser/brave_wallet/brave_wallet_service_delegate_base.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Copyright (c) 2023 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 https://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_DELEGATE_BASE_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_DELEGATE_BASE_H_

#include <string>

#include "base/memory/raw_ptr.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service_delegate.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"

namespace content {
class BrowserContext;
}

namespace url {
class Origin;
}

namespace brave_wallet {

// Shared BraveWalletServiceDelegate implementation between Desktop and Android.
class BraveWalletServiceDelegateBase : public BraveWalletServiceDelegate {
public:
explicit BraveWalletServiceDelegateBase(content::BrowserContext* context);
BraveWalletServiceDelegateBase(const BraveWalletServiceDelegateBase&) =
delete;
BraveWalletServiceDelegateBase& operator=(
const BraveWalletServiceDelegateBase&) = delete;
~BraveWalletServiceDelegateBase() override;

bool HasPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) override;
bool ResetPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) override;
bool IsPermissionDenied(mojom::CoinType coin,
const url::Origin& origin) override;
void ResetAllPermissions() override;

protected:
raw_ptr<content::BrowserContext> context_ = nullptr;
};

} // namespace brave_wallet

#endif // BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_DELEGATE_BASE_H_
41 changes: 1 addition & 40 deletions browser/brave_wallet/brave_wallet_service_delegate_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void ClearWalletStoragePartition(content::BrowserContext* context,

BraveWalletServiceDelegateImpl::BraveWalletServiceDelegateImpl(
content::BrowserContext* context)
: context_(context),
: BraveWalletServiceDelegateBase(context),
browser_tab_strip_tracker_(this, this),
weak_ptr_factory_(this) {
browser_tab_strip_tracker_.Init();
Expand Down Expand Up @@ -149,45 +149,6 @@ void BraveWalletServiceDelegateImpl::ContinueGetImportInfoFromExternalWallet(
}
}

bool BraveWalletServiceDelegateImpl::HasPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) {
bool has_permission = false;
auto type = CoinTypeToPermissionType(coin);
if (!type) {
return false;
}

bool success = permissions::BraveWalletPermissionContext::HasPermission(
*type, context_, origin, account, &has_permission);
return success && has_permission;
}

bool BraveWalletServiceDelegateImpl::ResetPermission(
mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) {
auto type = CoinTypeToPermissionType(coin);
if (!type) {
return false;
}

return permissions::BraveWalletPermissionContext::ResetPermission(
*type, context_, origin, account);
}

bool BraveWalletServiceDelegateImpl::IsPermissionDenied(
mojom::CoinType coin,
const url::Origin& origin) {
auto type = CoinTypeToPermissionType(coin);
if (!type) {
return false;
}

return permissions::BraveWalletPermissionContext::IsPermissionDenied(
*type, context_, origin);
}

void BraveWalletServiceDelegateImpl::OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
Expand Down
13 changes: 2 additions & 11 deletions browser/brave_wallet/brave_wallet_service_delegate_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "brave/browser/brave_wallet/brave_wallet_service_delegate_base.h"
#include "brave/browser/brave_wallet/external_wallets_importer.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service_delegate.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
Expand All @@ -27,7 +28,7 @@ class WebContents;

namespace brave_wallet {

class BraveWalletServiceDelegateImpl : public BraveWalletServiceDelegate,
class BraveWalletServiceDelegateImpl : public BraveWalletServiceDelegateBase,
public TabStripModelObserver,
public BrowserTabStripTrackerDelegate {
public:
Expand All @@ -50,15 +51,6 @@ class BraveWalletServiceDelegateImpl : public BraveWalletServiceDelegate,
const std::string& password,
GetImportInfoCallback callback) override;

bool HasPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) override;
bool ResetPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) override;
bool IsPermissionDenied(mojom::CoinType coin,
const url::Origin& origin) override;

absl::optional<url::Origin> GetActiveOrigin() override;

void ClearWalletUIStoragePartition() override;
Expand Down Expand Up @@ -92,7 +84,6 @@ class BraveWalletServiceDelegateImpl : public BraveWalletServiceDelegate,
absl::optional<url::Origin> GetActiveOriginInternal();
void FireActiveOriginChanged();

raw_ptr<content::BrowserContext> context_ = nullptr;
base::flat_map<mojom::ExternalWalletType,
std::unique_ptr<ExternalWalletsImporter>>
importers_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ content::WebContents* GetActiveWebContents(content::BrowserContext* context) {

BraveWalletServiceDelegateImpl::BraveWalletServiceDelegateImpl(
content::BrowserContext* context)
: context_(context), weak_ptr_factory_(this) {}
: BraveWalletServiceDelegateBase(context), weak_ptr_factory_(this) {}

BraveWalletServiceDelegateImpl::~BraveWalletServiceDelegateImpl() = default;

Expand All @@ -57,45 +57,6 @@ bool BraveWalletServiceDelegateImpl::AddPermission(mojom::CoinType coin,
*type, context_, origin, account);
}

bool BraveWalletServiceDelegateImpl::HasPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) {
bool has_permission = false;
auto type = CoinTypeToPermissionType(coin);
if (!type) {
return false;
}

bool success = permissions::BraveWalletPermissionContext::HasPermission(
*type, context_, origin, account, &has_permission);
return success && has_permission;
}

bool BraveWalletServiceDelegateImpl::ResetPermission(
mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) {
auto type = CoinTypeToPermissionType(coin);
if (!type) {
return false;
}

return permissions::BraveWalletPermissionContext::ResetPermission(
*type, context_, origin, account);
}

bool BraveWalletServiceDelegateImpl::IsPermissionDenied(
mojom::CoinType coin,
const url::Origin& origin) {
auto type = CoinTypeToPermissionType(coin);
if (!type) {
return false;
}

return permissions::BraveWalletPermissionContext::IsPermissionDenied(
*type, context_, origin);
}

void BraveWalletServiceDelegateImpl::GetWebSitesWithPermission(
mojom::CoinType coin,
GetWebSitesWithPermissionCallback callback) {
Expand Down
13 changes: 2 additions & 11 deletions browser/brave_wallet/brave_wallet_service_delegate_impl_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"

#include "brave/components/brave_wallet/browser/brave_wallet_service_delegate.h"
#include "brave/browser/brave_wallet/brave_wallet_service_delegate_base.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"

namespace content {
Expand All @@ -24,7 +24,7 @@ namespace brave_wallet {

class ExternalWalletsImporter;

class BraveWalletServiceDelegateImpl : public BraveWalletServiceDelegate {
class BraveWalletServiceDelegateImpl : public BraveWalletServiceDelegateBase {
public:
explicit BraveWalletServiceDelegateImpl(content::BrowserContext* context);
BraveWalletServiceDelegateImpl(const BraveWalletServiceDelegateImpl&) =
Expand All @@ -36,14 +36,6 @@ class BraveWalletServiceDelegateImpl : public BraveWalletServiceDelegate {
bool AddPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) override;
bool HasPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) override;
bool ResetPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) override;
bool IsPermissionDenied(mojom::CoinType coin,
const url::Origin& origin) override;
void GetWebSitesWithPermission(
mojom::CoinType coin,
GetWebSitesWithPermissionCallback callback) override;
Expand All @@ -53,7 +45,6 @@ class BraveWalletServiceDelegateImpl : public BraveWalletServiceDelegate {
absl::optional<url::Origin> GetActiveOrigin() override;

private:
raw_ptr<content::BrowserContext> context_ = nullptr;
base::ObserverList<BraveWalletServiceDelegate::Observer> observer_list_;

base::WeakPtrFactory<BraveWalletServiceDelegateImpl> weak_ptr_factory_;
Expand Down
34 changes: 33 additions & 1 deletion browser/brave_wallet/brave_wallet_service_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/brave_wallet/common/test_utils.h"
#include "brave/components/constants/webui_url_constants.h"
#include "brave/components/permissions/brave_permission_manager.h"
#include "brave/components/permissions/contexts/brave_wallet_permission_context.h"
#include "build/build_config.h"
#include "chrome/browser/permissions/permission_manager_factory.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
Expand Down Expand Up @@ -164,6 +167,8 @@ const char interface_not_supported_response[] = R"({
"result":"0x0000000000000000000000000000000000000000000000000000000000000000"
})";

constexpr char kBraveUrl[] = "https://brave.com";

class MockDataRemovalObserver : public StoragePartition::DataRemovalObserver {
public:
explicit MockDataRemovalObserver(StoragePartition* partition) {
Expand Down Expand Up @@ -337,6 +342,12 @@ class BraveWalletServiceUnitTest : public testing::Test {
observer_ = std::make_unique<TestBraveWalletServiceObserver>();
service_->AddObserver(observer_->GetReceiver());

profile_->SetPermissionControllerDelegate(
base::WrapUnique(static_cast<permissions::BravePermissionManager*>(
PermissionManagerFactory::GetInstance()
->BuildServiceInstanceForBrowserContext(profile_.get())
.release())));

auto* registry = BlockchainRegistry::GetInstance();
TokenListMap token_list_map;
ASSERT_TRUE(
Expand Down Expand Up @@ -417,6 +428,10 @@ class BraveWalletServiceUnitTest : public testing::Test {
true, "", "", mojom::kFilecoinMainnet, mojom::CoinType::FIL);
}

void TearDown() override {
profile_->SetPermissionControllerDelegate(nullptr);
}

mojom::BlockchainTokenPtr GetToken1() { return token1_.Clone(); }
mojom::BlockchainTokenPtr GetToken2() { return token2_.Clone(); }
mojom::BlockchainTokenPtr GetErc721Token() { return erc721_token_.Clone(); }
Expand Down Expand Up @@ -714,7 +729,7 @@ class BraveWalletServiceUnitTest : public testing::Test {
bool run_switch_network = false) {
mojom::AddSuggestTokenRequestPtr request =
mojom::AddSuggestTokenRequest::New(
MakeOriginInfo(url::Origin::Create(GURL("https://brave.com"))),
MakeOriginInfo(url::Origin::Create(GURL(kBraveUrl))),
suggested_token.Clone());
base::RunLoop run_loop;
service_->AddSuggestTokenRequest(
Expand Down Expand Up @@ -2745,8 +2760,25 @@ TEST_F(BraveWalletServiceUnitTest, Reset) {
base::Time(), base::Time::Max()));
#endif

const std::string eth_addr = "0x407637cC04893DA7FA4A7C0B58884F82d69eD448";
const std::string sol_addr = "BrG44HdsEhzapvs8bEqzvkq4egwevS3fRE6ze2ENo6S8";
auto origin = url::Origin::Create(GURL(kBraveUrl));
auto* delegate = service_->GetDelegateForTesting();
ASSERT_TRUE(delegate);

ASSERT_TRUE(permissions::BraveWalletPermissionContext::AddPermission(
blink::PermissionType::BRAVE_ETHEREUM, profile_.get(), origin, eth_addr));
ASSERT_TRUE(permissions::BraveWalletPermissionContext::AddPermission(
blink::PermissionType::BRAVE_SOLANA, profile_.get(), origin, sol_addr));

ASSERT_TRUE(delegate->HasPermission(mojom::CoinType::ETH, origin, eth_addr));
ASSERT_TRUE(delegate->HasPermission(mojom::CoinType::SOL, origin, sol_addr));

service_->Reset();

EXPECT_FALSE(delegate->HasPermission(mojom::CoinType::ETH, origin, eth_addr));
EXPECT_FALSE(delegate->HasPermission(mojom::CoinType::SOL, origin, sol_addr));

EXPECT_FALSE(GetPrefs()->HasPrefPath(kBraveWalletUserAssets));
EXPECT_FALSE(GetPrefs()->HasPrefPath(kDefaultBaseCurrency));
EXPECT_FALSE(GetPrefs()->HasPrefPath(kDefaultBaseCryptocurrency));
Expand Down
Loading

0 comments on commit 6aaecf9

Please sign in to comment.