From 326f666de6d119aec1eff20fa9d64e951834342c Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Sat, 9 Oct 2021 10:45:00 +0900 Subject: [PATCH] Fixed new tor window opens NTP when using open link tor menu fix https://github.com/brave/brave-browser/issues/11611 --- .../render_view_context_menu.cc | 56 ++++++++++++++++++- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc b/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc index 74ae6fc0f52f..c3db09e24c04 100644 --- a/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc +++ b/chromium_src/chrome/browser/renderer_context_menu/render_view_context_menu.cc @@ -13,6 +13,7 @@ #include "brave/components/tor/buildflags/buildflags.h" #include "brave/grit/brave_theme_resources.h" #include "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h" +#include "chrome/browser/ui/browser_list.h" #include "chrome/common/channel_info.h" #include "components/omnibox/browser/autocomplete_classifier.h" #include "components/omnibox/browser/autocomplete_controller.h" @@ -91,6 +92,52 @@ void RenderViewContextMenu::RegisterMenuShownCallbackForTesting( #undef RenderViewContextMenu #undef BRAVE_APPEND_SEARCH_PROVIDER +namespace { + +#if BUILDFLAG(ENABLE_TOR) +bool HasAlreadyOpenedTorWindow(Profile* profile) { + for (Browser* browser : *BrowserList::GetInstance()) { + if (browser->profile()->IsTor() && + browser->profile()->GetOriginalProfile() == profile) + return true; + } + + return false; +} + +// Modified OnProfileCreated() in render_view_context_menu.cc +// to handle additional |use_new_tab| param. +void OnTorProfileCreated(const GURL& link_url, + const content::Referrer& referrer, + bool use_new_tab, + Profile* profile, + Profile::CreateStatus status) { + if (status == Profile::CREATE_STATUS_INITIALIZED) { + Browser* browser = chrome::FindLastActiveWithProfile(profile); + /* |ui::PAGE_TRANSITION_TYPED| is used rather than + |ui::PAGE_TRANSITION_LINK| since this ultimately opens the link in + another browser. This parameter is used within the tab strip model of + the browser it opens in implying a link from the active tab in the + destination browser which is not correct. */ + NavigateParams nav_params(browser, link_url, ui::PAGE_TRANSITION_TYPED); + if (use_new_tab) { + nav_params.disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB; + } else { + // Stop NTP loading to show tab throbber wait spinning till tor is + // initialized. + browser->tab_strip_model()->GetActiveWebContents()->Stop(); + nav_params.disposition = WindowOpenDisposition::CURRENT_TAB; + } + nav_params.referrer = referrer; + nav_params.window_action = NavigateParams::SHOW_WINDOW; + Navigate(&nav_params); + } +} + +#endif + +} // namespace + BraveRenderViewContextMenu::BraveRenderViewContextMenu( content::RenderFrameHost* render_frame_host, const content::ContextMenuParams& params) @@ -176,14 +223,17 @@ void BraveRenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ExecuteIPFSCommand(id, event_flags); break; #endif +#if BUILDFLAG(ENABLE_TOR) case IDC_CONTENT_CONTEXT_OPENLINKTOR: TorProfileManager::SwitchToTorProfile( GetProfile(), base::BindRepeating( - OnProfileCreated, params_.link_url, - content::Referrer( - GURL(), network::mojom::ReferrerPolicy::kStrictOrigin))); + OnTorProfileCreated, params_.link_url, + content::Referrer(GURL(), + network::mojom::ReferrerPolicy::kStrictOrigin), + HasAlreadyOpenedTorWindow(GetProfile()))); break; +#endif default: RenderViewContextMenu_Chromium::ExecuteCommand(id, event_flags); }