Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / athena / content / web_contents_view_delegate_factory_impl.cc
blob8eecd2659913341755fbfcaa7063002b5d68c53c
1 // Copyright 2014 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 "athena/content/public/web_contents_view_delegate_creator.h"
7 #include "athena/content/render_view_context_menu_impl.h"
8 #include "components/web_modal/popup_manager.h"
9 #include "components/web_modal/single_web_contents_dialog_manager.h"
10 #include "components/web_modal/web_contents_modal_dialog_host.h"
11 #include "components/web_modal/web_contents_modal_dialog_manager.h"
12 #include "content/public/browser/render_widget_host_view.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_delegate.h"
15 #include "content/public/browser/web_contents_view_delegate.h"
16 #include "ui/aura/client/screen_position_client.h"
17 #include "ui/aura/window.h"
18 #include "ui/views/widget/widget.h"
20 namespace athena {
21 namespace {
23 class WebContentsViewDelegateImpl : public content::WebContentsViewDelegate {
24 public:
25 explicit WebContentsViewDelegateImpl(content::WebContents* web_contents)
26 : web_contents_(web_contents) {}
27 virtual ~WebContentsViewDelegateImpl() {}
29 virtual content::WebDragDestDelegate* GetDragDestDelegate() OVERRIDE {
30 // TODO(oshima): crbug.com/401610
31 return NULL;
34 virtual bool Focus() OVERRIDE {
35 web_modal::PopupManager* popup_manager =
36 web_modal::PopupManager::FromWebContents(web_contents_);
37 if (popup_manager)
38 popup_manager->WasFocused(web_contents_);
39 return false;
42 virtual void ShowContextMenu(
43 content::RenderFrameHost* render_frame_host,
44 const content::ContextMenuParams& params) OVERRIDE {
45 ShowMenu(BuildMenu(
46 content::WebContents::FromRenderFrameHost(render_frame_host), params));
49 virtual void SizeChanged(const gfx::Size& size) OVERRIDE {
50 // TODO(oshima|sadrul): Implement this when sad_tab is componentized.
51 // See c/b/ui/views/tab_contents/chrome_web_contents_view_delegate_views.cc
54 scoped_ptr<RenderViewContextMenuImpl> BuildMenu(
55 content::WebContents* web_contents,
56 const content::ContextMenuParams& params) {
57 scoped_ptr<RenderViewContextMenuImpl> menu;
58 content::RenderFrameHost* focused_frame = web_contents->GetFocusedFrame();
59 // If the frame tree does not have a focused frame at this point, do not
60 // bother creating RenderViewContextMenuViews.
61 // This happens if the frame has navigated to a different page before
62 // ContextMenu message was received by the current RenderFrameHost.
63 if (focused_frame) {
64 menu.reset(new RenderViewContextMenuImpl(focused_frame, params));
65 menu->Init();
67 return menu.Pass();
70 void ShowMenu(scoped_ptr<RenderViewContextMenuImpl> menu) {
71 context_menu_.reset(menu.release());
73 if (!context_menu_.get())
74 return;
76 // Menus need a Widget to work. If we're not the active tab we won't
77 // necessarily be in a widget.
78 views::Widget* top_level_widget = GetTopLevelWidget();
79 if (!top_level_widget)
80 return;
82 const content::ContextMenuParams& params = context_menu_->params();
83 // Don't show empty menus.
84 if (context_menu_->menu_model().GetItemCount() == 0)
85 return;
87 gfx::Point screen_point(params.x, params.y);
89 // Convert from target window coordinates to root window coordinates.
90 aura::Window* target_window = GetActiveNativeView();
91 aura::Window* root_window = target_window->GetRootWindow();
92 aura::client::ScreenPositionClient* screen_position_client =
93 aura::client::GetScreenPositionClient(root_window);
94 if (screen_position_client) {
95 screen_position_client->ConvertPointToScreen(target_window,
96 &screen_point);
98 // Enable recursive tasks on the message loop so we can get updates while
99 // the context menu is being displayed.
100 base::MessageLoop::ScopedNestableTaskAllower allow(
101 base::MessageLoop::current());
102 context_menu_->RunMenuAt(
103 top_level_widget, screen_point, params.source_type);
106 aura::Window* GetActiveNativeView() {
107 return web_contents_->GetFullscreenRenderWidgetHostView()
108 ? web_contents_->GetFullscreenRenderWidgetHostView()
109 ->GetNativeView()
110 : web_contents_->GetNativeView();
113 views::Widget* GetTopLevelWidget() {
114 return views::Widget::GetTopLevelWidgetForNativeView(GetActiveNativeView());
117 views::FocusManager* GetFocusManager() {
118 views::Widget* toplevel_widget = GetTopLevelWidget();
119 return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL;
122 void SetInitialFocus() {
123 if (web_contents_->FocusLocationBarByDefault()) {
124 if (web_contents_->GetDelegate())
125 web_contents_->GetDelegate()->SetFocusToLocationBar(false);
126 } else {
127 web_contents_->Focus();
130 scoped_ptr<RenderViewContextMenuImpl> context_menu_;
131 content::WebContents* web_contents_;
132 DISALLOW_COPY_AND_ASSIGN(WebContentsViewDelegateImpl);
135 } // namespace
137 content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
138 content::WebContents* web_contents) {
139 return new WebContentsViewDelegateImpl(web_contents);
142 } // namespace athena
144 namespace web_modal {
146 SingleWebContentsDialogManager*
147 WebContentsModalDialogManager::CreateNativeWebModalManager(
148 NativeWebContentsModalDialog dialog,
149 SingleWebContentsDialogManagerDelegate* native_delegate) {
150 // TODO(oshima): Investigate if we need to implement this.
151 NOTREACHED();
152 return NULL;
155 } // namespace web_modal