Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
| 15 #include "base/prefs/public/pref_member.h" | 15 #include "base/prefs/public/pref_member.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/time.h" | 18 #include "base/time.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "chrome/app/chrome_command_ids.h" | 20 #include "chrome/app/chrome_command_ids.h" |
| 21 #include "chrome/browser/app_mode/app_mode_utils.h" | |
| 21 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 22 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
| 22 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 23 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 23 #include "chrome/browser/autocomplete/autocomplete_match.h" | 24 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 24 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
| 25 #include "chrome/browser/devtools/devtools_window.h" | 26 #include "chrome/browser/devtools/devtools_window.h" |
| 26 #include "chrome/browser/download/download_service.h" | 27 #include "chrome/browser/download/download_service.h" |
| 27 #include "chrome/browser/download/download_service_factory.h" | 28 #include "chrome/browser/download/download_service_factory.h" |
| 28 #include "chrome/browser/download/download_util.h" | 29 #include "chrome/browser/download/download_util.h" |
| 29 #include "chrome/browser/extensions/extension_host.h" | 30 #include "chrome/browser/extensions/extension_host.h" |
| 30 #include "chrome/browser/extensions/extension_service.h" | 31 #include "chrome/browser/extensions/extension_service.h" |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 #endif | 225 #endif |
| 225 | 226 |
| 226 return true; | 227 return true; |
| 227 } | 228 } |
| 228 | 229 |
| 229 void DevToolsInspectElementAt(RenderViewHost* rvh, int x, int y) { | 230 void DevToolsInspectElementAt(RenderViewHost* rvh, int x, int y) { |
| 230 DevToolsWindow::InspectElement(rvh, x, y); | 231 DevToolsWindow::InspectElement(rvh, x, y); |
| 231 } | 232 } |
| 232 | 233 |
| 233 // Helper function to escape "&" as "&&". | 234 // Helper function to escape "&" as "&&". |
| 234 void EscapeAmpersands(string16& text) { | 235 void EscapeAmpersands(string16* text) { |
| 235 const char16 ampersand[] = {'&', 0}; | 236 const char16 ampersand[] = {'&', 0}; |
| 236 ReplaceChars(text, ampersand, ASCIIToUTF16("&&"), &text); | 237 ReplaceChars(*text, ampersand, ASCIIToUTF16("&&"), text); |
| 237 } | 238 } |
| 238 | 239 |
| 239 } // namespace | 240 } // namespace |
| 240 | 241 |
| 241 // static | 242 // static |
| 242 const size_t RenderViewContextMenu::kMaxSelectionTextLength = 50; | 243 const size_t RenderViewContextMenu::kMaxSelectionTextLength = 50; |
| 243 | 244 |
| 244 // static | 245 // static |
| 245 bool RenderViewContextMenu::IsDevToolsURL(const GURL& url) { | 246 bool RenderViewContextMenu::IsDevToolsURL(const GURL& url) { |
| 246 return url.SchemeIs(chrome::kChromeDevToolsScheme) && | 247 return url.SchemeIs(chrome::kChromeDevToolsScheme) && |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 | 389 |
| 389 if (sorted_ids.empty()) | 390 if (sorted_ids.empty()) |
| 390 return; | 391 return; |
| 391 | 392 |
| 392 int index = 0; | 393 int index = 0; |
| 393 base::TimeTicks begin = base::TimeTicks::Now(); | 394 base::TimeTicks begin = base::TimeTicks::Now(); |
| 394 std::vector<std::pair<std::string, std::string> >::const_iterator i; | 395 std::vector<std::pair<std::string, std::string> >::const_iterator i; |
| 395 for (i = sorted_ids.begin(); | 396 for (i = sorted_ids.begin(); |
| 396 i != sorted_ids.end(); ++i) { | 397 i != sorted_ids.end(); ++i) { |
| 397 string16 printable_selection_text = PrintableSelectionText(); | 398 string16 printable_selection_text = PrintableSelectionText(); |
| 398 EscapeAmpersands(printable_selection_text); | 399 EscapeAmpersands(&printable_selection_text); |
| 399 | 400 |
| 400 extension_items_.AppendExtensionItems(i->second, printable_selection_text, | 401 extension_items_.AppendExtensionItems(i->second, printable_selection_text, |
| 401 &index); | 402 &index); |
| 402 } | 403 } |
| 403 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime", | 404 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime", |
| 404 base::TimeTicks::Now() - begin); | 405 base::TimeTicks::Now() - begin); |
| 405 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index); | 406 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index); |
| 406 } | 407 } |
| 407 | 408 |
| 408 void RenderViewContextMenu::InitMenu() { | 409 void RenderViewContextMenu::InitMenu() { |
| 410 if (chrome::IsRunningInForcedAppMode()) { | |
| 411 AppendAppModeItems(); | |
| 412 return; | |
| 413 } | |
| 414 | |
| 409 chrome::ViewType view_type = chrome::GetViewType(source_web_contents_); | 415 chrome::ViewType view_type = chrome::GetViewType(source_web_contents_); |
| 410 if (view_type == chrome::VIEW_TYPE_APP_SHELL) { | 416 if (view_type == chrome::VIEW_TYPE_APP_SHELL) { |
| 411 AppendPlatformAppItems(); | 417 AppendPlatformAppItems(); |
| 412 return; | 418 return; |
| 413 } else if (view_type == chrome::VIEW_TYPE_EXTENSION_POPUP) { | 419 } else if (view_type == chrome::VIEW_TYPE_EXTENSION_POPUP) { |
| 414 AppendPopupExtensionItems(); | 420 AppendPopupExtensionItems(); |
| 415 return; | 421 return; |
| 416 } else if (view_type == chrome::VIEW_TYPE_PANEL) { | 422 } else if (view_type == chrome::VIEW_TYPE_PANEL) { |
| 417 AppendPanelItems(); | 423 AppendPanelItems(); |
| 418 return; | 424 return; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 512 extensions::ExtensionSystem* system = | 518 extensions::ExtensionSystem* system = |
| 513 extensions::ExtensionSystem::Get(profile_); | 519 extensions::ExtensionSystem::Get(profile_); |
| 514 // There is no process manager in some tests. | 520 // There is no process manager in some tests. |
| 515 if (!system->process_manager()) | 521 if (!system->process_manager()) |
| 516 return NULL; | 522 return NULL; |
| 517 | 523 |
| 518 return system->process_manager()->GetExtensionForRenderViewHost( | 524 return system->process_manager()->GetExtensionForRenderViewHost( |
| 519 source_web_contents_->GetRenderViewHost()); | 525 source_web_contents_->GetRenderViewHost()); |
| 520 } | 526 } |
| 521 | 527 |
| 528 void RenderViewContextMenu::AppendAppModeItems() { | |
| 529 bool has_selection = !params_.selection_text.empty(); | |
|
tfarina
2013/03/04 21:07:58
do you want to avoid this temp variable? otherwise
xiyuan
2013/03/04 21:55:32
Added "const" here and all similar cases in this f
| |
| 530 | |
| 531 if (params_.is_editable) | |
| 532 AppendEditableItems(); | |
| 533 else if (has_selection) | |
| 534 AppendCopyItem(); | |
| 535 } | |
| 536 | |
| 522 void RenderViewContextMenu::AppendPlatformAppItems() { | 537 void RenderViewContextMenu::AppendPlatformAppItems() { |
| 523 const Extension* platform_app = GetExtension(); | 538 const Extension* platform_app = GetExtension(); |
| 524 | 539 |
| 525 // The RVH might be for a process sandboxed from the extension. | 540 // The RVH might be for a process sandboxed from the extension. |
| 526 if (!platform_app) | 541 if (!platform_app) |
| 527 return; | 542 return; |
| 528 | 543 |
| 529 DCHECK(platform_app->is_platform_app()); | 544 DCHECK(platform_app->is_platform_app()); |
| 530 | 545 |
| 531 bool has_selection = !params_.selection_text.empty(); | 546 bool has_selection = !params_.selection_text.empty(); |
| (...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 825 ASCIIToUTF16(" "), ¶ms_.selection_text); | 840 ASCIIToUTF16(" "), ¶ms_.selection_text); |
| 826 | 841 |
| 827 AutocompleteMatch match; | 842 AutocompleteMatch match; |
| 828 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify( | 843 AutocompleteClassifierFactory::GetForProfile(profile_)->Classify( |
| 829 params_.selection_text, string16(), false, false, &match, NULL); | 844 params_.selection_text, string16(), false, false, &match, NULL); |
| 830 selection_navigation_url_ = match.destination_url; | 845 selection_navigation_url_ = match.destination_url; |
| 831 if (!selection_navigation_url_.is_valid()) | 846 if (!selection_navigation_url_.is_valid()) |
| 832 return; | 847 return; |
| 833 | 848 |
| 834 string16 printable_selection_text = PrintableSelectionText(); | 849 string16 printable_selection_text = PrintableSelectionText(); |
| 835 EscapeAmpersands(printable_selection_text); | 850 EscapeAmpersands(&printable_selection_text); |
| 836 | 851 |
| 837 if (AutocompleteMatch::IsSearchType(match.type)) { | 852 if (AutocompleteMatch::IsSearchType(match.type)) { |
| 838 const TemplateURL* const default_provider = | 853 const TemplateURL* const default_provider = |
| 839 TemplateURLServiceFactory::GetForProfile(profile_)-> | 854 TemplateURLServiceFactory::GetForProfile(profile_)-> |
| 840 GetDefaultSearchProvider(); | 855 GetDefaultSearchProvider(); |
| 841 if (!default_provider) | 856 if (!default_provider) |
| 842 return; | 857 return; |
| 843 menu_model_.AddItem( | 858 menu_model_.AddItem( |
| 844 IDC_CONTENT_CONTEXT_SEARCHWEBFOR, | 859 IDC_CONTENT_CONTEXT_SEARCHWEBFOR, |
| 845 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, | 860 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFOR, |
| 846 default_provider->short_name(), | 861 default_provider->short_name(), |
| 847 printable_selection_text)); | 862 printable_selection_text)); |
| 848 } else { | 863 } else { |
| 849 if ((selection_navigation_url_ != params_.link_url) && | 864 if ((selection_navigation_url_ != params_.link_url) && |
| 850 ChildProcessSecurityPolicy::GetInstance()->IsWebSafeScheme( | 865 ChildProcessSecurityPolicy::GetInstance()->IsWebSafeScheme( |
| 851 selection_navigation_url_.scheme())) { | 866 selection_navigation_url_.scheme())) { |
| 852 menu_model_.AddItem( | 867 menu_model_.AddItem( |
| 853 IDC_CONTENT_CONTEXT_GOTOURL, | 868 IDC_CONTENT_CONTEXT_GOTOURL, |
| 854 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_GOTOURL, | 869 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_GOTOURL, |
| 855 printable_selection_text)); | 870 printable_selection_text)); |
| 856 } | 871 } |
| 857 } | 872 } |
| 858 } | 873 } |
| 859 | 874 |
| 860 void RenderViewContextMenu::AppendEditableItems() { | 875 void RenderViewContextMenu::AppendEditableItems() { |
| 861 AppendSpellingSuggestionsSubMenu(); | 876 const bool use_spellcheck_and_search = !chrome::IsRunningInForcedAppMode(); |
| 877 | |
| 878 if (use_spellcheck_and_search) | |
| 879 AppendSpellingSuggestionsSubMenu(); | |
| 862 | 880 |
| 863 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_UNDO, | 881 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_UNDO, |
| 864 IDS_CONTENT_CONTEXT_UNDO); | 882 IDS_CONTENT_CONTEXT_UNDO); |
| 865 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_REDO, | 883 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_REDO, |
| 866 IDS_CONTENT_CONTEXT_REDO); | 884 IDS_CONTENT_CONTEXT_REDO); |
| 867 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 885 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
| 868 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_CUT, | 886 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_CUT, |
| 869 IDS_CONTENT_CONTEXT_CUT); | 887 IDS_CONTENT_CONTEXT_CUT); |
| 870 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPY, | 888 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPY, |
| 871 IDS_CONTENT_CONTEXT_COPY); | 889 IDS_CONTENT_CONTEXT_COPY); |
| 872 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE, | 890 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE, |
| 873 IDS_CONTENT_CONTEXT_PASTE); | 891 IDS_CONTENT_CONTEXT_PASTE); |
| 874 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE, | 892 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE, |
| 875 IDS_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE); | 893 IDS_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE); |
| 876 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_DELETE, | 894 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_DELETE, |
| 877 IDS_CONTENT_CONTEXT_DELETE); | 895 IDS_CONTENT_CONTEXT_DELETE); |
| 878 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 896 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
| 879 | 897 |
| 880 if (!params_.keyword_url.is_empty()) { | 898 if (use_spellcheck_and_search && !params_.keyword_url.is_empty()) { |
| 881 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_ADDSEARCHENGINE, | 899 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_ADDSEARCHENGINE, |
| 882 IDS_CONTENT_CONTEXT_ADDSEARCHENGINE); | 900 IDS_CONTENT_CONTEXT_ADDSEARCHENGINE); |
| 883 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 901 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
| 884 } | 902 } |
| 885 | 903 |
| 886 AppendSpellcheckOptionsSubMenu(); | 904 if (use_spellcheck_and_search) |
| 905 AppendSpellcheckOptionsSubMenu(); | |
| 887 AppendSpeechInputOptionsSubMenu(); | 906 AppendSpeechInputOptionsSubMenu(); |
| 888 AppendPlatformEditableItems(); | 907 AppendPlatformEditableItems(); |
| 889 | 908 |
| 890 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); | 909 menu_model_.AddSeparator(ui::NORMAL_SEPARATOR); |
| 891 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_SELECTALL, | 910 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_SELECTALL, |
| 892 IDS_CONTENT_CONTEXT_SELECTALL); | 911 IDS_CONTENT_CONTEXT_SELECTALL); |
| 893 } | 912 } |
| 894 | 913 |
| 895 void RenderViewContextMenu::AppendSpellingSuggestionsSubMenu() { | 914 void RenderViewContextMenu::AppendSpellingSuggestionsSubMenu() { |
| 896 if (!spelling_menu_observer_.get()) | 915 if (!spelling_menu_observer_.get()) |
| (...skipping 976 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1873 source_web_contents_->GetRenderViewHost()-> | 1892 source_web_contents_->GetRenderViewHost()-> |
| 1874 ExecuteMediaPlayerActionAtLocation(location, action); | 1893 ExecuteMediaPlayerActionAtLocation(location, action); |
| 1875 } | 1894 } |
| 1876 | 1895 |
| 1877 void RenderViewContextMenu::PluginActionAt( | 1896 void RenderViewContextMenu::PluginActionAt( |
| 1878 const gfx::Point& location, | 1897 const gfx::Point& location, |
| 1879 const WebPluginAction& action) { | 1898 const WebPluginAction& action) { |
| 1880 source_web_contents_->GetRenderViewHost()-> | 1899 source_web_contents_->GetRenderViewHost()-> |
| 1881 ExecutePluginActionAtLocation(location, action); | 1900 ExecutePluginActionAtLocation(location, action); |
| 1882 } | 1901 } |
| OLD | NEW |