| OLD | NEW |
| (Empty) |
| 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/tab_contents/chrome_web_contents_view_delegate_win.h" | |
| 6 | |
| 7 #include "chrome/browser/browser_shutdown.h" | |
| 8 #include "chrome/browser/tab_contents/web_drag_bookmark_handler_win.h" | |
| 9 #include "chrome/browser/ui/constrained_window_tab_helper.h" | |
| 10 #include "chrome/browser/ui/sad_tab_helper.h" | |
| 11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 12 #include "chrome/browser/ui/views/constrained_window_views.h" | |
| 13 #include "chrome/browser/ui/views/tab_contents/render_view_context_menu_views.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_view.h" | |
| 19 #include "ui/views/focus/focus_manager.h" | |
| 20 #include "ui/views/focus/view_storage.h" | |
| 21 #include "ui/views/widget/widget.h" | |
| 22 | |
| 23 ChromeWebContentsViewDelegateWin::ChromeWebContentsViewDelegateWin( | |
| 24 content::WebContents* web_contents) | |
| 25 : web_contents_(web_contents) { | |
| 26 last_focused_view_storage_id_ = | |
| 27 views::ViewStorage::GetInstance()->CreateStorageID(); | |
| 28 } | |
| 29 | |
| 30 ChromeWebContentsViewDelegateWin::~ChromeWebContentsViewDelegateWin() { | |
| 31 // Makes sure to remove any stored view we may still have in the ViewStorage. | |
| 32 // | |
| 33 // It is possible the view went away before us, so we only do this if the | |
| 34 // view is registered. | |
| 35 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); | |
| 36 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL) | |
| 37 view_storage->RemoveView(last_focused_view_storage_id_); | |
| 38 | |
| 39 } | |
| 40 | |
| 41 content::WebDragDestDelegate* | |
| 42 ChromeWebContentsViewDelegateWin::GetDragDestDelegate() { | |
| 43 // We install a chrome specific handler to intercept bookmark drags for the | |
| 44 // bookmark manager/extension API. | |
| 45 bookmark_handler_.reset(new WebDragBookmarkHandlerWin); | |
| 46 return bookmark_handler_.get(); | |
| 47 } | |
| 48 | |
| 49 bool ChromeWebContentsViewDelegateWin::Focus() { | |
| 50 TabContentsWrapper* wrapper = | |
| 51 TabContentsWrapper::GetCurrentWrapperForContents(web_contents_); | |
| 52 if (wrapper) { | |
| 53 views::Widget* sad_tab = wrapper->sad_tab_helper()->sad_tab(); | |
| 54 if (sad_tab) { | |
| 55 sad_tab->GetContentsView()->RequestFocus(); | |
| 56 return true; | |
| 57 } | |
| 58 | |
| 59 // TODO(erg): TabContents used to own constrained windows, which is why | |
| 60 // this is here. Eventually this should be ported to a containing view | |
| 61 // specializing in constrained window management. | |
| 62 ConstrainedWindowTabHelper* helper = | |
| 63 wrapper->constrained_window_tab_helper(); | |
| 64 if (helper->constrained_window_count() > 0) { | |
| 65 ConstrainedWindow* window = *helper->constrained_window_begin(); | |
| 66 DCHECK(window); | |
| 67 window->FocusConstrainedWindow(); | |
| 68 return true; | |
| 69 } | |
| 70 } | |
| 71 return false; | |
| 72 } | |
| 73 | |
| 74 void ChromeWebContentsViewDelegateWin::TakeFocus(bool reverse) { | |
| 75 GetFocusManager()->AdvanceFocus(reverse); | |
| 76 } | |
| 77 | |
| 78 void ChromeWebContentsViewDelegateWin::StoreFocus() { | |
| 79 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); | |
| 80 | |
| 81 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL) | |
| 82 view_storage->RemoveView(last_focused_view_storage_id_); | |
| 83 | |
| 84 if (!GetFocusManager()) | |
| 85 return; | |
| 86 views::View* focused_view = GetFocusManager()->GetFocusedView(); | |
| 87 if (focused_view) | |
| 88 view_storage->StoreView(last_focused_view_storage_id_, focused_view); | |
| 89 } | |
| 90 | |
| 91 void ChromeWebContentsViewDelegateWin::RestoreFocus() { | |
| 92 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); | |
| 93 views::View* last_focused_view = | |
| 94 view_storage->RetrieveView(last_focused_view_storage_id_); | |
| 95 | |
| 96 if (!last_focused_view) { | |
| 97 SetInitialFocus(); | |
| 98 } else { | |
| 99 if (last_focused_view->IsFocusable() && | |
| 100 GetFocusManager()->ContainsView(last_focused_view)) { | |
| 101 last_focused_view->RequestFocus(); | |
| 102 } else { | |
| 103 // The focused view may not belong to the same window hierarchy (e.g. | |
| 104 // if the location bar was focused and the tab is dragged out), or it may | |
| 105 // no longer be focusable (e.g. if the location bar was focused and then | |
| 106 // we switched to fullscreen mode). In that case we default to the | |
| 107 // default focus. | |
| 108 SetInitialFocus(); | |
| 109 } | |
| 110 view_storage->RemoveView(last_focused_view_storage_id_); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void ChromeWebContentsViewDelegateWin::ShowContextMenu( | |
| 115 const content::ContextMenuParams& params) { | |
| 116 context_menu_.reset(new RenderViewContextMenuViews(web_contents_, params)); | |
| 117 context_menu_->Init(); | |
| 118 | |
| 119 // Don't show empty menus. | |
| 120 if (context_menu_->menu_model().GetItemCount() == 0) | |
| 121 return; | |
| 122 | |
| 123 gfx::Point screen_point(params.x, params.y); | |
| 124 | |
| 125 POINT temp = screen_point.ToPOINT(); | |
| 126 ClientToScreen(web_contents_->GetView()->GetNativeView(), &temp); | |
| 127 screen_point = temp; | |
| 128 | |
| 129 // Enable recursive tasks on the message loop so we can get updates while | |
| 130 // the context menu is being displayed. | |
| 131 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
| 132 context_menu_->RunMenuAt(GetTopLevelWidget(), screen_point); | |
| 133 } | |
| 134 | |
| 135 void ChromeWebContentsViewDelegateWin::SizeChanged(const gfx::Size& size) { | |
| 136 TabContentsWrapper* wrapper = | |
| 137 TabContentsWrapper::GetCurrentWrapperForContents(web_contents_); | |
| 138 if (!wrapper) | |
| 139 return; | |
| 140 views::Widget* sad_tab = wrapper->sad_tab_helper()->sad_tab(); | |
| 141 if (sad_tab) | |
| 142 sad_tab->SetBounds(gfx::Rect(size)); | |
| 143 } | |
| 144 | |
| 145 views::Widget* ChromeWebContentsViewDelegateWin::GetTopLevelWidget() { | |
| 146 HWND top_level_window = web_contents_->GetView()->GetTopLevelNativeWindow(); | |
| 147 if (!top_level_window) | |
| 148 return NULL; | |
| 149 return views::Widget::GetWidgetForNativeWindow(top_level_window); | |
| 150 } | |
| 151 | |
| 152 views::FocusManager* | |
| 153 ChromeWebContentsViewDelegateWin::GetFocusManager() { | |
| 154 views::Widget* toplevel_widget = GetTopLevelWidget(); | |
| 155 return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL; | |
| 156 } | |
| 157 | |
| 158 void ChromeWebContentsViewDelegateWin::SetInitialFocus() { | |
| 159 if (web_contents_->FocusLocationBarByDefault()) { | |
| 160 web_contents_->SetFocusToLocationBar(false); | |
| 161 } else { | |
| 162 web_contents_->GetView()->Focus(); | |
| 163 } | |
| 164 } | |
| OLD | NEW |