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.
5 #include "chrome/browser/ui/views/tab_contents/chrome_web_contents_view_delegate_views.h"
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"
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/window.h"
29 #include "chrome/browser/ui/views/tab_contents/web_drag_bookmark_handler_win.h"
32 using web_modal::WebContentsModalDialogManager;
34 ChromeWebContentsViewDelegateViews::ChromeWebContentsViewDelegateViews(
35 content::WebContents* web_contents)
36 : web_contents_(web_contents) {
37 last_focused_view_storage_id_ =
38 views::ViewStorage::GetInstance()->CreateStorageID();
41 ChromeWebContentsViewDelegateViews::~ChromeWebContentsViewDelegateViews() {
42 // Makes sure to remove any stored view we may still have in the ViewStorage.
44 // It is possible the view went away before us, so we only do this if the
45 // view is registered.
46 views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
47 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
48 view_storage->RemoveView(last_focused_view_storage_id_);
51 content::WebDragDestDelegate*
52 ChromeWebContentsViewDelegateViews::GetDragDestDelegate() {
53 // We install a chrome specific handler to intercept bookmark drags for the
54 // bookmark manager/extension API.
56 bookmark_handler_.reset(new WebDragBookmarkHandlerAura);
58 bookmark_handler_.reset(new WebDragBookmarkHandlerWin);
60 return bookmark_handler_.get();
63 bool ChromeWebContentsViewDelegateViews::Focus() {
64 SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
66 SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
68 sad_tab->RequestFocus();
73 WebContentsModalDialogManager* web_contents_modal_dialog_manager =
74 WebContentsModalDialogManager::FromWebContents(web_contents_);
75 if (web_contents_modal_dialog_manager) {
76 // TODO(erg): WebContents used to own web contents modal dialogs, which is
77 // why this is here. Eventually this should be ported to a containing view
78 // specializing in web contents modal dialog management.
79 if (web_contents_modal_dialog_manager->IsDialogActive()) {
80 web_contents_modal_dialog_manager->FocusTopmostDialog();
88 void ChromeWebContentsViewDelegateViews::TakeFocus(bool reverse) {
89 views::FocusManager* focus_manager = GetFocusManager();
91 focus_manager->AdvanceFocus(reverse);
94 void ChromeWebContentsViewDelegateViews::StoreFocus() {
95 views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
97 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
98 view_storage->RemoveView(last_focused_view_storage_id_);
100 if (!GetFocusManager())
102 views::View* focused_view = GetFocusManager()->GetFocusedView();
104 view_storage->StoreView(last_focused_view_storage_id_, focused_view);
107 void ChromeWebContentsViewDelegateViews::RestoreFocus() {
108 views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
109 views::View* last_focused_view =
110 view_storage->RetrieveView(last_focused_view_storage_id_);
112 if (!last_focused_view) {
115 if (last_focused_view->IsFocusable() &&
116 GetFocusManager()->ContainsView(last_focused_view)) {
117 last_focused_view->RequestFocus();
119 // The focused view may not belong to the same window hierarchy (e.g.
120 // if the location bar was focused and the tab is dragged out), or it may
121 // no longer be focusable (e.g. if the location bar was focused and then
122 // we switched to fullscreen mode). In that case we default to the
126 view_storage->RemoveView(last_focused_view_storage_id_);
130 void ChromeWebContentsViewDelegateViews::ShowContextMenu(
131 const content::ContextMenuParams& params) {
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)
139 RenderViewContextMenuViews::Create(web_contents_, params));
140 context_menu_->Init();
142 // Don't show empty menus.
143 if (context_menu_->menu_model().GetItemCount() == 0)
146 gfx::Point screen_point(params.x, params.y);
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::Window* 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,
160 POINT temp = screen_point.ToPOINT();
161 ClientToScreen(web_contents_->GetView()->GetNativeView(), &temp);
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, params.source_type);
172 void ChromeWebContentsViewDelegateViews::SizeChanged(const gfx::Size& size) {
173 SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
176 SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
178 sad_tab->GetWidget()->SetBounds(gfx::Rect(size));
181 views::Widget* ChromeWebContentsViewDelegateViews::GetTopLevelWidget() {
182 return views::Widget::GetTopLevelWidgetForNativeView(
183 web_contents_->GetView()->GetNativeView());
187 ChromeWebContentsViewDelegateViews::GetFocusManager() {
188 views::Widget* toplevel_widget = GetTopLevelWidget();
189 return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL;
192 void ChromeWebContentsViewDelegateViews::SetInitialFocus() {
193 if (web_contents_->FocusLocationBarByDefault()) {
194 if (web_contents_->GetDelegate())
195 web_contents_->GetDelegate()->SetFocusToLocationBar(false);
197 web_contents_->GetView()->Focus();
203 content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
204 content::WebContents* web_contents) {
205 return new ChromeWebContentsViewDelegateViews(web_contents);
208 } // namespace chrome