Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / renderer_context_menu / context_menu_content_type.cc
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.
4
5 #include "components/renderer_context_menu/context_menu_content_type.h"
6
7 #include "base/bind.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"
14
15 using blink::WebContextMenuData;
16 using content::WebContents;
17 using extensions::Extension;
18
19 namespace {
20
21 bool IsDevToolsURL(const GURL& url) {
22   return url.SchemeIs(content::kChromeDevToolsScheme);
23 }
24
25 bool DefaultIsInternalResourcesURL(const GURL& url) {
26   return url.SchemeIs(content::kChromeUIScheme);
27 }
28
29 }  // namespace
30
31 ContextMenuContentType::ContextMenuContentType(
32     content::WebContents* web_contents,
33     const content::ContextMenuParams& params,
34     bool supports_custom_items)
35     : params_(params),
36       source_web_contents_(web_contents),
37       supports_custom_items_(supports_custom_items),
38       internal_resources_url_checker_(
39           base::Bind(&DefaultIsInternalResourcesURL)) {
40 }
41
42 ContextMenuContentType::~ContextMenuContentType() {
43 }
44
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())
50     return NULL;
51
52   return system->process_manager()->GetExtensionForRenderViewHost(
53       source_web_contents_->GetRenderViewHost());
54 }
55
56 bool ContextMenuContentType::SupportsGroup(int group) {
57   const bool has_selection = !params_.selection_text.empty();
58
59   if (supports_custom_items_ && !params_.custom_items.empty()) {
60     if (group == ITEM_GROUP_CUSTOM)
61       return true;
62
63     if (!has_selection) {
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;
69
70       return false;
71     }
72
73     // If there's a selection when there are custom items, fall through to
74     // adding the normal ones after the custom ones.
75   }
76
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) {
84       return false;
85     }
86   }
87
88   return SupportsGroupInternal(group);
89 }
90
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();
94
95   switch (group) {
96     case ITEM_GROUP_CUSTOM:
97       return supports_custom_items_ && !params_.custom_items.empty();
98
99     case ITEM_GROUP_PAGE: {
100       bool is_candidate =
101           params_.media_type == WebContextMenuData::MediaTypeNone &&
102           !has_link && !params_.is_editable && !has_selection;
103
104       if (!is_candidate && params_.page_url.is_empty())
105         DCHECK(params_.frame_url.is_empty());
106
107       return is_candidate && !params_.page_url.is_empty() &&
108           !IsInternalResourcesURL(params_.page_url);
109     }
110
111     case ITEM_GROUP_FRAME: {
112
113       bool page_group_supported = SupportsGroupInternal(ITEM_GROUP_PAGE);
114       return page_group_supported && !params_.frame_url.is_empty() &&
115           !IsInternalResourcesURL(params_.page_url);
116     }
117
118     case ITEM_GROUP_LINK:
119       return has_link;
120
121     case ITEM_GROUP_MEDIA_IMAGE:
122       return params_.media_type == WebContextMenuData::MediaTypeImage;
123
124     case ITEM_GROUP_SEARCHWEBFORIMAGE:
125       // Image menu items imply search web for image item.
126       return SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
127
128     case ITEM_GROUP_MEDIA_VIDEO:
129       return params_.media_type == WebContextMenuData::MediaTypeVideo;
130
131     case ITEM_GROUP_MEDIA_AUDIO:
132       return params_.media_type == WebContextMenuData::MediaTypeAudio;
133
134     case ITEM_GROUP_MEDIA_CANVAS:
135       return params_.media_type == WebContextMenuData::MediaTypeCanvas;
136
137     case ITEM_GROUP_MEDIA_PLUGIN:
138       return params_.media_type == WebContextMenuData::MediaTypePlugin;
139
140     case ITEM_GROUP_MEDIA_FILE:
141 #if defined(WEBCONTEXT_MEDIATYPEFILE_DEFINED)
142       return params_.media_type == WebContextMenuData::MediaTypeFile;
143 #else
144       return false;
145 #endif
146
147     case ITEM_GROUP_EDITABLE:
148       return params_.is_editable;
149
150     case ITEM_GROUP_COPY:
151       return !params_.is_editable && has_selection;
152
153     case ITEM_GROUP_SEARCH_PROVIDER:
154       return has_selection;
155
156     case ITEM_GROUP_PRINT: {
157       // Image menu items also imply print items.
158       return has_selection || SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
159     }
160
161     case ITEM_GROUP_ALL_EXTENSION:
162       return true;
163
164     case ITEM_GROUP_CURRENT_EXTENSION:
165       return false;
166
167     case ITEM_GROUP_DEVELOPER:
168       return true;
169
170     case ITEM_GROUP_DEVTOOLS_UNPACKED_EXT:
171       return false;
172
173     case ITEM_GROUP_PRINT_PREVIEW:
174 #if defined(ENABLE_FULL_PRINTING)
175       return true;
176 #else
177       return false;
178 #endif
179
180     default:
181       NOTREACHED();
182       return false;
183   }
184 }
185
186 bool ContextMenuContentType::IsInternalResourcesURL(const GURL& url) {
187   return internal_resources_url_checker_.Run(url);
188 }