OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/renderer_context_menu/context_menu_content_type.h" | 5 #include "chrome/browser/renderer_context_menu/context_menu_content_type.h" |
6 | 6 |
7 #include "chrome/app/chrome_command_ids.h" | 7 #include "chrome/app/chrome_command_ids.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/profiles/profile_io_data.h" | 9 #include "chrome/browser/profiles/profile_io_data.h" |
10 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 using content::WebContents; | 21 using content::WebContents; |
22 using extensions::Extension; | 22 using extensions::Extension; |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 bool IsDevToolsURL(const GURL& url) { | 26 bool IsDevToolsURL(const GURL& url) { |
27 return url.SchemeIs(content::kChromeDevToolsScheme); | 27 return url.SchemeIs(content::kChromeDevToolsScheme); |
28 } | 28 } |
29 | 29 |
30 bool IsInternalResourcesURL(const GURL& url) { | 30 bool IsInternalResourcesURL(const GURL& url) { |
31 if (!url.SchemeIs(content::kChromeUIScheme)) | 31 return url.SchemeIs(content::kChromeUIScheme) && |
32 return false; | 32 (url.host() == chrome::kChromeUISyncResourcesHost); |
33 return url.host() == chrome::kChromeUISyncResourcesHost; | |
34 } | 33 } |
35 | 34 |
36 } // namespace | 35 } // namespace |
37 | 36 |
38 ContextMenuContentType::ContextMenuContentType( | 37 ContextMenuContentType::ContextMenuContentType( |
39 RenderFrameHost* render_frame_host, | 38 RenderFrameHost* render_frame_host, |
40 const content::ContextMenuParams& params, | 39 const content::ContextMenuParams& params, |
41 bool supports_custom_items) | 40 bool supports_custom_items) |
42 : params_(params), | 41 : params_(params), |
43 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)), | 42 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host)), |
44 profile_(Profile::FromBrowserContext( | 43 profile_(Profile::FromBrowserContext( |
45 source_web_contents_->GetBrowserContext())), | 44 source_web_contents_->GetBrowserContext())), |
46 supports_custom_items_(supports_custom_items), | 45 supports_custom_items_(supports_custom_items) { |
47 has_custom_items_(false) { | |
48 Initialize(); | |
49 } | 46 } |
50 | 47 |
51 ContextMenuContentType::~ContextMenuContentType() { | 48 ContextMenuContentType::~ContextMenuContentType() { |
52 } | 49 } |
53 | 50 |
54 void ContextMenuContentType::Initialize() { | |
55 // Save it in |has_custom_items_| so we don't have to repeatedly call | |
56 // HasCustomItems(). | |
57 has_custom_items_ = supports_custom_items_ && | |
58 HasCustomItems(params_.custom_items); | |
59 } | |
60 | |
61 const Extension* ContextMenuContentType::GetExtension() const { | 51 const Extension* ContextMenuContentType::GetExtension() const { |
62 extensions::ExtensionSystem* system = | 52 extensions::ExtensionSystem* system = |
63 extensions::ExtensionSystem::Get(profile_); | 53 extensions::ExtensionSystem::Get(profile_); |
64 // There is no process manager in some tests. | 54 // There is no process manager in some tests. |
65 if (!system->process_manager()) | 55 if (!system->process_manager()) |
66 return NULL; | 56 return NULL; |
67 | 57 |
68 return system->process_manager()->GetExtensionForRenderViewHost( | 58 return system->process_manager()->GetExtensionForRenderViewHost( |
69 source_web_contents_->GetRenderViewHost()); | 59 source_web_contents_->GetRenderViewHost()); |
70 } | 60 } |
71 | 61 |
72 bool ContextMenuContentType::HasCustomItems( | |
73 const std::vector<content::MenuItem>& items) const { | |
74 for (size_t i = 0; i < items.size(); ++i) { | |
75 if (IDC_CONTENT_CONTEXT_CUSTOM_FIRST + items[i].action >= | |
76 IDC_CONTENT_CONTEXT_CUSTOM_LAST) { | |
77 return false; | |
78 } | |
79 return true; | |
80 } | |
81 return false; | |
82 } | |
83 | |
84 bool ContextMenuContentType::SupportsGroup(int group) { | 62 bool ContextMenuContentType::SupportsGroup(int group) { |
85 const bool has_selection = !params_.selection_text.empty(); | 63 const bool has_selection = !params_.selection_text.empty(); |
86 | 64 |
87 if (has_custom_items_) { | 65 if (supports_custom_items_ && !params_.custom_items.empty()) { |
88 if (group == ITEM_GROUP_CUSTOM) | 66 if (group == ITEM_GROUP_CUSTOM) |
89 return true; | 67 return true; |
90 | 68 |
91 if (!has_selection) { | 69 if (!has_selection) { |
92 // For menus with custom items, if there is no selection, we do not | 70 // For menus with custom items, if there is no selection, we do not |
93 // add items other than developer items. And for Pepper menu, don't even | 71 // add items other than developer items. And for Pepper menu, don't even |
94 // add developer items. | 72 // add developer items. |
95 if (!params_.custom_context.is_pepper_menu) | 73 if (!params_.custom_context.is_pepper_menu) |
96 return group == ITEM_GROUP_DEVELOPER; | 74 return group == ITEM_GROUP_DEVELOPER; |
97 | 75 |
98 return false; | 76 return false; |
99 } | 77 } |
100 | 78 |
101 // If there's a selection when there are custom items, fall through to | 79 // If there's a selection when there are custom items, fall through to |
102 // adding the normal ones after the custom ones. | 80 // adding the normal ones after the custom ones. |
103 } | 81 } |
104 | 82 |
105 return SupportsGroupInternal(group); | 83 return SupportsGroupInternal(group); |
106 } | 84 } |
107 | 85 |
108 bool ContextMenuContentType::SupportsGroupInternal(int group) { | 86 bool ContextMenuContentType::SupportsGroupInternal(int group) { |
109 const bool has_link = !params_.unfiltered_link_url.is_empty(); | 87 const bool has_link = !params_.unfiltered_link_url.is_empty(); |
110 const bool has_selection = !params_.selection_text.empty(); | 88 const bool has_selection = !params_.selection_text.empty(); |
111 | 89 |
112 switch (group) { | 90 switch (group) { |
113 case ITEM_GROUP_CUSTOM: | 91 case ITEM_GROUP_CUSTOM: |
114 return has_custom_items_; | 92 return supports_custom_items_ && !params_.custom_items.empty(); |
| 93 |
115 case ITEM_GROUP_PAGE: { | 94 case ITEM_GROUP_PAGE: { |
116 bool is_candidate = | 95 bool is_candidate = |
117 params_.media_type == WebContextMenuData::MediaTypeNone && | 96 params_.media_type == WebContextMenuData::MediaTypeNone && |
118 !has_link && !params_.is_editable && !has_selection; | 97 !has_link && !params_.is_editable && !has_selection; |
119 | 98 |
120 if (!is_candidate && params_.page_url.is_empty()) | 99 if (!is_candidate && params_.page_url.is_empty()) |
121 DCHECK(params_.frame_url.is_empty()); | 100 DCHECK(params_.frame_url.is_empty()); |
122 | 101 |
123 return is_candidate && !params_.page_url.is_empty() && | 102 return is_candidate && !params_.page_url.is_empty() && |
124 !IsDevToolsURL(params_.page_url) && | 103 !IsDevToolsURL(params_.page_url) && |
125 !IsInternalResourcesURL(params_.page_url); | 104 !IsInternalResourcesURL(params_.page_url); |
126 } | 105 } |
| 106 |
127 case ITEM_GROUP_FRAME: { | 107 case ITEM_GROUP_FRAME: { |
| 108 |
128 bool page_group_supported = SupportsGroupInternal(ITEM_GROUP_PAGE); | 109 bool page_group_supported = SupportsGroupInternal(ITEM_GROUP_PAGE); |
129 return page_group_supported && !params_.frame_url.is_empty() && | 110 return page_group_supported && !params_.frame_url.is_empty() && |
130 !IsDevToolsURL(params_.frame_url) && | 111 !IsDevToolsURL(params_.frame_url) && |
131 !IsInternalResourcesURL(params_.page_url); | 112 !IsInternalResourcesURL(params_.page_url); |
132 } | 113 } |
| 114 |
133 case ITEM_GROUP_LINK: | 115 case ITEM_GROUP_LINK: |
134 return has_link; | 116 return has_link; |
| 117 |
135 case ITEM_GROUP_MEDIA_IMAGE: | 118 case ITEM_GROUP_MEDIA_IMAGE: |
136 return params_.media_type == WebContextMenuData::MediaTypeImage; | 119 return params_.media_type == WebContextMenuData::MediaTypeImage; |
| 120 |
137 case ITEM_GROUP_SEARCHWEBFORIMAGE: | 121 case ITEM_GROUP_SEARCHWEBFORIMAGE: |
138 // Image menu items imply search web for image item. | 122 // Image menu items imply search web for image item. |
139 return SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE); | 123 return SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE); |
| 124 |
140 case ITEM_GROUP_MEDIA_VIDEO: | 125 case ITEM_GROUP_MEDIA_VIDEO: |
141 return params_.media_type == WebContextMenuData::MediaTypeVideo; | 126 return params_.media_type == WebContextMenuData::MediaTypeVideo; |
| 127 |
142 case ITEM_GROUP_MEDIA_AUDIO: | 128 case ITEM_GROUP_MEDIA_AUDIO: |
143 return params_.media_type == WebContextMenuData::MediaTypeAudio; | 129 return params_.media_type == WebContextMenuData::MediaTypeAudio; |
| 130 |
144 case ITEM_GROUP_MEDIA_PLUGIN: | 131 case ITEM_GROUP_MEDIA_PLUGIN: |
145 return params_.media_type == WebContextMenuData::MediaTypePlugin; | 132 return params_.media_type == WebContextMenuData::MediaTypePlugin; |
| 133 |
146 case ITEM_GROUP_MEDIA_FILE: | 134 case ITEM_GROUP_MEDIA_FILE: |
147 #ifdef WEBCONTEXT_MEDIATYPEFILE_DEFINED | 135 #if defined(WEBCONTEXT_MEDIATYPEFILE_DEFINED) |
148 return params_.media_type == WebContextMenuData::MediaTypeFile; | 136 return params_.media_type == WebContextMenuData::MediaTypeFile; |
| 137 #else |
| 138 return false; |
149 #endif | 139 #endif |
150 return false; | 140 |
151 case ITEM_GROUP_EDITABLE: | 141 case ITEM_GROUP_EDITABLE: |
152 return params_.is_editable; | 142 return params_.is_editable; |
| 143 |
153 case ITEM_GROUP_COPY: | 144 case ITEM_GROUP_COPY: |
154 return !params_.is_editable && has_selection; | 145 return !params_.is_editable && has_selection; |
| 146 |
155 case ITEM_GROUP_SEARCH_PROVIDER: | 147 case ITEM_GROUP_SEARCH_PROVIDER: |
156 return has_selection; | 148 return has_selection; |
| 149 |
157 case ITEM_GROUP_PRINT: { | 150 case ITEM_GROUP_PRINT: { |
158 bool enable = has_selection && !IsDevToolsURL(params_.page_url); | 151 bool enable = has_selection && !IsDevToolsURL(params_.page_url); |
159 // Image menu items also imply print items. | 152 // Image menu items also imply print items. |
160 return enable || SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE); | 153 return enable || SupportsGroupInternal(ITEM_GROUP_MEDIA_IMAGE); |
161 } | 154 } |
| 155 |
162 case ITEM_GROUP_ALL_EXTENSION: | 156 case ITEM_GROUP_ALL_EXTENSION: |
163 return !IsDevToolsURL(params_.page_url); | 157 return !IsDevToolsURL(params_.page_url); |
| 158 |
164 case ITEM_GROUP_CURRENT_EXTENSION: | 159 case ITEM_GROUP_CURRENT_EXTENSION: |
165 return false; | 160 return false; |
| 161 |
166 case ITEM_GROUP_DEVELOPER: | 162 case ITEM_GROUP_DEVELOPER: |
167 return true; | 163 return true; |
| 164 |
168 case ITEM_GROUP_DEVTOOLS_UNPACKED_EXT: | 165 case ITEM_GROUP_DEVTOOLS_UNPACKED_EXT: |
169 return false; | 166 return false; |
| 167 |
170 case ITEM_GROUP_PRINT_PREVIEW: | 168 case ITEM_GROUP_PRINT_PREVIEW: |
171 #if defined(ENABLE_FULL_PRINTING) | 169 #if defined(ENABLE_FULL_PRINTING) |
172 return true; | 170 return true; |
173 #else | 171 #else |
174 return false; | 172 return false; |
175 #endif | 173 #endif |
| 174 |
| 175 default: |
| 176 NOTREACHED(); |
| 177 return false; |
176 } | 178 } |
177 | |
178 NOTREACHED(); | |
179 return false; | |
180 } | 179 } |
OLD | NEW |