Commit ad5b7565 authored by thestig@chromium.org's avatar thestig@chromium.org

Clean up RenderViewContextMenu a bit.

Review URL: https://codereview.chromium.org/468913003

Cr-Commit-Position: refs/heads/master@{#291352}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@291352 0039d316-1c4b-4281-b951-d872f2087c98
parent 5acae4b6
...@@ -277,7 +277,30 @@ PrefService* GetPrefs(content::BrowserContext* context) { ...@@ -277,7 +277,30 @@ PrefService* GetPrefs(content::BrowserContext* context) {
return user_prefs::UserPrefs::Get(context); return user_prefs::UserPrefs::Get(context);
} }
bool custom_id_ranges_initialized = false; bool ExtensionPatternMatch(const extensions::URLPatternSet& patterns,
const GURL& url) {
// No patterns means no restriction, so that implicitly matches.
if (patterns.is_empty())
return true;
return patterns.MatchesURL(url);
}
const GURL& GetDocumentURL(const content::ContextMenuParams& params) {
return params.frame_url.is_empty() ? params.page_url : params.frame_url;
}
content::Referrer CreateSaveAsReferrer(
const GURL& url,
const content::ContextMenuParams& params) {
const GURL& referring_url = GetDocumentURL(params);
return content::Referrer::SanitizeForRequest(
url,
content::Referrer(referring_url.GetAsReferrer(), params.referrer_policy));
}
bool g_custom_id_ranges_initialized = false;
const int kSpellcheckRadioGroup = 1;
} // namespace } // namespace
...@@ -293,8 +316,6 @@ bool RenderViewContextMenu::IsInternalResourcesURL(const GURL& url) { ...@@ -293,8 +316,6 @@ bool RenderViewContextMenu::IsInternalResourcesURL(const GURL& url) {
return url.host() == chrome::kChromeUISyncResourcesHost; return url.host() == chrome::kChromeUISyncResourcesHost;
} }
static const int kSpellcheckRadioGroup = 1;
RenderViewContextMenu::RenderViewContextMenu( RenderViewContextMenu::RenderViewContextMenu(
content::RenderFrameHost* render_frame_host, content::RenderFrameHost* render_frame_host,
const content::ContextMenuParams& params) const content::ContextMenuParams& params)
...@@ -306,8 +327,8 @@ RenderViewContextMenu::RenderViewContextMenu( ...@@ -306,8 +327,8 @@ RenderViewContextMenu::RenderViewContextMenu(
protocol_handler_submenu_model_(this), protocol_handler_submenu_model_(this),
protocol_handler_registry_( protocol_handler_registry_(
ProtocolHandlerRegistryFactory::GetForBrowserContext(GetProfile())) { ProtocolHandlerRegistryFactory::GetForBrowserContext(GetProfile())) {
if (!custom_id_ranges_initialized) { if (!g_custom_id_ranges_initialized) {
custom_id_ranges_initialized = true; g_custom_id_ranges_initialized = true;
SetContentCustomCommandIdRange(IDC_CONTENT_CONTEXT_CUSTOM_FIRST, SetContentCustomCommandIdRange(IDC_CONTENT_CONTEXT_CUSTOM_FIRST,
IDC_CONTENT_CONTEXT_CUSTOM_LAST); IDC_CONTENT_CONTEXT_CUSTOM_LAST);
} }
...@@ -320,18 +341,10 @@ RenderViewContextMenu::~RenderViewContextMenu() { ...@@ -320,18 +341,10 @@ RenderViewContextMenu::~RenderViewContextMenu() {
// Menu construction functions ------------------------------------------------- // Menu construction functions -------------------------------------------------
static bool ExtensionPatternMatch(const extensions::URLPatternSet& patterns,
const GURL& url) {
// No patterns means no restriction, so that implicitly matches.
if (patterns.is_empty())
return true;
return patterns.MatchesURL(url);
}
// static // static
bool RenderViewContextMenu::ExtensionContextAndPatternMatch( bool RenderViewContextMenu::ExtensionContextAndPatternMatch(
const content::ContextMenuParams& params, const content::ContextMenuParams& params,
MenuItem::ContextList contexts, const MenuItem::ContextList& contexts,
const extensions::URLPatternSet& target_url_patterns) { const extensions::URLPatternSet& target_url_patterns) {
const bool has_link = !params.link_url.is_empty(); const bool has_link = !params.link_url.is_empty();
const bool has_selection = !params.selection_text.empty(); const bool has_selection = !params.selection_text.empty();
...@@ -381,10 +394,6 @@ bool RenderViewContextMenu::ExtensionContextAndPatternMatch( ...@@ -381,10 +394,6 @@ bool RenderViewContextMenu::ExtensionContextAndPatternMatch(
return false; return false;
} }
static const GURL& GetDocumentURL(const content::ContextMenuParams& params) {
return params.frame_url.is_empty() ? params.page_url : params.frame_url;
}
// static // static
bool RenderViewContextMenu::MenuItemMatchesParams( bool RenderViewContextMenu::MenuItemMatchesParams(
const content::ContextMenuParams& params, const content::ContextMenuParams& params,
...@@ -835,13 +844,13 @@ void RenderViewContextMenu::AppendSearchProvider() { ...@@ -835,13 +844,13 @@ void RenderViewContextMenu::AppendSearchProvider() {
base::ASCIIToUTF16(" "), &params_.selection_text); base::ASCIIToUTF16(" "), &params_.selection_text);
AutocompleteMatch match; AutocompleteMatch match;
AutocompleteClassifierFactory::GetForProfile(GetProfile()) AutocompleteClassifierFactory::GetForProfile(GetProfile())->Classify(
->Classify(params_.selection_text, params_.selection_text,
false, false,
false, false,
metrics::OmniboxEventProto::INVALID_SPEC, metrics::OmniboxEventProto::INVALID_SPEC,
&match, &match,
NULL); NULL);
selection_navigation_url_ = match.destination_url; selection_navigation_url_ = match.destination_url;
if (!selection_navigation_url_.is_valid()) if (!selection_navigation_url_.is_valid())
return; return;
...@@ -1249,15 +1258,11 @@ bool RenderViewContextMenu::IsCommandIdChecked(int id) const { ...@@ -1249,15 +1258,11 @@ bool RenderViewContextMenu::IsCommandIdChecked(int id) const {
return true; return true;
// See if the video is set to looping. // See if the video is set to looping.
if (id == IDC_CONTENT_CONTEXT_LOOP) { if (id == IDC_CONTENT_CONTEXT_LOOP)
return (params_.media_flags & return (params_.media_flags & WebContextMenuData::MediaLoop) != 0;
WebContextMenuData::MediaLoop) != 0;
}
if (id == IDC_CONTENT_CONTEXT_CONTROLS) { if (id == IDC_CONTENT_CONTEXT_CONTROLS)
return (params_.media_flags & return (params_.media_flags & WebContextMenuData::MediaControls) != 0;
WebContextMenuData::MediaControls) != 0;
}
// Extension items. // Extension items.
if (ContextMenuMatcher::IsExtensionsCustomCommandId(id)) if (ContextMenuMatcher::IsExtensionsCustomCommandId(id))
...@@ -1284,18 +1289,18 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ...@@ -1284,18 +1289,18 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
id <= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST) { id <= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST) {
ProtocolHandlerRegistry::ProtocolHandlerList handlers = ProtocolHandlerRegistry::ProtocolHandlerList handlers =
GetHandlersForLinkUrl(); GetHandlersForLinkUrl();
if (handlers.empty()) { if (handlers.empty())
return; return;
}
content::RecordAction( content::RecordAction(
UserMetricsAction("RegisterProtocolHandler.ContextMenu_Open")); UserMetricsAction("RegisterProtocolHandler.ContextMenu_Open"));
int handlerIndex = id - IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST; int handlerIndex = id - IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST;
WindowOpenDisposition disposition = WindowOpenDisposition disposition =
ForceNewTabDispositionFromEventFlags(event_flags); ForceNewTabDispositionFromEventFlags(event_flags);
OpenURL( OpenURL(handlers[handlerIndex].TranslateUrl(params_.link_url),
handlers[handlerIndex].TranslateUrl(params_.link_url), GetDocumentURL(params_),
params_.frame_url.is_empty() ? params_.page_url : params_.frame_url, disposition,
disposition, content::PAGE_TRANSITION_LINK); content::PAGE_TRANSITION_LINK);
return; return;
} }
...@@ -1303,19 +1308,18 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ...@@ -1303,19 +1308,18 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
case IDC_CONTENT_CONTEXT_OPENLINKNEWTAB: { case IDC_CONTENT_CONTEXT_OPENLINKNEWTAB: {
Browser* browser = Browser* browser =
chrome::FindBrowserWithWebContents(source_web_contents_); chrome::FindBrowserWithWebContents(source_web_contents_);
OpenURL( OpenURL(params_.link_url,
params_.link_url, GetDocumentURL(params_),
params_.frame_url.is_empty() ? params_.page_url : params_.frame_url, !browser || browser->is_app() ?
!browser || browser->is_app() ?
NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB, NEW_FOREGROUND_TAB : NEW_BACKGROUND_TAB,
content::PAGE_TRANSITION_LINK); content::PAGE_TRANSITION_LINK);
break; break;
} }
case IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW: case IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW:
OpenURL( OpenURL(params_.link_url,
params_.link_url, GetDocumentURL(params_),
params_.frame_url.is_empty() ? params_.page_url : params_.frame_url, NEW_WINDOW,
NEW_WINDOW, content::PAGE_TRANSITION_LINK); content::PAGE_TRANSITION_LINK);
break; break;
case IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD: case IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD:
...@@ -1326,12 +1330,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ...@@ -1326,12 +1330,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
case IDC_CONTENT_CONTEXT_SAVELINKAS: { case IDC_CONTENT_CONTEXT_SAVELINKAS: {
RecordDownloadSource(DOWNLOAD_INITIATED_BY_CONTEXT_MENU); RecordDownloadSource(DOWNLOAD_INITIATED_BY_CONTEXT_MENU);
const GURL& url = params_.link_url; const GURL& url = params_.link_url;
const GURL& referring_url = content::Referrer referrer = CreateSaveAsReferrer(url, params_);
params_.frame_url.is_empty() ? params_.page_url : params_.frame_url;
content::Referrer referrer = content::Referrer::SanitizeForRequest(
url,
content::Referrer(referring_url.GetAsReferrer(),
params_.referrer_policy));
DownloadManager* dlm = DownloadManager* dlm =
BrowserContext::GetDownloadManager(browser_context_); BrowserContext::GetDownloadManager(browser_context_);
scoped_ptr<DownloadUrlParameters> dl_params( scoped_ptr<DownloadUrlParameters> dl_params(
...@@ -1353,12 +1352,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ...@@ -1353,12 +1352,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
// TODO(zino): We can use SaveImageAt() like a case of canvas. // TODO(zino): We can use SaveImageAt() like a case of canvas.
RecordDownloadSource(DOWNLOAD_INITIATED_BY_CONTEXT_MENU); RecordDownloadSource(DOWNLOAD_INITIATED_BY_CONTEXT_MENU);
const GURL& url = params_.src_url; const GURL& url = params_.src_url;
const GURL& referring_url = content::Referrer referrer = CreateSaveAsReferrer(url, params_);
params_.frame_url.is_empty() ? params_.page_url : params_.frame_url;
content::Referrer referrer = content::Referrer::SanitizeForRequest(
url,
content::Referrer(referring_url.GetAsReferrer(),
params_.referrer_policy));
source_web_contents_->SaveFrame(url, referrer); source_web_contents_->SaveFrame(url, referrer);
} }
break; break;
...@@ -1383,10 +1377,10 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ...@@ -1383,10 +1377,10 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
case IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB: case IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB:
case IDC_CONTENT_CONTEXT_OPENAVNEWTAB: case IDC_CONTENT_CONTEXT_OPENAVNEWTAB:
OpenURL( OpenURL(params_.src_url,
params_.src_url, GetDocumentURL(params_),
params_.frame_url.is_empty() ? params_.page_url : params_.frame_url, NEW_BACKGROUND_TAB,
NEW_BACKGROUND_TAB, content::PAGE_TRANSITION_LINK); content::PAGE_TRANSITION_LINK);
break; break;
case IDC_CONTENT_CONTEXT_PLAYPAUSE: { case IDC_CONTENT_CONTEXT_PLAYPAUSE: {
...@@ -1437,9 +1431,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ...@@ -1437,9 +1431,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
UserMetricsAction("PluginContextMenu_RotateClockwise")); UserMetricsAction("PluginContextMenu_RotateClockwise"));
PluginActionAt( PluginActionAt(
gfx::Point(params_.x, params_.y), gfx::Point(params_.x, params_.y),
WebPluginAction( WebPluginAction(WebPluginAction::Rotate90Clockwise, true));
WebPluginAction::Rotate90Clockwise,
true));
break; break;
case IDC_CONTENT_CONTEXT_ROTATECCW: case IDC_CONTENT_CONTEXT_ROTATECCW:
...@@ -1447,9 +1439,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) { ...@@ -1447,9 +1439,7 @@ void RenderViewContextMenu::ExecuteCommand(int id, int event_flags) {
UserMetricsAction("PluginContextMenu_RotateCounterclockwise")); UserMetricsAction("PluginContextMenu_RotateCounterclockwise"));
PluginActionAt( PluginActionAt(
gfx::Point(params_.x, params_.y), gfx::Point(params_.x, params_.y),
WebPluginAction( WebPluginAction(WebPluginAction::Rotate90Counterclockwise, true));
WebPluginAction::Rotate90Counterclockwise,
true));
break; break;
case IDC_BACK: case IDC_BACK:
......
...@@ -71,7 +71,7 @@ class RenderViewContextMenu : public RenderViewContextMenuBase { ...@@ -71,7 +71,7 @@ class RenderViewContextMenu : public RenderViewContextMenuBase {
static bool IsInternalResourcesURL(const GURL& url); static bool IsInternalResourcesURL(const GURL& url);
static bool ExtensionContextAndPatternMatch( static bool ExtensionContextAndPatternMatch(
const content::ContextMenuParams& params, const content::ContextMenuParams& params,
extensions::MenuItem::ContextList contexts, const extensions::MenuItem::ContextList& contexts,
const extensions::URLPatternSet& target_url_patterns); const extensions::URLPatternSet& target_url_patterns);
static bool MenuItemMatchesParams(const content::ContextMenuParams& params, static bool MenuItemMatchesParams(const content::ContextMenuParams& params,
const extensions::MenuItem* item); const extensions::MenuItem* item);
......
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