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 #import <Cocoa/Cocoa.h>
9 #include "base/command_line.h"
10 #include "content/public/browser/render_frame_host.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/render_widget_host_view.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_view.h"
16 #include "content/public/common/context_menu_params.h"
17 #include "content/shell/browser/shell.h"
18 #include "content/shell/browser/shell_browser_context.h"
19 #include "content/shell/browser/shell_browser_main_parts.h"
20 #include "content/shell/browser/shell_content_browser_client.h"
21 #include "content/shell/browser/shell_devtools_frontend.h"
22 #include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
23 #include "content/shell/common/shell_switches.h"
24 #include "third_party/WebKit/public/web/WebContextMenuData.h"
26 using blink::WebContextMenuData;
29 ShellContextMenuItemCutTag = 0,
30 ShellContextMenuItemCopyTag,
31 ShellContextMenuItemPasteTag,
32 ShellContextMenuItemDeleteTag,
33 ShellContextMenuItemOpenLinkTag,
34 ShellContextMenuItemBackTag,
35 ShellContextMenuItemForwardTag,
36 ShellContextMenuItemReloadTag,
37 ShellContextMenuItemInspectTag
40 @interface ShellContextMenuDelegate : NSObject<NSMenuDelegate> {
42 content::ShellWebContentsViewDelegate* delegate_;
46 @implementation ShellContextMenuDelegate
47 - (id)initWithDelegate:(content::ShellWebContentsViewDelegate*) delegate {
48 if ((self = [super init])) {
54 - (void)itemSelected:(id)sender {
55 NSInteger tag = [sender tag];
56 delegate_->ActionPerformed(tag);
62 NSMenuItem* MakeContextMenuItem(NSString* title,
66 ShellContextMenuDelegate* delegate) {
67 NSMenuItem* menu_item =
68 [[NSMenuItem alloc] initWithTitle:title
69 action:@selector(itemSelected:)
71 [menu_item setTarget:delegate];
72 [menu_item setTag:tag];
73 [menu_item setEnabled:enabled];
74 [menu addItem:menu_item];
83 WebContentsViewDelegate* CreateShellWebContentsViewDelegate(
84 WebContents* web_contents) {
85 return new ShellWebContentsViewDelegate(web_contents);
88 ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
89 WebContents* web_contents)
90 : web_contents_(web_contents) {
93 ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {
96 void ShellWebContentsViewDelegate::ShowContextMenu(
97 RenderFrameHost* render_frame_host,
98 const ContextMenuParams& params) {
99 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
103 bool has_link = !params_.unfiltered_link_url.is_empty();
104 bool has_selection = ! params_.selection_text.empty();
106 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
107 ShellContextMenuDelegate* delegate =
108 [[ShellContextMenuDelegate alloc] initWithDelegate:this];
109 [menu setDelegate:delegate];
110 [menu setAutoenablesItems:NO];
112 if (params.media_type == WebContextMenuData::MediaTypeNone &&
115 !params_.is_editable) {
116 BOOL back_menu_enabled =
117 web_contents_->GetController().CanGoBack() ? YES : NO;
118 MakeContextMenuItem(@"Back",
119 ShellContextMenuItemBackTag,
124 BOOL forward_menu_enabled =
125 web_contents_->GetController().CanGoForward() ? YES : NO;
126 MakeContextMenuItem(@"Forward",
127 ShellContextMenuItemForwardTag,
129 forward_menu_enabled,
132 MakeContextMenuItem(@"Reload",
133 ShellContextMenuItemReloadTag,
138 NSMenuItem* separator = [NSMenuItem separatorItem];
139 [menu addItem:separator];
143 MakeContextMenuItem(@"Open In New Window",
144 ShellContextMenuItemOpenLinkTag,
149 NSMenuItem* separator = [NSMenuItem separatorItem];
150 [menu addItem:separator];
153 if (params_.is_editable) {
154 BOOL cut_menu_enabled =
155 (params_.edit_flags & WebContextMenuData::CanCut) ? YES : NO;
156 MakeContextMenuItem(@"Cut",
157 ShellContextMenuItemCutTag,
162 BOOL copy_menu_enabled =
163 (params_.edit_flags & WebContextMenuData::CanCopy) ? YES : NO;
164 MakeContextMenuItem(@"Copy",
165 ShellContextMenuItemCopyTag,
170 BOOL paste_menu_enabled =
171 (params_.edit_flags & WebContextMenuData::CanPaste) ? YES : NO;
172 MakeContextMenuItem(@"Paste",
173 ShellContextMenuItemPasteTag,
178 BOOL delete_menu_enabled =
179 (params_.edit_flags & WebContextMenuData::CanDelete) ? YES : NO;
180 MakeContextMenuItem(@"Delete",
181 ShellContextMenuItemDeleteTag,
186 NSMenuItem* separator = [NSMenuItem separatorItem];
187 [menu addItem:separator];
188 } else if (has_selection) {
189 MakeContextMenuItem(@"Copy",
190 ShellContextMenuItemCopyTag,
195 NSMenuItem* separator = [NSMenuItem separatorItem];
196 [menu addItem:separator];
199 MakeContextMenuItem(@"Inspect",
200 ShellContextMenuItemInspectTag,
205 NSView* parent_view = web_contents_->GetView()->GetContentNativeView();
206 NSEvent* currentEvent = [NSApp currentEvent];
207 NSWindow* window = [parent_view window];
208 NSPoint position = [window mouseLocationOutsideOfEventStream];
209 NSTimeInterval eventTime = [currentEvent timestamp];
210 NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown
212 modifierFlags:NSRightMouseDownMask
214 windowNumber:[window windowNumber]
220 [NSMenu popUpContextMenu:menu
222 forView:parent_view];
225 void ShellWebContentsViewDelegate::ActionPerformed(int tag) {
227 case ShellContextMenuItemCutTag:
228 web_contents_->Cut();
230 case ShellContextMenuItemCopyTag:
231 web_contents_->Copy();
233 case ShellContextMenuItemPasteTag:
234 web_contents_->Paste();
236 case ShellContextMenuItemDeleteTag:
237 web_contents_->Delete();
239 case ShellContextMenuItemOpenLinkTag: {
240 ShellBrowserContext* browser_context =
241 ShellContentBrowserClient::Get()->browser_context();
242 Shell::CreateNewWindow(browser_context,
249 case ShellContextMenuItemBackTag:
250 web_contents_->GetController().GoToOffset(-1);
251 web_contents_->GetView()->Focus();
253 case ShellContextMenuItemForwardTag:
254 web_contents_->GetController().GoToOffset(1);
255 web_contents_->GetView()->Focus();
257 case ShellContextMenuItemReloadTag: {
258 web_contents_->GetController().Reload(false);
259 web_contents_->GetView()->Focus();
262 case ShellContextMenuItemInspectTag: {
263 ShellDevToolsFrontend::Show(web_contents_);
269 WebDragDestDelegate* ShellWebContentsViewDelegate::GetDragDestDelegate() {
273 NSObject<RenderWidgetHostViewMacDelegate>*
274 ShellWebContentsViewDelegate::CreateRenderWidgetHostViewDelegate(
275 content::RenderWidgetHost* render_widget_host) {
279 } // namespace content