1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
11 #include "apps/app_load_service.h"
12 #include "base/command_line.h"
13 #include "base/logging.h"
14 #include "base/metrics/histogram.h"
15 #include "base/prefs/pref_member.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/stl_util.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "chrome/app/chrome_command_ids.h"
22 #include "chrome/browser/app_mode/app_mode_utils.h"
23 #include "chrome/browser/autocomplete/autocomplete_classifier.h"
24 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
25 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/chrome_notification_types.h"
27 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
28 #include "chrome/browser/devtools/devtools_window.h"
29 #include "chrome/browser/download/download_service.h"
30 #include "chrome/browser/download/download_service_factory.h"
31 #include "chrome/browser/download/download_stats.h"
32 #include "chrome/browser/extensions/devtools_util.h"
33 #include "chrome/browser/extensions/extension_service.h"
34 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
35 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
36 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
37 #include "chrome/browser/prefs/incognito_mode_prefs.h"
38 #include "chrome/browser/profiles/profile.h"
39 #include "chrome/browser/profiles/profile_io_data.h"
40 #include "chrome/browser/renderer_context_menu/context_menu_content_type_factory.h"
41 #include "chrome/browser/renderer_context_menu/spellchecker_submenu_observer.h"
42 #include "chrome/browser/renderer_context_menu/spelling_menu_observer.h"
43 #include "chrome/browser/search/search.h"
44 #include "chrome/browser/search_engines/template_url_service_factory.h"
45 #include "chrome/browser/spellchecker/spellcheck_host_metrics.h"
46 #include "chrome/browser/spellchecker/spellcheck_service.h"
47 #include "chrome/browser/tab_contents/retargeting_details.h"
48 #include "chrome/browser/translate/chrome_translate_client.h"
49 #include "chrome/browser/translate/translate_service.h"
50 #include "chrome/browser/ui/browser.h"
51 #include "chrome/browser/ui/browser_commands.h"
52 #include "chrome/browser/ui/browser_finder.h"
53 #include "chrome/browser/ui/chrome_pages.h"
54 #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h"
55 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
56 #include "chrome/common/chrome_constants.h"
57 #include "chrome/common/chrome_switches.h"
58 #include "chrome/common/content_restriction.h"
59 #include "chrome/common/net/url_util.h"
60 #include "chrome/common/pref_names.h"
61 #include "chrome/common/render_messages.h"
62 #include "chrome/common/spellcheck_messages.h"
63 #include "chrome/common/url_constants.h"
64 #include "chrome/grit/generated_resources.h"
65 #include "components/google/core/browser/google_util.h"
66 #include "components/metrics/proto/omnibox_input_type.pb.h"
67 #include "components/omnibox/autocomplete_match.h"
68 #include "components/search_engines/template_url.h"
69 #include "components/search_engines/template_url_service.h"
70 #include "components/translate/core/browser/translate_download_manager.h"
71 #include "components/translate/core/browser/translate_manager.h"
72 #include "components/translate/core/browser/translate_prefs.h"
73 #include "components/user_prefs/user_prefs.h"
74 #include "content/public/browser/child_process_security_policy.h"
75 #include "content/public/browser/download_manager.h"
76 #include "content/public/browser/download_save_info.h"
77 #include "content/public/browser/download_url_parameters.h"
78 #include "content/public/browser/navigation_details.h"
79 #include "content/public/browser/navigation_entry.h"
80 #include "content/public/browser/notification_service.h"
81 #include "content/public/browser/render_frame_host.h"
82 #include "content/public/browser/render_process_host.h"
83 #include "content/public/browser/render_view_host.h"
84 #include "content/public/browser/render_widget_host_view.h"
85 #include "content/public/browser/user_metrics.h"
86 #include "content/public/browser/web_contents.h"
87 #include "content/public/common/menu_item.h"
88 #include "content/public/common/ssl_status.h"
89 #include "content/public/common/url_utils.h"
90 #include "extensions/browser/extension_host.h"
91 #include "extensions/browser/extension_system.h"
92 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
93 #include "extensions/browser/view_type_utils.h"
94 #include "extensions/common/extension.h"
95 #include "net/base/escape.h"
96 #include "third_party/WebKit/public/web/WebContextMenuData.h"
97 #include "third_party/WebKit/public/web/WebMediaPlayerAction.h"
98 #include "third_party/WebKit/public/web/WebPluginAction.h"
99 #include "ui/base/clipboard/clipboard.h"
100 #include "ui/base/l10n/l10n_util.h"
101 #include "ui/gfx/favicon_size.h"
102 #include "ui/gfx/geometry/point.h"
103 #include "ui/gfx/geometry/size.h"
104 #include "ui/gfx/text_elider.h"
106 #if defined(ENABLE_PRINTING)
107 #include "chrome/browser/printing/print_view_manager_common.h"
108 #include "components/printing/common/print_messages.h"
110 #if defined(ENABLE_PRINT_PREVIEW)
111 #include "chrome/browser/printing/print_preview_context_menu_observer.h"
112 #include "chrome/browser/printing/print_preview_dialog_controller.h"
113 #endif // defined(ENABLE_PRINT_PREVIEW)
114 #endif // defined(ENABLE_PRINTING)
116 using base::UserMetricsAction
;
117 using blink::WebContextMenuData
;
118 using blink::WebMediaPlayerAction
;
119 using blink::WebPluginAction
;
120 using blink::WebString
;
122 using content::BrowserContext
;
123 using content::ChildProcessSecurityPolicy
;
124 using content::DownloadManager
;
125 using content::DownloadUrlParameters
;
126 using content::NavigationController
;
127 using content::NavigationEntry
;
128 using content::OpenURLParams
;
129 using content::RenderFrameHost
;
130 using content::RenderViewHost
;
131 using content::SSLStatus
;
132 using content::WebContents
;
133 using extensions::ContextMenuMatcher
;
134 using extensions::Extension
;
135 using extensions::MenuItem
;
136 using extensions::MenuManager
;
140 const int kImageSearchThumbnailMinSize
= 300 * 300;
141 const int kImageSearchThumbnailMaxWidth
= 600;
142 const int kImageSearchThumbnailMaxHeight
= 600;
144 // Maps UMA enumeration to IDC. IDC could be changed so we can't use
145 // just them and |UMA_HISTOGRAM_CUSTOM_ENUMERATION|.
146 // Never change mapping or reuse |enum_id|. Always push back new items.
147 // Items that is not used any more by |RenderViewContextMenu.ExecuteCommand|
148 // could be deleted, but don't change the rest of |kUmaEnumToControlId|.
149 const struct UmaEnumCommandIdPair
{
152 } kUmaEnumToControlId
[] = {
154 enum id for 0, 1 are detected using
155 RenderViewContextMenu::IsContentCustomCommandId and
156 ContextMenuMatcher::IsExtensionsCustomCommandId
158 {2, IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
},
159 {3, IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
},
160 {4, IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
},
161 {5, IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
},
162 {6, IDC_CONTENT_CONTEXT_SAVELINKAS
},
163 {7, IDC_CONTENT_CONTEXT_SAVEAVAS
},
164 {8, IDC_CONTENT_CONTEXT_SAVEIMAGEAS
},
165 {9, IDC_CONTENT_CONTEXT_COPYLINKLOCATION
},
166 {10, IDC_CONTENT_CONTEXT_COPYIMAGELOCATION
},
167 {11, IDC_CONTENT_CONTEXT_COPYAVLOCATION
},
168 {12, IDC_CONTENT_CONTEXT_COPYIMAGE
},
169 {13, IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB
},
170 {14, IDC_CONTENT_CONTEXT_OPENAVNEWTAB
},
171 {15, IDC_CONTENT_CONTEXT_PLAYPAUSE
},
172 {16, IDC_CONTENT_CONTEXT_MUTE
},
173 {17, IDC_CONTENT_CONTEXT_LOOP
},
174 {18, IDC_CONTENT_CONTEXT_CONTROLS
},
175 {19, IDC_CONTENT_CONTEXT_ROTATECW
},
176 {20, IDC_CONTENT_CONTEXT_ROTATECCW
},
181 {25, IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
},
182 {26, IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP
},
184 {28, IDC_VIEW_SOURCE
},
185 {29, IDC_CONTENT_CONTEXT_INSPECTELEMENT
},
186 {30, IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
},
187 {31, IDC_CONTENT_CONTEXT_VIEWPAGEINFO
},
188 {32, IDC_CONTENT_CONTEXT_TRANSLATE
},
189 {33, IDC_CONTENT_CONTEXT_RELOADFRAME
},
190 {34, IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE
},
191 {35, IDC_CONTENT_CONTEXT_VIEWFRAMEINFO
},
192 {36, IDC_CONTENT_CONTEXT_UNDO
},
193 {37, IDC_CONTENT_CONTEXT_REDO
},
194 {38, IDC_CONTENT_CONTEXT_CUT
},
195 {39, IDC_CONTENT_CONTEXT_COPY
},
196 {40, IDC_CONTENT_CONTEXT_PASTE
},
197 {41, IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
},
198 {42, IDC_CONTENT_CONTEXT_DELETE
},
199 {43, IDC_CONTENT_CONTEXT_SELECTALL
},
200 {44, IDC_CONTENT_CONTEXT_SEARCHWEBFOR
},
201 {45, IDC_CONTENT_CONTEXT_GOTOURL
},
202 {46, IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
},
203 {47, IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_SETTINGS
},
204 {48, IDC_CONTENT_CONTEXT_ADDSEARCHENGINE
},
205 {52, IDC_CONTENT_CONTEXT_OPENLINKWITH
},
206 {53, IDC_CHECK_SPELLING_WHILE_TYPING
},
207 {54, IDC_SPELLCHECK_MENU
},
208 {55, IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
},
209 {56, IDC_SPELLCHECK_LANGUAGES_FIRST
},
210 {57, IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
},
211 {58, IDC_SPELLCHECK_SUGGESTION_0
},
212 {59, IDC_SPELLCHECK_ADD_TO_DICTIONARY
},
213 {60, IDC_SPELLPANEL_TOGGLE
},
214 {61, IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
},
215 // Add new items here and use |enum_id| from the next line.
216 {62, 0}, // Must be the last. Increment |enum_id| when new IDC was added.
219 // Collapses large ranges of ids before looking for UMA enum.
220 int CollapseCommandsForUMA(int id
) {
221 DCHECK(!RenderViewContextMenu::IsContentCustomCommandId(id
));
222 DCHECK(!ContextMenuMatcher::IsExtensionsCustomCommandId(id
));
224 if (id
>= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
&&
225 id
<= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST
) {
226 return IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
;
229 if (id
>= IDC_SPELLCHECK_LANGUAGES_FIRST
&&
230 id
<= IDC_SPELLCHECK_LANGUAGES_LAST
) {
231 return IDC_SPELLCHECK_LANGUAGES_FIRST
;
234 if (id
>= IDC_SPELLCHECK_SUGGESTION_0
&&
235 id
<= IDC_SPELLCHECK_SUGGESTION_LAST
) {
236 return IDC_SPELLCHECK_SUGGESTION_0
;
242 // Returns UMA enum value for command specified by |id| or -1 if not found.
243 int FindUMAEnumValueForCommand(int id
) {
244 if (RenderViewContextMenu::IsContentCustomCommandId(id
))
247 if (ContextMenuMatcher::IsExtensionsCustomCommandId(id
))
250 id
= CollapseCommandsForUMA(id
);
251 const size_t kMappingSize
= arraysize(kUmaEnumToControlId
);
252 for (size_t i
= 0; i
< kMappingSize
; ++i
) {
253 if (kUmaEnumToControlId
[i
].control_id
== id
) {
254 return kUmaEnumToControlId
[i
].enum_id
;
260 // Usually a new tab is expected where this function is used,
261 // however users should be able to open a tab in background
262 // or in a new window.
263 WindowOpenDisposition
ForceNewTabDispositionFromEventFlags(
265 WindowOpenDisposition disposition
=
266 ui::DispositionFromEventFlags(event_flags
);
267 return disposition
== CURRENT_TAB
? NEW_FOREGROUND_TAB
: disposition
;
270 // Helper function to escape "&" as "&&".
271 void EscapeAmpersands(base::string16
* text
) {
272 base::ReplaceChars(*text
, base::ASCIIToUTF16("&"), base::ASCIIToUTF16("&&"),
276 // Returns the preference of the profile represented by the |context|.
277 PrefService
* GetPrefs(content::BrowserContext
* context
) {
278 return user_prefs::UserPrefs::Get(context
);
281 bool ExtensionPatternMatch(const extensions::URLPatternSet
& patterns
,
283 // No patterns means no restriction, so that implicitly matches.
284 if (patterns
.is_empty())
286 return patterns
.MatchesURL(url
);
289 const GURL
& GetDocumentURL(const content::ContextMenuParams
& params
) {
290 return params
.frame_url
.is_empty() ? params
.page_url
: params
.frame_url
;
293 content::Referrer
CreateSaveAsReferrer(
295 const content::ContextMenuParams
& params
) {
296 const GURL
& referring_url
= GetDocumentURL(params
);
297 return content::Referrer::SanitizeForRequest(
299 content::Referrer(referring_url
.GetAsReferrer(), params
.referrer_policy
));
302 bool g_custom_id_ranges_initialized
= false;
304 const int kSpellcheckRadioGroup
= 1;
309 bool RenderViewContextMenu::IsDevToolsURL(const GURL
& url
) {
310 return url
.SchemeIs(content::kChromeDevToolsScheme
);
314 bool RenderViewContextMenu::IsInternalResourcesURL(const GURL
& url
) {
315 if (!url
.SchemeIs(content::kChromeUIScheme
))
317 return url
.host() == chrome::kChromeUISyncResourcesHost
;
320 RenderViewContextMenu::RenderViewContextMenu(
321 content::RenderFrameHost
* render_frame_host
,
322 const content::ContextMenuParams
& params
)
323 : RenderViewContextMenuBase(render_frame_host
, params
),
324 extension_items_(browser_context_
,
327 base::Bind(MenuItemMatchesParams
, params_
)),
328 protocol_handler_submenu_model_(this),
329 protocol_handler_registry_(
330 ProtocolHandlerRegistryFactory::GetForBrowserContext(GetProfile())) {
331 if (!g_custom_id_ranges_initialized
) {
332 g_custom_id_ranges_initialized
= true;
333 SetContentCustomCommandIdRange(IDC_CONTENT_CONTEXT_CUSTOM_FIRST
,
334 IDC_CONTENT_CONTEXT_CUSTOM_LAST
);
336 set_content_type(ContextMenuContentTypeFactory::Create(
337 source_web_contents_
, params
));
340 RenderViewContextMenu::~RenderViewContextMenu() {
343 // Menu construction functions -------------------------------------------------
345 #if defined(ENABLE_EXTENSIONS)
347 bool RenderViewContextMenu::ExtensionContextAndPatternMatch(
348 const content::ContextMenuParams
& params
,
349 const MenuItem::ContextList
& contexts
,
350 const extensions::URLPatternSet
& target_url_patterns
) {
351 const bool has_link
= !params
.link_url
.is_empty();
352 const bool has_selection
= !params
.selection_text
.empty();
353 const bool in_frame
= !params
.frame_url
.is_empty();
355 if (contexts
.Contains(MenuItem::ALL
) ||
356 (has_selection
&& contexts
.Contains(MenuItem::SELECTION
)) ||
357 (params
.is_editable
&& contexts
.Contains(MenuItem::EDITABLE
)) ||
358 (in_frame
&& contexts
.Contains(MenuItem::FRAME
)))
361 if (has_link
&& contexts
.Contains(MenuItem::LINK
) &&
362 ExtensionPatternMatch(target_url_patterns
, params
.link_url
))
365 switch (params
.media_type
) {
366 case WebContextMenuData::MediaTypeImage
:
367 if (contexts
.Contains(MenuItem::IMAGE
) &&
368 ExtensionPatternMatch(target_url_patterns
, params
.src_url
))
372 case WebContextMenuData::MediaTypeVideo
:
373 if (contexts
.Contains(MenuItem::VIDEO
) &&
374 ExtensionPatternMatch(target_url_patterns
, params
.src_url
))
378 case WebContextMenuData::MediaTypeAudio
:
379 if (contexts
.Contains(MenuItem::AUDIO
) &&
380 ExtensionPatternMatch(target_url_patterns
, params
.src_url
))
388 // PAGE is the least specific context, so we only examine that if none of the
389 // other contexts apply (except for FRAME, which is included in PAGE for
390 // backwards compatibility).
391 if (!has_link
&& !has_selection
&& !params
.is_editable
&&
392 params
.media_type
== WebContextMenuData::MediaTypeNone
&&
393 contexts
.Contains(MenuItem::PAGE
))
400 bool RenderViewContextMenu::MenuItemMatchesParams(
401 const content::ContextMenuParams
& params
,
402 const extensions::MenuItem
* item
) {
403 bool match
= ExtensionContextAndPatternMatch(params
, item
->contexts(),
404 item
->target_url_patterns());
408 const GURL
& document_url
= GetDocumentURL(params
);
409 return ExtensionPatternMatch(item
->document_url_patterns(), document_url
);
412 void RenderViewContextMenu::AppendAllExtensionItems() {
413 extension_items_
.Clear();
414 ExtensionService
* service
=
415 extensions::ExtensionSystem::Get(browser_context_
)->extension_service();
417 return; // In unit-tests, we may not have an ExtensionService.
419 MenuManager
* menu_manager
= MenuManager::Get(browser_context_
);
423 base::string16 printable_selection_text
= PrintableSelectionText();
424 EscapeAmpersands(&printable_selection_text
);
426 // Get a list of extension id's that have context menu items, and sort by the
427 // top level context menu title of the extension.
428 std::set
<MenuItem::ExtensionKey
> ids
= menu_manager
->ExtensionIds();
429 std::vector
<base::string16
> sorted_menu_titles
;
430 std::map
<base::string16
, std::string
> map_ids
;
431 for (std::set
<MenuItem::ExtensionKey
>::iterator iter
= ids
.begin();
434 const Extension
* extension
=
435 service
->GetExtensionById(iter
->extension_id
, false);
436 // Platform apps have their context menus created directly in
437 // AppendPlatformAppItems.
438 if (extension
&& !extension
->is_platform_app()) {
439 base::string16 menu_title
= extension_items_
.GetTopLevelContextMenuTitle(
440 *iter
, printable_selection_text
);
441 map_ids
[menu_title
] = iter
->extension_id
;
442 sorted_menu_titles
.push_back(menu_title
);
445 if (sorted_menu_titles
.empty())
448 const std::string app_locale
= g_browser_process
->GetApplicationLocale();
449 l10n_util::SortStrings16(app_locale
, &sorted_menu_titles
);
452 for (size_t i
= 0; i
< sorted_menu_titles
.size(); ++i
) {
453 const std::string
& id
= map_ids
[sorted_menu_titles
[i
]];
454 const MenuItem::ExtensionKey
extension_key(id
);
455 extension_items_
.AppendExtensionItems(extension_key
,
456 printable_selection_text
,
458 false); // is_action_menu
462 void RenderViewContextMenu::AppendCurrentExtensionItems() {
463 // Avoid appending extension related items when |extension| is null.
464 // For Panel, this happens when the panel is navigated to a url outside of the
465 // extension's package.
466 const Extension
* extension
= GetExtension();
468 // Only add extension items from this extension.
470 const MenuItem::ExtensionKey
key(
472 extensions::WebViewGuest::GetViewInstanceId(source_web_contents_
));
473 extension_items_
.AppendExtensionItems(key
,
474 PrintableSelectionText(),
476 false); // is_action_menu
479 #endif // defined(ENABLE_EXTENSIONS)
481 void RenderViewContextMenu::InitMenu() {
482 RenderViewContextMenuBase::InitMenu();
484 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PAGE
))
487 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_FRAME
)) {
488 // Merge in frame items with page items if we clicked within a frame that
490 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
494 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_LINK
)) {
496 if (params_
.media_type
!= WebContextMenuData::MediaTypeNone
)
497 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
500 if (content_type_
->SupportsGroup(
501 ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE
)) {
505 if (content_type_
->SupportsGroup(
506 ContextMenuContentType::ITEM_GROUP_SEARCHWEBFORIMAGE
)) {
507 AppendSearchWebForImageItems();
510 if (content_type_
->SupportsGroup(
511 ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO
)) {
515 if (content_type_
->SupportsGroup(
516 ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO
)) {
520 if (content_type_
->SupportsGroup(
521 ContextMenuContentType::ITEM_GROUP_MEDIA_CANVAS
)) {
525 if (content_type_
->SupportsGroup(
526 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN
)) {
530 // ITEM_GROUP_MEDIA_FILE has no specific items.
532 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_EDITABLE
))
533 AppendEditableItems();
535 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_COPY
)) {
536 DCHECK(!content_type_
->SupportsGroup(
537 ContextMenuContentType::ITEM_GROUP_EDITABLE
));
541 if (content_type_
->SupportsGroup(
542 ContextMenuContentType::ITEM_GROUP_SEARCH_PROVIDER
)) {
543 AppendSearchProvider();
546 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PRINT
))
549 if (content_type_
->SupportsGroup(
550 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN
)) {
551 AppendRotationItems();
554 if (content_type_
->SupportsGroup(
555 ContextMenuContentType::ITEM_GROUP_ALL_EXTENSION
)) {
556 DCHECK(!content_type_
->SupportsGroup(
557 ContextMenuContentType::ITEM_GROUP_CURRENT_EXTENSION
));
558 AppendAllExtensionItems();
561 if (content_type_
->SupportsGroup(
562 ContextMenuContentType::ITEM_GROUP_CURRENT_EXTENSION
)) {
563 DCHECK(!content_type_
->SupportsGroup(
564 ContextMenuContentType::ITEM_GROUP_ALL_EXTENSION
));
565 AppendCurrentExtensionItems();
568 if (content_type_
->SupportsGroup(
569 ContextMenuContentType::ITEM_GROUP_DEVELOPER
)) {
570 AppendDeveloperItems();
573 if (content_type_
->SupportsGroup(
574 ContextMenuContentType::ITEM_GROUP_DEVTOOLS_UNPACKED_EXT
)) {
575 AppendDevtoolsForUnpackedExtensions();
578 if (content_type_
->SupportsGroup(
579 ContextMenuContentType::ITEM_GROUP_PRINT_PREVIEW
)) {
580 AppendPrintPreviewItems();
584 Profile
* RenderViewContextMenu::GetProfile() {
585 return Profile::FromBrowserContext(browser_context_
);
588 void RenderViewContextMenu::RecordUsedItem(int id
) {
589 int enum_id
= FindUMAEnumValueForCommand(id
);
591 const size_t kMappingSize
= arraysize(kUmaEnumToControlId
);
592 UMA_HISTOGRAM_ENUMERATION("RenderViewContextMenu.Used", enum_id
,
593 kUmaEnumToControlId
[kMappingSize
- 1].enum_id
);
595 NOTREACHED() << "Update kUmaEnumToControlId. Unhanded IDC: " << id
;
599 void RenderViewContextMenu::RecordShownItem(int id
) {
600 int enum_id
= FindUMAEnumValueForCommand(id
);
602 const size_t kMappingSize
= arraysize(kUmaEnumToControlId
);
603 UMA_HISTOGRAM_ENUMERATION("RenderViewContextMenu.Shown", enum_id
,
604 kUmaEnumToControlId
[kMappingSize
- 1].enum_id
);
606 // Just warning here. It's harder to maintain list of all possibly
607 // visible items than executable items.
608 DLOG(ERROR
) << "Update kUmaEnumToControlId. Unhanded IDC: " << id
;
612 #if defined(ENABLE_PLUGINS)
613 void RenderViewContextMenu::HandleAuthorizeAllPlugins() {
614 ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
615 source_web_contents_
, false, std::string());
619 void RenderViewContextMenu::AppendPrintPreviewItems() {
620 #if defined(ENABLE_PRINT_PREVIEW)
621 if (!print_preview_menu_observer_
.get()) {
622 print_preview_menu_observer_
.reset(
623 new PrintPreviewContextMenuObserver(source_web_contents_
));
626 observers_
.AddObserver(print_preview_menu_observer_
.get());
630 const Extension
* RenderViewContextMenu::GetExtension() const {
631 return extensions::ProcessManager::Get(browser_context_
)
632 ->GetExtensionForRenderViewHost(
633 source_web_contents_
->GetRenderViewHost());
636 void RenderViewContextMenu::AppendDeveloperItems() {
637 // Show Inspect Element in DevTools itself only in case of the debug
639 bool show_developer_items
= !IsDevToolsURL(params_
.page_url
);
641 #if defined(DEBUG_DEVTOOLS)
642 show_developer_items
= true;
645 if (!show_developer_items
)
648 // In the DevTools popup menu, "developer items" is normally the only
649 // section, so omit the separator there.
650 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
651 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_INSPECTELEMENT
,
652 IDS_CONTENT_CONTEXT_INSPECTELEMENT
);
655 void RenderViewContextMenu::AppendDevtoolsForUnpackedExtensions() {
656 // Add a separator if there are any items already in the menu.
657 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
659 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
,
660 IDS_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
);
661 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP
,
662 IDS_CONTENT_CONTEXT_RESTART_APP
);
663 AppendDeveloperItems();
664 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
,
665 IDS_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
);
668 void RenderViewContextMenu::AppendLinkItems() {
669 if (!params_
.link_url
.is_empty()) {
670 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
,
671 IDS_CONTENT_CONTEXT_OPENLINKNEWTAB
);
672 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
,
673 IDS_CONTENT_CONTEXT_OPENLINKNEWWINDOW
);
674 if (params_
.link_url
.is_valid()) {
675 AppendProtocolHandlerSubMenu();
678 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
,
679 IDS_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
);
680 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVELINKAS
,
681 IDS_CONTENT_CONTEXT_SAVELINKAS
);
684 menu_model_
.AddItemWithStringId(
685 IDC_CONTENT_CONTEXT_COPYLINKLOCATION
,
686 params_
.link_url
.SchemeIs(url::kMailToScheme
) ?
687 IDS_CONTENT_CONTEXT_COPYEMAILADDRESS
:
688 IDS_CONTENT_CONTEXT_COPYLINKLOCATION
);
691 void RenderViewContextMenu::AppendImageItems() {
692 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEIMAGEAS
,
693 IDS_CONTENT_CONTEXT_SAVEIMAGEAS
);
694 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYIMAGELOCATION
,
695 IDS_CONTENT_CONTEXT_COPYIMAGELOCATION
);
696 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYIMAGE
,
697 IDS_CONTENT_CONTEXT_COPYIMAGE
);
698 if (!browser_context_
->IsOffTheRecord() &&
699 DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
700 browser_context_
)->CanUseDataReductionProxy(params_
.src_url
)) {
701 menu_model_
.AddItemWithStringId(
702 IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
,
703 IDS_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
);
705 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB
,
706 IDS_CONTENT_CONTEXT_OPENIMAGENEWTAB
);
710 void RenderViewContextMenu::AppendSearchWebForImageItems() {
711 TemplateURLService
* service
=
712 TemplateURLServiceFactory::GetForProfile(GetProfile());
713 const TemplateURL
* const default_provider
=
714 service
->GetDefaultSearchProvider();
715 if (params_
.has_image_contents
&& default_provider
&&
716 !default_provider
->image_url().empty() &&
717 default_provider
->image_url_ref().IsValid(service
->search_terms_data())) {
719 IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
,
720 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
,
721 default_provider
->short_name()));
725 void RenderViewContextMenu::AppendAudioItems() {
727 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
728 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEAVAS
,
729 IDS_CONTENT_CONTEXT_SAVEAUDIOAS
);
730 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYAVLOCATION
,
731 IDS_CONTENT_CONTEXT_COPYAUDIOLOCATION
);
732 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENAVNEWTAB
,
733 IDS_CONTENT_CONTEXT_OPENAUDIONEWTAB
);
736 void RenderViewContextMenu::AppendCanvasItems() {
737 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEIMAGEAS
,
738 IDS_CONTENT_CONTEXT_SAVEIMAGEAS
);
739 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYIMAGE
,
740 IDS_CONTENT_CONTEXT_COPYIMAGE
);
743 void RenderViewContextMenu::AppendVideoItems() {
745 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
746 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEAVAS
,
747 IDS_CONTENT_CONTEXT_SAVEVIDEOAS
);
748 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYAVLOCATION
,
749 IDS_CONTENT_CONTEXT_COPYVIDEOLOCATION
);
750 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENAVNEWTAB
,
751 IDS_CONTENT_CONTEXT_OPENVIDEONEWTAB
);
754 void RenderViewContextMenu::AppendMediaItems() {
755 int media_flags
= params_
.media_flags
;
757 menu_model_
.AddItemWithStringId(
758 IDC_CONTENT_CONTEXT_PLAYPAUSE
,
759 media_flags
& WebContextMenuData::MediaPaused
?
760 IDS_CONTENT_CONTEXT_PLAY
:
761 IDS_CONTENT_CONTEXT_PAUSE
);
763 menu_model_
.AddItemWithStringId(
764 IDC_CONTENT_CONTEXT_MUTE
,
765 media_flags
& WebContextMenuData::MediaMuted
?
766 IDS_CONTENT_CONTEXT_UNMUTE
:
767 IDS_CONTENT_CONTEXT_MUTE
);
769 menu_model_
.AddCheckItemWithStringId(IDC_CONTENT_CONTEXT_LOOP
,
770 IDS_CONTENT_CONTEXT_LOOP
);
771 menu_model_
.AddCheckItemWithStringId(IDC_CONTENT_CONTEXT_CONTROLS
,
772 IDS_CONTENT_CONTEXT_CONTROLS
);
775 void RenderViewContextMenu::AppendPluginItems() {
776 if (params_
.page_url
== params_
.src_url
||
777 extensions::GuestViewBase::IsGuest(source_web_contents_
)) {
778 // Full page plugin, so show page menu items.
779 if (params_
.link_url
.is_empty() && params_
.selection_text
.empty())
782 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEAVAS
,
783 IDS_CONTENT_CONTEXT_SAVEPAGEAS
);
784 // The "Print" menu item should always be included for plugins. If
785 // content_type_->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PRINT)
786 // is true the item will be added inside AppendPrintItem(). Otherwise we
788 if (!content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PRINT
))
789 menu_model_
.AddItemWithStringId(IDC_PRINT
, IDS_CONTENT_CONTEXT_PRINT
);
793 void RenderViewContextMenu::AppendPageItems() {
794 menu_model_
.AddItemWithStringId(IDC_BACK
, IDS_CONTENT_CONTEXT_BACK
);
795 menu_model_
.AddItemWithStringId(IDC_FORWARD
, IDS_CONTENT_CONTEXT_FORWARD
);
796 menu_model_
.AddItemWithStringId(IDC_RELOAD
, IDS_CONTENT_CONTEXT_RELOAD
);
797 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
798 menu_model_
.AddItemWithStringId(IDC_SAVE_PAGE
,
799 IDS_CONTENT_CONTEXT_SAVEPAGEAS
);
800 menu_model_
.AddItemWithStringId(IDC_PRINT
, IDS_CONTENT_CONTEXT_PRINT
);
802 if (TranslateService::IsTranslatableURL(params_
.page_url
)) {
803 std::string locale
= g_browser_process
->GetApplicationLocale();
804 locale
= translate::TranslateDownloadManager::GetLanguageCode(locale
);
805 base::string16 language
=
806 l10n_util::GetDisplayNameForLocale(locale
, locale
, true);
808 IDC_CONTENT_CONTEXT_TRANSLATE
,
809 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_TRANSLATE
, language
));
812 menu_model_
.AddItemWithStringId(IDC_VIEW_SOURCE
,
813 IDS_CONTENT_CONTEXT_VIEWPAGESOURCE
);
814 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_VIEWPAGEINFO
,
815 IDS_CONTENT_CONTEXT_VIEWPAGEINFO
);
818 void RenderViewContextMenu::AppendFrameItems() {
819 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_RELOADFRAME
,
820 IDS_CONTENT_CONTEXT_RELOADFRAME
);
821 // These two menu items have yet to be implemented.
822 // http://code.google.com/p/chromium/issues/detail?id=11827
823 // IDS_CONTENT_CONTEXT_SAVEFRAMEAS
824 // IDS_CONTENT_CONTEXT_PRINTFRAME
825 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE
,
826 IDS_CONTENT_CONTEXT_VIEWFRAMESOURCE
);
827 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_VIEWFRAMEINFO
,
828 IDS_CONTENT_CONTEXT_VIEWFRAMEINFO
);
831 void RenderViewContextMenu::AppendCopyItem() {
832 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPY
,
833 IDS_CONTENT_CONTEXT_COPY
);
836 void RenderViewContextMenu::AppendPrintItem() {
837 if (GetPrefs(browser_context_
)->GetBoolean(prefs::kPrintingEnabled
) &&
838 (params_
.media_type
== WebContextMenuData::MediaTypeNone
||
839 params_
.media_flags
& WebContextMenuData::MediaCanPrint
)) {
840 menu_model_
.AddItemWithStringId(IDC_PRINT
, IDS_CONTENT_CONTEXT_PRINT
);
844 void RenderViewContextMenu::AppendRotationItems() {
845 if (params_
.media_flags
& WebContextMenuData::MediaCanRotate
) {
846 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
847 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_ROTATECW
,
848 IDS_CONTENT_CONTEXT_ROTATECW
);
849 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_ROTATECCW
,
850 IDS_CONTENT_CONTEXT_ROTATECCW
);
854 void RenderViewContextMenu::AppendSearchProvider() {
855 DCHECK(browser_context_
);
857 base::TrimWhitespace(params_
.selection_text
, base::TRIM_ALL
,
858 ¶ms_
.selection_text
);
859 if (params_
.selection_text
.empty())
862 base::ReplaceChars(params_
.selection_text
, AutocompleteMatch::kInvalidChars
,
863 base::ASCIIToUTF16(" "), ¶ms_
.selection_text
);
865 AutocompleteMatch match
;
866 AutocompleteClassifierFactory::GetForProfile(GetProfile())->Classify(
867 params_
.selection_text
,
870 metrics::OmniboxEventProto::INVALID_SPEC
,
873 selection_navigation_url_
= match
.destination_url
;
874 if (!selection_navigation_url_
.is_valid())
877 base::string16 printable_selection_text
= PrintableSelectionText();
878 EscapeAmpersands(&printable_selection_text
);
880 if (AutocompleteMatch::IsSearchType(match
.type
)) {
881 const TemplateURL
* const default_provider
=
882 TemplateURLServiceFactory::GetForProfile(GetProfile())
883 ->GetDefaultSearchProvider();
884 if (!default_provider
)
887 IDC_CONTENT_CONTEXT_SEARCHWEBFOR
,
888 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFOR
,
889 default_provider
->short_name(),
890 printable_selection_text
));
892 if ((selection_navigation_url_
!= params_
.link_url
) &&
893 ChildProcessSecurityPolicy::GetInstance()->IsWebSafeScheme(
894 selection_navigation_url_
.scheme())) {
896 IDC_CONTENT_CONTEXT_GOTOURL
,
897 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_GOTOURL
,
898 printable_selection_text
));
903 void RenderViewContextMenu::AppendEditableItems() {
904 const bool use_spellcheck_and_search
= !chrome::IsRunningInForcedAppMode();
906 if (use_spellcheck_and_search
)
907 AppendSpellingSuggestionsSubMenu();
909 if (!IsDevToolsURL(params_
.page_url
)) {
910 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_UNDO
,
911 IDS_CONTENT_CONTEXT_UNDO
);
912 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_REDO
,
913 IDS_CONTENT_CONTEXT_REDO
);
914 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
917 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_CUT
,
918 IDS_CONTENT_CONTEXT_CUT
);
919 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPY
,
920 IDS_CONTENT_CONTEXT_COPY
);
921 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE
,
922 IDS_CONTENT_CONTEXT_PASTE
);
923 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
,
924 IDS_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
);
925 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_DELETE
,
926 IDS_CONTENT_CONTEXT_DELETE
);
927 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
929 if (use_spellcheck_and_search
&& !params_
.keyword_url
.is_empty()) {
930 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_ADDSEARCHENGINE
,
931 IDS_CONTENT_CONTEXT_ADDSEARCHENGINE
);
932 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
935 if (use_spellcheck_and_search
)
936 AppendSpellcheckOptionsSubMenu();
937 AppendPlatformEditableItems();
939 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
940 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SELECTALL
,
941 IDS_CONTENT_CONTEXT_SELECTALL
);
944 void RenderViewContextMenu::AppendSpellingSuggestionsSubMenu() {
945 if (!spelling_menu_observer_
.get())
946 spelling_menu_observer_
.reset(new SpellingMenuObserver(this));
947 observers_
.AddObserver(spelling_menu_observer_
.get());
948 spelling_menu_observer_
->InitMenu(params_
);
951 void RenderViewContextMenu::AppendSpellcheckOptionsSubMenu() {
952 if (!spellchecker_submenu_observer_
.get()) {
953 spellchecker_submenu_observer_
.reset(new SpellCheckerSubMenuObserver(
954 this, this, kSpellcheckRadioGroup
));
956 spellchecker_submenu_observer_
->InitMenu(params_
);
957 observers_
.AddObserver(spellchecker_submenu_observer_
.get());
960 void RenderViewContextMenu::AppendProtocolHandlerSubMenu() {
961 const ProtocolHandlerRegistry::ProtocolHandlerList handlers
=
962 GetHandlersForLinkUrl();
963 if (handlers
.empty())
965 size_t max
= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST
-
966 IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
;
967 for (size_t i
= 0; i
< handlers
.size() && i
<= max
; i
++) {
968 protocol_handler_submenu_model_
.AddItem(
969 IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
+ i
,
970 base::UTF8ToUTF16(handlers
[i
].url().host()));
972 protocol_handler_submenu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
973 protocol_handler_submenu_model_
.AddItem(
974 IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_SETTINGS
,
975 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_OPENLINKWITH_CONFIGURE
));
977 menu_model_
.AddSubMenu(
978 IDC_CONTENT_CONTEXT_OPENLINKWITH
,
979 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_OPENLINKWITH
),
980 &protocol_handler_submenu_model_
);
983 // Menu delegate functions -----------------------------------------------------
985 bool RenderViewContextMenu::IsCommandIdEnabled(int id
) const {
987 bool enabled
= false;
988 if (RenderViewContextMenuBase::IsCommandIdKnown(id
, &enabled
))
992 CoreTabHelper
* core_tab_helper
=
993 CoreTabHelper::FromWebContents(source_web_contents_
);
994 int content_restrictions
= 0;
996 content_restrictions
= core_tab_helper
->content_restrictions();
997 if (id
== IDC_PRINT
&& (content_restrictions
& CONTENT_RESTRICTION_PRINT
))
1000 if (id
== IDC_SAVE_PAGE
&&
1001 (content_restrictions
& CONTENT_RESTRICTION_SAVE
)) {
1005 PrefService
* prefs
= GetPrefs(browser_context_
);
1007 // Allow Spell Check language items on sub menu for text area context menu.
1008 if ((id
>= IDC_SPELLCHECK_LANGUAGES_FIRST
) &&
1009 (id
< IDC_SPELLCHECK_LANGUAGES_LAST
)) {
1010 return prefs
->GetBoolean(prefs::kEnableContinuousSpellcheck
);
1014 if (ContextMenuMatcher::IsExtensionsCustomCommandId(id
))
1015 return extension_items_
.IsCommandIdEnabled(id
);
1017 if (id
>= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
&&
1018 id
<= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST
) {
1022 IncognitoModePrefs::Availability incognito_avail
=
1023 IncognitoModePrefs::GetAvailability(prefs
);
1026 return embedder_web_contents_
->GetController().CanGoBack();
1029 return embedder_web_contents_
->GetController().CanGoForward();
1032 CoreTabHelper
* core_tab_helper
=
1033 CoreTabHelper::FromWebContents(embedder_web_contents_
);
1034 if (!core_tab_helper
)
1037 CoreTabHelperDelegate
* core_delegate
= core_tab_helper
->delegate();
1038 return !core_delegate
||
1039 core_delegate
->CanReloadContents(embedder_web_contents_
);
1042 case IDC_VIEW_SOURCE
:
1043 case IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE
:
1044 return embedder_web_contents_
->GetController().CanViewSource();
1046 case IDC_CONTENT_CONTEXT_INSPECTELEMENT
:
1047 case IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
:
1048 case IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
:
1049 case IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP
:
1050 return IsDevCommandEnabled(id
);
1052 case IDC_CONTENT_CONTEXT_VIEWPAGEINFO
:
1053 if (embedder_web_contents_
->GetController().GetVisibleEntry() == NULL
)
1055 // Disabled if no browser is associated (e.g. desktop notifications).
1056 if (chrome::FindBrowserWithWebContents(embedder_web_contents_
) == NULL
)
1060 case IDC_CONTENT_CONTEXT_TRANSLATE
: {
1061 ChromeTranslateClient
* chrome_translate_client
=
1062 ChromeTranslateClient::FromWebContents(embedder_web_contents_
);
1063 if (!chrome_translate_client
)
1065 std::string original_lang
=
1066 chrome_translate_client
->GetLanguageState().original_language();
1067 std::string target_lang
= g_browser_process
->GetApplicationLocale();
1069 translate::TranslateDownloadManager::GetLanguageCode(target_lang
);
1070 // Note that we intentionally enable the menu even if the original and
1071 // target languages are identical. This is to give a way to user to
1072 // translate a page that might contains text fragments in a different
1074 return ((params_
.edit_flags
& WebContextMenuData::CanTranslate
) != 0) &&
1075 !original_lang
.empty() && // Did we receive the page language yet?
1076 !chrome_translate_client
->GetLanguageState().IsPageTranslated() &&
1077 !embedder_web_contents_
->GetInterstitialPage() &&
1078 // There are some application locales which can't be used as a
1079 // target language for translation.
1080 translate::TranslateDownloadManager::IsSupportedLanguage(
1082 // Disable on the Instant Extended NTP.
1083 !chrome::IsInstantNTP(embedder_web_contents_
);
1086 case IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
:
1087 case IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
:
1088 return params_
.link_url
.is_valid();
1090 case IDC_CONTENT_CONTEXT_COPYLINKLOCATION
:
1091 return params_
.unfiltered_link_url
.is_valid();
1093 case IDC_CONTENT_CONTEXT_SAVELINKAS
: {
1094 PrefService
* local_state
= g_browser_process
->local_state();
1095 DCHECK(local_state
);
1096 // Test if file-selection dialogs are forbidden by policy.
1097 if (!local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
))
1100 return params_
.link_url
.is_valid() &&
1101 ProfileIOData::IsHandledProtocol(params_
.link_url
.scheme());
1104 case IDC_CONTENT_CONTEXT_SAVEIMAGEAS
: {
1105 PrefService
* local_state
= g_browser_process
->local_state();
1106 DCHECK(local_state
);
1107 // Test if file-selection dialogs are forbidden by policy.
1108 if (!local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
))
1111 return params_
.has_image_contents
;
1114 // The images shown in the most visited thumbnails can't be opened or
1115 // searched for conventionally.
1116 case IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
:
1117 case IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB
:
1118 case IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
:
1119 return params_
.src_url
.is_valid() &&
1120 (params_
.src_url
.scheme() != content::kChromeUIScheme
);
1122 case IDC_CONTENT_CONTEXT_COPYIMAGE
:
1123 return params_
.has_image_contents
;
1125 // Media control commands should all be disabled if the player is in an
1127 case IDC_CONTENT_CONTEXT_PLAYPAUSE
:
1128 case IDC_CONTENT_CONTEXT_LOOP
:
1129 return (params_
.media_flags
&
1130 WebContextMenuData::MediaInError
) == 0;
1132 // Mute and unmute should also be disabled if the player has no audio.
1133 case IDC_CONTENT_CONTEXT_MUTE
:
1134 return (params_
.media_flags
&
1135 WebContextMenuData::MediaHasAudio
) != 0 &&
1136 (params_
.media_flags
&
1137 WebContextMenuData::MediaInError
) == 0;
1139 case IDC_CONTENT_CONTEXT_CONTROLS
:
1140 return (params_
.media_flags
&
1141 WebContextMenuData::MediaCanToggleControls
) != 0;
1143 case IDC_CONTENT_CONTEXT_ROTATECW
:
1144 case IDC_CONTENT_CONTEXT_ROTATECCW
:
1146 (params_
.media_flags
& WebContextMenuData::MediaCanRotate
) != 0;
1148 case IDC_CONTENT_CONTEXT_COPYAVLOCATION
:
1149 case IDC_CONTENT_CONTEXT_COPYIMAGELOCATION
:
1150 return params_
.src_url
.is_valid();
1152 case IDC_CONTENT_CONTEXT_SAVEAVAS
: {
1153 PrefService
* local_state
= g_browser_process
->local_state();
1154 DCHECK(local_state
);
1155 // Test if file-selection dialogs are forbidden by policy.
1156 if (!local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
))
1159 const GURL
& url
= params_
.src_url
;
1161 (params_
.media_flags
& WebContextMenuData::MediaCanSave
) &&
1162 url
.is_valid() && ProfileIOData::IsHandledProtocol(url
.scheme());
1163 #if defined(ENABLE_PRINT_PREVIEW)
1164 // Do not save the preview PDF on the print preview page.
1165 can_save
= can_save
&&
1166 !(printing::PrintPreviewDialogController::IsPrintPreviewURL(url
));
1171 case IDC_CONTENT_CONTEXT_OPENAVNEWTAB
:
1172 // Currently, a media element can be opened in a new tab iff it can
1173 // be saved. So rather than duplicating the MediaCanSave flag, we rely
1175 return !!(params_
.media_flags
& WebContextMenuData::MediaCanSave
);
1177 case IDC_SAVE_PAGE
: {
1178 CoreTabHelper
* core_tab_helper
=
1179 CoreTabHelper::FromWebContents(embedder_web_contents_
);
1180 if (!core_tab_helper
)
1183 CoreTabHelperDelegate
* core_delegate
= core_tab_helper
->delegate();
1184 if (core_delegate
&&
1185 !core_delegate
->CanSaveContents(embedder_web_contents_
))
1188 PrefService
* local_state
= g_browser_process
->local_state();
1189 DCHECK(local_state
);
1190 // Test if file-selection dialogs are forbidden by policy.
1191 if (!local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
))
1194 // We save the last committed entry (which the user is looking at), as
1195 // opposed to any pending URL that hasn't committed yet.
1196 NavigationEntry
* entry
=
1197 embedder_web_contents_
->GetController().GetLastCommittedEntry();
1198 return content::IsSavableURL(entry
? entry
->GetURL() : GURL());
1201 case IDC_CONTENT_CONTEXT_RELOADFRAME
:
1202 return params_
.frame_url
.is_valid();
1204 case IDC_CONTENT_CONTEXT_UNDO
:
1205 return !!(params_
.edit_flags
& WebContextMenuData::CanUndo
);
1207 case IDC_CONTENT_CONTEXT_REDO
:
1208 return !!(params_
.edit_flags
& WebContextMenuData::CanRedo
);
1210 case IDC_CONTENT_CONTEXT_CUT
:
1211 return !!(params_
.edit_flags
& WebContextMenuData::CanCut
);
1213 case IDC_CONTENT_CONTEXT_COPY
:
1214 return !!(params_
.edit_flags
& WebContextMenuData::CanCopy
);
1216 case IDC_CONTENT_CONTEXT_PASTE
: {
1217 if (!(params_
.edit_flags
& WebContextMenuData::CanPaste
))
1220 std::vector
<base::string16
> types
;
1222 ui::Clipboard::GetForCurrentThread()->ReadAvailableTypes(
1223 ui::CLIPBOARD_TYPE_COPY_PASTE
, &types
, &ignore
);
1224 return !types
.empty();
1227 case IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
: {
1228 if (!(params_
.edit_flags
& WebContextMenuData::CanPaste
))
1231 return ui::Clipboard::GetForCurrentThread()->IsFormatAvailable(
1232 ui::Clipboard::GetPlainTextFormatType(),
1233 ui::CLIPBOARD_TYPE_COPY_PASTE
);
1236 case IDC_CONTENT_CONTEXT_DELETE
:
1237 return !!(params_
.edit_flags
& WebContextMenuData::CanDelete
);
1239 case IDC_CONTENT_CONTEXT_SELECTALL
:
1240 return !!(params_
.edit_flags
& WebContextMenuData::CanSelectAll
);
1242 case IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
:
1243 return !browser_context_
->IsOffTheRecord() &&
1244 params_
.link_url
.is_valid() &&
1245 incognito_avail
!= IncognitoModePrefs::DISABLED
;
1248 return prefs
->GetBoolean(prefs::kPrintingEnabled
) &&
1249 (params_
.media_type
== WebContextMenuData::MediaTypeNone
||
1250 params_
.media_flags
& WebContextMenuData::MediaCanPrint
);
1252 case IDC_CONTENT_CONTEXT_SEARCHWEBFOR
:
1253 case IDC_CONTENT_CONTEXT_GOTOURL
:
1254 case IDC_SPELLPANEL_TOGGLE
:
1255 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
:
1257 case IDC_CONTENT_CONTEXT_VIEWFRAMEINFO
:
1258 // Disabled if no browser is associated (e.g. desktop notifications).
1259 if (chrome::FindBrowserWithWebContents(source_web_contents_
) == NULL
)
1263 case IDC_CHECK_SPELLING_WHILE_TYPING
:
1264 return prefs
->GetBoolean(prefs::kEnableContinuousSpellcheck
);
1266 #if !defined(OS_MACOSX) && defined(OS_POSIX)
1267 // TODO(suzhe): this should not be enabled for password fields.
1268 case IDC_INPUT_METHODS_MENU
:
1272 case IDC_CONTENT_CONTEXT_ADDSEARCHENGINE
:
1273 return !params_
.keyword_url
.is_empty();
1275 case IDC_SPELLCHECK_MENU
:
1278 case IDC_CONTENT_CONTEXT_OPENLINKWITH
:
1281 case IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_SETTINGS
:
1290 bool RenderViewContextMenu::IsCommandIdChecked(int id
) const {
1291 if (RenderViewContextMenuBase::IsCommandIdChecked(id
))
1294 // See if the video is set to looping.
1295 if (id
== IDC_CONTENT_CONTEXT_LOOP
)
1296 return (params_
.media_flags
& WebContextMenuData::MediaLoop
) != 0;
1298 if (id
== IDC_CONTENT_CONTEXT_CONTROLS
)
1299 return (params_
.media_flags
& WebContextMenuData::MediaControls
) != 0;
1302 if (ContextMenuMatcher::IsExtensionsCustomCommandId(id
))
1303 return extension_items_
.IsCommandIdChecked(id
);
1308 void RenderViewContextMenu::ExecuteCommand(int id
, int event_flags
) {
1309 RenderViewContextMenuBase::ExecuteCommand(id
, event_flags
);
1310 if (command_executed_
)
1312 command_executed_
= true;
1314 RenderFrameHost
* render_frame_host
= GetRenderFrameHost();
1316 // Process extension menu items.
1317 if (ContextMenuMatcher::IsExtensionsCustomCommandId(id
)) {
1318 extension_items_
.ExecuteCommand(id
, source_web_contents_
, params_
);
1322 if (id
>= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
&&
1323 id
<= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST
) {
1324 ProtocolHandlerRegistry::ProtocolHandlerList handlers
=
1325 GetHandlersForLinkUrl();
1326 if (handlers
.empty())
1329 content::RecordAction(
1330 UserMetricsAction("RegisterProtocolHandler.ContextMenu_Open"));
1331 int handlerIndex
= id
- IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
;
1332 WindowOpenDisposition disposition
=
1333 ForceNewTabDispositionFromEventFlags(event_flags
);
1334 OpenURL(handlers
[handlerIndex
].TranslateUrl(params_
.link_url
),
1335 GetDocumentURL(params_
),
1337 ui::PAGE_TRANSITION_LINK
);
1342 case IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
: {
1344 chrome::FindBrowserWithWebContents(source_web_contents_
);
1345 OpenURL(params_
.link_url
,
1346 GetDocumentURL(params_
),
1347 !browser
|| browser
->is_app() ?
1348 NEW_FOREGROUND_TAB
: NEW_BACKGROUND_TAB
,
1349 ui::PAGE_TRANSITION_LINK
);
1352 case IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
:
1353 OpenURL(params_
.link_url
,
1354 GetDocumentURL(params_
),
1356 ui::PAGE_TRANSITION_LINK
);
1359 case IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
:
1360 OpenURL(params_
.link_url
, GURL(), OFF_THE_RECORD
,
1361 ui::PAGE_TRANSITION_LINK
);
1364 case IDC_CONTENT_CONTEXT_SAVELINKAS
: {
1365 RecordDownloadSource(DOWNLOAD_INITIATED_BY_CONTEXT_MENU
);
1366 const GURL
& url
= params_
.link_url
;
1367 content::Referrer referrer
= CreateSaveAsReferrer(url
, params_
);
1368 DownloadManager
* dlm
=
1369 BrowserContext::GetDownloadManager(browser_context_
);
1370 scoped_ptr
<DownloadUrlParameters
> dl_params(
1371 DownloadUrlParameters::FromWebContents(source_web_contents_
, url
));
1372 dl_params
->set_referrer(referrer
);
1373 dl_params
->set_referrer_encoding(params_
.frame_charset
);
1374 dl_params
->set_suggested_name(params_
.suggested_filename
);
1375 dl_params
->set_prompt(true);
1376 dlm
->DownloadUrl(dl_params
.Pass());
1380 case IDC_CONTENT_CONTEXT_SAVEAVAS
:
1381 case IDC_CONTENT_CONTEXT_SAVEIMAGEAS
: {
1382 bool is_large_data_url
= params_
.has_image_contents
&&
1383 params_
.src_url
.is_empty();
1384 if (params_
.media_type
== WebContextMenuData::MediaTypeCanvas
||
1385 (params_
.media_type
== WebContextMenuData::MediaTypeImage
&&
1386 is_large_data_url
)) {
1387 source_web_contents_
->GetRenderViewHost()->SaveImageAt(
1388 params_
.x
, params_
.y
);
1390 RecordDownloadSource(DOWNLOAD_INITIATED_BY_CONTEXT_MENU
);
1391 const GURL
& url
= params_
.src_url
;
1392 content::Referrer referrer
= CreateSaveAsReferrer(url
, params_
);
1393 source_web_contents_
->SaveFrame(url
, referrer
);
1398 case IDC_CONTENT_CONTEXT_COPYLINKLOCATION
:
1399 WriteURLToClipboard(params_
.unfiltered_link_url
);
1402 case IDC_CONTENT_CONTEXT_COPYIMAGELOCATION
:
1403 case IDC_CONTENT_CONTEXT_COPYAVLOCATION
:
1404 WriteURLToClipboard(params_
.src_url
);
1407 case IDC_CONTENT_CONTEXT_COPYIMAGE
:
1408 CopyImageAt(params_
.x
, params_
.y
);
1411 case IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
:
1412 GetImageThumbnailForSearch();
1415 case IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
:
1416 OpenURLWithExtraHeaders(
1417 params_
.src_url
, GetDocumentURL(params_
), NEW_BACKGROUND_TAB
,
1418 ui::PAGE_TRANSITION_LINK
,
1419 data_reduction_proxy::kDataReductionPassThroughHeader
);
1422 case IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB
:
1423 case IDC_CONTENT_CONTEXT_OPENAVNEWTAB
:
1424 OpenURL(params_
.src_url
,
1425 GetDocumentURL(params_
),
1427 ui::PAGE_TRANSITION_LINK
);
1430 case IDC_CONTENT_CONTEXT_PLAYPAUSE
: {
1431 bool play
= !!(params_
.media_flags
& WebContextMenuData::MediaPaused
);
1433 content::RecordAction(UserMetricsAction("MediaContextMenu_Play"));
1435 content::RecordAction(UserMetricsAction("MediaContextMenu_Pause"));
1437 MediaPlayerActionAt(gfx::Point(params_
.x
, params_
.y
),
1438 WebMediaPlayerAction(
1439 WebMediaPlayerAction::Play
, play
));
1443 case IDC_CONTENT_CONTEXT_MUTE
: {
1444 bool mute
= !(params_
.media_flags
& WebContextMenuData::MediaMuted
);
1446 content::RecordAction(UserMetricsAction("MediaContextMenu_Mute"));
1448 content::RecordAction(UserMetricsAction("MediaContextMenu_Unmute"));
1450 MediaPlayerActionAt(gfx::Point(params_
.x
, params_
.y
),
1451 WebMediaPlayerAction(
1452 WebMediaPlayerAction::Mute
, mute
));
1456 case IDC_CONTENT_CONTEXT_LOOP
:
1457 content::RecordAction(UserMetricsAction("MediaContextMenu_Loop"));
1458 MediaPlayerActionAt(gfx::Point(params_
.x
, params_
.y
),
1459 WebMediaPlayerAction(
1460 WebMediaPlayerAction::Loop
,
1461 !IsCommandIdChecked(IDC_CONTENT_CONTEXT_LOOP
)));
1464 case IDC_CONTENT_CONTEXT_CONTROLS
:
1465 content::RecordAction(UserMetricsAction("MediaContextMenu_Controls"));
1466 MediaPlayerActionAt(
1467 gfx::Point(params_
.x
, params_
.y
),
1468 WebMediaPlayerAction(
1469 WebMediaPlayerAction::Controls
,
1470 !IsCommandIdChecked(IDC_CONTENT_CONTEXT_CONTROLS
)));
1473 case IDC_CONTENT_CONTEXT_ROTATECW
:
1474 content::RecordAction(
1475 UserMetricsAction("PluginContextMenu_RotateClockwise"));
1477 gfx::Point(params_
.x
, params_
.y
),
1478 WebPluginAction(WebPluginAction::Rotate90Clockwise
, true));
1481 case IDC_CONTENT_CONTEXT_ROTATECCW
:
1482 content::RecordAction(
1483 UserMetricsAction("PluginContextMenu_RotateCounterclockwise"));
1485 gfx::Point(params_
.x
, params_
.y
),
1486 WebPluginAction(WebPluginAction::Rotate90Counterclockwise
, true));
1490 embedder_web_contents_
->GetController().GoBack();
1494 embedder_web_contents_
->GetController().GoForward();
1498 embedder_web_contents_
->OnSavePage();
1502 embedder_web_contents_
->GetController().Reload(true);
1505 case IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
: {
1506 const Extension
* platform_app
= GetExtension();
1507 DCHECK(platform_app
);
1508 DCHECK(platform_app
->is_platform_app());
1510 extensions::ExtensionSystem::Get(browser_context_
)
1511 ->extension_service()
1512 ->ReloadExtension(platform_app
->id());
1516 case IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP
: {
1517 const Extension
* platform_app
= GetExtension();
1518 DCHECK(platform_app
);
1519 DCHECK(platform_app
->is_platform_app());
1521 apps::AppLoadService::Get(GetProfile())
1522 ->RestartApplication(platform_app
->id());
1527 #if defined(ENABLE_PRINTING)
1528 if (params_
.media_type
!= WebContextMenuData::MediaTypeNone
) {
1529 if (render_frame_host
) {
1530 render_frame_host
->Send(new PrintMsg_PrintNodeUnderContextMenu(
1531 render_frame_host
->GetRoutingID()));
1536 printing::StartPrint(
1537 source_web_contents_
,
1538 GetPrefs(browser_context_
)->GetBoolean(prefs::kPrintPreviewDisabled
),
1539 !params_
.selection_text
.empty());
1540 #endif // ENABLE_PRINTING
1544 case IDC_VIEW_SOURCE
:
1545 embedder_web_contents_
->ViewSource();
1548 case IDC_CONTENT_CONTEXT_INSPECTELEMENT
:
1549 Inspect(params_
.x
, params_
.y
);
1552 case IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
: {
1553 const Extension
* platform_app
= GetExtension();
1554 DCHECK(platform_app
);
1555 DCHECK(platform_app
->is_platform_app());
1557 extensions::devtools_util::InspectBackgroundPage(platform_app
,
1562 case IDC_CONTENT_CONTEXT_VIEWPAGEINFO
: {
1563 NavigationController
* controller
=
1564 &embedder_web_contents_
->GetController();
1565 // Important to use GetVisibleEntry to match what's showing in the
1566 // omnibox. This may return null.
1567 NavigationEntry
* nav_entry
= controller
->GetVisibleEntry();
1571 chrome::FindBrowserWithWebContents(embedder_web_contents_
);
1572 chrome::ShowWebsiteSettings(browser
, embedder_web_contents_
,
1573 nav_entry
->GetURL(), nav_entry
->GetSSL());
1577 case IDC_CONTENT_CONTEXT_TRANSLATE
: {
1578 // A translation might have been triggered by the time the menu got
1579 // selected, do nothing in that case.
1580 ChromeTranslateClient
* chrome_translate_client
=
1581 ChromeTranslateClient::FromWebContents(embedder_web_contents_
);
1582 if (!chrome_translate_client
||
1583 chrome_translate_client
->GetLanguageState().IsPageTranslated() ||
1584 chrome_translate_client
->GetLanguageState().translation_pending()) {
1587 std::string original_lang
=
1588 chrome_translate_client
->GetLanguageState().original_language();
1589 std::string target_lang
= g_browser_process
->GetApplicationLocale();
1591 translate::TranslateDownloadManager::GetLanguageCode(target_lang
);
1592 // Since the user decided to translate for that language and site, clears
1593 // any preferences for not translating them.
1594 scoped_ptr
<translate::TranslatePrefs
> prefs(
1595 ChromeTranslateClient::CreateTranslatePrefs(
1596 GetPrefs(browser_context_
)));
1597 prefs
->UnblockLanguage(original_lang
);
1598 prefs
->RemoveSiteFromBlacklist(params_
.page_url
.HostNoBrackets());
1599 translate::TranslateManager
* manager
=
1600 chrome_translate_client
->GetTranslateManager();
1602 manager
->TranslatePage(original_lang
, target_lang
, true);
1606 case IDC_CONTENT_CONTEXT_RELOADFRAME
:
1607 // We always obey the cache here.
1608 // TODO(evanm): Perhaps we could allow shift-clicking the menu item to do
1609 // a cache-ignoring reload of the frame.
1610 source_web_contents_
->ReloadFocusedFrame(false);
1613 case IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE
:
1614 source_web_contents_
->ViewFrameSource(params_
.frame_url
,
1615 params_
.frame_page_state
);
1618 case IDC_CONTENT_CONTEXT_VIEWFRAMEINFO
: {
1619 Browser
* browser
= chrome::FindBrowserWithWebContents(
1620 source_web_contents_
);
1621 chrome::ShowWebsiteSettings(browser
, source_web_contents_
,
1622 params_
.frame_url
, params_
.security_info
);
1626 case IDC_CONTENT_CONTEXT_UNDO
:
1627 source_web_contents_
->Undo();
1630 case IDC_CONTENT_CONTEXT_REDO
:
1631 source_web_contents_
->Redo();
1634 case IDC_CONTENT_CONTEXT_CUT
:
1635 source_web_contents_
->Cut();
1638 case IDC_CONTENT_CONTEXT_COPY
:
1639 source_web_contents_
->Copy();
1642 case IDC_CONTENT_CONTEXT_PASTE
:
1643 source_web_contents_
->Paste();
1646 case IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
:
1647 source_web_contents_
->PasteAndMatchStyle();
1650 case IDC_CONTENT_CONTEXT_DELETE
:
1651 source_web_contents_
->Delete();
1654 case IDC_CONTENT_CONTEXT_SELECTALL
:
1655 source_web_contents_
->SelectAll();
1658 case IDC_CONTENT_CONTEXT_SEARCHWEBFOR
:
1659 case IDC_CONTENT_CONTEXT_GOTOURL
: {
1660 WindowOpenDisposition disposition
=
1661 ForceNewTabDispositionFromEventFlags(event_flags
);
1662 OpenURL(selection_navigation_url_
, GURL(), disposition
,
1663 ui::PAGE_TRANSITION_LINK
);
1666 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
: {
1667 WindowOpenDisposition disposition
=
1668 ForceNewTabDispositionFromEventFlags(event_flags
);
1669 GURL url
= chrome::GetSettingsUrl(chrome::kLanguageOptionsSubPage
);
1670 OpenURL(url
, GURL(), disposition
, ui::PAGE_TRANSITION_LINK
);
1674 case IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_SETTINGS
: {
1675 content::RecordAction(
1676 UserMetricsAction("RegisterProtocolHandler.ContextMenu_Settings"));
1677 WindowOpenDisposition disposition
=
1678 ForceNewTabDispositionFromEventFlags(event_flags
);
1679 GURL url
= chrome::GetSettingsUrl(chrome::kHandlerSettingsSubPage
);
1680 OpenURL(url
, GURL(), disposition
, ui::PAGE_TRANSITION_LINK
);
1684 case IDC_CONTENT_CONTEXT_ADDSEARCHENGINE
: {
1685 // Make sure the model is loaded.
1686 TemplateURLService
* model
=
1687 TemplateURLServiceFactory::GetForProfile(GetProfile());
1692 SearchEngineTabHelper
* search_engine_tab_helper
=
1693 SearchEngineTabHelper::FromWebContents(source_web_contents_
);
1694 if (search_engine_tab_helper
&&
1695 search_engine_tab_helper
->delegate()) {
1696 base::string16
keyword(TemplateURL::GenerateKeyword(params_
.page_url
));
1697 TemplateURLData data
;
1698 data
.short_name
= keyword
;
1699 data
.SetKeyword(keyword
);
1700 data
.SetURL(params_
.keyword_url
.spec());
1702 TemplateURL::GenerateFaviconURL(params_
.page_url
.GetOrigin());
1703 // Takes ownership of the TemplateURL.
1704 search_engine_tab_helper
->delegate()->ConfirmAddSearchProvider(
1705 new TemplateURL(data
), GetProfile());
1716 ProtocolHandlerRegistry::ProtocolHandlerList
1717 RenderViewContextMenu::GetHandlersForLinkUrl() {
1718 ProtocolHandlerRegistry::ProtocolHandlerList handlers
=
1719 protocol_handler_registry_
->GetHandlersFor(params_
.link_url
.scheme());
1720 std::sort(handlers
.begin(), handlers
.end());
1724 void RenderViewContextMenu::NotifyMenuShown() {
1725 content::NotificationService::current()->Notify(
1726 chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN
,
1727 content::Source
<RenderViewContextMenu
>(this),
1728 content::NotificationService::NoDetails());
1731 void RenderViewContextMenu::NotifyURLOpened(
1733 content::WebContents
* new_contents
) {
1734 RetargetingDetails details
;
1735 details
.source_web_contents
= source_web_contents_
;
1736 // Don't use GetRenderFrameHost() as it may be NULL. crbug.com/399789
1737 details
.source_render_frame_id
= render_frame_id_
;
1738 details
.target_url
= url
;
1739 details
.target_web_contents
= new_contents
;
1740 details
.not_yet_in_tabstrip
= false;
1742 content::NotificationService::current()->Notify(
1743 chrome::NOTIFICATION_RETARGETING
,
1744 content::Source
<Profile
>(GetProfile()),
1745 content::Details
<RetargetingDetails
>(&details
));
1748 bool RenderViewContextMenu::IsDevCommandEnabled(int id
) const {
1749 if (id
== IDC_CONTENT_CONTEXT_INSPECTELEMENT
||
1750 id
== IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
) {
1751 if (!GetPrefs(browser_context_
)
1752 ->GetBoolean(prefs::kWebKitJavascriptEnabled
))
1755 // Don't enable the web inspector if the developer tools are disabled via
1756 // the preference dev-tools-disabled.
1757 if (GetPrefs(browser_context_
)->GetBoolean(prefs::kDevToolsDisabled
))
1764 base::string16
RenderViewContextMenu::PrintableSelectionText() {
1765 return gfx::TruncateString(params_
.selection_text
,
1766 kMaxSelectionTextLength
,
1770 // Controller functions --------------------------------------------------------
1772 void RenderViewContextMenu::CopyImageAt(int x
, int y
) {
1773 source_web_contents_
->GetRenderViewHost()->CopyImageAt(x
, y
);
1776 void RenderViewContextMenu::GetImageThumbnailForSearch() {
1777 RenderFrameHost
* render_frame_host
= GetRenderFrameHost();
1778 if (!render_frame_host
)
1780 render_frame_host
->Send(new ChromeViewMsg_RequestThumbnailForContextNode(
1781 render_frame_host
->GetRoutingID(),
1782 kImageSearchThumbnailMinSize
,
1783 gfx::Size(kImageSearchThumbnailMaxWidth
,
1784 kImageSearchThumbnailMaxHeight
)));
1787 void RenderViewContextMenu::Inspect(int x
, int y
) {
1788 content::RecordAction(UserMetricsAction("DevTools_InspectElement"));
1789 RenderFrameHost
* render_frame_host
= GetRenderFrameHost();
1790 if (!render_frame_host
)
1792 DevToolsWindow::InspectElement(
1793 WebContents::FromRenderFrameHost(render_frame_host
), x
, y
);
1796 void RenderViewContextMenu::WriteURLToClipboard(const GURL
& url
) {
1797 chrome_common_net::WriteURLToClipboard(
1798 url
, GetPrefs(browser_context_
)->GetString(prefs::kAcceptLanguages
));
1801 void RenderViewContextMenu::MediaPlayerActionAt(
1802 const gfx::Point
& location
,
1803 const WebMediaPlayerAction
& action
) {
1804 source_web_contents_
->GetRenderViewHost()->
1805 ExecuteMediaPlayerActionAtLocation(location
, action
);
1808 void RenderViewContextMenu::PluginActionAt(
1809 const gfx::Point
& location
,
1810 const WebPluginAction
& action
) {
1811 source_web_contents_
->GetRenderViewHost()->
1812 ExecutePluginActionAtLocation(location
, action
);