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_factory.h"
24 #include "chrome/browser/browser_process.h"
25 #include "chrome/browser/chrome_notification_types.h"
26 #include "chrome/browser/custom_handlers/protocol_handler_registry_factory.h"
27 #include "chrome/browser/devtools/devtools_window.h"
28 #include "chrome/browser/download/download_service.h"
29 #include "chrome/browser/download/download_service_factory.h"
30 #include "chrome/browser/download/download_stats.h"
31 #include "chrome/browser/extensions/devtools_util.h"
32 #include "chrome/browser/extensions/extension_service.h"
33 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
34 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
35 #include "chrome/browser/password_manager/chrome_password_manager_client.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/data_reduction_proxy/core/common/data_reduction_proxy_headers.h"
66 #include "components/google/core/browser/google_util.h"
67 #include "components/metrics/proto/omnibox_input_type.pb.h"
68 #include "components/omnibox/browser/autocomplete_classifier.h"
69 #include "components/omnibox/browser/autocomplete_match.h"
70 #include "components/password_manager/core/common/experiments.h"
71 #include "components/search_engines/template_url.h"
72 #include "components/search_engines/template_url_service.h"
73 #include "components/translate/core/browser/translate_download_manager.h"
74 #include "components/translate/core/browser/translate_manager.h"
75 #include "components/translate/core/browser/translate_prefs.h"
76 #include "components/user_prefs/user_prefs.h"
77 #include "content/public/browser/child_process_security_policy.h"
78 #include "content/public/browser/download_manager.h"
79 #include "content/public/browser/download_save_info.h"
80 #include "content/public/browser/download_url_parameters.h"
81 #include "content/public/browser/navigation_details.h"
82 #include "content/public/browser/navigation_entry.h"
83 #include "content/public/browser/notification_service.h"
84 #include "content/public/browser/render_frame_host.h"
85 #include "content/public/browser/render_process_host.h"
86 #include "content/public/browser/render_view_host.h"
87 #include "content/public/browser/render_widget_host_view.h"
88 #include "content/public/browser/user_metrics.h"
89 #include "content/public/browser/web_contents.h"
90 #include "content/public/common/menu_item.h"
91 #include "content/public/common/ssl_status.h"
92 #include "content/public/common/url_utils.h"
93 #include "extensions/browser/extension_host.h"
94 #include "extensions/browser/extension_system.h"
95 #include "extensions/browser/guest_view/web_view/web_view_guest.h"
96 #include "extensions/browser/view_type_utils.h"
97 #include "extensions/common/extension.h"
98 #include "net/base/escape.h"
99 #include "third_party/WebKit/public/web/WebContextMenuData.h"
100 #include "third_party/WebKit/public/web/WebMediaPlayerAction.h"
101 #include "third_party/WebKit/public/web/WebPluginAction.h"
102 #include "ui/base/clipboard/clipboard.h"
103 #include "ui/base/l10n/l10n_util.h"
104 #include "ui/gfx/favicon_size.h"
105 #include "ui/gfx/geometry/point.h"
106 #include "ui/gfx/geometry/size.h"
107 #include "ui/gfx/text_elider.h"
109 #if defined(ENABLE_EXTENSIONS)
110 #include "extensions/browser/guest_view/mime_handler_view/mime_handler_view_guest.h"
113 #if defined(ENABLE_PRINTING)
114 #include "chrome/browser/printing/print_view_manager_common.h"
115 #include "components/printing/common/print_messages.h"
117 #if defined(ENABLE_PRINT_PREVIEW)
118 #include "chrome/browser/printing/print_preview_context_menu_observer.h"
119 #include "chrome/browser/printing/print_preview_dialog_controller.h"
120 #endif // defined(ENABLE_PRINT_PREVIEW)
121 #endif // defined(ENABLE_PRINTING)
123 using base::UserMetricsAction
;
124 using blink::WebContextMenuData
;
125 using blink::WebMediaPlayerAction
;
126 using blink::WebPluginAction
;
127 using blink::WebString
;
129 using content::BrowserContext
;
130 using content::ChildProcessSecurityPolicy
;
131 using content::DownloadManager
;
132 using content::DownloadUrlParameters
;
133 using content::NavigationController
;
134 using content::NavigationEntry
;
135 using content::OpenURLParams
;
136 using content::RenderFrameHost
;
137 using content::RenderViewHost
;
138 using content::SSLStatus
;
139 using content::WebContents
;
140 using extensions::ContextMenuMatcher
;
141 using extensions::Extension
;
142 using extensions::MenuItem
;
143 using extensions::MenuManager
;
147 const int kImageSearchThumbnailMinSize
= 300 * 300;
148 const int kImageSearchThumbnailMaxWidth
= 600;
149 const int kImageSearchThumbnailMaxHeight
= 600;
151 // Maps UMA enumeration to IDC. IDC could be changed so we can't use
152 // just them and |UMA_HISTOGRAM_CUSTOM_ENUMERATION|.
153 // Never change mapping or reuse |enum_id|. Always push back new items.
154 // Items that is not used any more by |RenderViewContextMenu.ExecuteCommand|
155 // could be deleted, but don't change the rest of |kUmaEnumToControlId|.
156 const struct UmaEnumCommandIdPair
{
159 } kUmaEnumToControlId
[] = {
161 enum id for 0, 1 are detected using
162 RenderViewContextMenu::IsContentCustomCommandId and
163 ContextMenuMatcher::IsExtensionsCustomCommandId
165 {2, IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
},
166 {3, IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
},
167 {4, IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
},
168 {5, IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
},
169 {6, IDC_CONTENT_CONTEXT_SAVELINKAS
},
170 {7, IDC_CONTENT_CONTEXT_SAVEAVAS
},
171 {8, IDC_CONTENT_CONTEXT_SAVEIMAGEAS
},
172 {9, IDC_CONTENT_CONTEXT_COPYLINKLOCATION
},
173 {10, IDC_CONTENT_CONTEXT_COPYIMAGELOCATION
},
174 {11, IDC_CONTENT_CONTEXT_COPYAVLOCATION
},
175 {12, IDC_CONTENT_CONTEXT_COPYIMAGE
},
176 {13, IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB
},
177 {14, IDC_CONTENT_CONTEXT_OPENAVNEWTAB
},
178 {15, IDC_CONTENT_CONTEXT_PLAYPAUSE
},
179 {16, IDC_CONTENT_CONTEXT_MUTE
},
180 {17, IDC_CONTENT_CONTEXT_LOOP
},
181 {18, IDC_CONTENT_CONTEXT_CONTROLS
},
182 {19, IDC_CONTENT_CONTEXT_ROTATECW
},
183 {20, IDC_CONTENT_CONTEXT_ROTATECCW
},
188 {25, IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
},
189 {26, IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP
},
191 {28, IDC_VIEW_SOURCE
},
192 {29, IDC_CONTENT_CONTEXT_INSPECTELEMENT
},
193 {30, IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
},
194 {31, IDC_CONTENT_CONTEXT_VIEWPAGEINFO
},
195 {32, IDC_CONTENT_CONTEXT_TRANSLATE
},
196 {33, IDC_CONTENT_CONTEXT_RELOADFRAME
},
197 {34, IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE
},
198 {35, IDC_CONTENT_CONTEXT_VIEWFRAMEINFO
},
199 {36, IDC_CONTENT_CONTEXT_UNDO
},
200 {37, IDC_CONTENT_CONTEXT_REDO
},
201 {38, IDC_CONTENT_CONTEXT_CUT
},
202 {39, IDC_CONTENT_CONTEXT_COPY
},
203 {40, IDC_CONTENT_CONTEXT_PASTE
},
204 {41, IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
},
205 {42, IDC_CONTENT_CONTEXT_DELETE
},
206 {43, IDC_CONTENT_CONTEXT_SELECTALL
},
207 {44, IDC_CONTENT_CONTEXT_SEARCHWEBFOR
},
208 {45, IDC_CONTENT_CONTEXT_GOTOURL
},
209 {46, IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
},
210 {47, IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_SETTINGS
},
211 {48, IDC_CONTENT_CONTEXT_ADDSEARCHENGINE
},
212 {52, IDC_CONTENT_CONTEXT_OPENLINKWITH
},
213 {53, IDC_CHECK_SPELLING_WHILE_TYPING
},
214 {54, IDC_SPELLCHECK_MENU
},
215 {55, IDC_CONTENT_CONTEXT_SPELLING_TOGGLE
},
216 {56, IDC_SPELLCHECK_LANGUAGES_FIRST
},
217 {57, IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
},
218 {58, IDC_SPELLCHECK_SUGGESTION_0
},
219 {59, IDC_SPELLCHECK_ADD_TO_DICTIONARY
},
220 {60, IDC_SPELLPANEL_TOGGLE
},
221 {61, IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
},
222 {62, IDC_WRITING_DIRECTION_MENU
},
223 {63, IDC_WRITING_DIRECTION_DEFAULT
},
224 {64, IDC_WRITING_DIRECTION_LTR
},
225 {65, IDC_WRITING_DIRECTION_RTL
},
226 {66, IDC_CONTENT_CONTEXT_LOAD_ORIGINAL_IMAGE
},
227 {67, IDC_CONTENT_CONTEXT_FORCESAVEPASSWORD
},
228 // Add new items here and use |enum_id| from the next line.
229 // Also, add new items to RenderViewContextMenuItem enum in histograms.xml.
230 {68, 0}, // Must be the last. Increment |enum_id| when new IDC was added.
233 // Collapses large ranges of ids before looking for UMA enum.
234 int CollapseCommandsForUMA(int id
) {
235 DCHECK(!RenderViewContextMenu::IsContentCustomCommandId(id
));
236 DCHECK(!ContextMenuMatcher::IsExtensionsCustomCommandId(id
));
238 if (id
>= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
&&
239 id
<= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST
) {
240 return IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
;
243 if (id
>= IDC_SPELLCHECK_LANGUAGES_FIRST
&&
244 id
<= IDC_SPELLCHECK_LANGUAGES_LAST
) {
245 return IDC_SPELLCHECK_LANGUAGES_FIRST
;
248 if (id
>= IDC_SPELLCHECK_SUGGESTION_0
&&
249 id
<= IDC_SPELLCHECK_SUGGESTION_LAST
) {
250 return IDC_SPELLCHECK_SUGGESTION_0
;
256 // Returns UMA enum value for command specified by |id| or -1 if not found.
257 int FindUMAEnumValueForCommand(int id
) {
258 if (RenderViewContextMenu::IsContentCustomCommandId(id
))
261 if (ContextMenuMatcher::IsExtensionsCustomCommandId(id
))
264 id
= CollapseCommandsForUMA(id
);
265 const size_t kMappingSize
= arraysize(kUmaEnumToControlId
);
266 for (size_t i
= 0; i
< kMappingSize
; ++i
) {
267 if (kUmaEnumToControlId
[i
].control_id
== id
) {
268 return kUmaEnumToControlId
[i
].enum_id
;
274 // Usually a new tab is expected where this function is used,
275 // however users should be able to open a tab in background
276 // or in a new window.
277 WindowOpenDisposition
ForceNewTabDispositionFromEventFlags(
279 WindowOpenDisposition disposition
=
280 ui::DispositionFromEventFlags(event_flags
);
281 return disposition
== CURRENT_TAB
? NEW_FOREGROUND_TAB
: disposition
;
284 // Returns the preference of the profile represented by the |context|.
285 PrefService
* GetPrefs(content::BrowserContext
* context
) {
286 return user_prefs::UserPrefs::Get(context
);
289 bool ExtensionPatternMatch(const extensions::URLPatternSet
& patterns
,
291 // No patterns means no restriction, so that implicitly matches.
292 if (patterns
.is_empty())
294 return patterns
.MatchesURL(url
);
297 const GURL
& GetDocumentURL(const content::ContextMenuParams
& params
) {
298 return params
.frame_url
.is_empty() ? params
.page_url
: params
.frame_url
;
301 content::Referrer
CreateSaveAsReferrer(
303 const content::ContextMenuParams
& params
) {
304 const GURL
& referring_url
= GetDocumentURL(params
);
305 return content::Referrer::SanitizeForRequest(
307 content::Referrer(referring_url
.GetAsReferrer(), params
.referrer_policy
));
310 content::WebContents
* GetWebContentsToUse(content::WebContents
* web_contents
) {
311 #if defined(ENABLE_EXTENSIONS)
312 // If we're viewing in a MimeHandlerViewGuest, use its embedder WebContents.
313 if (extensions::MimeHandlerViewGuest::FromWebContents(web_contents
)) {
314 WebContents
* top_level_web_contents
=
315 guest_view::GuestViewBase::GetTopLevelWebContents(web_contents
);
316 if (top_level_web_contents
)
317 return top_level_web_contents
;
323 bool g_custom_id_ranges_initialized
= false;
325 const int kSpellcheckRadioGroup
= 1;
330 gfx::Vector2d
RenderViewContextMenu::GetOffset(
331 RenderFrameHost
* render_frame_host
) {
332 gfx::Vector2d offset
;
333 #if defined(ENABLE_EXTENSIONS)
334 WebContents
* web_contents
=
335 WebContents::FromRenderFrameHost(render_frame_host
);
336 WebContents
* top_level_web_contents
=
337 guest_view::GuestViewBase::GetTopLevelWebContents(web_contents
);
338 if (web_contents
&& top_level_web_contents
&&
339 web_contents
!= top_level_web_contents
) {
340 gfx::Rect bounds
= web_contents
->GetContainerBounds();
341 gfx::Rect top_level_bounds
= top_level_web_contents
->GetContainerBounds();
342 offset
= bounds
.origin() - top_level_bounds
.origin();
344 #endif // defined(ENABLE_EXTENSIONS)
349 bool RenderViewContextMenu::IsDevToolsURL(const GURL
& url
) {
350 return url
.SchemeIs(content::kChromeDevToolsScheme
);
354 bool RenderViewContextMenu::IsInternalResourcesURL(const GURL
& url
) {
355 if (!url
.SchemeIs(content::kChromeUIScheme
))
357 return url
.host() == chrome::kChromeUISyncResourcesHost
;
360 RenderViewContextMenu::RenderViewContextMenu(
361 content::RenderFrameHost
* render_frame_host
,
362 const content::ContextMenuParams
& params
)
363 : RenderViewContextMenuBase(render_frame_host
, params
),
364 extension_items_(browser_context_
,
367 base::Bind(MenuItemMatchesParams
, params_
)),
368 protocol_handler_submenu_model_(this),
369 protocol_handler_registry_(
370 ProtocolHandlerRegistryFactory::GetForBrowserContext(GetProfile())),
371 embedder_web_contents_(GetWebContentsToUse(source_web_contents_
)) {
372 if (!g_custom_id_ranges_initialized
) {
373 g_custom_id_ranges_initialized
= true;
374 SetContentCustomCommandIdRange(IDC_CONTENT_CONTEXT_CUSTOM_FIRST
,
375 IDC_CONTENT_CONTEXT_CUSTOM_LAST
);
377 set_content_type(ContextMenuContentTypeFactory::Create(
378 source_web_contents_
, params
));
381 RenderViewContextMenu::~RenderViewContextMenu() {
384 // Menu construction functions -------------------------------------------------
386 #if defined(ENABLE_EXTENSIONS)
388 bool RenderViewContextMenu::ExtensionContextAndPatternMatch(
389 const content::ContextMenuParams
& params
,
390 const MenuItem::ContextList
& contexts
,
391 const extensions::URLPatternSet
& target_url_patterns
) {
392 const bool has_link
= !params
.link_url
.is_empty();
393 const bool has_selection
= !params
.selection_text
.empty();
394 const bool in_frame
= !params
.frame_url
.is_empty();
396 if (contexts
.Contains(MenuItem::ALL
) ||
397 (has_selection
&& contexts
.Contains(MenuItem::SELECTION
)) ||
398 (params
.is_editable
&& contexts
.Contains(MenuItem::EDITABLE
)) ||
399 (in_frame
&& contexts
.Contains(MenuItem::FRAME
)))
402 if (has_link
&& contexts
.Contains(MenuItem::LINK
) &&
403 ExtensionPatternMatch(target_url_patterns
, params
.link_url
))
406 switch (params
.media_type
) {
407 case WebContextMenuData::MediaTypeImage
:
408 if (contexts
.Contains(MenuItem::IMAGE
) &&
409 ExtensionPatternMatch(target_url_patterns
, params
.src_url
))
413 case WebContextMenuData::MediaTypeVideo
:
414 if (contexts
.Contains(MenuItem::VIDEO
) &&
415 ExtensionPatternMatch(target_url_patterns
, params
.src_url
))
419 case WebContextMenuData::MediaTypeAudio
:
420 if (contexts
.Contains(MenuItem::AUDIO
) &&
421 ExtensionPatternMatch(target_url_patterns
, params
.src_url
))
429 // PAGE is the least specific context, so we only examine that if none of the
430 // other contexts apply (except for FRAME, which is included in PAGE for
431 // backwards compatibility).
432 if (!has_link
&& !has_selection
&& !params
.is_editable
&&
433 params
.media_type
== WebContextMenuData::MediaTypeNone
&&
434 contexts
.Contains(MenuItem::PAGE
))
441 bool RenderViewContextMenu::MenuItemMatchesParams(
442 const content::ContextMenuParams
& params
,
443 const extensions::MenuItem
* item
) {
444 bool match
= ExtensionContextAndPatternMatch(params
, item
->contexts(),
445 item
->target_url_patterns());
449 const GURL
& document_url
= GetDocumentURL(params
);
450 return ExtensionPatternMatch(item
->document_url_patterns(), document_url
);
453 void RenderViewContextMenu::AppendAllExtensionItems() {
454 extension_items_
.Clear();
455 ExtensionService
* service
=
456 extensions::ExtensionSystem::Get(browser_context_
)->extension_service();
458 return; // In unit-tests, we may not have an ExtensionService.
460 MenuManager
* menu_manager
= MenuManager::Get(browser_context_
);
464 base::string16 printable_selection_text
= PrintableSelectionText();
465 EscapeAmpersands(&printable_selection_text
);
467 // Get a list of extension id's that have context menu items, and sort by the
468 // top level context menu title of the extension.
469 std::set
<MenuItem::ExtensionKey
> ids
= menu_manager
->ExtensionIds();
470 std::vector
<base::string16
> sorted_menu_titles
;
471 std::map
<base::string16
, std::vector
<const Extension
*>>
472 title_to_extensions_map
;
473 for (std::set
<MenuItem::ExtensionKey
>::iterator iter
= ids
.begin();
476 const Extension
* extension
=
477 service
->GetExtensionById(iter
->extension_id
, false);
478 // Platform apps have their context menus created directly in
479 // AppendPlatformAppItems.
480 if (extension
&& !extension
->is_platform_app()) {
481 base::string16 menu_title
= extension_items_
.GetTopLevelContextMenuTitle(
482 *iter
, printable_selection_text
);
483 title_to_extensions_map
[menu_title
].push_back(extension
);
484 sorted_menu_titles
.push_back(menu_title
);
487 if (sorted_menu_titles
.empty())
490 const std::string app_locale
= g_browser_process
->GetApplicationLocale();
491 l10n_util::SortStrings16(app_locale
, &sorted_menu_titles
);
492 sorted_menu_titles
.erase(
493 std::unique(sorted_menu_titles
.begin(), sorted_menu_titles
.end()),
494 sorted_menu_titles
.end());
497 for (size_t i
= 0; i
< sorted_menu_titles
.size(); ++i
) {
498 std::vector
<const Extension
*>& extensions
=
499 title_to_extensions_map
[sorted_menu_titles
[i
]];
500 for (const auto& extension
: extensions
) {
501 MenuItem::ExtensionKey
extension_key(extension
->id());
502 extension_items_
.AppendExtensionItems(extension_key
,
503 printable_selection_text
, &index
,
504 false); // is_action_menu
509 void RenderViewContextMenu::AppendCurrentExtensionItems() {
510 // Avoid appending extension related items when |extension| is null.
511 // For Panel, this happens when the panel is navigated to a url outside of the
512 // extension's package.
513 const Extension
* extension
= GetExtension();
517 extensions::WebViewGuest
* web_view_guest
=
518 extensions::WebViewGuest::FromWebContents(source_web_contents_
);
519 MenuItem::ExtensionKey key
;
520 if (web_view_guest
) {
521 key
= MenuItem::ExtensionKey(
523 web_view_guest
->owner_web_contents()->GetRenderProcessHost()->GetID(),
524 web_view_guest
->view_instance_id());
526 key
= MenuItem::ExtensionKey(extension
->id());
529 // Only add extension items from this extension.
531 extension_items_
.AppendExtensionItems(key
, PrintableSelectionText(), &index
,
532 false /* is_action_menu */);
534 #endif // defined(ENABLE_EXTENSIONS)
536 void RenderViewContextMenu::InitMenu() {
537 RenderViewContextMenuBase::InitMenu();
539 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PAGE
))
542 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_FRAME
)) {
543 // Merge in frame items with page items if we clicked within a frame that
545 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
549 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_LINK
)) {
551 if (params_
.media_type
!= WebContextMenuData::MediaTypeNone
)
552 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
555 if (content_type_
->SupportsGroup(
556 ContextMenuContentType::ITEM_GROUP_MEDIA_IMAGE
)) {
560 if (content_type_
->SupportsGroup(
561 ContextMenuContentType::ITEM_GROUP_SEARCHWEBFORIMAGE
)) {
562 AppendSearchWebForImageItems();
565 if (content_type_
->SupportsGroup(
566 ContextMenuContentType::ITEM_GROUP_MEDIA_VIDEO
)) {
570 if (content_type_
->SupportsGroup(
571 ContextMenuContentType::ITEM_GROUP_MEDIA_AUDIO
)) {
575 if (content_type_
->SupportsGroup(
576 ContextMenuContentType::ITEM_GROUP_MEDIA_CANVAS
)) {
580 if (content_type_
->SupportsGroup(
581 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN
)) {
585 // ITEM_GROUP_MEDIA_FILE has no specific items.
587 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_EDITABLE
))
588 AppendEditableItems();
590 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_COPY
)) {
591 DCHECK(!content_type_
->SupportsGroup(
592 ContextMenuContentType::ITEM_GROUP_EDITABLE
));
596 if (content_type_
->SupportsGroup(
597 ContextMenuContentType::ITEM_GROUP_SEARCH_PROVIDER
)) {
598 AppendSearchProvider();
601 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PRINT
))
604 if (content_type_
->SupportsGroup(
605 ContextMenuContentType::ITEM_GROUP_MEDIA_PLUGIN
)) {
606 AppendRotationItems();
609 if (content_type_
->SupportsGroup(
610 ContextMenuContentType::ITEM_GROUP_ALL_EXTENSION
)) {
611 DCHECK(!content_type_
->SupportsGroup(
612 ContextMenuContentType::ITEM_GROUP_CURRENT_EXTENSION
));
613 AppendAllExtensionItems();
616 if (content_type_
->SupportsGroup(
617 ContextMenuContentType::ITEM_GROUP_CURRENT_EXTENSION
)) {
618 DCHECK(!content_type_
->SupportsGroup(
619 ContextMenuContentType::ITEM_GROUP_ALL_EXTENSION
));
620 AppendCurrentExtensionItems();
623 if (content_type_
->SupportsGroup(
624 ContextMenuContentType::ITEM_GROUP_DEVELOPER
)) {
625 AppendDeveloperItems();
628 if (content_type_
->SupportsGroup(
629 ContextMenuContentType::ITEM_GROUP_DEVTOOLS_UNPACKED_EXT
)) {
630 AppendDevtoolsForUnpackedExtensions();
633 if (content_type_
->SupportsGroup(
634 ContextMenuContentType::ITEM_GROUP_PRINT_PREVIEW
)) {
635 AppendPrintPreviewItems();
638 if (content_type_
->SupportsGroup(
639 ContextMenuContentType::ITEM_GROUP_PASSWORD
)) {
640 AppendPasswordItems();
644 Profile
* RenderViewContextMenu::GetProfile() {
645 return Profile::FromBrowserContext(browser_context_
);
648 void RenderViewContextMenu::RecordUsedItem(int id
) {
649 int enum_id
= FindUMAEnumValueForCommand(id
);
651 const size_t kMappingSize
= arraysize(kUmaEnumToControlId
);
652 UMA_HISTOGRAM_ENUMERATION("RenderViewContextMenu.Used", enum_id
,
653 kUmaEnumToControlId
[kMappingSize
- 1].enum_id
);
655 NOTREACHED() << "Update kUmaEnumToControlId. Unhanded IDC: " << id
;
659 void RenderViewContextMenu::RecordShownItem(int id
) {
660 int enum_id
= FindUMAEnumValueForCommand(id
);
662 const size_t kMappingSize
= arraysize(kUmaEnumToControlId
);
663 UMA_HISTOGRAM_ENUMERATION("RenderViewContextMenu.Shown", enum_id
,
664 kUmaEnumToControlId
[kMappingSize
- 1].enum_id
);
666 // Just warning here. It's harder to maintain list of all possibly
667 // visible items than executable items.
668 DLOG(ERROR
) << "Update kUmaEnumToControlId. Unhanded IDC: " << id
;
672 #if defined(ENABLE_PLUGINS)
673 void RenderViewContextMenu::HandleAuthorizeAllPlugins() {
674 ChromePluginServiceFilter::GetInstance()->AuthorizeAllPlugins(
675 source_web_contents_
, false, std::string());
679 void RenderViewContextMenu::AppendPrintPreviewItems() {
680 #if defined(ENABLE_PRINT_PREVIEW)
681 if (!print_preview_menu_observer_
.get()) {
682 print_preview_menu_observer_
.reset(
683 new PrintPreviewContextMenuObserver(source_web_contents_
));
686 observers_
.AddObserver(print_preview_menu_observer_
.get());
690 const Extension
* RenderViewContextMenu::GetExtension() const {
691 return extensions::ProcessManager::Get(browser_context_
)
692 ->GetExtensionForWebContents(source_web_contents_
);
695 void RenderViewContextMenu::AppendDeveloperItems() {
696 // Show Inspect Element in DevTools itself only in case of the debug
698 bool show_developer_items
= !IsDevToolsURL(params_
.page_url
);
700 #if defined(DEBUG_DEVTOOLS)
701 show_developer_items
= true;
704 if (!show_developer_items
)
707 // In the DevTools popup menu, "developer items" is normally the only
708 // section, so omit the separator there.
709 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
710 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_INSPECTELEMENT
,
711 IDS_CONTENT_CONTEXT_INSPECTELEMENT
);
714 void RenderViewContextMenu::AppendDevtoolsForUnpackedExtensions() {
715 // Add a separator if there are any items already in the menu.
716 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
718 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
,
719 IDS_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
);
720 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP
,
721 IDS_CONTENT_CONTEXT_RESTART_APP
);
722 AppendDeveloperItems();
723 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
,
724 IDS_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
);
727 void RenderViewContextMenu::AppendLinkItems() {
728 if (!params_
.link_url
.is_empty()) {
729 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
,
730 IDS_CONTENT_CONTEXT_OPENLINKNEWTAB
);
731 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
,
732 IDS_CONTENT_CONTEXT_OPENLINKNEWWINDOW
);
733 if (params_
.link_url
.is_valid()) {
734 AppendProtocolHandlerSubMenu();
737 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
,
738 IDS_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
);
739 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVELINKAS
,
740 IDS_CONTENT_CONTEXT_SAVELINKAS
);
743 menu_model_
.AddItemWithStringId(
744 IDC_CONTENT_CONTEXT_COPYLINKLOCATION
,
745 params_
.link_url
.SchemeIs(url::kMailToScheme
) ?
746 IDS_CONTENT_CONTEXT_COPYEMAILADDRESS
:
747 IDS_CONTENT_CONTEXT_COPYLINKLOCATION
);
750 void RenderViewContextMenu::AppendImageItems() {
751 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEIMAGEAS
,
752 IDS_CONTENT_CONTEXT_SAVEIMAGEAS
);
753 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYIMAGELOCATION
,
754 IDS_CONTENT_CONTEXT_COPYIMAGELOCATION
);
755 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYIMAGE
,
756 IDS_CONTENT_CONTEXT_COPYIMAGE
);
757 std::map
<std::string
, std::string
>::const_iterator it
=
758 params_
.properties
.find(data_reduction_proxy::chrome_proxy_header());
759 if (it
!= params_
.properties
.end() && it
->second
==
760 data_reduction_proxy::chrome_proxy_lo_fi_directive()) {
761 menu_model_
.AddItemWithStringId(
762 IDC_CONTENT_CONTEXT_LOAD_ORIGINAL_IMAGE
,
763 IDS_CONTENT_CONTEXT_LOAD_ORIGINAL_IMAGE
);
765 DataReductionProxyChromeSettings
* settings
=
766 DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
768 if (settings
&& settings
->CanUseDataReductionProxy(params_
.src_url
)) {
769 menu_model_
.AddItemWithStringId(
770 IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
,
771 IDS_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
);
773 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB
,
774 IDS_CONTENT_CONTEXT_OPENIMAGENEWTAB
);
778 void RenderViewContextMenu::AppendSearchWebForImageItems() {
779 TemplateURLService
* service
=
780 TemplateURLServiceFactory::GetForProfile(GetProfile());
781 const TemplateURL
* const default_provider
=
782 service
->GetDefaultSearchProvider();
783 if (params_
.has_image_contents
&& default_provider
&&
784 !default_provider
->image_url().empty() &&
785 default_provider
->image_url_ref().IsValid(service
->search_terms_data())) {
787 IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
,
788 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
,
789 default_provider
->short_name()));
793 void RenderViewContextMenu::AppendAudioItems() {
795 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
796 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEAVAS
,
797 IDS_CONTENT_CONTEXT_SAVEAUDIOAS
);
798 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYAVLOCATION
,
799 IDS_CONTENT_CONTEXT_COPYAUDIOLOCATION
);
800 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENAVNEWTAB
,
801 IDS_CONTENT_CONTEXT_OPENAUDIONEWTAB
);
804 void RenderViewContextMenu::AppendCanvasItems() {
805 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEIMAGEAS
,
806 IDS_CONTENT_CONTEXT_SAVEIMAGEAS
);
807 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYIMAGE
,
808 IDS_CONTENT_CONTEXT_COPYIMAGE
);
811 void RenderViewContextMenu::AppendVideoItems() {
813 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
814 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEAVAS
,
815 IDS_CONTENT_CONTEXT_SAVEVIDEOAS
);
816 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPYAVLOCATION
,
817 IDS_CONTENT_CONTEXT_COPYVIDEOLOCATION
);
818 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_OPENAVNEWTAB
,
819 IDS_CONTENT_CONTEXT_OPENVIDEONEWTAB
);
822 void RenderViewContextMenu::AppendMediaItems() {
823 int media_flags
= params_
.media_flags
;
825 menu_model_
.AddItemWithStringId(
826 IDC_CONTENT_CONTEXT_PLAYPAUSE
,
827 media_flags
& WebContextMenuData::MediaPaused
?
828 IDS_CONTENT_CONTEXT_PLAY
:
829 IDS_CONTENT_CONTEXT_PAUSE
);
831 menu_model_
.AddItemWithStringId(
832 IDC_CONTENT_CONTEXT_MUTE
,
833 media_flags
& WebContextMenuData::MediaMuted
?
834 IDS_CONTENT_CONTEXT_UNMUTE
:
835 IDS_CONTENT_CONTEXT_MUTE
);
837 menu_model_
.AddCheckItemWithStringId(IDC_CONTENT_CONTEXT_LOOP
,
838 IDS_CONTENT_CONTEXT_LOOP
);
839 menu_model_
.AddCheckItemWithStringId(IDC_CONTENT_CONTEXT_CONTROLS
,
840 IDS_CONTENT_CONTEXT_CONTROLS
);
843 void RenderViewContextMenu::AppendPluginItems() {
844 if (params_
.page_url
== params_
.src_url
||
845 guest_view::GuestViewBase::IsGuest(source_web_contents_
)) {
846 // Full page plugin, so show page menu items.
847 if (params_
.link_url
.is_empty() && params_
.selection_text
.empty())
850 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SAVEAVAS
,
851 IDS_CONTENT_CONTEXT_SAVEPAGEAS
);
852 // The "Print" menu item should always be included for plugins. If
853 // content_type_->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PRINT)
854 // is true the item will be added inside AppendPrintItem(). Otherwise we
856 if (!content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_PRINT
))
857 menu_model_
.AddItemWithStringId(IDC_PRINT
, IDS_CONTENT_CONTEXT_PRINT
);
861 void RenderViewContextMenu::AppendPageItems() {
862 menu_model_
.AddItemWithStringId(IDC_BACK
, IDS_CONTENT_CONTEXT_BACK
);
863 menu_model_
.AddItemWithStringId(IDC_FORWARD
, IDS_CONTENT_CONTEXT_FORWARD
);
864 menu_model_
.AddItemWithStringId(IDC_RELOAD
, IDS_CONTENT_CONTEXT_RELOAD
);
865 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
866 menu_model_
.AddItemWithStringId(IDC_SAVE_PAGE
,
867 IDS_CONTENT_CONTEXT_SAVEPAGEAS
);
868 menu_model_
.AddItemWithStringId(IDC_PRINT
, IDS_CONTENT_CONTEXT_PRINT
);
870 if (TranslateService::IsTranslatableURL(params_
.page_url
)) {
871 std::string locale
= g_browser_process
->GetApplicationLocale();
872 locale
= translate::TranslateDownloadManager::GetLanguageCode(locale
);
873 base::string16 language
=
874 l10n_util::GetDisplayNameForLocale(locale
, locale
, true);
876 IDC_CONTENT_CONTEXT_TRANSLATE
,
877 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_TRANSLATE
, language
));
880 menu_model_
.AddItemWithStringId(IDC_VIEW_SOURCE
,
881 IDS_CONTENT_CONTEXT_VIEWPAGESOURCE
);
882 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_VIEWPAGEINFO
,
883 IDS_CONTENT_CONTEXT_VIEWPAGEINFO
);
886 void RenderViewContextMenu::AppendFrameItems() {
887 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_RELOADFRAME
,
888 IDS_CONTENT_CONTEXT_RELOADFRAME
);
889 // These two menu items have yet to be implemented.
890 // http://code.google.com/p/chromium/issues/detail?id=11827
891 // IDS_CONTENT_CONTEXT_SAVEFRAMEAS
892 // IDS_CONTENT_CONTEXT_PRINTFRAME
893 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE
,
894 IDS_CONTENT_CONTEXT_VIEWFRAMESOURCE
);
895 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_VIEWFRAMEINFO
,
896 IDS_CONTENT_CONTEXT_VIEWFRAMEINFO
);
899 void RenderViewContextMenu::AppendCopyItem() {
900 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPY
,
901 IDS_CONTENT_CONTEXT_COPY
);
904 void RenderViewContextMenu::AppendPrintItem() {
905 if (GetPrefs(browser_context_
)->GetBoolean(prefs::kPrintingEnabled
) &&
906 (params_
.media_type
== WebContextMenuData::MediaTypeNone
||
907 params_
.media_flags
& WebContextMenuData::MediaCanPrint
)) {
908 menu_model_
.AddItemWithStringId(IDC_PRINT
, IDS_CONTENT_CONTEXT_PRINT
);
912 void RenderViewContextMenu::AppendRotationItems() {
913 if (params_
.media_flags
& WebContextMenuData::MediaCanRotate
) {
914 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
915 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_ROTATECW
,
916 IDS_CONTENT_CONTEXT_ROTATECW
);
917 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_ROTATECCW
,
918 IDS_CONTENT_CONTEXT_ROTATECCW
);
922 void RenderViewContextMenu::AppendSearchProvider() {
923 DCHECK(browser_context_
);
925 base::TrimWhitespace(params_
.selection_text
, base::TRIM_ALL
,
926 ¶ms_
.selection_text
);
927 if (params_
.selection_text
.empty())
930 base::ReplaceChars(params_
.selection_text
, AutocompleteMatch::kInvalidChars
,
931 base::ASCIIToUTF16(" "), ¶ms_
.selection_text
);
933 AutocompleteMatch match
;
934 AutocompleteClassifierFactory::GetForProfile(GetProfile())->Classify(
935 params_
.selection_text
,
938 metrics::OmniboxEventProto::INVALID_SPEC
,
941 selection_navigation_url_
= match
.destination_url
;
942 if (!selection_navigation_url_
.is_valid())
945 base::string16 printable_selection_text
= PrintableSelectionText();
946 EscapeAmpersands(&printable_selection_text
);
948 if (AutocompleteMatch::IsSearchType(match
.type
)) {
949 const TemplateURL
* const default_provider
=
950 TemplateURLServiceFactory::GetForProfile(GetProfile())
951 ->GetDefaultSearchProvider();
952 if (!default_provider
)
955 IDC_CONTENT_CONTEXT_SEARCHWEBFOR
,
956 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_SEARCHWEBFOR
,
957 default_provider
->short_name(),
958 printable_selection_text
));
960 if ((selection_navigation_url_
!= params_
.link_url
) &&
961 ChildProcessSecurityPolicy::GetInstance()->IsWebSafeScheme(
962 selection_navigation_url_
.scheme())) {
964 IDC_CONTENT_CONTEXT_GOTOURL
,
965 l10n_util::GetStringFUTF16(IDS_CONTENT_CONTEXT_GOTOURL
,
966 printable_selection_text
));
971 void RenderViewContextMenu::AppendEditableItems() {
972 const bool use_spellcheck_and_search
= !chrome::IsRunningInForcedAppMode();
974 if (use_spellcheck_and_search
)
975 AppendSpellingSuggestionsSubMenu();
977 if (!IsDevToolsURL(params_
.page_url
)) {
978 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_UNDO
,
979 IDS_CONTENT_CONTEXT_UNDO
);
980 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_REDO
,
981 IDS_CONTENT_CONTEXT_REDO
);
982 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
985 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_CUT
,
986 IDS_CONTENT_CONTEXT_CUT
);
987 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_COPY
,
988 IDS_CONTENT_CONTEXT_COPY
);
989 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE
,
990 IDS_CONTENT_CONTEXT_PASTE
);
991 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
,
992 IDS_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
);
993 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_DELETE
,
994 IDS_CONTENT_CONTEXT_DELETE
);
995 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
997 if (use_spellcheck_and_search
&& !params_
.keyword_url
.is_empty()) {
998 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_ADDSEARCHENGINE
,
999 IDS_CONTENT_CONTEXT_ADDSEARCHENGINE
);
1000 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
1003 if (use_spellcheck_and_search
)
1004 AppendSpellcheckOptionsSubMenu();
1005 AppendPlatformEditableItems();
1007 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
1008 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_SELECTALL
,
1009 IDS_CONTENT_CONTEXT_SELECTALL
);
1012 void RenderViewContextMenu::AppendSpellingSuggestionsSubMenu() {
1013 if (!spelling_menu_observer_
.get())
1014 spelling_menu_observer_
.reset(new SpellingMenuObserver(this));
1015 observers_
.AddObserver(spelling_menu_observer_
.get());
1016 spelling_menu_observer_
->InitMenu(params_
);
1019 void RenderViewContextMenu::AppendSpellcheckOptionsSubMenu() {
1020 if (!spellchecker_submenu_observer_
.get()) {
1021 spellchecker_submenu_observer_
.reset(new SpellCheckerSubMenuObserver(
1022 this, this, kSpellcheckRadioGroup
));
1024 spellchecker_submenu_observer_
->InitMenu(params_
);
1025 observers_
.AddObserver(spellchecker_submenu_observer_
.get());
1028 void RenderViewContextMenu::AppendProtocolHandlerSubMenu() {
1029 const ProtocolHandlerRegistry::ProtocolHandlerList handlers
=
1030 GetHandlersForLinkUrl();
1031 if (handlers
.empty())
1033 size_t max
= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST
-
1034 IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
;
1035 for (size_t i
= 0; i
< handlers
.size() && i
<= max
; i
++) {
1036 protocol_handler_submenu_model_
.AddItem(
1037 IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
+ i
,
1038 base::UTF8ToUTF16(handlers
[i
].url().host()));
1040 protocol_handler_submenu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
1041 protocol_handler_submenu_model_
.AddItem(
1042 IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_SETTINGS
,
1043 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_OPENLINKWITH_CONFIGURE
));
1045 menu_model_
.AddSubMenu(
1046 IDC_CONTENT_CONTEXT_OPENLINKWITH
,
1047 l10n_util::GetStringUTF16(IDS_CONTENT_CONTEXT_OPENLINKWITH
),
1048 &protocol_handler_submenu_model_
);
1051 void RenderViewContextMenu::AppendPasswordItems() {
1052 if (!password_manager::ForceSavingExperimentEnabled())
1055 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
1056 menu_model_
.AddItemWithStringId(IDC_CONTENT_CONTEXT_FORCESAVEPASSWORD
,
1057 IDS_CONTENT_CONTEXT_FORCESAVEPASSWORD
);
1060 // Menu delegate functions -----------------------------------------------------
1062 bool RenderViewContextMenu::IsCommandIdEnabled(int id
) const {
1064 bool enabled
= false;
1065 if (RenderViewContextMenuBase::IsCommandIdKnown(id
, &enabled
))
1069 CoreTabHelper
* core_tab_helper
=
1070 CoreTabHelper::FromWebContents(source_web_contents_
);
1071 int content_restrictions
= 0;
1072 if (core_tab_helper
)
1073 content_restrictions
= core_tab_helper
->content_restrictions();
1074 if (id
== IDC_PRINT
&& (content_restrictions
& CONTENT_RESTRICTION_PRINT
))
1077 if (id
== IDC_SAVE_PAGE
&&
1078 (content_restrictions
& CONTENT_RESTRICTION_SAVE
)) {
1082 PrefService
* prefs
= GetPrefs(browser_context_
);
1084 // Allow Spell Check language items on sub menu for text area context menu.
1085 if ((id
>= IDC_SPELLCHECK_LANGUAGES_FIRST
) &&
1086 (id
< IDC_SPELLCHECK_LANGUAGES_LAST
)) {
1087 return prefs
->GetBoolean(prefs::kEnableContinuousSpellcheck
);
1091 if (ContextMenuMatcher::IsExtensionsCustomCommandId(id
))
1092 return extension_items_
.IsCommandIdEnabled(id
);
1094 if (id
>= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
&&
1095 id
<= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST
) {
1099 IncognitoModePrefs::Availability incognito_avail
=
1100 IncognitoModePrefs::GetAvailability(prefs
);
1103 return embedder_web_contents_
->GetController().CanGoBack();
1106 return embedder_web_contents_
->GetController().CanGoForward();
1109 CoreTabHelper
* core_tab_helper
=
1110 CoreTabHelper::FromWebContents(embedder_web_contents_
);
1111 if (!core_tab_helper
)
1114 CoreTabHelperDelegate
* core_delegate
= core_tab_helper
->delegate();
1115 return !core_delegate
||
1116 core_delegate
->CanReloadContents(embedder_web_contents_
);
1119 case IDC_VIEW_SOURCE
:
1120 case IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE
:
1121 return embedder_web_contents_
->GetController().CanViewSource();
1123 case IDC_CONTENT_CONTEXT_INSPECTELEMENT
:
1124 case IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
:
1125 case IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
:
1126 case IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP
:
1127 return IsDevCommandEnabled(id
);
1129 case IDC_CONTENT_CONTEXT_VIEWPAGEINFO
:
1130 if (embedder_web_contents_
->GetController().GetVisibleEntry() == NULL
)
1132 // Disabled if no browser is associated (e.g. desktop notifications).
1133 if (chrome::FindBrowserWithWebContents(embedder_web_contents_
) == NULL
)
1137 case IDC_CONTENT_CONTEXT_TRANSLATE
: {
1138 ChromeTranslateClient
* chrome_translate_client
=
1139 ChromeTranslateClient::FromWebContents(embedder_web_contents_
);
1140 if (!chrome_translate_client
)
1142 std::string original_lang
=
1143 chrome_translate_client
->GetLanguageState().original_language();
1144 std::string target_lang
= g_browser_process
->GetApplicationLocale();
1146 translate::TranslateDownloadManager::GetLanguageCode(target_lang
);
1147 // Note that we intentionally enable the menu even if the original and
1148 // target languages are identical. This is to give a way to user to
1149 // translate a page that might contains text fragments in a different
1151 return ((params_
.edit_flags
& WebContextMenuData::CanTranslate
) != 0) &&
1152 !original_lang
.empty() && // Did we receive the page language yet?
1153 !chrome_translate_client
->GetLanguageState().IsPageTranslated() &&
1154 !embedder_web_contents_
->GetInterstitialPage() &&
1155 // There are some application locales which can't be used as a
1156 // target language for translation.
1157 translate::TranslateDownloadManager::IsSupportedLanguage(
1159 // Disable on the Instant Extended NTP.
1160 !chrome::IsInstantNTP(embedder_web_contents_
);
1163 case IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
:
1164 case IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
:
1165 return params_
.link_url
.is_valid();
1167 case IDC_CONTENT_CONTEXT_COPYLINKLOCATION
:
1168 return params_
.unfiltered_link_url
.is_valid();
1170 case IDC_CONTENT_CONTEXT_SAVELINKAS
: {
1171 PrefService
* local_state
= g_browser_process
->local_state();
1172 DCHECK(local_state
);
1173 // Test if file-selection dialogs are forbidden by policy.
1174 if (!local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
))
1177 return params_
.link_url
.is_valid() &&
1178 ProfileIOData::IsHandledProtocol(params_
.link_url
.scheme());
1181 case IDC_CONTENT_CONTEXT_SAVEIMAGEAS
: {
1182 PrefService
* local_state
= g_browser_process
->local_state();
1183 DCHECK(local_state
);
1184 // Test if file-selection dialogs are forbidden by policy.
1185 if (!local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
))
1188 return params_
.has_image_contents
;
1191 // The images shown in the most visited thumbnails can't be opened or
1192 // searched for conventionally.
1193 case IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
:
1194 case IDC_CONTENT_CONTEXT_LOAD_ORIGINAL_IMAGE
:
1195 case IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB
:
1196 case IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
:
1197 return params_
.src_url
.is_valid() &&
1198 (params_
.src_url
.scheme() != content::kChromeUIScheme
);
1200 case IDC_CONTENT_CONTEXT_COPYIMAGE
:
1201 return params_
.has_image_contents
;
1203 // Media control commands should all be disabled if the player is in an
1205 case IDC_CONTENT_CONTEXT_PLAYPAUSE
:
1206 case IDC_CONTENT_CONTEXT_LOOP
:
1207 return (params_
.media_flags
&
1208 WebContextMenuData::MediaInError
) == 0;
1210 // Mute and unmute should also be disabled if the player has no audio.
1211 case IDC_CONTENT_CONTEXT_MUTE
:
1212 return (params_
.media_flags
&
1213 WebContextMenuData::MediaHasAudio
) != 0 &&
1214 (params_
.media_flags
&
1215 WebContextMenuData::MediaInError
) == 0;
1217 case IDC_CONTENT_CONTEXT_CONTROLS
:
1218 return (params_
.media_flags
&
1219 WebContextMenuData::MediaCanToggleControls
) != 0;
1221 case IDC_CONTENT_CONTEXT_ROTATECW
:
1222 case IDC_CONTENT_CONTEXT_ROTATECCW
:
1224 (params_
.media_flags
& WebContextMenuData::MediaCanRotate
) != 0;
1226 case IDC_CONTENT_CONTEXT_COPYAVLOCATION
:
1227 case IDC_CONTENT_CONTEXT_COPYIMAGELOCATION
:
1228 return params_
.src_url
.is_valid();
1230 case IDC_CONTENT_CONTEXT_SAVEAVAS
: {
1231 PrefService
* local_state
= g_browser_process
->local_state();
1232 DCHECK(local_state
);
1233 // Test if file-selection dialogs are forbidden by policy.
1234 if (!local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
))
1237 const GURL
& url
= params_
.src_url
;
1239 (params_
.media_flags
& WebContextMenuData::MediaCanSave
) &&
1240 url
.is_valid() && ProfileIOData::IsHandledProtocol(url
.scheme());
1241 #if defined(ENABLE_PRINT_PREVIEW)
1242 // Do not save the preview PDF on the print preview page.
1243 can_save
= can_save
&&
1244 !(printing::PrintPreviewDialogController::IsPrintPreviewURL(url
));
1249 case IDC_CONTENT_CONTEXT_OPENAVNEWTAB
:
1250 // Currently, a media element can be opened in a new tab iff it can
1251 // be saved. So rather than duplicating the MediaCanSave flag, we rely
1253 return !!(params_
.media_flags
& WebContextMenuData::MediaCanSave
);
1255 case IDC_SAVE_PAGE
: {
1256 CoreTabHelper
* core_tab_helper
=
1257 CoreTabHelper::FromWebContents(embedder_web_contents_
);
1258 if (!core_tab_helper
)
1261 CoreTabHelperDelegate
* core_delegate
= core_tab_helper
->delegate();
1262 if (core_delegate
&&
1263 !core_delegate
->CanSaveContents(embedder_web_contents_
))
1266 PrefService
* local_state
= g_browser_process
->local_state();
1267 DCHECK(local_state
);
1268 // Test if file-selection dialogs are forbidden by policy.
1269 if (!local_state
->GetBoolean(prefs::kAllowFileSelectionDialogs
))
1272 // We save the last committed entry (which the user is looking at), as
1273 // opposed to any pending URL that hasn't committed yet.
1274 NavigationEntry
* entry
=
1275 embedder_web_contents_
->GetController().GetLastCommittedEntry();
1276 return content::IsSavableURL(entry
? entry
->GetURL() : GURL());
1279 case IDC_CONTENT_CONTEXT_RELOADFRAME
:
1280 return params_
.frame_url
.is_valid();
1282 case IDC_CONTENT_CONTEXT_UNDO
:
1283 return !!(params_
.edit_flags
& WebContextMenuData::CanUndo
);
1285 case IDC_CONTENT_CONTEXT_REDO
:
1286 return !!(params_
.edit_flags
& WebContextMenuData::CanRedo
);
1288 case IDC_CONTENT_CONTEXT_CUT
:
1289 return !!(params_
.edit_flags
& WebContextMenuData::CanCut
);
1291 case IDC_CONTENT_CONTEXT_COPY
:
1292 return !!(params_
.edit_flags
& WebContextMenuData::CanCopy
);
1294 case IDC_CONTENT_CONTEXT_PASTE
: {
1295 if (!(params_
.edit_flags
& WebContextMenuData::CanPaste
))
1298 std::vector
<base::string16
> types
;
1300 ui::Clipboard::GetForCurrentThread()->ReadAvailableTypes(
1301 ui::CLIPBOARD_TYPE_COPY_PASTE
, &types
, &ignore
);
1302 return !types
.empty();
1305 case IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
: {
1306 if (!(params_
.edit_flags
& WebContextMenuData::CanPaste
))
1309 return ui::Clipboard::GetForCurrentThread()->IsFormatAvailable(
1310 ui::Clipboard::GetPlainTextFormatType(),
1311 ui::CLIPBOARD_TYPE_COPY_PASTE
);
1314 case IDC_CONTENT_CONTEXT_DELETE
:
1315 return !!(params_
.edit_flags
& WebContextMenuData::CanDelete
);
1317 case IDC_CONTENT_CONTEXT_SELECTALL
:
1318 return !!(params_
.edit_flags
& WebContextMenuData::CanSelectAll
);
1320 case IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
:
1321 return !browser_context_
->IsOffTheRecord() &&
1322 params_
.link_url
.is_valid() &&
1323 incognito_avail
!= IncognitoModePrefs::DISABLED
;
1326 return prefs
->GetBoolean(prefs::kPrintingEnabled
) &&
1327 (params_
.media_type
== WebContextMenuData::MediaTypeNone
||
1328 params_
.media_flags
& WebContextMenuData::MediaCanPrint
);
1330 case IDC_CONTENT_CONTEXT_SEARCHWEBFOR
:
1331 case IDC_CONTENT_CONTEXT_GOTOURL
:
1332 case IDC_SPELLPANEL_TOGGLE
:
1333 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
:
1335 case IDC_CONTENT_CONTEXT_VIEWFRAMEINFO
:
1336 // Disabled if no browser is associated (e.g. desktop notifications).
1337 if (chrome::FindBrowserWithWebContents(source_web_contents_
) == NULL
)
1341 case IDC_CHECK_SPELLING_WHILE_TYPING
:
1342 return prefs
->GetBoolean(prefs::kEnableContinuousSpellcheck
);
1344 #if !defined(OS_MACOSX) && defined(OS_POSIX)
1345 // TODO(suzhe): this should not be enabled for password fields.
1346 case IDC_INPUT_METHODS_MENU
:
1350 case IDC_CONTENT_CONTEXT_ADDSEARCHENGINE
:
1351 return !params_
.keyword_url
.is_empty();
1353 case IDC_SPELLCHECK_MENU
:
1356 case IDC_CONTENT_CONTEXT_OPENLINKWITH
:
1359 case IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_SETTINGS
:
1362 case IDC_CONTENT_CONTEXT_FORCESAVEPASSWORD
:
1371 bool RenderViewContextMenu::IsCommandIdChecked(int id
) const {
1372 if (RenderViewContextMenuBase::IsCommandIdChecked(id
))
1375 // See if the video is set to looping.
1376 if (id
== IDC_CONTENT_CONTEXT_LOOP
)
1377 return (params_
.media_flags
& WebContextMenuData::MediaLoop
) != 0;
1379 if (id
== IDC_CONTENT_CONTEXT_CONTROLS
)
1380 return (params_
.media_flags
& WebContextMenuData::MediaControls
) != 0;
1383 if (ContextMenuMatcher::IsExtensionsCustomCommandId(id
))
1384 return extension_items_
.IsCommandIdChecked(id
);
1389 void RenderViewContextMenu::ExecuteCommand(int id
, int event_flags
) {
1390 RenderViewContextMenuBase::ExecuteCommand(id
, event_flags
);
1391 if (command_executed_
)
1393 command_executed_
= true;
1395 RenderFrameHost
* render_frame_host
= GetRenderFrameHost();
1397 // Process extension menu items.
1398 if (ContextMenuMatcher::IsExtensionsCustomCommandId(id
)) {
1399 extension_items_
.ExecuteCommand(id
, source_web_contents_
, params_
);
1403 if (id
>= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
&&
1404 id
<= IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_LAST
) {
1405 ProtocolHandlerRegistry::ProtocolHandlerList handlers
=
1406 GetHandlersForLinkUrl();
1407 if (handlers
.empty())
1410 content::RecordAction(
1411 UserMetricsAction("RegisterProtocolHandler.ContextMenu_Open"));
1412 int handlerIndex
= id
- IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_FIRST
;
1413 WindowOpenDisposition disposition
=
1414 ForceNewTabDispositionFromEventFlags(event_flags
);
1415 OpenURL(handlers
[handlerIndex
].TranslateUrl(params_
.link_url
),
1416 GetDocumentURL(params_
),
1418 ui::PAGE_TRANSITION_LINK
);
1423 case IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
: {
1425 chrome::FindBrowserWithWebContents(source_web_contents_
);
1426 OpenURL(params_
.link_url
,
1427 GetDocumentURL(params_
),
1428 !browser
|| browser
->is_app() ?
1429 NEW_FOREGROUND_TAB
: NEW_BACKGROUND_TAB
,
1430 ui::PAGE_TRANSITION_LINK
);
1433 case IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
:
1434 OpenURL(params_
.link_url
,
1435 GetDocumentURL(params_
),
1437 ui::PAGE_TRANSITION_LINK
);
1440 case IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
:
1441 OpenURL(params_
.link_url
, GURL(), OFF_THE_RECORD
,
1442 ui::PAGE_TRANSITION_LINK
);
1445 case IDC_CONTENT_CONTEXT_SAVELINKAS
: {
1446 RecordDownloadSource(DOWNLOAD_INITIATED_BY_CONTEXT_MENU
);
1447 const GURL
& url
= params_
.link_url
;
1448 content::Referrer referrer
= CreateSaveAsReferrer(url
, params_
);
1449 DownloadManager
* dlm
=
1450 BrowserContext::GetDownloadManager(browser_context_
);
1451 scoped_ptr
<DownloadUrlParameters
> dl_params(
1452 DownloadUrlParameters::FromWebContents(source_web_contents_
, url
));
1453 dl_params
->set_referrer(referrer
);
1454 dl_params
->set_referrer_encoding(params_
.frame_charset
);
1455 dl_params
->set_suggested_name(params_
.suggested_filename
);
1456 dl_params
->set_prompt(true);
1457 dlm
->DownloadUrl(dl_params
.Pass());
1461 case IDC_CONTENT_CONTEXT_SAVEAVAS
:
1462 case IDC_CONTENT_CONTEXT_SAVEIMAGEAS
: {
1463 bool is_large_data_url
= params_
.has_image_contents
&&
1464 params_
.src_url
.is_empty();
1465 if (params_
.media_type
== WebContextMenuData::MediaTypeCanvas
||
1466 (params_
.media_type
== WebContextMenuData::MediaTypeImage
&&
1467 is_large_data_url
)) {
1468 source_web_contents_
->GetRenderViewHost()->SaveImageAt(
1469 params_
.x
, params_
.y
);
1471 RecordDownloadSource(DOWNLOAD_INITIATED_BY_CONTEXT_MENU
);
1472 const GURL
& url
= params_
.src_url
;
1473 content::Referrer referrer
= CreateSaveAsReferrer(url
, params_
);
1475 std::string headers
;
1476 DataReductionProxyChromeSettings
* settings
=
1477 DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
1479 if (params_
.media_type
== WebContextMenuData::MediaTypeImage
&&
1480 settings
&& settings
->CanUseDataReductionProxy(params_
.src_url
)) {
1481 headers
= data_reduction_proxy::kDataReductionPassThroughHeader
;
1484 source_web_contents_
->SaveFrameWithHeaders(url
, referrer
, headers
);
1489 case IDC_CONTENT_CONTEXT_COPYLINKLOCATION
:
1490 WriteURLToClipboard(params_
.unfiltered_link_url
);
1493 case IDC_CONTENT_CONTEXT_COPYIMAGELOCATION
:
1494 case IDC_CONTENT_CONTEXT_COPYAVLOCATION
:
1495 WriteURLToClipboard(params_
.src_url
);
1498 case IDC_CONTENT_CONTEXT_COPYIMAGE
:
1499 CopyImageAt(params_
.x
, params_
.y
);
1502 case IDC_CONTENT_CONTEXT_SEARCHWEBFORIMAGE
:
1503 GetImageThumbnailForSearch();
1506 case IDC_CONTENT_CONTEXT_OPEN_ORIGINAL_IMAGE_NEW_TAB
:
1507 OpenURLWithExtraHeaders(
1508 params_
.src_url
, GetDocumentURL(params_
), NEW_BACKGROUND_TAB
,
1509 ui::PAGE_TRANSITION_LINK
,
1510 data_reduction_proxy::kDataReductionPassThroughHeader
);
1513 case IDC_CONTENT_CONTEXT_LOAD_ORIGINAL_IMAGE
:
1514 LoadOriginalImage();
1517 case IDC_CONTENT_CONTEXT_OPENIMAGENEWTAB
:
1518 case IDC_CONTENT_CONTEXT_OPENAVNEWTAB
:
1519 OpenURL(params_
.src_url
,
1520 GetDocumentURL(params_
),
1522 ui::PAGE_TRANSITION_LINK
);
1525 case IDC_CONTENT_CONTEXT_PLAYPAUSE
: {
1526 bool play
= !!(params_
.media_flags
& WebContextMenuData::MediaPaused
);
1528 content::RecordAction(UserMetricsAction("MediaContextMenu_Play"));
1530 content::RecordAction(UserMetricsAction("MediaContextMenu_Pause"));
1532 MediaPlayerActionAt(gfx::Point(params_
.x
, params_
.y
),
1533 WebMediaPlayerAction(
1534 WebMediaPlayerAction::Play
, play
));
1538 case IDC_CONTENT_CONTEXT_MUTE
: {
1539 bool mute
= !(params_
.media_flags
& WebContextMenuData::MediaMuted
);
1541 content::RecordAction(UserMetricsAction("MediaContextMenu_Mute"));
1543 content::RecordAction(UserMetricsAction("MediaContextMenu_Unmute"));
1545 MediaPlayerActionAt(gfx::Point(params_
.x
, params_
.y
),
1546 WebMediaPlayerAction(
1547 WebMediaPlayerAction::Mute
, mute
));
1551 case IDC_CONTENT_CONTEXT_LOOP
:
1552 content::RecordAction(UserMetricsAction("MediaContextMenu_Loop"));
1553 MediaPlayerActionAt(gfx::Point(params_
.x
, params_
.y
),
1554 WebMediaPlayerAction(
1555 WebMediaPlayerAction::Loop
,
1556 !IsCommandIdChecked(IDC_CONTENT_CONTEXT_LOOP
)));
1559 case IDC_CONTENT_CONTEXT_CONTROLS
:
1560 content::RecordAction(UserMetricsAction("MediaContextMenu_Controls"));
1561 MediaPlayerActionAt(
1562 gfx::Point(params_
.x
, params_
.y
),
1563 WebMediaPlayerAction(
1564 WebMediaPlayerAction::Controls
,
1565 !IsCommandIdChecked(IDC_CONTENT_CONTEXT_CONTROLS
)));
1568 case IDC_CONTENT_CONTEXT_ROTATECW
:
1569 content::RecordAction(
1570 UserMetricsAction("PluginContextMenu_RotateClockwise"));
1572 gfx::Point(params_
.x
, params_
.y
),
1573 WebPluginAction(WebPluginAction::Rotate90Clockwise
, true));
1576 case IDC_CONTENT_CONTEXT_ROTATECCW
:
1577 content::RecordAction(
1578 UserMetricsAction("PluginContextMenu_RotateCounterclockwise"));
1580 gfx::Point(params_
.x
, params_
.y
),
1581 WebPluginAction(WebPluginAction::Rotate90Counterclockwise
, true));
1585 embedder_web_contents_
->GetController().GoBack();
1589 embedder_web_contents_
->GetController().GoForward();
1593 embedder_web_contents_
->OnSavePage();
1597 embedder_web_contents_
->GetController().Reload(true);
1600 case IDC_CONTENT_CONTEXT_RELOAD_PACKAGED_APP
: {
1601 const Extension
* platform_app
= GetExtension();
1602 DCHECK(platform_app
);
1603 DCHECK(platform_app
->is_platform_app());
1605 extensions::ExtensionSystem::Get(browser_context_
)
1606 ->extension_service()
1607 ->ReloadExtension(platform_app
->id());
1611 case IDC_CONTENT_CONTEXT_RESTART_PACKAGED_APP
: {
1612 const Extension
* platform_app
= GetExtension();
1613 DCHECK(platform_app
);
1614 DCHECK(platform_app
->is_platform_app());
1616 apps::AppLoadService::Get(GetProfile())
1617 ->RestartApplication(platform_app
->id());
1622 #if defined(ENABLE_PRINTING)
1623 if (params_
.media_type
!= WebContextMenuData::MediaTypeNone
) {
1624 if (render_frame_host
) {
1625 render_frame_host
->Send(new PrintMsg_PrintNodeUnderContextMenu(
1626 render_frame_host
->GetRoutingID()));
1631 printing::StartPrint(
1632 source_web_contents_
,
1633 GetPrefs(browser_context_
)->GetBoolean(prefs::kPrintPreviewDisabled
),
1634 !params_
.selection_text
.empty());
1635 #endif // ENABLE_PRINTING
1639 case IDC_VIEW_SOURCE
:
1640 embedder_web_contents_
->ViewSource();
1643 case IDC_CONTENT_CONTEXT_INSPECTELEMENT
:
1644 Inspect(params_
.x
, params_
.y
);
1647 case IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
: {
1648 const Extension
* platform_app
= GetExtension();
1649 DCHECK(platform_app
);
1650 DCHECK(platform_app
->is_platform_app());
1652 extensions::devtools_util::InspectBackgroundPage(platform_app
,
1657 case IDC_CONTENT_CONTEXT_VIEWPAGEINFO
: {
1658 NavigationController
* controller
=
1659 &embedder_web_contents_
->GetController();
1660 // Important to use GetVisibleEntry to match what's showing in the
1661 // omnibox. This may return null.
1662 NavigationEntry
* nav_entry
= controller
->GetVisibleEntry();
1666 chrome::FindBrowserWithWebContents(embedder_web_contents_
);
1667 chrome::ShowWebsiteSettings(browser
, embedder_web_contents_
,
1668 nav_entry
->GetURL(), nav_entry
->GetSSL());
1672 case IDC_CONTENT_CONTEXT_TRANSLATE
: {
1673 // A translation might have been triggered by the time the menu got
1674 // selected, do nothing in that case.
1675 ChromeTranslateClient
* chrome_translate_client
=
1676 ChromeTranslateClient::FromWebContents(embedder_web_contents_
);
1677 if (!chrome_translate_client
||
1678 chrome_translate_client
->GetLanguageState().IsPageTranslated() ||
1679 chrome_translate_client
->GetLanguageState().translation_pending()) {
1682 std::string original_lang
=
1683 chrome_translate_client
->GetLanguageState().original_language();
1684 std::string target_lang
= g_browser_process
->GetApplicationLocale();
1686 translate::TranslateDownloadManager::GetLanguageCode(target_lang
);
1687 // Since the user decided to translate for that language and site, clears
1688 // any preferences for not translating them.
1689 scoped_ptr
<translate::TranslatePrefs
> prefs(
1690 ChromeTranslateClient::CreateTranslatePrefs(
1691 GetPrefs(browser_context_
)));
1692 prefs
->UnblockLanguage(original_lang
);
1693 prefs
->RemoveSiteFromBlacklist(params_
.page_url
.HostNoBrackets());
1694 translate::TranslateManager
* manager
=
1695 chrome_translate_client
->GetTranslateManager();
1697 manager
->TranslatePage(original_lang
, target_lang
, true);
1701 case IDC_CONTENT_CONTEXT_RELOADFRAME
:
1702 // We always obey the cache here.
1703 // TODO(evanm): Perhaps we could allow shift-clicking the menu item to do
1704 // a cache-ignoring reload of the frame.
1705 source_web_contents_
->ReloadFocusedFrame(false);
1708 case IDC_CONTENT_CONTEXT_VIEWFRAMESOURCE
:
1709 source_web_contents_
->ViewFrameSource(params_
.frame_url
,
1710 params_
.frame_page_state
);
1713 case IDC_CONTENT_CONTEXT_VIEWFRAMEINFO
: {
1714 Browser
* browser
= chrome::FindBrowserWithWebContents(
1715 source_web_contents_
);
1716 chrome::ShowWebsiteSettings(browser
, source_web_contents_
,
1717 params_
.frame_url
, params_
.security_info
);
1721 case IDC_CONTENT_CONTEXT_UNDO
:
1722 source_web_contents_
->Undo();
1725 case IDC_CONTENT_CONTEXT_REDO
:
1726 source_web_contents_
->Redo();
1729 case IDC_CONTENT_CONTEXT_CUT
:
1730 source_web_contents_
->Cut();
1733 case IDC_CONTENT_CONTEXT_COPY
:
1734 source_web_contents_
->Copy();
1737 case IDC_CONTENT_CONTEXT_PASTE
:
1738 source_web_contents_
->Paste();
1741 case IDC_CONTENT_CONTEXT_PASTE_AND_MATCH_STYLE
:
1742 source_web_contents_
->PasteAndMatchStyle();
1745 case IDC_CONTENT_CONTEXT_DELETE
:
1746 source_web_contents_
->Delete();
1749 case IDC_CONTENT_CONTEXT_SELECTALL
:
1750 source_web_contents_
->SelectAll();
1753 case IDC_CONTENT_CONTEXT_SEARCHWEBFOR
:
1754 case IDC_CONTENT_CONTEXT_GOTOURL
: {
1755 WindowOpenDisposition disposition
=
1756 ForceNewTabDispositionFromEventFlags(event_flags
);
1757 OpenURL(selection_navigation_url_
, GURL(), disposition
,
1758 ui::PAGE_TRANSITION_LINK
);
1761 case IDC_CONTENT_CONTEXT_LANGUAGE_SETTINGS
: {
1762 WindowOpenDisposition disposition
=
1763 ForceNewTabDispositionFromEventFlags(event_flags
);
1764 GURL url
= chrome::GetSettingsUrl(chrome::kLanguageOptionsSubPage
);
1765 OpenURL(url
, GURL(), disposition
, ui::PAGE_TRANSITION_LINK
);
1769 case IDC_CONTENT_CONTEXT_PROTOCOL_HANDLER_SETTINGS
: {
1770 content::RecordAction(
1771 UserMetricsAction("RegisterProtocolHandler.ContextMenu_Settings"));
1772 WindowOpenDisposition disposition
=
1773 ForceNewTabDispositionFromEventFlags(event_flags
);
1774 GURL url
= chrome::GetSettingsUrl(chrome::kHandlerSettingsSubPage
);
1775 OpenURL(url
, GURL(), disposition
, ui::PAGE_TRANSITION_LINK
);
1779 case IDC_CONTENT_CONTEXT_ADDSEARCHENGINE
: {
1780 // Make sure the model is loaded.
1781 TemplateURLService
* model
=
1782 TemplateURLServiceFactory::GetForProfile(GetProfile());
1787 SearchEngineTabHelper
* search_engine_tab_helper
=
1788 SearchEngineTabHelper::FromWebContents(source_web_contents_
);
1789 if (search_engine_tab_helper
&&
1790 search_engine_tab_helper
->delegate()) {
1791 base::string16
keyword(TemplateURL::GenerateKeyword(params_
.page_url
));
1792 TemplateURLData data
;
1793 data
.SetShortName(keyword
);
1794 data
.SetKeyword(keyword
);
1795 data
.SetURL(params_
.keyword_url
.spec());
1797 TemplateURL::GenerateFaviconURL(params_
.page_url
.GetOrigin());
1798 // Takes ownership of the TemplateURL.
1799 search_engine_tab_helper
->delegate()->ConfirmAddSearchProvider(
1800 new TemplateURL(data
), GetProfile());
1805 case IDC_CONTENT_CONTEXT_FORCESAVEPASSWORD
:
1806 ChromePasswordManagerClient::FromWebContents(source_web_contents_
)->
1807 ForceSavePassword();
1816 ProtocolHandlerRegistry::ProtocolHandlerList
1817 RenderViewContextMenu::GetHandlersForLinkUrl() {
1818 ProtocolHandlerRegistry::ProtocolHandlerList handlers
=
1819 protocol_handler_registry_
->GetHandlersFor(params_
.link_url
.scheme());
1820 std::sort(handlers
.begin(), handlers
.end());
1824 void RenderViewContextMenu::NotifyMenuShown() {
1825 content::NotificationService::current()->Notify(
1826 chrome::NOTIFICATION_RENDER_VIEW_CONTEXT_MENU_SHOWN
,
1827 content::Source
<RenderViewContextMenu
>(this),
1828 content::NotificationService::NoDetails());
1831 void RenderViewContextMenu::NotifyURLOpened(
1833 content::WebContents
* new_contents
) {
1834 RetargetingDetails details
;
1835 details
.source_web_contents
= source_web_contents_
;
1836 // Don't use GetRenderFrameHost() as it may be NULL. crbug.com/399789
1837 details
.source_render_frame_id
= render_frame_id_
;
1838 details
.target_url
= url
;
1839 details
.target_web_contents
= new_contents
;
1840 details
.not_yet_in_tabstrip
= false;
1842 content::NotificationService::current()->Notify(
1843 chrome::NOTIFICATION_RETARGETING
,
1844 content::Source
<Profile
>(GetProfile()),
1845 content::Details
<RetargetingDetails
>(&details
));
1848 bool RenderViewContextMenu::IsDevCommandEnabled(int id
) const {
1849 if (id
== IDC_CONTENT_CONTEXT_INSPECTELEMENT
||
1850 id
== IDC_CONTENT_CONTEXT_INSPECTBACKGROUNDPAGE
) {
1851 if (!GetPrefs(browser_context_
)
1852 ->GetBoolean(prefs::kWebKitJavascriptEnabled
))
1855 // Don't enable the web inspector if the developer tools are disabled via
1856 // the preference dev-tools-disabled.
1857 if (GetPrefs(browser_context_
)->GetBoolean(prefs::kDevToolsDisabled
))
1864 base::string16
RenderViewContextMenu::PrintableSelectionText() {
1865 return gfx::TruncateString(params_
.selection_text
,
1866 kMaxSelectionTextLength
,
1870 void RenderViewContextMenu::EscapeAmpersands(base::string16
* text
) {
1871 base::ReplaceChars(*text
, base::ASCIIToUTF16("&"), base::ASCIIToUTF16("&&"),
1875 // Controller functions --------------------------------------------------------
1877 void RenderViewContextMenu::CopyImageAt(int x
, int y
) {
1878 source_web_contents_
->GetRenderViewHost()->CopyImageAt(x
, y
);
1881 void RenderViewContextMenu::LoadOriginalImage() {
1882 RenderFrameHost
* render_frame_host
= GetRenderFrameHost();
1883 if (!render_frame_host
)
1885 render_frame_host
->Send(new ChromeViewMsg_RequestReloadImageForContextNode(
1886 render_frame_host
->GetRoutingID()));
1889 void RenderViewContextMenu::GetImageThumbnailForSearch() {
1890 RenderFrameHost
* render_frame_host
= GetRenderFrameHost();
1891 if (!render_frame_host
)
1893 render_frame_host
->Send(new ChromeViewMsg_RequestThumbnailForContextNode(
1894 render_frame_host
->GetRoutingID(),
1895 kImageSearchThumbnailMinSize
,
1896 gfx::Size(kImageSearchThumbnailMaxWidth
,
1897 kImageSearchThumbnailMaxHeight
)));
1900 void RenderViewContextMenu::Inspect(int x
, int y
) {
1901 content::RecordAction(UserMetricsAction("DevTools_InspectElement"));
1902 RenderFrameHost
* render_frame_host
= GetRenderFrameHost();
1903 if (!render_frame_host
)
1905 DevToolsWindow::InspectElement(
1906 WebContents::FromRenderFrameHost(render_frame_host
), x
, y
);
1909 void RenderViewContextMenu::WriteURLToClipboard(const GURL
& url
) {
1910 chrome_common_net::WriteURLToClipboard(
1911 url
, GetPrefs(browser_context_
)->GetString(prefs::kAcceptLanguages
));
1914 void RenderViewContextMenu::MediaPlayerActionAt(
1915 const gfx::Point
& location
,
1916 const WebMediaPlayerAction
& action
) {
1917 source_web_contents_
->GetRenderViewHost()->
1918 ExecuteMediaPlayerActionAtLocation(location
, action
);
1921 void RenderViewContextMenu::PluginActionAt(
1922 const gfx::Point
& location
,
1923 const WebPluginAction
& action
) {
1924 source_web_contents_
->GetRenderViewHost()->
1925 ExecutePluginActionAtLocation(location
, action
);