Skip to content

Commit

Permalink
Add permission duration to connect from panel on desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdh committed Sep 5, 2023
1 parent cd6a396 commit dfbc4e9
Show file tree
Hide file tree
Showing 25 changed files with 122 additions and 92 deletions.
12 changes: 0 additions & 12 deletions browser/brave_wallet/brave_wallet_provider_delegate_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ void OnRequestPermissions(
}
}

absl::optional<permissions::RequestType> CoinTypeToPermissionRequestType(
mojom::CoinType coin_type) {
switch (coin_type) {
case mojom::CoinType::ETH:
return permissions::RequestType::kBraveEthereum;
case mojom::CoinType::SOL:
return permissions::RequestType::kBraveSolana;
default:
return absl::nullopt;
}
}

} // namespace

BraveWalletProviderDelegateImpl::BraveWalletProviderDelegateImpl(
Expand Down
12 changes: 0 additions & 12 deletions browser/brave_wallet/brave_wallet_service_delegate_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,6 @@ void BraveWalletServiceDelegateImpl::ContinueGetImportInfoFromExternalWallet(
}
}

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

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

bool BraveWalletServiceDelegateImpl::HasPermission(mojom::CoinType coin,
const url::Origin& origin,
const std::string& account) {
Expand Down
3 changes: 0 additions & 3 deletions browser/brave_wallet/brave_wallet_service_delegate_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ class BraveWalletServiceDelegateImpl : public BraveWalletServiceDelegate,
const std::string& password,
GetImportInfoCallback callback) override;

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;
Expand Down
10 changes: 3 additions & 7 deletions browser/brave_wallet/ethereum_provider_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,9 @@ class EthereumProviderImplUnitTest : public testing::Test {
}

void AddEthereumPermission(const mojom::AccountIdPtr& account_id) {
base::RunLoop run_loop;
brave_wallet_service_->AddPermission(
account_id.Clone(), base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_TRUE(permissions::BraveWalletPermissionContext::AddPermission(
blink::PermissionType::BRAVE_ETHEREUM, browser_context(), GetOrigin(),
account_id->address));
}

void ResetEthereumPermission(const mojom::AccountIdPtr& account_id) {
Expand Down
17 changes: 4 additions & 13 deletions browser/brave_wallet/send_or_sign_transaction_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_tab_helper.h"
#include "brave/browser/brave_wallet/json_rpc_service_factory.h"
#include "brave/browser/brave_wallet/keyring_service_factory.h"
#include "brave/browser/brave_wallet/tx_service_factory.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/json_rpc_service.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
Expand Down Expand Up @@ -190,9 +188,6 @@ class SendOrSignTransactionBrowserTest : public InProcessBrowserTest {
https_server_for_files()->ServeFilesFromDirectory(test_data_dir);
ASSERT_TRUE(https_server_for_files()->Start());

brave_wallet_service_ =
brave_wallet::BraveWalletServiceFactory::GetServiceForContext(
browser()->profile());
keyring_service_ =
KeyringServiceFactory::GetServiceForContext(browser()->profile());
tx_service_ = TxServiceFactory::GetServiceForContext(browser()->profile());
Expand Down Expand Up @@ -340,13 +335,10 @@ class SendOrSignTransactionBrowserTest : public InProcessBrowserTest {
}

void AddEthereumPermission(const mojom::AccountIdPtr& account_id) {
base::RunLoop run_loop;
brave_wallet_service_->AddPermission(
account_id.Clone(), base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_TRUE(permissions::BraveWalletPermissionContext::AddPermission(
blink::PermissionType::BRAVE_ETHEREUM, browser()->profile(),
web_contents()->GetPrimaryMainFrame()->GetLastCommittedOrigin(),
account_id->address));
}

const mojom::AccountInfoPtr& default_account() const {
Expand Down Expand Up @@ -604,7 +596,6 @@ class SendOrSignTransactionBrowserTest : public InProcessBrowserTest {
}

protected:
raw_ptr<BraveWalletService> brave_wallet_service_ = nullptr;
mojom::AccountInfoPtr default_account_;

private:
Expand Down
11 changes: 4 additions & 7 deletions browser/brave_wallet/solana_provider_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/brave_wallet/common/solana_utils.h"
#include "brave/components/permissions/brave_permission_manager.h"
#include "brave/components/permissions/contexts/brave_wallet_permission_context.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/permissions/permission_manager_factory.h"
#include "chrome/test/base/scoped_testing_local_state.h"
Expand Down Expand Up @@ -266,13 +267,9 @@ class SolanaProviderImplUnitTest : public testing::Test {
}

void AddSolanaPermission(const mojom::AccountIdPtr& account_id) {
base::RunLoop run_loop;
brave_wallet_service_->AddPermission(
account_id.Clone(), base::BindLambdaForTesting([&](bool success) {
EXPECT_TRUE(success);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_TRUE(permissions::BraveWalletPermissionContext::AddPermission(
blink::PermissionType::BRAVE_SOLANA, browser_context(), GetOrigin(),
account_id->address));
}

std::string Connect(absl::optional<base::Value::Dict> arg,
Expand Down
1 change: 1 addition & 0 deletions browser/ui/webui/brave_wallet/panel_handler/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ source_set("panel_handler") {
"//brave/browser/brave_wallet",
"//brave/browser/brave_wallet:tab_helper",
"//brave/components/brave_wallet/browser",
"//brave/components/brave_wallet/browser:permission_utils",
"//brave/components/brave_wallet/common:mojom",
"//chrome/browser/profiles:profile",
"//components/permissions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "base/functional/callback.h"
#include "brave/browser/brave_wallet/brave_wallet_tab_helper.h"
#include "brave/components/brave_wallet/browser/permission_utils.h"
#include "brave/components/permissions/contexts/brave_wallet_permission_context.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
Expand Down Expand Up @@ -83,3 +84,40 @@ void WalletPanelHandler::IsSolanaAccountConnected(
std::move(callback).Run(
tab_helper->IsSolanaAccountConnected(rfh->GetGlobalId(), account));
}

void WalletPanelHandler::RequestPermission(
brave_wallet::mojom::AccountIdPtr account_id,
RequestPermissionCallback callback) {
content::RenderFrameHost* rfh = nullptr;
if (!(rfh = active_web_contents_->GetFocusedFrame())) {
std::move(callback).Run(false);
return;
}

auto request_type =
brave_wallet::CoinTypeToPermissionRequestType(account_id->coin);
auto permission = brave_wallet::CoinTypeToPermissionType(account_id->coin);
if (!request_type || !permission) {
std::move(callback).Run(false);
return;
}

if (permissions::BraveWalletPermissionContext::HasRequestsInProgress(
rfh, *request_type)) {
std::move(callback).Run(false);
return;
}

permissions::BraveWalletPermissionContext::RequestPermissions(
*permission, rfh, {account_id->address},
base::BindOnce(
[](RequestPermissionCallback cb,
const std::vector<blink::mojom::PermissionStatus>& responses) {
if (responses.empty() || responses.size() != 1u) {
std::move(cb).Run(false);
} else {
std::move(cb).Run(true);
}
},
std::move(callback)));
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class WalletPanelHandler : public brave_wallet::mojom::PanelHandler {
void IsSolanaAccountConnected(
const std::string& account,
IsSolanaAccountConnectedCallback callback) override;
void RequestPermission(brave_wallet::mojom::AccountIdPtr account_id,
RequestPermissionCallback callback) override;

private:
mojo::Receiver<brave_wallet::mojom::PanelHandler> receiver_;
Expand Down
12 changes: 0 additions & 12 deletions components/brave_wallet/browser/brave_wallet_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -926,18 +926,6 @@ void BraveWalletService::OnBraveWalletNftDiscoveryEnabled() {
}
}

void BraveWalletService::AddPermission(mojom::AccountIdPtr account_id,
AddPermissionCallback callback) {
auto origin = delegate_->GetActiveOrigin();
if (!origin) {
std::move(callback).Run(false);
return;
}

std::move(callback).Run(
delegate_->AddPermission(account_id->coin, *origin, account_id->address));
}

void BraveWalletService::HasPermission(
std::vector<mojom::AccountIdPtr> accounts,
HasPermissionCallback callback) {
Expand Down
2 changes: 0 additions & 2 deletions components/brave_wallet/browser/brave_wallet_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,6 @@ class BraveWalletService : public KeyedService,
void SetNetworkForSelectedAccountOnActiveOrigin(
const std::string& chain_id,
SetNetworkForSelectedAccountOnActiveOriginCallback callback) override;
void AddPermission(mojom::AccountIdPtr account_id,
AddPermissionCallback callback) override;
void HasPermission(std::vector<mojom::AccountIdPtr> accounts,
HasPermissionCallback callback) override;
void ResetPermission(mojom::AccountIdPtr account_id,
Expand Down
12 changes: 12 additions & 0 deletions components/brave_wallet/browser/permission_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,16 @@ absl::optional<blink::PermissionType> CoinTypeToPermissionType(
}
}

absl::optional<permissions::RequestType> CoinTypeToPermissionRequestType(
mojom::CoinType coin_type) {
switch (coin_type) {
case mojom::CoinType::ETH:
return permissions::RequestType::kBraveEthereum;
case mojom::CoinType::SOL:
return permissions::RequestType::kBraveSolana;
default:
return absl::nullopt;
}
}

} // namespace brave_wallet
3 changes: 3 additions & 0 deletions components/brave_wallet/browser/permission_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ GURL GetConnectWithSiteWebUIURL(const GURL& webui_base_url,
absl::optional<blink::PermissionType> CoinTypeToPermissionType(
mojom::CoinType coin_type);

absl::optional<permissions::RequestType> CoinTypeToPermissionRequestType(
mojom::CoinType coin_type);

} // namespace brave_wallet

#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_PERMISSION_UTILS_H_
22 changes: 22 additions & 0 deletions components/brave_wallet/browser/permission_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,26 @@ TEST(PermissionUtilsUnitTest, SyncingWithCreatePermissionLifetimeOptions) {
absl::nullopt);
}

TEST(PermissionUtilsUnitTest, CoinTypeToPermissionType) {
auto type = CoinTypeToPermissionType(mojom::CoinType::ETH);
ASSERT_TRUE(type);
EXPECT_EQ(*type, blink::PermissionType::BRAVE_ETHEREUM);
type = CoinTypeToPermissionType(mojom::CoinType::SOL);
ASSERT_TRUE(type);
EXPECT_EQ(*type, blink::PermissionType::BRAVE_SOLANA);
EXPECT_FALSE(CoinTypeToPermissionType(mojom::CoinType::FIL));
EXPECT_FALSE(CoinTypeToPermissionType(mojom::CoinType::BTC));
}

TEST(PermissionUtilsUnitTest, CoinTypeToPermissionRequestType) {
auto request = CoinTypeToPermissionRequestType(mojom::CoinType::ETH);
ASSERT_TRUE(request);
EXPECT_EQ(*request, permissions::RequestType::kBraveEthereum);
request = CoinTypeToPermissionRequestType(mojom::CoinType::SOL);
ASSERT_TRUE(request);
EXPECT_EQ(*request, permissions::RequestType::kBraveSolana);
EXPECT_FALSE(CoinTypeToPermissionType(mojom::CoinType::FIL));
EXPECT_FALSE(CoinTypeToPermissionType(mojom::CoinType::BTC));
}

} // namespace brave_wallet
11 changes: 7 additions & 4 deletions components/brave_wallet/common/brave_wallet.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ interface PanelHandler {
Focus();

IsSolanaAccountConnected(string account) => (bool connected);

// Create a permission request for users based on active web contents.
// The flow will be similar to dapp request permission except it is initiated
// from panel. Desktop front end will be opened with same #connectWitSite ref
// and we need to call ConnectToSite or CancelConnectToSite based on user
// decision.
RequestPermission(AccountId account_id) => (bool success);
};

// Browser-side handler for requests from WebUI page.
Expand Down Expand Up @@ -1758,10 +1765,6 @@ interface BraveWalletService {
GetNetworkForSelectedAccountOnActiveOrigin() => (NetworkInfo? network);
SetNetworkForSelectedAccountOnActiveOrigin(string chain_id) => (bool success);

// Adds the permission for the account on active origin.
AddPermission(AccountId account_id)
=> (bool success);

// Filters accounts with permissions on active origin.
HasPermission(array<AccountId> accounts)
=> (bool success, array<AccountId> accounts_with_permission);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export const {
activeOriginChanged,
addAccount,
addFavoriteApp,
addSitePermission,
addUserAsset,
addUserAssetError,
autoLockMinutesChanged,
Expand Down
7 changes: 0 additions & 7 deletions components/brave_wallet_ui/common/async/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { mapLimit } from 'async'
import AsyncActionHandler from '../../../common/AsyncActionHandler'
import * as WalletActions from '../actions/wallet_actions'
import {
AddSitePermissionPayloadType,
RemoveSitePermissionPayloadType,
SetUserAssetVisiblePayloadType,
UnlockWalletPayloadType,
Expand Down Expand Up @@ -320,12 +319,6 @@ handler.on(WalletActions.removeSitePermission.type, async (store: Store, payload
await refreshWalletInfo(store)
})

handler.on(WalletActions.addSitePermission.type, async (store: Store, payload: AddSitePermissionPayloadType) => {
const braveWalletService = getAPIProxy().braveWalletService
await braveWalletService.addPermission(payload.accountId)
await refreshWalletInfo(store)
})

handler.on(WalletActions.expandWalletNetworks.type, async (store) => {
chrome.tabs.create({ url: 'chrome://settings/wallet/networks' }, () => {
if (chrome.runtime.lastError) {
Expand Down
4 changes: 0 additions & 4 deletions components/brave_wallet_ui/common/constants/action_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ export type RemoveSitePermissionPayloadType = {
accountId: BraveWallet.AccountId
}

export type AddSitePermissionPayloadType = {
accountId: BraveWallet.AccountId
}

export type GetCoinMarketPayload = {
vsAsset: string
limit: number
Expand Down
3 changes: 0 additions & 3 deletions components/brave_wallet_ui/common/slices/wallet.slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
ImportAccountErrorType
} from '../../constants/types'
import {
AddSitePermissionPayloadType,
DefaultBaseCryptocurrencyChanged,
DefaultBaseCurrencyChanged,
DefaultEthereumWalletChanged,
Expand Down Expand Up @@ -197,8 +196,6 @@ export const WalletAsyncActions = {
removeSitePermission: createAction<RemoveSitePermissionPayloadType>(
'removeSitePermission'
),
addSitePermission:
createAction<AddSitePermissionPayloadType>('addSitePermission'),
refreshNetworksAndTokens:
createAction<RefreshOpts>('refreshNetworksAndTokens'),
expandWalletNetworks: createAction('expandWalletNetworks'), // replace with chrome.tabs.create helper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as React from 'react'
import { useDispatch } from 'react-redux'

// Actions
import { PanelActions } from '../../../panel/actions'
import { WalletActions } from '../../../common/actions'

// Types
Expand Down Expand Up @@ -83,7 +84,8 @@ export const ConnectedAccountItem = (props: Props) => {

// methods
const onClickConnect = React.useCallback(() => {
dispatch(WalletActions.addSitePermission({ accountId: account.accountId }))
dispatch(PanelActions
.requestSitePermission({ accountId: account.accountId }))
if (selectedCoin !== BraveWallet.CoinType.SOL) {
setSelectedAccount(account.accountId)
}
Expand Down
Loading

0 comments on commit dfbc4e9

Please sign in to comment.