Skip to content

Commit

Permalink
Merge pull request #20008 from brave/permission-duration-panel-connect
Browse files Browse the repository at this point in the history
Add permission duration to connect from panel on desktop
  • Loading branch information
darkdh authored Oct 6, 2023
2 parents b3e8e9e + 09165d5 commit 5128052
Show file tree
Hide file tree
Showing 35 changed files with 355 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import org.chromium.base.Callback;
import org.chromium.base.Log;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.brave_wallet.mojom.AccountInfo;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.PermissionLifetimeOption;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.BraveRewardsHelper;
import org.chromium.chrome.browser.ChromeTabbedActivity;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.app.domain.WalletModel;
Expand All @@ -32,21 +37,26 @@
import org.chromium.chrome.browser.crypto_wallet.util.AccountsPermissionsHelper;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.chrome.browser.crypto_wallet.util.WalletUtils;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper.DefaultFaviconHelper;
import org.chromium.chrome.browser.ui.favicon.FaviconHelper.FaviconImageCallback;
import org.chromium.content_public.browser.WebContents;
import org.chromium.url.GURL;

import java.util.HashSet;
import java.util.Iterator;

/**
* Fragment used to connect Dapps to the crypto account
*/
public class ConnectAccountFragment extends BaseDAppsFragment
implements BravePermissionAccountsListAdapter.BravePermissionDelegate {
private static final String TAG = "ConnectAccount";

private TextView mWebSite;
private TextView mAccountsConnected;
private TextView mbtNewAccount;
private TextView mButtonNewAccount;
private ImageView mFavicon;
private AccountInfo[] mAccountInfos;
private HashSet<AccountInfo> mAccountsWithPermissions;
Expand Down Expand Up @@ -99,8 +109,8 @@ public View onCreateView(
View view = inflater.inflate(R.layout.fragment_connect_account, container, false);
mWebSite = view.findViewById(R.id.fragment_connect_account_website);
mAccountsConnected = view.findViewById(R.id.fragment_connect_account_accounts_connected);
mbtNewAccount = view.findViewById(R.id.fragment_connect_account_new_account_id);
mbtNewAccount.setOnClickListener(new View.OnClickListener() {
mButtonNewAccount = view.findViewById(R.id.fragment_connect_account_new_account_id);
mButtonNewAccount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BottomSheetDialogFragment sheetDialogFragment =
Expand Down Expand Up @@ -175,17 +185,55 @@ public AccountInfo getSelectedAccount() {
return mSelectedAccount;
}

/**
* Data passed from ConnectAccountFragment to BraveDappPermissionPromptDialog for pending
* connect account requests
*/
public static class ConnectAccountPendingData {
ConnectAccountPendingData(String accountAddress, int permissionLifetimeOption) {
this.accountAddress = accountAddress;
this.permissionLifetimeOption = permissionLifetimeOption;
}
public String accountAddress;
public int permissionLifetimeOption;
}
private static ConnectAccountPendingData sConnectAccountPendingData;

private static void setConnectAccountPendingData(
String accountAddress, int permissionLifetimeOption) {
sConnectAccountPendingData =
new ConnectAccountPendingData(accountAddress, permissionLifetimeOption);
}

public static ConnectAccountPendingData getAndResetConnectAccountPendingData() {
if (sConnectAccountPendingData == null) {
return null;
}
ConnectAccountPendingData connectAccountPendingDataCopy = sConnectAccountPendingData;
sConnectAccountPendingData = null;
return connectAccountPendingDataCopy;
}

@Override
public void connectAccount(AccountInfo account) {
getBraveWalletService().addPermission(account.accountId, success -> {
if (!success) {
return;
}
if (CoinType.SOL != account.accountId.coin) {
getKeyringService().setSelectedAccount(account.accountId, setSuccess -> {});
Tab tab = BraveRewardsHelper.currentActiveChromeTabbedActivityTab();
if (tab != null) {
if (tab.getWebContents() != null) {
// Static data for BraveDappPermissionPromptDialog.show
setConnectAccountPendingData(account.address, PermissionLifetimeOption.FOREVER);
ConnectAccountFragmentJni.get().connectAccount(
account.address, account.accountId.coin, tab.getWebContents(), success -> {
if (!success) {
return;
}
if (CoinType.SOL != account.accountId.coin) {
getKeyringService().setSelectedAccount(
account.accountId, setSuccess -> {});
}
updateAccounts();
});
}
updateAccounts();
});
}
}

@Override
Expand Down Expand Up @@ -228,4 +276,15 @@ private GURL getCurrentHostHttpAddress() {
}
return GURL.emptyGURL();
}

@CalledByNative
private static void onConnectAccountDone(Callback<Boolean> callback, Boolean result) {
callback.onResult(result);
}

@NativeMethods
interface Natives {
void connectAccount(String accountAddress, int accountIdCoin, WebContents webContents,
Callback<Boolean> callback);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import org.chromium.brave_wallet.mojom.BraveWalletService;
import org.chromium.brave_wallet.mojom.CoinType;
import org.chromium.brave_wallet.mojom.KeyringService;
import org.chromium.brave_wallet.mojom.PermissionLifetimeOption;
import org.chromium.chrome.R;
import org.chromium.chrome.browser.app.BraveActivity;
import org.chromium.chrome.browser.app.domain.WalletModel;
import org.chromium.chrome.browser.app.helpers.ImageLoader;
import org.chromium.chrome.browser.crypto_wallet.BraveWalletServiceFactory;
import org.chromium.chrome.browser.crypto_wallet.KeyringServiceFactory;
import org.chromium.chrome.browser.crypto_wallet.fragments.dapps.ConnectAccountFragment;
import org.chromium.chrome.browser.crypto_wallet.util.Utils;
import org.chromium.chrome.browser.crypto_wallet.util.WalletConstants;
import org.chromium.components.browser_ui.modaldialog.ModalDialogView;
Expand All @@ -50,6 +52,9 @@
import java.lang.ref.WeakReference;
import java.util.List;

/**
* Dialog to grant website permissions to use Dapps
*/
public class BraveDappPermissionPromptDialog
implements ModalDialogProperties.Controller, ConnectionErrorHandler {
private static final String TAG = "BraveDappPermission";
Expand Down Expand Up @@ -113,7 +118,7 @@ void show() {
setFavIcon();
mRecyclerView = customView.findViewById(R.id.accounts_list);

InitBraveWalletService();
initBraveWalletService();
TextView domain = customView.findViewById(R.id.domain);
mBraveWalletService.getActiveOrigin(
originInfo -> { domain.setText(Utils.geteTldSpanned(originInfo)); });
Expand All @@ -131,7 +136,7 @@ void show() {
.with(ModalDialogProperties.FILTER_TOUCH_FOR_SECURITY, true)
.build();
mModalDialogManager.showDialog(mPropertyModel, ModalDialogType.APP);
InitKeyringService();
initKeyringService();
try {
BraveActivity activity = BraveActivity.getBraveActivity();
activity.dismissWalletPanelOrDialog();
Expand All @@ -150,6 +155,11 @@ void show() {
initAccounts();
}

@PermissionLifetimeOption.EnumType
int getPermissionLifetimeOption() {
return PermissionLifetimeOption.FOREVER;
}

@NonNull
private ViewGroup getPermissionModalViewContainer(View customView) {
ViewParent viewParent = customView.getParent();
Expand All @@ -162,7 +172,7 @@ private ViewGroup getPermissionModalViewContainer(View customView) {
return (ViewGroup) viewParent;
}

private void InitBraveWalletService() {
private void initBraveWalletService() {
if (mBraveWalletService != null) {
return;
}
Expand Down Expand Up @@ -198,6 +208,18 @@ public void onAccountCheckChanged(AccountInfo account, boolean isChecked) {
}
}
mAccountsListAdapter.notifyDataSetChanged();

// We are on the flow from ConnectAccountFragment.connectAccount
ConnectAccountFragment.ConnectAccountPendingData capd =
ConnectAccountFragment.getAndResetConnectAccountPendingData();
if (capd != null) {
final String[] selectedAccounts = {capd.accountAddress};
BraveDappPermissionPromptDialogJni.get().onPrimaryButtonClicked(
mNativeDialogController, selectedAccounts,
capd.permissionLifetimeOption);
mModalDialogManager.dismissDialog(
mPropertyModel, DialogDismissalCause.POSITIVE_BUTTON_CLICKED);
}
});
}

Expand Down Expand Up @@ -227,7 +249,7 @@ public String[] getSelectedAccounts() {
public void onClick(PropertyModel model, @ButtonType int buttonType) {
if (buttonType == ButtonType.POSITIVE) {
BraveDappPermissionPromptDialogJni.get().onPrimaryButtonClicked(
mNativeDialogController, getSelectedAccounts());
mNativeDialogController, getSelectedAccounts(), getPermissionLifetimeOption());
mModalDialogManager.dismissDialog(
mPropertyModel, DialogDismissalCause.POSITIVE_BUTTON_CLICKED);
} else if (buttonType == ButtonType.NEGATIVE) {
Expand All @@ -240,7 +262,7 @@ public void onClick(PropertyModel model, @ButtonType int buttonType) {

@Override
public void onDismiss(PropertyModel model, int dismissalCause) {
DisconnectMojoServices();
disconnectMojoServices();
BraveDappPermissionPromptDialogJni.get().onDialogDismissed(mNativeDialogController);
mNativeDialogController = 0;
}
Expand All @@ -250,7 +272,7 @@ private void dismissDialog() {
mModalDialogManager.dismissDialog(mPropertyModel, DialogDismissalCause.DISMISSED_BY_NATIVE);
}

public void DisconnectMojoServices() {
public void disconnectMojoServices() {
mMojoServicesClosed = true;
if (mKeyringService != null) {
mKeyringService.close();
Expand All @@ -269,10 +291,10 @@ public void onConnectionError(MojoException e) {
}
mKeyringService.close();
mKeyringService = null;
InitKeyringService();
initKeyringService();
}

protected void InitKeyringService() {
protected void initKeyringService() {
if (mKeyringService != null) {
return;
}
Expand All @@ -282,8 +304,8 @@ protected void InitKeyringService() {

@NativeMethods
interface Natives {
void onPrimaryButtonClicked(
long nativeBraveDappPermissionPromptDialogController, String[] accounts);
void onPrimaryButtonClicked(long nativeBraveDappPermissionPromptDialogController,
String[] accounts, int permissionLifetimeOption);
void onNegativeButtonClicked(long nativeBraveDappPermissionPromptDialogController);
void onDialogDismissed(long nativeBraveDappPermissionPromptDialogController);
}
Expand Down
1 change: 1 addition & 0 deletions browser/brave_wallet/android/sources.gni
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ brave_browser_brave_wallet_android_sources = [
"//brave/browser/permissions/brave_dapp_permission_prompt_dialog_controller_android.h",
"//brave/browser/permissions/brave_wallet_permission_prompt_android.cc",
"//brave/browser/permissions/brave_wallet_permission_prompt_android.h",
"//brave/browser/permissions/connect_account_android.cc",
]

brave_browser_brave_wallet_android_deps = [
Expand Down
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
11 changes: 4 additions & 7 deletions browser/brave_wallet/ethereum_provider_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/hex_utils.h"
#include "brave/components/permissions/brave_permission_manager.h"
#include "brave/components/permissions/contexts/brave_wallet_permission_context.h"
#include "brave/components/version_info/version_info.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/permissions/permission_manager_factory.h"
Expand Down Expand Up @@ -455,13 +456,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 @@ -191,9 +189,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 @@ -332,13 +327,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 @@ -596,7 +588,6 @@ class SendOrSignTransactionBrowserTest : public InProcessBrowserTest {
}

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

private:
Expand Down
Loading

0 comments on commit 5128052

Please sign in to comment.