From 0cf75093d6a36d1eedc57178adf95b7c8e98e7e8 Mon Sep 17 00:00:00 2001 From: brave-builds Date: Fri, 22 May 2020 17:37:38 +0000 Subject: [PATCH 1/2] Uplift of #5513 (squashed) to beta --- app/brave_generated_resources.grd | 17 +- browser/brave_content_browser_client.cc | 20 +- browser/brave_profile_prefs.cc | 2 + browser/brave_profile_prefs_browsertest.cc | 5 + browser/extensions/BUILD.gn | 2 - browser/extensions/api/brave_wallet_api.cc | 68 +++++- browser/extensions/api/brave_wallet_api.h | 26 +- .../api/settings_private/brave_prefs_util.cc | 2 + browser/extensions/brave_component_loader.cc | 14 +- browser/extensions/brave_wallet_apitest.cc | 14 ++ .../brave_wallet_navigation_throttle.cc | 119 --------- .../brave_wallet_navigation_throttle.h | 50 ---- ...ave_wallet_navigation_throttle_unittest.cc | 228 ------------------ .../crypto_wallets_infobar_delegate.cc | 59 ++++- .../crypto_wallets_infobar_delegate.h | 17 +- .../brave_default_extensions_page.html | 4 + browser/ui/brave_pages.cc | 6 + browser/ui/brave_pages.h | 1 + .../brave_location_bar_model_delegate.cc | 6 +- browser/ui/webui/brave_webui_source.cc | 12 +- .../browser/sessions/session_common_utils.cc | 21 ++ .../settings_localized_strings_provider.cc | 2 + common/extensions/api/_api_features.json | 3 +- .../api/_brave_wallet_api_features.json | 3 +- common/extensions/api/brave_wallet.json | 21 +- common/pref_names.cc | 2 + common/pref_names.h | 2 + common/webui_url_constants.cc | 1 + common/webui_url_constants.h | 2 + .../browser/brave_wallet_service.cc | 39 +++ .../browser/brave_wallet_service.h | 9 +- components/brave_wallet_ui/brave_wallet.html | 36 ++- components/brave_wallet_ui/brave_wallet.tsx | 60 ++++- components/brave_wallet_ui/components/app.tsx | 22 ++ .../opt-in/assets/crypto-banner.tsx | 41 ++++ .../components/opt-in/index.tsx | 91 +++++++ .../components/opt-in/style.ts | 84 +++++++ components/definitions/chromel.d.ts | 4 +- .../resources/brave_components_strings.grd | 8 + test/BUILD.gn | 1 - .../api_test/braveWallet/background.js | 4 +- .../br_elements/br_toolbar/br_toolbar.html | 2 +- 42 files changed, 662 insertions(+), 468 deletions(-) delete mode 100644 browser/extensions/brave_wallet_navigation_throttle.cc delete mode 100644 browser/extensions/brave_wallet_navigation_throttle.h delete mode 100644 browser/extensions/brave_wallet_navigation_throttle_unittest.cc create mode 100644 chromium_src/chrome/browser/sessions/session_common_utils.cc create mode 100644 components/brave_wallet_ui/components/app.tsx create mode 100644 components/brave_wallet_ui/components/opt-in/assets/crypto-banner.tsx create mode 100644 components/brave_wallet_ui/components/opt-in/index.tsx create mode 100644 components/brave_wallet_ui/components/opt-in/style.ts diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index ce3acfb986d4..3fa5245eec38 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -520,6 +520,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U Web3 provider for using Dapps + + Load Crypto Wallets on startup + Uses Hangouts component to enable screen sharing and other features in the browser. @@ -719,11 +722,14 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U Would you like to use Brave's Crypto Wallets or MetaMask for Dapps? + + Would you like to start Crypto Wallets for web3 support on this page? + Set up Crypto Wallets to interact with this app and others like it. - - Use Crypto Wallets + + Setup Crypto Wallets Use MetaMask @@ -731,6 +737,13 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U SETUP + + Start Crypto Wallets and reload + + + Settings + + Sorry, that page is missing. diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 6393e55107b0..3869fe7935a4 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -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" @@ -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, @@ -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( @@ -450,11 +455,6 @@ BraveContentBrowserClient::CreateThrottlesForNavigation( std::vector> throttles = ChromeContentBrowserClient::CreateThrottlesForNavigation(handle); -#if BUILDFLAG(BRAVE_WALLET_ENABLED) - throttles.push_back( - std::make_unique(handle)); -#endif - #if BUILDFLAG(ENABLE_BRAVE_WEBTORRENT) throttles.push_back( std::make_unique(handle)); diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index 088452a65b84..8334f484a501 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -209,6 +209,8 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { registry->RegisterStringPref(kBraveWalletEncryptedSeed, ""); registry->RegisterIntegerPref(kBraveWalletWeb3Provider, static_cast(BraveWalletWeb3ProviderTypes::ASK)); + registry->RegisterBooleanPref(kLoadCryptoWalletsOnStartup, false); + registry->RegisterBooleanPref(kOptedIntoCryptoWallets, false); // Binance widget #if BUILDFLAG(BINANCE_ENABLED) diff --git a/browser/brave_profile_prefs_browsertest.cc b/browser/brave_profile_prefs_browsertest.cc index 8e3e479f58c5..58ea8b98b1d7 100644 --- a/browser/brave_profile_prefs_browsertest.cc +++ b/browser/brave_profile_prefs_browsertest.cc @@ -66,6 +66,11 @@ IN_PROC_BROWSER_TEST_F(BraveProfilePrefsBrowserTest, MiscBravePrefs) { EXPECT_EQ( browser()->profile()->GetPrefs()->GetInteger(kBraveWalletWeb3Provider), static_cast(BraveWalletWeb3ProviderTypes::ASK)); + EXPECT_FALSE( + browser()->profile()->GetPrefs()->GetBoolean( + kLoadCryptoWalletsOnStartup)); + EXPECT_FALSE( + browser()->profile()->GetPrefs()->GetBoolean(kOptedIntoCryptoWallets)); #if !BUILDFLAG(USE_GCM_FROM_PLATFORM) EXPECT_FALSE( diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn index 13397170f6e3..fb10e58da95d 100644 --- a/browser/extensions/BUILD.gn +++ b/browser/extensions/BUILD.gn @@ -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" ] diff --git a/browser/extensions/api/brave_wallet_api.cc b/browser/extensions/api/brave_wallet_api.cc index e1aaa5deaf2c..b8377d9dd95b 100644 --- a/browser/extensions/api/brave_wallet_api.cc +++ b/browser/extensions/api/brave_wallet_api.cc @@ -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(enabled))); + if (!registry->ready_extensions().Contains(metamask_extension_id)) { + profile->GetPrefs()->SetInteger(kBraveWalletWeb3Provider, + static_cast(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(should_prompt))); } ExtensionFunction::ResponseAction @@ -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(dappDetection))); @@ -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)); diff --git a/browser/extensions/api/brave_wallet_api.h b/browser/extensions/api/brave_wallet_api.h index b00877e9494a..3df36514b28d 100644 --- a/browser/extensions/api/brave_wallet_api.h +++ b/browser/extensions/api/brave_wallet_api.h @@ -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) @@ -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; }; diff --git a/browser/extensions/api/settings_private/brave_prefs_util.cc b/browser/extensions/api/settings_private/brave_prefs_util.cc index b264cbc3107c..9f2ce62f7f0b 100644 --- a/browser/extensions/api/settings_private/brave_prefs_util.cc +++ b/browser/extensions/api/settings_private/brave_prefs_util.cc @@ -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; diff --git a/browser/extensions/brave_component_loader.cc b/browser/extensions/brave_component_loader.cc index e92e4861e81c..e3363d3012d2 100644 --- a/browser/extensions/brave_component_loader.cc +++ b/browser/extensions/brave_component_loader.cc @@ -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 diff --git a/browser/extensions/brave_wallet_apitest.cc b/browser/extensions/brave_wallet_apitest.cc index b0be53b616df..81c2a817d85c 100644 --- a/browser/extensions/brave_wallet_apitest.cc +++ b/browser/extensions/brave_wallet_apitest.cc @@ -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(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 diff --git a/browser/extensions/brave_wallet_navigation_throttle.cc b/browser/extensions/brave_wallet_navigation_throttle.cc deleted file mode 100644 index 84040f5b7996..000000000000 --- a/browser/extensions/brave_wallet_navigation_throttle.cc +++ /dev/null @@ -1,119 +0,0 @@ -/* Copyright (c) 2019 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/extensions/brave_wallet_navigation_throttle.h" - -#include "base/bind.h" -#include "brave/browser/extensions/brave_component_loader.h" -#include "brave/browser/profiles/profile_util.h" -#include "brave/common/extensions/extension_constants.h" -#include "brave/common/pref_names.h" -#include "brave/browser/extensions/brave_wallet_util.h" -#include "brave/components/brave_wallet/browser/buildflags/buildflags.h" -#include "chrome/browser/extensions/extension_service.h" -#include "chrome/browser/profiles/profile.h" -#include "content/public/browser/browser_context.h" -#include "content/public/browser/browser_thread.h" -#include "content/public/browser/navigation_handle.h" -#include "content/public/browser/web_contents.h" -#include "content/public/common/url_constants.h" -#include "extensions/browser/extension_registry.h" -#include "extensions/browser/extension_system.h" -#include "extensions/common/extension.h" - -namespace extensions { - -BraveWalletNavigationThrottle::BraveWalletNavigationThrottle( - content::NavigationHandle* navigation_handle) : - content::NavigationThrottle(navigation_handle), - extension_registry_observer_(this), - resume_pending_(false) { - extension_registry_observer_.Add( - ExtensionRegistry::Get( - navigation_handle->GetWebContents()->GetBrowserContext())); -} - -BraveWalletNavigationThrottle::~BraveWalletNavigationThrottle() { - timer_.Stop(); -} - -content::NavigationThrottle::ThrottleCheckResult -BraveWalletNavigationThrottle::WillStartRequest() { - DCHECK_CURRENTLY_ON(content::BrowserThread::UI); - content::WebContents* web_contents = navigation_handle()->GetWebContents(); - - // Is this navigation targeting an extension resource? - const GURL& url = navigation_handle()->GetURL(); - if (url.SchemeIs(content::kChromeUIScheme) && - url.host() == ethereum_remote_client_host) { - // If a user has explicitly disabled the Brave Wallet, - // or a project id is not configured, then don't defer - // and try to install it. - content::BrowserContext* browser_context = - web_contents->GetBrowserContext(); - Profile* profile = Profile::FromBrowserContext(browser_context); - bool has_project_id = HasInfuraProjectID(); - if (brave::IsTorProfile(profile) || !has_project_id) { - return content::NavigationThrottle::BLOCK_REQUEST; - } - auto* registry = ExtensionRegistry::Get(browser_context); - if (!registry->ready_extensions().GetByID( - ethereum_remote_client_extension_id)) { - resume_pending_ = true; - extensions::ExtensionService* service = - extensions::ExtensionSystem::Get(browser_context)->extension_service(); - if (service) { - extensions::ComponentLoader* loader = service->component_loader(); - static_cast(loader)-> - AddEthereumRemoteClientExtension(); - } - return content::NavigationThrottle::DEFER; - } - } - return content::NavigationThrottle::PROCEED; -} - -const char* BraveWalletNavigationThrottle::GetNameForLogging() { - return "BraveWalletNavigationThrottle"; -} - -void BraveWalletNavigationThrottle::OnExtensionReady( - content::BrowserContext* browser_context, - const Extension* extension) { - if (resume_pending_ && - extension->id() == ethereum_remote_client_extension_id) { - // TODO(bbondy): For some reason the page won't load directly after install - // and on startups even though the Ready event has fired. - // There are no ExtensionRegistryObserver functions that get - // called after this. I even tried checking if the background - // process was available, and it was. - // Delaying 1 second for when the extension is not already - // loaded and ready makes this work reliabley for now. - // The bug without this only seems to surface itself in Release builds. - ScheduleBackgroundScriptTimer(); - } -} - -void BraveWalletNavigationThrottle::ScheduleBackgroundScriptTimer() { - constexpr base::TimeDelta kBackgroundScriptTimeout = - base::TimeDelta::FromSeconds(1); - timer_.Stop(); - timer_.Start(FROM_HERE, kBackgroundScriptTimeout, - base::BindOnce( - &BraveWalletNavigationThrottle::WalletBackgroundScriptTimer, - base::Unretained(this))); -} - -void BraveWalletNavigationThrottle::WalletBackgroundScriptTimer() { - ResumeThrottle(); -} - -void BraveWalletNavigationThrottle::ResumeThrottle() { - timer_.Stop(); - resume_pending_ = false; - Resume(); -} - -} // namespace extensions diff --git a/browser/extensions/brave_wallet_navigation_throttle.h b/browser/extensions/brave_wallet_navigation_throttle.h deleted file mode 100644 index d7de86f03149..000000000000 --- a/browser/extensions/brave_wallet_navigation_throttle.h +++ /dev/null @@ -1,50 +0,0 @@ -/* Copyright (c) 2019 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_EXTENSIONS_BRAVE_WALLET_NAVIGATION_THROTTLE_H_ -#define BRAVE_BROWSER_EXTENSIONS_BRAVE_WALLET_NAVIGATION_THROTTLE_H_ - -#include "base/macros.h" -#include "base/timer/timer.h" -#include "content/public/browser/navigation_throttle.h" -#include "extensions/browser/test_extension_registry_observer.h" - -namespace content { -class NavigationHandle; -} - -namespace extensions { - -// This class allows loads of brave://wallet to wait until -// ethereum-remote-client is installed. -class BraveWalletNavigationThrottle : public content::NavigationThrottle, - public ExtensionRegistryObserver { - public: - explicit BraveWalletNavigationThrottle( - content::NavigationHandle* navigation_handle); - ~BraveWalletNavigationThrottle() override; - - // content::NavigationThrottle implementation: - ThrottleCheckResult WillStartRequest() override; - const char* GetNameForLogging() override; - - private: - void WalletBackgroundScriptTimer(); - void ScheduleBackgroundScriptTimer(); - void ResumeThrottle(); - - // ExtensionRegistryObserver: - void OnExtensionReady(content::BrowserContext* browser_context, - const extensions::Extension* extension) override; - ScopedObserver - extension_registry_observer_; - bool resume_pending_; - base::OneShotTimer timer_; - DISALLOW_COPY_AND_ASSIGN(BraveWalletNavigationThrottle); -}; - -} // namespace extensions - -#endif // BRAVE_BROWSER_EXTENSIONS_BRAVE_WALLET_NAVIGATION_THROTTLE_H_ diff --git a/browser/extensions/brave_wallet_navigation_throttle_unittest.cc b/browser/extensions/brave_wallet_navigation_throttle_unittest.cc deleted file mode 100644 index 664679122c41..000000000000 --- a/browser/extensions/brave_wallet_navigation_throttle_unittest.cc +++ /dev/null @@ -1,228 +0,0 @@ -/* Copyright (c) 2019 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/extensions/brave_wallet_navigation_throttle.h" - -#include -#include -#include - -#include "base/environment.h" -#include "brave/browser/profiles/brave_profile_manager.h" -#include "brave/browser/profiles/profile_util.h" -#include "brave/browser/profiles/tor_unittest_profile_manager.h" -#include "brave/browser/tor/buildflags.h" -#include "brave/common/extensions/extension_constants.h" -#include "brave/common/pref_names.h" -#include "chrome/browser/prefs/browser_prefs.h" -#include "chrome/test/base/scoped_testing_local_state.h" -#include "chrome/test/base/testing_browser_process.h" -#include "chrome/test/base/testing_profile.h" -#include "components/prefs/testing_pref_service.h" -#include "components/sync_preferences/testing_pref_service_syncable.h" -#include "content/public/browser/content_browser_client.h" -#include "content/public/browser/navigation_handle.h" -#include "content/public/common/content_client.h" -#include "content/public/test/mock_navigation_handle.h" -#include "content/public/test/test_renderer_host.h" -#include "content/public/test/web_contents_tester.h" -#include "extensions/browser/extension_registry.h" -#include "extensions/common/extension.h" -#include "extensions/common/extension_builder.h" -#include "testing/gtest/include/gtest/gtest.h" -#include "url/gurl.h" - -#if BUILDFLAG(ENABLE_TOR) -#include "brave/common/tor/pref_names.h" -#endif - -using content::NavigationThrottle; - -namespace extensions { - -namespace { - -class MockBrowserClient : public content::ContentBrowserClient { - public: - MockBrowserClient() {} - ~MockBrowserClient() override {} - - // Only construct an BraveWalletNavigationThrottle so that we can test it in - // isolation. - std::vector> CreateThrottlesForNavigation( - content::NavigationHandle* handle) override { - std::vector> throttles; - throttles.push_back( - std::make_unique(handle)); - return throttles; - } -}; - -} // namespace - -class BraveWalletNavigationThrottleUnitTest - : public content::RenderViewHostTestHarness { - public: - BraveWalletNavigationThrottleUnitTest() - : local_state_(TestingBrowserProcess::GetGlobal()) { - } - - void SetUp() override { -#if BUILDFLAG(ENABLE_TOR) - ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); - ProfileManager* profile_manager - = new TorUnittestProfileManager(temp_dir_.GetPath()); - TestingBrowserProcess::GetGlobal()->SetProfileManager(profile_manager); - profile_manager->InitProfileUserPrefs( - ProfileManager::GetActiveUserProfile()); - ASSERT_TRUE(profile_manager); -#endif - original_client_ = content::SetBrowserClientForTesting(&client_); - content::RenderViewHostTestHarness::SetUp(); - - // For debug builds, set a fake BRAVE_INFURA_PROJECT_ID env var - std::unique_ptr env(base::Environment::Create()); - if (!env->HasVar("BRAVE_INFURA_PROJECT_ID")) { - env->SetVar("BRAVE_INFURA_PROJECT_ID", "test_project_id"); - } - } - - std::unique_ptr CreateBrowserContext() override { - TestingProfile::Builder builder; - auto prefs = - std::make_unique(); - RegisterUserProfilePrefs(prefs->registry()); - builder.SetPrefService(std::move(prefs)); - return builder.Build(); - } - - void TearDown() override { -#if BUILDFLAG(ENABLE_TOR) - TestingBrowserProcess::GetGlobal()->SetProfileManager(nullptr); -#endif - content::SetBrowserClientForTesting(original_client_); - content::RenderViewHostTestHarness::TearDown(); - } - - TestingProfile* profile() { - return static_cast(browser_context()); - } - - content::RenderFrameHostTester* render_frame_host_tester( - content::RenderFrameHost* host) { - return content::RenderFrameHostTester::For(host); - } - - content::WebContentsTester* web_contents_tester() { - return content::WebContentsTester::For(web_contents()); - } - - void AddExtension() { - DictionaryBuilder manifest; - manifest.Set("name", "ext") - .Set("version", "0.1") - .Set("manifest_version", 2); - extension_ = ExtensionBuilder() - .SetManifest(manifest.Build()) - .SetID(ethereum_remote_client_extension_id) - .Build(); - ASSERT_TRUE(extension_); - ExtensionRegistry::Get(browser_context())->AddReady(extension_.get()); - } - - private: - scoped_refptr extension_; - MockBrowserClient client_; - content::ContentBrowserClient* original_client_; - ScopedTestingLocalState local_state_; - base::ScopedTempDir temp_dir_; - sync_preferences::TestingPrefServiceSyncable prefs_; - DISALLOW_COPY_AND_ASSIGN(BraveWalletNavigationThrottleUnitTest); -}; - -// Tests the basic case of loading a URL, it should proceed. -TEST_F(BraveWalletNavigationThrottleUnitTest, ExternalWebPage) { - web_contents_tester()->NavigateAndCommit(GURL("http://example.com")); - content::RenderFrameHost* host = - render_frame_host_tester(main_rfh())->AppendChild("child"); - GURL url("http://www.example.com"); - content::MockNavigationHandle test_handle(url, host); - test_handle.set_starting_site_instance(host->GetSiteInstance()); - auto throttle = std::make_unique(&test_handle); - EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillStartRequest().action()) - << url; -} - -// Tests the case of loading brave://settings without the extension installed. -// It should just proceed. -TEST_F(BraveWalletNavigationThrottleUnitTest, DifferentChromePageWithExt) { - web_contents_tester()->NavigateAndCommit(GURL("http://example.com")); - content::RenderFrameHost* host = - render_frame_host_tester(main_rfh())->AppendChild("child"); - GURL url("chrome://settings"); - content::MockNavigationHandle test_handle(url, host); - test_handle.set_starting_site_instance(host->GetSiteInstance()); - auto throttle = std::make_unique(&test_handle); - EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillStartRequest().action()) - << url; -} - -// Tests the case of loading brave://wallet without having the extension -// installed.a It should defer which it does to install the extension. -TEST_F(BraveWalletNavigationThrottleUnitTest, ChromeWalletUrlNotInstalled) { - web_contents_tester()->NavigateAndCommit(GURL("http://example.com")); - content::RenderFrameHost* host = - render_frame_host_tester(main_rfh())->AppendChild("child"); - GURL url("chrome://wallet"); - content::MockNavigationHandle test_handle(url, host); - test_handle.set_starting_site_instance(host->GetSiteInstance()); - auto throttle = std::make_unique(&test_handle); - EXPECT_EQ(NavigationThrottle::DEFER, throttle->WillStartRequest().action()) - << url; -} - -// Tests the case of loading brave://wallet with the extension installed. -// It should just proceed. -TEST_F(BraveWalletNavigationThrottleUnitTest, ChromeWalletUrlInstalled) { - AddExtension(); - web_contents_tester()->NavigateAndCommit(GURL("http://example.com")); - content::RenderFrameHost* host = - render_frame_host_tester(main_rfh())->AppendChild("child"); - GURL url("chrome://wallet"); - content::MockNavigationHandle test_handle(url, host); - test_handle.set_starting_site_instance(host->GetSiteInstance()); - auto throttle = std::make_unique(&test_handle); - EXPECT_EQ(NavigationThrottle::PROCEED, throttle->WillStartRequest().action()) - << url; -} - -#if BUILDFLAG(ENABLE_TOR) -// Make sure Brave Wallet is not available in a Tor profile. -TEST_F(BraveWalletNavigationThrottleUnitTest, - ChromeWalletNotAvailInTorProfile) { - ProfileManager* profile_manager = g_browser_process->profile_manager(); - Profile* profile = ProfileManager::GetLastUsedProfile(); - Profile* tor_profile = profile_manager->GetProfile( - BraveProfileManager::GetTorProfilePath()); - ASSERT_EQ(brave::GetParentProfile(tor_profile), profile); - std::unique_ptr tor_web_contents = - content::WebContentsTester::CreateTestWebContents(tor_profile, nullptr); - - content::WebContentsTester::For(tor_web_contents.get())-> - NavigateAndCommit(GURL("http://example.com")); - content::RenderFrameHost* host = - render_frame_host_tester(tor_web_contents->GetMainFrame())-> - AppendChild("child"); - GURL url("chrome://wallet"); - content::MockNavigationHandle test_handle(url, host); - test_handle.set_starting_site_instance(host->GetSiteInstance()); - auto throttle = std::make_unique(&test_handle); - EXPECT_EQ(NavigationThrottle::BLOCK_REQUEST, - throttle->WillStartRequest().action()) << url; - tor_web_contents.reset(); -} -#endif - -} // namespace extensions diff --git a/browser/infobars/crypto_wallets_infobar_delegate.cc b/browser/infobars/crypto_wallets_infobar_delegate.cc index 16854bf48701..60433c48d02b 100644 --- a/browser/infobars/crypto_wallets_infobar_delegate.cc +++ b/browser/infobars/crypto_wallets_infobar_delegate.cc @@ -8,15 +8,18 @@ #include #include +#include "brave/browser/brave_wallet/brave_wallet_service_factory.h" #include "brave/browser/ui/brave_pages.h" #include "brave/common/brave_wallet_constants.h" #include "brave/common/extensions/extension_constants.h" #include "brave/common/pref_names.h" #include "brave/common/url_constants.h" +#include "brave/components/brave_wallet/browser/brave_wallet_service.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/browser/infobars/infobar_service.h" #include "chrome/browser/ui/browser_finder.h" #include "chrome/grit/chromium_strings.h" +#include "chrome/browser/profiles/profile.h" #include "components/infobars/core/infobar.h" #include "components/prefs/pref_service.h" #include "components/strings/grit/components_strings.h" @@ -25,16 +28,18 @@ #include "ui/base/l10n/l10n_util.h" #include "ui/views/vector_icons.h" + // static void CryptoWalletsInfoBarDelegate::Create(InfoBarService* infobar_service, - bool metamask_installed) { + CryptoWalletsInfoBarDelegate::InfobarSubType subtype) { infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar( std::unique_ptr( - new CryptoWalletsInfoBarDelegate(metamask_installed)))); + new CryptoWalletsInfoBarDelegate(subtype)))); } CryptoWalletsInfoBarDelegate::CryptoWalletsInfoBarDelegate( - bool metamask_installed) : metamask_installed_(metamask_installed) { + CryptoWalletsInfoBarDelegate::InfobarSubType subtype) : + subtype_(subtype) { } CryptoWalletsInfoBarDelegate::~CryptoWalletsInfoBarDelegate() {} @@ -52,7 +57,10 @@ void CryptoWalletsInfoBarDelegate::InfoBarDismissed() { } base::string16 CryptoWalletsInfoBarDelegate::GetMessageText() const { - if (metamask_installed_) { + if (subtype_ == InfobarSubType::LOAD_CRYPTO_WALLETS) { + return l10n_util::GetStringUTF16(IDS_BRAVE_CRYPTO_WALLETS_LAZY_LOAD_TEXT); + } + if (subtype_ == InfobarSubType::CRYPTO_WALLETS_METAMASK) { return l10n_util::GetStringUTF16( IDS_BRAVE_CRYPTO_WALLETS_METAMASK_INFOBAR_TEXT); } @@ -60,7 +68,8 @@ base::string16 CryptoWalletsInfoBarDelegate::GetMessageText() const { } int CryptoWalletsInfoBarDelegate::GetButtons() const { - if (metamask_installed_) { + if (subtype_ == InfobarSubType::LOAD_CRYPTO_WALLETS || + subtype_ == InfobarSubType::CRYPTO_WALLETS_METAMASK) { return BUTTON_OK | BUTTON_CANCEL; } return BUTTON_OK; @@ -68,12 +77,20 @@ int CryptoWalletsInfoBarDelegate::GetButtons() const { base::string16 CryptoWalletsInfoBarDelegate::GetButtonLabel( InfoBarButton button) const { + if (subtype_ == InfobarSubType::LOAD_CRYPTO_WALLETS) { + if (button == BUTTON_CANCEL) { + return l10n_util::GetStringUTF16(IDS_BRAVE_CRYPTO_WALLETS_SETTINGS); + } + return l10n_util::GetStringUTF16( + IDS_BRAVE_CRYPTO_WALLETS_START_AND_RELOAD); + } + if (button == BUTTON_CANCEL) { return l10n_util::GetStringUTF16(IDS_BRAVE_CRYPTO_WALLETS_USE_METAMASK); } - return metamask_installed_ ? - l10n_util::GetStringUTF16(IDS_BRAVE_CRYPTO_WALLETS_USE_CRYPTO_WALLETS) : + return subtype_ == InfobarSubType::CRYPTO_WALLETS_METAMASK ? + l10n_util::GetStringUTF16(IDS_BRAVE_CRYPTO_WALLETS_SETUP_CRYPTO_WALLETS) : l10n_util::GetStringUTF16(IDS_BRAVE_CRYPTO_WALLETS_SETUP); } @@ -86,6 +103,19 @@ GURL CryptoWalletsInfoBarDelegate::GetLinkURL() const { } bool CryptoWalletsInfoBarDelegate::Accept() { + if (subtype_ == InfobarSubType::LOAD_CRYPTO_WALLETS) { + content::WebContents* web_contents = + InfoBarService::WebContentsFromInfoBar(infobar()); + if (web_contents) { + auto* browser_context = web_contents->GetBrowserContext(); + Profile* profile = Profile::FromBrowserContext(browser_context); + auto* service = BraveWalletServiceFactory::GetForProfile(profile); + service->LoadCryptoWalletsExtension(base::BindOnce( + &CryptoWalletsInfoBarDelegate::OnCryptoWalletsLoaded, + base::Unretained(this), web_contents)); + } + return true; + } if (infobar() && infobar()->owner()) { content::WebContents* web_contents = InfoBarService::WebContentsFromInfoBar(infobar()); @@ -102,6 +132,16 @@ bool CryptoWalletsInfoBarDelegate::Accept() { } bool CryptoWalletsInfoBarDelegate::Cancel() { + if (subtype_ == InfobarSubType::LOAD_CRYPTO_WALLETS) { + content::WebContents* web_contents = + InfoBarService::WebContentsFromInfoBar(infobar()); + if (web_contents) { + Browser* browser = chrome::FindBrowserWithWebContents(web_contents); + brave::ShowExtensionSettings(browser); + } + return true; + } + content::WebContents* web_contents = InfoBarService::WebContentsFromInfoBar(infobar()); if (web_contents) { @@ -111,3 +151,8 @@ bool CryptoWalletsInfoBarDelegate::Cancel() { } return true; } + +void CryptoWalletsInfoBarDelegate::OnCryptoWalletsLoaded( + content::WebContents* web_contents) { + web_contents->GetController().Reload(content::ReloadType::NORMAL, true); +} diff --git a/browser/infobars/crypto_wallets_infobar_delegate.h b/browser/infobars/crypto_wallets_infobar_delegate.h index db79e053a6c2..eb3eeca27a4f 100644 --- a/browser/infobars/crypto_wallets_infobar_delegate.h +++ b/browser/infobars/crypto_wallets_infobar_delegate.h @@ -15,15 +15,26 @@ class InfoBarService; class PrefService; +namespace content { +class WebContents; +} + // An infobar that is run with a string, buttons, and a "Learn More" link. class CryptoWalletsInfoBarDelegate : public ConfirmInfoBarDelegate { public: - static void Create(InfoBarService* infobar_service, bool metamask_installed); + enum class InfobarSubType { + LOAD_CRYPTO_WALLETS, + GENERIC_SETUP, + CRYPTO_WALLETS_METAMASK + }; + static void Create(InfoBarService* infobar_service, InfobarSubType subtype); private: - explicit CryptoWalletsInfoBarDelegate(bool metamask_installed); + explicit CryptoWalletsInfoBarDelegate(InfobarSubType subtype); ~CryptoWalletsInfoBarDelegate() override; + void OnCryptoWalletsLoaded(content::WebContents*); + bool ShouldShowLazyLoadInfobar(); infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override; const gfx::VectorIcon& GetVectorIcon() const override; void InfoBarDismissed() override; @@ -35,7 +46,7 @@ class CryptoWalletsInfoBarDelegate : public ConfirmInfoBarDelegate { bool Accept() override; bool Cancel() override; - bool metamask_installed_; + InfobarSubType subtype_; DISALLOW_COPY_AND_ASSIGN(CryptoWalletsInfoBarDelegate); }; diff --git a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html index 27fb9b323385..c5ab2b4b1722 100644 --- a/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html +++ b/browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.html @@ -73,6 +73,10 @@ menu-options="[[braveWeb3Providers_]]"> + + HasPrefPath(kBraveWalletAES256GCMSivNonce) && + prefs->HasPrefPath(kBraveWalletEncryptedSeed); +} + +bool BraveWalletService::IsCryptoWalletsReady() const { + auto* registry = extensions::ExtensionRegistry::Get(context_); + return registry->ready_extensions().Contains( + ethereum_remote_client_extension_id); +} + +bool BraveWalletService::ShouldShowLazyLoadInfobar() const { + Profile* profile = Profile::FromBrowserContext(context_); + auto provider = static_cast( + profile->GetPrefs()->GetInteger(kBraveWalletWeb3Provider)); + return provider == BraveWalletWeb3ProviderTypes::CRYPTO_WALLETS && + !IsCryptoWalletsReady(); +} + +void BraveWalletService::LoadCryptoWalletsExtension(LoadUICallback callback) { + load_ui_callback_ = std::move(callback); + extensions::ExtensionService* service = + extensions::ExtensionSystem::Get(context_)->extension_service(); + if (service) { + extensions::ComponentLoader* loader = service->component_loader(); + static_cast(loader)-> + AddEthereumRemoteClientExtension(); + } +} + +void BraveWalletService::CryptoWalletsExtensionReady() { + if (load_ui_callback_) { + std::move(load_ui_callback_).Run(); + } +} diff --git a/components/brave_wallet/browser/brave_wallet_service.h b/components/brave_wallet/browser/brave_wallet_service.h index 7a05763a0414..09fec1891ab2 100644 --- a/components/brave_wallet/browser/brave_wallet_service.h +++ b/components/brave_wallet/browser/brave_wallet_service.h @@ -12,7 +12,7 @@ #include #include -#include "base/callback_forward.h" +#include "base/callback.h" #include "base/containers/queue.h" #include "base/files/file_path.h" #include "base/macros.h" @@ -43,10 +43,16 @@ class BraveWalletService : public KeyedService, public: explicit BraveWalletService(content::BrowserContext* context); ~BraveWalletService() override; + using LoadUICallback = base::OnceCallback; void ResetCryptoWallets(); std::string GetWalletSeed(std::vector key); std::string GetBitGoSeed(std::vector key); + bool IsCryptoWalletsSetup() const; + bool IsCryptoWalletsReady() const; + bool ShouldShowLazyLoadInfobar() const; + void LoadCryptoWalletsExtension(LoadUICallback callback); + void CryptoWalletsExtensionReady(); static std::string GetEthereumRemoteClientSeedFromRootSeed( const std::string& seed); @@ -78,6 +84,7 @@ class BraveWalletService : public KeyedService, ScopedObserver extension_registry_observer_{this}; scoped_refptr file_task_runner_; + LoadUICallback load_ui_callback_; base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(BraveWalletService); }; diff --git a/components/brave_wallet_ui/brave_wallet.html b/components/brave_wallet_ui/brave_wallet.html index 2cb68f8712c9..71c47b0df2bd 100644 --- a/components/brave_wallet_ui/brave_wallet.html +++ b/components/brave_wallet_ui/brave_wallet.html @@ -1,15 +1,27 @@ - - - - - - - - - - + + + + Crypto Wallets + + + + + + + + + + + + + +
+ diff --git a/components/brave_wallet_ui/brave_wallet.tsx b/components/brave_wallet_ui/brave_wallet.tsx index 6809958ffd0f..fd3efb89def4 100644 --- a/components/brave_wallet_ui/brave_wallet.tsx +++ b/components/brave_wallet_ui/brave_wallet.tsx @@ -1 +1,59 @@ -window.location.href = 'chrome://wallet' +// 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/. + +import * as React from 'react' +import { render } from 'react-dom' +import { initLocale } from 'brave-ui' + +import 'emptykit.css' + +// Fonts +import '../../ui/webui/resources/fonts/poppins.css' +import '../../ui/webui/resources/fonts/muli.css' + +// Components +import App from './components/app' +import Theme from 'brave-ui/theme/brave-default' +import DarkTheme from 'brave-ui/theme/brave-dark' +import BraveCoreThemeProvider from '../common/BraveCoreThemeProvider' + +function initialize () { + chrome.braveWallet.shouldPromptForSetup((prompt: boolean) => { + if (!prompt) { + chrome.braveWallet.loadUI(() => { + window.location.href = 'chrome://wallet' + }) + return + } + + renderWebUIView() + }) +} + +function renderWebUIView () { + new Promise(resolve => chrome.braveTheme.getBraveThemeType(resolve)) + .then((themeType: chrome.braveTheme.ThemeType) => { + window.i18nTemplate.process(window.document, window.loadTimeData) + if (window.loadTimeData && window.loadTimeData.data_) { + initLocale(window.loadTimeData.data_) + } + + render( + + + , + document.getElementById('root') + ) + }) + .catch(({ message }) => { + console.error(`Could not mount brave wallet: ${message}`) + }) +} + +document.addEventListener('DOMContentLoaded', initialize) diff --git a/components/brave_wallet_ui/components/app.tsx b/components/brave_wallet_ui/components/app.tsx new file mode 100644 index 000000000000..0f0f4a5a31b4 --- /dev/null +++ b/components/brave_wallet_ui/components/app.tsx @@ -0,0 +1,22 @@ +// 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/. + +import * as React from 'react' +import OptIn from './opt-in' + +export default class App extends React.PureComponent<{}, {}> { + + onWalletOptin = () => { + chrome.braveWallet.loadUI(() => { + window.location.href = 'chrome://wallet' + }) + } + + render () { + return ( + + ) + } +} diff --git a/components/brave_wallet_ui/components/opt-in/assets/crypto-banner.tsx b/components/brave_wallet_ui/components/opt-in/assets/crypto-banner.tsx new file mode 100644 index 000000000000..865dc8c4794c --- /dev/null +++ b/components/brave_wallet_ui/components/opt-in/assets/crypto-banner.tsx @@ -0,0 +1,41 @@ +// 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/. + +import * as React from 'react' + +export default class CryptoBanner extends React.PureComponent<{}, {}> { + + render () { + return ( + + + + + + + + + + + + + + + + + + + + + + + + + + + + ) + } +} diff --git a/components/brave_wallet_ui/components/opt-in/index.tsx b/components/brave_wallet_ui/components/opt-in/index.tsx new file mode 100644 index 000000000000..7df4ab59a868 --- /dev/null +++ b/components/brave_wallet_ui/components/opt-in/index.tsx @@ -0,0 +1,91 @@ +// 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/. + +import * as React from 'react' + +// Components +import CryptoBanner from './assets/crypto-banner' +import { LoaderIcon } from 'brave-ui/components/icons' +import { + StyledWrapper, + StyledInner, + StyledContent, + StyledHeader, + StyledSeparator, + StyledDisclosure, + StyledText, + StyledRewards, + StyledButtonWrapper, + StyledButton, + StyledLoader +} from './style' + +// Utils +import { getLocale } from '../../../common/locale' + +interface Props { + onWalletOptIn: () => void +} + +interface State { + isLoading: boolean +} + +export default class OptIn extends React.PureComponent { + + constructor (props: Props) { + super(props) + this.state = { + isLoading: false + } + } + + openRewards = () => { + window.open('chrome://rewards', '_blank') + } + + onWalletOptIn = () => { + this.setState({ isLoading: true }) + this.props.onWalletOptIn() + } + + render () { + return ( + + + + + + {getLocale('cryptoWalletsWelcome')} + + + + + {getLocale('cryptoWalletsDisclosureOne')} + + + {getLocale('cryptoWalletsDisclosureTwo')} + + + {getLocale('cryptoWalletsDisclosureThree')} {getLocale('cryptoWalletsBraveRewards')}. {getLocale('cryptoWalletsDisclosureFour')} + + + + + + { + this.state.isLoading + ? + + + : getLocale('cryptoWalletsDisclosureConfirm') + } + + + + + ) + } +} diff --git a/components/brave_wallet_ui/components/opt-in/style.ts b/components/brave_wallet_ui/components/opt-in/style.ts new file mode 100644 index 000000000000..d4d0edfa5ba8 --- /dev/null +++ b/components/brave_wallet_ui/components/opt-in/style.ts @@ -0,0 +1,84 @@ +// 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/. + +import styled from 'styled-components' + +const isDarkTheme = (p: any) => { + return p.theme.name === 'Brave Dark' +} + +export const StyledWrapper = styled<{}, 'div'>('div')` + width: 100%; + color: ${p => isDarkTheme(p) ? '#fff' : '#000'}; + background: ${p => isDarkTheme(p) ? '#1d2025' : '#fff'}; +` + +export const StyledInner = styled<{}, 'div'>('div')` + margin: 75px auto; + background: inherit; + border-radius: 6px; + overflow: hidden; + padding: 20px 0px; + max-width: 620px; +` + +export const StyledContent = styled<{}, 'div'>('div')` + max-width: 90%; + margin: 0 auto; + text-align: center; +` + +export const StyledHeader = styled<{}, 'span'>('span')` + font-weight: bold; + font-size: 21px; +` + +export const StyledSeparator = styled<{}, 'div'>('div')` + background: #d3d3d3; + height: 1px; + margin-top: 10px; +` + +export const StyledDisclosure = styled<{}, 'div'>('div')` + font-size: 14px; + margin-top: 15px; + display: block; + text-align: left; +` + +export const StyledText = styled<{}, 'span'>('span')` + margin-top: 10px; + display: block; +` + +export const StyledRewards = styled<{}, 'span'>('span')` + color: #0076ff; + font-size: 14px; + cursor: pointer; +` + +export const StyledButtonWrapper = styled<{}, 'div'>('div')` + width: 100%; + margin-top: 50px; + text-align: center; +` + +export const StyledButton = styled<{}, 'button'>('button')` + color: #fff; + background: #0076ff; + border-radius: 20px; + height: 40px; + width: 184px; + font-size: 14px; + cursor: pointer; + border: none; +` + +export const StyledLoader = styled<{}, 'div'>('div')` + display: inline-block; + width: 20px; + height: 20px; + vertical-align: bottom; +` diff --git a/components/definitions/chromel.d.ts b/components/definitions/chromel.d.ts index c8b8c4751aad..001180e7da6b 100644 --- a/components/definitions/chromel.d.ts +++ b/components/definitions/chromel.d.ts @@ -249,8 +249,10 @@ declare namespace chrome.braveShields { declare namespace chrome.braveWallet { const promptToEnableWallet: (tabId: number | undefined) => void + const ready: () => void const shouldCheckForDapps: (callback: (dappDetection: boolean) => void) => void - const isInstalled: (callback: (enabled: boolean) => void) => void + const shouldPromptForSetup: (callback: (dappDetection: boolean) => void) => void + const loadUI: (callback: () => void) => void } declare namespace chrome.test { diff --git a/components/resources/brave_components_strings.grd b/components/resources/brave_components_strings.grd index 7834577d0e9a..6e8eb08291a3 100644 --- a/components/resources/brave_components_strings.grd +++ b/components/resources/brave_components_strings.grd @@ -896,6 +896,14 @@ Your computer's clock may be set to the wrong time or time zone. Check your clock settings. Crypto Wallets + + Welcome to Brave Crypto Wallets + Crypto Wallets lets you work with cryptocurrency and related assets in Brave. Crypto Wallets supports Ethereum tokens like ETH and BAT, as well as collectibles, and lets you work with Ðapps and other smart contracts. All this is meant for people who are familiar with Ethereum and other blockchain systems — or are excited to learn. + Only use Crypto Wallets if you accept that you alone are responsible for all risk associated with this use, and that Brave Software bears no responsibility or liability for any losses including incidental or consequential losses you might suffer as a result of this use. + All of this is completely separate from + So if you were looking to support your favorite sites, head over there instead. + Brave Rewards + I understand diff --git a/test/BUILD.gn b/test/BUILD.gn index 1840324ed11a..a1daee722e29 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -324,7 +324,6 @@ test("brave_unit_tests") { if (brave_wallet_enabled) { sources += [ - "//brave/browser/extensions/brave_wallet_navigation_throttle_unittest.cc", "//brave/browser/extensions/brave_wallet_unittest.cc", "//brave/browser/brave_wallet/brave_wallet_utils_unittest.cc", ] diff --git a/test/data/extensions/api_test/braveWallet/background.js b/test/data/extensions/api_test/braveWallet/background.js index 107961bd184e..ca1da72b94b6 100644 --- a/test/data/extensions/api_test/braveWallet/background.js +++ b/test/data/extensions/api_test/braveWallet/background.js @@ -136,7 +136,9 @@ function testProviderIsNone() { function testBasics() { chrome.test.runTests([ function braveWalletExtensionHasAccess() { - if (chrome.braveWallet && chrome.braveWallet.isInstalled && + if (chrome.braveWallet && + chrome.braveWallet.shouldPromptForSetup && + chrome.braveWallet.loadUI && chrome.braveWallet.promptToEnableWallet) { chrome.test.succeed(); } else { diff --git a/ui/webui/resources/br_elements/br_toolbar/br_toolbar.html b/ui/webui/resources/br_elements/br_toolbar/br_toolbar.html index 126e646a4504..4ec6006048b0 100644 --- a/ui/webui/resources/br_elements/br_toolbar/br_toolbar.html +++ b/ui/webui/resources/br_elements/br_toolbar/br_toolbar.html @@ -166,7 +166,7 @@