Skip to content

Commit

Permalink
Connect/disconnect crypto accounts from ConnectAccountFragment throug…
Browse files Browse the repository at this point in the history
…h permissions
  • Loading branch information
AlexeyBarabash committed Sep 14, 2023
1 parent dfbc4e9 commit 025f593
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 27 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,56 @@ 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) {
assert sConnectAccountPendingData == null;
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;
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();
});
}
if (CoinType.SOL != account.accountId.coin) {
getKeyringService().setSelectedAccount(account.accountId, setSuccess -> {});
}
updateAccounts();
});
}
}

@Override
Expand Down Expand Up @@ -228,4 +277,13 @@ 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,11 +26,13 @@
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.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 @@ -51,6 +53,9 @@
import java.util.Iterator;
import java.util.List;

/**
* Dialog to grant website permissions to use Dapps
*/
public class BraveDappPermissionPromptDialog
implements ModalDialogProperties.Controller, ImageDownloadCallback, 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 @@ -253,7 +275,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 @@ -266,7 +288,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 @@ -276,7 +298,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 @@ -295,10 +317,10 @@ public void onConnectionError(MojoException e) {
}
mKeyringService.close();
mKeyringService = null;
InitKeyringService();
initKeyringService();
}

protected void InitKeyringService() {
protected void initKeyringService() {
if (mKeyringService != null) {
return;
}
Expand All @@ -308,8 +330,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
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ void BraveDappPermissionPromptDialogController::ShowDialog() {

void BraveDappPermissionPromptDialogController::OnPrimaryButtonClicked(
JNIEnv* env,
const base::android::JavaParamRef<jobjectArray>& accounts) {
const base::android::JavaParamRef<jobjectArray>& accounts,
int permission_lifetime_option) {
std::vector<std::string> allowedAccounts;
base::android::AppendJavaStringArrayToStringVector(env, accounts,
&allowedAccounts);
delegate_->ConnectToSite(allowedAccounts);
delegate_->ConnectToSite(allowedAccounts, permission_lifetime_option);
}

void BraveDappPermissionPromptDialogController::OnNegativeButtonClicked(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class BraveDappPermissionPromptDialogController {
class Delegate {
public:
virtual void OnDialogDismissed() = 0;
virtual void ConnectToSite(const std::vector<std::string>& accounts) = 0;
virtual void ConnectToSite(const std::vector<std::string>& accounts,
int permission_lifetime_option) = 0;
virtual void CancelConnectToSite() = 0;
};

Expand All @@ -39,7 +40,8 @@ class BraveDappPermissionPromptDialogController {

void OnPrimaryButtonClicked(
JNIEnv* env,
const base::android::JavaParamRef<jobjectArray>& accounts);
const base::android::JavaParamRef<jobjectArray>& accounts,
int permission_lifetime_option);
void OnNegativeButtonClicked(JNIEnv* env);
void OnDialogDismissed(JNIEnv* env);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ BraveWalletPermissionPrompt::BraveWalletPermissionPrompt(
BraveWalletPermissionPrompt::~BraveWalletPermissionPrompt() {}

void BraveWalletPermissionPrompt::ConnectToSite(
const std::vector<std::string>& accounts) {
const std::vector<std::string>& accounts,
int permission_lifetime_option) {
has_interacted_with_dialog_ = true;
dialog_controller_.reset();
// TODO(SergeyZhukovsky): Use the real option that the user chooses, using
Expand Down
3 changes: 2 additions & 1 deletion browser/permissions/brave_wallet_permission_prompt_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class BraveWalletPermissionPrompt
protected:
// BraveDappPermissionPromptDialogController::Delegate:
void OnDialogDismissed() override;
void ConnectToSite(const std::vector<std::string>& accounts) override;
void ConnectToSite(const std::vector<std::string>& accounts,
int permission_lifetime_option) override;
void CancelConnectToSite() override;

private:
Expand Down
Loading

0 comments on commit 025f593

Please sign in to comment.