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" | |
22 #include "chrome/browser/search_engines/template_url_model.h" | 21 #include "chrome/browser/search_engines/template_url_model.h" |
23 #include "chrome/browser/spellchecker.h" | 22 #include "chrome/browser/spellchecker.h" |
24 #include "chrome/browser/spellchecker_platform_engine.h" | 23 #include "chrome/browser/spellchecker_platform_engine.h" |
25 #include "chrome/browser/tab_contents/navigation_entry.h" | 24 #include "chrome/browser/tab_contents/navigation_entry.h" |
26 #include "chrome/browser/tab_contents/tab_contents.h" | 25 #include "chrome/browser/tab_contents/tab_contents.h" |
27 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
28 #include "chrome/common/platform_util.h" | 27 #include "chrome/common/platform_util.h" |
29 #include "chrome/common/pref_service.h" | 28 #include "chrome/common/pref_service.h" |
30 #include "chrome/common/url_constants.h" | 29 #include "chrome/common/url_constants.h" |
31 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 } | 66 } |
68 | 67 |
69 // Menu construction functions ------------------------------------------------- | 68 // Menu construction functions ------------------------------------------------- |
70 | 69 |
71 void RenderViewContextMenu::Init() { | 70 void RenderViewContextMenu::Init() { |
72 InitMenu(); | 71 InitMenu(); |
73 DoInit(); | 72 DoInit(); |
74 } | 73 } |
75 | 74 |
76 void RenderViewContextMenu::InitMenu() { | 75 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) | 223 if (default_provider != NULL) { |
224 return; | 224 string16 selection_text = EscapeAmpersands(WideToUTF16( |
225 | 225 l10n_util::TruncateString(params_.selection_text, 50))); |
226 string16 selection_text = EscapeAmpersands(WideToUTF16( | 226 if (!selection_text.empty()) { |
227 l10n_util::TruncateString(params_.selection_text, 50))); | 227 string16 label(l10n_util::GetStringFUTF16( |
228 if (selection_text.empty()) | 228 IDS_CONTENT_CONTEXT_SEARCHWEBFOR, |
229 return; | 229 WideToUTF16(default_provider->short_name()), |
230 | 230 selection_text)); |
231 bool is_search; | 231 AppendMenuItem(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, label); |
232 profile_->GetSearchVersusNavigateClassifier()->Classify( | 232 } |
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); | |
248 } | 233 } |
249 } | 234 } |
250 | 235 |
251 void RenderViewContextMenu::AppendEditableItems() { | 236 void RenderViewContextMenu::AppendEditableItems() { |
252 // Append Dictionary spell check suggestions. | 237 // Append Dictionary spell check suggestions. |
253 for (size_t i = 0; i < params_.dictionary_suggestions.size() && | 238 for (size_t i = 0; i < params_.dictionary_suggestions.size() && |
254 IDC_SPELLCHECK_SUGGESTION_0 + i <= IDC_SPELLCHECK_SUGGESTION_LAST; | 239 IDC_SPELLCHECK_SUGGESTION_0 + i <= IDC_SPELLCHECK_SUGGESTION_LAST; |
255 ++i) { | 240 ++i) { |
256 AppendMenuItem(IDC_SPELLCHECK_SUGGESTION_0 + static_cast<int>(i), | 241 AppendMenuItem(IDC_SPELLCHECK_SUGGESTION_0 + static_cast<int>(i), |
257 params_.dictionary_suggestions[i]); | 242 params_.dictionary_suggestions[i]); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 case IDS_CONTENT_CONTEXT_OPENFRAMEOFFTHERECORD: | 438 case IDS_CONTENT_CONTEXT_OPENFRAMEOFFTHERECORD: |
454 return !profile_->IsOffTheRecord() && params_.frame_url.is_valid(); | 439 return !profile_->IsOffTheRecord() && params_.frame_url.is_valid(); |
455 | 440 |
456 case IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY: | 441 case IDS_CONTENT_CONTEXT_ADD_TO_DICTIONARY: |
457 return !params_.misspelled_word.empty(); | 442 return !params_.misspelled_word.empty(); |
458 | 443 |
459 case IDS_CONTENT_CONTEXT_RELOAD: | 444 case IDS_CONTENT_CONTEXT_RELOAD: |
460 case IDS_CONTENT_CONTEXT_COPYIMAGE: | 445 case IDS_CONTENT_CONTEXT_COPYIMAGE: |
461 case IDS_CONTENT_CONTEXT_PRINT: | 446 case IDS_CONTENT_CONTEXT_PRINT: |
462 case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: | 447 case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: |
463 case IDS_CONTENT_CONTEXT_GOTOURL: | |
464 case IDC_SPELLCHECK_SUGGESTION_0: | 448 case IDC_SPELLCHECK_SUGGESTION_0: |
465 case IDC_SPELLCHECK_SUGGESTION_1: | 449 case IDC_SPELLCHECK_SUGGESTION_1: |
466 case IDC_SPELLCHECK_SUGGESTION_2: | 450 case IDC_SPELLCHECK_SUGGESTION_2: |
467 case IDC_SPELLCHECK_SUGGESTION_3: | 451 case IDC_SPELLCHECK_SUGGESTION_3: |
468 case IDC_SPELLCHECK_SUGGESTION_4: | 452 case IDC_SPELLCHECK_SUGGESTION_4: |
469 case IDC_SPELLCHECK_MENU: | 453 case IDC_SPELLCHECK_MENU: |
470 case IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS: | 454 case IDS_CONTENT_CONTEXT_LANGUAGE_SETTINGS: |
471 case IDS_CONTENT_CONTEXT_VIEWFRAMEINFO: | 455 case IDS_CONTENT_CONTEXT_VIEWFRAMEINFO: |
472 return true; | 456 return true; |
473 | 457 |
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
710 break; | 694 break; |
711 | 695 |
712 case IDS_CONTENT_CONTEXT_DELETE: | 696 case IDS_CONTENT_CONTEXT_DELETE: |
713 source_tab_contents_->render_view_host()->Delete(); | 697 source_tab_contents_->render_view_host()->Delete(); |
714 break; | 698 break; |
715 | 699 |
716 case IDS_CONTENT_CONTEXT_SELECTALL: | 700 case IDS_CONTENT_CONTEXT_SELECTALL: |
717 source_tab_contents_->render_view_host()->SelectAll(); | 701 source_tab_contents_->render_view_host()->SelectAll(); |
718 break; | 702 break; |
719 | 703 |
720 case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: | 704 case IDS_CONTENT_CONTEXT_SEARCHWEBFOR: { |
721 case IDS_CONTENT_CONTEXT_GOTOURL: { | 705 const TemplateURL* const default_provider = |
722 OpenURL(selection_navigation_url_, NEW_FOREGROUND_TAB, transition_); | 706 profile_->GetTemplateURLModel()->GetDefaultSearchProvider(); |
| 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); |
723 break; | 714 break; |
724 } | 715 } |
725 | 716 |
726 case IDC_SPELLCHECK_SUGGESTION_0: | 717 case IDC_SPELLCHECK_SUGGESTION_0: |
727 case IDC_SPELLCHECK_SUGGESTION_1: | 718 case IDC_SPELLCHECK_SUGGESTION_1: |
728 case IDC_SPELLCHECK_SUGGESTION_2: | 719 case IDC_SPELLCHECK_SUGGESTION_2: |
729 case IDC_SPELLCHECK_SUGGESTION_3: | 720 case IDC_SPELLCHECK_SUGGESTION_3: |
730 case IDC_SPELLCHECK_SUGGESTION_4: | 721 case IDC_SPELLCHECK_SUGGESTION_4: |
731 source_tab_contents_->render_view_host()->Replace( | 722 source_tab_contents_->render_view_host()->Replace( |
732 params_.dictionary_suggestions[id - IDC_SPELLCHECK_SUGGESTION_0]); | 723 params_.dictionary_suggestions[id - IDC_SPELLCHECK_SUGGESTION_0]); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), | 816 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), |
826 g_browser_process->clipboard()); | 817 g_browser_process->clipboard()); |
827 } | 818 } |
828 | 819 |
829 void RenderViewContextMenu::MediaPlayerActionAt( | 820 void RenderViewContextMenu::MediaPlayerActionAt( |
830 const gfx::Point& location, | 821 const gfx::Point& location, |
831 const WebMediaPlayerAction& action) { | 822 const WebMediaPlayerAction& action) { |
832 source_tab_contents_->render_view_host()->MediaPlayerActionAt( | 823 source_tab_contents_->render_view_host()->MediaPlayerActionAt( |
833 location, action); | 824 location, action); |
834 } | 825 } |
OLD | NEW |