chrome_web_contents_view_delegate_views.cc revision a02191e04bc25c4935f804f2c080ae28663d096d
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      RenderViewContextMenuViews::Create(web_contents->GetFocusedFrame(),
127                                         params));
128  menu->Init();
129  return menu.Pass();
130}
131
132void ChromeWebContentsViewDelegateViews::ShowMenu(
133    scoped_ptr<RenderViewContextMenu> menu) {
134  DCHECK(menu.get());
135  context_menu_.reset(static_cast<RenderViewContextMenuViews*>(menu.release()));
136
137  // Menus need a Widget to work. If we're not the active tab we won't
138  // necessarily be in a widget.
139  views::Widget* top_level_widget = GetTopLevelWidget();
140  if (!top_level_widget)
141    return;
142
143  const content::ContextMenuParams& params = context_menu_->params();
144  // Don't show empty menus.
145  if (context_menu_->menu_model().GetItemCount() == 0)
146    return;
147
148  gfx::Point screen_point(params.x, params.y);
149
150  // Convert from target window coordinates to root window coordinates.
151  aura::Window* target_window = GetActiveNativeView();
152  aura::Window* root_window = target_window->GetRootWindow();
153  aura::client::ScreenPositionClient* screen_position_client =
154      aura::client::GetScreenPositionClient(root_window);
155  if (screen_position_client) {
156    screen_position_client->ConvertPointToScreen(target_window,
157                                                 &screen_point);
158  }
159  // Enable recursive tasks on the message loop so we can get updates while
160  // the context menu is being displayed.
161  base::MessageLoop::ScopedNestableTaskAllower allow(
162      base::MessageLoop::current());
163  context_menu_->RunMenuAt(top_level_widget, screen_point, params.source_type);
164}
165
166void ChromeWebContentsViewDelegateViews::ShowContextMenu(
167    content::RenderFrameHost* render_frame_host,
168    const content::ContextMenuParams& params) {
169  ShowMenu(
170      BuildMenu(content::WebContents::FromRenderFrameHost(render_frame_host),
171                params));
172}
173
174void ChromeWebContentsViewDelegateViews::SizeChanged(const gfx::Size& size) {
175  SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
176  if (!sad_tab_helper)
177    return;
178  SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
179  if (sad_tab)
180    sad_tab->GetWidget()->SetBounds(gfx::Rect(size));
181}
182
183aura::Window* ChromeWebContentsViewDelegateViews::GetActiveNativeView() {
184  return web_contents_->GetFullscreenRenderWidgetHostView() ?
185      web_contents_->GetFullscreenRenderWidgetHostView()->GetNativeView() :
186      web_contents_->GetView()->GetNativeView();
187}
188
189views::Widget* ChromeWebContentsViewDelegateViews::GetTopLevelWidget() {
190  return views::Widget::GetTopLevelWidgetForNativeView(GetActiveNativeView());
191}
192
193views::FocusManager*
194    ChromeWebContentsViewDelegateViews::GetFocusManager() {
195  views::Widget* toplevel_widget = GetTopLevelWidget();
196  return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL;
197}
198
199void ChromeWebContentsViewDelegateViews::SetInitialFocus() {
200  if (web_contents_->FocusLocationBarByDefault()) {
201    if (web_contents_->GetDelegate())
202      web_contents_->GetDelegate()->SetFocusToLocationBar(false);
203  } else {
204    web_contents_->GetView()->Focus();
205  }
206}
207
208namespace chrome {
209
210content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
211    content::WebContents* web_contents) {
212  return new ChromeWebContentsViewDelegateViews(web_contents);
213}
214
215}  // namespace chrome
216