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

Disable Brave Wallet by policy #15252

Merged
merged 7 commits into from
Sep 30, 2022
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
30 changes: 30 additions & 0 deletions browser/brave_wallet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ source_set("brave_wallet") {
"//brave/components/brave_wallet/browser",
"//brave/components/brave_wallet/browser:constants",
"//brave/components/brave_wallet/common",
"//brave/components/brave_wallet/common:common_utils",
"//brave/components/brave_wallet/common:mojom",
"//chrome/browser:browser_process",
"//chrome/browser/profiles:profiles",
Expand Down Expand Up @@ -192,3 +193,32 @@ source_set("unit_tests") {
}
}
}

source_set("browser_tests") {
testonly = true
if (!is_android) {
sources = [ "brave_wallet_policy_browsertest.cc" ]

deps = [
":brave_wallet",
"//base/test:test_support",
"//brave/app:command_ids",
"//brave/browser",
"//brave/browser/ui",
"//brave/browser/ui/sidebar",
"//brave/components/brave_wallet/common:common_utils",
"//brave/components/brave_wallet/common:pref_names",
"//brave/components/sidebar",
"//chrome/test:test_support",
"//chrome/test:test_support_ui",
"//components/policy/core/browser",
"//components/policy/core/common:test_support",
"//components/prefs:prefs",
"//components/user_prefs:user_prefs",
"//content/test:test_support",
"//testing/gtest",
"//url",
]
defines = [ "HAS_OUT_OF_PROC_TEST_RUNNER" ]
}
} # source_set("browser_tests")
6 changes: 5 additions & 1 deletion browser/brave_wallet/brave_wallet_context_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/components/brave_wallet/common/common_util.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"

namespace brave_wallet {

bool IsAllowedForContext(content::BrowserContext* context) {
if (context && !brave::IsRegularProfile(context))
if (context && (!brave::IsRegularProfile(context) ||
!IsAllowed(user_prefs::UserPrefs::Get(context)))) {
return false;
}

return true;
}
Expand Down
153 changes: 153 additions & 0 deletions browser/brave_wallet/brave_wallet_policy_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/* Copyright (c) 2022 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 "base/strings/stringprintf.h"
#include "brave/app/brave_command_ids.h"
#include "brave/browser/brave_wallet/brave_wallet_context_utils.h"
#include "brave/browser/brave_wallet/brave_wallet_service_factory.h"
#include "brave/browser/ui/brave_browser.h"
#include "brave/browser/ui/sidebar/sidebar_controller.h"
#include "brave/browser/ui/sidebar/sidebar_model.h"
#include "brave/components/brave_wallet/common/common_util.h"
#include "brave/components/brave_wallet/common/pref_names.h"
#include "brave/components/sidebar/sidebar_item.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/navigation_handle_observer.h"
#include "extensions/common/constants.h"
#include "url/gurl.h"

namespace policy {

class BraveWalletPolicyTest : public InProcessBrowserTest,
public ::testing::WithParamInterface<bool> {
public:
BraveWalletPolicyTest() = default;
~BraveWalletPolicyTest() override = default;

void SetUpInProcessBrowserTestFixture() override {
EXPECT_CALL(provider_, IsInitializationComplete(testing::_))
.WillRepeatedly(testing::Return(true));
BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
PolicyMap policies;
policies.Set(key::kBraveWalletDisabled, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_PLATFORM,
base::Value(IsBraveWalletDisabledTest()), nullptr);
provider_.UpdateChromePolicy(policies);
}

bool IsBraveWalletDisabledTest() { return GetParam(); }

content::WebContents* web_contents() const {
return browser()->tab_strip_model()->GetActiveWebContents();
}

content::BrowserContext* browser_context() {
return web_contents()->GetBrowserContext();
}

Profile* profile() { return browser()->profile(); }

PrefService* prefs() { return user_prefs::UserPrefs::Get(browser_context()); }

private:
MockConfigurationPolicyProvider provider_;
};

// Verify that brave_wallet::IsDisabledByPolicy works correctly based on the
// preference set by the policy.
IN_PROC_BROWSER_TEST_P(BraveWalletPolicyTest, IsBraveWalletDisabled) {
EXPECT_TRUE(prefs()->FindPreference(brave_wallet::prefs::kDisabledByPolicy));
if (IsBraveWalletDisabledTest()) {
EXPECT_TRUE(prefs()->GetBoolean(brave_wallet::prefs::kDisabledByPolicy));
EXPECT_FALSE(brave_wallet::IsAllowed(prefs()));
EXPECT_FALSE(brave_wallet::IsAllowedForContext(profile()));
} else {
EXPECT_FALSE(prefs()->GetBoolean(brave_wallet::prefs::kDisabledByPolicy));
EXPECT_TRUE(brave_wallet::IsAllowed(prefs()));
EXPECT_TRUE(brave_wallet::IsAllowedForContext(profile()));
}
}

// Verify that Wallet service doesn't get created when Brave Wallet is
// disabled by policy.
IN_PROC_BROWSER_TEST_P(BraveWalletPolicyTest, GetWalletService) {
if (IsBraveWalletDisabledTest()) {
EXPECT_EQ(brave_wallet::BraveWalletServiceFactory::GetServiceForContext(
profile()),
nullptr);
} else {
EXPECT_NE(brave_wallet::BraveWalletServiceFactory::GetServiceForContext(
profile()),
nullptr);
}
}

// Verify that Wallet menu item isn't enabled in the app menu when Brave
// Wallet is disabled by policy.
IN_PROC_BROWSER_TEST_P(BraveWalletPolicyTest, AppMenuItemDisabled) {
auto* command_controller = browser()->command_controller();
if (IsBraveWalletDisabledTest()) {
EXPECT_FALSE(command_controller->IsCommandEnabled(IDC_SHOW_BRAVE_WALLET));
} else {
EXPECT_TRUE(command_controller->IsCommandEnabled(IDC_SHOW_BRAVE_WALLET));
}
}

// Verify that brave://wallet page isn't reachable when Brave Wallet is
// disabled by policy.
IN_PROC_BROWSER_TEST_P(BraveWalletPolicyTest, WalletPageAccess) {
const GURL url("chrome://wallet");
auto* rfh = ui_test_utils::NavigateToURL(browser(), url);
EXPECT_TRUE(rfh);
EXPECT_EQ(IsBraveWalletDisabledTest(), rfh->IsErrorDocument());
}

// Verify that the wallet item is not shown in the sidebar when Brave Wallet is
// disabled by policy.
IN_PROC_BROWSER_TEST_P(BraveWalletPolicyTest, WalletInSidebar) {
BraveBrowser* brave_browser = static_cast<BraveBrowser*>(browser());
sidebar::SidebarController* controller = brave_browser->sidebar_controller();
sidebar::SidebarModel* model = controller->model();

const auto items = model->GetAllSidebarItems();
EXPECT_LT(0UL, items.size());

const auto iter = base::ranges::find_if(items, [](const auto& i) {
return (i.built_in_item_type ==
sidebar::SidebarItem::BuiltInItemType::kWallet);
});

if (IsBraveWalletDisabledTest()) {
EXPECT_TRUE(iter == items.end());
} else {
EXPECT_FALSE(iter == items.end());
}
}

INSTANTIATE_TEST_SUITE_P(
BraveWalletPolicyTest,
BraveWalletPolicyTest,
::testing::Bool(),
[](const testing::TestParamInfo<BraveWalletPolicyTest::ParamType>& info) {
return base::StringPrintf("BraveWallet_%sByPolicy",
info.param ? "Disabled" : "NotDisabled");
});

} // namespace policy
1 change: 1 addition & 0 deletions browser/extensions/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ source_set("extensions") {
"//brave/components/brave_shields/common",
"//brave/components/brave_vpn/buildflags",
"//brave/components/brave_wallet/browser:utils",
"//brave/components/brave_wallet/common:common_utils",
"//brave/components/brave_wayback_machine:buildflags",
"//brave/components/constants",
"//brave/components/de_amp/common",
Expand Down
4 changes: 3 additions & 1 deletion browser/extensions/api/binance_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "brave/browser/binance/binance_service_factory.h"
#include "brave/common/extensions/api/binance.h"
#include "brave/components/binance/browser/binance_service.h"
#include "brave/components/brave_wallet/common/common_util.h"
#include "brave/components/constants/pref_names.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/extension_tab_util.h"
Expand All @@ -32,7 +33,8 @@ BinanceService* GetBinanceService(content::BrowserContext* context) {

bool IsBinanceAPIAvailable(content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
return !profile->IsIncognitoProfile() && !profile->IsGuestSession();
return !profile->IsIncognitoProfile() && !profile->IsGuestSession() &&
brave_wallet::IsAllowed(profile->GetPrefs());
}

} // namespace
Expand Down
7 changes: 5 additions & 2 deletions browser/extensions/api/crypto_dot_com_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#include <string>
#include <utility>

#include "brave/browser/crypto_dot_com/crypto_dot_com_service_factory.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/common/extensions/api/crypto_dot_com.h"
#include "brave/browser/crypto_dot_com/crypto_dot_com_service_factory.h"
#include "brave/components/brave_wallet/common/common_util.h"
#include "brave/components/crypto_dot_com/browser/crypto_dot_com_service.h"
#include "brave/components/crypto_dot_com/browser/regions.h"
#include "brave/components/crypto_dot_com/common/pref_names.h"
Expand All @@ -31,7 +32,9 @@ CryptoDotComService* GetCryptoDotComService(content::BrowserContext* context) {
}

bool IsCryptoDotComAPIAvailable(content::BrowserContext* context) {
return brave::IsRegularProfile(context);
return brave::IsRegularProfile(context) &&
brave_wallet::IsAllowed(
Profile::FromBrowserContext(context)->GetPrefs());
}

} // namespace
Expand Down
7 changes: 5 additions & 2 deletions browser/extensions/api/ftx_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "brave/browser/ftx/ftx_service_factory.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/common/extensions/api/ftx.h"
#include "brave/components/brave_wallet/common/common_util.h"
#include "brave/components/ftx/browser/ftx_service.h"
#include "brave/components/ftx/browser/regions.h"
#include "brave/components/ftx/common/pref_names.h"
Expand Down Expand Up @@ -186,8 +187,10 @@ void FtxGetAccountBalancesFunction::OnGetAccountBalances(

ExtensionFunction::ResponseAction FtxIsSupportedFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
bool is_supported = ntp_widget_utils::IsRegionSupported(
profile->GetPrefs(), ::ftx::unsupported_regions, false);
bool is_supported =
ntp_widget_utils::IsRegionSupported(profile->GetPrefs(),
::ftx::unsupported_regions, false) &&
brave_wallet::IsAllowed(profile->GetPrefs());
return RespondNow(OneArgument(base::Value(is_supported)));
}

Expand Down
11 changes: 7 additions & 4 deletions browser/extensions/api/gemini_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
#include <utility>
#include <vector>

#include "brave/common/extensions/api/gemini.h"
#include "brave/browser/gemini/gemini_service_factory.h"
#include "brave/components/ntp_widget_utils/browser/ntp_widget_utils_region.h"
#include "brave/common/extensions/api/gemini.h"
#include "brave/components/brave_wallet/common/common_util.h"
#include "brave/components/gemini/browser/gemini_service.h"
#include "brave/components/gemini/browser/regions.h"
#include "brave/components/ntp_widget_utils/browser/ntp_widget_utils_region.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/profiles/profile.h"
Expand Down Expand Up @@ -233,8 +234,10 @@ void GeminiExecuteOrderFunction::OnOrderExecuted(bool success) {
ExtensionFunction::ResponseAction
GeminiIsSupportedFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
bool is_supported = ntp_widget_utils::IsRegionSupported(
profile->GetPrefs(), ::gemini::supported_regions, true);
bool is_supported =
ntp_widget_utils::IsRegionSupported(profile->GetPrefs(),
::gemini::supported_regions, true) &&
brave_wallet::IsAllowed(profile->GetPrefs());
return RespondNow(OneArgument(base::Value(is_supported)));
}

Expand Down
41 changes: 24 additions & 17 deletions browser/resources/settings/brave_overrides/basic_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,22 @@ RegisterPolymerTemplateModifications({
prefs: '{{prefs}}'
}
))
const sectionWallet = document.createElement('template')
sectionWallet.setAttribute('is', 'dom-if')
sectionWallet.setAttribute('restamp', true)
sectionWallet.setAttribute('if', '[[showPage_(pageVisibility.braveWallet)]]')
sectionWallet.content.appendChild(createSectionElement(
'wallet',
'braveWallet',
'settings-brave-wallet-page',
{
prefs: '{{prefs}}'
}
))
const isBraveWalletAllowed = loadTimeData.getBoolean('isBraveWalletAllowed')
let sectionWallet = undefined
if (isBraveWalletAllowed) {
sectionWallet = document.createElement('template')
sectionWallet.setAttribute('is', 'dom-if')
sectionWallet.setAttribute('restamp', true)
sectionWallet.setAttribute('if', '[[showPage_(pageVisibility.braveWallet)]]')
sectionWallet.content.appendChild(createSectionElement(
'wallet',
'braveWallet',
'settings-brave-wallet-page',
{
prefs: '{{prefs}}'
}
))
}
const sectionSync = document.createElement('template')
sectionSync.setAttribute('is', 'dom-if')
sectionSync.setAttribute('restamp', true)
Expand Down Expand Up @@ -277,12 +281,15 @@ RegisterPolymerTemplateModifications({
sectionSync.insertAdjacentElement('afterend', sectionSearch)
// Insert extensions
sectionSearch.insertAdjacentElement('afterend', sectionExtensions)
// Insert Wallet
sectionExtensions.insertAdjacentElement('afterend', sectionWallet)
// Insert IPFS
sectionWallet.insertAdjacentElement('afterend', sectionIPFS)
// Insert Wallet and IPFS
if (isBraveWalletAllowed) {
sectionExtensions.insertAdjacentElement('afterend', sectionWallet)
sectionWallet.insertAdjacentElement('afterend', sectionIPFS)
} else {
sectionExtensions.insertAdjacentElement('afterend', sectionIPFS)
}
// Insert Tor
simonhong marked this conversation as resolved.
Show resolved Hide resolved
sectionWallet.insertAdjacentElement('afterend', sectionTor)
sectionIPFS.insertAdjacentElement('afterend', sectionTor)
// Advanced
const advancedTemplate = templateContent.querySelector('template[if="[[showAdvancedSettings_(pageVisibility.advancedSettings)]]"]')
if (!advancedTemplate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function getPageVisibility () {
privacy: alwaysTrueProxy,
// custom properties
braveSync: !loadTimeData.getBoolean('isSyncDisabled'),
braveWallet: loadTimeData.getBoolean('isBraveWalletAllowed'),
}
// Proxy so we can respond to any other property
return new Proxy(staticProps, {
Expand Down
Loading