chrome_web_contents_view_delegate_views.cc revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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    content::ContextMenuSourceType type) {
132  // Menus need a Widget to work. If we're not the active tab we won't
133  // necessarily be in a widget.
134  views::Widget* top_level_widget = GetTopLevelWidget();
135  if (!top_level_widget)
136    return;
137
138  context_menu_.reset(
139      RenderViewContextMenuViews::Create(web_contents_, params));
140  context_menu_->Init();
141
142  // Don't show empty menus.
143  if (context_menu_->menu_model().GetItemCount() == 0)
144    return;
145
146  gfx::Point screen_point(params.x, params.y);
147
148#if defined(USE_AURA)
149  // Convert from content coordinates to window coordinates.
150  aura::Window* web_contents_window =
151      web_contents_->GetView()->GetNativeView();
152  aura::RootWindow* root_window = web_contents_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(web_contents_window,
157                                                 &screen_point);
158  }
159#else
160  POINT temp = screen_point.ToPOINT();
161  ClientToScreen(web_contents_->GetView()->GetNativeView(), &temp);
162  screen_point = temp;
163#endif
164
165  // Enable recursive tasks on the message loop so we can get updates while
166  // the context menu is being displayed.
167  base::MessageLoop::ScopedNestableTaskAllower allow(
168      base::MessageLoop::current());
169  context_menu_->RunMenuAt(top_level_widget, screen_point, type);
170}
171
172void ChromeWebContentsViewDelegateViews::SizeChanged(const gfx::Size& size) {
173  SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
174  if (!sad_tab_helper)
175    return;
176  SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
177  if (sad_tab)
178    sad_tab->GetWidget()->SetBounds(gfx::Rect(size));
179}
180
181views::Widget* ChromeWebContentsViewDelegateViews::GetTopLevelWidget() {
182  gfx::NativeWindow top_level_window =
183      web_contents_->GetView()->GetTopLevelNativeWindow();
184  if (!top_level_window)
185    return NULL;
186  return views::Widget::GetWidgetForNativeWindow(top_level_window);
187}
188
189views::FocusManager*
190    ChromeWebContentsViewDelegateViews::GetFocusManager() {
191  views::Widget* toplevel_widget = GetTopLevelWidget();
192  return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL;
193}
194
195void ChromeWebContentsViewDelegateViews::SetInitialFocus() {
196  if (web_contents_->FocusLocationBarByDefault()) {
197    if (web_contents_->GetDelegate())
198      web_contents_->GetDelegate()->SetFocusToLocationBar(false);
199  } else {
200    web_contents_->GetView()->Focus();
201  }
202}
203
204namespace chrome {
205
206content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
207    content::WebContents* web_contents) {
208  return new ChromeWebContentsViewDelegateViews(web_contents);
209}
210
211}  // namespace chrome
212