Skip to content

Commit

Permalink
Merge pull request #5624 from brave/pr5513_lazy-load-wallet_1.10.x
Browse files Browse the repository at this point in the history
Lazy load Brave Crypto Wallets (uplift to 1.10.x)
  • Loading branch information
srirambv authored May 26, 2020
2 parents d859905 + 8f03003 commit 2b15839
Show file tree
Hide file tree
Showing 42 changed files with 665 additions and 468 deletions.
17 changes: 15 additions & 2 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<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_LOAD_CRYPTO_WALLETS_ON_STARTUP" desc="The description for always loading the Crypto Wallets extension on startup">
Load Crypto Wallets on startup
</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.
</message>
Expand Down Expand Up @@ -719,18 +722,28 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
<message name="IDS_BRAVE_CRYPTO_WALLETS_METAMASK_INFOBAR_TEXT" desc="Text that shows on the info bar when a Dapp is detected">
Would you like to use Brave's Crypto Wallets or MetaMask for Dapps?
</message>
<message name="IDS_BRAVE_CRYPTO_WALLETS_LAZY_LOAD_TEXT" desc="Text that shows on the info bar when a Dapp is detected">
Would you like to start Crypto Wallets for web3 support on this page?
</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.
</message>
<message name="IDS_BRAVE_CRYPTO_WALLETS_USE_CRYPTO_WALLETS" desc="Use Crypto Wallets">
Use Crypto Wallets
<message name="IDS_BRAVE_CRYPTO_WALLETS_SETUP_CRYPTO_WALLETS" desc="Setup Crypto Wallets">
Setup Crypto Wallets
</message>
<message name="IDS_BRAVE_CRYPTO_WALLETS_USE_METAMASK" desc="Use MetaMask and don't use Crypto Wallets">
Use MetaMask
</message>
<message name="IDS_BRAVE_CRYPTO_WALLETS_SETUP" desc="Use Crypto Wallets">
SETUP
</message>
<message name="IDS_BRAVE_CRYPTO_WALLETS_START_AND_RELOAD" desc="Start Crypto Wallets and reload">
Start Crypto Wallets and reload
</message>
<message name="IDS_BRAVE_CRYPTO_WALLETS_SETTINGS" desc="Open Crypto Wallets settings">
Settings
</message>

<!-- Brave Wayback Machine -->
<message name="IDS_BRAVE_WAYBACK_MACHINE_INFOBAR_PAGE_MISSING_TEXT" desc="The label for sorry message when page is not available on infobar">
Sorry, that page is missing.
Expand Down
20 changes: 10 additions & 10 deletions browser/brave_content_browser_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ using extensions::ChromeContentBrowserClientExtensionsPart;
#include "brave/components/services/tor/tor_launcher_service.h"
#endif

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
#include "brave/browser/extensions/brave_wallet_navigation_throttle.h"
#endif

#if BUILDFLAG(ENABLE_SPEEDREADER)
#include "brave/browser/speedreader/speedreader_tab_helper.h"
#include "brave/components/speedreader/speedreader_throttle.h"
Expand All @@ -107,6 +103,11 @@ using extensions::ChromeContentBrowserClientExtensionsPart;
#include "brave/browser/binance/binance_protocol_handler.h"
#endif

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/components/brave_wallet/browser/brave_wallet_service.h"
#endif

namespace {

bool HandleURLReverseOverrideRewrite(GURL* url,
Expand Down Expand Up @@ -430,7 +431,11 @@ bool BraveContentBrowserClient::HandleURLOverrideRewrite(GURL* url,
}

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
if (url->SchemeIs(content::kChromeUIScheme) &&
// If the Crypto Wallets extension is loaded, then it replaces the WebUI
Profile* profile = Profile::FromBrowserContext(browser_context);
auto* service = BraveWalletServiceFactory::GetForProfile(profile);
if (service->IsCryptoWalletsReady() &&
url->SchemeIs(content::kChromeUIScheme) &&
url->host() == ethereum_remote_client_host) {
auto* registry = extensions::ExtensionRegistry::Get(browser_context);
if (registry->ready_extensions().GetByID(
Expand All @@ -450,11 +455,6 @@ BraveContentBrowserClient::CreateThrottlesForNavigation(
std::vector<std::unique_ptr<content::NavigationThrottle>> throttles =
ChromeContentBrowserClient::CreateThrottlesForNavigation(handle);

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
throttles.push_back(
std::make_unique<extensions::BraveWalletNavigationThrottle>(handle));
#endif

#if BUILDFLAG(ENABLE_BRAVE_WEBTORRENT)
throttles.push_back(
std::make_unique<extensions::BraveWebTorrentNavigationThrottle>(handle));
Expand Down
2 changes: 2 additions & 0 deletions browser/brave_profile_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterStringPref(kBraveWalletEncryptedSeed, "");
registry->RegisterIntegerPref(kBraveWalletWeb3Provider,
static_cast<int>(BraveWalletWeb3ProviderTypes::ASK));
registry->RegisterBooleanPref(kLoadCryptoWalletsOnStartup, false);
registry->RegisterBooleanPref(kOptedIntoCryptoWallets, false);

// Binance widget
#if BUILDFLAG(BINANCE_ENABLED)
Expand Down
5 changes: 5 additions & 0 deletions browser/brave_profile_prefs_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ IN_PROC_BROWSER_TEST_F(BraveProfilePrefsBrowserTest, MiscBravePrefs) {
EXPECT_EQ(
browser()->profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider),
static_cast<int>(BraveWalletWeb3ProviderTypes::ASK));
EXPECT_FALSE(
browser()->profile()->GetPrefs()->GetBoolean(
kLoadCryptoWalletsOnStartup));
EXPECT_FALSE(
browser()->profile()->GetPrefs()->GetBoolean(kOptedIntoCryptoWallets));

#if !BUILDFLAG(USE_GCM_FROM_PLATFORM)
EXPECT_FALSE(
Expand Down
2 changes: 0 additions & 2 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ source_set("extensions") {
sources += [
"api/brave_wallet_api.cc",
"api/brave_wallet_api.h",
"brave_wallet_navigation_throttle.cc",
"brave_wallet_navigation_throttle.h",
"brave_wallet_util.cc",
"brave_wallet_util.h"
]
Expand Down
68 changes: 59 additions & 9 deletions browser/extensions/api/brave_wallet_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,69 @@ BraveWalletPromptToEnableWalletFunction::Run() {
InfoBarService* infobar_service =
InfoBarService::FromWebContents(contents);
if (infobar_service) {
CryptoWalletsInfoBarDelegate::InfobarSubType subtype =
CryptoWalletsInfoBarDelegate::InfobarSubType::GENERIC_SETUP;
auto* service = GetBraveWalletService(browser_context());
auto* registry = extensions::ExtensionRegistry::Get(profile);
bool metamask_enabled = !brave::IsTorProfile(profile) &&
registry->ready_extensions().GetByID(metamask_extension_id);
CryptoWalletsInfoBarDelegate::Create(infobar_service, metamask_enabled);
if (service->ShouldShowLazyLoadInfobar()) {
subtype = CryptoWalletsInfoBarDelegate::InfobarSubType::
LOAD_CRYPTO_WALLETS;
} else if (registry->ready_extensions().Contains(metamask_extension_id)) {
subtype = CryptoWalletsInfoBarDelegate::InfobarSubType::
CRYPTO_WALLETS_METAMASK;
}
CryptoWalletsInfoBarDelegate::Create(infobar_service, subtype);
}

return RespondNow(NoArguments());
}

ExtensionFunction::ResponseAction
BraveWalletIsInstalledFunction::Run() {
BraveWalletReadyFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
if (brave::IsTorProfile(profile)) {
return RespondNow(Error("Not available in Tor profile"));
}

auto* service = GetBraveWalletService(browser_context());
service->CryptoWalletsExtensionReady();
return RespondNow(NoArguments());
}

ExtensionFunction::ResponseAction
BraveWalletLoadUIFunction::Run() {
auto* service = GetBraveWalletService(browser_context());
// If the extension is already ready, respond right away
if (service->IsCryptoWalletsReady()) {
return RespondNow(NoArguments());
}

// If the user has opt-ed in and MetaMask is not installed, then
// set the Dapp provider to Crypto Wallets.
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* registry = extensions::ExtensionRegistry::Get(profile);
bool enabled = !brave::IsTorProfile(profile) &&
registry->ready_extensions().GetByID(ethereum_remote_client_extension_id);
return RespondNow(OneArgument(std::make_unique<base::Value>(enabled)));
if (!registry->ready_extensions().Contains(metamask_extension_id)) {
profile->GetPrefs()->SetInteger(kBraveWalletWeb3Provider,
static_cast<int>(BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS));
}

profile->GetPrefs()->SetBoolean(kOptedIntoCryptoWallets, true);
service->LoadCryptoWalletsExtension(base::BindOnce(
&BraveWalletLoadUIFunction::OnLoaded, this));
return RespondLater();
}

void BraveWalletLoadUIFunction::OnLoaded() {
Respond(NoArguments());
}

ExtensionFunction::ResponseAction
BraveWalletShouldPromptForSetupFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* service = BraveWalletServiceFactory::GetForProfile(profile);
bool should_prompt = !service->IsCryptoWalletsSetup() &&
!profile->GetPrefs()->GetBoolean(kOptedIntoCryptoWallets);
return RespondNow(OneArgument(std::make_unique<base::Value>(should_prompt)));
}

ExtensionFunction::ResponseAction
Expand All @@ -104,7 +151,10 @@ BraveWalletShouldCheckForDappsFunction::Run() {
profile->GetPrefs()->GetInteger(kBraveWalletWeb3Provider));
bool dappDetection = !brave::IsTorProfile(profile);
if (provider != BraveWalletWeb3ProviderTypes::ASK) {
dappDetection = false;
auto* service = GetBraveWalletService(browser_context());
dappDetection = provider == BraveWalletWeb3ProviderTypes::ASK ||
(provider == BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS &&
!service->IsCryptoWalletsReady());
}
return RespondNow(OneArgument(
std::make_unique<base::Value>(dappDetection)));
Expand Down Expand Up @@ -201,7 +251,7 @@ BraveWalletGetWeb3ProviderListFunction::Run() {
BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS));
Profile* profile = Profile::FromBrowserContext(browser_context());
auto* registry = extensions::ExtensionRegistry::Get(profile);
if (registry->ready_extensions().GetByID(metamask_extension_id)) {
if (registry->ready_extensions().Contains(metamask_extension_id)) {
list.Append(MakeSelectValue(
l10n_util::GetStringUTF16(IDS_BRAVE_WALLET_WEB3_PROVIDER_METAMASK),
BraveWalletWeb3ProviderTypes::METAMASK));
Expand Down
26 changes: 23 additions & 3 deletions browser/extensions/api/brave_wallet_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ class BraveWalletPromptToEnableWalletFunction :
ResponseAction Run() override;
};

class BraveWalletReadyFunction :
public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveWallet.ready", UNKNOWN)

protected:
~BraveWalletReadyFunction() override {}
ResponseAction Run() override;
};

class BraveWalletShouldCheckForDappsFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveWallet.shouldCheckForDapps", UNKNOWN)
Expand All @@ -34,12 +44,22 @@ class BraveWalletShouldCheckForDappsFunction : public ExtensionFunction {
ResponseAction Run() override;
};

class BraveWalletIsInstalledFunction : public ExtensionFunction {
class BraveWalletLoadUIFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveWallet.loadUI", UNKNOWN)
void OnLoaded();

protected:
~BraveWalletLoadUIFunction() override {}
ResponseAction Run() override;
};

class BraveWalletShouldPromptForSetupFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveWallet.isInstalled", UNKNOWN)
DECLARE_EXTENSION_FUNCTION("braveWallet.shouldPromptForSetup", UNKNOWN)

protected:
~BraveWalletIsInstalledFunction() override {}
~BraveWalletShouldPromptForSetupFunction() override {}
ResponseAction Run() override;
};

Expand Down
2 changes: 2 additions & 0 deletions browser/extensions/api/settings_private/brave_prefs_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() {
// Brave Wallet pref
(*s_brave_whitelist)[kBraveWalletWeb3Provider] =
settings_api::PrefType::PREF_TYPE_NUMBER;
(*s_brave_whitelist)[kLoadCryptoWalletsOnStartup] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
// Media Router Pref
(*s_brave_whitelist)[kBraveEnabledMediaRouter] =
settings_api::PrefType::PREF_TYPE_BOOLEAN;
Expand Down
14 changes: 4 additions & 10 deletions browser/extensions/brave_component_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,10 @@ void BraveComponentLoader::AddDefaultComponentExtensions(
#endif

#if BUILDFLAG(BRAVE_WALLET_ENABLED)
// If Crypto Wallets has been loaded at least once before, load it
// always.
// We could only do this when the provider is Crypto Wallets, but
// it would cause a bug with loading brave://wallet not loading
// if a tab is left open and you restart the browser. That would
// need to be fixed first.
// Does not load if project id is not configured.
bool has_project_id = HasInfuraProjectID();
if (has_project_id && ExtensionPrefs::Get(profile_)->
HasPrefForExtension(ethereum_remote_client_extension_id)) {
// Only load if the eagerly load Crypto Wallets setting is on and there is a
// project id configured in the build.
if (HasInfuraProjectID() &&
profile_prefs_->GetBoolean(kLoadCryptoWalletsOnStartup)) {
AddEthereumRemoteClientExtension();
}
#endif
Expand Down
14 changes: 14 additions & 0 deletions browser/extensions/brave_wallet_apitest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,25 @@ IN_PROC_BROWSER_TEST_F(BraveWalletExtensionApiTest,
ResultCatcher catcher;
const Extension* extension =
LoadExtension(extension_dir_.AppendASCII("braveShieldsWithWallet"));
LoadExtension(extension_dir_.AppendASCII("braveWallet"));
ASSERT_TRUE(browsertest_util::ExecuteScriptInBackgroundPageNoWait(
browser()->profile(), brave_extension_id, "testNoDappCheck()"));
ASSERT_TRUE(extension);
ASSERT_TRUE(catcher.GetNextResult()) << message_;
}

IN_PROC_BROWSER_TEST_F(BraveWalletExtensionApiTest,
BraveShieldsDappDetectionWhenCryptoWalletsNotReady) {
GetPrefs()->SetInteger(kBraveWalletWeb3Provider,
static_cast<int>(BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS));
ResultCatcher catcher;
const Extension* extension =
LoadExtension(extension_dir_.AppendASCII("braveShieldsWithWallet"));
ASSERT_TRUE(browsertest_util::ExecuteScriptInBackgroundPageNoWait(
browser()->profile(), brave_extension_id, "testDappCheck()"));
ASSERT_TRUE(extension);
ASSERT_TRUE(catcher.GetNextResult()) << message_;
}

} // namespace
} // namespace extensions
Loading

0 comments on commit 2b15839

Please sign in to comment.