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/render_view_context_menu_base.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "content/public/browser/render_frame_host.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/render_view_host.h"
15 #include "content/public/browser/render_widget_host_view.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/common/menu_item.h"
18 #include "third_party/WebKit/public/web/WebContextMenuData.h"
20 using blink::WebContextMenuData
;
21 using blink::WebString
;
23 using content::BrowserContext
;
24 using content::OpenURLParams
;
25 using content::RenderFrameHost
;
26 using content::RenderViewHost
;
27 using content::WebContents
;
31 // The (inclusive) range of command IDs reserved for content's custom menus.
32 int content_context_custom_first
= -1;
33 int content_context_custom_last
= -1;
35 bool IsCustomItemEnabledInternal(const std::vector
<content::MenuItem
>& items
,
37 DCHECK(RenderViewContextMenuBase::IsContentCustomCommandId(id
));
38 for (size_t i
= 0; i
< items
.size(); ++i
) {
39 int action_id
= RenderViewContextMenuBase::ConvertToContentCustomCommandId(
42 return items
[i
].enabled
;
43 if (items
[i
].type
== content::MenuItem::SUBMENU
) {
44 if (IsCustomItemEnabledInternal(items
[i
].submenu
, id
))
51 bool IsCustomItemCheckedInternal(const std::vector
<content::MenuItem
>& items
,
53 DCHECK(RenderViewContextMenuBase::IsContentCustomCommandId(id
));
54 for (size_t i
= 0; i
< items
.size(); ++i
) {
55 int action_id
= RenderViewContextMenuBase::ConvertToContentCustomCommandId(
58 return items
[i
].checked
;
59 if (items
[i
].type
== content::MenuItem::SUBMENU
) {
60 if (IsCustomItemCheckedInternal(items
[i
].submenu
, id
))
67 const size_t kMaxCustomMenuDepth
= 5;
68 const size_t kMaxCustomMenuTotalItems
= 1000;
70 void AddCustomItemsToMenu(const std::vector
<content::MenuItem
>& items
,
73 ui::SimpleMenuModel::Delegate
* delegate
,
74 ui::SimpleMenuModel
* menu_model
) {
75 if (depth
> kMaxCustomMenuDepth
) {
76 LOG(ERROR
) << "Custom menu too deeply nested.";
79 for (size_t i
= 0; i
< items
.size(); ++i
) {
80 int command_id
= RenderViewContextMenuBase::ConvertToContentCustomCommandId(
82 if (!RenderViewContextMenuBase::IsContentCustomCommandId(command_id
)) {
83 LOG(ERROR
) << "Custom menu action value out of range.";
86 if (*total_items
>= kMaxCustomMenuTotalItems
) {
87 LOG(ERROR
) << "Custom menu too large (too many items).";
91 switch (items
[i
].type
) {
92 case content::MenuItem::OPTION
:
94 RenderViewContextMenuBase::ConvertToContentCustomCommandId(
98 case content::MenuItem::CHECKABLE_OPTION
:
99 menu_model
->AddCheckItem(
100 RenderViewContextMenuBase::ConvertToContentCustomCommandId(
104 case content::MenuItem::GROUP
:
105 // TODO(viettrungluu): I don't know what this is supposed to do.
108 case content::MenuItem::SEPARATOR
:
109 menu_model
->AddSeparator(ui::NORMAL_SEPARATOR
);
111 case content::MenuItem::SUBMENU
: {
112 ui::SimpleMenuModel
* submenu
= new ui::SimpleMenuModel(delegate
);
113 AddCustomItemsToMenu(items
[i
].submenu
, depth
+ 1, total_items
, delegate
,
115 menu_model
->AddSubMenu(
116 RenderViewContextMenuBase::ConvertToContentCustomCommandId(
132 void RenderViewContextMenuBase::SetContentCustomCommandIdRange(
133 int first
, int last
) {
134 // The range is inclusive.
135 content_context_custom_first
= first
;
136 content_context_custom_last
= last
;
140 const size_t RenderViewContextMenuBase::kMaxSelectionTextLength
= 50;
143 int RenderViewContextMenuBase::ConvertToContentCustomCommandId(int id
) {
144 return content_context_custom_first
+ id
;
148 bool RenderViewContextMenuBase::IsContentCustomCommandId(int id
) {
149 return id
>= content_context_custom_first
&&
150 id
<= content_context_custom_last
;
153 RenderViewContextMenuBase::RenderViewContextMenuBase(
154 content::RenderFrameHost
* render_frame_host
,
155 const content::ContextMenuParams
& params
)
157 source_web_contents_(WebContents::FromRenderFrameHost(render_frame_host
)),
158 browser_context_(source_web_contents_
->GetBrowserContext()),
160 render_frame_id_(render_frame_host
->GetRoutingID()),
161 command_executed_(false),
162 render_process_id_(render_frame_host
->GetProcess()->GetID()) {
165 RenderViewContextMenuBase::~RenderViewContextMenuBase() {
168 // Menu construction functions -------------------------------------------------
170 void RenderViewContextMenuBase::Init() {
171 // Command id range must have been already initializerd.
172 DCHECK_NE(-1, content_context_custom_first
);
173 DCHECK_NE(-1, content_context_custom_last
);
176 if (toolkit_delegate_
)
177 toolkit_delegate_
->Init(&menu_model_
);
180 void RenderViewContextMenuBase::Cancel() {
181 if (toolkit_delegate_
)
182 toolkit_delegate_
->Cancel();
185 void RenderViewContextMenuBase::InitMenu() {
186 if (content_type_
->SupportsGroup(ContextMenuContentType::ITEM_GROUP_CUSTOM
)) {
189 const bool has_selection
= !params_
.selection_text
.empty();
191 // We will add more items if there's a selection, so add a separator.
192 // TODO(lazyboy): Clean up separator logic.
193 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
198 void RenderViewContextMenuBase::AddMenuItem(int command_id
,
199 const base::string16
& title
) {
200 menu_model_
.AddItem(command_id
, title
);
203 void RenderViewContextMenuBase::AddCheckItem(int command_id
,
204 const base::string16
& title
) {
205 menu_model_
.AddCheckItem(command_id
, title
);
208 void RenderViewContextMenuBase::AddSeparator() {
209 menu_model_
.AddSeparator(ui::NORMAL_SEPARATOR
);
212 void RenderViewContextMenuBase::AddSubMenu(int command_id
,
213 const base::string16
& label
,
214 ui::MenuModel
* model
) {
215 menu_model_
.AddSubMenu(command_id
, label
, model
);
218 void RenderViewContextMenuBase::UpdateMenuItem(int command_id
,
221 const base::string16
& label
) {
222 if (toolkit_delegate_
) {
223 toolkit_delegate_
->UpdateMenuItem(command_id
,
230 RenderViewHost
* RenderViewContextMenuBase::GetRenderViewHost() const {
231 return source_web_contents_
->GetRenderViewHost();
234 WebContents
* RenderViewContextMenuBase::GetWebContents() const {
235 return source_web_contents_
;
238 BrowserContext
* RenderViewContextMenuBase::GetBrowserContext() const {
239 return browser_context_
;
242 bool RenderViewContextMenuBase::AppendCustomItems() {
243 size_t total_items
= 0;
244 AddCustomItemsToMenu(params_
.custom_items
, 0, &total_items
, this,
246 return total_items
> 0;
249 bool RenderViewContextMenuBase::IsCommandIdKnown(
251 bool* enabled
) const {
252 // If this command is is added by one of our observers, we dispatch
253 // it to the observer.
254 base::ObserverListBase
<RenderViewContextMenuObserver
>::Iterator
it(
256 RenderViewContextMenuObserver
* observer
;
257 while ((observer
= it
.GetNext()) != NULL
) {
258 if (observer
->IsCommandIdSupported(id
)) {
259 *enabled
= observer
->IsCommandIdEnabled(id
);
265 if (IsContentCustomCommandId(id
)) {
266 *enabled
= IsCustomItemEnabled(id
);
273 // Menu delegate functions -----------------------------------------------------
275 bool RenderViewContextMenuBase::IsCommandIdChecked(int id
) const {
276 // If this command is is added by one of our observers, we dispatch it to the
278 base::ObserverListBase
<RenderViewContextMenuObserver
>::Iterator
it(
280 RenderViewContextMenuObserver
* observer
;
281 while ((observer
= it
.GetNext()) != NULL
) {
282 if (observer
->IsCommandIdSupported(id
))
283 return observer
->IsCommandIdChecked(id
);
287 if (IsContentCustomCommandId(id
))
288 return IsCustomItemChecked(id
);
293 void RenderViewContextMenuBase::ExecuteCommand(int id
, int event_flags
) {
294 command_executed_
= true;
297 // If this command is is added by one of our observers, we dispatch
298 // it to the observer.
299 base::ObserverListBase
<RenderViewContextMenuObserver
>::Iterator
it(
301 RenderViewContextMenuObserver
* observer
;
302 while ((observer
= it
.GetNext()) != NULL
) {
303 if (observer
->IsCommandIdSupported(id
))
304 return observer
->ExecuteCommand(id
);
307 // Process custom actions range.
308 if (IsContentCustomCommandId(id
)) {
309 unsigned action
= id
- content_context_custom_first
;
310 const content::CustomContextMenuContext
& context
= params_
.custom_context
;
311 #if defined(ENABLE_PLUGINS)
312 if (context
.request_id
&& !context
.is_pepper_menu
)
313 HandleAuthorizeAllPlugins();
315 source_web_contents_
->ExecuteCustomContextMenuCommand(action
, context
);
318 command_executed_
= false;
321 void RenderViewContextMenuBase::MenuWillShow(ui::SimpleMenuModel
* source
) {
322 for (int i
= 0; i
< source
->GetItemCount(); ++i
) {
323 if (source
->IsVisibleAt(i
) &&
324 source
->GetTypeAt(i
) != ui::MenuModel::TYPE_SEPARATOR
) {
325 RecordShownItem(source
->GetCommandIdAt(i
));
329 // Ignore notifications from submenus.
330 if (source
!= &menu_model_
)
333 content::RenderWidgetHostView
* view
=
334 source_web_contents_
->GetRenderWidgetHostView();
336 view
->SetShowingContextMenu(true);
341 void RenderViewContextMenuBase::MenuClosed(ui::SimpleMenuModel
* source
) {
342 // Ignore notifications from submenus.
343 if (source
!= &menu_model_
)
346 content::RenderWidgetHostView
* view
=
347 source_web_contents_
->GetRenderWidgetHostView();
349 view
->SetShowingContextMenu(false);
350 source_web_contents_
->NotifyContextMenuClosed(params_
.custom_context
);
352 if (!command_executed_
) {
353 FOR_EACH_OBSERVER(RenderViewContextMenuObserver
,
359 RenderFrameHost
* RenderViewContextMenuBase::GetRenderFrameHost() {
360 return RenderFrameHost::FromID(render_process_id_
, render_frame_id_
);
363 // Controller functions --------------------------------------------------------
365 void RenderViewContextMenuBase::OpenURL(
366 const GURL
& url
, const GURL
& referring_url
,
367 WindowOpenDisposition disposition
,
368 ui::PageTransition transition
) {
369 OpenURLWithExtraHeaders(url
, referring_url
, disposition
, transition
, "");
372 void RenderViewContextMenuBase::OpenURLWithExtraHeaders(
374 const GURL
& referring_url
,
375 WindowOpenDisposition disposition
,
376 ui::PageTransition transition
,
377 const std::string
& extra_headers
) {
378 content::Referrer referrer
= content::Referrer::SanitizeForRequest(
380 content::Referrer(referring_url
.GetAsReferrer(),
381 params_
.referrer_policy
));
383 if (params_
.link_url
== url
&& disposition
!= OFF_THE_RECORD
)
384 params_
.custom_context
.link_followed
= url
;
386 OpenURLParams
open_url_params(url
, referrer
, disposition
, transition
, false);
387 if (!extra_headers
.empty())
388 open_url_params
.extra_headers
= extra_headers
;
390 WebContents
* new_contents
= source_web_contents_
->OpenURL(open_url_params
);
394 NotifyURLOpened(url
, new_contents
);
397 bool RenderViewContextMenuBase::IsCustomItemChecked(int id
) const {
398 return IsCustomItemCheckedInternal(params_
.custom_items
, id
);
401 bool RenderViewContextMenuBase::IsCustomItemEnabled(int id
) const {
402 return IsCustomItemEnabledInternal(params_
.custom_items
, id
);