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