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

Improve compatibility with MetaMask and allow web3 provider selection #4273

Merged
merged 1 commit into from
Jan 31, 2020
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
18 changes: 15 additions & 3 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,20 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_SETTINGS_WEBTORRENT_ENABLED_DESC" desc="The description for WebTorrent switch in settings">
Uses WebTorrent to display torrents directly in the browser. Supports torrent files and magnet links.
</message>
<message name="IDS_SETTINGS_BRAVE_WALLET_ENABLED_DESC" desc="The description for Brave Wallet switch in settings">
Provides an Ethereum wallet and Dapp browser inside Brave.
<message name="IDS_BRAVE_WALLET_WEB3_PROVIDER_ASK" desc="Select control value for which web3 provider to use">
Ask
</message>
<message name="IDS_BRAVE_WALLET_WEB3_PROVIDER_NONE" desc="Select control value for which web3 provider to use">
None
</message>
<message name="IDS_BRAVE_WALLET_WEB3_PROVIDER_CRYPTO_WALLETS" desc="Select control value for which web3 provider to use">
Crypto Wallets
</message>
<message name="IDS_BRAVE_WALLET_WEB3_PROVIDER_METAMASK" desc="Select control value for which web3 provider to use">
MetaMask
</message>
<message name="IDS_SETTINGS_BRAVE_WEB3_PROVIDER_DESC" desc="The description for Brave web3 provider select control in settings">
Web3 provider for using Dapps
</message>
<message name="IDS_SETTINGS_HANGOUTS_ENABLED_DESC" desc="The description for Hangouts switch in settings">
Uses Hangouts component to enable screen sharing and other features in the browser.
Expand Down Expand Up @@ -658,7 +670,7 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
</message>
<!-- Web3 detection -->
<message name="IDS_BRAVE_CRYPTO_WALLETS_METAMASK_INFOBAR_TEXT" desc="Text that shows on the info bar when a Dapp is detected">
Crypto Wallets is now available in Brave. You can't use MetaMask and Crypto Wallets at the same time.
Would you like to use Brave's Crypto Wallets or MetaMask for Dapps?
</message>
<message name="IDS_BRAVE_CRYPTO_WALLETS_INFOBAR_TEXT" desc="Text that shows on the info bar when a Dapp is detected">
Set up Crypto Wallets to interact with this app and others like it.
Expand Down
1 change: 1 addition & 0 deletions browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ source_set("browser_process") {
if (brave_wallet_enabled) {
deps += [
"//brave/components/brave_wallet/browser",
"brave_wallet",
]
}

Expand Down
13 changes: 5 additions & 8 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,11 @@ bool BraveContentBrowserClient::HandleURLOverrideRewrite(GURL* url,
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
if (url->SchemeIs(content::kChromeUIScheme) &&
url->host() == ethereum_remote_client_host) {
Profile* profile = Profile::FromBrowserContext(browser_context);
if (profile->GetPrefs()->GetBoolean(kBraveWalletEnabled)) {
auto* registry = extensions::ExtensionRegistry::Get(browser_context);
if (registry->ready_extensions().GetByID(
ethereum_remote_client_extension_id)) {
*url = GURL(ethereum_remote_client_base_url);
return true;
}
auto* registry = extensions::ExtensionRegistry::Get(browser_context);
if (registry->ready_extensions().GetByID(
ethereum_remote_client_extension_id)) {
*url = GURL(ethereum_remote_client_base_url);
return true;
}
}
#endif
Expand Down
14 changes: 13 additions & 1 deletion browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#include "brave/browser/brave_profile_prefs.h"

#include "brave/browser/themes/brave_dark_mode_utils.h"
#include "brave/common/brave_wallet_constants.h"
#include "brave/common/pref_names.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_sync/brave_sync_prefs.h"
#include "brave/components/brave_wallet/browser/buildflags/buildflags.h"
#include "brave/components/brave_wayback_machine/buildflags.h"
#include "brave/components/brave_webtorrent/browser/buildflags/buildflags.h"
#include "chrome/browser/net/prediction_options.h"
Expand Down Expand Up @@ -45,6 +47,10 @@
#include "brave/components/brave_wayback_machine/pref_names.h"
#endif

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
#include "brave/browser/brave_wallet/brave_wallet_utils.h"
#endif

using extensions::FeatureSwitch;

namespace brave {
Expand All @@ -56,6 +62,10 @@ void RegisterProfilePrefsForMigration(
#endif

dark_mode::RegisterBraveDarkModePrefsForMigration(registry);

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
brave_wallet::RegisterBraveWalletProfilePrefsForMigration(registry);
#endif
}

void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
Expand Down Expand Up @@ -187,9 +197,11 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(kNewTabPageShowRewards, true);

// Brave Wallet
registry->RegisterIntegerPref(kBraveWalletPrefVersion, 0);
registry->RegisterStringPref(kBraveWalletAES256GCMSivNonce, "");
registry->RegisterStringPref(kBraveWalletEncryptedSeed, "");
registry->RegisterBooleanPref(kBraveWalletEnabled, true);
registry->RegisterIntegerPref(kBraveWalletWeb3Provider,
static_cast<int>(BraveWalletWeb3ProviderTypes::ASK));

// Autocomplete in address bar
registry->RegisterBooleanPref(kAutocompleteEnabled, true);
Expand Down
6 changes: 4 additions & 2 deletions browser/brave_profile_prefs_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/common/pref_names.h"
#include "brave/components/brave_wayback_machine/buildflags.h"
#include "brave/common/brave_wallet_constants.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
Expand Down Expand Up @@ -60,8 +61,9 @@ IN_PROC_BROWSER_TEST_F(BraveProfilePrefsBrowserTest, MiscBravePrefs) {
browser()->profile()->GetPrefs()->GetBoolean(kHideBraveRewardsButton));
EXPECT_FALSE(
browser()->profile()->GetPrefs()->GetBoolean(kIPFSCompanionEnabled));
EXPECT_TRUE(
browser()->profile()->GetPrefs()->GetBoolean(kBraveWalletEnabled));
EXPECT_EQ(
browser()->profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider),
static_cast<int>(BraveWalletWeb3ProviderTypes::ASK));

#if !BUILDFLAG(USE_GCM_FROM_PLATFORM)
EXPECT_FALSE(
Expand Down
17 changes: 17 additions & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import("//brave/components/brave_wallet/browser/buildflags/buildflags.gni")

assert(brave_wallet_enabled)

source_set("brave_wallet") {
sources = [
"brave_wallet_service_factory.h",
"brave_wallet_service_factory.cc",
"brave_wallet_utils.cc",
"brave_wallet_utils.h",
]
deps = [
"//components/pref_registry",
"//components/prefs",
"//extensions/browser",
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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/. */

#include "brave/components/brave_wallet/browser/brave_wallet_service_factory.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"

#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#include "chrome/browser/profiles/incognito_helpers.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* 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/. */

#ifndef BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_FACTORY_H_
#define BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_FACTORY_H_
#ifndef BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_FACTORY_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_FACTORY_H_

#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
Expand All @@ -31,4 +31,4 @@ class BraveWalletServiceFactory : public BrowserContextKeyedServiceFactory {
DISALLOW_COPY_AND_ASSIGN(BraveWalletServiceFactory);
};

#endif // BRAVE_COMPONENTS_BRAVE_WALLET_BROWSER_BRAVE_WALLET_SERVICE_FACTORY_H_
#endif // BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_SERVICE_FACTORY_H_
72 changes: 72 additions & 0 deletions browser/brave_wallet/brave_wallet_utils.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Copyright (c) 2020 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 http://mozilla.org/MPL/2.0/. */

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

#include "brave/common/brave_wallet_constants.h"
#include "brave/common/extensions/extension_constants.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/profiles/profile.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "extensions/browser/extension_prefs.h"

namespace {

void MigrateBraveWalletPrefs_V0_V1(Profile* profile) {
PrefService* prefs = profile->GetPrefs();
bool wallet_was_enabled = true; // true was the default
if (prefs->HasPrefPath(kBraveWalletEnabledDeprecated)) {
wallet_was_enabled = prefs->GetBoolean(kBraveWalletEnabledDeprecated);
}

bool has_crypto_wallets = extensions::ExtensionPrefs::Get(profile)->
HasPrefForExtension(ethereum_remote_client_extension_id);
bool has_metamask = extensions::ExtensionPrefs::Get(profile)->
HasPrefForExtension(metamask_extension_id);

BraveWalletWeb3ProviderTypes provider =
BraveWalletWeb3ProviderTypes::ASK;
if (!wallet_was_enabled&& has_metamask) {
// If Crypto Wallets was disabled and MetaMask is installed, set to MetaMask
provider = BraveWalletWeb3ProviderTypes::METAMASK;
} else if (!wallet_was_enabled && !has_metamask) {
// If Crypto Wallets is diabled, and MetaMask not installed, set None
provider = BraveWalletWeb3ProviderTypes::NONE;
} else if (wallet_was_enabled && has_metamask) {
// If Crypto Wallets is enabled, and MetaMask is installed, set
// to Crypto Wallets
provider = BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS;
} else if (has_crypto_wallets && wallet_was_enabled) {
// If CryptoWallets is enabled and installed, but MetaMask is not
// installed, set Crypto Wallets.
provider = BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS;
} else if (!has_crypto_wallets && wallet_was_enabled) {
// If CryptoWallets is enabled and not installed yet, and MetaMask is not
// installed, set Ask
provider = BraveWalletWeb3ProviderTypes::ASK;
}
prefs->SetInteger(kBraveWalletWeb3Provider, static_cast<int>(provider));
prefs->ClearPref(kBraveWalletEnabledDeprecated);
prefs->SetInteger(kBraveWalletPrefVersion, 1);
}

} // namespace

namespace brave_wallet {

void RegisterBraveWalletProfilePrefsForMigration(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(kBraveWalletEnabledDeprecated, true);
}

void MigrateBraveWalletPrefs(Profile* profile) {
PrefService* prefs = profile->GetPrefs();
if (prefs->GetInteger(kBraveWalletPrefVersion) == 0) {
MigrateBraveWalletPrefs_V0_V1(profile);
}
}

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

#ifndef BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_
#define BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_

class Profile;

namespace user_prefs {
class PrefRegistrySyncable;
}

namespace brave_wallet {

void MigrateBraveWalletPrefs(Profile* profile);
void RegisterBraveWalletProfilePrefsForMigration(
user_prefs::PrefRegistrySyncable* registry);

} // namespace brave_wallet

#endif // BRAVE_BROWSER_BRAVE_WALLET_BRAVE_WALLET_UTILS_H_
Loading