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