Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / tab_contents / chrome_web_contents_view_delegate_mac.mm
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.
4
5 #import "chrome/browser/ui/cocoa/tab_contents/chrome_web_contents_view_delegate_mac.h"
6
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"
13
14 ChromeWebContentsViewDelegateMac::ChromeWebContentsViewDelegateMac(
15     content::WebContents* web_contents)
16     : ContextMenuDelegate(web_contents),
17       bookmark_handler_(new WebDragBookmarkHandlerMac),
18       web_contents_(web_contents) {
19 }
20
21 ChromeWebContentsViewDelegateMac::~ChromeWebContentsViewDelegateMac() {
22 }
23
24 NSObject<RenderWidgetHostViewMacDelegate>*
25 ChromeWebContentsViewDelegateMac::CreateRenderWidgetHostViewDelegate(
26     content::RenderWidgetHost* render_widget_host) {
27   return [[ChromeRenderWidgetHostViewMacDelegate alloc]
28       initWithRenderWidgetHost:render_widget_host];
29 }
30
31 content::WebDragDestDelegate*
32     ChromeWebContentsViewDelegateMac::GetDragDestDelegate() {
33   return bookmark_handler_.get();
34 }
35
36 void ChromeWebContentsViewDelegateMac::ShowContextMenu(
37     content::RenderFrameHost* render_frame_host,
38     const content::ContextMenuParams& params) {
39   ShowMenu(
40       BuildMenu(content::WebContents::FromRenderFrameHost(render_frame_host),
41                 params));
42 }
43
44 void ChromeWebContentsViewDelegateMac::ShowMenu(
45     scoped_ptr<RenderViewContextMenu> menu) {
46   context_menu_.reset(static_cast<RenderViewContextMenuMac*>(menu.release()));
47   if (!context_menu_.get())
48     return;
49
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())
59     return;
60
61   context_menu_->Show();
62 }
63
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.
73   if (focused_frame) {
74     content::RenderWidgetHostView* widget_view =
75         GetActiveRenderWidgetHostView();
76     menu.reset(new RenderViewContextMenuMac(
77         focused_frame, params, widget_view->GetNativeView()));
78     menu->Init();
79   }
80
81   return menu.PassAs<RenderViewContextMenu>();
82 }
83
84 content::RenderWidgetHostView*
85 ChromeWebContentsViewDelegateMac::GetActiveRenderWidgetHostView() {
86   return web_contents_->GetFullscreenRenderWidgetHostView() ?
87       web_contents_->GetFullscreenRenderWidgetHostView() :
88       web_contents_->GetRenderWidgetHostView();
89 }
90
91 namespace chrome {
92
93 content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
94     content::WebContents* web_contents) {
95   return new ChromeWebContentsViewDelegateMac(web_contents);
96 }
97
98 }  // namespace chrome