Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise the behavior of "Extensions (Brave Wallet fallback)" setting (uplift to 1.50.x) #18229

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ class BraveWalletEthereumChainTest : public InProcessBrowserTest {
}

void SetUpOnMainThread() override {
brave_wallet::SetDefaultEthereumWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWallet);
InProcessBrowserTest::SetUpOnMainThread();
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class BraveWalletEventEmitterTest : public InProcessBrowserTest {
}

void SetUpOnMainThread() override {
brave_wallet::SetDefaultEthereumWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWallet);
InProcessBrowserTest::SetUpOnMainThread();
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "brave/browser/brave_wallet/brave_wallet_tab_helper.h"
#include "brave/browser/brave_wallet/keyring_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/keyring_service.h"
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/constants/brave_paths.h"
Expand Down Expand Up @@ -77,6 +78,9 @@ class BraveWalletSignMessageBrowserTest : public InProcessBrowserTest {
}

void SetUpOnMainThread() override {
brave_wallet::SetDefaultEthereumWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWallet);
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");

Expand Down
4 changes: 4 additions & 0 deletions browser/brave_wallet/brave_wallet_tab_helper_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "brave/browser/brave_wallet/brave_wallet_tab_helper.h"
#include "brave/browser/brave_wallet/json_rpc_service_factory.h"
#include "brave/browser/ui/webui/brave_wallet/wallet_common_ui.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/common/features.h"
#include "brave/components/constants/brave_paths.h"
#include "brave/components/constants/webui_url_constants.h"
Expand Down Expand Up @@ -108,6 +109,9 @@ class BraveWalletTabHelperBrowserTest : public InProcessBrowserTest {
}

void SetUpOnMainThread() override {
brave_wallet::SetDefaultEthereumWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWallet);
InProcessBrowserTest::SetUpOnMainThread();
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");
Expand Down
4 changes: 4 additions & 0 deletions browser/brave_wallet/ethereum_provider_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "base/path_service.h"
#include "base/test/bind.h"
#include "brave/browser/brave_wallet/keyring_service_factory.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/constants/brave_paths.h"
#include "chrome/browser/profiles/profile.h"
Expand Down Expand Up @@ -64,6 +65,9 @@ class EthereumProviderBrowserTest : public InProcessBrowserTest {
}

void SetUpOnMainThread() override {
brave_wallet::SetDefaultEthereumWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWallet);
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ class SendOrSignTransactionBrowserTest : public InProcessBrowserTest {
}

void SetUpOnMainThread() override {
brave_wallet::SetDefaultEthereumWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWallet);
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");

Expand Down
3 changes: 3 additions & 0 deletions browser/brave_wallet/solana_provider_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ class SolanaProviderTest : public InProcessBrowserTest {
~SolanaProviderTest() override = default;

void SetUpOnMainThread() override {
brave_wallet::SetDefaultSolanaWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWallet);
host_resolver()->AddRule("*", "127.0.0.1");

https_server_for_files_.SetSSLConfig(
Expand Down
43 changes: 43 additions & 0 deletions browser/brave_wallet/solana_provider_renderer_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
#include "base/strings/string_number_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "brave/browser/brave_content_browser_client.h"
#include "brave/browser/brave_wallet/keyring_service_factory.h"
#include "brave/browser/profiles/brave_renderer_updater.h"
#include "brave/browser/profiles/brave_renderer_updater_factory.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/brave_wallet/common/brave_wallet_constants.h"
#include "brave/components/brave_wallet/common/features.h"
Expand Down Expand Up @@ -525,6 +529,9 @@ class SolanaProviderRendererTest : public InProcessBrowserTest {

void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
brave_wallet::SetDefaultSolanaWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWallet);
content::SetBrowserClientForTesting(&test_content_browser_client_);
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");
Expand Down Expand Up @@ -630,6 +637,42 @@ IN_PROC_BROWSER_TEST_F(SolanaProviderRendererTest, ExtensionOverwrite) {
"test");
}

IN_PROC_BROWSER_TEST_F(SolanaProviderRendererTest,
DoNotAttachIfNoWalletCreated) {
auto* keyring_service =
brave_wallet::KeyringServiceFactory::GetServiceForContext(
browser()->profile());
keyring_service->Reset(false);

brave_wallet::SetDefaultSolanaWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWalletPreferExtension);
ReloadAndWaitForLoadStop(browser());

std::string command = "window.solana.isBraveWallet";
EXPECT_TRUE(content::EvalJs(web_contents(browser()), command)
.error.find("Cannot read properties of undefined") !=
std::string::npos);
EXPECT_EQ(browser()->tab_strip_model()->GetTabCount(), 1);
}

IN_PROC_BROWSER_TEST_F(SolanaProviderRendererTest, AttachIfWalletCreated) {
auto* keyring_service =
brave_wallet::KeyringServiceFactory::GetServiceForContext(
browser()->profile());
keyring_service->CreateWallet("password", base::DoNothing());

brave_wallet::SetDefaultSolanaWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWalletPreferExtension);
ReloadAndWaitForLoadStop(browser());

constexpr char kEvalIsBraveWallet[] = "window.solana.isBraveWallet";
EXPECT_TRUE(content::EvalJs(web_contents(browser())->GetPrimaryMainFrame(),
kEvalIsBraveWallet)
.ExtractBool());
EXPECT_EQ(browser()->tab_strip_model()->GetTabCount(), 1);
}
IN_PROC_BROWSER_TEST_F(SolanaProviderRendererTest, NonWritable) {
for (const std::string& provider : {"braveSolana", "solana"}) {
// window.braveSolana.* and window.solana.* (methods)
Expand Down
3 changes: 3 additions & 0 deletions browser/brave_wallet/wallet_watch_asset_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class WalletWatchAssetBrowserTest : public InProcessBrowserTest {
}

void SetUpOnMainThread() override {
brave_wallet::SetDefaultEthereumWallet(
browser()->profile()->GetPrefs(),
brave_wallet::mojom::DefaultWallet::BraveWallet);
mock_cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
host_resolver()->AddRule("*", "127.0.0.1");

Expand Down
10 changes: 10 additions & 0 deletions browser/profiles/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Copyright (c) 2018 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/.

import("//brave/components/tor/buildflags/buildflags.gni")
import("//components/gcm_driver/config.gni")
import("//extensions/buildflags/buildflags.gni")

source_set("profiles") {
# Remove when https://github.com/brave/brave-browser/issues/10648 is resolved
Expand Down Expand Up @@ -42,6 +48,10 @@ source_set("profiles") {
"//ui/base",
]

if (enable_extensions) {
deps += [ "//extensions/common" ]
}

if (use_gcm_from_platform) {
deps += [ "//brave/browser/gcm_driver" ]
}
Expand Down
37 changes: 33 additions & 4 deletions browser/profiles/brave_renderer_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

#include "base/functional/bind.h"
#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/brave_wallet/keyring_service_factory.h"
#include "brave/browser/ethereum_remote_client/ethereum_remote_client_constants.h"
#include "brave/common/brave_renderer_configuration.mojom.h"
#include "brave/components/brave_wallet/browser/brave_wallet_utils.h"
#include "brave/components/brave_wallet/browser/keyring_service.h"
#include "brave/components/brave_wallet/browser/pref_names.h"
#include "brave/components/brave_wallet/common/brave_wallet.mojom.h"
#include "brave/components/de_amp/browser/de_amp_util.h"
Expand All @@ -20,9 +23,14 @@
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "extensions/buildflags/buildflags.h"
#include "ipc/ipc_channel_proxy.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/browser/extension_registry.h"
#endif

BraveRendererUpdater::BraveRendererUpdater(Profile* profile)
: profile_(profile), is_wallet_allowed_for_context_(false) {
PrefService* pref_service = profile->GetPrefs();
Expand Down Expand Up @@ -99,12 +107,32 @@ void BraveRendererUpdater::UpdateAllRenderers() {
void BraveRendererUpdater::UpdateRenderer(
mojo::AssociatedRemote<brave::mojom::BraveRendererConfiguration>*
renderer_configuration) {
#if BUILDFLAG(ENABLE_EXTENSIONS)
extensions::ExtensionRegistry* registry =
extensions::ExtensionRegistry::Get(profile_);
bool has_installed_metamask =
registry &&
registry->enabled_extensions().Contains(metamask_extension_id);
#else
bool has_installed_metamask = false;
#endif

brave_wallet::KeyringService* keyring_service =
brave_wallet::KeyringServiceFactory::GetServiceForContext(profile_);
bool wallet_created =
keyring_service &&
keyring_service->IsKeyringCreated(brave_wallet::mojom::kDefaultKeyringId);
bool should_ignore_brave_wallet_for_eth =
!wallet_created || has_installed_metamask;
bool should_ignore_brave_wallet_for_sol = !wallet_created;

auto default_ethereum_wallet =
static_cast<brave_wallet::mojom::DefaultWallet>(
brave_wallet_ethereum_provider_.GetValue());
bool brave_use_native_ethereum_wallet =
(default_ethereum_wallet ==
brave_wallet::mojom::DefaultWallet::BraveWalletPreferExtension ||
((default_ethereum_wallet ==
brave_wallet::mojom::DefaultWallet::BraveWalletPreferExtension &&
!should_ignore_brave_wallet_for_eth) ||
default_ethereum_wallet ==
brave_wallet::mojom::DefaultWallet::BraveWallet) &&
is_wallet_allowed_for_context_ && brave_wallet::IsDappsSupportEnabled();
Expand All @@ -115,8 +143,9 @@ void BraveRendererUpdater::UpdateRenderer(
auto default_solana_wallet = static_cast<brave_wallet::mojom::DefaultWallet>(
brave_wallet_solana_provider_.GetValue());
bool brave_use_native_solana_wallet =
(default_solana_wallet ==
brave_wallet::mojom::DefaultWallet::BraveWalletPreferExtension ||
((default_solana_wallet ==
brave_wallet::mojom::DefaultWallet::BraveWalletPreferExtension &&
!should_ignore_brave_wallet_for_sol) ||
default_solana_wallet ==
brave_wallet::mojom::DefaultWallet::BraveWallet) &&
is_wallet_allowed_for_context_ && brave_wallet::IsDappsSupportEnabled();
Expand Down
11 changes: 11 additions & 0 deletions renderer/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import("//extensions/buildflags/buildflags.gni")
import("//testing/test.gni")

source_set("browser_tests") {
Expand All @@ -22,6 +23,7 @@ source_set("browser_tests") {
deps = [
"//base/test:test_support",
"//brave/browser/brave_wallet",
"//brave/browser/profiles:profiles",
"//brave/common",
"//brave/components/brave_wallet/browser",
"//brave/components/brave_wallet/browser:utils",
Expand All @@ -34,4 +36,13 @@ source_set("browser_tests") {
"//content/test:test_support",
"//net:test_support",
]

if (enable_extensions) {
deps += [
"//brave/browser/ethereum_remote_client:ethereum_remote_client",
"//chrome/browser/extensions",
"//extensions:test_support",
"//extensions/browser",
]
}
}
Loading