diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-02 03:08:11 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-02 03:08:11 +0000 |
commit | b3167e7f0b780992cf4d44bf983cf880831d0699 (patch) | |
tree | 93b1fdd046ce0510b336bda48ecf0111020728ef | |
parent | c5726b841ef81c0a6432cfd18381b82767cc15da (diff) | |
download | chromium_src-b3167e7f0b780992cf4d44bf983cf880831d0699.zip chromium_src-b3167e7f0b780992cf4d44bf983cf880831d0699.tar.gz chromium_src-b3167e7f0b780992cf4d44bf983cf880831d0699.tar.bz2 |
Add "Go to..." to the context menu when the selected text in a page is a url.
So when you select:
"http://www.google.com"
The context menu that will appear will be:
"Go to http://www.google.com"
Instead of:
"Search Google for http://www.google.com"
BUG=1978
TEST=open chromium, select a text that is a url, see if appears the menu "Go to..."; click on it,
see if it goes to the desired page.
Review URL: http://codereview.chromium.org/326026
Patch from tfarina.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30685 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/app/generated_resources.grd | 6 | ||||
-rw-r--r-- | chrome/browser/tab_contents/render_view_context_menu.cc | 51 | ||||
-rw-r--r-- | chrome/browser/tab_contents/render_view_context_menu.h | 12 |
3 files changed, 45 insertions, 24 deletions
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd index 275e7a7a6..7de0c3d 100644 --- a/chrome/app/generated_resources.grd +++ b/chrome/app/generated_resources.grd @@ -489,6 +489,9 @@ each locale. --> <message name="IDS_CONTENT_CONTEXT_SEARCHWEBFOR" desc="The name of the Search the Web for 'string' command in the content area context menu"> &Search <ph name="SEARCH_ENGINE">$1<ex>Google</ex></ph> for '<ph name="SEARCH_TERMS">$2<ex>flowers</ex></ph>' </message> + <message name="IDS_CONTENT_CONTEXT_GOTOURL" desc="The name of the Go to 'url' command in the content area context menu"> + &Go to <ph name="URL">$1<ex>http://www.google.com/</ex></ph> + </message> <message name="IDS_CONTENT_CONTEXT_ADDSEARCHENGINE" desc="The name of the Add as Search Engine command in the content area context menu"> Add as search en&gine... </message> @@ -662,6 +665,9 @@ each locale. --> <message name="IDS_CONTENT_CONTEXT_SEARCHWEBFOR" desc="In Title Case: The name of the Search the Web for 'string' command in the content area context menu"> &Search <ph name="SEARCH_ENGINE">$1<ex>Google</ex></ph> for '<ph name="SEARCH_TERMS">$2<ex>flowers</ex></ph>' </message> + <message name="IDS_CONTENT_CONTEXT_GOTOURL" desc="In Title Case: The name of the Go to url for 'string' command in the content area context menu"> + &Go to <ph name="URL">$1<ex>http://www.google.com/</ex></ph> + </message> <message name="IDS_CONTENT_CONTEXT_ADDSEARCHENGINE" desc="In Title Case: The name of the Add as Search Engine command in the content area context menu"> Add As Search En&gine... </message> diff --git a/chrome/browser/tab_contents/render_view_context_menu.cc b/chrome/browser/tab_contents/render_view_context_menu.cc index ff719c4e..255a3ea 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.cc +++ b/chrome/browser/tab_contents/render_view_context_menu.cc @@ -18,6 +18,7 @@ #include "chrome/browser/net/browser_url_util.h" #include "chrome/browser/page_info_window.h" #include "chrome/browser/profile.h" +#include "chrome/browser/search_versus_navigate_classifier.h" #include "chrome/browser/search_engines/template_url_model.h" #include "chrome/browser/spellchecker.h" #include "chrome/browser/spellchecker_platform_engine.h" @@ -73,7 +74,6 @@ void RenderViewContextMenu::Init() { } void RenderViewContextMenu::InitMenu() { - bool has_link = !params_.link_url.is_empty(); bool has_selection = !params_.selection_text.empty(); @@ -220,16 +220,31 @@ void RenderViewContextMenu::AppendSearchProvider() { DCHECK(profile_); const TemplateURL* const default_provider = profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); - if (default_provider != NULL) { - string16 selection_text = EscapeAmpersands(WideToUTF16( - l10n_util::TruncateString(params_.selection_text, 50))); - if (!selection_text.empty()) { - string16 label(l10n_util::GetStringFUTF16( - IDS_CONTENT_CONTEXT_SEARCHWEBFOR, - WideToUTF16(default_provider->short_name()), - selection_text)); - AppendMenuItem(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, label); - } + if (!default_provider) + return; + + string16 selection_text = EscapeAmpersands(WideToUTF16( + l10n_util::TruncateString(params_.selection_text, 50))); + if (selection_text.empty()) + return; + + bool is_search; + profile_->GetSearchVersusNavigateClassifier()->Classify( + UTF16ToWide(selection_text), std::wstring(), &is_search, + &selection_navigation_url_, &transition_, NULL, NULL); + if (!selection_navigation_url_.is_valid()) + return; + + if (is_search) { + string16 label(l10n_util::GetStringFUTF16( + IDS_CONTENT_CONTEXT_SEARCHWEBFOR, + WideToUTF16(default_provider->short_name()), + selection_text)); + AppendMenuItem(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, label); + } else { + string16 label(l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_GOTOURL, + selection_text)); + AppendMenuItem(IDS_CONTENT_CONTEXT_GOTOURL, label); } } @@ -445,6 +460,7 @@ bool RenderViewContextMenu::IsItemCommandEnabled(int id) const { case IDS_CONTENT_CONTEXT_COPYIMAGE: case IDS_CONTENT_CONTEXT_PRINT: case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: + case IDS_CONTENT_CONTEXT_GOTOURL: case IDC_SPELLCHECK_SUGGESTION_0: case IDC_SPELLCHECK_SUGGESTION_1: case IDC_SPELLCHECK_SUGGESTION_2: @@ -701,16 +717,9 @@ void RenderViewContextMenu::ExecuteItemCommand(int id) { source_tab_contents_->render_view_host()->SelectAll(); break; - case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: { - const TemplateURL* const default_provider = - profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); - DCHECK(default_provider); // The context menu should not contain this - // item when there is no provider. - const TemplateURLRef* const search_url = default_provider->url(); - DCHECK(search_url->SupportsReplacement()); - OpenURL(GURL(WideToUTF8(search_url->ReplaceSearchTerms(*default_provider, - params_.selection_text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, - std::wstring()))), NEW_FOREGROUND_TAB, PageTransition::GENERATED); + case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: + case IDS_CONTENT_CONTEXT_GOTOURL: { + OpenURL(selection_navigation_url_, NEW_FOREGROUND_TAB, transition_); break; } diff --git a/chrome/browser/tab_contents/render_view_context_menu.h b/chrome/browser/tab_contents/render_view_context_menu.h index 7a4bbc8..599051f 100644 --- a/chrome/browser/tab_contents/render_view_context_menu.h +++ b/chrome/browser/tab_contents/render_view_context_menu.h @@ -23,9 +23,8 @@ struct WebMediaPlayerAction; class RenderViewContextMenu { public: - RenderViewContextMenu( - TabContents* tab_contents, - const ContextMenuParams& params); + RenderViewContextMenu(TabContents* tab_contents, + const ContextMenuParams& params); virtual ~RenderViewContextMenu(); @@ -109,6 +108,13 @@ class RenderViewContextMenu { bool IsDevCommandEnabled(int id) const; + // The destination URL to use if the user tries to search for or navigate to + // a text selection. + GURL selection_navigation_url_; + + // The transition type of |selection_navigation_url_|. + PageTransition::Type transition_; + DISALLOW_COPY_AND_ASSIGN(RenderViewContextMenu); }; |