Skip to content

Commit

Permalink
Close original tab after auto onion redirect if it is not last tab of…
Browse files Browse the repository at this point in the history
… the window
  • Loading branch information
darkdh committed Oct 9, 2020
1 parent c2bcc83 commit 14bc579
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
7 changes: 4 additions & 3 deletions browser/tor/onion_location_navigation_throttle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
23 changes: 19 additions & 4 deletions browser/tor/onion_location_navigation_throttle_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 =
Expand All @@ -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(&params);

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));
}

Expand Down

0 comments on commit 14bc579

Please sign in to comment.