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_process_host.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/render_widget_host_view.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_view.h"
15 #include "content/public/common/context_menu_params.h"
16 #include "content/shell/browser/shell.h"
17 #include "content/shell/browser/shell_browser_context.h"
18 #include "content/shell/browser/shell_browser_main_parts.h"
19 #include "content/shell/browser/shell_content_browser_client.h"
20 #include "content/shell/browser/shell_devtools_frontend.h"
21 #include "content/shell/browser/shell_web_contents_view_delegate_creator.h"
22 #include "content/shell/common/shell_switches.h"
23 #include "third_party/WebKit/public/web/WebContextMenuData.h"
25 using WebKit::WebContextMenuData;
28 ShellContextMenuItemCutTag = 0,
29 ShellContextMenuItemCopyTag,
30 ShellContextMenuItemPasteTag,
31 ShellContextMenuItemDeleteTag,
32 ShellContextMenuItemOpenLinkTag,
33 ShellContextMenuItemBackTag,
34 ShellContextMenuItemForwardTag,
35 ShellContextMenuItemReloadTag,
36 ShellContextMenuItemInspectTag
39 @interface ShellContextMenuDelegate : NSObject<NSMenuDelegate> {
41 content::ShellWebContentsViewDelegate* delegate_;
45 @implementation ShellContextMenuDelegate
46 - (id)initWithDelegate:(content::ShellWebContentsViewDelegate*) delegate {
47 if ((self = [super init])) {
53 - (void)itemSelected:(id)sender {
54 NSInteger tag = [sender tag];
55 delegate_->ActionPerformed(tag);
61 NSMenuItem* MakeContextMenuItem(NSString* title,
65 ShellContextMenuDelegate* delegate) {
66 NSMenuItem* menu_item =
67 [[NSMenuItem alloc] initWithTitle:title
68 action:@selector(itemSelected:)
70 [menu_item setTarget:delegate];
71 [menu_item setTag:tag];
72 [menu_item setEnabled:enabled];
73 [menu addItem:menu_item];
82 WebContentsViewDelegate* CreateShellWebContentsViewDelegate(
83 WebContents* web_contents) {
84 return new ShellWebContentsViewDelegate(web_contents);
87 ShellWebContentsViewDelegate::ShellWebContentsViewDelegate(
88 WebContents* web_contents)
89 : web_contents_(web_contents) {
92 ShellWebContentsViewDelegate::~ShellWebContentsViewDelegate() {
95 void ShellWebContentsViewDelegate::ShowContextMenu(
96 const ContextMenuParams& params) {
97 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
101 bool has_link = !params_.unfiltered_link_url.is_empty();
102 bool has_selection = ! params_.selection_text.empty();
104 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
105 ShellContextMenuDelegate* delegate =
106 [[ShellContextMenuDelegate alloc] initWithDelegate:this];
107 [menu setDelegate:delegate];
108 [menu setAutoenablesItems:NO];
110 if (params.media_type == WebContextMenuData::MediaTypeNone &&
113 !params_.is_editable) {
114 BOOL back_menu_enabled =
115 web_contents_->GetController().CanGoBack() ? YES : NO;
116 MakeContextMenuItem(@"Back",
117 ShellContextMenuItemBackTag,
122 BOOL forward_menu_enabled =
123 web_contents_->GetController().CanGoForward() ? YES : NO;
124 MakeContextMenuItem(@"Forward",
125 ShellContextMenuItemForwardTag,
127 forward_menu_enabled,
130 MakeContextMenuItem(@"Reload",
131 ShellContextMenuItemReloadTag,
136 NSMenuItem* separator = [NSMenuItem separatorItem];
137 [menu addItem:separator];
141 MakeContextMenuItem(@"Open In New Window",
142 ShellContextMenuItemOpenLinkTag,
147 NSMenuItem* separator = [NSMenuItem separatorItem];
148 [menu addItem:separator];
151 if (params_.is_editable) {
152 BOOL cut_menu_enabled =
153 (params_.edit_flags & WebContextMenuData::CanCut) ? YES : NO;
154 MakeContextMenuItem(@"Cut",
155 ShellContextMenuItemCutTag,
160 BOOL copy_menu_enabled =
161 (params_.edit_flags & WebContextMenuData::CanCopy) ? YES : NO;
162 MakeContextMenuItem(@"Copy",
163 ShellContextMenuItemCopyTag,
168 BOOL paste_menu_enabled =
169 (params_.edit_flags & WebContextMenuData::CanPaste) ? YES : NO;
170 MakeContextMenuItem(@"Paste",
171 ShellContextMenuItemPasteTag,
176 BOOL delete_menu_enabled =
177 (params_.edit_flags & WebContextMenuData::CanDelete) ? YES : NO;
178 MakeContextMenuItem(@"Delete",
179 ShellContextMenuItemDeleteTag,
184 NSMenuItem* separator = [NSMenuItem separatorItem];
185 [menu addItem:separator];
186 } else if (has_selection) {
187 MakeContextMenuItem(@"Copy",
188 ShellContextMenuItemCopyTag,
193 NSMenuItem* separator = [NSMenuItem separatorItem];
194 [menu addItem:separator];
197 MakeContextMenuItem(@"Inspect",
198 ShellContextMenuItemInspectTag,
203 NSView* parent_view = web_contents_->GetView()->GetContentNativeView();
204 NSEvent* currentEvent = [NSApp currentEvent];
205 NSWindow* window = [parent_view window];
206 NSPoint position = [window mouseLocationOutsideOfEventStream];
207 NSTimeInterval eventTime = [currentEvent timestamp];
208 NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown
210 modifierFlags:NSRightMouseDownMask
212 windowNumber:[window windowNumber]
218 [NSMenu popUpContextMenu:menu
220 forView:parent_view];
223 void ShellWebContentsViewDelegate::ActionPerformed(int tag) {
225 case ShellContextMenuItemCutTag:
226 web_contents_->GetRenderViewHost()->Cut();
228 case ShellContextMenuItemCopyTag:
229 web_contents_->GetRenderViewHost()->Copy();
231 case ShellContextMenuItemPasteTag:
232 web_contents_->GetRenderViewHost()->Paste();
234 case ShellContextMenuItemDeleteTag:
235 web_contents_->GetRenderViewHost()->Delete();
237 case ShellContextMenuItemOpenLinkTag: {
238 ShellBrowserContext* browser_context =
239 ShellContentBrowserClient::Get()->browser_context();
240 Shell::CreateNewWindow(browser_context,
247 case ShellContextMenuItemBackTag:
248 web_contents_->GetController().GoToOffset(-1);
249 web_contents_->GetView()->Focus();
251 case ShellContextMenuItemForwardTag:
252 web_contents_->GetController().GoToOffset(1);
253 web_contents_->GetView()->Focus();
255 case ShellContextMenuItemReloadTag: {
256 web_contents_->GetController().Reload(false);
257 web_contents_->GetView()->Focus();
260 case ShellContextMenuItemInspectTag: {
261 ShellDevToolsFrontend::Show(web_contents_);
267 WebDragDestDelegate* ShellWebContentsViewDelegate::GetDragDestDelegate() {
271 NSObject<RenderWidgetHostViewMacDelegate>*
272 ShellWebContentsViewDelegate::CreateRenderWidgetHostViewDelegate(
273 content::RenderWidgetHost* render_widget_host) {
277 } // namespace content