Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / content / shell / browser / shell_web_contents_view_delegate_mac.mm
blob5d8ecdd8f0e3b7e84434906fb85cc791860fa150
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;
27 enum {
28   ShellContextMenuItemCutTag = 0,
29   ShellContextMenuItemCopyTag,
30   ShellContextMenuItemPasteTag,
31   ShellContextMenuItemDeleteTag,
32   ShellContextMenuItemOpenLinkTag,
33   ShellContextMenuItemBackTag,
34   ShellContextMenuItemForwardTag,
35   ShellContextMenuItemReloadTag,
36   ShellContextMenuItemInspectTag
39 @interface ShellContextMenuDelegate : NSObject<NSMenuDelegate> {
40  @private
41   content::ShellWebContentsViewDelegate* delegate_;
43 @end
45 @implementation ShellContextMenuDelegate
46 - (id)initWithDelegate:(content::ShellWebContentsViewDelegate*) delegate {
47   if ((self = [super init])) {
48     delegate_ = delegate;
49   }
50   return self;
53 - (void)itemSelected:(id)sender {
54   NSInteger tag = [sender tag];
55   delegate_->ActionPerformed(tag);
57 @end
59 namespace {
61 NSMenuItem* MakeContextMenuItem(NSString* title,
62                                 NSInteger tag,
63                                 NSMenu* menu,
64                                 BOOL enabled,
65                                 ShellContextMenuDelegate* delegate) {
66   NSMenuItem* menu_item =
67       [[NSMenuItem alloc] initWithTitle:title
68                                  action:@selector(itemSelected:)
69                           keyEquivalent:@""];
70   [menu_item setTarget:delegate];
71   [menu_item setTag:tag];
72   [menu_item setEnabled:enabled];
73   [menu addItem:menu_item];
75   return menu_item;
78 }  // namespace
80 namespace content {
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))
100     return;
102   params_ = params;
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 &&
113       !has_link &&
114       !has_selection &&
115       !params_.is_editable) {
116     BOOL back_menu_enabled =
117         web_contents_->GetController().CanGoBack() ? YES : NO;
118     MakeContextMenuItem(@"Back",
119                         ShellContextMenuItemBackTag,
120                         menu,
121                         back_menu_enabled,
122                         delegate);
124     BOOL forward_menu_enabled =
125         web_contents_->GetController().CanGoForward() ? YES : NO;
126     MakeContextMenuItem(@"Forward",
127                         ShellContextMenuItemForwardTag,
128                         menu,
129                         forward_menu_enabled,
130                         delegate);
132     MakeContextMenuItem(@"Reload",
133                         ShellContextMenuItemReloadTag,
134                         menu,
135                         YES,
136                         delegate);
138     NSMenuItem* separator = [NSMenuItem separatorItem];
139     [menu addItem:separator];
140   }
142   if (has_link) {
143     MakeContextMenuItem(@"Open In New Window",
144                         ShellContextMenuItemOpenLinkTag,
145                         menu,
146                         YES,
147                         delegate);
149     NSMenuItem* separator = [NSMenuItem separatorItem];
150     [menu addItem:separator];
151   }
153   if (params_.is_editable) {
154     BOOL cut_menu_enabled =
155         (params_.edit_flags & WebContextMenuData::CanCut) ? YES : NO;
156     MakeContextMenuItem(@"Cut",
157                         ShellContextMenuItemCutTag,
158                         menu,
159                         cut_menu_enabled,
160                         delegate);
162     BOOL copy_menu_enabled =
163         (params_.edit_flags & WebContextMenuData::CanCopy) ? YES : NO;
164     MakeContextMenuItem(@"Copy",
165                         ShellContextMenuItemCopyTag,
166                         menu,
167                         copy_menu_enabled,
168                         delegate);
170     BOOL paste_menu_enabled =
171         (params_.edit_flags & WebContextMenuData::CanPaste) ? YES : NO;
172     MakeContextMenuItem(@"Paste",
173                         ShellContextMenuItemPasteTag,
174                         menu,
175                         paste_menu_enabled,
176                         delegate);
178     BOOL delete_menu_enabled =
179         (params_.edit_flags & WebContextMenuData::CanDelete) ? YES : NO;
180     MakeContextMenuItem(@"Delete",
181                         ShellContextMenuItemDeleteTag,
182                         menu,
183                         delete_menu_enabled,
184                         delegate);
186     NSMenuItem* separator = [NSMenuItem separatorItem];
187     [menu addItem:separator];
188   } else if (has_selection) {
189     MakeContextMenuItem(@"Copy",
190                         ShellContextMenuItemCopyTag,
191                         menu,
192                         YES,
193                         delegate);
195     NSMenuItem* separator = [NSMenuItem separatorItem];
196     [menu addItem:separator];
197   }
199   MakeContextMenuItem(@"Inspect",
200                       ShellContextMenuItemInspectTag,
201                       menu,
202                       YES,
203                       delegate);
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
211                                            location:position
212                                       modifierFlags:NSRightMouseDownMask
213                                           timestamp:eventTime
214                                        windowNumber:[window windowNumber]
215                                             context:nil
216                                         eventNumber:0
217                                          clickCount:1
218                                            pressure:1.0];
220   [NSMenu popUpContextMenu:menu
221                  withEvent:clickEvent
222                    forView:parent_view];
225 void ShellWebContentsViewDelegate::ActionPerformed(int tag) {
226   switch (tag) {
227     case ShellContextMenuItemCutTag:
228       web_contents_->Cut();
229       break;
230     case ShellContextMenuItemCopyTag:
231       web_contents_->Copy();
232       break;
233     case ShellContextMenuItemPasteTag:
234       web_contents_->Paste();
235       break;
236     case ShellContextMenuItemDeleteTag:
237       web_contents_->Delete();
238       break;
239     case ShellContextMenuItemOpenLinkTag: {
240       ShellBrowserContext* browser_context =
241           ShellContentBrowserClient::Get()->browser_context();
242       Shell::CreateNewWindow(browser_context,
243                              params_.link_url,
244                              NULL,
245                              gfx::Size());
246       break;
247     }
248     case ShellContextMenuItemBackTag:
249       web_contents_->GetController().GoToOffset(-1);
250       web_contents_->Focus();
251       break;
252     case ShellContextMenuItemForwardTag:
253       web_contents_->GetController().GoToOffset(1);
254       web_contents_->Focus();
255       break;
256     case ShellContextMenuItemReloadTag: {
257       web_contents_->GetController().Reload(false);
258       web_contents_->Focus();
259       break;
260     }
261     case ShellContextMenuItemInspectTag: {
262       ShellDevToolsFrontend::Show(web_contents_);
263       break;
264     }
265   }
268 }  // namespace content