From 14bc579819f42b1123da8d41b815b35244e90c0c Mon Sep 17 00:00:00 2001 From: Anthony Tseng Date: Fri, 9 Oct 2020 13:42:20 -0700 Subject: [PATCH] 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)); }