OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 5 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
6 | 6 |
7 #include "app/clipboard/clipboard.h" | 7 #include "app/clipboard/clipboard.h" |
8 #include "app/clipboard/scoped_clipboard_writer.h" | 8 #include "app/clipboard/scoped_clipboard_writer.h" |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "chrome/app/chrome_dll_resource.h" | 12 #include "chrome/app/chrome_dll_resource.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/debugger/devtools_manager.h" | 14 #include "chrome/browser/debugger/devtools_manager.h" |
15 #include "chrome/browser/download/download_manager.h" | 15 #include "chrome/browser/download/download_manager.h" |
16 #include "chrome/browser/fonts_languages_window.h" | 16 #include "chrome/browser/fonts_languages_window.h" |
17 #include "chrome/browser/metrics/user_metrics.h" | 17 #include "chrome/browser/metrics/user_metrics.h" |
18 #include "chrome/browser/net/browser_url_util.h" | 18 #include "chrome/browser/net/browser_url_util.h" |
19 #include "chrome/browser/page_info_window.h" | 19 #include "chrome/browser/page_info_window.h" |
20 #include "chrome/browser/profile.h" | 20 #include "chrome/browser/profile.h" |
| 21 #include "chrome/browser/search_versus_navigate_classifier.h" |
21 #include "chrome/browser/search_engines/template_url_model.h" | 22 #include "chrome/browser/search_engines/template_url_model.h" |
22 #include "chrome/browser/spellchecker.h" | 23 #include "chrome/browser/spellchecker.h" |
23 #include "chrome/browser/spellchecker_platform_engine.h" | 24 #include "chrome/browser/spellchecker_platform_engine.h" |
24 #include "chrome/browser/tab_contents/navigation_entry.h" | 25 #include "chrome/browser/tab_contents/navigation_entry.h" |
25 #include "chrome/browser/tab_contents/tab_contents.h" | 26 #include "chrome/browser/tab_contents/tab_contents.h" |
26 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
27 #include "chrome/common/platform_util.h" | 28 #include "chrome/common/platform_util.h" |
28 #include "chrome/common/pref_service.h" | 29 #include "chrome/common/pref_service.h" |
29 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
30 #include "grit/generated_resources.h" | 31 #include "grit/generated_resources.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 } | 67 } |
67 | 68 |
68 // Menu construction functions ------------------------------------------------- | 69 // Menu construction functions ------------------------------------------------- |
69 | 70 |
70 void RenderViewContextMenu::Init() { | 71 void RenderViewContextMenu::Init() { |
71 InitMenu(); | 72 InitMenu(); |
72 DoInit(); | 73 DoInit(); |
73 } | 74 } |
74 | 75 |
75 void RenderViewContextMenu::InitMenu() { | 76 void RenderViewContextMenu::InitMenu() { |
76 | |
77 bool has_link = !params_.link_url.is_empty(); | 77 bool has_link = !params_.link_url.is_empty(); |
78 bool has_selection = !params_.selection_text.empty(); | 78 bool has_selection = !params_.selection_text.empty(); |
79 | 79 |
80 // When no special node or text is selected and selection has no link, | 80 // When no special node or text is selected and selection has no link, |
81 // show page items. | 81 // show page items. |
82 if (params_.media_type == WebContextMenuData::MediaTypeNone && | 82 if (params_.media_type == WebContextMenuData::MediaTypeNone && |
83 !has_link && | 83 !has_link && |
84 !params_.is_editable && | 84 !params_.is_editable && |
85 !has_selection) { | 85 !has_selection) { |
86 // If context is in subframe, show subframe options instead. | 86 // If context is in subframe, show subframe options instead. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 } | 213 } |
214 | 214 |
215 void RenderViewContextMenu::AppendCopyItem() { | 215 void RenderViewContextMenu::AppendCopyItem() { |
216 AppendMenuItem(IDS_CONTENT_CONTEXT_COPY); | 216 AppendMenuItem(IDS_CONTENT_CONTEXT_COPY); |
217 } | 217 } |
218 | 218 |
219 void RenderViewContextMenu::AppendSearchProvider() { | 219 void RenderViewContextMenu::AppendSearchProvider() { |
220 DCHECK(profile_); | 220 DCHECK(profile_); |
221 const TemplateURL* const default_provider = | 221 const TemplateURL* const default_provider = |
222 profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); | 222 profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); |
223 if (default_provider != NULL) { | 223 if (!default_provider) |
224 string16 selection_text = EscapeAmpersands(WideToUTF16( | 224 return; |
225 l10n_util::TruncateString(params_.selection_text, 50))); | 225 |
226 if (!selection_text.empty()) { | 226 string16 selection_text = EscapeAmpersands(WideToUTF16( |
227 string16 label(l10n_util::GetStringFUTF16( | 227 l10n_util::TruncateString(params_.selection_text, 50))); |
228 IDS_CONTENT_CONTEXT_SEARCHWEBFOR, | 228 if (selection_text.empty()) |
229 WideToUTF16(default_provider->short_name()), | 229 return; |
230 selection_text)); | 230 |
231 AppendMenuItem(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, label); | 231 bool is_search; |
232 } | 232 profile_->GetSearchVersusNavigateClassifier()->Classify( |
| 233 UTF16ToWide(selection_text), std::wstring(), &is_search, |
| 234 &selection_navigation_url_, &transition_, NULL, NULL); |
| 235 if (!selection_navigation_url_.is_valid()) |
| 236 return; |
| 237 |
| 238 if (is_search) { |
| 239 string16 label(l10n_util::GetStringFUTF16( |
| 240 IDS_CONTENT_CONTEXT_SEARCHWEBFOR, |
| 241 WideToUTF16(default_provider->short_name()), |
| 242 selection_text)); |
| 243 AppendMenuItem(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, label); |
| 244 } else { |
| 245 string16 label(l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_GOTOURL, |
| 246 selection_text)); |
| 247 AppendMenuItem(IDS_CONTENT_CONTEXT_GOTOURL, label); |
233 } | 248 } |
234 } | 249 } |
235 | 250 |
236 void RenderViewContextMenu::AppendEditableItems() { | 251 void RenderViewContextMenu::AppendEditableItems() { |
237 // Append Dictionary spell check suggestions. | 252 // Append Dictionary spell check suggestions. |
238 for (size_t i = 0; i < params_.dictionary_suggestions.size() && | 253 for (size_t i = 0; i < params_.dictionary_suggestions.size() && |
239 IDC_SPELLCHECK_SUGGESTION_0 + i <= IDC_SPELLCHECK_SUGGESTION_LAST; | 254 IDC_SPELLCHECK_SUGGESTION_0 + i <= IDC_SPELLCHECK_SUGGESTION_LAST; |
240 ++i) { | 255 ++i) { |
241 AppendMenuItem(IDC_SPELLCHECK_SUGGESTION_0 + static_cast<int>(i), | 256 AppendMenuItem(IDC_SPELLCHECK_SUGGESTION_0 + static_cast<int>(i), |
242 params_.dictionary_suggestions[i]); | 257 params_.dictionary_suggestions[i]); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 case IDS_CONTENT_CONTEXT_OPENFRAMEOFFTHERECORD: | 453 case IDS_CONTENT_CONTEXT_OPENFRAMEOFFTHERECORD: |
439 return !profile_->IsOffTheRecord() && params_.frame_url.is_valid(); | 454 return !profile_->IsOffTheRecord() && params_.frame_url.is_valid(); |
440 | 455 |
441 case IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY: | 456 case IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY: |
442 return !params_.misspelled_word.empty(); | 457 return !params_.misspelled_word.empty(); |
443 | 458 |
444 case IDS_CONTENT_CONTEXT_RELOAD: | 459 case IDS_CONTENT_CONTEXT_RELOAD: |
445 case IDS_CONTENT_CONTEXT_COPYIMAGE: | 460 case IDS_CONTENT_CONTEXT_COPYIMAGE: |
446 case IDS_CONTENT_CONTEXT_PRINT: | 461 case IDS_CONTENT_CONTEXT_PRINT: |
447 case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: | 462 case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: |
| 463 case IDS_CONTENT_CONTEXT_GOTOURL: |
448 case IDC_SPELLCHECK_SUGGESTION_0: | 464 case IDC_SPELLCHECK_SUGGESTION_0: |
449 case IDC_SPELLCHECK_SUGGESTION_1: | 465 case IDC_SPELLCHECK_SUGGESTION_1: |
450 case IDC_SPELLCHECK_SUGGESTION_2: | 466 case IDC_SPELLCHECK_SUGGESTION_2: |
451 case IDC_SPELLCHECK_SUGGESTION_3: | 467 case IDC_SPELLCHECK_SUGGESTION_3: |
452 case IDC_SPELLCHECK_SUGGESTION_4: | 468 case IDC_SPELLCHECK_SUGGESTION_4: |
453 case IDC_SPELLCHECK_MENU: | 469 case IDC_SPELLCHECK_MENU: |
454 case IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS: | 470 case IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS: |
455 case IDS_CONTENT_CONTEXT_VIEWFRAMEINFO: | 471 case IDS_CONTENT_CONTEXT_VIEWFRAMEINFO: |
456 return true; | 472 return true; |
457 | 473 |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 break; | 710 break; |
695 | 711 |
696 case IDS_CONTENT_CONTEXT_DELETE: | 712 case IDS_CONTENT_CONTEXT_DELETE: |
697 source_tab_contents_->render_view_host()->Delete(); | 713 source_tab_contents_->render_view_host()->Delete(); |
698 break; | 714 break; |
699 | 715 |
700 case IDS_CONTENT_CONTEXT_SELECTALL: | 716 case IDS_CONTENT_CONTEXT_SELECTALL: |
701 source_tab_contents_->render_view_host()->SelectAll(); | 717 source_tab_contents_->render_view_host()->SelectAll(); |
702 break; | 718 break; |
703 | 719 |
704 case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: { | 720 case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: |
705 const TemplateURL* const default_provider = | 721 case IDS_CONTENT_CONTEXT_GOTOURL: { |
706 profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); | 722 OpenURL(selection_navigation_url_, NEW_FOREGROUND_TAB, transition_); |
707 DCHECK(default_provider); // The context menu should not contain this | |
708 // item when there is no provider. | |
709 const TemplateURLRef* const search_url = default_provider->url(); | |
710 DCHECK(search_url->SupportsReplacement()); | |
711 OpenURL(GURL(WideToUTF8(search_url->ReplaceSearchTerms(*default_provider, | |
712 params_.selection_text, TemplateURLRef::NO_SUGGESTIONS_AVAILABLE, | |
713 std::wstring()))), NEW_FOREGROUND_TAB, PageTransition::GENERATED); | |
714 break; | 723 break; |
715 } | 724 } |
716 | 725 |
717 case IDC_SPELLCHECK_SUGGESTION_0: | 726 case IDC_SPELLCHECK_SUGGESTION_0: |
718 case IDC_SPELLCHECK_SUGGESTION_1: | 727 case IDC_SPELLCHECK_SUGGESTION_1: |
719 case IDC_SPELLCHECK_SUGGESTION_2: | 728 case IDC_SPELLCHECK_SUGGESTION_2: |
720 case IDC_SPELLCHECK_SUGGESTION_3: | 729 case IDC_SPELLCHECK_SUGGESTION_3: |
721 case IDC_SPELLCHECK_SUGGESTION_4: | 730 case IDC_SPELLCHECK_SUGGESTION_4: |
722 source_tab_contents_->render_view_host()->Replace( | 731 source_tab_contents_->render_view_host()->Replace( |
723 params_.dictionary_suggestions[id - IDC_SPELLCHECK_SUGGESTION_0]); | 732 params_.dictionary_suggestions[id - IDC_SPELLCHECK_SUGGESTION_0]); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), | 825 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), |
817 g_browser_process->clipboard()); | 826 g_browser_process->clipboard()); |
818 } | 827 } |
819 | 828 |
820 void RenderViewContextMenu::MediaPlayerActionAt( | 829 void RenderViewContextMenu::MediaPlayerActionAt( |
821 const gfx::Point& location, | 830 const gfx::Point& location, |
822 const WebMediaPlayerAction& action) { | 831 const WebMediaPlayerAction& action) { |
823 source_tab_contents_->render_view_host()->MediaPlayerActionAt( | 832 source_tab_contents_->render_view_host()->MediaPlayerActionAt( |
824 location, action); | 833 location, action); |
825 } | 834 } |
OLD | NEW |