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