Skip to content

Commit

Permalink
Fix enable_tor=false
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdh committed Oct 23, 2020
1 parent 33534c6 commit c24c61d
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 19 deletions.
8 changes: 7 additions & 1 deletion browser/search_engines/search_engine_tracker_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
#include "brave/browser/profiles/profile_util.h"
#include "brave/browser/ui/browser_commands.h"
#include "brave/components/search_engines/brave_prepopulated_engines.h"
#include "brave/components/tor/tor_launcher_factory.h"
#include "brave/components/tor/buildflags/buildflags.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/search_engines/template_url_prepopulate_data.h"
#include "content/public/test/browser_test.h"

#if BUILDFLAG(ENABLE_TOR)
#include "brave/components/tor/tor_launcher_factory.h"
#endif

class SearchEngineProviderP3ATest : public InProcessBrowserTest {
public:
SearchEngineProviderP3ATest() {
Expand Down Expand Up @@ -47,8 +51,10 @@ IN_PROC_BROWSER_TEST_F(SearchEngineProviderP3ATest,

// Check that incognito or TOR profiles do not emit the metric.
CreateIncognitoBrowser();
#if BUILDFLAG(ENABLE_TOR)
ScopedTorLaunchPreventerForTest prevent_tor_process;
brave::NewOffTheRecordWindowTor(browser());
#endif

histogram_tester_->ExpectTotalCount(kDefaultSearchEngineMetric, 2);
}
10 changes: 8 additions & 2 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import("//brave/components/brave_wallet/buildflags/buildflags.gni")
import("//brave/components/brave_wayback_machine/buildflags/buildflags.gni")
import("//brave/components/ipfs/buildflags/buildflags.gni")
import("//brave/components/speedreader/buildflags.gni")
import("//brave/components/tor/buildflags/buildflags.gni")
import("//build/config/features.gni")
import("//components/gcm_driver/config.gni")
import("//chrome/common/features.gni")
Expand Down Expand Up @@ -293,8 +294,6 @@ source_set("ui") {
"views/location_bar/brave_location_bar_view.h",
"views/location_bar/brave_star_view.cc",
"views/location_bar/brave_star_view.h",
"views/location_bar/onion_location_view.cc",
"views/location_bar/onion_location_view.h",
"views/profiles/brave_avatar_toolbar_button.cc",
"views/profiles/brave_avatar_toolbar_button.h",
"views/tabs/brave_alert_indicator.cc",
Expand All @@ -319,6 +318,13 @@ source_set("ui") {
]
}

if (enable_tor) {
sources += [
"views/location_bar/onion_location_view.cc",
"views/location_bar/onion_location_view.h",
]
}

deps += [
"//brave/browser/resources/extensions:resources",
"//brave/components/brave_extension:generated_resources",
Expand Down
4 changes: 1 addition & 3 deletions browser/ui/brave_browser_command_controller_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ IN_PROC_BROWSER_TEST_F(BraveBrowserCommandControllerTest,
EXPECT_TRUE(command_controller->IsCommandEnabled(
IDC_SHOW_BRAVE_WEBCOMPAT_REPORTER));

#if BUILDFLAG(ENABLE_TOR)
// Launch tor window and check its command status.
content::WindowedNotificationObserver tor_browser_creation_observer(
chrome::NOTIFICATION_BROWSER_OPENED,
Expand All @@ -176,12 +177,10 @@ IN_PROC_BROWSER_TEST_F(BraveBrowserCommandControllerTest,

EXPECT_TRUE(command_controller->IsCommandEnabled(IDC_SHOW_BRAVE_ADBLOCK));

#if BUILDFLAG(ENABLE_TOR)
EXPECT_TRUE(
command_controller->IsCommandEnabled(IDC_NEW_TOR_CONNECTION_FOR_SITE));
EXPECT_TRUE(
command_controller->IsCommandEnabled(IDC_NEW_OFFTHERECORD_WINDOW_TOR));
#endif

#if BUILDFLAG(ENABLE_BRAVE_SYNC)
if (switches::IsSyncAllowedByFlag())
Expand All @@ -200,7 +199,6 @@ IN_PROC_BROWSER_TEST_F(BraveBrowserCommandControllerTest,
IDC_SHOW_BRAVE_WEBCOMPAT_REPORTER));

// Check tor commands when tor is disabled.
#if BUILDFLAG(ENABLE_TOR)
TorProfileServiceFactory::SetTorDisabled(true);
command_controller = browser()->command_controller();
EXPECT_FALSE(
Expand Down
4 changes: 2 additions & 2 deletions browser/ui/toolbar/brave_app_menu_model_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ IN_PROC_BROWSER_TEST_F(BraveAppMenuBrowserTest, MenuOrderTest) {
CheckCommandsAreDisabledInMenuModel(guest_browser,
commands_disabled_for_guest_profile);

#if BUILDFLAG(ENABLE_TOR)
content::WindowedNotificationObserver tor_browser_creation_observer(
chrome::NOTIFICATION_BROWSER_OPENED,
content::NotificationService::AllSources());
Expand All @@ -215,9 +216,7 @@ IN_PROC_BROWSER_TEST_F(BraveAppMenuBrowserTest, MenuOrderTest) {
IDC_NEW_TOR_CONNECTION_FOR_SITE,
IDC_NEW_WINDOW,
IDC_NEW_INCOGNITO_WINDOW,
#if BUILDFLAG(ENABLE_TOR)
IDC_NEW_OFFTHERECORD_WINDOW_TOR,
#endif
#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
IDC_SHOW_BRAVE_REWARDS,
#endif
Expand Down Expand Up @@ -251,4 +250,5 @@ IN_PROC_BROWSER_TEST_F(BraveAppMenuBrowserTest, MenuOrderTest) {
commands_in_order_for_tor_profile);
CheckCommandsAreDisabledInMenuModel(tor_browser,
commands_disabled_for_tor_profile);
#endif
}
19 changes: 16 additions & 3 deletions browser/ui/views/location_bar/brave_location_bar_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "brave/browser/profiles/profile_util.h"
#include "brave/browser/themes/brave_theme_service.h"
#include "brave/browser/ui/views/brave_actions/brave_actions_container.h"
#include "brave/browser/ui/views/location_bar/onion_location_view.h"
#include "brave/browser/ui/views/toolbar/brave_toolbar_view.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_service_factory.h"
Expand All @@ -21,6 +20,10 @@
#include "components/version_info/channel.h"
#include "ui/views/controls/highlight_path_generator.h"

#if BUILDFLAG(ENABLE_TOR)
#include "brave/browser/ui/views/location_bar/onion_location_view.h"
#endif

namespace {

class BraveLocationBarViewFocusRingHighlightPathGenerator
Expand Down Expand Up @@ -49,11 +52,13 @@ void BraveLocationBarView::Init() {
std::make_unique<
BraveLocationBarViewFocusRingHighlightPathGenerator>());
}
// brave action buttons
#if BUILDFLAG(ENABLE_TOR)
onion_location_view_ = new OnionLocationView(browser_->profile());
AddChildView(onion_location_view_);
#endif
// brave action buttons
brave_actions_ = new BraveActionsContainer(browser_, profile());
brave_actions_->Init();
AddChildView(onion_location_view_);
AddChildView(brave_actions_);
// Call Update again to cause a Layout
Update(nullptr);
Expand All @@ -69,8 +74,10 @@ void BraveLocationBarView::Update(content::WebContents* contents) {
if (brave_actions_) {
brave_actions_->Update();
}
#if BUILDFLAG(ENABLE_TOR)
if (onion_location_view_)
onion_location_view_->Update(contents);
#endif
LocationBarView::Update(contents);
}

Expand All @@ -81,18 +88,22 @@ void BraveLocationBarView::OnChanged() {
ShouldHidePageActionIcons() && !omnibox_view_->GetText().empty();
brave_actions_->SetShouldHide(should_hide);
}
#if BUILDFLAG(ENABLE_TOR)
if (onion_location_view_)
onion_location_view_->Update(
browser_->tab_strip_model()->GetActiveWebContents());
#endif

// OnChanged calls Layout
LocationBarView::OnChanged();
}

std::vector<views::View*> BraveLocationBarView::GetTrailingViews() {
std::vector<views::View*> views;
#if BUILDFLAG(ENABLE_TOR)
if (onion_location_view_)
views.push_back(onion_location_view_);
#endif
if (brave_actions_)
views.push_back(brave_actions_);

Expand All @@ -107,11 +118,13 @@ gfx::Size BraveLocationBarView::CalculatePreferredSize() const {
GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING);
min_size.Enlarge(extra_width, 0);
}
#if BUILDFLAG(ENABLE_TOR)
if (onion_location_view_ && onion_location_view_->GetVisible()) {
const int extra_width = GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING) +
onion_location_view_->GetMinimumSize().width();
min_size.Enlarge(extra_width, 0);
}
#endif
return min_size;
}

Expand Down
10 changes: 9 additions & 1 deletion browser/ui/views/location_bar/brave_location_bar_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@

#include <vector>

#include "brave/components/tor/buildflags/buildflags.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"

class BraveActionsContainer;
class BraveActionsContainerTest;
class OnionLocationView;
class RewardsBrowserTest;
class SkPath;

#if BUILDFLAG(ENABLE_TOR)
class OnionLocationView;
#endif

// The purposes of this subclass are to:
// - Add the BraveActionsContainer to the location bar
class BraveLocationBarView : public LocationBarView {
Expand All @@ -25,7 +29,9 @@ class BraveLocationBarView : public LocationBarView {
void Update(content::WebContents* contents) override;
void OnChanged() override;
BraveActionsContainer* GetBraveActionsContainer() { return brave_actions_; }
#if BUILDFLAG(ENABLE_TOR)
OnionLocationView* GetOnionLocationView() { return onion_location_view_; }
#endif

// LocationBarView:
std::vector<views::View*> GetTrailingViews() override;
Expand All @@ -44,7 +50,9 @@ class BraveLocationBarView : public LocationBarView {
friend class ::BraveActionsContainerTest;
friend class ::RewardsBrowserTest;
BraveActionsContainer* brave_actions_ = nullptr;
#if BUILDFLAG(ENABLE_TOR)
OnionLocationView* onion_location_view_ = nullptr;
#endif

DISALLOW_COPY_AND_ASSIGN(BraveLocationBarView);
};
Expand Down
18 changes: 14 additions & 4 deletions browser/ui/webui/settings/brave_default_extensions_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ void BraveDefaultExtensionsHandler::RegisterMessages() {
"getRestartNeeded",
base::BindRepeating(&BraveDefaultExtensionsHandler::GetRestartNeeded,
base::Unretained(this)));
#if BUILDFLAG(ENABLE_TOR)
web_ui()->RegisterMessageCallback(
"setTorEnabled",
base::BindRepeating(
Expand All @@ -97,7 +96,6 @@ void BraveDefaultExtensionsHandler::RegisterMessages() {
"isTorManaged",
base::BindRepeating(&BraveDefaultExtensionsHandler::IsTorManaged,
base::Unretained(this)));
#endif

// Can't call this in ctor because it needs to access web_ui().
InitializePrefCallbacks();
Expand Down Expand Up @@ -233,13 +231,14 @@ void BraveDefaultExtensionsHandler::SetMediaRouterEnabled(
about_flags::SetFeatureEntryEnabled(&flags_storage, feature_name, true);
}

#if BUILDFLAG(ENABLE_TOR)
void BraveDefaultExtensionsHandler::SetTorEnabled(const base::ListValue* args) {
#if BUILDFLAG(ENABLE_TOR)
CHECK_EQ(args->GetSize(), 1U);
bool enabled;
args->GetBoolean(0, &enabled);
AllowJavascript();
TorProfileServiceFactory::SetTorDisabled(!enabled);
#endif
}

void BraveDefaultExtensionsHandler::IsTorEnabled(
Expand All @@ -248,28 +247,39 @@ void BraveDefaultExtensionsHandler::IsTorEnabled(
AllowJavascript();
ResolveJavascriptCallback(
args->GetList()[0],
#if BUILDFLAG(ENABLE_TOR)
base::Value(!TorProfileServiceFactory::IsTorDisabled()));
#else
base::Value(false));
#endif
}

void BraveDefaultExtensionsHandler::OnTorEnabledChanged() {
if (IsJavascriptAllowed()) {
FireWebUIListener(
"tor-enabled-changed",
#if BUILDFLAG(ENABLE_TOR)
base::Value(!TorProfileServiceFactory::IsTorDisabled()));
#else
base::Value(false));
#endif
}
}

void BraveDefaultExtensionsHandler::IsTorManaged(
const base::ListValue* args) {
CHECK_EQ(args->GetSize(), 1U);

#if BUILDFLAG(ENABLE_TOR)
const bool is_managed = g_brave_browser_process->local_state()->
FindPreference(tor::prefs::kTorDisabled)->IsManaged();
#else
const bool is_managed = false;
#endif

AllowJavascript();
ResolveJavascriptCallback(args->GetList()[0], base::Value(is_managed));
}
#endif

void BraveDefaultExtensionsHandler::SetIPFSCompanionEnabled(
const base::ListValue* args) {
Expand Down
2 changes: 0 additions & 2 deletions browser/ui/webui/settings/brave_default_extensions_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ class BraveDefaultExtensionsHandler : public settings::SettingsPageUIHandler {
#if BUILDFLAG(BRAVE_WALLET_ENABLED)
void SetBraveWalletEnabled(const base::ListValue* args);
#endif
#if BUILDFLAG(ENABLE_TOR)
void SetTorEnabled(const base::ListValue* args);
void IsTorEnabled(const base::ListValue* args);
void OnTorEnabledChanged();
void IsTorManaged(const base::ListValue* args);
#endif

void InitializePrefCallbacks();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ void BraveRenderViewContextMenu::AddSpellCheckServiceItem(
void BraveRenderViewContextMenu::InitMenu() {
RenderViewContextMenu_Chromium::InitMenu();

int index = -1;
#if BUILDFLAG(ENABLE_TOR)
// Add Open Link with Tor
int index = -1;
if (!TorProfileServiceFactory::IsTorDisabled() &&
!params_.link_url.is_empty()) {
const Browser* browser = GetBrowser();
Expand Down

0 comments on commit c24c61d

Please sign in to comment.