1 // Copyright 2013 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 "content/shell/browser/shell_web_contents_view_delegate.h"
7 #include "base/command_line.h"
8 #include "content/public/browser/render_frame_host.h"
9 #include "content/public/browser/render_process_host.h"
10 #include "content/public/browser/render_view_host.h"
11 #include "content/public/browser/render_widget_host_view.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/common/context_menu_params.h"
14 #include "content/shell/browser/shell.h"
15 #include "content/shell/browser/shell_browser_context.h"
16 #include "content/shell/browser/shell_browser_main_parts.h"
17 #include "content/shell/browser/shell_content_browser_client.h"
18 #include "content/shell/browser/shell_devtools_frontend.h"
19 #include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
20 #include "content/shell/common/shell_switches.h"
21 #include "third_party/WebKit/public/web/WebContextMenuData.h"
23 using blink::WebContextMenuData
;
28 ShellContextMenuItemCutId
= 10001,
29 ShellContextMenuItemCopyId
,
30 ShellContextMenuItemPasteId
,
31 ShellContextMenuItemDeleteId
,
32 ShellContextMenuItemOpenLinkId
,
33 ShellContextMenuItemBackId
,
34 ShellContextMenuItemForwardId
,
35 ShellContextMenuItemReloadId
,
36 ShellContextMenuItemInspectId
39 void MakeContextMenuItem(HMENU menu
,
44 MENUITEMINFO mii
= {0};
45 mii
.cbSize
= sizeof(mii
);
46 mii
.fMask
= MIIM_FTYPE
| MIIM_ID
| MIIM_DATA
| MIIM_STRING
| MIIM_STATE
;
47 mii
.fState
= enabled
? MFS_ENABLED
: (MF_DISABLED
| MFS_GRAYED
);
48 mii
.fType
= MFT_STRING
;
50 mii
.dwTypeData
= const_cast<LPTSTR
>(text
);
52 InsertMenuItem(menu
, menu_index
, TRUE
, &mii
);
59 WebContentsViewDelegate
* CreateShellWebContentsViewDelegate(
60 WebContents
* web_contents
) {
61 return new ShellWebContentsViewDelegate(web_contents
);
64 ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
65 WebContents
* web_contents
)
66 : web_contents_(web_contents
) {
69 ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {
72 void ShellWebContentsViewDelegate::ShowContextMenu(
73 RenderFrameHost
* render_frame_host
,
74 const ContextMenuParams
& params
) {
75 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
76 switches::kRunLayoutTest
))
80 bool has_link
= !params_
.unfiltered_link_url
.is_empty();
81 bool has_selection
= !params_
.selection_text
.empty();
83 HMENU menu
= CreateMenu();
84 HMENU sub_menu
= CreatePopupMenu();
85 AppendMenu(menu
, MF_STRING
| MF_POPUP
, (UINT
)sub_menu
, L
"");
88 if (params_
.media_type
== WebContextMenuData::MediaTypeNone
&&
91 !params_
.is_editable
) {
92 MakeContextMenuItem(sub_menu
,
95 ShellContextMenuItemBackId
,
96 web_contents_
->GetController().CanGoBack());
98 MakeContextMenuItem(sub_menu
,
101 ShellContextMenuItemForwardId
,
102 web_contents_
->GetController().CanGoForward());
104 MakeContextMenuItem(sub_menu
,
107 ShellContextMenuItemReloadId
,
110 AppendMenu(sub_menu
, MF_SEPARATOR
, 0, NULL
);
115 MakeContextMenuItem(sub_menu
,
117 L
"Open in New Window",
118 ShellContextMenuItemOpenLinkId
,
120 AppendMenu(sub_menu
, MF_SEPARATOR
, 0, NULL
);
124 if (params_
.is_editable
) {
125 bool cut_enabled
= ((params_
.edit_flags
& WebContextMenuData::CanCut
) != 0);
126 MakeContextMenuItem(sub_menu
,
129 ShellContextMenuItemCutId
,
133 ((params_
.edit_flags
& WebContextMenuData::CanCopy
) != 0);
134 MakeContextMenuItem(sub_menu
,
137 ShellContextMenuItemCopyId
,
141 ((params_
.edit_flags
& WebContextMenuData::CanPaste
) != 0);
142 MakeContextMenuItem(sub_menu
,
145 ShellContextMenuItemPasteId
,
147 bool delete_enabled
=
148 ((params_
.edit_flags
& WebContextMenuData::CanDelete
) != 0);
149 MakeContextMenuItem(sub_menu
,
152 ShellContextMenuItemDeleteId
,
155 AppendMenu(sub_menu
, MF_SEPARATOR
, 0, NULL
);
157 } else if (has_selection
) {
158 MakeContextMenuItem(sub_menu
,
161 ShellContextMenuItemCopyId
,
164 AppendMenu(sub_menu
, MF_SEPARATOR
, 0, NULL
);
168 MakeContextMenuItem(sub_menu
,
171 ShellContextMenuItemInspectId
,
177 void ShellWebContentsViewDelegate::MenuItemSelected(int selection
) {
179 case ShellContextMenuItemCutId
:
180 web_contents_
->Cut();
182 case ShellContextMenuItemCopyId
:
183 web_contents_
->Copy();
185 case ShellContextMenuItemPasteId
:
186 web_contents_
->Paste();
188 case ShellContextMenuItemDeleteId
:
189 web_contents_
->Delete();
191 case ShellContextMenuItemOpenLinkId
: {
192 ShellBrowserContext
* browser_context
=
193 ShellContentBrowserClient::Get()->browser_context();
194 Shell::CreateNewWindow(browser_context
,
200 case ShellContextMenuItemBackId
:
201 web_contents_
->GetController().GoToOffset(-1);
202 web_contents_
->Focus();
204 case ShellContextMenuItemForwardId
:
205 web_contents_
->GetController().GoToOffset(1);
206 web_contents_
->Focus();
208 case ShellContextMenuItemReloadId
:
209 web_contents_
->GetController().Reload(false);
210 web_contents_
->Focus();
212 case ShellContextMenuItemInspectId
: {
213 ShellDevToolsFrontend::Show(web_contents_
);
219 } // namespace content