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