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/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 blink::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 RenderFrameHost* render_frame_host,
97 const ContextMenuParams& params) {
98 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kDumpRenderTree))
102 bool has_link = !params_.unfiltered_link_url.is_empty();
103 bool has_selection = ! params_.selection_text.empty();
105 NSMenu* menu = [[[NSMenu alloc] initWithTitle:@""] autorelease];
106 ShellContextMenuDelegate* delegate =
107 [[ShellContextMenuDelegate alloc] initWithDelegate:this];
108 [menu setDelegate:delegate];
109 [menu setAutoenablesItems:NO];
111 if (params.media_type == WebContextMenuData::MediaTypeNone &&
114 !params_.is_editable) {
115 BOOL back_menu_enabled =
116 web_contents_->GetController().CanGoBack() ? YES : NO;
117 MakeContextMenuItem(@"Back",
118 ShellContextMenuItemBackTag,
123 BOOL forward_menu_enabled =
124 web_contents_->GetController().CanGoForward() ? YES : NO;
125 MakeContextMenuItem(@"Forward",
126 ShellContextMenuItemForwardTag,
128 forward_menu_enabled,
131 MakeContextMenuItem(@"Reload",
132 ShellContextMenuItemReloadTag,
137 NSMenuItem* separator = [NSMenuItem separatorItem];
138 [menu addItem:separator];
142 MakeContextMenuItem(@"Open In New Window",
143 ShellContextMenuItemOpenLinkTag,
148 NSMenuItem* separator = [NSMenuItem separatorItem];
149 [menu addItem:separator];
152 if (params_.is_editable) {
153 BOOL cut_menu_enabled =
154 (params_.edit_flags & WebContextMenuData::CanCut) ? YES : NO;
155 MakeContextMenuItem(@"Cut",
156 ShellContextMenuItemCutTag,
161 BOOL copy_menu_enabled =
162 (params_.edit_flags & WebContextMenuData::CanCopy) ? YES : NO;
163 MakeContextMenuItem(@"Copy",
164 ShellContextMenuItemCopyTag,
169 BOOL paste_menu_enabled =
170 (params_.edit_flags & WebContextMenuData::CanPaste) ? YES : NO;
171 MakeContextMenuItem(@"Paste",
172 ShellContextMenuItemPasteTag,
177 BOOL delete_menu_enabled =
178 (params_.edit_flags & WebContextMenuData::CanDelete) ? YES : NO;
179 MakeContextMenuItem(@"Delete",
180 ShellContextMenuItemDeleteTag,
185 NSMenuItem* separator = [NSMenuItem separatorItem];
186 [menu addItem:separator];
187 } else if (has_selection) {
188 MakeContextMenuItem(@"Copy",
189 ShellContextMenuItemCopyTag,
194 NSMenuItem* separator = [NSMenuItem separatorItem];
195 [menu addItem:separator];
198 MakeContextMenuItem(@"Inspect",
199 ShellContextMenuItemInspectTag,
204 NSView* parent_view = web_contents_->GetContentNativeView();
205 NSEvent* currentEvent = [NSApp currentEvent];
206 NSWindow* window = [parent_view window];
207 NSPoint position = [window mouseLocationOutsideOfEventStream];
208 NSTimeInterval eventTime = [currentEvent timestamp];
209 NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown
211 modifierFlags:NSRightMouseDownMask
213 windowNumber:[window windowNumber]
219 [NSMenu popUpContextMenu:menu
221 forView:parent_view];
224 void ShellWebContentsViewDelegate::ActionPerformed(int tag) {
226 case ShellContextMenuItemCutTag:
227 web_contents_->Cut();
229 case ShellContextMenuItemCopyTag:
230 web_contents_->Copy();
232 case ShellContextMenuItemPasteTag:
233 web_contents_->Paste();
235 case ShellContextMenuItemDeleteTag:
236 web_contents_->Delete();
238 case ShellContextMenuItemOpenLinkTag: {
239 ShellBrowserContext* browser_context =
240 ShellContentBrowserClient::Get()->browser_context();
241 Shell::CreateNewWindow(browser_context,
248 case ShellContextMenuItemBackTag:
249 web_contents_->GetController().GoToOffset(-1);
250 web_contents_->Focus();
252 case ShellContextMenuItemForwardTag:
253 web_contents_->GetController().GoToOffset(1);
254 web_contents_->Focus();
256 case ShellContextMenuItemReloadTag: {
257 web_contents_->GetController().Reload(false);
258 web_contents_->Focus();
261 case ShellContextMenuItemInspectTag: {
262 ShellDevToolsFrontend::Show(web_contents_);
268 } // namespace content