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/defaults.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 "components/web_modal/popup_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 "ui/aura/client/screen_position_client.h"
20 #include "ui/aura/window.h"
21 #include "ui/views/focus/focus_manager.h"
22 #include "ui/views/focus/view_storage.h"
23 #include "ui/views/widget/widget.h"
26 #include "chrome/browser/ui/views/link_disambiguation/link_disambiguation_popup.h"
29 ChromeWebContentsViewDelegateViews::ChromeWebContentsViewDelegateViews(
30 content::WebContents* web_contents)
31 : ContextMenuDelegate(web_contents),
32 web_contents_(web_contents) {
33 last_focused_view_storage_id_ =
34 views::ViewStorage::GetInstance()->CreateStorageID();
37 ChromeWebContentsViewDelegateViews::~ChromeWebContentsViewDelegateViews() {
38 // Makes sure to remove any stored view we may still have in the ViewStorage.
40 // It is possible the view went away before us, so we only do this if the
41 // view is registered.
42 views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
43 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
44 view_storage->RemoveView(last_focused_view_storage_id_);
47 content::WebDragDestDelegate*
48 ChromeWebContentsViewDelegateViews::GetDragDestDelegate() {
49 // We install a chrome specific handler to intercept bookmark drags for the
50 // bookmark manager/extension API.
51 bookmark_handler_.reset(new WebDragBookmarkHandlerAura);
52 return bookmark_handler_.get();
55 bool ChromeWebContentsViewDelegateViews::Focus() {
56 SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
58 SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
60 sad_tab->RequestFocus();
65 web_modal::PopupManager* popup_manager =
66 web_modal::PopupManager::FromWebContents(web_contents_);
68 popup_manager->WasFocused(web_contents_);
73 void ChromeWebContentsViewDelegateViews::TakeFocus(bool reverse) {
74 views::FocusManager* focus_manager = GetFocusManager();
76 focus_manager->AdvanceFocus(reverse);
79 void ChromeWebContentsViewDelegateViews::StoreFocus() {
80 views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
82 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL)
83 view_storage->RemoveView(last_focused_view_storage_id_);
85 if (!GetFocusManager())
87 views::View* focused_view = GetFocusManager()->GetFocusedView();
89 view_storage->StoreView(last_focused_view_storage_id_, focused_view);
92 void ChromeWebContentsViewDelegateViews::RestoreFocus() {
93 views::ViewStorage* view_storage = views::ViewStorage::GetInstance();
94 views::View* last_focused_view =
95 view_storage->RetrieveView(last_focused_view_storage_id_);
97 if (!last_focused_view) {
100 if (last_focused_view->IsFocusable() &&
101 GetFocusManager()->ContainsView(last_focused_view)) {
102 last_focused_view->RequestFocus();
104 // The focused view may not belong to the same window hierarchy (e.g.
105 // if the location bar was focused and the tab is dragged out), or it may
106 // no longer be focusable (e.g. if the location bar was focused and then
107 // we switched to fullscreen mode). In that case we default to the
111 view_storage->RemoveView(last_focused_view_storage_id_);
115 scoped_ptr<RenderViewContextMenu> ChromeWebContentsViewDelegateViews::BuildMenu(
116 content::WebContents* web_contents,
117 const content::ContextMenuParams& params) {
118 scoped_ptr<RenderViewContextMenu> menu;
119 content::RenderFrameHost* focused_frame = web_contents->GetFocusedFrame();
120 // If the frame tree does not have a focused frame at this point, do not
121 // bother creating RenderViewContextMenuViews.
122 // This happens if the frame has navigated to a different page before
123 // ContextMenu message was received by the current RenderFrameHost.
125 menu.reset(RenderViewContextMenuViews::Create(focused_frame, params));
131 void ChromeWebContentsViewDelegateViews::ShowMenu(
132 scoped_ptr<RenderViewContextMenu> menu) {
133 context_menu_.reset(static_cast<RenderViewContextMenuViews*>(menu.release()));
134 if (!context_menu_.get())
137 // Menus need a Widget to work. If we're not the active tab we won't
138 // necessarily be in a widget.
139 views::Widget* top_level_widget = GetTopLevelWidget();
140 if (!top_level_widget)
143 const content::ContextMenuParams& params = context_menu_->params();
144 // Don't show empty menus.
145 if (context_menu_->menu_model().GetItemCount() == 0)
148 gfx::Point screen_point(params.x, params.y);
150 // Convert from target window coordinates to root window coordinates.
151 aura::Window* target_window = GetActiveNativeView();
152 aura::Window* root_window = target_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(target_window,
159 // Enable recursive tasks on the message loop so we can get updates while
160 // the context menu is being displayed.
161 base::MessageLoop::ScopedNestableTaskAllower allow(
162 base::MessageLoop::current());
163 context_menu_->RunMenuAt(top_level_widget, screen_point, params.source_type);
166 void ChromeWebContentsViewDelegateViews::ShowContextMenu(
167 content::RenderFrameHost* render_frame_host,
168 const content::ContextMenuParams& params) {
170 BuildMenu(content::WebContents::FromRenderFrameHost(render_frame_host),
174 void ChromeWebContentsViewDelegateViews::ShowDisambiguationPopup(
175 const gfx::Rect& target_rect,
176 const SkBitmap& zoomed_bitmap,
177 const gfx::NativeView content,
178 const base::Callback<void(ui::GestureEvent*)>& gesture_cb,
179 const base::Callback<void(ui::MouseEvent*)>& mouse_cb) {
180 #if defined(USE_AURA)
181 if (!browser_defaults::kShowLinkDisambiguationPopup)
184 link_disambiguation_popup_.reset(new LinkDisambiguationPopup);
185 link_disambiguation_popup_->Show(
186 zoomed_bitmap, target_rect, content, gesture_cb, mouse_cb);
190 void ChromeWebContentsViewDelegateViews::HideDisambiguationPopup() {
191 link_disambiguation_popup_.reset();
194 void ChromeWebContentsViewDelegateViews::SizeChanged(const gfx::Size& size) {
195 SadTabHelper* sad_tab_helper = SadTabHelper::FromWebContents(web_contents_);
198 SadTabView* sad_tab = static_cast<SadTabView*>(sad_tab_helper->sad_tab());
200 sad_tab->GetWidget()->SetBounds(gfx::Rect(size));
203 aura::Window* ChromeWebContentsViewDelegateViews::GetActiveNativeView() {
204 return web_contents_->GetFullscreenRenderWidgetHostView() ?
205 web_contents_->GetFullscreenRenderWidgetHostView()->GetNativeView() :
206 web_contents_->GetNativeView();
209 views::Widget* ChromeWebContentsViewDelegateViews::GetTopLevelWidget() {
210 return views::Widget::GetTopLevelWidgetForNativeView(GetActiveNativeView());
214 ChromeWebContentsViewDelegateViews::GetFocusManager() {
215 views::Widget* toplevel_widget = GetTopLevelWidget();
216 return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL;
219 void ChromeWebContentsViewDelegateViews::SetInitialFocus() {
220 if (web_contents_->FocusLocationBarByDefault()) {
221 if (web_contents_->GetDelegate())
222 web_contents_->GetDelegate()->SetFocusToLocationBar(false);
224 web_contents_->Focus();
230 content::WebContentsViewDelegate* CreateWebContentsViewDelegate(
231 content::WebContents* web_contents) {
232 return new ChromeWebContentsViewDelegateViews(web_contents);
235 } // namespace chrome