Skip to content

Commit 3e01e1f

Browse files
committed
Don't need to restart after changing tor option
Browsr commands will be updated after changing tor option. If user turns it off, tor component will be removed at the next launching. If there is already opened tor window, it can be used. But, user can't create new tor window anymore.
1 parent 744f854 commit 3e01e1f

16 files changed

+106
-103
lines changed

browser/brave_browser_process_impl.cc

+24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "brave/browser/component_updater/brave_component_updater_delegate.h"
1717
#include "brave/browser/profiles/brave_profile_manager.h"
1818
#include "brave/browser/tor/buildflags.h"
19+
#include "brave/browser/ui/brave_browser_command_controller.h"
1920
#include "brave/components/brave_component_updater/browser/local_data_files_service.h"
2021
#include "brave/components/brave_shields/browser/ad_block_custom_filters_service.h"
2122
#include "brave/components/brave_shields/browser/ad_block_regional_service_manager.h"
@@ -27,6 +28,7 @@
2728
#include "brave/components/p3a/buildflags.h"
2829
#include "brave/components/p3a/brave_histogram_rewrite.h"
2930
#include "brave/components/p3a/brave_p3a_service.h"
31+
#include "chrome/browser/ui/browser_list.h"
3032
#include "chrome/common/buildflags.h"
3133
#include "chrome/common/chrome_paths.h"
3234
#include "components/component_updater/component_updater_service.h"
@@ -57,11 +59,14 @@
5759

5860
#if BUILDFLAG(ENABLE_TOR)
5961
#include "brave/browser/extensions/brave_tor_client_updater.h"
62+
#include "brave/common/tor/pref_names.h"
6063
#endif
6164

6265
#if defined(OS_ANDROID)
6366
#include "chrome/browser/android/chrome_feature_list.h"
6467
#include "chrome/browser/android/component_updater/background_task_update_scheduler.h"
68+
#else
69+
#include "chrome/browser/ui/browser.h"
6570
#endif
6671

6772
BraveBrowserProcessImpl* g_brave_browser_process = nullptr;
@@ -102,6 +107,17 @@ BraveBrowserProcessImpl::BraveBrowserProcessImpl(StartupData* startup_data)
102107
#endif // BUILDFLAG(BRAVE_P3A_ENABLED)
103108
}
104109

110+
void BraveBrowserProcessImpl::Init() {
111+
BrowserProcessImpl::Init();
112+
113+
#if BUILDFLAG(ENABLE_TOR)
114+
pref_change_registrar_.Add(
115+
tor::prefs::kTorDisabled,
116+
base::Bind(&BraveBrowserProcessImpl::OnTorEnabledChanged,
117+
base::Unretained(this)));
118+
#endif
119+
}
120+
105121
brave_component_updater::BraveComponent::Delegate*
106122
BraveBrowserProcessImpl::brave_component_updater_delegate() {
107123
if (!brave_component_updater_delegate_)
@@ -246,6 +262,14 @@ BraveBrowserProcessImpl::tor_client_updater() {
246262
brave_component_updater_delegate());
247263
return tor_client_updater_.get();
248264
}
265+
266+
void BraveBrowserProcessImpl::OnTorEnabledChanged() {
267+
// Update all browsers' tor command status.
268+
for (Browser* browser : *BrowserList::GetInstance()) {
269+
static_cast<chrome::BraveBrowserCommandController*>(
270+
browser->command_controller())->UpdateCommandForTor();
271+
}
272+
}
249273
#endif
250274

251275
brave::BraveP3AService* BraveBrowserProcessImpl::brave_p3a_service() {

browser/brave_browser_process_impl.h

+7
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,16 @@ class BraveBrowserProcessImpl : public BrowserProcessImpl {
9393
brave::BraveStatsUpdater* brave_stats_updater();
9494

9595
private:
96+
// BrowserProcessImpl overrides:
97+
void Init() override;
98+
9699
void CreateProfileManager();
97100
void CreateNotificationPlatformBridge();
98101

102+
#if BUILDFLAG(ENABLE_TOR)
103+
void OnTorEnabledChanged();
104+
#endif
105+
99106
BraveComponent::Delegate* brave_component_updater_delegate();
100107

101108
// local_data_files_service_ should always be first because it needs

browser/brave_local_state_browsertest.cc

-27
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#if BUILDFLAG(ENABLE_TOR)
1111
#include "brave/browser/tor/tor_profile_service.h"
12-
#include "brave/common/tor/pref_names.h"
1312
#endif
1413

1514
using BraveLocalStateBrowserTest = InProcessBrowserTest;
@@ -20,29 +19,3 @@ IN_PROC_BROWSER_TEST_F(BraveLocalStateBrowserTest, BasicTest) {
2019
EXPECT_FALSE(tor::TorProfileService::IsTorDisabled());
2120
#endif
2221
}
23-
24-
#if BUILDFLAG(ENABLE_TOR)
25-
IN_PROC_BROWSER_TEST_F(BraveLocalStateBrowserTest, PRE_TorDisabledTest) {
26-
EXPECT_EQ(tor::TorProfileService::IsTorDisabled(),
27-
tor::TorProfileService::GetTorDisabledPref());
28-
29-
// Set to same value(enable) to next launching prefs, doesn't affect anything.
30-
tor::TorProfileService::SetTorDisabledPref(false);
31-
EXPECT_EQ(tor::TorProfileService::IsTorDisabled(),
32-
tor::TorProfileService::GetTorDisabledPref());
33-
34-
// Check setting to disable to next launching prefs doesn't affect
35-
// IsTorDisabled().
36-
tor::TorProfileService::SetTorDisabledPref(true);
37-
EXPECT_FALSE(tor::TorProfileService::IsTorDisabled());
38-
EXPECT_NE(tor::TorProfileService::IsTorDisabled(),
39-
tor::TorProfileService::GetTorDisabledPref());
40-
}
41-
42-
IN_PROC_BROWSER_TEST_F(BraveLocalStateBrowserTest, TorDisabledTest) {
43-
// Check IsTorDisabled() is changed from previous test run.
44-
EXPECT_TRUE(tor::TorProfileService::IsTorDisabled());
45-
EXPECT_EQ(tor::TorProfileService::IsTorDisabled(),
46-
tor::TorProfileService::GetTorDisabledPref());
47-
}
48-
#endif

browser/extensions/brave_tor_client_updater.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ BraveTorClientUpdater::~BraveTorClientUpdater() {
123123
void BraveTorClientUpdater::Register() {
124124
const base::CommandLine& command_line =
125125
*base::CommandLine::ForCurrentProcess();
126-
if (tor::TorProfileService::GetTorDisabledPref() ||
126+
if (tor::TorProfileService::IsTorDisabled() ||
127127
command_line.HasSwitch(switches::kDisableTorClientUpdaterExtension) ||
128128
registered_) {
129129
return;

browser/extensions/brave_tor_client_updater.h

-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ class BraveTorClientUpdater : public BraveComponent {
6161
void AddObserver(Observer* observer);
6262
void RemoveObserver(Observer* observer);
6363

64-
bool registered() const { return registered_; }
65-
6664
protected:
6765
void OnComponentReady(const std::string& component_id,
6866
const base::FilePath& install_dir,

browser/policy/brave_policy_browsertest.cc

+4-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#if BUILDFLAG(ENABLE_TOR)
1818
#include "brave/browser/tor/tor_profile_service.h"
19-
#include "brave/common/tor/pref_names.h"
2019
#endif
2120

2221
using testing::_;
@@ -40,7 +39,6 @@ class BravePolicyTest : public InProcessBrowserTest {
4039
};
4140

4241
#if BUILDFLAG(ENABLE_TOR)
43-
#if defined(OS_WIN)
4442
// This policy only exists on Windows.
4543
// Sets the tor policy before the browser is started.
4644
class TorDisabledPolicyBrowserTest : public BravePolicyTest {
@@ -61,9 +59,8 @@ class TorDisabledPolicyBrowserTest : public BravePolicyTest {
6159

6260
IN_PROC_BROWSER_TEST_F(TorDisabledPolicyBrowserTest, TorDisabledPrefValueTest) {
6361
// When policy is set, explicit setting doesn't change its pref value.
64-
g_browser_process->local_state()->SetBoolean(tor::prefs::kTorDisabled, false);
65-
EXPECT_TRUE(
66-
g_browser_process->local_state()->GetBoolean(tor::prefs::kTorDisabled));
62+
tor::TorProfileService::SetTorDisabled(false);
63+
EXPECT_TRUE(tor::TorProfileService::IsTorDisabled());
6764
}
6865

6966
class TorEnabledPolicyBrowserTest : public BravePolicyTest {
@@ -84,11 +81,9 @@ class TorEnabledPolicyBrowserTest : public BravePolicyTest {
8481

8582
IN_PROC_BROWSER_TEST_F(TorEnabledPolicyBrowserTest, TorDisabledPrefValueTest) {
8683
// When policy is set, explicit setting doesn't change its pref value.
87-
g_browser_process->local_state()->SetBoolean(tor::prefs::kTorDisabled, true);
88-
EXPECT_FALSE(
89-
g_browser_process->local_state()->GetBoolean(tor::prefs::kTorDisabled));
84+
tor::TorProfileService::SetTorDisabled(true);
85+
EXPECT_FALSE(tor::TorProfileService::IsTorDisabled());
9086
}
91-
#endif // OS_WIN
9287
#endif // ENABLE_TOR
9388

9489
} // namespace policy

browser/resources/settings/brave_default_extensions_page/brave_default_extensions_browser_proxy.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ cr.define('settings', function() {
1313
setHangoutsEnabled(value) {}
1414
setIPFSCompanionEnabled(value) {}
1515
setTorEnabled(value) {}
16-
getTorEnabled() {}
17-
getTorManaged() {}
16+
isTorEnabled() {}
17+
isTorManaged() {}
1818
getRestartNeeded() {}
1919
}
2020

@@ -41,10 +41,10 @@ cr.define('settings', function() {
4141
setTorEnabled(value) {
4242
chrome.send('setTorEnabled', [value]);
4343
}
44-
getTorEnabled() {
45-
return cr.sendWithPromise('getTorEnabled');
44+
isTorEnabled() {
45+
return cr.sendWithPromise('isTorEnabled');
4646
}
47-
getTorManaged() {
47+
isTorManaged() {
4848
return cr.sendWithPromise('isTorManaged');
4949
}
5050
getRestartNeeded() {

browser/resources/settings/brave_default_extensions_page/brave_default_extensions_page.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ Polymer({
4343
this.addWebUIListener('brave-needs-restart-changed', (needsRestart) => {
4444
this.showRestartToast_ = needsRestart
4545
})
46-
this.addWebUIListener('tor-enabled-pref-changed', (enabled) => {
46+
this.addWebUIListener('tor-enabled-changed', (enabled) => {
4747
this.torEnabled_ = enabled
4848
})
4949

5050
this.browserProxy_.getRestartNeeded().then(show => {
5151
this.showRestartToast_ = show;
5252
});
53-
this.browserProxy_.getTorEnabled().then(enabled => {
53+
this.browserProxy_.isTorEnabled().then(enabled => {
5454
this.torEnabled_ = enabled
5555
})
56-
this.browserProxy_.getTorManaged().then(managed => {
56+
this.browserProxy_.isTorManaged().then(managed => {
5757
this.disableTorOption_ = managed
5858
})
5959
},

browser/tor/tor_profile_service.cc

+1-17
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,16 @@ void TorProfileService::RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
5656
const std::string tor_proxy_uri =
5757
std::string(kTorProxyScheme) + std::string(kTorProxyAddress) + ":" + port;
5858
registry->RegisterStringPref(prefs::kTorProxyString, tor_proxy_uri);
59-
// This pref value and current tor enabled state might be different because
60-
// user can change pref value. But, this pref changes doesn't affect current
61-
// tor enabled state. Instead, this pref value uses whether tor component is
62-
// registered or not at startup. Tor component is only registered if this has
63-
// false.
64-
// kTorDisabled could be managed. User can only change it via settings if it's
65-
// not managed. For now, only Windows support tor group policy.
6659
registry->RegisterBooleanPref(prefs::kTorDisabled, false);
6760
}
6861

6962
// static
7063
bool TorProfileService::IsTorDisabled() {
71-
// In test, |g_brave_browser_process| could be null.
72-
if (!g_brave_browser_process) {
73-
return false;
74-
}
75-
return !g_brave_browser_process->tor_client_updater()->registered();
76-
}
77-
78-
// static
79-
bool TorProfileService::GetTorDisabledPref() {
8064
return g_browser_process->local_state()->GetBoolean(prefs::kTorDisabled);
8165
}
8266

8367
// static
84-
void TorProfileService::SetTorDisabledPref(bool disabled) {
68+
void TorProfileService::SetTorDisabled(bool disabled) {
8569
g_browser_process->local_state()->SetBoolean(prefs::kTorDisabled, disabled);
8670
}
8771

browser/tor/tor_profile_service.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class TorProfileService : public KeyedService {
4040
~TorProfileService() override;
4141

4242
static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
43-
static void SetTorDisabledPref(bool disabled);
44-
static bool GetTorDisabledPref();
43+
static void SetTorDisabled(bool disabled);
4544
static bool IsTorDisabled();
4645

4746
virtual void SetNewTorCircuit(content::WebContents* web_contents) = 0;

browser/ui/brave_browser_command_controller.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ void BraveBrowserCommandController::UpdateCommandForWebcompatReporter() {
143143
UpdateCommandEnabled(IDC_SHOW_BRAVE_WEBCOMPAT_REPORTER, true);
144144
}
145145

146-
void BraveBrowserCommandController::UpdateCommandForTor() {
147146
#if BUILDFLAG(ENABLE_TOR)
147+
void BraveBrowserCommandController::UpdateCommandForTor() {
148148
const bool is_tor_enabled = !tor::TorProfileService::IsTorDisabled();
149149
UpdateCommandEnabled(IDC_NEW_TOR_CONNECTION_FOR_SITE, is_tor_enabled);
150150
UpdateCommandEnabled(IDC_NEW_OFFTHERECORD_WINDOW_TOR, is_tor_enabled);
151-
#endif
152151
}
152+
#endif
153153

154154
void BraveBrowserCommandController::UpdateCommandForBraveSync() {
155155
UpdateCommandEnabled(IDC_SHOW_BRAVE_SYNC, true);

browser/ui/brave_browser_command_controller.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BRAVE_BROWSER_UI_BRAVE_BROWSER_COMMAND_CONTROLLER_H_
77
#define BRAVE_BROWSER_UI_BRAVE_BROWSER_COMMAND_CONTROLLER_H_
88

9+
#include "brave/browser/tor/buildflags.h"
910
#include "chrome/browser/ui/browser_command_controller.h"
1011

1112
// This namespace is needed for a chromium_src override
@@ -15,6 +16,10 @@ class BraveBrowserCommandController : public chrome::BrowserCommandController {
1516
public:
1617
explicit BraveBrowserCommandController(Browser* browser);
1718

19+
#if BUILDFLAG(ENABLE_TOR)
20+
void UpdateCommandForTor();
21+
#endif
22+
1823
private:
1924
// Overriden from CommandUpdater:
2025
bool SupportsCommand(int id) const override;
@@ -32,7 +37,6 @@ class BraveBrowserCommandController : public chrome::BrowserCommandController {
3237
void UpdateCommandForBraveRewards();
3338
void UpdateCommandForBraveAdblock();
3439
void UpdateCommandForWebcompatReporter();
35-
void UpdateCommandForTor();
3640
void UpdateCommandForBraveSync();
3741
void UpdateCommandForBraveWallet();
3842

browser/ui/toolbar/brave_app_menu_model_browsertest.cc

+3-6
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,10 @@ IN_PROC_BROWSER_TEST_F(BraveAppMenuBrowserTest, BasicTest) {
137137
}
138138

139139
#if BUILDFLAG(ENABLE_TOR)
140-
// Tor is enabled by default. Change pref to disable it at next test run.
141-
IN_PROC_BROWSER_TEST_F(BraveAppMenuBrowserTest, PRE_TorAppMenuTest) {
142-
tor::TorProfileService::SetTorDisabledPref(true);
143-
}
144-
145-
// If tor is disabled, corresponding menu and commands should be also disabled.
146140
IN_PROC_BROWSER_TEST_F(BraveAppMenuBrowserTest, TorAppMenuTest) {
141+
// Tor is enabled by default. Change pref to disable.
142+
tor::TorProfileService::SetTorDisabled(true);
143+
147144
auto* browser_view = BrowserView::GetBrowserViewForBrowser(browser());
148145
BraveAppMenuModel normal_model(browser_view->toolbar(), browser());
149146
normal_model.Init();

0 commit comments

Comments
 (0)