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 (base::CommandLine::ForCurrentProcess()->HasSwitch(
99 switches::kRunLayoutTest))
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_->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,
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