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 "extensions/browser/extension_system.h"
11 #include "extensions/browser/process_manager.h"
12 #include "extensions/common/extension.h"
13 #include "third_party/WebKit/public/web/WebContextMenuData.h"
15 using blink::WebContextMenuData;
16 using content::WebContents;
17 using extensions::Extension;
21 bool IsDevToolsURL(const GURL& url) {
22 return url.SchemeIs(content::kChromeDevToolsScheme);
25 bool DefaultIsInternalResourcesURL(const GURL& url) {
26 return url.SchemeIs(content::kChromeUIScheme);
31 ContextMenuContentType::ContextMenuContentType(
32 content::WebContents* web_contents,
33 const content::ContextMenuParams& params,
34 bool supports_custom_items)
36 source_web_contents_(web_contents),
37 supports_custom_items_(supports_custom_items),
38 internal_resources_url_checker_(
39 base::Bind(&DefaultIsInternalResourcesURL)) {
42 ContextMenuContentType::~ContextMenuContentType() {
45 const Extension* ContextMenuContentType::GetExtension() const {
46 extensions::ExtensionSystem* system = extensions::ExtensionSystem::Get(
47 source_web_contents_->GetBrowserContext());
48 // There is no process manager in some tests.
49 if (!system->process_manager())
52 return system->process_manager()->GetExtensionForRenderViewHost(
53 source_web_contents_->GetRenderViewHost());
56 bool ContextMenuContentType::SupportsGroup(int group) {
57 const bool has_selection = !params_.selection_text.empty();
59 if (supports_custom_items_ && !params_.custom_items.empty()) {
60 if (group == ITEM_GROUP_CUSTOM)
64 // For menus with custom items, if there is no selection, we do not
65 // add items other than developer items. And for Pepper menu, don't even
66 // add developer items.
67 if (!params_.custom_context.is_pepper_menu)
68 return group == ITEM_GROUP_DEVELOPER;
73 // If there's a selection when there are custom items, fall through to
74 // adding the normal ones after the custom ones.
77 if (IsDevToolsURL(params_.page_url)) {
78 // DevTools mostly provides custom context menu and uses
79 // only the following default options.
80 if (group != ITEM_GROUP_CUSTOM &&
81 group != ITEM_GROUP_EDITABLE &&
82 group != ITEM_GROUP_COPY &&
83 group != ITEM_GROUP_DEVELOPER) {
88 return SupportsGroupInternal(group);
91 bool ContextMenuContentType::SupportsGroupInternal(int group) {
92 const bool has_link = !params_.unfiltered_link_url.is_empty();
93 const bool has_selection = !params_.selection_text.empty();
96 case ITEM_GROUP_CUSTOM:
97 return supports_custom_items_ && !params_.custom_items.empty();
99 case ITEM_GROUP_PAGE: {
101 params_.media_type == WebContextMenuData::MediaTypeNone &&
102 !has_link && !params_.is_editable && !has_selection;
104 if (!is_candidate && params_.page_url.is_empty())
105 DCHECK(params_.frame_url.is_empty());
107 return is_candidate && !params_.page_url.is_empty() &&
108 !IsInternalResourcesURL(params_.page_url);
111 case ITEM_GROUP_FRAME: {
113 bool page_group_supported = SupportsGroupInternal(ITEM_GROUP_PAGE);
114 return page_group_supported && !params_.frame_url.is_empty() &&
115 !IsInternalResourcesURL(params_.page_url);
118 case ITEM_GROUP_LINK:
121 case ITEM_GROUP_MEDIA_IMAGE:
122 return params_.media_type == WebContextMenuData::MediaTypeImage;
124 case ITEM_GROUP_SEARCHWEBFORIMAGE:
125 // Image menu items imply search web for image item.
126 return SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
128 case ITEM_GROUP_MEDIA_VIDEO:
129 return params_.media_type == WebContextMenuData::MediaTypeVideo;
131 case ITEM_GROUP_MEDIA_AUDIO:
132 return params_.media_type == WebContextMenuData::MediaTypeAudio;
134 case ITEM_GROUP_MEDIA_CANVAS:
135 return params_.media_type == WebContextMenuData::MediaTypeCanvas;
137 case ITEM_GROUP_MEDIA_PLUGIN:
138 return params_.media_type == WebContextMenuData::MediaTypePlugin;
140 case ITEM_GROUP_MEDIA_FILE:
141 #if defined(WEBCONTEXT_MEDIATYPEFILE_DEFINED)
142 return params_.media_type == WebContextMenuData::MediaTypeFile;
147 case ITEM_GROUP_EDITABLE:
148 return params_.is_editable;
150 case ITEM_GROUP_COPY:
151 return !params_.is_editable && has_selection;
153 case ITEM_GROUP_SEARCH_PROVIDER:
154 return has_selection;
156 case ITEM_GROUP_PRINT: {
157 // Image menu items also imply print items.
158 return has_selection || SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
161 case ITEM_GROUP_ALL_EXTENSION:
164 case ITEM_GROUP_CURRENT_EXTENSION:
167 case ITEM_GROUP_DEVELOPER:
170 case ITEM_GROUP_DEVTOOLS_UNPACKED_EXT:
173 case ITEM_GROUP_PRINT_PREVIEW:
174 #if defined(ENABLE_FULL_PRINTING)
186 bool ContextMenuContentType::IsInternalResourcesURL(const GURL& url) {
187 return internal_resources_url_checker_.Run(url);