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 "components/renderer_context_menu/context_menu_content_type.h"
8 #include "content/public/browser/web_contents.h"
9 #include "content/public/common/url_constants.h"
10 #include "third_party/WebKit/public/web/WebContextMenuData.h"
12 #if defined(ENABLE_EXTENSIONS)
13 #include "extensions/browser/process_manager.h"
14 #include "extensions/common/extension.h"
17 using blink::WebContextMenuData;
18 using content::WebContents;
20 #if defined(ENABLE_EXTENSIONS)
21 using extensions::Extension;
22 using extensions::ProcessManager;
27 bool IsDevToolsURL(const GURL& url) {
28 return url.SchemeIs(content::kChromeDevToolsScheme);
31 bool DefaultIsInternalResourcesURL(const GURL& url) {
32 return url.SchemeIs(content::kChromeUIScheme);
37 ContextMenuContentType::ContextMenuContentType(
38 content::WebContents* web_contents,
39 const content::ContextMenuParams& params,
40 bool supports_custom_items)
42 source_web_contents_(web_contents),
43 supports_custom_items_(supports_custom_items),
44 internal_resources_url_checker_(
45 base::Bind(&DefaultIsInternalResourcesURL)) {
48 ContextMenuContentType::~ContextMenuContentType() {
51 #if defined(ENABLE_EXTENSIONS)
52 const Extension* ContextMenuContentType::GetExtension() const {
53 ProcessManager* process_manager =
54 ProcessManager::Get(source_web_contents_->GetBrowserContext());
55 return process_manager->GetExtensionForRenderViewHost(
56 source_web_contents_->GetRenderViewHost());
60 bool ContextMenuContentType::SupportsGroup(int group) {
61 const bool has_selection = !params_.selection_text.empty();
63 if (supports_custom_items_ && !params_.custom_items.empty()) {
64 if (group == ITEM_GROUP_CUSTOM)
68 // For menus with custom items, if there is no selection, we do not
69 // add items other than developer items. And for Pepper menu, don't even
70 // add developer items.
71 if (!params_.custom_context.is_pepper_menu)
72 return group == ITEM_GROUP_DEVELOPER;
77 // If there's a selection when there are custom items, fall through to
78 // adding the normal ones after the custom ones.
81 if (IsDevToolsURL(params_.page_url)) {
82 // DevTools mostly provides custom context menu and uses
83 // only the following default options.
84 if (group != ITEM_GROUP_CUSTOM &&
85 group != ITEM_GROUP_EDITABLE &&
86 group != ITEM_GROUP_COPY &&
87 group != ITEM_GROUP_DEVELOPER) {
92 return SupportsGroupInternal(group);
95 bool ContextMenuContentType::SupportsGroupInternal(int group) {
96 const bool has_link = !params_.unfiltered_link_url.is_empty();
97 const bool has_selection = !params_.selection_text.empty();
100 case ITEM_GROUP_CUSTOM:
101 return supports_custom_items_ && !params_.custom_items.empty();
103 case ITEM_GROUP_PAGE: {
105 params_.media_type == WebContextMenuData::MediaTypeNone &&
106 !has_link && !params_.is_editable && !has_selection;
108 if (!is_candidate && params_.page_url.is_empty())
109 DCHECK(params_.frame_url.is_empty());
111 return is_candidate && !params_.page_url.is_empty() &&
112 !IsInternalResourcesURL(params_.page_url);
115 case ITEM_GROUP_FRAME: {
116 bool page_group_supported = SupportsGroupInternal(ITEM_GROUP_PAGE);
117 return page_group_supported && !params_.frame_url.is_empty() &&
118 !IsInternalResourcesURL(params_.page_url);
121 case ITEM_GROUP_LINK:
124 case ITEM_GROUP_MEDIA_IMAGE:
125 return params_.media_type == WebContextMenuData::MediaTypeImage;
127 case ITEM_GROUP_SEARCHWEBFORIMAGE:
128 // Image menu items imply search web for image item.
129 return SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
131 case ITEM_GROUP_MEDIA_VIDEO:
132 return params_.media_type == WebContextMenuData::MediaTypeVideo;
134 case ITEM_GROUP_MEDIA_AUDIO:
135 return params_.media_type == WebContextMenuData::MediaTypeAudio;
137 case ITEM_GROUP_MEDIA_CANVAS:
138 return params_.media_type == WebContextMenuData::MediaTypeCanvas;
140 case ITEM_GROUP_MEDIA_PLUGIN:
141 return params_.media_type == WebContextMenuData::MediaTypePlugin;
143 case ITEM_GROUP_MEDIA_FILE:
144 #if defined(WEBCONTEXT_MEDIATYPEFILE_DEFINED)
145 return params_.media_type == WebContextMenuData::MediaTypeFile;
150 case ITEM_GROUP_EDITABLE:
151 return params_.is_editable;
153 case ITEM_GROUP_COPY:
154 return !params_.is_editable && has_selection;
156 case ITEM_GROUP_SEARCH_PROVIDER:
157 return has_selection;
159 case ITEM_GROUP_PRINT: {
160 // Image menu items also imply print items.
161 return has_selection || SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
164 case ITEM_GROUP_ALL_EXTENSION:
167 case ITEM_GROUP_CURRENT_EXTENSION:
170 case ITEM_GROUP_DEVELOPER:
173 case ITEM_GROUP_DEVTOOLS_UNPACKED_EXT:
176 case ITEM_GROUP_PRINT_PREVIEW:
177 #if defined(ENABLE_PRINT_PREVIEW)
189 bool ContextMenuContentType::IsInternalResourcesURL(const GURL& url) {
190 return internal_resources_url_checker_.Run(url);