Update To 11.40.268.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 "third_party/WebKit/public/web/WebContextMenuData.h"
11
12 #if defined(ENABLE_EXTENSIONS)
13 #include "extensions/browser/process_manager.h"
14 #include "extensions/common/extension.h"
15 #endif
16
17 using blink::WebContextMenuData;
18 using content::WebContents;
19
20 #if defined(ENABLE_EXTENSIONS)
21 using extensions::Extension;
22 using extensions::ProcessManager;
23 #endif
24
25 namespace {
26
27 bool IsDevToolsURL(const GURL& url) {
28   return url.SchemeIs(content::kChromeDevToolsScheme);
29 }
30
31 bool DefaultIsInternalResourcesURL(const GURL& url) {
32   return url.SchemeIs(content::kChromeUIScheme);
33 }
34
35 }  // namespace
36
37 ContextMenuContentType::ContextMenuContentType(
38     content::WebContents* web_contents,
39     const content::ContextMenuParams& params,
40     bool supports_custom_items)
41     : params_(params),
42       source_web_contents_(web_contents),
43       supports_custom_items_(supports_custom_items),
44       internal_resources_url_checker_(
45           base::Bind(&DefaultIsInternalResourcesURL)) {
46 }
47
48 ContextMenuContentType::~ContextMenuContentType() {
49 }
50
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());
57 }
58 #endif
59
60 bool ContextMenuContentType::SupportsGroup(int group) {
61   const bool has_selection = !params_.selection_text.empty();
62
63   if (supports_custom_items_ && !params_.custom_items.empty()) {
64     if (group == ITEM_GROUP_CUSTOM)
65       return true;
66
67     if (!has_selection) {
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;
73
74       return false;
75     }
76
77     // If there's a selection when there are custom items, fall through to
78     // adding the normal ones after the custom ones.
79   }
80
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) {
88       return false;
89     }
90   }
91
92   return SupportsGroupInternal(group);
93 }
94
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();
98
99   switch (group) {
100     case ITEM_GROUP_CUSTOM:
101       return supports_custom_items_ && !params_.custom_items.empty();
102
103     case ITEM_GROUP_PAGE: {
104       bool is_candidate =
105           params_.media_type == WebContextMenuData::MediaTypeNone &&
106           !has_link && !params_.is_editable && !has_selection;
107
108       if (!is_candidate && params_.page_url.is_empty())
109         DCHECK(params_.frame_url.is_empty());
110
111       return is_candidate && !params_.page_url.is_empty() &&
112           !IsInternalResourcesURL(params_.page_url);
113     }
114
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);
119     }
120
121     case ITEM_GROUP_LINK:
122       return has_link;
123
124     case ITEM_GROUP_MEDIA_IMAGE:
125       return params_.media_type == WebContextMenuData::MediaTypeImage;
126
127     case ITEM_GROUP_SEARCHWEBFORIMAGE:
128       // Image menu items imply search web for image item.
129       return SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
130
131     case ITEM_GROUP_MEDIA_VIDEO:
132       return params_.media_type == WebContextMenuData::MediaTypeVideo;
133
134     case ITEM_GROUP_MEDIA_AUDIO:
135       return params_.media_type == WebContextMenuData::MediaTypeAudio;
136
137     case ITEM_GROUP_MEDIA_CANVAS:
138       return params_.media_type == WebContextMenuData::MediaTypeCanvas;
139
140     case ITEM_GROUP_MEDIA_PLUGIN:
141       return params_.media_type == WebContextMenuData::MediaTypePlugin;
142
143     case ITEM_GROUP_MEDIA_FILE:
144 #if defined(WEBCONTEXT_MEDIATYPEFILE_DEFINED)
145       return params_.media_type == WebContextMenuData::MediaTypeFile;
146 #else
147       return false;
148 #endif
149
150     case ITEM_GROUP_EDITABLE:
151       return params_.is_editable;
152
153     case ITEM_GROUP_COPY:
154       return !params_.is_editable && has_selection;
155
156     case ITEM_GROUP_SEARCH_PROVIDER:
157       return has_selection;
158
159     case ITEM_GROUP_PRINT: {
160       // Image menu items also imply print items.
161       return has_selection || SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE);
162     }
163
164     case ITEM_GROUP_ALL_EXTENSION:
165       return true;
166
167     case ITEM_GROUP_CURRENT_EXTENSION:
168       return false;
169
170     case ITEM_GROUP_DEVELOPER:
171       return true;
172
173     case ITEM_GROUP_DEVTOOLS_UNPACKED_EXT:
174       return false;
175
176     case ITEM_GROUP_PRINT_PREVIEW:
177 #if defined(ENABLE_PRINT_PREVIEW)
178       return true;
179 #else
180       return false;
181 #endif
182
183     default:
184       NOTREACHED();
185       return false;
186   }
187 }
188
189 bool ContextMenuContentType::IsInternalResourcesURL(const GURL& url) {
190   return internal_resources_url_checker_.Run(url);
191 }