Skip to content

Commit

Permalink
Implement open link in Tor profile in context menu
Browse files Browse the repository at this point in the history
  • Loading branch information
yrliou committed Jun 18, 2019
1 parent 6a3a0c0 commit 75216e1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
26 changes: 26 additions & 0 deletions chromium_src/chrome/browser/ui/browser_navigator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
#include "url/gurl.h"

namespace {

constexpr char kBraveIsTor[] = "brave_is_tor";

std::pair<Browser*, int> BraveGetBrowserAndTabForDisposition(
const NavigateParams& params);

void AdjustNavigateParamsForURLBraveImpl(NavigateParams* params) {
if (params->url.SchemeIs(content::kBraveUIScheme)) {
GURL::Replacements replacements;
Expand All @@ -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<Browser*, int> 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
14 changes: 12 additions & 2 deletions patches/chrome-browser-ui-browser_navigator.cc.patch
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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();
Expand Down

0 comments on commit 75216e1

Please sign in to comment.