1 // Copyright (c) 2012 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 #import "chrome/browser/ui/cocoa/tab_contents/chrome_web_contents_view_delegate_mac.h"
7 #import "chrome/browser/renderer_host/chrome_render_widget_host_view_mac_delegate.h"
8 #include "chrome/browser/ui/cocoa/renderer_context_menu/render_view_context_menu_mac.h"
9 #include "chrome/browser/ui/cocoa/tab_contents/web_drag_bookmark_handler_mac.h"
10 #include "chrome/browser/ui/tab_contents/chrome_web_contents_view_delegate.h"
11 #include "content/public/browser/render_widget_host_view.h"
12 #include "content/public/browser/web_contents.h"
14 ChromeWebContentsViewDelegateMac::ChromeWebContentsViewDelegateMac(
15 content::WebContents* web_contents)
16 : ContextMenuDelegate(web_contents),
17 bookmark_handler_(new WebDragBookmarkHandlerMac),
18 web_contents_(web_contents) {
21 ChromeWebContentsViewDelegateMac::~ChromeWebContentsViewDelegateMac() {
24 NSObject<RenderWidgetHostViewMacDelegate>*
25 ChromeWebContentsViewDelegateMac::CreateRenderWidgetHostViewDelegate(
26 content::RenderWidgetHost* render_widget_host) {
27 return [[ChromeRenderWidgetHostViewMacDelegate alloc]
28 initWithRenderWidgetHost:render_widget_host];
31 content::WebDragDestDelegate*
32 ChromeWebContentsViewDelegateMac::GetDragDestDelegate() {
33 return bookmark_handler_.get();
36 void ChromeWebContentsViewDelegateMac::ShowContextMenu(
37 content::RenderFrameHost* render_frame_host,
38 const content::ContextMenuParams& params) {
40 BuildMenu(content::WebContents::FromRenderFrameHost(render_frame_host),
44 void ChromeWebContentsViewDelegateMac::ShowMenu(
45 scoped_ptr<RenderViewContextMenu> menu) {
46 context_menu_.reset(static_cast<RenderViewContextMenuMac*>(menu.release()));
47 if (!context_menu_.get())
50 // The renderer may send the "show context menu" message multiple times, one
51 // for each right click mouse event it receives. Normally, this doesn't happen
52 // because mouse events are not forwarded once the context menu is showing.
53 // However, there's a race - the context menu may not yet be showing when
54 // the second mouse event arrives. In this case, |ShowContextMenu()| will
55 // get called multiple times - if so, don't create another context menu.
56 // TODO(asvitkine): Fix the renderer so that it doesn't do this.
57 content::RenderWidgetHostView* widget_view = GetActiveRenderWidgetHostView();
58 if (widget_view && widget_view->IsShowingContextMenu())
61 context_menu_->Show();
64 scoped_ptr<RenderViewContextMenu> ChromeWebContentsViewDelegateMac::BuildMenu(
65 content::WebContents* web_contents,
66 const content::ContextMenuParams& params) {
67 scoped_ptr<RenderViewContextMenuMac> menu;
68 content::RenderFrameHost* focused_frame = web_contents->GetFocusedFrame();
69 // If the frame tree does not have a focused frame at this point, do not
70 // bother creating RenderViewContextMenuMac.
71 // This happens if the frame has navigated to a different page before
72 // ContextMenu message was received by the current RenderFrameHost.
74 content::RenderWidgetHostView* widget_view =
75 GetActiveRenderWidgetHostView();
76 menu.reset(new RenderViewContextMenuMac(
77 focused_frame, params, widget_view->GetNativeView()));
81 return menu.PassAs<RenderViewContextMenu>();
84 content::RenderWidgetHostView*
85 ChromeWebContentsViewDelegateMac::GetActiveRenderWidgetHostView() {
86 return web_contents_->GetFullscreenRenderWidgetHostView() ?
87 web_contents_->GetFullscreenRenderWidgetHostView() :
88 web_contents_->GetRenderWidgetHostView();
93 content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
94 content::WebContents* web_contents) {
95 return new ChromeWebContentsViewDelegateMac(web_contents);