chrome_web_contents_view_delegate_views.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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/browser_finder.h" 10#include "chrome/browser/ui/sad_tab_helper.h" 11#include "chrome/browser/ui/tab_contents/chrome_web_contents_view_delegate.h" 12#include "chrome/browser/ui/views/renderer_context_menu/render_view_context_menu_views.h" 13#include "chrome/browser/ui/views/sad_tab_view.h" 14#include "chrome/common/chrome_switches.h" 15#include "components/web_modal/popup_manager.h" 16#include "content/public/browser/render_process_host.h" 17#include "content/public/browser/render_view_host.h" 18#include "content/public/browser/render_widget_host_view.h" 19#include "content/public/browser/web_contents.h" 20#include "content/public/browser/web_contents_delegate.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 27ChromeWebContentsViewDelegateViews::ChromeWebContentsViewDelegateViews( 28 content::WebContents* web_contents) 29 : ContextMenuDelegate(web_contents), 30 web_contents_(web_contents) { 31 last_focused_view_storage_id_ = 32 views::ViewStorage::GetInstance()->CreateStorageID(); 33} 34 35ChromeWebContentsViewDelegateViews::~ChromeWebContentsViewDelegateViews() { 36 // Makes sure to remove any stored view we may still have in the ViewStorage. 37 // 38 // It is possible the view went away before us, so we only do this if the 39 // view is registered. 40 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); 41 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL) 42 view_storage->RemoveView(last_focused_view_storage_id_); 43} 44 45content::WebDragDestDelegate* 46 ChromeWebContentsViewDelegateViews::GetDragDestDelegate() { 47 // We install a chrome specific handler to intercept bookmark drags for the 48 // bookmark manager/extension API. 49 bookmark_handler_.reset(new WebDragBookmarkHandlerAura); 50 return bookmark_handler_.get(); 51} 52 53bool ChromeWebContentsViewDelegateViews::Focus() { 54 SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_); 55 if (sad_tab_helper) { 56 SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab()); 57 if (sad_tab) { 58 sad_tab->RequestFocus(); 59 return true; 60 } 61 } 62 63 web_modal::PopupManager* popup_manager = 64 web_modal::PopupManager::FromWebContents(web_contents_); 65 if (popup_manager) 66 popup_manager->WasFocused(web_contents_); 67 68 return false; 69} 70 71void ChromeWebContentsViewDelegateViews::TakeFocus(bool reverse) { 72 views::FocusManager* focus_manager = GetFocusManager(); 73 if (focus_manager) 74 focus_manager->AdvanceFocus(reverse); 75} 76 77void ChromeWebContentsViewDelegateViews::StoreFocus() { 78 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); 79 80 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL) 81 view_storage->RemoveView(last_focused_view_storage_id_); 82 83 if (!GetFocusManager()) 84 return; 85 views::View* focused_view = GetFocusManager()->GetFocusedView(); 86 if (focused_view) 87 view_storage->StoreView(last_focused_view_storage_id_, focused_view); 88} 89 90void ChromeWebContentsViewDelegateViews::RestoreFocus() { 91 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); 92 views::View* last_focused_view = 93 view_storage->RetrieveView(last_focused_view_storage_id_); 94 95 if (!last_focused_view) { 96 SetInitialFocus(); 97 } else { 98 if (last_focused_view->IsFocusable() && 99 GetFocusManager()->ContainsView(last_focused_view)) { 100 last_focused_view->RequestFocus(); 101 } else { 102 // The focused view may not belong to the same window hierarchy (e.g. 103 // if the location bar was focused and the tab is dragged out), or it may 104 // no longer be focusable (e.g. if the location bar was focused and then 105 // we switched to fullscreen mode). In that case we default to the 106 // default focus. 107 SetInitialFocus(); 108 } 109 view_storage->RemoveView(last_focused_view_storage_id_); 110 } 111} 112 113scoped_ptr<RenderViewContextMenu> ChromeWebContentsViewDelegateViews::BuildMenu( 114 content::WebContents* web_contents, 115 const content::ContextMenuParams& params) { 116 scoped_ptr<RenderViewContextMenu> menu; 117 content::RenderFrameHost* focused_frame = web_contents->GetFocusedFrame(); 118 // If the frame tree does not have a focused frame at this point, do not 119 // bother creating RenderViewContextMenuViews. 120 // This happens if the frame has navigated to a different page before 121 // ContextMenu message was received by the current RenderFrameHost. 122 if (focused_frame) { 123 menu.reset(RenderViewContextMenuViews::Create(focused_frame, params)); 124 menu->Init(); 125 } 126 return menu.Pass(); 127} 128 129void ChromeWebContentsViewDelegateViews::ShowMenu( 130 scoped_ptr<RenderViewContextMenu> menu) { 131 context_menu_.reset(static_cast<RenderViewContextMenuViews*>(menu.release())); 132 if (!context_menu_.get()) 133 return; 134 135 // Menus need a Widget to work. If we're not the active tab we won't 136 // necessarily be in a widget. 137 views::Widget* top_level_widget = GetTopLevelWidget(); 138 if (!top_level_widget) 139 return; 140 141 const content::ContextMenuParams& params = context_menu_->params(); 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 // Convert from target window coordinates to root window coordinates. 149 aura::Window* target_window = GetActiveNativeView(); 150 aura::Window* root_window = target_window->GetRootWindow(); 151 aura::client::ScreenPositionClient* screen_position_client = 152 aura::client::GetScreenPositionClient(root_window); 153 if (screen_position_client) { 154 screen_position_client->ConvertPointToScreen(target_window, 155 &screen_point); 156 } 157 // Enable recursive tasks on the message loop so we can get updates while 158 // the context menu is being displayed. 159 base::MessageLoop::ScopedNestableTaskAllower allow( 160 base::MessageLoop::current()); 161 context_menu_->RunMenuAt(top_level_widget, screen_point, params.source_type); 162} 163 164void ChromeWebContentsViewDelegateViews::ShowContextMenu( 165 content::RenderFrameHost* render_frame_host, 166 const content::ContextMenuParams& params) { 167 ShowMenu( 168 BuildMenu(content::WebContents::FromRenderFrameHost(render_frame_host), 169 params)); 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 181aura::Window* ChromeWebContentsViewDelegateViews::GetActiveNativeView() { 182 return web_contents_->GetFullscreenRenderWidgetHostView() ? 183 web_contents_->GetFullscreenRenderWidgetHostView()->GetNativeView() : 184 web_contents_->GetNativeView(); 185} 186 187views::Widget* ChromeWebContentsViewDelegateViews::GetTopLevelWidget() { 188 return views::Widget::GetTopLevelWidgetForNativeView(GetActiveNativeView()); 189} 190 191views::FocusManager* 192 ChromeWebContentsViewDelegateViews::GetFocusManager() { 193 views::Widget* toplevel_widget = GetTopLevelWidget(); 194 return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL; 195} 196 197void ChromeWebContentsViewDelegateViews::SetInitialFocus() { 198 if (web_contents_->FocusLocationBarByDefault()) { 199 if (web_contents_->GetDelegate()) 200 web_contents_->GetDelegate()->SetFocusToLocationBar(false); 201 } else { 202 web_contents_->Focus(); 203 } 204} 205 206namespace chrome { 207 208content::WebContentsViewDelegate* CreateWebContentsViewDelegate( 209 content::WebContents* web_contents) { 210 return new ChromeWebContentsViewDelegateViews(web_contents); 211} 212 213} // namespace chrome 214