Commit 6e9a13fa authored by Peter Kasting's avatar Peter Kasting Committed by Commit Bot

Cleanup: Add "current tab disposition" argument to DispositionFromXXX() funcs.

Many callers want to do things like "if the link would open in the current tab,
open in a new foreground tab instead".  This provides a simple way to do that.

Bug: none
Change-Id: Ia49088031543905ad4d1d0122a8656c675a33e14
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1986122
Commit-Queue: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#728583}
parent 525de700
...@@ -473,18 +473,6 @@ bool IsCommandForOpenLink(int id) { ...@@ -473,18 +473,6 @@ bool IsCommandForOpenLink(int id) {
id <= IDC_OPEN_LINK_IN_PROFILE_LAST); id <= IDC_OPEN_LINK_IN_PROFILE_LAST);
} }
// Usually a new tab is expected where this function is used,
// however users should be able to open a tab in background
// or in a new window.
WindowOpenDisposition ForceNewTabDispositionFromEventFlags(
int event_flags) {
WindowOpenDisposition disposition =
ui::DispositionFromEventFlags(event_flags);
return disposition == WindowOpenDisposition::CURRENT_TAB
? WindowOpenDisposition::NEW_FOREGROUND_TAB
: disposition;
}
// Returns the preference of the profile represented by the |context|. // Returns the preference of the profile represented by the |context|.
PrefService* GetPrefs(content::BrowserContext* context) { PrefService* GetPrefs(content::BrowserContext* context) {
return user_prefs::UserPrefs::Get(context); return user_prefs::UserPrefs::Get(context);
...@@ -2400,7 +2388,8 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ...@@ -2400,7 +2388,8 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
case IDC_CONTENT_CONTEXT_SEARCHWEBFOR: case IDC_CONTENT_CONTEXT_SEARCHWEBFOR:
case IDC_CONTENT_CONTEXT_GOTOURL: case IDC_CONTENT_CONTEXT_GOTOURL:
OpenURL(selection_navigation_url_, GURL(), OpenURL(selection_navigation_url_, GURL(),
ForceNewTabDispositionFromEventFlags(event_flags), ui::DispositionFromEventFlags(
event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB),
ui::PAGE_TRANSITION_LINK); ui::PAGE_TRANSITION_LINK);
break; break;
...@@ -2724,8 +2713,8 @@ void RenderViewContextMenu::ExecProtocolHandler(int event_flags, ...@@ -2724,8 +2713,8 @@ void RenderViewContextMenu::ExecProtocolHandler(int event_flags,
base::RecordAction( base::RecordAction(
UserMetricsAction("RegisterProtocolHandler.ContextMenu_Open")); UserMetricsAction("RegisterProtocolHandler.ContextMenu_Open"));
WindowOpenDisposition disposition = WindowOpenDisposition disposition = ui::DispositionFromEventFlags(
ForceNewTabDispositionFromEventFlags(event_flags); event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB);
OpenURL(handlers[handler_index].TranslateUrl(params_.link_url), OpenURL(handlers[handler_index].TranslateUrl(params_.link_url),
GetDocumentURL(params_), GetDocumentURL(params_),
disposition, disposition,
...@@ -3027,8 +3016,8 @@ void RenderViewContextMenu::ExecTranslate() { ...@@ -3027,8 +3016,8 @@ void RenderViewContextMenu::ExecTranslate() {
} }
void RenderViewContextMenu::ExecLanguageSettings(int event_flags) { void RenderViewContextMenu::ExecLanguageSettings(int event_flags) {
WindowOpenDisposition disposition = WindowOpenDisposition disposition = ui::DispositionFromEventFlags(
ForceNewTabDispositionFromEventFlags(event_flags); event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB);
GURL url = chrome::GetSettingsUrl(chrome::kLanguageOptionsSubPage); GURL url = chrome::GetSettingsUrl(chrome::kLanguageOptionsSubPage);
OpenURL(url, GURL(), disposition, ui::PAGE_TRANSITION_LINK); OpenURL(url, GURL(), disposition, ui::PAGE_TRANSITION_LINK);
} }
...@@ -3036,8 +3025,8 @@ void RenderViewContextMenu::ExecLanguageSettings(int event_flags) { ...@@ -3036,8 +3025,8 @@ void RenderViewContextMenu::ExecLanguageSettings(int event_flags) {
void RenderViewContextMenu::ExecProtocolHandlerSettings(int event_flags) { void RenderViewContextMenu::ExecProtocolHandlerSettings(int event_flags) {
base::RecordAction( base::RecordAction(
UserMetricsAction("RegisterProtocolHandler.ContextMenu_Settings")); UserMetricsAction("RegisterProtocolHandler.ContextMenu_Settings"));
WindowOpenDisposition disposition = WindowOpenDisposition disposition = ui::DispositionFromEventFlags(
ForceNewTabDispositionFromEventFlags(event_flags); event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB);
GURL url = chrome::GetSettingsUrl(chrome::kHandlerSettingsSubPage); GURL url = chrome::GetSettingsUrl(chrome::kHandlerSettingsSubPage);
OpenURL(url, GURL(), disposition, ui::PAGE_TRANSITION_LINK); OpenURL(url, GURL(), disposition, ui::PAGE_TRANSITION_LINK);
} }
......
...@@ -791,11 +791,10 @@ void SearchTabHelper::OpenExtensionsPage(double button, ...@@ -791,11 +791,10 @@ void SearchTabHelper::OpenExtensionsPage(double button,
bool shift_key) { bool shift_key) {
if (!search::DefaultSearchProviderIsGoogle(profile())) if (!search::DefaultSearchProviderIsGoogle(profile()))
return; return;
bool middle_button = (button == 1.0); WindowOpenDisposition disposition =
WindowOpenDisposition disposition = ui::DispositionFromClick( (button > 1) ? WindowOpenDisposition::NEW_FOREGROUND_TAB
middle_button, alt_key, ctrl_key, meta_key, shift_key); : ui::DispositionFromClick((button == 1.0), alt_key,
if (button > 1) ctrl_key, meta_key, shift_key);
disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
web_contents_->OpenURL(content::OpenURLParams( web_contents_->OpenURL(content::OpenURLParams(
GURL(chrome::kChromeUIExtensionsURL), content::Referrer(), disposition, GURL(chrome::kChromeUIExtensionsURL), content::Referrer(), disposition,
ui::PAGE_TRANSITION_LINK, false)); ui::PAGE_TRANSITION_LINK, false));
......
...@@ -259,12 +259,8 @@ void RecentTabsSubMenuModel::ExecuteCommand(int command_id, int event_flags) { ...@@ -259,12 +259,8 @@ void RecentTabsSubMenuModel::ExecuteCommand(int command_id, int event_flags) {
DCHECK_NE(IDC_RECENT_TABS_NO_DEVICE_TABS, command_id); DCHECK_NE(IDC_RECENT_TABS_NO_DEVICE_TABS, command_id);
DCHECK(!IsDeviceNameCommandId(command_id)); DCHECK(!IsDeviceNameCommandId(command_id));
WindowOpenDisposition disposition = WindowOpenDisposition disposition = ui::DispositionFromEventFlags(
ui::DispositionFromEventFlags(event_flags); event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB);
if (disposition == WindowOpenDisposition::CURRENT_TAB) {
// Force to open a new foreground tab.
disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
}
sessions::TabRestoreService* service = sessions::TabRestoreService* service =
TabRestoreServiceFactory::GetForProfile(browser_->profile()); TabRestoreServiceFactory::GetForProfile(browser_->profile());
......
...@@ -161,14 +161,10 @@ void InvertBubbleView::ButtonPressed(views::Button* sender, ...@@ -161,14 +161,10 @@ void InvertBubbleView::ButtonPressed(views::Button* sender,
} }
void InvertBubbleView::OpenLink(const std::string& url, int event_flags) { void InvertBubbleView::OpenLink(const std::string& url, int event_flags) {
WindowOpenDisposition disposition = WindowOpenDisposition disposition = ui::DispositionFromEventFlags(
ui::DispositionFromEventFlags(event_flags); event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB);
content::OpenURLParams params( content::OpenURLParams params(GURL(url), content::Referrer(), disposition,
GURL(url), content::Referrer(), ui::PAGE_TRANSITION_LINK, false);
disposition == WindowOpenDisposition::CURRENT_TAB
? WindowOpenDisposition::NEW_FOREGROUND_TAB
: disposition,
ui::PAGE_TRANSITION_LINK, false);
browser_->OpenURL(params); browser_->OpenURL(params);
} }
......
...@@ -101,15 +101,12 @@ void NetworkProfileBubbleView::LinkClicked(views::Link* source, ...@@ -101,15 +101,12 @@ void NetworkProfileBubbleView::LinkClicked(views::Link* source,
int event_flags) { int event_flags) {
NetworkProfileBubble::RecordUmaEvent( NetworkProfileBubble::RecordUmaEvent(
NetworkProfileBubble::METRIC_LEARN_MORE_CLICKED); NetworkProfileBubble::METRIC_LEARN_MORE_CLICKED);
WindowOpenDisposition disposition = WindowOpenDisposition disposition = ui::DispositionFromEventFlags(
ui::DispositionFromEventFlags(event_flags); event_flags, WindowOpenDisposition::NEW_FOREGROUND_TAB);
content::OpenURLParams params( content::OpenURLParams params(
GURL("https://sites.google.com/a/chromium.org/dev/administrators/" GURL("https://sites.google.com/a/chromium.org/dev/administrators/"
"common-problems-and-solutions#network_profile"), "common-problems-and-solutions#network_profile"),
content::Referrer(), disposition == WindowOpenDisposition::CURRENT_TAB content::Referrer(), disposition, ui::PAGE_TRANSITION_LINK, false);
? WindowOpenDisposition::NEW_FOREGROUND_TAB
: disposition,
ui::PAGE_TRANSITION_LINK, false);
navigator_->OpenURL(params); navigator_->OpenURL(params);
// If the user interacted with the bubble we don't reduce the number of // If the user interacted with the bubble we don't reduce the number of
......
...@@ -1000,12 +1000,9 @@ void AppMenu::OnMenuClosed(views::MenuItemView* menu) { ...@@ -1000,12 +1000,9 @@ void AppMenu::OnMenuClosed(views::MenuItemView* menu) {
bool AppMenu::ShouldExecuteCommandWithoutClosingMenu(int command_id, bool AppMenu::ShouldExecuteCommandWithoutClosingMenu(int command_id,
const ui::Event& event) { const ui::Event& event) {
if (IsRecentTabsCommand(command_id) && event.IsMouseEvent()) { return IsRecentTabsCommand(command_id) && event.IsMouseEvent() &&
const auto disposition = ui::DispositionFromEventFlags(event.flags()); (ui::DispositionFromEventFlags(event.flags()) ==
if (disposition == WindowOpenDisposition::NEW_BACKGROUND_TAB) WindowOpenDisposition::NEW_BACKGROUND_TAB);
return true;
}
return false;
} }
void AppMenu::BookmarkModelChanged() { void AppMenu::BookmarkModelChanged() {
......
...@@ -46,11 +46,9 @@ void NavigationHandler::HandleNavigateToUrl(const base::ListValue* args) { ...@@ -46,11 +46,9 @@ void NavigationHandler::HandleNavigateToUrl(const base::ListValue* args) {
bool middle_button = (button == 1.0); bool middle_button = (button == 1.0);
WindowOpenDisposition disposition = ui::DispositionFromClick( WindowOpenDisposition disposition = ui::DispositionFromClick(
middle_button, alt_key, ctrl_key, meta_key, shift_key); middle_button, alt_key, ctrl_key, meta_key, shift_key,
if (disposition == WindowOpenDisposition::CURRENT_TAB && (target_string == "_blank") ? WindowOpenDisposition::NEW_FOREGROUND_TAB
target_string == "_blank") : WindowOpenDisposition::CURRENT_TAB);
disposition = WindowOpenDisposition::NEW_FOREGROUND_TAB;
web_ui()->GetWebContents()->OpenURL( web_ui()->GetWebContents()->OpenURL(
content::OpenURLParams(GURL(url_string), content::Referrer(), disposition, content::OpenURLParams(GURL(url_string), content::Referrer(), disposition,
ui::PAGE_TRANSITION_LINK, false)); ui::PAGE_TRANSITION_LINK, false));
......
...@@ -9,11 +9,13 @@ ...@@ -9,11 +9,13 @@
namespace ui { namespace ui {
WindowOpenDisposition DispositionFromClick(bool middle_button, WindowOpenDisposition DispositionFromClick(
bool alt_key, bool middle_button,
bool ctrl_key, bool alt_key,
bool meta_key, bool ctrl_key,
bool shift_key) { bool meta_key,
bool shift_key,
WindowOpenDisposition disposition_for_current_tab) {
// MacOS uses meta key (Command key) to spawn new tabs. // MacOS uses meta key (Command key) to spawn new tabs.
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
if (middle_button || meta_key) if (middle_button || meta_key)
...@@ -26,16 +28,18 @@ WindowOpenDisposition DispositionFromClick(bool middle_button, ...@@ -26,16 +28,18 @@ WindowOpenDisposition DispositionFromClick(bool middle_button,
return WindowOpenDisposition::NEW_WINDOW; return WindowOpenDisposition::NEW_WINDOW;
if (alt_key) if (alt_key)
return WindowOpenDisposition::SAVE_TO_DISK; return WindowOpenDisposition::SAVE_TO_DISK;
return WindowOpenDisposition::CURRENT_TAB; return disposition_for_current_tab;
} }
WindowOpenDisposition DispositionFromEventFlags(int event_flags) { WindowOpenDisposition DispositionFromEventFlags(
return DispositionFromClick( int event_flags,
(event_flags & ui::EF_MIDDLE_MOUSE_BUTTON) != 0, WindowOpenDisposition disposition_for_current_tab) {
(event_flags & ui::EF_ALT_DOWN) != 0, return DispositionFromClick((event_flags & ui::EF_MIDDLE_MOUSE_BUTTON) != 0,
(event_flags & ui::EF_CONTROL_DOWN) != 0, (event_flags & ui::EF_ALT_DOWN) != 0,
(event_flags & ui::EF_COMMAND_DOWN) != 0, (event_flags & ui::EF_CONTROL_DOWN) != 0,
(event_flags & ui::EF_SHIFT_DOWN) != 0); (event_flags & ui::EF_COMMAND_DOWN) != 0,
(event_flags & ui::EF_SHIFT_DOWN) != 0,
disposition_for_current_tab);
} }
} // namespace ui } // namespace ui
...@@ -35,19 +35,26 @@ enum class WindowOpenDisposition { ...@@ -35,19 +35,26 @@ enum class WindowOpenDisposition {
namespace ui { namespace ui {
// Translates event flags from a click on a link into the user's desired // Translates event flags from a click on a link into the user's desired window
// window disposition. For example, a middle click would mean to open // disposition. For example, a middle click would mean to open a background
// a background tab. // tab. |disposition_for_current_tab| is the disposition to return if the flags
UI_BASE_EXPORT WindowOpenDisposition DispositionFromClick(bool middle_button, // suggest opening in the current tab; for example, a caller could set this to
bool alt_key, // NEW_FOREGROUND_TAB to prevent a click from overwriting the current tab by
bool ctrl_key, // default.
bool meta_key, UI_BASE_EXPORT WindowOpenDisposition
bool shift_key); DispositionFromClick(bool middle_button,
bool alt_key,
// Translates event flags into what kind of disposition they represents. bool ctrl_key,
// For example, a middle click would mean to open a background tab. bool meta_key,
// event_flags are the flags as understood by ui::MouseEvent. bool shift_key,
UI_BASE_EXPORT WindowOpenDisposition DispositionFromEventFlags(int event_flags); WindowOpenDisposition disposition_for_current_tab =
WindowOpenDisposition::CURRENT_TAB);
// As with DispositionFromClick(), but using |event_flags| as in ui::MouseEvent.
UI_BASE_EXPORT WindowOpenDisposition
DispositionFromEventFlags(int event_flags,
WindowOpenDisposition disposition_for_current_tab =
WindowOpenDisposition::CURRENT_TAB);
} // namespace ui } // namespace ui
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment