Skip to content

Commit

Permalink
Merge pull request #4046 from brave/add_options_for_disabling_tor
Browse files Browse the repository at this point in the history
Add setting option for disabling tor component
  • Loading branch information
simonhong authored Dec 4, 2019
2 parents ab933b3 + 3e01e1f commit b1d0576
Show file tree
Hide file tree
Showing 20 changed files with 321 additions and 101 deletions.
6 changes: 6 additions & 0 deletions app/brave_generated_resources.grd
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@
<message name="IDS_SETTINGS_ASK_WIDEVINE_INSTALL_DESC" desc="Text fragment for asking widevine install or not">
Ask when a site wants to install Widevine on your computer.
</message>
<message name="IDS_SETTINGS_ENABLE_TOR_TITLE" desc="Text fragment for enabling tor component">
Private Window with Tor
</message>
<message name="IDS_SETTINGS_ENABLE_TOR_DESC" desc="Text fragment for enabling tor component">
Tor hides your IP address from the sites you visit.
</message>
<if expr="is_linux">
<message name="IDS_WIDEVINE_PERMISSION_REQUEST_TEXT_FRAGMENT_INSTALL" desc="Text fragment for Widevine permission request. 'Widevine' is the name of a plugin and should not be translated.">
Install Widevine
Expand Down
24 changes: 24 additions & 0 deletions browser/brave_browser_process_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "brave/browser/component_updater/brave_component_updater_delegate.h"
#include "brave/browser/profiles/brave_profile_manager.h"
#include "brave/browser/tor/buildflags.h"
#include "brave/browser/ui/brave_browser_command_controller.h"
#include "brave/components/brave_component_updater/browser/local_data_files_service.h"
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h"
Expand All @@ -27,6 +28,7 @@
#include "brave/components/p3a/buildflags.h"
#include "brave/components/p3a/brave_histogram_rewrite.h"
#include "brave/components/p3a/brave_p3a_service.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/chrome_paths.h"
#include "components/component_updater/component_updater_service.h"
Expand Down Expand Up @@ -57,11 +59,14 @@

#if BUILDFLAG(ENABLE_TOR)
#include "brave/browser/extensions/brave_tor_client_updater.h"
#include "brave/common/tor/pref_names.h"
#endif

#if defined(OS_ANDROID)
#include "chrome/browser/android/chrome_feature_list.h"
#include "chrome/browser/android/component_updater/background_task_update_scheduler.h"
#else
#include "chrome/browser/ui/browser.h"
#endif

BraveBrowserProcessImpl* g_brave_browser_process = nullptr;
Expand Down Expand Up @@ -102,6 +107,17 @@ BraveBrowserProcessImpl::BraveBrowserProcessImpl(StartupData* startup_data)
#endif // BUILDFLAG(BRAVE_P3A_ENABLED)
}

void BraveBrowserProcessImpl::Init() {
BrowserProcessImpl::Init();

#if BUILDFLAG(ENABLE_TOR)
pref_change_registrar_.Add(
tor::prefs::kTorDisabled,
base::Bind(&BraveBrowserProcessImpl::OnTorEnabledChanged,
base::Unretained(this)));
#endif
}

brave_component_updater::BraveComponent::Delegate*
BraveBrowserProcessImpl::brave_component_updater_delegate() {
if (!brave_component_updater_delegate_)
Expand Down Expand Up @@ -246,6 +262,14 @@ BraveBrowserProcessImpl::tor_client_updater() {
brave_component_updater_delegate());
return tor_client_updater_.get();
}

void BraveBrowserProcessImpl::OnTorEnabledChanged() {
// Update all browsers' tor command status.
for (Browser* browser : *BrowserList::GetInstance()) {
static_cast<chrome::BraveBrowserCommandController*>(
browser->command_controller())->UpdateCommandForTor();
}
}
#endif

brave::BraveP3AService* BraveBrowserProcessImpl::brave_p3a_service() {
Expand Down
7 changes: 7 additions & 0 deletions browser/brave_browser_process_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,16 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
brave::BraveStatsUpdater* brave_stats_updater();

private:
// BrowserProcessImpl overrides:
void Init() override;

void CreateProfileManager();
void CreateNotificationPlatformBridge();

#if BUILDFLAG(ENABLE_TOR)
void OnTorEnabledChanged();
#endif

BraveComponent::Delegate* brave_component_updater_delegate();

// local_data_files_service_ should always be first because it needs
Expand Down
21 changes: 21 additions & 0 deletions browser/brave_local_state_browsertest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* 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/tor/buildflags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/test/base/in_process_browser_test.h"

#if BUILDFLAG(ENABLE_TOR)
#include "brave/browser/tor/tor_profile_service.h"
#endif

using BraveLocalStateBrowserTest = InProcessBrowserTest;

IN_PROC_BROWSER_TEST_F(BraveLocalStateBrowserTest, BasicTest) {
#if BUILDFLAG(ENABLE_TOR)
// Tor is enabled by default.
EXPECT_FALSE(tor::TorProfileService::IsTorDisabled());
#endif
}
14 changes: 5 additions & 9 deletions browser/policy/brave_policy_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "brave/browser/tor/buildflags.h"
#include "brave/common/pref_names.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
Expand All @@ -15,7 +16,6 @@

#if BUILDFLAG(ENABLE_TOR)
#include "brave/browser/tor/tor_profile_service.h"
#include "brave/common/tor/pref_names.h"
#endif

using testing::_;
Expand All @@ -39,7 +39,6 @@ class BravePolicyTest : public InProcessBrowserTest {
};

#if BUILDFLAG(ENABLE_TOR)
#if defined(OS_WIN)
// This policy only exists on Windows.
// Sets the tor policy before the browser is started.
class TorDisabledPolicyBrowserTest : public BravePolicyTest {
Expand All @@ -59,6 +58,8 @@ class TorDisabledPolicyBrowserTest : public BravePolicyTest {
};

IN_PROC_BROWSER_TEST_F(TorDisabledPolicyBrowserTest, TorDisabledPrefValueTest) {
// When policy is set, explicit setting doesn't change its pref value.
tor::TorProfileService::SetTorDisabled(false);
EXPECT_TRUE(tor::TorProfileService::IsTorDisabled());
}

Expand All @@ -79,15 +80,10 @@ class TorEnabledPolicyBrowserTest : public BravePolicyTest {
};

IN_PROC_BROWSER_TEST_F(TorEnabledPolicyBrowserTest, TorDisabledPrefValueTest) {
// When policy is set, explicit setting doesn't change its pref value.
tor::TorProfileService::SetTorDisabled(true);
EXPECT_FALSE(tor::TorProfileService::IsTorDisabled());
}

// W/o TorDisabled group policy, kTorDisabled pref value should be false.
IN_PROC_BROWSER_TEST_F(NoTorPolicyBrowserTest,
DefaultTorDisabledPrefValueTest) {
EXPECT_FALSE(tor::TorProfileService::IsTorDisabled());
}
#endif // OS_WIN
#endif // ENABLE_TOR

} // namespace policy
6 changes: 5 additions & 1 deletion browser/resources/settings/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import("//brave/browser/tor/buildflags/buildflags.gni")
import("//brave/build/config.gni")
import("//chrome/browser/resources/optimize_webui.gni")
import("//chrome/common/features.gni")
Expand All @@ -6,7 +7,10 @@ import("//tools/grit/grit_rule.gni")
grit("resources") {
source = "settings_resources.grd"

defines = ["is_release_channel=$is_release_channel"]
defines = [
"is_release_channel=$is_release_channel",
"enable_tor=$enable_tor",
]

source_is_generated = optimize_webui

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ cr.define('settings', function() {
setBraveWalletEnabled(value) {}
setHangoutsEnabled(value) {}
setIPFSCompanionEnabled(value) {}
setTorEnabled(value) {}
isTorEnabled() {}
isTorManaged() {}
getRestartNeeded() {}
}

Expand All @@ -35,6 +38,15 @@ cr.define('settings', function() {
setMediaRouterEnabled(value) {
chrome.send('setMediaRouterEnabled', [value]);
}
setTorEnabled(value) {
chrome.send('setTorEnabled', [value]);
}
isTorEnabled() {
return cr.sendWithPromise('isTorEnabled');
}
isTorManaged() {
return cr.sendWithPromise('isTorManaged');
}
getRestartNeeded() {
return cr.sendWithPromise('getRestartNeeded');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,6 @@
sub-label="$i18n{braveWalletEnabledDesc}"
on-settings-boolean-control-change="onBraveWalletEnabledChange_">
</settings-toggle-button>
<settings-toggle-button id="webTorrentEnabled"
pref="{{prefs.brave.webtorrent_enabled}}"
label="WebTorrent"
sub-label="$i18n{webTorrentEnabledDesc}"
on-settings-boolean-control-change="onWebTorrentEnabledChange_">
</settings-toggle-button>
<settings-toggle-button id="hangoutsEnabled"
pref="{{prefs.brave.hangouts_enabled}}"
label="Hangouts"
Expand All @@ -90,17 +84,33 @@
sub-label="$i18n{ipfsCompanionEnabledDesc}"
on-settings-boolean-control-change="onIPFSCompanionEnabledChange_">
</settings-toggle-button>
<settings-toggle-button
pref="{{prefs.brave.ask_widevine_install}}"
label="Widevine"
sub-label="$i18n{appearanceSettingsAskWidevineInstallDesc}">
</settings-toggle-button>
<settings-toggle-button id="mediaRouterEnabled"
pref="{{prefs.brave.enable_media_router}}"
label="Media Router"
sub-label="$i18n{mediaRouterEnabledDesc}"
on-settings-boolean-control-change="onMediaRouterEnabledChange_">
</settings-toggle-button>
<if expr="enable_tor">
<settings-toggle-button id="torEnabled"
pref=""
checked="[[torEnabled_]]"
label="$i18n{torEnabledLabel}"
sub-label="$i18n{torEnabledDesc}"
disabled="[[disableTorOption_]]"
on-settings-boolean-control-change="onTorEnabledChange_">
</settings-toggle-button>
</if>
<settings-toggle-button id="webTorrentEnabled"
pref="{{prefs.brave.webtorrent_enabled}}"
label="WebTorrent"
sub-label="$i18n{webTorrentEnabledDesc}"
on-settings-boolean-control-change="onWebTorrentEnabledChange_">
</settings-toggle-button>
<settings-toggle-button
pref="{{prefs.brave.ask_widevine_install}}"
label="Widevine"
sub-label="$i18n{appearanceSettingsAskWidevineInstallDesc}">
</settings-toggle-button>
<div class="settings-row" id="manageExtensionsRow">
<cr-link-row icon-class="icon-external"
label="$i18n{manageExtensionsLabel}" on-click="openExtensionsPage_">
Expand All @@ -113,7 +123,7 @@
on-click="openWebStoreUrl_" external>
</cr-link-row>
</div>
<template is="dom-if" if="{{ showRestartToast }}">
<template is="dom-if" if="{{ showRestartToast_ }}">
<div id="needsRestart">
<div class="flex-container">
<div class="flex restart-notice" jstcache="0">$i18n{restartNotice}</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@ Polymer({
WebUIListenerBehavior,
],

properties: {
showRestartToast_: Boolean,
torEnabled_: Boolean,
disableTorOption_: Boolean,
},

/** @private {?settings.BraveDefaultExtensionsBrowserProxy} */
browserProxy_: null,
showRestartToast: false,

/** @override */
created: function() {
this.browserProxy_ = settings.BraveDefaultExtensionsBrowserProxyImpl.getInstance();
this.browserProxy_.getRestartNeeded().then(show => {
this.showRestartToast = show;
});
this.addWebUIListener('brave-needs-restart-changed', (needsRestart) => {
this.showRestartToast = needsRestart
})
},

/** @override */
Expand All @@ -39,6 +38,24 @@ Polymer({
this.onIPFSCompanionEnabledChange_ = this.onIPFSCompanionEnabledChange_.bind(this)
this.openExtensionsPage_ = this.openExtensionsPage_.bind(this)
this.restartBrowser_ = this.restartBrowser_.bind(this)
this.onTorEnabledChange_ = this.onTorEnabledChange_.bind(this)

this.addWebUIListener('brave-needs-restart-changed', (needsRestart) => {
this.showRestartToast_ = needsRestart
})
this.addWebUIListener('tor-enabled-changed', (enabled) => {
this.torEnabled_ = enabled
})

this.browserProxy_.getRestartNeeded().then(show => {
this.showRestartToast_ = show;
});
this.browserProxy_.isTorEnabled().then(enabled => {
this.torEnabled_ = enabled
})
this.browserProxy_.isTorManaged().then(managed => {
this.disableTorOption_ = managed
})
},

onWebTorrentEnabledChange_: function() {
Expand All @@ -65,6 +82,10 @@ Polymer({
this.browserProxy_.setMediaRouterEnabled(this.$.mediaRouterEnabled.checked);
},

onTorEnabledChange_: function() {
this.browserProxy_.setTorEnabled(this.$.torEnabled.checked);
},

openExtensionsPage_: function() {
window.open("chrome://extensions", "_self");
},
Expand Down
6 changes: 5 additions & 1 deletion browser/tor/tor_profile_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ void TorProfileService::RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
const std::string tor_proxy_uri =
std::string(kTorProxyScheme) + std::string(kTorProxyAddress) + ":" + port;
registry->RegisterStringPref(prefs::kTorProxyString, tor_proxy_uri);
// Tor for group policy. Only can be true by group policy.
registry->RegisterBooleanPref(prefs::kTorDisabled, false);
}

Expand All @@ -65,6 +64,11 @@ bool TorProfileService::IsTorDisabled() {
return g_browser_process->local_state()->GetBoolean(prefs::kTorDisabled);
}

// static
void TorProfileService::SetTorDisabled(bool disabled) {
g_browser_process->local_state()->SetBoolean(prefs::kTorDisabled, disabled);
}

std::string TorProfileService::GetTorProxyURI() {
return g_browser_process->local_state()->GetString(prefs::kTorProxyString);
}
Expand Down
2 changes: 2 additions & 0 deletions browser/tor/tor_profile_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PrefRegistrySyncable;
}

class PrefRegistrySimple;
class BraveAppMenuBrowserTestWithTorDisabled;

namespace tor {

Expand All @@ -39,6 +40,7 @@ class TorProfileService : public KeyedService {
~TorProfileService() override;

static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
static void SetTorDisabled(bool disabled);
static bool IsTorDisabled();

virtual void SetNewTorCircuit(content::WebContents* web_contents) = 0;
Expand Down
10 changes: 8 additions & 2 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ source_set("ui") {
"//brave/components/webcompat_reporter/browser",
"//brave/common",
"//brave/common:pref_names",
"//brave/common/tor",
"//brave/components/brave_adblock_ui:generated_resources",
"//brave/components/webcompat_reporter/ui:generated_resources",
"//brave/components/brave_new_tab_ui:generated_resources",
Expand Down Expand Up @@ -237,10 +238,15 @@ source_set("ui") {
"views/tabs/brave_new_tab_button.h",
"views/toolbar/brave_toolbar_view.cc",
"views/toolbar/brave_toolbar_view.h",
"webui/settings/brave_default_extensions_handler.cc",
"webui/settings/brave_default_extensions_handler.h",
]

if (enable_extensions) {
sources += [
"webui/settings/brave_default_extensions_handler.cc",
"webui/settings/brave_default_extensions_handler.h",
]
}

if (brave_rewards_enabled) {
sources += [
"views/brave_actions/brave_rewards_action_stub_view.cc",
Expand Down
Loading

0 comments on commit b1d0576

Please sign in to comment.