| 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_views.h" | |
| 6 | |
| 7 #include "chrome/browser/browser_shutdown.h" | |
| 8 #include "chrome/browser/ui/constrained_window_tab_helper.h" | |
| 9 #include "chrome/browser/ui/sad_tab_helper.h" | |
| 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 11 #include "chrome/browser/ui/views/constrained_window_views.h" | |
| 12 #include "chrome/browser/ui/views/tab_contents/render_view_context_menu_views.h" | |
| 13 #include "content/public/browser/render_process_host.h" | |
| 14 #include "content/public/browser/render_view_host.h" | |
| 15 #include "content/public/browser/render_widget_host_view.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "content/public/browser/web_contents_view.h" | |
| 18 #include "ui/views/focus/focus_manager.h" | |
| 19 #include "ui/views/focus/view_storage.h" | |
| 20 #include "ui/views/widget/widget.h" | |
| 21 | |
| 22 #if defined(USE_AURA) | |
| 23 #include "chrome/browser/tab_contents/web_drag_bookmark_handler_aura.h" | |
| 24 #include "ui/aura/window.h" | |
| 25 #else | |
| 26 #include "chrome/browser/tab_contents/web_drag_bookmark_handler_win.h" | |
| 27 #endif | |
| 28 | |
| 29 ChromeWebContentsViewDelegateViews::ChromeWebContentsViewDelegateViews( | |
| 30 content::WebContents* web_contents) | |
| 31 : web_contents_(web_contents) { | |
| 32 last_focused_view_storage_id_ = | |
| 33 views::ViewStorage::GetInstance()->CreateStorageID(); | |
| 34 } | |
| 35 | |
| 36 ChromeWebContentsViewDelegateViews::~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 } | |
| 46 | |
| 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 #if defined(USE_AURA) | |
| 52 bookmark_handler_.reset(new WebDragBookmarkHandlerAura); | |
| 53 #else | |
| 54 bookmark_handler_.reset(new WebDragBookmarkHandlerWin); | |
| 55 #endif | |
| 56 return bookmark_handler_.get(); | |
| 57 } | |
| 58 | |
| 59 bool ChromeWebContentsViewDelegateViews::Focus() { | |
| 60 TabContentsWrapper* wrapper = | |
| 61 TabContentsWrapper::GetCurrentWrapperForContents(web_contents_); | |
| 62 if (wrapper) { | |
| 63 views::Widget* sad_tab = wrapper->sad_tab_helper()->sad_tab(); | |
| 64 if (sad_tab) { | |
| 65 sad_tab->GetContentsView()->RequestFocus(); | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 // TODO(erg): TabContents used to own constrained windows, which is why | |
| 70 // this is here. Eventually this should be ported to a containing view | |
| 71 // specializing in constrained window management. | |
| 72 ConstrainedWindowTabHelper* helper = | |
| 73 wrapper->constrained_window_tab_helper(); | |
| 74 if (helper->constrained_window_count() > 0) { | |
| 75 ConstrainedWindow* window = *helper->constrained_window_begin(); | |
| 76 DCHECK(window); | |
| 77 window->FocusConstrainedWindow(); | |
| 78 return true; | |
| 79 } | |
| 80 } | |
| 81 return false; | |
| 82 } | |
| 83 | |
| 84 void ChromeWebContentsViewDelegateViews::TakeFocus(bool reverse) { | |
| 85 GetFocusManager()->AdvanceFocus(reverse); | |
| 86 } | |
| 87 | |
| 88 void ChromeWebContentsViewDelegateViews::StoreFocus() { | |
| 89 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); | |
| 90 | |
| 91 if (view_storage->RetrieveView(last_focused_view_storage_id_) != NULL) | |
| 92 view_storage->RemoveView(last_focused_view_storage_id_); | |
| 93 | |
| 94 if (!GetFocusManager()) | |
| 95 return; | |
| 96 views::View* focused_view = GetFocusManager()->GetFocusedView(); | |
| 97 if (focused_view) | |
| 98 view_storage->StoreView(last_focused_view_storage_id_, focused_view); | |
| 99 } | |
| 100 | |
| 101 void ChromeWebContentsViewDelegateViews::RestoreFocus() { | |
| 102 views::ViewStorage* view_storage = views::ViewStorage::GetInstance(); | |
| 103 views::View* last_focused_view = | |
| 104 view_storage->RetrieveView(last_focused_view_storage_id_); | |
| 105 | |
| 106 if (!last_focused_view) { | |
| 107 SetInitialFocus(); | |
| 108 } else { | |
| 109 if (last_focused_view->IsFocusable() && | |
| 110 GetFocusManager()->ContainsView(last_focused_view)) { | |
| 111 last_focused_view->RequestFocus(); | |
| 112 } else { | |
| 113 // The focused view may not belong to the same window hierarchy (e.g. | |
| 114 // if the location bar was focused and the tab is dragged out), or it may | |
| 115 // no longer be focusable (e.g. if the location bar was focused and then | |
| 116 // we switched to fullscreen mode). In that case we default to the | |
| 117 // default focus. | |
| 118 SetInitialFocus(); | |
| 119 } | |
| 120 view_storage->RemoveView(last_focused_view_storage_id_); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 void ChromeWebContentsViewDelegateViews::ShowContextMenu( | |
| 125 const content::ContextMenuParams& params) { | |
| 126 context_menu_.reset(new RenderViewContextMenuViews(web_contents_, params)); | |
| 127 context_menu_->Init(); | |
| 128 | |
| 129 // Don't show empty menus. | |
| 130 if (context_menu_->menu_model().GetItemCount() == 0) | |
| 131 return; | |
| 132 | |
| 133 gfx::Point screen_point(params.x, params.y); | |
| 134 | |
| 135 #if defined(USE_AURA) | |
| 136 gfx::Point view_origin = | |
| 137 web_contents_->GetView()->GetNativeView()->GetScreenBounds().origin(); | |
| 138 screen_point.Offset(view_origin.x(), view_origin.y()); | |
| 139 #else | |
| 140 POINT temp = screen_point.ToPOINT(); | |
| 141 ClientToScreen(web_contents_->GetView()->GetNativeView(), &temp); | |
| 142 screen_point = temp; | |
| 143 #endif | |
| 144 | |
| 145 // Enable recursive tasks on the message loop so we can get updates while | |
| 146 // the context menu is being displayed. | |
| 147 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
| 148 context_menu_->RunMenuAt(GetTopLevelWidget(), screen_point); | |
| 149 } | |
| 150 | |
| 151 void ChromeWebContentsViewDelegateViews::SizeChanged(const gfx::Size& size) { | |
| 152 TabContentsWrapper* wrapper = | |
| 153 TabContentsWrapper::GetCurrentWrapperForContents(web_contents_); | |
| 154 if (!wrapper) | |
| 155 return; | |
| 156 views::Widget* sad_tab = wrapper->sad_tab_helper()->sad_tab(); | |
| 157 if (sad_tab) | |
| 158 sad_tab->SetBounds(gfx::Rect(size)); | |
| 159 } | |
| 160 | |
| 161 views::Widget* ChromeWebContentsViewDelegateViews::GetTopLevelWidget() { | |
| 162 gfx::NativeWindow top_level_window = | |
| 163 web_contents_->GetView()->GetTopLevelNativeWindow(); | |
| 164 if (!top_level_window) | |
| 165 return NULL; | |
| 166 return views::Widget::GetWidgetForNativeWindow(top_level_window); | |
| 167 } | |
| 168 | |
| 169 views::FocusManager* | |
| 170 ChromeWebContentsViewDelegateViews::GetFocusManager() { | |
| 171 views::Widget* toplevel_widget = GetTopLevelWidget(); | |
| 172 return toplevel_widget ? toplevel_widget->GetFocusManager() : NULL; | |
| 173 } | |
| 174 | |
| 175 void ChromeWebContentsViewDelegateViews::SetInitialFocus() { | |
| 176 if (web_contents_->FocusLocationBarByDefault()) { | |
| 177 web_contents_->SetFocusToLocationBar(false); | |
| 178 } else { | |
| 179 web_contents_->GetView()->Focus(); | |
| 180 } | |
| 181 } | |
| OLD | NEW |