Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: chrome/browser/tab_contents/render_view_context_menu.cc

Issue 6253017: Pepper/Flapper: First pass at context menu implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: PPB_Flash: 4 -> 5 Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <algorithm> 5 #include <algorithm>
6 #include <set> 6 #include <set>
7 7
8 #include "chrome/browser/tab_contents/render_view_context_menu.h" 8 #include "chrome/browser/tab_contents/render_view_context_menu.h"
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 #include "net/url_request/url_request.h" 53 #include "net/url_request/url_request.h"
54 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h" 54 #include "third_party/WebKit/Source/WebKit/chromium/public/WebContextMenuData.h"
55 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayerAction. h" 55 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaPlayerAction. h"
56 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextDirection.h" 56 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextDirection.h"
57 #include "ui/base/l10n/l10n_util.h" 57 #include "ui/base/l10n/l10n_util.h"
58 #include "webkit/glue/webmenuitem.h" 58 #include "webkit/glue/webmenuitem.h"
59 59
60 using WebKit::WebContextMenuData; 60 using WebKit::WebContextMenuData;
61 using WebKit::WebMediaPlayerAction; 61 using WebKit::WebMediaPlayerAction;
62 62
63 namespace {
64
65 bool IsCustomItemEnabled(const std::vector<WebMenuItem>& items, int id) {
66 DCHECK(id >= IDC_CONTENT_CONTEXT_CUSTOM_FIRST &&
67 id <= IDC_CONTENT_CONTEXT_CUSTOM_LAST);
68 for (size_t i = 0; i < items.size(); ++i) {
69 int action_id = IDC_CONTENT_CONTEXT_CUSTOM_FIRST + items[i].action;
70 if (action_id == id)
71 return items[i].enabled;
72 if (items[i].type == WebMenuItem::SUBMENU) {
73 if (IsCustomItemEnabled(items[i].submenu, id))
74 return true;
75 }
76 }
77 return false;
78 }
79
80 bool IsCustomItemChecked(const std::vector<WebMenuItem>& items, int id) {
81 DCHECK(id >= IDC_CONTENT_CONTEXT_CUSTOM_FIRST &&
82 id <= IDC_CONTENT_CONTEXT_CUSTOM_LAST);
83 for (size_t i = 0; i < items.size(); ++i) {
84 int action_id = IDC_CONTENT_CONTEXT_CUSTOM_FIRST + items[i].action;
85 if (action_id == id)
86 return items[i].checked;
87 if (items[i].type == WebMenuItem::SUBMENU) {
88 if (IsCustomItemChecked(items[i].submenu, id))
89 return true;
90 }
91 }
92 return false;
93 }
94
95 void AddCustomItemsToMenu(const std::vector<WebMenuItem>& items,
96 ui::SimpleMenuModel::Delegate* delegate,
97 ui::SimpleMenuModel* menu_model) {
98 for (size_t i = 0; i < items.size(); ++i) {
99 DCHECK(IDC_CONTENT_CONTEXT_CUSTOM_FIRST + items[i].action <
100 IDC_CONTENT_CONTEXT_CUSTOM_LAST);
101 switch (items[i].type) {
102 case WebMenuItem::OPTION:
103 menu_model->AddItem(
104 items[i].action + IDC_CONTENT_CONTEXT_CUSTOM_FIRST,
105 items[i].label);
106 break;
107 case WebMenuItem::CHECKABLE_OPTION:
108 menu_model->AddCheckItem(
109 items[i].action + IDC_CONTENT_CONTEXT_CUSTOM_FIRST,
110 items[i].label);
111 break;
112 case WebMenuItem::GROUP:
113 // TODO(viettrungluu): I don't know what this is supposed to do.
114 NOTREACHED();
115 break;
116 case WebMenuItem::SEPARATOR:
117 menu_model->AddSeparator();
118 break;
119 case WebMenuItem::SUBMENU: {
120 ui::SimpleMenuModel* submenu = new ui::SimpleMenuModel(delegate);
121 AddCustomItemsToMenu(items[i].submenu, delegate, submenu);
122 menu_model->AddSubMenu(
123 items[i].action + IDC_CONTENT_CONTEXT_CUSTOM_FIRST,
124 items[i].label,
125 submenu);
126 break;
127 }
128 default:
129 NOTREACHED();
130 break;
131 }
132 }
133 }
134
135 } // namespace
136
63 // static 137 // static
64 const size_t RenderViewContextMenu::kMaxExtensionItemTitleLength = 75; 138 const size_t RenderViewContextMenu::kMaxExtensionItemTitleLength = 75;
65 // static 139 // static
66 const size_t RenderViewContextMenu::kMaxSelectionTextLength = 50; 140 const size_t RenderViewContextMenu::kMaxSelectionTextLength = 50;
67 141
68 // static 142 // static
69 bool RenderViewContextMenu::IsDevToolsURL(const GURL& url) { 143 bool RenderViewContextMenu::IsDevToolsURL(const GURL& url) {
70 return url.SchemeIs(chrome::kChromeDevToolsScheme) && 144 return url.SchemeIs(chrome::kChromeDevToolsScheme) &&
71 url.host() == chrome::kChromeUIDevToolsHost; 145 url.host() == chrome::kChromeUIDevToolsHost;
72 } 146 }
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime", 425 UMA_HISTOGRAM_TIMES("Extensions.ContextMenus_BuildTime",
352 base::TimeTicks::Now() - begin); 426 base::TimeTicks::Now() - begin);
353 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index); 427 UMA_HISTOGRAM_COUNTS("Extensions.ContextMenus_ItemCount", index);
354 } 428 }
355 429
356 void RenderViewContextMenu::InitMenu() { 430 void RenderViewContextMenu::InitMenu() {
357 bool has_link = !params_.link_url.is_empty(); 431 bool has_link = !params_.link_url.is_empty();
358 bool has_selection = !params_.selection_text.empty(); 432 bool has_selection = !params_.selection_text.empty();
359 433
360 if (AppendCustomItems()) { 434 if (AppendCustomItems()) {
361 AppendDeveloperItems(); 435 // Don't add items for Pepper menu.
436 if (!params_.custom_context.is_pepper_menu)
437 AppendDeveloperItems();
362 return; 438 return;
363 } 439 }
364 440
365 // When no special node or text is selected and selection has no link, 441 // When no special node or text is selected and selection has no link,
366 // show page items. 442 // show page items.
367 bool is_devtools = false; 443 bool is_devtools = false;
368 if (params_.media_type == WebContextMenuData::MediaTypeNone && 444 if (params_.media_type == WebContextMenuData::MediaTypeNone &&
369 !has_link && 445 !has_link &&
370 !params_.is_editable && 446 !params_.is_editable &&
371 !has_selection) { 447 !has_selection) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 493
418 AppendDeveloperItems(); 494 AppendDeveloperItems();
419 } 495 }
420 496
421 void RenderViewContextMenu::LookUpInDictionary() { 497 void RenderViewContextMenu::LookUpInDictionary() {
422 // Used only in the Mac port. 498 // Used only in the Mac port.
423 NOTREACHED(); 499 NOTREACHED();
424 } 500 }
425 501
426 bool RenderViewContextMenu::AppendCustomItems() { 502 bool RenderViewContextMenu::AppendCustomItems() {
427 std::vector<WebMenuItem>& custom_items = params_.custom_items; 503 AddCustomItemsToMenu(params_.custom_items, this, &menu_model_);
428 for (size_t i = 0; i < custom_items.size(); ++i) { 504 return params_.custom_items.size() > 0;
429 DCHECK(IDC_CONTENT_CONTEXT_CUSTOM_FIRST + custom_items[i].action <
430 IDC_CONTENT_CONTEXT_CUSTOM_LAST);
431 if (custom_items[i].type == WebMenuItem::SEPARATOR) {
432 menu_model_.AddSeparator();
433 } else if (custom_items[i].type == WebMenuItem::CHECKABLE_OPTION) {
434 menu_model_.AddCheckItem(
435 custom_items[i].action + IDC_CONTENT_CONTEXT_CUSTOM_FIRST,
436 custom_items[i].label);
437 } else {
438 menu_model_.AddItem(
439 custom_items[i].action + IDC_CONTENT_CONTEXT_CUSTOM_FIRST,
440 custom_items[i].label);
441 }
442 }
443 return custom_items.size() > 0;
444 } 505 }
445 506
446 void RenderViewContextMenu::AppendDeveloperItems() { 507 void RenderViewContextMenu::AppendDeveloperItems() {
447 if (g_browser_process->have_inspector_files()) { 508 if (g_browser_process->have_inspector_files()) {
448 // In the DevTools popup menu, "developer items" is normally the only 509 // In the DevTools popup menu, "developer items" is normally the only
449 // section, so omit the separator there. 510 // section, so omit the separator there.
450 if (menu_model_.GetItemCount() > 0) 511 if (menu_model_.GetItemCount() > 0)
451 menu_model_.AddSeparator(); 512 menu_model_.AddSeparator();
452 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_INSPECTELEMENT, 513 menu_model_.AddItemWithStringId(IDC_CONTENT_CONTEXT_INSPECTELEMENT,
453 IDS_CONTENT_CONTEXT_INSPECTELEMENT); 514 IDS_CONTENT_CONTEXT_INSPECTELEMENT);
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
766 CONTENT_RESTRICTION_SAVE)) { 827 CONTENT_RESTRICTION_SAVE)) {
767 return false; 828 return false;
768 } 829 }
769 830
770 // Allow Spell Check language items on sub menu for text area context menu. 831 // Allow Spell Check language items on sub menu for text area context menu.
771 if ((id >= IDC_SPELLCHECK_LANGUAGES_FIRST) && 832 if ((id >= IDC_SPELLCHECK_LANGUAGES_FIRST) &&
772 (id < IDC_SPELLCHECK_LANGUAGES_LAST)) { 833 (id < IDC_SPELLCHECK_LANGUAGES_LAST)) {
773 return profile_->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck); 834 return profile_->GetPrefs()->GetBoolean(prefs::kEnableSpellCheck);
774 } 835 }
775 836
776 // Process custom actions range. 837 // Custom items.
777 if ((id >= IDC_CONTENT_CONTEXT_CUSTOM_FIRST) &&
778 (id < IDC_CONTENT_CONTEXT_CUSTOM_LAST)) {
779 unsigned action = id - IDC_CONTENT_CONTEXT_CUSTOM_FIRST;
780 for (size_t i = 0; i < params_.custom_items.size(); ++i) {
781 if (params_.custom_items[i].action == action)
782 return params_.custom_items[i].enabled;
783 }
784 NOTREACHED();
785 return false;
786 }
787
788 // Custom WebKit items.
789 if (id >= IDC_CONTENT_CONTEXT_CUSTOM_FIRST && 838 if (id >= IDC_CONTENT_CONTEXT_CUSTOM_FIRST &&
790 id <= IDC_CONTENT_CONTEXT_CUSTOM_LAST) { 839 id <= IDC_CONTENT_CONTEXT_CUSTOM_LAST) {
791 const std::vector<WebMenuItem>& custom_items = params_.custom_items; 840 return IsCustomItemEnabled(params_.custom_items, id);
792 for (size_t i = 0; i < custom_items.size(); ++i) {
793 int action_id = IDC_CONTENT_CONTEXT_CUSTOM_FIRST + custom_items[i].action;
794 if (action_id == id)
795 return custom_items[i].enabled;
796 }
797 return true;
798 } 841 }
799 842
800 // Extension items. 843 // Extension items.
801 if (id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && 844 if (id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
802 id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { 845 id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
803 // In the future we may add APIs for extensions to disable items, but for 846 // In the future we may add APIs for extensions to disable items, but for
804 // now all items are implicitly enabled. 847 // now all items are implicitly enabled.
805 return true; 848 return true;
806 } 849 }
807 850
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
1013 if (id == IDC_CONTENT_CONTEXT_LOOP) { 1056 if (id == IDC_CONTENT_CONTEXT_LOOP) {
1014 return (params_.media_flags & 1057 return (params_.media_flags &
1015 WebContextMenuData::MediaLoop) != 0; 1058 WebContextMenuData::MediaLoop) != 0;
1016 } 1059 }
1017 1060
1018 if (id == IDC_CONTENT_CONTEXT_CONTROLS) { 1061 if (id == IDC_CONTENT_CONTEXT_CONTROLS) {
1019 return (params_.media_flags & 1062 return (params_.media_flags &
1020 WebContextMenuData::MediaControls) != 0; 1063 WebContextMenuData::MediaControls) != 0;
1021 } 1064 }
1022 1065
1023 // Custom WebKit items. 1066 // Custom items.
1024 if (id >= IDC_CONTENT_CONTEXT_CUSTOM_FIRST && 1067 if (id >= IDC_CONTENT_CONTEXT_CUSTOM_FIRST &&
1025 id <= IDC_CONTENT_CONTEXT_CUSTOM_LAST) { 1068 id <= IDC_CONTENT_CONTEXT_CUSTOM_LAST) {
1026 const std::vector<WebMenuItem>& custom_items = params_.custom_items; 1069 return IsCustomItemChecked(params_.custom_items, id);
1027 for (size_t i = 0; i < custom_items.size(); ++i) {
1028 int action_id = IDC_CONTENT_CONTEXT_CUSTOM_FIRST + custom_items[i].action;
1029 if (action_id == id)
1030 return custom_items[i].checked;
1031 }
1032 return false;
1033 } 1070 }
1034 1071
1035 // Extension items. 1072 // Extension items.
1036 if (id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && 1073 if (id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
1037 id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { 1074 id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
1038 ExtensionMenuItem* item = GetExtensionMenuItem(id); 1075 ExtensionMenuItem* item = GetExtensionMenuItem(id);
1039 if (item) 1076 if (item)
1040 return item->checked(); 1077 return item->checked();
1041 else 1078 else
1042 return false; 1079 return false;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
1083 if (language_number < languages.size()) { 1120 if (language_number < languages.size()) {
1084 StringPrefMember dictionary_language; 1121 StringPrefMember dictionary_language;
1085 dictionary_language.Init(prefs::kSpellCheckDictionary, 1122 dictionary_language.Init(prefs::kSpellCheckDictionary,
1086 profile_->GetPrefs(), NULL); 1123 profile_->GetPrefs(), NULL);
1087 dictionary_language.SetValue(languages[language_number]); 1124 dictionary_language.SetValue(languages[language_number]);
1088 } 1125 }
1089 return; 1126 return;
1090 } 1127 }
1091 1128
1092 // Process custom actions range. 1129 // Process custom actions range.
1093 if ((id >= IDC_CONTENT_CONTEXT_CUSTOM_FIRST) && 1130 if (id >= IDC_CONTENT_CONTEXT_CUSTOM_FIRST &&
1094 (id < IDC_CONTENT_CONTEXT_CUSTOM_LAST)) { 1131 id <= IDC_CONTENT_CONTEXT_CUSTOM_LAST) {
1095 unsigned action = id - IDC_CONTENT_CONTEXT_CUSTOM_FIRST; 1132 unsigned action = id - IDC_CONTENT_CONTEXT_CUSTOM_FIRST;
1096 source_tab_contents_->render_view_host()-> 1133 source_tab_contents_->render_view_host()->PerformCustomContextMenuAction(
1097 PerformCustomContextMenuAction(action); 1134 params_.custom_context, action);
1098 return; 1135 return;
1099 } 1136 }
1100 1137
1101 // Process extension menu items. 1138 // Process extension menu items.
1102 if (id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST && 1139 if (id >= IDC_EXTENSIONS_CONTEXT_CUSTOM_FIRST &&
1103 id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) { 1140 id <= IDC_EXTENSIONS_CONTEXT_CUSTOM_LAST) {
1104 ExtensionMenuManager* manager = 1141 ExtensionMenuManager* manager =
1105 profile_->GetExtensionService()->menu_manager(); 1142 profile_->GetExtensionService()->menu_manager();
1106 std::map<int, ExtensionMenuItem::Id>::const_iterator i = 1143 std::map<int, ExtensionMenuItem::Id>::const_iterator i =
1107 extension_item_map_.find(id); 1144 extension_item_map_.find(id);
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
1392 break; 1429 break;
1393 #endif // OS_MACOSX 1430 #endif // OS_MACOSX
1394 1431
1395 default: 1432 default:
1396 NOTREACHED(); 1433 NOTREACHED();
1397 break; 1434 break;
1398 } 1435 }
1399 } 1436 }
1400 1437
1401 void RenderViewContextMenu::MenuClosed() { 1438 void RenderViewContextMenu::MenuClosed() {
1402 source_tab_contents_->render_view_host()->ContextMenuClosed(); 1439 source_tab_contents_->render_view_host()->ContextMenuClosed(
1440 params_.custom_context);
1403 } 1441 }
1404 1442
1405 bool RenderViewContextMenu::IsDevCommandEnabled(int id) const { 1443 bool RenderViewContextMenu::IsDevCommandEnabled(int id) const {
1406 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 1444 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
1407 if (command_line.HasSwitch(switches::kAlwaysEnableDevTools)) 1445 if (command_line.HasSwitch(switches::kAlwaysEnableDevTools))
1408 return true; 1446 return true;
1409 1447
1410 NavigationEntry *active_entry = 1448 NavigationEntry *active_entry =
1411 source_tab_contents_->controller().GetActiveEntry(); 1449 source_tab_contents_->controller().GetActiveEntry();
1412 if (!active_entry) 1450 if (!active_entry)
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1472 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), 1510 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages),
1473 g_browser_process->clipboard()); 1511 g_browser_process->clipboard());
1474 } 1512 }
1475 1513
1476 void RenderViewContextMenu::MediaPlayerActionAt( 1514 void RenderViewContextMenu::MediaPlayerActionAt(
1477 const gfx::Point& location, 1515 const gfx::Point& location,
1478 const WebMediaPlayerAction& action) { 1516 const WebMediaPlayerAction& action) {
1479 source_tab_contents_->render_view_host()->MediaPlayerActionAt( 1517 source_tab_contents_->render_view_host()->MediaPlayerActionAt(
1480 location, action); 1518 location, action);
1481 } 1519 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698