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 1d47d6c0421d..353e5882ed1f 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 @@ -27,6 +27,10 @@ // Make it clear which class we mean here. #undef RenderViewContextMenu +namespace { +constexpr char kBraveIsTor[] = "brave_is_tor"; +} // namespace + BraveRenderViewContextMenu::BraveRenderViewContextMenu( content::RenderFrameHost* render_frame_host, const content::ContextMenuParams& params) @@ -67,11 +71,20 @@ bool BraveRenderViewContextMenu::IsCommandIdEnabled(int id) const { void BraveRenderViewContextMenu::ExecuteCommand(int id, int event_flags) { switch (id) { case IDC_CONTENT_CONTEXT_OPENLINKTOR: - profiles::SwitchToTorProfile( - base::Bind( - OnProfileCreated, params_.link_url, - content::Referrer(GURL(), - network::mojom::ReferrerPolicy::kStrictOrigin))); + // To avoid patching a new disposition type and support it all over the + // code base which could introduce more patching, we use the + // extra_headers which was supposed to be an empty string to specify that + // we're going to open it in Tor profile. + // This string will be used in BraveGetBrowserAndTabForDisposition in + // browser_navigator.cc to open the new link in Tor profile window and + // it will be reset to an empty string right after returning from + // BraveGetBrowserAndTabForDisposition. + OpenURLWithExtraHeaders( + params_.link_url, GURL(), + WindowOpenDisposition::OFF_THE_RECORD, + ui::PAGE_TRANSITION_LINK, + kBraveIsTor /* extra_headers */, + true /* started_from_context_menu */); break; default: RenderViewContextMenu_Chromium::ExecuteCommand(id, event_flags); diff --git a/chromium_src/chrome/browser/ui/browser_navigator.cc b/chromium_src/chrome/browser/ui/browser_navigator.cc index c81d76d03225..cb63cf4ba0f0 100644 --- a/chromium_src/chrome/browser/ui/browser_navigator.cc +++ b/chromium_src/chrome/browser/ui/browser_navigator.cc @@ -10,6 +10,12 @@ #include "url/gurl.h" namespace { + +constexpr char kBraveIsTor[] = "brave_is_tor"; + +std::pair BraveGetBrowserAndTabForDisposition( + const NavigateParams& params); + void AdjustNavigateParamsForURLBraveImpl(NavigateParams* params) { if (params->url.SchemeIs(content::kBraveUIScheme)) { GURL::Replacements replacements; @@ -27,8 +33,28 @@ bool IsHostAllowedInIncognitoBraveImpl(const base::StringPiece& host) { return true; } + } // namespace #define ChromeNavigationUIData BraveNavigationUIData #include "../../../../chrome/browser/ui/browser_navigator.cc" // NOLINT #undef ChromeNavigationUIData + +namespace { + +std::pair BraveGetBrowserAndTabForDisposition( + const NavigateParams& params) { + Profile* profile = params.initiating_profile; + + // This temporary extra_headers will be reset to an empty string right after + // this function is returned. + if (params.extra_headers == kBraveIsTor) { + return {GetOrCreateBrowser(profile->GetTorProfile(), + params.user_gesture), + -1}; + } + + return GetBrowserAndTabForDisposition(params); +} + +} // namespace diff --git a/patches/chrome-browser-ui-browser_navigator.cc.patch b/patches/chrome-browser-ui-browser_navigator.cc.patch index d24bdc6ceabc..1b6ca2d6a142 100644 --- a/patches/chrome-browser-ui-browser_navigator.cc.patch +++ b/patches/chrome-browser-ui-browser_navigator.cc.patch @@ -1,5 +1,5 @@ diff --git a/chrome/browser/ui/browser_navigator.cc b/chrome/browser/ui/browser_navigator.cc -index 0d1221f617040499eeff4955458d6139b6baa1b2..6806bcaaef009be13f8c79d3a045bfdd09d72ca7 100644 +index 0d1221f617040499eeff4955458d6139b6baa1b2..8b60d5c786294f9abe2a940b3856410ce1b5dcb5 100644 --- a/chrome/browser/ui/browser_navigator.cc +++ b/chrome/browser/ui/browser_navigator.cc @@ -105,6 +105,7 @@ Browser* GetOrCreateBrowser(Profile* profile, bool user_gesture) { @@ -10,7 +10,17 @@ index 0d1221f617040499eeff4955458d6139b6baa1b2..6806bcaaef009be13f8c79d3a045bfdd if (params->contents_to_insert || params->switch_to_singleton_tab || IsURLAllowedInIncognito(params->url, params->initiating_profile) || params->initiating_profile->IsGuestSession()) { -@@ -724,6 +725,7 @@ void Navigate(NavigateParams* params) { +@@ -504,7 +505,8 @@ void Navigate(NavigateParams* params) { + } + int singleton_index; + std::tie(params->browser, singleton_index) = +- GetBrowserAndTabForDisposition(*params); ++ BraveGetBrowserAndTabForDisposition(*params); ++ if (params->extra_headers == kBraveIsTor) params->extra_headers = ""; + if (!params->browser) + return; + if (singleton_index != -1) { +@@ -724,6 +726,7 @@ void Navigate(NavigateParams* params) { bool IsHostAllowedInIncognito(const GURL& url) { std::string scheme = url.scheme(); base::StringPiece host = url.host_piece();