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 (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree
))
79 bool has_link
= !params_
.unfiltered_link_url
.is_empty();
80 bool has_selection
= !params_
.selection_text
.empty();
82 HMENU menu
= CreateMenu();
83 HMENU sub_menu
= CreatePopupMenu();
84 AppendMenu(menu
, MF_STRING
| MF_POPUP
, (UINT
)sub_menu
, L
"");
87 if (params_
.media_type
== WebContextMenuData::MediaTypeNone
&&
90 !params_
.is_editable
) {
91 MakeContextMenuItem(sub_menu
,
94 ShellContextMenuItemBackId
,
95 web_contents_
->GetController().CanGoBack());
97 MakeContextMenuItem(sub_menu
,
100 ShellContextMenuItemForwardId
,
101 web_contents_
->GetController().CanGoForward());
103 MakeContextMenuItem(sub_menu
,
106 ShellContextMenuItemReloadId
,
109 AppendMenu(sub_menu
, MF_SEPARATOR
, 0, NULL
);
114 MakeContextMenuItem(sub_menu
,
116 L
"Open in New Window",
117 ShellContextMenuItemOpenLinkId
,
119 AppendMenu(sub_menu
, MF_SEPARATOR
, 0, NULL
);
123 if (params_
.is_editable
) {
124 bool cut_enabled
= ((params_
.edit_flags
& WebContextMenuData::CanCut
) != 0);
125 MakeContextMenuItem(sub_menu
,
128 ShellContextMenuItemCutId
,
132 ((params_
.edit_flags
& WebContextMenuData::CanCopy
) != 0);
133 MakeContextMenuItem(sub_menu
,
136 ShellContextMenuItemCopyId
,
140 ((params_
.edit_flags
& WebContextMenuData::CanPaste
) != 0);
141 MakeContextMenuItem(sub_menu
,
144 ShellContextMenuItemPasteId
,
146 bool delete_enabled
=
147 ((params_
.edit_flags
& WebContextMenuData::CanDelete
) != 0);
148 MakeContextMenuItem(sub_menu
,
151 ShellContextMenuItemDeleteId
,
154 AppendMenu(sub_menu
, MF_SEPARATOR
, 0, NULL
);
156 } else if (has_selection
) {
157 MakeContextMenuItem(sub_menu
,
160 ShellContextMenuItemCopyId
,
163 AppendMenu(sub_menu
, MF_SEPARATOR
, 0, NULL
);
167 MakeContextMenuItem(sub_menu
,
170 ShellContextMenuItemInspectId
,
172 #if defined(USE_AURA)
175 gfx::Point
screen_point(params
.x
, params
.y
);
176 POINT point
= screen_point
.ToPOINT();
177 ClientToScreen(web_contents_
->GetNativeView(), &point
);
180 TrackPopupMenu(sub_menu
,
181 TPM_LEFTALIGN
| TPM_RIGHTBUTTON
| TPM_RETURNCMD
,
184 web_contents_
->GetContentNativeView(),
187 MenuItemSelected(selection
);
192 void ShellWebContentsViewDelegate::MenuItemSelected(int selection
) {
194 case ShellContextMenuItemCutId
:
195 web_contents_
->Cut();
197 case ShellContextMenuItemCopyId
:
198 web_contents_
->Copy();
200 case ShellContextMenuItemPasteId
:
201 web_contents_
->Paste();
203 case ShellContextMenuItemDeleteId
:
204 web_contents_
->Delete();
206 case ShellContextMenuItemOpenLinkId
: {
207 ShellBrowserContext
* browser_context
=
208 ShellContentBrowserClient::Get()->browser_context();
209 Shell::CreateNewWindow(browser_context
,
216 case ShellContextMenuItemBackId
:
217 web_contents_
->GetController().GoToOffset(-1);
218 web_contents_
->Focus();
220 case ShellContextMenuItemForwardId
:
221 web_contents_
->GetController().GoToOffset(1);
222 web_contents_
->Focus();
224 case ShellContextMenuItemReloadId
:
225 web_contents_
->GetController().Reload(false);
226 web_contents_
->Focus();
228 case ShellContextMenuItemInspectId
: {
229 ShellDevToolsFrontend::Show(web_contents_
);
235 WebDragDestDelegate
* ShellWebContentsViewDelegate::GetDragDestDelegate() {
239 void ShellWebContentsViewDelegate::StoreFocus() {
242 void ShellWebContentsViewDelegate::RestoreFocus() {
245 bool ShellWebContentsViewDelegate::Focus() {
249 void ShellWebContentsViewDelegate::TakeFocus(bool reverse
) {
252 void ShellWebContentsViewDelegate::SizeChanged(const gfx::Size
& size
) {
255 } // namespace content