From 32e45944154ec4df54eff5cbb4456165415a2bb9 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Fri, 2 Oct 2020 11:32:39 -0700 Subject: [PATCH 01/17] Handle onion-location HTTP header & .onion domain --- browser/brave_content_browser_client.cc | 6 + browser/brave_tab_helpers.cc | 2 + browser/tor/BUILD.gn | 4 + .../tor/onion_location_navigation_throttle.cc | 109 ++++++++++++++++++ .../tor/onion_location_navigation_throttle.h | 53 +++++++++ browser/tor/onion_location_tab_helper.cc | 28 +++++ browser/tor/onion_location_tab_helper.h | 41 +++++++ browser/ui/BUILD.gn | 2 + .../brave_actions/brave_actions_container.cc | 9 +- .../brave_actions/brave_actions_container.h | 3 + .../views/location_bar/onion_location_view.cc | 53 +++++++++ .../views/location_bar/onion_location_view.h | 41 +++++++ 12 files changed, 349 insertions(+), 2 deletions(-) create mode 100644 browser/tor/onion_location_navigation_throttle.cc create mode 100644 browser/tor/onion_location_navigation_throttle.h create mode 100644 browser/tor/onion_location_tab_helper.cc create mode 100644 browser/tor/onion_location_tab_helper.h create mode 100644 browser/ui/views/location_bar/onion_location_view.cc create mode 100644 browser/ui/views/location_bar/onion_location_view.h diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 3e8a733f9d40..c37af2200857 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -86,6 +86,7 @@ using extensions::ChromeContentBrowserClientExtensionsPart; #if BUILDFLAG(ENABLE_TOR) #include "brave/browser/tor/tor_navigation_throttle.h" +#include "brave/browser/tor/onion_location_navigation_throttle.h" #endif #if BUILDFLAG(ENABLE_SPEEDREADER) @@ -460,6 +461,11 @@ BraveContentBrowserClient::CreateThrottlesForNavigation( tor::TorNavigationThrottle::MaybeCreateThrottleFor(handle); if (tor_navigation_throttle) throttles.push_back(std::move(tor_navigation_throttle)); + std::unique_ptr + onion_location_navigation_throttle = + tor::OnionLocationNavigationThrottle::CreateThrottleFor(handle); + if (onion_location_navigation_throttle) + throttles.push_back(std::move(onion_location_navigation_throttle)); #endif #if BUILDFLAG(IPFS_ENABLED) diff --git a/browser/brave_tab_helpers.cc b/browser/brave_tab_helpers.cc index cab2af134d81..c3bf429ff674 100644 --- a/browser/brave_tab_helpers.cc +++ b/browser/brave_tab_helpers.cc @@ -60,6 +60,7 @@ #if BUILDFLAG(ENABLE_TOR) #include "brave/browser/tor/tor_tab_helper.h" +#include "brave/browser/tor/onion_location_tab_helper.h" #endif #if BUILDFLAG(IPFS_ENABLED) @@ -118,6 +119,7 @@ void AttachTabHelpers(content::WebContents* web_contents) { #if BUILDFLAG(ENABLE_TOR) tor::TorTabHelper::MaybeCreateForWebContents(web_contents); + tor::OnionLocationTabHelper::CreateForWebContents(web_contents); #endif #if BUILDFLAG(IPFS_ENABLED) diff --git a/browser/tor/BUILD.gn b/browser/tor/BUILD.gn index 140aa75c7034..38416f0a896d 100644 --- a/browser/tor/BUILD.gn +++ b/browser/tor/BUILD.gn @@ -36,12 +36,16 @@ source_set("tor") { "tor_launcher_factory.h", "tor_navigation_throttle.cc", "tor_navigation_throttle.h", + "onion_location_navigation_throttle.cc", + "onion_location_navigation_throttle.h", "tor_profile_service.cc", "tor_profile_service.h", "tor_profile_service_impl.cc", "tor_profile_service_impl.h", "tor_tab_helper.cc", "tor_tab_helper.h", + "onion_location_tab_helper.cc", + "onion_location_tab_helper.h", ] deps += [ diff --git a/browser/tor/onion_location_navigation_throttle.cc b/browser/tor/onion_location_navigation_throttle.cc new file mode 100644 index 000000000000..61cd82ccc132 --- /dev/null +++ b/browser/tor/onion_location_navigation_throttle.cc @@ -0,0 +1,109 @@ +/* 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/. */ + +#include "brave/browser/tor/onion_location_navigation_throttle.h" + +#include +#include + +#include "base/bind.h" +#include "brave/browser/profiles/profile_util.h" +#include "brave/browser/tor/onion_location_tab_helper.h" +#include "chrome/browser/profiles/profile.h" +#include "chrome/browser/profiles/profile_window.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_finder.h" +#include "content/public/browser/navigation_handle.h" +#include "content/public/browser/web_contents.h" + +namespace tor { + +namespace { +bool GetOnionLocation(const net::HttpResponseHeaders* headers, + std::string* onion_location) { + onion_location->clear(); + std::string name = "onion-location"; + + if (!headers || !headers->EnumerateHeader(nullptr, name, onion_location)) + return false; + return true; +} +} // namespace + +// static +std::unique_ptr +OnionLocationNavigationThrottle::CreateThrottleFor( + content::NavigationHandle* navigation_handle) { + return std::make_unique(navigation_handle); +} + +OnionLocationNavigationThrottle::OnionLocationNavigationThrottle( + content::NavigationHandle* navigation_handle) + : content::NavigationThrottle(navigation_handle) { + profile_ = Profile::FromBrowserContext( + navigation_handle->GetWebContents()->GetBrowserContext()); +} + +OnionLocationNavigationThrottle::~OnionLocationNavigationThrottle() {} + +content::NavigationThrottle::ThrottleCheckResult +OnionLocationNavigationThrottle::WillProcessResponse() { + if (navigation_handle()->IsInMainFrame()) { + auto* headers = navigation_handle()->GetResponseHeaders(); + std::string onion_location; + if (headers && GetOnionLocation(headers, &onion_location) && + !navigation_handle()->GetURL().DomainIs("onion")) { + // TODO(darkdh): add pref for always redirect when onion available + // If we are in tor window, open onion site directly + if (brave::IsTorProfile(profile_)) { + profiles::SwitchToTorProfile(base::BindRepeating( + &OnionLocationNavigationThrottle::OnTorProfileCreated, + weak_ptr_factory_.GetWeakPtr(), GURL(onion_location))); + } else { + OnionLocationTabHelper::SetOnionLocation( + navigation_handle()->GetWebContents(), GURL(onion_location)); + } + } else { + OnionLocationTabHelper::SetOnionLocation( + navigation_handle()->GetWebContents(), GURL()); + } + } + return content::NavigationThrottle::PROCEED; +} + +content::NavigationThrottle::ThrottleCheckResult +OnionLocationNavigationThrottle::WillStartRequest() { + if (!brave::IsTorProfile(profile_)) { + GURL url = navigation_handle()->GetURL(); + if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion")) { + profiles::SwitchToTorProfile(base::BindRepeating( + &OnionLocationNavigationThrottle::OnTorProfileCreated, + weak_ptr_factory_.GetWeakPtr(), std::move(url))); + return content::NavigationThrottle::CANCEL_AND_IGNORE; + } + } + return content::NavigationThrottle::PROCEED; +} + +const char* OnionLocationNavigationThrottle::GetNameForLogging() { + return "OnionLocationNavigationThrottle"; +} + +void OnionLocationNavigationThrottle::OnTorProfileCreated( + GURL onion_location, + Profile* profile, + Profile::CreateStatus status) { + if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) + return; + Browser* browser = chrome::FindTabbedBrowser(profile, true); + if (!browser) + return; + content::OpenURLParams open_tor(onion_location, content::Referrer(), + WindowOpenDisposition::OFF_THE_RECORD, + ui::PAGE_TRANSITION_TYPED, false); + browser->OpenURL(open_tor); +} + +} // namespace tor diff --git a/browser/tor/onion_location_navigation_throttle.h b/browser/tor/onion_location_navigation_throttle.h new file mode 100644 index 000000000000..35e2fd6b0e86 --- /dev/null +++ b/browser/tor/onion_location_navigation_throttle.h @@ -0,0 +1,53 @@ +/* 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/. */ + +#ifndef BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_ +#define BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_ + +#include + +#include "base/memory/weak_ptr.h" +#include "chrome/browser/profiles/profile.h" +#include "content/public/browser/navigation_throttle.h" + +class Profile; + +namespace content { +class NavigationHandle; +} // namespace content + +namespace tor { + +class OnionLocationNavigationThrottle : public content::NavigationThrottle { + public: + static std::unique_ptr CreateThrottleFor( + content::NavigationHandle* navigation_handle); + explicit OnionLocationNavigationThrottle( + content::NavigationHandle* navigation_handle); + ~OnionLocationNavigationThrottle() override; + + // content::NavigationThrottle implementation: + ThrottleCheckResult WillProcessResponse() override; + ThrottleCheckResult WillStartRequest() override; + const char* GetNameForLogging() override; + + private: + void OnTorProfileCreated(GURL onion_location, + Profile* proile, + Profile::CreateStatus status); + + Profile* profile_; + + base::WeakPtrFactory weak_ptr_factory_{this}; + + OnionLocationNavigationThrottle(const OnionLocationNavigationThrottle&) = + delete; + OnionLocationNavigationThrottle& operator=( + const OnionLocationNavigationThrottle&) = delete; +}; + +} // namespace tor + +#endif // BRAVE_BROWSER_TOR_ONION_LOCATION_NAVIGATION_THROTTLE_H_ diff --git a/browser/tor/onion_location_tab_helper.cc b/browser/tor/onion_location_tab_helper.cc new file mode 100644 index 000000000000..a2f81b50cf4e --- /dev/null +++ b/browser/tor/onion_location_tab_helper.cc @@ -0,0 +1,28 @@ +/* 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/. */ + +#include "brave/browser/tor/onion_location_tab_helper.h" + +namespace tor { + +OnionLocationTabHelper::~OnionLocationTabHelper() = default; + +// static +void OnionLocationTabHelper::SetOnionLocation( + content::WebContents* web_contents, + const GURL& onion_location) { + OnionLocationTabHelper* tab_helper = FromWebContents(web_contents); + if (!tab_helper) + return; + tab_helper->onion_location_ = onion_location; +} + +OnionLocationTabHelper::OnionLocationTabHelper( + content::WebContents* web_contents) + : content::WebContentsObserver(web_contents) {} + +WEB_CONTENTS_USER_DATA_KEY_IMPL(OnionLocationTabHelper) + +} // namespace tor diff --git a/browser/tor/onion_location_tab_helper.h b/browser/tor/onion_location_tab_helper.h new file mode 100644 index 000000000000..b8491db6a04d --- /dev/null +++ b/browser/tor/onion_location_tab_helper.h @@ -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/. */ + +#ifndef BRAVE_BROWSER_TOR_ONION_LOCATION_TAB_HELPER_H_ +#define BRAVE_BROWSER_TOR_ONION_LOCATION_TAB_HELPER_H_ + +#include "content/public/browser/web_contents_observer.h" +#include "content/public/browser/web_contents_user_data.h" + +namespace tor { + +class OnionLocationTabHelper + : public content::WebContentsObserver, + public content::WebContentsUserData { + public: + ~OnionLocationTabHelper() override; + + static void SetOnionLocation(content::WebContents* web_contents, + const GURL& onion_location); + + bool should_show_icon() const { return !onion_location_.is_empty(); } + + GURL onion_location() const { return onion_location_; } + + WEB_CONTENTS_USER_DATA_KEY_DECL(); + + private: + explicit OnionLocationTabHelper(content::WebContents* web_contents); + friend class content::WebContentsUserData; + + GURL onion_location_; + + OnionLocationTabHelper(const OnionLocationTabHelper&) = delete; + OnionLocationTabHelper& operator=(const OnionLocationTabHelper&) = delete; +}; + +} // namespace tor + +#endif // BRAVE_BROWSER_TOR_ONION_LOCATION_TAB_HELPER_H_ diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index 0986c6ca529a..f394b5f22a30 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -291,6 +291,8 @@ 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", diff --git a/browser/ui/views/brave_actions/brave_actions_container.cc b/browser/ui/views/brave_actions/brave_actions_container.cc index d676173e17af..9237917789d3 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.cc +++ b/browser/ui/views/brave_actions/brave_actions_container.cc @@ -15,6 +15,7 @@ #include "brave/browser/ui/brave_actions/brave_action_view_controller.h" #include "brave/browser/ui/views/brave_actions/brave_action_view.h" #include "brave/browser/ui/views/brave_actions/brave_rewards_action_stub_view.h" +#include "brave/browser/ui/views/location_bar/onion_location_view.h" #include "brave/browser/ui/views/rounded_separator.h" #include "brave/common/brave_switches.h" #include "brave/common/pref_names.h" @@ -38,6 +39,7 @@ #include "ui/views/layout/grid_layout.h" #include "ui/views/view.h" + class BraveActionsContainer::EmptyExtensionsContainer : public ExtensionsContainer { public: @@ -146,11 +148,13 @@ void BraveActionsContainer::Init() { // separator right margin brave_button_separator_->SetBorder( views::CreateEmptyBorder(0, 0, 0, kSeparatorRightMargin)); + onion_location_view_ = new OnionLocationView(); + AddChildViewAt(onion_location_view_, 0); // Just in case the extensions load before this function does (not likely!) // make sure separator is at index 0 - AddChildViewAt(brave_button_separator_, 0); + AddChildViewAt(brave_button_separator_, 1); // Populate actions - actions_[brave_extension_id].position_ = 1; + actions_[brave_extension_id].position_ = 2; actions_[brave_rewards_extension_id].position_ = ACTION_ANY_POSITION; // React to Brave Rewards preferences changes. @@ -312,6 +316,7 @@ void BraveActionsContainer::Update() { // only show separator if we're showing any buttons const bool visible = !should_hide_ && can_show; SetVisible(visible); + onion_location_view_->Update(GetCurrentWebContents()); Layout(); } diff --git a/browser/ui/views/brave_actions/brave_actions_container.h b/browser/ui/views/brave_actions/brave_actions_container.h index d635b63324b2..d800cb0707a9 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.h +++ b/browser/ui/views/brave_actions/brave_actions_container.h @@ -27,6 +27,7 @@ #endif class BraveActionViewController; +class OnionLocationView; class BraveActionsContainerTest; class RewardsBrowserTest; @@ -187,6 +188,8 @@ class BraveActionsContainer : public views::View, std::unique_ptr empty_extensions_container_; + OnionLocationView* onion_location_view_ = nullptr; + base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(BraveActionsContainer); diff --git a/browser/ui/views/location_bar/onion_location_view.cc b/browser/ui/views/location_bar/onion_location_view.cc new file mode 100644 index 000000000000..c273cfa7d465 --- /dev/null +++ b/browser/ui/views/location_bar/onion_location_view.cc @@ -0,0 +1,53 @@ +/* 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/. */ + +#include "brave/browser/ui/views/location_bar/onion_location_view.h" + +#include "base/strings/utf_string_conversions.h" +#include "brave/browser/tor/onion_location_tab_helper.h" +#include "brave/browser/profiles/profile_util.h" +#include "chrome/browser/profiles/profile_window.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_finder.h" + +OnionLocationView::OnionLocationView() + : LabelButton(this, base::ASCIIToUTF16("Open in Tor")) { + // label()->SetBackgroundColor(SkColorSetRGB(98,60,129)); + SetVisible(false); +} + +OnionLocationView::~OnionLocationView() {} + +void OnionLocationView::Update(content::WebContents* web_contents) { + if (!web_contents) + return; + tor::OnionLocationTabHelper* helper = + tor::OnionLocationTabHelper::FromWebContents(web_contents); + if (!helper) + return; + SetVisible(helper->should_show_icon()); + onion_location_ = helper->onion_location(); +} + +void OnionLocationView::ButtonPressed(Button* sender, const ui::Event& event) { + profiles::SwitchToTorProfile(base::BindRepeating( + &OnionLocationView::OnTorProfileCreated, weak_ptr_factory_.GetWeakPtr(), + GURL(onion_location_))); +} + +void OnionLocationView::OnTorProfileCreated( + GURL onion_location, + Profile* profile, + Profile::CreateStatus status) { + if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) + return; + Browser* browser = chrome::FindTabbedBrowser(profile, true); + if (!browser) + return; + content::OpenURLParams open_tor(onion_location, content::Referrer(), + WindowOpenDisposition::OFF_THE_RECORD, + ui::PAGE_TRANSITION_TYPED, false); + browser->OpenURL(open_tor); +} diff --git a/browser/ui/views/location_bar/onion_location_view.h b/browser/ui/views/location_bar/onion_location_view.h new file mode 100644 index 000000000000..b2b9ecd59471 --- /dev/null +++ b/browser/ui/views/location_bar/onion_location_view.h @@ -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/. */ + +#ifndef BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ +#define BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ + +#include "base/memory/weak_ptr.h" +#include "chrome/browser/profiles/profile.h" +#include "ui/views/controls/button/label_button.h" + +namespace content { +class WebContents; +} // namespace content + +class OnionLocationView : public views::LabelButton, + public views::ButtonListener { + public: + OnionLocationView(); + ~OnionLocationView() override; + + void Update(content::WebContents* web_contents); + + // views::ButtonListener + void ButtonPressed(Button* sender, const ui::Event& event) override; + + private: + void OnTorProfileCreated( + GURL onion_location, + Profile* proile, Profile::CreateStatus status); + + GURL onion_location_; + + base::WeakPtrFactory weak_ptr_factory_{this}; + + OnionLocationView(const OnionLocationView&) = delete; + OnionLocationView& operator=(const OnionLocationView&) = delete; +}; + +#endif // BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ From 04e6ae4e03914561ac3007fa6accc02b18c6dc7d Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Mon, 5 Oct 2020 13:45:38 -0700 Subject: [PATCH 02/17] Add label icon and l10n --- app/brave_generated_resources.grd | 3 ++ .../brave_actions/brave_actions_container.cc | 15 +++---- .../views/location_bar/onion_location_view.cc | 38 ++++++++++++++---- components/img/toolbar/open-in-tor-window.png | Bin 0 -> 193 bytes .../resources/brave_components_resources.grd | 3 ++ 5 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 components/img/toolbar/open-in-tor-window.png diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index 0a339b419d39..f1fc0bc33cc2 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -325,6 +325,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U Open Tor Window + + Open in Tor + New Private Window with Tor diff --git a/browser/ui/views/brave_actions/brave_actions_container.cc b/browser/ui/views/brave_actions/brave_actions_container.cc index 9237917789d3..97d3befc162b 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.cc +++ b/browser/ui/views/brave_actions/brave_actions_container.cc @@ -137,21 +137,22 @@ void BraveActionsContainer::Init() { SetLayoutManager(std::move(vertical_container_layout)); // children + onion_location_view_ = new OnionLocationView(); + AddChildViewAt(onion_location_view_, 0); + RoundedSeparator* brave_button_separator_ = new RoundedSeparator(); // TODO(petemill): theme color brave_button_separator_->SetColor(SkColorSetRGB(0xb2, 0xb5, 0xb7)); - constexpr int kSeparatorRightMargin = 2; + constexpr int kSeparatorMargin = 3; constexpr int kSeparatorWidth = 1; brave_button_separator_->SetPreferredSize(gfx::Size( - kSeparatorWidth + kSeparatorRightMargin, + kSeparatorWidth + kSeparatorMargin*2, GetLayoutConstant(LOCATION_BAR_ICON_SIZE))); - // separator right margin + // separator left & right margin brave_button_separator_->SetBorder( - views::CreateEmptyBorder(0, 0, 0, kSeparatorRightMargin)); - onion_location_view_ = new OnionLocationView(); - AddChildViewAt(onion_location_view_, 0); + views::CreateEmptyBorder(0, kSeparatorMargin, 0, kSeparatorMargin)); // Just in case the extensions load before this function does (not likely!) - // make sure separator is at index 0 + // make sure separator is at index 1 AddChildViewAt(brave_button_separator_, 1); // Populate actions actions_[brave_extension_id].position_ = 2; diff --git a/browser/ui/views/location_bar/onion_location_view.cc b/browser/ui/views/location_bar/onion_location_view.cc index c273cfa7d465..012ffdd1dbe1 100644 --- a/browser/ui/views/location_bar/onion_location_view.cc +++ b/browser/ui/views/location_bar/onion_location_view.cc @@ -6,16 +6,41 @@ #include "brave/browser/ui/views/location_bar/onion_location_view.h" #include "base/strings/utf_string_conversions.h" -#include "brave/browser/tor/onion_location_tab_helper.h" #include "brave/browser/profiles/profile_util.h" +#include "brave/browser/tor/onion_location_tab_helper.h" +#include "brave/grit/brave_generated_resources.h" #include "chrome/browser/profiles/profile_window.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_finder.h" +#include "chrome/browser/ui/views/toolbar/toolbar_ink_drop_util.h" +#include "components/grit/brave_components_resources.h" +#include "ui/base/l10n/l10n_util.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/gfx/image/image.h" +#include "ui/gfx/image/image_skia.h" +#include "ui/views/background.h" + +namespace { +constexpr SkColor kOpenInTorBg = SkColorSetRGB(0x6a, 0x37, 0x85); +} // namespace OnionLocationView::OnionLocationView() - : LabelButton(this, base::ASCIIToUTF16("Open in Tor")) { - // label()->SetBackgroundColor(SkColorSetRGB(98,60,129)); + : LabelButton(this, + l10n_util::GetStringUTF16((IDS_LOCATION_BAR_OPEN_IN_TOR))) { + SetBackground(views::CreateSolidBackground(kOpenInTorBg)); + ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); + gfx::ImageSkia image; + const SkBitmap bitmap = + rb.GetImageNamed(IDR_BRAVE_OPEN_IN_TOR_WINDOW_IMG).AsBitmap(); + image.AddRepresentation(gfx::ImageSkiaRep(bitmap, 1)); + SetImage(views::Button::STATE_NORMAL, image); + SetImageLabelSpacing(6); SetVisible(false); + + SetInkDropMode(InkDropMode::ON); + set_has_ink_drop_action_on_click(true); + SetHorizontalAlignment(gfx::ALIGN_CENTER); + set_ink_drop_visible_opacity(kToolbarInkDropVisibleOpacity); } OnionLocationView::~OnionLocationView() {} @@ -37,10 +62,9 @@ void OnionLocationView::ButtonPressed(Button* sender, const ui::Event& event) { GURL(onion_location_))); } -void OnionLocationView::OnTorProfileCreated( - GURL onion_location, - Profile* profile, - Profile::CreateStatus status) { +void OnionLocationView::OnTorProfileCreated(GURL onion_location, + Profile* profile, + Profile::CreateStatus status) { if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) return; Browser* browser = chrome::FindTabbedBrowser(profile, true); diff --git a/components/img/toolbar/open-in-tor-window.png b/components/img/toolbar/open-in-tor-window.png new file mode 100644 index 0000000000000000000000000000000000000000..e87672c5b9f387ae016e4fa7526a7ad8dd2d786d GIT binary patch literal 193 zcmeAS@N?(olHy`uVBq!ia0vp^JRr=$1|-8uW1a&k&H|6fVg?3oVGw3ym^DWND9BhG z + + + From 2fcf8417186c3f2a8f0b5597d733837c334c4e66 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Mon, 5 Oct 2020 16:31:25 -0700 Subject: [PATCH 03/17] Add prefs for auto onion location redirect --- app/brave_generated_resources.grd | 6 ++++++ browser/brave_content_browser_client.cc | 2 +- browser/brave_profile_prefs.cc | 9 +++++++++ .../api/settings_private/brave_prefs_util.cc | 9 +++++++++ .../brave_default_extensions_page.html | 7 +++++++ browser/tor/onion_location_navigation_throttle.cc | 13 +++++++++---- browser/tor/onion_location_navigation_throttle.h | 4 ++-- browser/tor/tor_profile_service.cc | 5 +++++ browser/tor/tor_profile_service.h | 1 + .../settings/settings_localized_strings_provider.cc | 4 ++++ common/tor/pref_names.cc | 2 ++ common/tor/pref_names.h | 3 +++ 12 files changed, 58 insertions(+), 7 deletions(-) diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index f1fc0bc33cc2..126a8678c900 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -196,6 +196,12 @@ Tor hides your IP address from the sites you visit. + + Automatically redirect .onion sites + + + Brave will open onion service of the website in Tor window when available + Install Widevine diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index c37af2200857..0109d7870674 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -463,7 +463,7 @@ BraveContentBrowserClient::CreateThrottlesForNavigation( throttles.push_back(std::move(tor_navigation_throttle)); std::unique_ptr onion_location_navigation_throttle = - tor::OnionLocationNavigationThrottle::CreateThrottleFor(handle); + tor::OnionLocationNavigationThrottle::MaybeCreateThrottleFor(handle); if (onion_location_navigation_throttle) throttles.push_back(std::move(onion_location_navigation_throttle)); #endif diff --git a/browser/brave_profile_prefs.cc b/browser/brave_profile_prefs.cc index acd42a7816cc..4b79541f5ab1 100644 --- a/browser/brave_profile_prefs.cc +++ b/browser/brave_profile_prefs.cc @@ -7,6 +7,7 @@ #include "brave/browser/search/ntp_utils.h" #include "brave/browser/themes/brave_dark_mode_utils.h" +#include "brave/browser/tor/buildflags.h" #include "brave/browser/ui/omnibox/brave_omnibox_client_impl.h" #include "brave/common/pref_names.h" #include "brave/components/binance/browser/buildflags/buildflags.h" @@ -87,6 +88,10 @@ #include "brave/components/crypto_dot_com/common/pref_names.h" #endif +#if BUILDFLAG(ENABLE_TOR) +#include "brave/browser/tor/tor_profile_service.h" +#endif + #if defined(OS_ANDROID) #include "components/feed/core/shared_prefs/pref_names.h" #include "components/ntp_tiles/pref_names.h" @@ -297,6 +302,10 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { crypto_dot_com::RegisterPrefs(registry); #endif +#if BUILDFLAG(ENABLE_TOR) + tor::TorProfileService::RegisterPrefs(registry); +#endif + #if !defined(OS_ANDROID) BraveOmniboxClientImpl::RegisterPrefs(registry); #endif diff --git a/browser/extensions/api/settings_private/brave_prefs_util.cc b/browser/extensions/api/settings_private/brave_prefs_util.cc index 21337114036e..81ecfd038700 100644 --- a/browser/extensions/api/settings_private/brave_prefs_util.cc +++ b/browser/extensions/api/settings_private/brave_prefs_util.cc @@ -5,6 +5,7 @@ #include "brave/browser/extensions/api/settings_private/brave_prefs_util.h" +#include "brave/browser/tor/buildflags.h" #include "brave/common/pref_names.h" #include "brave/components/brave_rewards/common/pref_names.h" #include "brave/components/brave_wallet/browser/buildflags/buildflags.h" @@ -39,6 +40,10 @@ #include "brave/components/crypto_dot_com/common/pref_names.h" #endif +#if BUILDFLAG(ENABLE_TOR) +#include "brave/common/tor/pref_names.h" +#endif + namespace extensions { using ntp_background_images::prefs::kNewTabPageShowBackgroundImage; @@ -185,6 +190,10 @@ const PrefsUtil::TypedPrefMap& BravePrefsUtil::GetWhitelistedKeys() { // Omnibox pref (*s_brave_whitelist)[omnibox::kPreventUrlElisionsInOmnibox] = settings_api::PrefType::PREF_TYPE_BOOLEAN; +#if BUILDFLAG(ENABLE_TOR) + (*s_brave_whitelist)[tor::prefs::kAutoOnionLocation] = + settings_api::PrefType::PREF_TYPE_BOOLEAN; +#endif return *s_brave_whitelist; } 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 f2f52c35b718..df31a8dea00c 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 @@ -136,6 +136,13 @@ disabled="[[disableTorOption_]]" on-settings-boolean-control-change="onTorEnabledChange_"> + + -OnionLocationNavigationThrottle::CreateThrottleFor( +OnionLocationNavigationThrottle::MaybeCreateThrottleFor( content::NavigationHandle* navigation_handle) { + if (tor::TorProfileService::IsTorDisabled()) + return nullptr; return std::make_unique(navigation_handle); } @@ -55,9 +59,10 @@ OnionLocationNavigationThrottle::WillProcessResponse() { std::string onion_location; if (headers && GetOnionLocation(headers, &onion_location) && !navigation_handle()->GetURL().DomainIs("onion")) { - // TODO(darkdh): add pref for always redirect when onion available - // If we are in tor window, open onion site directly - if (brave::IsTorProfile(profile_)) { + // If we are in tor window, open onion site directly or user prefers + // opening it automatically + if (brave::IsTorProfile(profile_) || + profile_->GetPrefs()->GetBoolean(prefs::kAutoOnionLocation)) { profiles::SwitchToTorProfile(base::BindRepeating( &OnionLocationNavigationThrottle::OnTorProfileCreated, weak_ptr_factory_.GetWeakPtr(), GURL(onion_location))); diff --git a/browser/tor/onion_location_navigation_throttle.h b/browser/tor/onion_location_navigation_throttle.h index 35e2fd6b0e86..3529671ce21b 100644 --- a/browser/tor/onion_location_navigation_throttle.h +++ b/browser/tor/onion_location_navigation_throttle.h @@ -22,8 +22,8 @@ namespace tor { class OnionLocationNavigationThrottle : public content::NavigationThrottle { public: - static std::unique_ptr CreateThrottleFor( - content::NavigationHandle* navigation_handle); + static std::unique_ptr + MaybeCreateThrottleFor(content::NavigationHandle* navigation_handle); explicit OnionLocationNavigationThrottle( content::NavigationHandle* navigation_handle); ~OnionLocationNavigationThrottle() override; diff --git a/browser/tor/tor_profile_service.cc b/browser/tor/tor_profile_service.cc index 2e845cae84f5..4cb42490a02f 100644 --- a/browser/tor/tor_profile_service.cc +++ b/browser/tor/tor_profile_service.cc @@ -29,6 +29,11 @@ void TorProfileService::RegisterLocalStatePrefs(PrefRegistrySimple* registry) { registry->RegisterBooleanPref(prefs::kTorDisabled, false); } +// static +void TorProfileService::RegisterPrefs(PrefRegistrySimple* registry) { + registry->RegisterBooleanPref(prefs::kAutoOnionLocation, false); +} + // static bool TorProfileService::IsTorDisabled() { if (!g_brave_browser_process) diff --git a/browser/tor/tor_profile_service.h b/browser/tor/tor_profile_service.h index e1d35f67e0d7..79b04d43c808 100644 --- a/browser/tor/tor_profile_service.h +++ b/browser/tor/tor_profile_service.h @@ -40,6 +40,7 @@ class TorProfileService : public KeyedService { ~TorProfileService() override; static void RegisterLocalStatePrefs(PrefRegistrySimple* registry); + static void RegisterPrefs(PrefRegistrySimple* registry); static void SetTorDisabled(bool disabled); static bool IsTorDisabled(); static void RegisterTorClientUpdater(); diff --git a/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc b/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc index 71fbcea0aaf9..22d4600a5460 100644 --- a/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc +++ b/chromium_src/chrome/browser/ui/webui/settings/settings_localized_strings_provider.cc @@ -303,6 +303,10 @@ void BraveAddCommonStrings(content::WebUIDataSource* html_source, IDS_SETTINGS_ENABLE_TOR_TITLE}, {"torEnabledDesc", IDS_SETTINGS_ENABLE_TOR_DESC}, + {"autoOnionLocationLabel", + IDS_SETTINGS_AUTO_ONION_LOCATION_TITLE}, + {"autoOnionLocationDesc", + IDS_SETTINGS_AUTO_ONION_LOCATION_DESC}, {"restartNotice", IDS_SETTINGS_RESTART_NOTICE}, {"relaunchButtonLabel", diff --git a/common/tor/pref_names.cc b/common/tor/pref_names.cc index d1957ddeae57..25e609540c55 100644 --- a/common/tor/pref_names.cc +++ b/common/tor/pref_names.cc @@ -10,5 +10,7 @@ namespace prefs { const char kTorDisabled[] = "tor.tor_disabled"; +const char kAutoOnionLocation[] = "tor.auto_onion_location"; + } // namespace prefs } // namespace tor diff --git a/common/tor/pref_names.h b/common/tor/pref_names.h index 9019bde95d47..e8c7a87a0480 100644 --- a/common/tor/pref_names.h +++ b/common/tor/pref_names.h @@ -11,6 +11,9 @@ namespace prefs { extern const char kTorDisabled[]; +// Automatically open onion site in tor window when available +extern const char kAutoOnionLocation[]; + } // namespace prefs } // namespace tor From 31cb0d270f8881cfbb8e52faf5d6e6fc09f94f9c Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Tue, 6 Oct 2020 16:43:01 -0700 Subject: [PATCH 04/17] Fix OnTorProfileCreated lifetime issue --- .../tor/onion_location_navigation_throttle.cc | 44 +++++++++---------- .../tor/onion_location_navigation_throttle.h | 7 --- .../views/location_bar/onion_location_view.cc | 35 ++++++++------- .../views/location_bar/onion_location_view.h | 7 --- 4 files changed, 40 insertions(+), 53 deletions(-) diff --git a/browser/tor/onion_location_navigation_throttle.cc b/browser/tor/onion_location_navigation_throttle.cc index 183b4d0229d1..6098e9c72103 100644 --- a/browser/tor/onion_location_navigation_throttle.cc +++ b/browser/tor/onion_location_navigation_throttle.cc @@ -23,6 +23,7 @@ namespace tor { namespace { + bool GetOnionLocation(const net::HttpResponseHeaders* headers, std::string* onion_location) { onion_location->clear(); @@ -32,6 +33,21 @@ bool GetOnionLocation(const net::HttpResponseHeaders* headers, return false; return true; } + +void OnTorProfileCreated(GURL onion_location, + Profile* profile, + Profile::CreateStatus status) { + if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) + return; + Browser* browser = chrome::FindTabbedBrowser(profile, true); + if (!browser) + return; + content::OpenURLParams open_tor(onion_location, content::Referrer(), + WindowOpenDisposition::OFF_THE_RECORD, + ui::PAGE_TRANSITION_TYPED, false); + browser->OpenURL(open_tor); +} + } // namespace // static @@ -63,9 +79,8 @@ OnionLocationNavigationThrottle::WillProcessResponse() { // opening it automatically if (brave::IsTorProfile(profile_) || profile_->GetPrefs()->GetBoolean(prefs::kAutoOnionLocation)) { - profiles::SwitchToTorProfile(base::BindRepeating( - &OnionLocationNavigationThrottle::OnTorProfileCreated, - weak_ptr_factory_.GetWeakPtr(), GURL(onion_location))); + profiles::SwitchToTorProfile( + base::BindRepeating(&OnTorProfileCreated, GURL(onion_location))); } else { OnionLocationTabHelper::SetOnionLocation( navigation_handle()->GetWebContents(), GURL(onion_location)); @@ -82,10 +97,10 @@ content::NavigationThrottle::ThrottleCheckResult OnionLocationNavigationThrottle::WillStartRequest() { if (!brave::IsTorProfile(profile_)) { GURL url = navigation_handle()->GetURL(); - if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion")) { - profiles::SwitchToTorProfile(base::BindRepeating( - &OnionLocationNavigationThrottle::OnTorProfileCreated, - weak_ptr_factory_.GetWeakPtr(), std::move(url))); + if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion") && + navigation_handle()->IsInMainFrame()) { + profiles::SwitchToTorProfile( + base::BindRepeating(&OnTorProfileCreated, std::move(url))); return content::NavigationThrottle::CANCEL_AND_IGNORE; } } @@ -96,19 +111,4 @@ const char* OnionLocationNavigationThrottle::GetNameForLogging() { return "OnionLocationNavigationThrottle"; } -void OnionLocationNavigationThrottle::OnTorProfileCreated( - GURL onion_location, - Profile* profile, - Profile::CreateStatus status) { - if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) - return; - Browser* browser = chrome::FindTabbedBrowser(profile, true); - if (!browser) - return; - content::OpenURLParams open_tor(onion_location, content::Referrer(), - WindowOpenDisposition::OFF_THE_RECORD, - ui::PAGE_TRANSITION_TYPED, false); - browser->OpenURL(open_tor); -} - } // namespace tor diff --git a/browser/tor/onion_location_navigation_throttle.h b/browser/tor/onion_location_navigation_throttle.h index 3529671ce21b..77827cd17d8e 100644 --- a/browser/tor/onion_location_navigation_throttle.h +++ b/browser/tor/onion_location_navigation_throttle.h @@ -8,7 +8,6 @@ #include -#include "base/memory/weak_ptr.h" #include "chrome/browser/profiles/profile.h" #include "content/public/browser/navigation_throttle.h" @@ -34,14 +33,8 @@ class OnionLocationNavigationThrottle : public content::NavigationThrottle { const char* GetNameForLogging() override; private: - void OnTorProfileCreated(GURL onion_location, - Profile* proile, - Profile::CreateStatus status); - Profile* profile_; - base::WeakPtrFactory weak_ptr_factory_{this}; - OnionLocationNavigationThrottle(const OnionLocationNavigationThrottle&) = delete; OnionLocationNavigationThrottle& operator=( diff --git a/browser/ui/views/location_bar/onion_location_view.cc b/browser/ui/views/location_bar/onion_location_view.cc index 012ffdd1dbe1..549f4dce45ee 100644 --- a/browser/ui/views/location_bar/onion_location_view.cc +++ b/browser/ui/views/location_bar/onion_location_view.cc @@ -21,7 +21,23 @@ #include "ui/views/background.h" namespace { + constexpr SkColor kOpenInTorBg = SkColorSetRGB(0x6a, 0x37, 0x85); + +void OnTorProfileCreated(GURL onion_location, + Profile* profile, + Profile::CreateStatus status) { + if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) + return; + Browser* browser = chrome::FindTabbedBrowser(profile, true); + if (!browser) + return; + content::OpenURLParams open_tor(onion_location, content::Referrer(), + WindowOpenDisposition::OFF_THE_RECORD, + ui::PAGE_TRANSITION_TYPED, false); + browser->OpenURL(open_tor); +} + } // namespace OnionLocationView::OnionLocationView() @@ -57,21 +73,6 @@ void OnionLocationView::Update(content::WebContents* web_contents) { } void OnionLocationView::ButtonPressed(Button* sender, const ui::Event& event) { - profiles::SwitchToTorProfile(base::BindRepeating( - &OnionLocationView::OnTorProfileCreated, weak_ptr_factory_.GetWeakPtr(), - GURL(onion_location_))); -} - -void OnionLocationView::OnTorProfileCreated(GURL onion_location, - Profile* profile, - Profile::CreateStatus status) { - if (status != Profile::CreateStatus::CREATE_STATUS_INITIALIZED) - return; - Browser* browser = chrome::FindTabbedBrowser(profile, true); - if (!browser) - return; - content::OpenURLParams open_tor(onion_location, content::Referrer(), - WindowOpenDisposition::OFF_THE_RECORD, - ui::PAGE_TRANSITION_TYPED, false); - browser->OpenURL(open_tor); + profiles::SwitchToTorProfile( + base::BindRepeating(&OnTorProfileCreated, GURL(onion_location_))); } diff --git a/browser/ui/views/location_bar/onion_location_view.h b/browser/ui/views/location_bar/onion_location_view.h index b2b9ecd59471..103b8523abf6 100644 --- a/browser/ui/views/location_bar/onion_location_view.h +++ b/browser/ui/views/location_bar/onion_location_view.h @@ -6,7 +6,6 @@ #ifndef BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ #define BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ -#include "base/memory/weak_ptr.h" #include "chrome/browser/profiles/profile.h" #include "ui/views/controls/button/label_button.h" @@ -26,14 +25,8 @@ class OnionLocationView : public views::LabelButton, void ButtonPressed(Button* sender, const ui::Event& event) override; private: - void OnTorProfileCreated( - GURL onion_location, - Profile* proile, Profile::CreateStatus status); - GURL onion_location_; - base::WeakPtrFactory weak_ptr_factory_{this}; - OnionLocationView(const OnionLocationView&) = delete; OnionLocationView& operator=(const OnionLocationView&) = delete; }; From ba2de916e3b49d1d185f1449e04948aad40f187d Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Tue, 6 Oct 2020 18:37:36 -0700 Subject: [PATCH 05/17] Add browser test for OnionLocationNavigationThrottle --- ...ocation_navigation_throttle_browsertest.cc | 124 ++++++++++++++++++ test/BUILD.gn | 1 + 2 files changed, 125 insertions(+) create mode 100644 browser/tor/onion_location_navigation_throttle_browsertest.cc diff --git a/browser/tor/onion_location_navigation_throttle_browsertest.cc b/browser/tor/onion_location_navigation_throttle_browsertest.cc new file mode 100644 index 000000000000..b3d5a7725aa1 --- /dev/null +++ b/browser/tor/onion_location_navigation_throttle_browsertest.cc @@ -0,0 +1,124 @@ +/* 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/. */ + +#include "brave/browser/profiles/profile_util.h" +#include "brave/browser/tor/onion_location_tab_helper.h" +#include "brave/browser/ui/browser_commands.h" +#include "brave/common/tor/pref_names.h" +#include "chrome/browser/chrome_notification_types.h" +#include "chrome/browser/ui/browser.h" +#include "chrome/browser/ui/browser_list.h" +#include "chrome/test/base/in_process_browser_test.h" +#include "chrome/test/base/ui_test_utils.h" +#include "components/prefs/pref_service.h" +#include "content/public/browser/notification_service.h" +#include "content/public/test/browser_test.h" +#include "net/test/embedded_test_server/http_request.h" +#include "net/test/embedded_test_server/http_response.h" + +namespace { + +constexpr char kTestOnionPath[] = "/onion"; +constexpr char kTestOnionURL[] = "https://brave.onion"; + +std::unique_ptr HandleOnionLocation( + const net::test_server::HttpRequest& request) { + std::unique_ptr http_response( + new net::test_server::BasicHttpResponse()); + if (request.GetURL().path_piece() == kTestOnionPath) { + http_response->set_code(net::HTTP_OK); + http_response->set_content_type("text/html"); + http_response->set_content(""); + http_response->AddCustomHeader("onion-location", kTestOnionURL); + } else { + http_response->set_code(net::HTTP_OK); + http_response->set_content_type("text/html"); + http_response->set_content(""); + } + return std::move(http_response); +} +} // namespace + +class OnionLocationNavigationThrottleBrowserTest : public InProcessBrowserTest { + public: + void SetUpOnMainThread() override { + InProcessBrowserTest::SetUpOnMainThread(); + test_server_.reset(new net::EmbeddedTestServer( + net::test_server::EmbeddedTestServer::TYPE_HTTPS)); + test_server_->SetSSLConfig(net::EmbeddedTestServer::CERT_OK); + test_server_->RegisterRequestHandler(base::Bind(&HandleOnionLocation)); + ASSERT_TRUE(test_server_->Start()); + } + + net::EmbeddedTestServer* test_server() { return test_server_.get(); } + + private: + std::unique_ptr test_server_; +}; + +// TODO(darkdh): check label button +IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, + OnionLocationHeader) { + GURL url1 = test_server()->GetURL("/onion"); + ui_test_utils::NavigateToURL(browser(), url1); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + tor::OnionLocationTabHelper* helper = + tor::OnionLocationTabHelper::FromWebContents(web_contents); + EXPECT_TRUE(helper->should_show_icon()); + EXPECT_EQ(helper->onion_location(), GURL(kTestOnionURL)); + + GURL url2 = test_server()->GetURL("/no_onion"); + ui_test_utils::NavigateToURL(browser(), url2); + web_contents = browser()->tab_strip_model()->GetActiveWebContents(); + helper = tor::OnionLocationTabHelper::FromWebContents(web_contents); + EXPECT_FALSE(helper->should_show_icon()); + EXPECT_TRUE(helper->onion_location().is_empty()); +} + +IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, + OnionDomain) { + BrowserList* browser_list = BrowserList::GetInstance(); + ui_test_utils::NavigateToURL(browser(), GURL("https://brave.com")); + EXPECT_EQ(1U, browser_list->size()); + ASSERT_FALSE(brave::IsTorProfile(browser_list->get(0)->profile())); + + content::WindowedNotificationObserver tor_browser_creation_observer( + chrome::NOTIFICATION_BROWSER_OPENED, + content::NotificationService::AllSources()); + ui_test_utils::NavigateToURL(browser(), GURL(kTestOnionURL)); + tor_browser_creation_observer.Wait(); + EXPECT_EQ(2U, browser_list->size()); + ASSERT_TRUE(brave::IsTorProfile(browser_list->get(1)->profile())); + content::WebContents* web_contents = + browser_list->get(1)->tab_strip_model()->GetActiveWebContents(); + EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL)); +} + +IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, + AutoOnionLocationPref) { + browser()->profile()->GetPrefs()->SetBoolean(tor::prefs::kAutoOnionLocation, + true); + content::WindowedNotificationObserver tor_browser_creation_observer( + chrome::NOTIFICATION_BROWSER_OPENED, + content::NotificationService::AllSources()); + + GURL url = test_server()->GetURL("/onion"); + ui_test_utils::NavigateToURL(browser(), url); + tor_browser_creation_observer.Wait(); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + tor::OnionLocationTabHelper* helper = + tor::OnionLocationTabHelper::FromWebContents(web_contents); + EXPECT_FALSE(helper->should_show_icon()); + EXPECT_TRUE(helper->onion_location().is_empty()); + + BrowserList* browser_list = BrowserList::GetInstance(); + EXPECT_EQ(2U, browser_list->size()); + ASSERT_TRUE(brave::IsTorProfile(browser_list->get(1)->profile())); + web_contents = + browser_list->get(1)->tab_strip_model()->GetActiveWebContents(); + EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL)); +} diff --git a/test/BUILD.gn b/test/BUILD.gn index 0a427d1d4d64..dc516dd1d299 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -537,6 +537,7 @@ if (!is_android) { "//brave/browser/search_engines/search_engine_tracker_browsertest.cc", "//brave/browser/themes/brave_dark_mode_prefs_migration_browsertest.cc", "//brave/browser/themes/brave_theme_service_browsertest.cc", + "//brave/browser/tor/onion_location_navigation_throttle_browsertest.cc", "//brave/browser/ui/bookmark/bookmark_tab_helper_browsertest.cc", "//brave/browser/ui/brave_browser_command_controller_browsertest.cc", "//brave/browser/ui/content_settings/brave_autoplay_blocked_image_model_browsertest.cc", From 89c36b07bbcccfbf4eb4f027bbbb8b91eca6a896 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Wed, 7 Oct 2020 10:38:26 -0700 Subject: [PATCH 06/17] Check onion location label in browser test --- ...ocation_navigation_throttle_browsertest.cc | 45 ++++++++++++++++++- .../brave_actions/brave_actions_container.cc | 4 ++ .../brave_actions/brave_actions_container.h | 2 + 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/browser/tor/onion_location_navigation_throttle_browsertest.cc b/browser/tor/onion_location_navigation_throttle_browsertest.cc index b3d5a7725aa1..8bbc1fee45c8 100644 --- a/browser/tor/onion_location_navigation_throttle_browsertest.cc +++ b/browser/tor/onion_location_navigation_throttle_browsertest.cc @@ -3,13 +3,18 @@ * 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/utf_string_conversions.h" #include "brave/browser/profiles/profile_util.h" #include "brave/browser/tor/onion_location_tab_helper.h" #include "brave/browser/ui/browser_commands.h" +#include "brave/browser/ui/views/brave_actions/brave_actions_container.h" +#include "brave/browser/ui/views/location_bar/brave_location_bar_view.h" +#include "brave/grit/brave_generated_resources.h" #include "brave/common/tor/pref_names.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_list.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/prefs/pref_service.h" @@ -54,11 +59,26 @@ class OnionLocationNavigationThrottleBrowserTest : public InProcessBrowserTest { net::EmbeddedTestServer* test_server() { return test_server_.get(); } + void CheckOnionLocationLabel(Browser* browser) { + BrowserView* browser_view = BrowserView::GetBrowserViewForBrowser(browser); + ASSERT_NE(browser_view, nullptr); + BraveLocationBarView* brave_location_bar_view = + static_cast(browser_view->GetLocationBarView()); + ASSERT_NE(brave_location_bar_view, nullptr); + BraveActionsContainer* brave_actions = + brave_location_bar_view->GetBraveActionsContainer(); + ASSERT_NE(brave_actions, nullptr); + views::LabelButton* onion_label = + brave_actions->GetOnionLocationViewForTest(); + EXPECT_TRUE(onion_label->GetVisible()); + EXPECT_EQ(onion_label->GetText(), + l10n_util::GetStringUTF16((IDS_LOCATION_BAR_OPEN_IN_TOR))); + } + private: std::unique_ptr test_server_; }; -// TODO(darkdh): check label button IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, OnionLocationHeader) { GURL url1 = test_server()->GetURL("/onion"); @@ -69,6 +89,7 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, tor::OnionLocationTabHelper::FromWebContents(web_contents); EXPECT_TRUE(helper->should_show_icon()); EXPECT_EQ(helper->onion_location(), GURL(kTestOnionURL)); + CheckOnionLocationLabel(browser()); GURL url2 = test_server()->GetURL("/no_onion"); ui_test_utils::NavigateToURL(browser(), url2); @@ -97,6 +118,28 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL)); } +IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, + OnionDomain_TorWindow) { + content::WindowedNotificationObserver tor_browser_creation_observer( + chrome::NOTIFICATION_BROWSER_OPENED, + content::NotificationService::AllSources()); + brave::NewOffTheRecordWindowTor(browser()); + tor_browser_creation_observer.Wait(); + + BrowserList* browser_list = BrowserList::GetInstance(); + Browser* tor_browser = browser_list->get(1); + ASSERT_TRUE(brave::IsTorProfile(tor_browser->profile())); + EXPECT_EQ(2U, browser_list->size()); + + ui_test_utils::NavigateToURL(browser(), GURL("https://brave.com")); + ui_test_utils::NavigateToURL(browser(), GURL(kTestOnionURL)); + EXPECT_EQ(2U, browser_list->size()); + content::WebContents* web_contents = + tor_browser->tab_strip_model()->GetActiveWebContents(); + EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL)); + EXPECT_EQ(tor_browser->tab_strip_model()->count(), 2); +} + IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, AutoOnionLocationPref) { browser()->profile()->GetPrefs()->SetBoolean(tor::prefs::kAutoOnionLocation, diff --git a/browser/ui/views/brave_actions/brave_actions_container.cc b/browser/ui/views/brave_actions/brave_actions_container.cc index 97d3befc162b..69019e84e4f0 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.cc +++ b/browser/ui/views/brave_actions/brave_actions_container.cc @@ -448,6 +448,10 @@ void BraveActionsContainer::ChildPreferredSizeChanged(views::View* child) { PreferredSizeChanged(); } +views::LabelButton* BraveActionsContainer::GetOnionLocationViewForTest() { + return static_cast(onion_location_view_); +} + // Brave Rewards preferences change observers callback void BraveActionsContainer::OnBraveRewardsPreferencesChanged() { ShowAction(brave_rewards_extension_id, ShouldAddBraveRewardsAction()); diff --git a/browser/ui/views/brave_actions/brave_actions_container.h b/browser/ui/views/brave_actions/brave_actions_container.h index d800cb0707a9..a2bdfed6929a 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.h +++ b/browser/ui/views/brave_actions/brave_actions_container.h @@ -107,6 +107,8 @@ class BraveActionsContainer : public views::View, // views::View: void ChildPreferredSizeChanged(views::View* child) override; + views::LabelButton* GetOnionLocationViewForTest(); + private: friend class ::BraveActionsContainerTest; friend class ::RewardsBrowserTest; From e01eafe8157ad73156184b61fc3d9498d3df717b Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Wed, 7 Oct 2020 13:22:32 -0700 Subject: [PATCH 07/17] Use Onion Available label button in Tor window --- app/brave_generated_resources.grd | 3 +++ browser/tor/onion_location_navigation_throttle.cc | 6 ++---- browser/ui/views/brave_actions/brave_actions_container.cc | 2 +- browser/ui/views/location_bar/onion_location_view.cc | 6 ++++-- browser/ui/views/location_bar/onion_location_view.h | 4 +++- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd index 126a8678c900..e2de5cae32d9 100644 --- a/app/brave_generated_resources.grd +++ b/app/brave_generated_resources.grd @@ -334,6 +334,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U Open in Tor + + Onion Available + New Private Window with Tor diff --git a/browser/tor/onion_location_navigation_throttle.cc b/browser/tor/onion_location_navigation_throttle.cc index 6098e9c72103..b899fe6337b3 100644 --- a/browser/tor/onion_location_navigation_throttle.cc +++ b/browser/tor/onion_location_navigation_throttle.cc @@ -75,10 +75,8 @@ OnionLocationNavigationThrottle::WillProcessResponse() { std::string onion_location; if (headers && GetOnionLocation(headers, &onion_location) && !navigation_handle()->GetURL().DomainIs("onion")) { - // If we are in tor window, open onion site directly or user prefers - // opening it automatically - if (brave::IsTorProfile(profile_) || - profile_->GetPrefs()->GetBoolean(prefs::kAutoOnionLocation)) { + // If user prefers opening it automatically + if (profile_->GetPrefs()->GetBoolean(prefs::kAutoOnionLocation)) { profiles::SwitchToTorProfile( base::BindRepeating(&OnTorProfileCreated, GURL(onion_location))); } else { diff --git a/browser/ui/views/brave_actions/brave_actions_container.cc b/browser/ui/views/brave_actions/brave_actions_container.cc index 69019e84e4f0..322de738c41d 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.cc +++ b/browser/ui/views/brave_actions/brave_actions_container.cc @@ -137,7 +137,7 @@ void BraveActionsContainer::Init() { SetLayoutManager(std::move(vertical_container_layout)); // children - onion_location_view_ = new OnionLocationView(); + onion_location_view_ = new OnionLocationView(browser_->profile()); AddChildViewAt(onion_location_view_, 0); RoundedSeparator* brave_button_separator_ = new RoundedSeparator(); diff --git a/browser/ui/views/location_bar/onion_location_view.cc b/browser/ui/views/location_bar/onion_location_view.cc index 549f4dce45ee..2254fdab6b1a 100644 --- a/browser/ui/views/location_bar/onion_location_view.cc +++ b/browser/ui/views/location_bar/onion_location_view.cc @@ -40,9 +40,11 @@ void OnTorProfileCreated(GURL onion_location, } // namespace -OnionLocationView::OnionLocationView() +OnionLocationView::OnionLocationView(Profile* profile) : LabelButton(this, - l10n_util::GetStringUTF16((IDS_LOCATION_BAR_OPEN_IN_TOR))) { + l10n_util::GetStringUTF16(IDS_LOCATION_BAR_OPEN_IN_TOR)) { + if (brave::IsTorProfile(profile)) + SetText(l10n_util::GetStringUTF16(IDS_LOCATION_BAR_ONION_AVAILABLE)); SetBackground(views::CreateSolidBackground(kOpenInTorBg)); ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); gfx::ImageSkia image; diff --git a/browser/ui/views/location_bar/onion_location_view.h b/browser/ui/views/location_bar/onion_location_view.h index 103b8523abf6..0bebbb775bbf 100644 --- a/browser/ui/views/location_bar/onion_location_view.h +++ b/browser/ui/views/location_bar/onion_location_view.h @@ -9,6 +9,8 @@ #include "chrome/browser/profiles/profile.h" #include "ui/views/controls/button/label_button.h" +class Profile; + namespace content { class WebContents; } // namespace content @@ -16,7 +18,7 @@ class WebContents; class OnionLocationView : public views::LabelButton, public views::ButtonListener { public: - OnionLocationView(); + explicit OnionLocationView(Profile* profile); ~OnionLocationView() override; void Update(content::WebContents* web_contents); From 8faf100aea672ae1137e3886fe86af50d7ef22f9 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 8 Oct 2020 12:08:48 -0700 Subject: [PATCH 08/17] Address review feedbacks from iefremov --- .../brave_default_extensions_page.html | 10 ++--- browser/tor/BUILD.gn | 12 +++--- .../tor/onion_location_navigation_throttle.cc | 37 ++++++++++--------- .../brave_actions/brave_actions_container.cc | 1 - .../brave_actions/brave_actions_container.h | 2 +- .../views/location_bar/onion_location_view.h | 1 - 6 files changed, 31 insertions(+), 32 deletions(-) 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 df31a8dea00c..a417ee8826a3 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 @@ -137,11 +137,11 @@ on-settings-boolean-control-change="onTorEnabledChange_"> + pref="{{prefs.tor.auto_onion_location}}" + class="cr-row" + label="$i18n{autoOnionLocationLabel}" + sub-label="$i18n{autoOnionLocationDesc}" + disabled="[[!torEnabled_]]"> clear(); std::string name = "onion-location"; @@ -54,7 +56,8 @@ void OnTorProfileCreated(GURL onion_location, std::unique_ptr OnionLocationNavigationThrottle::MaybeCreateThrottleFor( content::NavigationHandle* navigation_handle) { - if (tor::TorProfileService::IsTorDisabled()) + if (tor::TorProfileService::IsTorDisabled() || + !navigation_handle->IsInMainFrame()) return nullptr; return std::make_unique(navigation_handle); } @@ -70,23 +73,24 @@ OnionLocationNavigationThrottle::~OnionLocationNavigationThrottle() {} content::NavigationThrottle::ThrottleCheckResult OnionLocationNavigationThrottle::WillProcessResponse() { - if (navigation_handle()->IsInMainFrame()) { - auto* headers = navigation_handle()->GetResponseHeaders(); - std::string onion_location; - if (headers && GetOnionLocation(headers, &onion_location) && - !navigation_handle()->GetURL().DomainIs("onion")) { - // If user prefers opening it automatically - if (profile_->GetPrefs()->GetBoolean(prefs::kAutoOnionLocation)) { - profiles::SwitchToTorProfile( - base::BindRepeating(&OnTorProfileCreated, GURL(onion_location))); - } else { - OnionLocationTabHelper::SetOnionLocation( - navigation_handle()->GetWebContents(), GURL(onion_location)); - } - } else { + auto* headers = navigation_handle()->GetResponseHeaders(); + std::string onion_location; + if (headers && GetOnionLocation(headers, &onion_location) && + !navigation_handle()->GetURL().DomainIs("onion")) { + // If user prefers opening it automatically + if (profile_->GetPrefs()->GetBoolean(prefs::kAutoOnionLocation)) { + // Cleanup previous label before user switching to kAutoOnionLocation OnionLocationTabHelper::SetOnionLocation( navigation_handle()->GetWebContents(), GURL()); + profiles::SwitchToTorProfile( + base::BindRepeating(&OnTorProfileCreated, GURL(onion_location))); + } else { + OnionLocationTabHelper::SetOnionLocation( + navigation_handle()->GetWebContents(), GURL(onion_location)); } + } else { + OnionLocationTabHelper::SetOnionLocation( + navigation_handle()->GetWebContents(), GURL()); } return content::NavigationThrottle::PROCEED; } @@ -95,8 +99,7 @@ content::NavigationThrottle::ThrottleCheckResult OnionLocationNavigationThrottle::WillStartRequest() { if (!brave::IsTorProfile(profile_)) { GURL url = navigation_handle()->GetURL(); - if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion") && - navigation_handle()->IsInMainFrame()) { + if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion")) { profiles::SwitchToTorProfile( base::BindRepeating(&OnTorProfileCreated, std::move(url))); return content::NavigationThrottle::CANCEL_AND_IGNORE; diff --git a/browser/ui/views/brave_actions/brave_actions_container.cc b/browser/ui/views/brave_actions/brave_actions_container.cc index 322de738c41d..2d1465358936 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.cc +++ b/browser/ui/views/brave_actions/brave_actions_container.cc @@ -39,7 +39,6 @@ #include "ui/views/layout/grid_layout.h" #include "ui/views/view.h" - class BraveActionsContainer::EmptyExtensionsContainer : public ExtensionsContainer { public: diff --git a/browser/ui/views/brave_actions/brave_actions_container.h b/browser/ui/views/brave_actions/brave_actions_container.h index a2bdfed6929a..fc7474ff8ea2 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.h +++ b/browser/ui/views/brave_actions/brave_actions_container.h @@ -27,8 +27,8 @@ #endif class BraveActionViewController; -class OnionLocationView; class BraveActionsContainerTest; +class OnionLocationView; class RewardsBrowserTest; namespace extensions { diff --git a/browser/ui/views/location_bar/onion_location_view.h b/browser/ui/views/location_bar/onion_location_view.h index 0bebbb775bbf..b6ec1c02aeec 100644 --- a/browser/ui/views/location_bar/onion_location_view.h +++ b/browser/ui/views/location_bar/onion_location_view.h @@ -6,7 +6,6 @@ #ifndef BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ #define BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ -#include "chrome/browser/profiles/profile.h" #include "ui/views/controls/button/label_button.h" class Profile; From 37aa25786657809a2db84888b030b05d0a8a3673 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 8 Oct 2020 16:05:08 -0700 Subject: [PATCH 09/17] Fix crash when automatically opening onion site and close the tor window at browser startup [8357:775:1008/110058.682124:FATAL:remote.h(97)] Check failed: is_bound(). Cannot issue Interface method calls on an unbound Remote 0 libbase.dylib 0x000000010eb7c7b9 base::debug::CollectStackTrace(void**, unsigned long) + 9 1 libbase.dylib 0x000000010ea67f43 base::debug::StackTrace::StackTrace() + 19 2 libbase.dylib 0x000000010ea8b075 logging::LogMessage::~LogMessage() + 261 3 libbase.dylib 0x000000010ea8bd5e logging::LogMessage::~LogMessage() + 14 4 libchrome_dll.dylib 0x0000000114832d6b TorLauncherFactory::OnTorControlCheckComplete() + 219 5 libchrome_dll.dylib 0x00000001148361cb base::internal::Invoker >, void ()>::RunOnce(base::internal::BindStateBase*) + 155 6 libbase.dylib 0x000000010eb09dbb base::TaskAnnotator::RunTask(char const*, base::PendingTask*) + 411 --- browser/tor/tor_launcher_factory.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/browser/tor/tor_launcher_factory.cc b/browser/tor/tor_launcher_factory.cc index d23221b903f6..a1e741e061cd 100644 --- a/browser/tor/tor_launcher_factory.cc +++ b/browser/tor/tor_launcher_factory.cc @@ -114,9 +114,13 @@ void TorLauncherFactory::LaunchTorProcess(const tor::TorConfig& config) { void TorLauncherFactory::OnTorControlCheckComplete() { DCHECK_CURRENTLY_ON(BrowserThread::UI); - tor_launcher_->Launch(config_, - base::BindOnce(&TorLauncherFactory::OnTorLaunched, - weak_ptr_factory_.GetWeakPtr())); + if (tor_launcher_.is_bound()) { + tor_launcher_->Launch(config_, + base::BindOnce(&TorLauncherFactory::OnTorLaunched, + weak_ptr_factory_.GetWeakPtr())); + } else { + is_starting_ = false; + } } void TorLauncherFactory::KillTorProcess() { From 5d74b40ad3eb8f8a67616bf2fc945b45832442b8 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Thu, 8 Oct 2020 16:31:32 -0700 Subject: [PATCH 10/17] Add TorDisabled test case --- ...ocation_navigation_throttle_browsertest.cc | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/browser/tor/onion_location_navigation_throttle_browsertest.cc b/browser/tor/onion_location_navigation_throttle_browsertest.cc index 8bbc1fee45c8..e79c915c88a5 100644 --- a/browser/tor/onion_location_navigation_throttle_browsertest.cc +++ b/browser/tor/onion_location_navigation_throttle_browsertest.cc @@ -6,6 +6,7 @@ #include "base/strings/utf_string_conversions.h" #include "brave/browser/profiles/profile_util.h" #include "brave/browser/tor/onion_location_tab_helper.h" +#include "brave/browser/tor/tor_profile_service.h" #include "brave/browser/ui/browser_commands.h" #include "brave/browser/ui/views/brave_actions/brave_actions_container.h" #include "brave/browser/ui/views/location_bar/brave_location_bar_view.h" @@ -165,3 +166,30 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, browser_list->get(1)->tab_strip_model()->GetActiveWebContents(); EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL)); } + +IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, + TorDisabled) { + // Disable tor + tor::TorProfileService::SetTorDisabled(true); + + // OnionLocationHeader_ + GURL url = test_server()->GetURL("/onion"); + ui_test_utils::NavigateToURL(browser(), url); + content::WebContents* web_contents = + browser()->tab_strip_model()->GetActiveWebContents(); + tor::OnionLocationTabHelper* helper = + tor::OnionLocationTabHelper::FromWebContents(web_contents); + EXPECT_FALSE(helper->should_show_icon()); + EXPECT_TRUE(helper->onion_location().is_empty()); + + // Onion Domain + ui_test_utils::NavigateToURL(browser(), GURL(kTestOnionURL)); + BrowserList* browser_list = BrowserList::GetInstance(); + EXPECT_EQ(1U, browser_list->size()); + + // AutoOnionLocationPref + browser()->profile()->GetPrefs()->SetBoolean(tor::prefs::kAutoOnionLocation, + true); + ui_test_utils::NavigateToURL(browser(), url); + EXPECT_EQ(1U, browser_list->size()); +} From 6c12465eddd62ae898c910b3db6bd3fffcf44602 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Fri, 9 Oct 2020 13:42:20 -0700 Subject: [PATCH 11/17] Close original tab after auto onion redirect if it is not last tab of the window --- .../tor/onion_location_navigation_throttle.cc | 7 +++--- ...ocation_navigation_throttle_browsertest.cc | 23 +++++++++++++++---- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/browser/tor/onion_location_navigation_throttle.cc b/browser/tor/onion_location_navigation_throttle.cc index 9f419544359e..86c196c7013c 100644 --- a/browser/tor/onion_location_navigation_throttle.cc +++ b/browser/tor/onion_location_navigation_throttle.cc @@ -79,11 +79,12 @@ OnionLocationNavigationThrottle::WillProcessResponse() { !navigation_handle()->GetURL().DomainIs("onion")) { // If user prefers opening it automatically if (profile_->GetPrefs()->GetBoolean(prefs::kAutoOnionLocation)) { - // Cleanup previous label before user switching to kAutoOnionLocation - OnionLocationTabHelper::SetOnionLocation( - navigation_handle()->GetWebContents(), GURL()); profiles::SwitchToTorProfile( base::BindRepeating(&OnTorProfileCreated, GURL(onion_location))); + // We do not close last tab of the window + Browser* browser = chrome::FindBrowserWithProfile(profile_); + if (browser && browser->tab_strip_model()->count() > 1) + navigation_handle()->GetWebContents()->ClosePage(); } else { OnionLocationTabHelper::SetOnionLocation( navigation_handle()->GetWebContents(), GURL(onion_location)); diff --git a/browser/tor/onion_location_navigation_throttle_browsertest.cc b/browser/tor/onion_location_navigation_throttle_browsertest.cc index e79c915c88a5..62d4dba5532c 100644 --- a/browser/tor/onion_location_navigation_throttle_browsertest.cc +++ b/browser/tor/onion_location_navigation_throttle_browsertest.cc @@ -10,8 +10,8 @@ #include "brave/browser/ui/browser_commands.h" #include "brave/browser/ui/views/brave_actions/brave_actions_container.h" #include "brave/browser/ui/views/location_bar/brave_location_bar_view.h" -#include "brave/grit/brave_generated_resources.h" #include "brave/common/tor/pref_names.h" +#include "brave/grit/brave_generated_resources.h" #include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_list.h" @@ -152,6 +152,8 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, GURL url = test_server()->GetURL("/onion"); ui_test_utils::NavigateToURL(browser(), url); tor_browser_creation_observer.Wait(); + // Last tab will not be closed + EXPECT_EQ(browser()->tab_strip_model()->count(), 1); content::WebContents* web_contents = browser()->tab_strip_model()->GetActiveWebContents(); tor::OnionLocationTabHelper* helper = @@ -161,9 +163,22 @@ IN_PROC_BROWSER_TEST_F(OnionLocationNavigationThrottleBrowserTest, BrowserList* browser_list = BrowserList::GetInstance(); EXPECT_EQ(2U, browser_list->size()); - ASSERT_TRUE(brave::IsTorProfile(browser_list->get(1)->profile())); - web_contents = - browser_list->get(1)->tab_strip_model()->GetActiveWebContents(); + Browser* tor_browser = browser_list->get(1); + ASSERT_TRUE(brave::IsTorProfile(tor_browser->profile())); + web_contents = tor_browser->tab_strip_model()->GetActiveWebContents(); + EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL)); + + // Open a new tab + NavigateParams params( + NavigateParams(browser(), url, ui::PAGE_TRANSITION_TYPED)); + params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; + ui_test_utils::NavigateToURL(¶ms); + + EXPECT_EQ(browser()->tab_strip_model()->count(), 1); + + EXPECT_EQ(2U, browser_list->size()); + web_contents = tor_browser->tab_strip_model()->GetWebContentsAt(2); + EXPECT_EQ(tor_browser->tab_strip_model()->count(), 3); EXPECT_EQ(web_contents->GetURL(), GURL(kTestOnionURL)); } From f14d87a8e6a9326009d89301212f9d375b0abf39 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Mon, 12 Oct 2020 12:06:25 -0700 Subject: [PATCH 12/17] Move OnionLocationView to BraveLocationBarView --- ...ocation_navigation_throttle_browsertest.cc | 9 +++------ .../brave_actions/brave_actions_container.cc | 15 +++----------- .../brave_actions/brave_actions_container.h | 5 ----- .../location_bar/brave_location_bar_view.cc | 20 ++++++++++++++++--- .../location_bar/brave_location_bar_view.h | 3 +++ .../views/location_bar/location_bar_view.cc | 8 +++++--- .../ui/views/location_bar/location_bar_view.h | 2 +- ...ws-location_bar-location_bar_view.cc.patch | 4 ++-- 8 files changed, 34 insertions(+), 32 deletions(-) diff --git a/browser/tor/onion_location_navigation_throttle_browsertest.cc b/browser/tor/onion_location_navigation_throttle_browsertest.cc index 62d4dba5532c..3c28d152ac68 100644 --- a/browser/tor/onion_location_navigation_throttle_browsertest.cc +++ b/browser/tor/onion_location_navigation_throttle_browsertest.cc @@ -8,8 +8,8 @@ #include "brave/browser/tor/onion_location_tab_helper.h" #include "brave/browser/tor/tor_profile_service.h" #include "brave/browser/ui/browser_commands.h" -#include "brave/browser/ui/views/brave_actions/brave_actions_container.h" #include "brave/browser/ui/views/location_bar/brave_location_bar_view.h" +#include "brave/browser/ui/views/location_bar/onion_location_view.h" #include "brave/common/tor/pref_names.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/browser/chrome_notification_types.h" @@ -66,11 +66,8 @@ class OnionLocationNavigationThrottleBrowserTest : public InProcessBrowserTest { BraveLocationBarView* brave_location_bar_view = static_cast(browser_view->GetLocationBarView()); ASSERT_NE(brave_location_bar_view, nullptr); - BraveActionsContainer* brave_actions = - brave_location_bar_view->GetBraveActionsContainer(); - ASSERT_NE(brave_actions, nullptr); - views::LabelButton* onion_label = - brave_actions->GetOnionLocationViewForTest(); + OnionLocationView* onion_label = + brave_location_bar_view->GetOnionLocationView(); EXPECT_TRUE(onion_label->GetVisible()); EXPECT_EQ(onion_label->GetText(), l10n_util::GetStringUTF16((IDS_LOCATION_BAR_OPEN_IN_TOR))); diff --git a/browser/ui/views/brave_actions/brave_actions_container.cc b/browser/ui/views/brave_actions/brave_actions_container.cc index 2d1465358936..ac06d559fbb8 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.cc +++ b/browser/ui/views/brave_actions/brave_actions_container.cc @@ -15,7 +15,6 @@ #include "brave/browser/ui/brave_actions/brave_action_view_controller.h" #include "brave/browser/ui/views/brave_actions/brave_action_view.h" #include "brave/browser/ui/views/brave_actions/brave_rewards_action_stub_view.h" -#include "brave/browser/ui/views/location_bar/onion_location_view.h" #include "brave/browser/ui/views/rounded_separator.h" #include "brave/common/brave_switches.h" #include "brave/common/pref_names.h" @@ -136,9 +135,6 @@ void BraveActionsContainer::Init() { SetLayoutManager(std::move(vertical_container_layout)); // children - onion_location_view_ = new OnionLocationView(browser_->profile()); - AddChildViewAt(onion_location_view_, 0); - RoundedSeparator* brave_button_separator_ = new RoundedSeparator(); // TODO(petemill): theme color brave_button_separator_->SetColor(SkColorSetRGB(0xb2, 0xb5, 0xb7)); @@ -151,10 +147,10 @@ void BraveActionsContainer::Init() { brave_button_separator_->SetBorder( views::CreateEmptyBorder(0, kSeparatorMargin, 0, kSeparatorMargin)); // Just in case the extensions load before this function does (not likely!) - // make sure separator is at index 1 - AddChildViewAt(brave_button_separator_, 1); + // make sure separator is at index 0 + AddChildViewAt(brave_button_separator_, 0); // Populate actions - actions_[brave_extension_id].position_ = 2; + actions_[brave_extension_id].position_ = 1; actions_[brave_rewards_extension_id].position_ = ACTION_ANY_POSITION; // React to Brave Rewards preferences changes. @@ -316,7 +312,6 @@ void BraveActionsContainer::Update() { // only show separator if we're showing any buttons const bool visible = !should_hide_ && can_show; SetVisible(visible); - onion_location_view_->Update(GetCurrentWebContents()); Layout(); } @@ -447,10 +442,6 @@ void BraveActionsContainer::ChildPreferredSizeChanged(views::View* child) { PreferredSizeChanged(); } -views::LabelButton* BraveActionsContainer::GetOnionLocationViewForTest() { - return static_cast(onion_location_view_); -} - // Brave Rewards preferences change observers callback void BraveActionsContainer::OnBraveRewardsPreferencesChanged() { ShowAction(brave_rewards_extension_id, ShouldAddBraveRewardsAction()); diff --git a/browser/ui/views/brave_actions/brave_actions_container.h b/browser/ui/views/brave_actions/brave_actions_container.h index fc7474ff8ea2..d635b63324b2 100644 --- a/browser/ui/views/brave_actions/brave_actions_container.h +++ b/browser/ui/views/brave_actions/brave_actions_container.h @@ -28,7 +28,6 @@ class BraveActionViewController; class BraveActionsContainerTest; -class OnionLocationView; class RewardsBrowserTest; namespace extensions { @@ -107,8 +106,6 @@ class BraveActionsContainer : public views::View, // views::View: void ChildPreferredSizeChanged(views::View* child) override; - views::LabelButton* GetOnionLocationViewForTest(); - private: friend class ::BraveActionsContainerTest; friend class ::RewardsBrowserTest; @@ -190,8 +187,6 @@ class BraveActionsContainer : public views::View, std::unique_ptr empty_extensions_container_; - OnionLocationView* onion_location_view_ = nullptr; - base::WeakPtrFactory weak_ptr_factory_; DISALLOW_COPY_AND_ASSIGN(BraveActionsContainer); diff --git a/browser/ui/views/location_bar/brave_location_bar_view.cc b/browser/ui/views/location_bar/brave_location_bar_view.cc index f7e1ec1ad9c4..f8410e03a7b9 100644 --- a/browser/ui/views/location_bar/brave_location_bar_view.cc +++ b/browser/ui/views/location_bar/brave_location_bar_view.cc @@ -6,10 +6,13 @@ #include "brave/browser/ui/views/location_bar/brave_location_bar_view.h" #include +#include +#include #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" @@ -49,8 +52,10 @@ void BraveLocationBarView::Init() { BraveLocationBarViewFocusRingHighlightPathGenerator>()); } // brave action buttons + onion_location_view_ = new OnionLocationView(browser_->profile()); 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); @@ -61,7 +66,12 @@ void BraveLocationBarView::Init() { } void BraveLocationBarView::Layout() { - LocationBarView::Layout(brave_actions_ ? brave_actions_ : nullptr); + std::vector views; + if (onion_location_view_) + views.push_back(onion_location_view_); + if (brave_actions_) + views.push_back(brave_actions_); + LocationBarView::Layout(std::move(views)); } void BraveLocationBarView::Update(content::WebContents* contents) { @@ -70,6 +80,8 @@ void BraveLocationBarView::Update(content::WebContents* contents) { if (brave_actions_) { brave_actions_->Update(); } + if (onion_location_view_) + onion_location_view_->Update(contents); LocationBarView::Update(contents); } @@ -80,6 +92,9 @@ void BraveLocationBarView::OnChanged() { ShouldHidePageActionIcons() && !omnibox_view_->GetText().empty(); brave_actions_->SetShouldHide(should_hide); } + if (onion_location_view_) + onion_location_view_->Update( + browser_->tab_strip_model()->GetActiveWebContents()); // OnChanged calls Layout LocationBarView::OnChanged(); @@ -136,6 +151,5 @@ BraveLocationBarView::GetContentSettingsImageViewForTesting(size_t idx) { // header via a patch. This should never be called as the only instantiated // implementation should be our |BraveLocationBarView|. void LocationBarView::Layout() { - Layout(nullptr); + Layout(std::vector()); } - diff --git a/browser/ui/views/location_bar/brave_location_bar_view.h b/browser/ui/views/location_bar/brave_location_bar_view.h index 10c4573ea288..f2883ba7e5e9 100644 --- a/browser/ui/views/location_bar/brave_location_bar_view.h +++ b/browser/ui/views/location_bar/brave_location_bar_view.h @@ -10,6 +10,7 @@ class BraveActionsContainer; class BraveActionsContainerTest; +class OnionLocationView; class RewardsBrowserTest; class SkPath; @@ -23,6 +24,7 @@ class BraveLocationBarView : public LocationBarView { void Update(content::WebContents* contents) override; void OnChanged() override; BraveActionsContainer* GetBraveActionsContainer() { return brave_actions_; } + OnionLocationView* GetOnionLocationView() { return onion_location_view_; } // views::View: gfx::Size CalculatePreferredSize() const override; @@ -38,6 +40,7 @@ class BraveLocationBarView : public LocationBarView { friend class ::BraveActionsContainerTest; friend class ::RewardsBrowserTest; BraveActionsContainer* brave_actions_ = nullptr; + OnionLocationView* onion_location_view_ = nullptr; DISALLOW_COPY_AND_ASSIGN(BraveLocationBarView); }; diff --git a/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.cc b/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.cc index c2c2d6650fea..0a53b4dff45c 100644 --- a/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.cc +++ b/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.cc @@ -5,9 +5,11 @@ #include "brave/browser/ui/omnibox/brave_omnibox_client_impl.h" -#define BRAVE_LAYOUT_TRAILING_DECORATIONS \ - if (right_most && right_most->GetVisible()) \ - trailing_decorations.AddDecoration(0, height(), false, 0, 0, right_most); +#define BRAVE_LAYOUT_TRAILING_DECORATIONS \ + for (auto it = right_most.rbegin(); it != right_most.rend(); it++) { \ + if ((*it)->GetVisible()) \ + trailing_decorations.AddDecoration(0, height(), false, 0, 0, *it); \ + } #define ChromeOmniboxClient BraveOmniboxClientImpl #include "../../../../../../../chrome/browser/ui/views/location_bar/location_bar_view.cc" diff --git a/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.h b/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.h index 29b2ec2935a2..f1a79e3fed3b 100644 --- a/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.h +++ b/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.h @@ -11,7 +11,7 @@ friend class BraveLocationBarView; \ \ public: \ - void Layout(views::View* trailing_view); + void Layout(std::vector trailing_views); #define GetBorderRadius virtual GetBorderRadius #include "../../../../../../../chrome/browser/ui/views/location_bar/location_bar_view.h" diff --git a/patches/chrome-browser-ui-views-location_bar-location_bar_view.cc.patch b/patches/chrome-browser-ui-views-location_bar-location_bar_view.cc.patch index db6b06112aa9..1decfcceddae 100644 --- a/patches/chrome-browser-ui-views-location_bar-location_bar_view.cc.patch +++ b/patches/chrome-browser-ui-views-location_bar-location_bar_view.cc.patch @@ -1,5 +1,5 @@ diff --git a/chrome/browser/ui/views/location_bar/location_bar_view.cc b/chrome/browser/ui/views/location_bar/location_bar_view.cc -index 5d2b59f118785cf8265afd0d37d18985f5df8fa6..cf7467edf1e0cdd88599b064b04f52a587ce1805 100644 +index 5d2b59f118785cf8265afd0d37d18985f5df8fa6..4cd739ed73c92bfd2131eda44cccc3a964fa9e13 100644 --- a/chrome/browser/ui/views/location_bar/location_bar_view.cc +++ b/chrome/browser/ui/views/location_bar/location_bar_view.cc @@ -461,7 +461,7 @@ void LocationBarView::OnKeywordFaviconFetched(const gfx::Image& icon) { @@ -7,7 +7,7 @@ index 5d2b59f118785cf8265afd0d37d18985f5df8fa6..cf7467edf1e0cdd88599b064b04f52a5 } -void LocationBarView::Layout() { -+void LocationBarView::Layout(views::View* right_most) { ++void LocationBarView::Layout(std::vector right_most) { if (!IsInitialized()) return; From 4494e6a5ad0c897dca1636a3442750b414254436 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Mon, 12 Oct 2020 12:46:36 -0700 Subject: [PATCH 13/17] Remove unused WebContentsObserve from OnionLocationTabHelper --- browser/tor/onion_location_tab_helper.cc | 3 +-- browser/tor/onion_location_tab_helper.h | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/browser/tor/onion_location_tab_helper.cc b/browser/tor/onion_location_tab_helper.cc index a2f81b50cf4e..05fb3144a54f 100644 --- a/browser/tor/onion_location_tab_helper.cc +++ b/browser/tor/onion_location_tab_helper.cc @@ -20,8 +20,7 @@ void OnionLocationTabHelper::SetOnionLocation( } OnionLocationTabHelper::OnionLocationTabHelper( - content::WebContents* web_contents) - : content::WebContentsObserver(web_contents) {} + content::WebContents* web_contents) {} WEB_CONTENTS_USER_DATA_KEY_IMPL(OnionLocationTabHelper) diff --git a/browser/tor/onion_location_tab_helper.h b/browser/tor/onion_location_tab_helper.h index b8491db6a04d..c5ab89e91fa8 100644 --- a/browser/tor/onion_location_tab_helper.h +++ b/browser/tor/onion_location_tab_helper.h @@ -6,14 +6,12 @@ #ifndef BRAVE_BROWSER_TOR_ONION_LOCATION_TAB_HELPER_H_ #define BRAVE_BROWSER_TOR_ONION_LOCATION_TAB_HELPER_H_ -#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_user_data.h" namespace tor { class OnionLocationTabHelper - : public content::WebContentsObserver, - public content::WebContentsUserData { + : public content::WebContentsUserData { public: ~OnionLocationTabHelper() override; From 165c706d7e8df7c1e401916ce09705b95b55423c Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Mon, 12 Oct 2020 14:13:27 -0700 Subject: [PATCH 14/17] Simplify adding brave trailing views to LocationBarView::Layout() --- .../location_bar/brave_location_bar_view.cc | 28 +++++++------------ .../location_bar/brave_location_bar_view.h | 6 +++- .../views/location_bar/location_bar_view.cc | 5 ++++ .../ui/views/location_bar/location_bar_view.h | 2 +- ...ws-location_bar-location_bar_view.cc.patch | 11 +------- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/browser/ui/views/location_bar/brave_location_bar_view.cc b/browser/ui/views/location_bar/brave_location_bar_view.cc index f8410e03a7b9..043246e19892 100644 --- a/browser/ui/views/location_bar/brave_location_bar_view.cc +++ b/browser/ui/views/location_bar/brave_location_bar_view.cc @@ -6,8 +6,6 @@ #include "brave/browser/ui/views/location_bar/brave_location_bar_view.h" #include -#include -#include #include "brave/browser/profiles/profile_util.h" #include "brave/browser/themes/brave_theme_service.h" @@ -65,15 +63,6 @@ void BraveLocationBarView::Init() { content_setting_view->disable_animation(); } -void BraveLocationBarView::Layout() { - std::vector views; - if (onion_location_view_) - views.push_back(onion_location_view_); - if (brave_actions_) - views.push_back(brave_actions_); - LocationBarView::Layout(std::move(views)); -} - void BraveLocationBarView::Update(content::WebContents* contents) { // base Init calls update before our Init is run, so our children // may not be initialized yet @@ -100,6 +89,16 @@ void BraveLocationBarView::OnChanged() { LocationBarView::OnChanged(); } +std::vector BraveLocationBarView::GetTrailingViews() { + std::vector views; + if (onion_location_view_) + views.push_back(onion_location_view_); + if (brave_actions_) + views.push_back(brave_actions_); + + return views; +} + gfx::Size BraveLocationBarView::CalculatePreferredSize() const { gfx::Size min_size = LocationBarView::CalculatePreferredSize(); if (brave_actions_ && brave_actions_->GetVisible()) { @@ -146,10 +145,3 @@ BraveLocationBarView::GetContentSettingsImageViewForTesting(size_t idx) { DCHECK(idx < content_setting_views_.size()); return content_setting_views_[idx]; } - -// Provide base class implementation for Update override that has been added to -// header via a patch. This should never be called as the only instantiated -// implementation should be our |BraveLocationBarView|. -void LocationBarView::Layout() { - Layout(std::vector()); -} diff --git a/browser/ui/views/location_bar/brave_location_bar_view.h b/browser/ui/views/location_bar/brave_location_bar_view.h index f2883ba7e5e9..539b2752b6d4 100644 --- a/browser/ui/views/location_bar/brave_location_bar_view.h +++ b/browser/ui/views/location_bar/brave_location_bar_view.h @@ -6,6 +6,8 @@ #ifndef BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_BRAVE_LOCATION_BAR_VIEW_H_ #define BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_BRAVE_LOCATION_BAR_VIEW_H_ +#include + #include "chrome/browser/ui/views/location_bar/location_bar_view.h" class BraveActionsContainer; @@ -20,12 +22,14 @@ class BraveLocationBarView : public LocationBarView { public: using LocationBarView::LocationBarView; void Init() override; - void Layout() override; void Update(content::WebContents* contents) override; void OnChanged() override; BraveActionsContainer* GetBraveActionsContainer() { return brave_actions_; } OnionLocationView* GetOnionLocationView() { return onion_location_view_; } + // LocationBarView: + std::vector GetTrailingViews() override; + // views::View: gfx::Size CalculatePreferredSize() const override; void OnThemeChanged() override; diff --git a/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.cc b/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.cc index 0a53b4dff45c..300a6907d677 100644 --- a/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.cc +++ b/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.cc @@ -6,6 +6,7 @@ #include "brave/browser/ui/omnibox/brave_omnibox_client_impl.h" #define BRAVE_LAYOUT_TRAILING_DECORATIONS \ + auto right_most = GetTrailingViews(); \ for (auto it = right_most.rbegin(); it != right_most.rend(); it++) { \ if ((*it)->GetVisible()) \ trailing_decorations.AddDecoration(0, height(), false, 0, 0, *it); \ @@ -15,3 +16,7 @@ #include "../../../../../../../chrome/browser/ui/views/location_bar/location_bar_view.cc" #undef ChromeOmniboxClient #undef BRAVE_LAYOUT_TRAILING_DECORATIONS + +std::vector LocationBarView::GetTrailingViews() { + return std::vector(); +} diff --git a/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.h b/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.h index f1a79e3fed3b..6757a885b8b8 100644 --- a/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.h +++ b/chromium_src/chrome/browser/ui/views/location_bar/location_bar_view.h @@ -11,7 +11,7 @@ friend class BraveLocationBarView; \ \ public: \ - void Layout(std::vector trailing_views); + virtual std::vector GetTrailingViews(); #define GetBorderRadius virtual GetBorderRadius #include "../../../../../../../chrome/browser/ui/views/location_bar/location_bar_view.h" diff --git a/patches/chrome-browser-ui-views-location_bar-location_bar_view.cc.patch b/patches/chrome-browser-ui-views-location_bar-location_bar_view.cc.patch index 1decfcceddae..10269b216786 100644 --- a/patches/chrome-browser-ui-views-location_bar-location_bar_view.cc.patch +++ b/patches/chrome-browser-ui-views-location_bar-location_bar_view.cc.patch @@ -1,16 +1,7 @@ diff --git a/chrome/browser/ui/views/location_bar/location_bar_view.cc b/chrome/browser/ui/views/location_bar/location_bar_view.cc -index 5d2b59f118785cf8265afd0d37d18985f5df8fa6..4cd739ed73c92bfd2131eda44cccc3a964fa9e13 100644 +index 5d2b59f118785cf8265afd0d37d18985f5df8fa6..03c733f97feb72dff446ea6db3ec89d346750499 100644 --- a/chrome/browser/ui/views/location_bar/location_bar_view.cc +++ b/chrome/browser/ui/views/location_bar/location_bar_view.cc -@@ -461,7 +461,7 @@ void LocationBarView::OnKeywordFaviconFetched(const gfx::Image& icon) { - selected_keyword_view_->SetCustomImage(icon); - } - --void LocationBarView::Layout() { -+void LocationBarView::Layout(std::vector right_most) { - if (!IsInitialized()) - return; - @@ -568,6 +568,7 @@ void LocationBarView::Layout() { } }; From 15b4acd44abe9f855d8602b7cf0c14aa31edc685 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Tue, 13 Oct 2020 11:30:25 -0700 Subject: [PATCH 15/17] nits fix and add comments for .onion handling --- browser/brave_content_browser_client.cc | 2 +- browser/tor/onion_location_navigation_throttle.cc | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc index 0109d7870674..ab084d7618ea 100644 --- a/browser/brave_content_browser_client.cc +++ b/browser/brave_content_browser_client.cc @@ -85,8 +85,8 @@ using extensions::ChromeContentBrowserClientExtensionsPart; #endif #if BUILDFLAG(ENABLE_TOR) -#include "brave/browser/tor/tor_navigation_throttle.h" #include "brave/browser/tor/onion_location_navigation_throttle.h" +#include "brave/browser/tor/tor_navigation_throttle.h" #endif #if BUILDFLAG(ENABLE_SPEEDREADER) diff --git a/browser/tor/onion_location_navigation_throttle.cc b/browser/tor/onion_location_navigation_throttle.cc index 86c196c7013c..698dcfba4cb1 100644 --- a/browser/tor/onion_location_navigation_throttle.cc +++ b/browser/tor/onion_location_navigation_throttle.cc @@ -75,6 +75,8 @@ content::NavigationThrottle::ThrottleCheckResult OnionLocationNavigationThrottle::WillProcessResponse() { auto* headers = navigation_handle()->GetResponseHeaders(); std::string onion_location; + // The webpage defining the Onion-Location header must not be an onionsite. + // https://gitweb.torproject.org/tor-browser-spec.git/plain/proposals/100-onion-location-header.txt if (headers && GetOnionLocation(headers, &onion_location) && !navigation_handle()->GetURL().DomainIs("onion")) { // If user prefers opening it automatically @@ -98,6 +100,7 @@ OnionLocationNavigationThrottle::WillProcessResponse() { content::NavigationThrottle::ThrottleCheckResult OnionLocationNavigationThrottle::WillStartRequest() { + // Open .onion site in Tor window if (!brave::IsTorProfile(profile_)) { GURL url = navigation_handle()->GetURL(); if (url.SchemeIsHTTPOrHTTPS() && url.DomainIs("onion")) { From f097016c1de4bd1751917d8ee2e7f74e05eab8b9 Mon Sep 17 00:00:00 2001 From: Pete Miller Date: Tue, 13 Oct 2020 21:10:55 -0700 Subject: [PATCH 16/17] "Open In Tor" icon gets rounded border, margin, and vector icon --- app/vector_icons/BUILD.gn | 1 + app/vector_icons/open_in_tor.icon | 38 +++++ .../location_bar/brave_location_bar_view.cc | 5 + .../views/location_bar/onion_location_view.cc | 136 ++++++++++++++---- .../views/location_bar/onion_location_view.h | 10 +- components/img/toolbar/open-in-tor-window.png | Bin 193 -> 0 bytes .../resources/brave_components_resources.grd | 3 - 7 files changed, 160 insertions(+), 33 deletions(-) create mode 100644 app/vector_icons/open_in_tor.icon delete mode 100644 components/img/toolbar/open-in-tor-window.png diff --git a/app/vector_icons/BUILD.gn b/app/vector_icons/BUILD.gn index 0d50cf6e966f..b436032efed3 100644 --- a/app/vector_icons/BUILD.gn +++ b/app/vector_icons/BUILD.gn @@ -9,6 +9,7 @@ aggregate_vector_icons("brave_vector_icons") { icons = [ "download_unlock.icon", + "open_in_tor.icon", "speedreader.icon", "speedreader_on_active.icon", "speedreader_on_inactive.icon", diff --git a/app/vector_icons/open_in_tor.icon b/app/vector_icons/open_in_tor.icon new file mode 100644 index 000000000000..0c358687e4e3 --- /dev/null +++ b/app/vector_icons/open_in_tor.icon @@ -0,0 +1,38 @@ +// 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/. + +CANVAS_DIMENSIONS, 12, +MOVE_TO, 11.5f, 10, +R_CUBIC_TO, -0.28f, 0, -0.5f, -0.22f, -0.5f, -0.5f, +V_LINE_TO, 1, +H_LINE_TO, 2.5f, +CUBIC_TO, 2.22f, 1, 2, 0.78f, 2, 0.5f, +R_CUBIC_TO, 0, -0.28f, 0.22f, -0.5f, 0.5f, -0.5f, +H_LINE_TO, 11, +R_CUBIC_TO, 0.55f, 0, 1, 0.45f, 1, 1, +R_V_LINE_TO, 8.5f, +R_CUBIC_TO, 0, 0.28f, -0.22f, 0.5f, -0.5f, 0.5f, +CLOSE, +MOVE_TO, 10, 3, +R_V_LINE_TO, 8, +R_CUBIC_TO, 0, 0.55f, -0.45f, 1, -1, 1, +H_LINE_TO, 1, +R_CUBIC_TO, -0.55f, 0, -1, -0.45f, -1, -1, +V_LINE_TO, 3, +R_CUBIC_TO, 0, -0.55f, 0.45f, -1, 1, -1, +R_H_LINE_TO, 8, +R_CUBIC_TO, 0.55f, 0, 1, 0.45f, 1, 1, +CLOSE, +R_MOVE_TO, -9, 8, +R_H_LINE_TO, 8, +R_LINE_TO, 0, -6, +H_LINE_TO, 1, +R_V_LINE_TO, 6, +CLOSE, +R_MOVE_TO, 0, -7, +R_H_LINE_TO, 8, +V_LINE_TO, 3, +H_LINE_TO, 1, +R_V_LINE_TO, 1, +CLOSE diff --git a/browser/ui/views/location_bar/brave_location_bar_view.cc b/browser/ui/views/location_bar/brave_location_bar_view.cc index 043246e19892..e1f6a3e636b3 100644 --- a/browser/ui/views/location_bar/brave_location_bar_view.cc +++ b/browser/ui/views/location_bar/brave_location_bar_view.cc @@ -107,6 +107,11 @@ gfx::Size BraveLocationBarView::CalculatePreferredSize() const { GetLayoutConstant(LOCATION_BAR_ELEMENT_PADDING); min_size.Enlarge(extra_width, 0); } + 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); + } return min_size; } diff --git a/browser/ui/views/location_bar/onion_location_view.cc b/browser/ui/views/location_bar/onion_location_view.cc index 2254fdab6b1a..5cbe75769477 100644 --- a/browser/ui/views/location_bar/onion_location_view.cc +++ b/browser/ui/views/location_bar/onion_location_view.cc @@ -5,24 +5,43 @@ #include "brave/browser/ui/views/location_bar/onion_location_view.h" +#include +#include + #include "base/strings/utf_string_conversions.h" +#include "brave/app/vector_icons/vector_icons.h" #include "brave/browser/profiles/profile_util.h" #include "brave/browser/tor/onion_location_tab_helper.h" #include "brave/grit/brave_generated_resources.h" #include "chrome/browser/profiles/profile_window.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_finder.h" +#include "chrome/browser/ui/layout_constants.h" #include "chrome/browser/ui/views/toolbar/toolbar_ink_drop_util.h" #include "components/grit/brave_components_resources.h" #include "ui/base/l10n/l10n_util.h" #include "ui/base/resource/resource_bundle.h" -#include "ui/gfx/image/image.h" +#include "ui/base/models/image_model.h" +#include "ui/gfx/color_palette.h" +#include "ui/gfx/geometry/size.h" #include "ui/gfx/image/image_skia.h" +#include "ui/gfx/paint_vector_icon.h" +#include "ui/gfx/text_constants.h" +#include "ui/views/animation/ink_drop_impl.h" #include "ui/views/background.h" +#include "ui/views/border.h" +#include "ui/views/controls/button/label_button.h" +#include "ui/views/controls/highlight_path_generator.h" +#include "ui/views/layout/box_layout.h" +#include "ui/views/painter.h" +#include "ui/views/view.h" namespace { constexpr SkColor kOpenInTorBg = SkColorSetRGB(0x6a, 0x37, 0x85); +constexpr SkColor kIconColor = SkColorSetRGB(0xf0, 0xf2, 0xff); +constexpr SkColor kTextColor = SK_ColorWHITE; +constexpr int kIconSize = 12; void OnTorProfileCreated(GURL onion_location, Profile* profile, @@ -38,27 +57,100 @@ void OnTorProfileCreated(GURL onion_location, browser->OpenURL(open_tor); } +// Sets the focus and ink drop highlight path to match the background +// along with it's corner radius. +class HighlightPathGenerator + : public views::HighlightPathGenerator { + public: + HighlightPathGenerator() = default; + + // views::HighlightPathGenerator: + SkPath GetHighlightPath(const views::View* view) override { + const gfx::Rect highlight_bounds = view->GetLocalBounds(); + const SkRect rect = RectToSkRect(highlight_bounds); + const int corner_radius = view->height() / 2; + return SkPath().addRoundRect(rect, corner_radius, corner_radius); + } + + private: + DISALLOW_COPY_AND_ASSIGN(HighlightPathGenerator); +}; + +class OnionLocationButtonView : public views::LabelButton, + public views::ButtonListener { + public: + explicit OnionLocationButtonView(Profile* profile) + : LabelButton(this, + l10n_util::GetStringUTF16(IDS_LOCATION_BAR_OPEN_IN_TOR)) { + if (brave::IsTorProfile(profile)) + SetText(l10n_util::GetStringUTF16(IDS_LOCATION_BAR_ONION_AVAILABLE)); + // Render vector icon + const gfx::ImageSkia image = gfx::CreateVectorIcon( + kOpenInTorIcon, kIconSize, kIconColor); + SetImageModel(views::Button::STATE_NORMAL, + ui::ImageModel::FromImageSkia(image)); + // Set style specifics + SetEnabledTextColors(kTextColor); + SetHorizontalAlignment(gfx::ALIGN_RIGHT); + SetImageLabelSpacing(6); + SetInkDropMode(InkDropMode::ON); + SetBorder(views::CreateEmptyBorder( + GetLayoutInsets(LOCATION_BAR_ICON_INTERIOR_PADDING))); + set_has_ink_drop_action_on_click(true); + set_ink_drop_visible_opacity(kToolbarInkDropVisibleOpacity); + UpdateBorder(); + // Ensure focus ring follows border + views::HighlightPathGenerator::Install( + this, std::make_unique()); + } + + ~OnionLocationButtonView() override { } + + // views::ButtonListener + void ButtonPressed(Button* sender, const ui::Event& event) override { + profiles::SwitchToTorProfile( + base::BindRepeating(&OnTorProfileCreated, GURL(onion_location_))); + } + + void SetOnionLocation(GURL location) { + onion_location_ = location; + } + + private: + // views::View + void Layout() override { + views::LabelButton::Layout(); + UpdateBorder(); + } + + void UpdateBorder() { + SetBackground( + views::CreateRoundedRectBackground(kOpenInTorBg, height() / 2)); + } + + GURL onion_location_; + + OnionLocationButtonView(const OnionLocationButtonView&) = delete; + OnionLocationButtonView& operator=(const OnionLocationButtonView&) = delete; +}; + } // namespace -OnionLocationView::OnionLocationView(Profile* profile) - : LabelButton(this, - l10n_util::GetStringUTF16(IDS_LOCATION_BAR_OPEN_IN_TOR)) { - if (brave::IsTorProfile(profile)) - SetText(l10n_util::GetStringUTF16(IDS_LOCATION_BAR_ONION_AVAILABLE)); - SetBackground(views::CreateSolidBackground(kOpenInTorBg)); - ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); - gfx::ImageSkia image; - const SkBitmap bitmap = - rb.GetImageNamed(IDR_BRAVE_OPEN_IN_TOR_WINDOW_IMG).AsBitmap(); - image.AddRepresentation(gfx::ImageSkiaRep(bitmap, 1)); - SetImage(views::Button::STATE_NORMAL, image); - SetImageLabelSpacing(6); +OnionLocationView::OnionLocationView(Profile* profile) { + SetBorder(views::CreateEmptyBorder(gfx::Insets(3, 3))); + // GetLayoutInsets(LOCATION_BAR_ICON_INTERIOR_PADDING))); SetVisible(false); + // automatic layout + auto vertical_container_layout = std::make_unique( + views::BoxLayout::Orientation::kHorizontal); + vertical_container_layout->set_main_axis_alignment( + views::BoxLayout::MainAxisAlignment::kCenter); + vertical_container_layout->set_cross_axis_alignment( + views::BoxLayout::CrossAxisAlignment::kCenter); + SetLayoutManager(std::move(vertical_container_layout)); - SetInkDropMode(InkDropMode::ON); - set_has_ink_drop_action_on_click(true); - SetHorizontalAlignment(gfx::ALIGN_CENTER); - set_ink_drop_visible_opacity(kToolbarInkDropVisibleOpacity); + button_ = new OnionLocationButtonView(profile); + AddChildView(button_); } OnionLocationView::~OnionLocationView() {} @@ -71,10 +163,6 @@ void OnionLocationView::Update(content::WebContents* web_contents) { if (!helper) return; SetVisible(helper->should_show_icon()); - onion_location_ = helper->onion_location(); -} - -void OnionLocationView::ButtonPressed(Button* sender, const ui::Event& event) { - profiles::SwitchToTorProfile( - base::BindRepeating(&OnTorProfileCreated, GURL(onion_location_))); + reinterpret_cast(button_)->SetOnionLocation( + helper->onion_location()); } diff --git a/browser/ui/views/location_bar/onion_location_view.h b/browser/ui/views/location_bar/onion_location_view.h index b6ec1c02aeec..19f43fced62e 100644 --- a/browser/ui/views/location_bar/onion_location_view.h +++ b/browser/ui/views/location_bar/onion_location_view.h @@ -7,6 +7,8 @@ #define BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ #include "ui/views/controls/button/label_button.h" +#include "ui/views/controls/button/label_button_border.h" +#include "ui/gfx/geometry/size.h" class Profile; @@ -14,19 +16,15 @@ namespace content { class WebContents; } // namespace content -class OnionLocationView : public views::LabelButton, - public views::ButtonListener { +class OnionLocationView : public views::View { public: explicit OnionLocationView(Profile* profile); ~OnionLocationView() override; void Update(content::WebContents* web_contents); - // views::ButtonListener - void ButtonPressed(Button* sender, const ui::Event& event) override; - private: - GURL onion_location_; + View* button_ = nullptr; OnionLocationView(const OnionLocationView&) = delete; OnionLocationView& operator=(const OnionLocationView&) = delete; diff --git a/components/img/toolbar/open-in-tor-window.png b/components/img/toolbar/open-in-tor-window.png deleted file mode 100644 index e87672c5b9f387ae016e4fa7526a7ad8dd2d786d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 193 zcmeAS@N?(olHy`uVBq!ia0vp^JRr=$1|-8uW1a&k&H|6fVg?3oVGw3ym^DWND9BhG z - - - From 008196fc1bd60bc341b4ff7aa47c2b931ee57a31 Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Tue, 13 Oct 2020 22:00:58 -0700 Subject: [PATCH 17/17] Fix browser test for new icon and clang-format --- ...ocation_navigation_throttle_browsertest.cc | 8 +++---- .../views/location_bar/onion_location_view.cc | 24 ++++++++----------- .../views/location_bar/onion_location_view.h | 6 +++-- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/browser/tor/onion_location_navigation_throttle_browsertest.cc b/browser/tor/onion_location_navigation_throttle_browsertest.cc index 3c28d152ac68..5b75795546b5 100644 --- a/browser/tor/onion_location_navigation_throttle_browsertest.cc +++ b/browser/tor/onion_location_navigation_throttle_browsertest.cc @@ -66,10 +66,10 @@ class OnionLocationNavigationThrottleBrowserTest : public InProcessBrowserTest { BraveLocationBarView* brave_location_bar_view = static_cast(browser_view->GetLocationBarView()); ASSERT_NE(brave_location_bar_view, nullptr); - OnionLocationView* onion_label = - brave_location_bar_view->GetOnionLocationView(); - EXPECT_TRUE(onion_label->GetVisible()); - EXPECT_EQ(onion_label->GetText(), + views::LabelButton* onion_button = + brave_location_bar_view->GetOnionLocationView()->GetButton(); + EXPECT_TRUE(onion_button->GetVisible()); + EXPECT_EQ(onion_button->GetText(), l10n_util::GetStringUTF16((IDS_LOCATION_BAR_OPEN_IN_TOR))); } diff --git a/browser/ui/views/location_bar/onion_location_view.cc b/browser/ui/views/location_bar/onion_location_view.cc index 5cbe75769477..5f7cbac451de 100644 --- a/browser/ui/views/location_bar/onion_location_view.cc +++ b/browser/ui/views/location_bar/onion_location_view.cc @@ -20,8 +20,8 @@ #include "chrome/browser/ui/views/toolbar/toolbar_ink_drop_util.h" #include "components/grit/brave_components_resources.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/base/resource/resource_bundle.h" #include "ui/base/models/image_model.h" +#include "ui/base/resource/resource_bundle.h" #include "ui/gfx/color_palette.h" #include "ui/gfx/geometry/size.h" #include "ui/gfx/image/image_skia.h" @@ -59,8 +59,7 @@ void OnTorProfileCreated(GURL onion_location, // Sets the focus and ink drop highlight path to match the background // along with it's corner radius. -class HighlightPathGenerator - : public views::HighlightPathGenerator { +class HighlightPathGenerator : public views::HighlightPathGenerator { public: HighlightPathGenerator() = default; @@ -77,18 +76,18 @@ class HighlightPathGenerator }; class OnionLocationButtonView : public views::LabelButton, - public views::ButtonListener { + public views::ButtonListener { public: explicit OnionLocationButtonView(Profile* profile) : LabelButton(this, - l10n_util::GetStringUTF16(IDS_LOCATION_BAR_OPEN_IN_TOR)) { + l10n_util::GetStringUTF16(IDS_LOCATION_BAR_OPEN_IN_TOR)) { if (brave::IsTorProfile(profile)) SetText(l10n_util::GetStringUTF16(IDS_LOCATION_BAR_ONION_AVAILABLE)); // Render vector icon - const gfx::ImageSkia image = gfx::CreateVectorIcon( - kOpenInTorIcon, kIconSize, kIconColor); + const gfx::ImageSkia image = + gfx::CreateVectorIcon(kOpenInTorIcon, kIconSize, kIconColor); SetImageModel(views::Button::STATE_NORMAL, - ui::ImageModel::FromImageSkia(image)); + ui::ImageModel::FromImageSkia(image)); // Set style specifics SetEnabledTextColors(kTextColor); SetHorizontalAlignment(gfx::ALIGN_RIGHT); @@ -104,7 +103,7 @@ class OnionLocationButtonView : public views::LabelButton, this, std::make_unique()); } - ~OnionLocationButtonView() override { } + ~OnionLocationButtonView() override {} // views::ButtonListener void ButtonPressed(Button* sender, const ui::Event& event) override { @@ -112,9 +111,7 @@ class OnionLocationButtonView : public views::LabelButton, base::BindRepeating(&OnTorProfileCreated, GURL(onion_location_))); } - void SetOnionLocation(GURL location) { - onion_location_ = location; - } + void SetOnionLocation(GURL location) { onion_location_ = location; } private: // views::View @@ -125,7 +122,7 @@ class OnionLocationButtonView : public views::LabelButton, void UpdateBorder() { SetBackground( - views::CreateRoundedRectBackground(kOpenInTorBg, height() / 2)); + views::CreateRoundedRectBackground(kOpenInTorBg, height() / 2)); } GURL onion_location_; @@ -138,7 +135,6 @@ class OnionLocationButtonView : public views::LabelButton, OnionLocationView::OnionLocationView(Profile* profile) { SetBorder(views::CreateEmptyBorder(gfx::Insets(3, 3))); - // GetLayoutInsets(LOCATION_BAR_ICON_INTERIOR_PADDING))); SetVisible(false); // automatic layout auto vertical_container_layout = std::make_unique( diff --git a/browser/ui/views/location_bar/onion_location_view.h b/browser/ui/views/location_bar/onion_location_view.h index 19f43fced62e..f0a2c5b7e7f7 100644 --- a/browser/ui/views/location_bar/onion_location_view.h +++ b/browser/ui/views/location_bar/onion_location_view.h @@ -6,9 +6,9 @@ #ifndef BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ #define BRAVE_BROWSER_UI_VIEWS_LOCATION_BAR_ONION_LOCATION_VIEW_H_ +#include "ui/gfx/geometry/size.h" #include "ui/views/controls/button/label_button.h" #include "ui/views/controls/button/label_button_border.h" -#include "ui/gfx/geometry/size.h" class Profile; @@ -23,8 +23,10 @@ class OnionLocationView : public views::View { void Update(content::WebContents* web_contents); + views::LabelButton* GetButton() { return button_; } + private: - View* button_ = nullptr; + views::LabelButton* button_ = nullptr; OnionLocationView(const OnionLocationView&) = delete; OnionLocationView& operator=(const OnionLocationView&) = delete;