chrome_web_contents_view_delegate_views.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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#include "chrome/browser/ui/views/tab_contents/chrome_web_contents_view_delegate_views.h"
6
7#include "chrome/browser/browser_shutdown.h"
8#include "chrome/browser/ui/aura/tab_contents/web_drag_bookmark_handler_aura.h"
9#include "chrome/browser/ui/sad_tab_helper.h"
10#include "chrome/browser/ui/tab_contents/chrome_web_contents_view_delegate.h"
11#include "chrome/browser/ui/views/renderer_context_menu/render_view_context_menu_views.h"
12#include "chrome/browser/ui/views/sad_tab_view.h"
13#include "chrome/common/chrome_switches.h"
14#include "components/web_modal/web_contents_modal_dialog_manager.h"
15#include "content/public/browser/render_process_host.h"
16#include "content/public/browser/render_view_host.h"
17#include "content/public/browser/render_widget_host_view.h"
18#include "content/public/browser/web_contents.h"
19#include "content/public/browser/web_contents_delegate.h"
20#include "content/public/browser/web_contents_view.h"
21#include "ui/aura/client/screen_position_client.h"
22#include "ui/aura/window.h"
23#include "ui/views/focus/focus_manager.h"
24#include "ui/views/focus/view_storage.h"
25#include "ui/views/widget/widget.h"
26
27using web_modal::WebContentsModalDialogManager;
28
29ChromeWebContentsViewDelegateViews::ChromeWebContentsViewDelegateViews(
30    content::WebContents* web_contents)
31    : ContextMenuDelegate(web_contents),
32      web_contents_(web_contents) {
33  last_focused_view_storage_id_ =
34      views::ViewStorage::GetInstance()->CreateStorageID();
35}
36
37ChromeWebContentsViewDelegateViews::~ChromeWebContentsViewDelegateViews() {
38  // Makes sure to remove any stored view we may still have in the ViewStorage.
39  //
40  // It is possible the view went away before us, so we only do this if the
41  // view is registered.
42  views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
43  if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
44    view_storage->RemoveView(last_focused_view_storage_id_);
45}
46
47content::WebDragDestDelegate*
48    ChromeWebContentsViewDelegateViews::GetDragDestDelegate() {
49  // We install a chrome specific handler to intercept bookmark drags for the
50  // bookmark manager/extension API.
51  bookmark_handler_.reset(new WebDragBookmarkHandlerAura);
52  return bookmark_handler_.get();
53}
54
55bool ChromeWebContentsViewDelegateViews::Focus() {
56  SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
57  if (sad_tab_helper) {
58    SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
59    if (sad_tab) {
60      sad_tab->RequestFocus();
61      return true;
62    }
63  }
64
65  WebContentsModalDialogManager* web_contents_modal_dialog_manager =
66      WebContentsModalDialogManager::FromWebContents(web_contents_);
67  if (web_contents_modal_dialog_manager) {
68    // TODO(erg): WebContents used to own web contents modal dialogs, which is
69    // why this is here. Eventually this should be ported to a containing view
70    // specializing in web contents modal dialog management.
71    if (web_contents_modal_dialog_manager->IsDialogActive()) {
72      web_contents_modal_dialog_manager->FocusTopmostDialog();
73      return true;
74    }
75  }
76
77  return false;
78}
79
80void ChromeWebContentsViewDelegateViews::TakeFocus(bool reverse) {
81  views::FocusManager* focus_manager = GetFocusManager();
82  if (focus_manager)
83    focus_manager->AdvanceFocus(reverse);
84}
85
86void ChromeWebContentsViewDelegateViews::StoreFocus() {
87  views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
88
89  if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
90    view_storage->RemoveView(last_focused_view_storage_id_);
91
92  if (!GetFocusManager())
93    return;
94  views::View* focused_view = GetFocusManager()->GetFocusedView();
95  if (focused_view)
96    view_storage->StoreView(last_focused_view_storage_id_, focused_view);
97}
98
99void ChromeWebContentsViewDelegateViews::RestoreFocus() {
100  views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
101  views::View* last_focused_view =
102      view_storage->RetrieveView(last_focused_view_storage_id_);
103
104  if (!last_focused_view) {
105    SetInitialFocus();
106  } else {
107    if (last_focused_view->IsFocusable() &&
108        GetFocusManager()->ContainsView(last_focused_view)) {
109      last_focused_view->RequestFocus();
110    } else {
111      // The focused view may not belong to the same window hierarchy (e.g.
112      // if the location bar was focused and the tab is dragged out), or it may
113      // no longer be focusable (e.g. if the location bar was focused and then
114      // we switched to fullscreen mode).  In that case we default to the
115      // default focus.
116      SetInitialFocus();
117    }
118    view_storage->RemoveView(last_focused_view_storage_id_);
119  }
120}
121
122scoped_ptr<RenderViewContextMenu> ChromeWebContentsViewDelegateViews::BuildMenu(
123    content::WebContents* web_contents,
124    const content::ContextMenuParams& params) {
125  scoped_ptr<RenderViewContextMenu> menu;
126  content::RenderFrameHost* focused_frame = web_contents->GetFocusedFrame();
127  // If the frame tree does not have a focused frame at this point, do not
128  // bother creating RenderViewContextMenuViews.
129  // This happens if the frame has navigated to a different page before
130  // ContextMenu message was received by the current RenderFrameHost.
131  if (focused_frame) {
132    menu.reset(RenderViewContextMenuViews::Create(focused_frame, params));
133    menu->Init();
134  }
135  return menu.Pass();
136}
137
138void ChromeWebContentsViewDelegateViews::ShowMenu(
139    scoped_ptr<RenderViewContextMenu> menu) {
140  context_menu_.reset(static_cast<RenderViewContextMenuViews*>(menu.release()));
141  if (!context_menu_.get())
142    return;
143
144  // Menus need a Widget to work. If we're not the active tab we won't
145  // necessarily be in a widget.
146  views::Widget* top_level_widget = GetTopLevelWidget();
147  if (!top_level_widget)
148    return;
149
150  const content::ContextMenuParams& params = context_menu_->params();
151  // Don't show empty menus.
152  if (context_menu_->menu_model().GetItemCount() == 0)
153    return;
154
155  gfx::Point screen_point(params.x, params.y);
156
157  // Convert from target window coordinates to root window coordinates.
158  aura::Window* target_window = GetActiveNativeView();
159  aura::Window* root_window = target_window->GetRootWindow();
160  aura::client::ScreenPositionClient* screen_position_client =
161      aura::client::GetScreenPositionClient(root_window);
162  if (screen_position_client) {
163    screen_position_client->ConvertPointToScreen(target_window,
164                                                 &screen_point);
165  }
166  // Enable recursive tasks on the message loop so we can get updates while
167  // the context menu is being displayed.
168  base::MessageLoop::ScopedNestableTaskAllower allow(
169      base::MessageLoop::current());
170  context_menu_->RunMenuAt(top_level_widget, screen_point, params.source_type);
171}
172
173void ChromeWebContentsViewDelegateViews::ShowContextMenu(
174    content::RenderFrameHost* render_frame_host,
175    const content::ContextMenuParams& params) {
176  ShowMenu(
177      BuildMenu(content::WebContents::FromRenderFrameHost(render_frame_host),
178                params));
179}
180
181void ChromeWebContentsViewDelegateViews::SizeChanged(const gfx::Size& size) {
182  SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
183  if (!sad_tab_helper)
184    return;
185  SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
186  if (sad_tab)
187    sad_tab->GetWidget()->SetBounds(gfx::Rect(size));
188}
189
190aura::Window* ChromeWebContentsViewDelegateViews::GetActiveNativeView() {
191  return web_contents_->GetFullscreenRenderWidgetHostView() ?
192      web_contents_->GetFullscreenRenderWidgetHostView()->GetNativeView() :
193      web_contents_->GetView()->GetNativeView();
194}
195
196views::Widget* ChromeWebContentsViewDelegateViews::GetTopLevelWidget() {
197  return views::Widget::GetTopLevelWidgetForNativeView(GetActiveNativeView());
198}
199
200views::FocusManager*
201    ChromeWebContentsViewDelegateViews::GetFocusManager() {
202  views::Widget* toplevel_widget = GetTopLevelWidget();
203  return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL;
204}
205
206void ChromeWebContentsViewDelegateViews::SetInitialFocus() {
207  if (web_contents_->FocusLocationBarByDefault()) {
208    if (web_contents_->GetDelegate())
209      web_contents_->GetDelegate()->SetFocusToLocationBar(false);
210  } else {
211    web_contents_->GetView()->Focus();
212  }
213}
214
215namespace chrome {
216
217content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
218    content::WebContents* web_contents) {
219  return new ChromeWebContentsViewDelegateViews(web_contents);
220}
221
222}  // namespace chrome
223