OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/services/window_manager/window_manager_app.h" | 5 #include "mojo/services/window_manager/window_manager_app.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "mojo/aura/aura_init.h" | 9 #include "mojo/aura/aura_init.h" |
10 #include "mojo/aura/window_tree_host_mojo.h" | |
11 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
11 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" | |
12 #include "mojo/services/public/cpp/view_manager/view.h" | 12 #include "mojo/services/public/cpp/view_manager/view.h" |
13 #include "mojo/services/public/cpp/view_manager/view_manager.h" | 13 #include "mojo/services/public/cpp/view_manager/view_manager.h" |
14 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
15 #include "ui/aura/window_delegate.h" | |
15 #include "ui/aura/window_property.h" | 16 #include "ui/aura/window_property.h" |
17 #include "ui/base/hit_test.h" | |
16 #include "ui/wm/core/capture_controller.h" | 18 #include "ui/wm/core/capture_controller.h" |
17 #include "ui/wm/core/focus_controller.h" | 19 #include "ui/wm/core/focus_controller.h" |
18 #include "ui/wm/core/focus_rules.h" | 20 #include "ui/wm/core/focus_rules.h" |
19 #include "ui/wm/public/activation_client.h" | 21 #include "ui/wm/public/activation_client.h" |
20 | 22 |
21 DECLARE_WINDOW_PROPERTY_TYPE(mojo::View*); | 23 DECLARE_WINDOW_PROPERTY_TYPE(mojo::View*); |
22 | 24 |
23 namespace mojo { | 25 namespace mojo { |
26 | |
27 // The aura::Windows we use to track Views don't render, so we don't actually | |
28 // need to supply a fully functional WindowDelegate. We do need to provide _a_ | |
29 // delegate however, otherwise the event dispatcher won't dispatch events to | |
30 // these windows. (The aura WindowTargeter won't allow a delegate-less window | |
31 // to be the target of an event, since the window delegate is considered the | |
32 // "target handler"). | |
33 class DummyDelegate : public aura::WindowDelegate { | |
34 public: | |
35 DummyDelegate() {} | |
36 virtual ~DummyDelegate() {} | |
37 | |
38 private: | |
39 // WindowDelegate overrides: | |
40 virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); } | |
41 virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); } | |
42 virtual void OnBoundsChanged(const gfx::Rect& old_bounds, | |
43 const gfx::Rect& new_bounds) OVERRIDE {} | |
44 virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE { | |
45 return gfx::kNullCursor; | |
46 } | |
47 virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE { | |
48 return HTCAPTION; | |
49 } | |
50 virtual bool ShouldDescendIntoChildForEventHandling( | |
51 aura::Window* child, | |
52 const gfx::Point& location) OVERRIDE { return true; } | |
53 virtual bool CanFocus() OVERRIDE { return true; } | |
54 virtual void OnCaptureLost() OVERRIDE {} | |
55 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {} | |
56 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {} | |
57 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {} | |
58 virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {} | |
59 virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {} | |
60 virtual bool HasHitTestMask() const OVERRIDE { return false; } | |
61 virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {} | |
62 | |
63 DISALLOW_COPY_AND_ASSIGN(DummyDelegate); | |
64 }; | |
65 | |
24 namespace { | 66 namespace { |
25 | 67 |
26 DEFINE_WINDOW_PROPERTY_KEY(View*, kViewKey, NULL); | 68 DEFINE_WINDOW_PROPERTY_KEY(View*, kViewKey, NULL); |
27 | 69 |
28 Id GetIdForWindow(aura::Window* window) { | 70 Id GetIdForWindow(aura::Window* window) { |
29 return window ? window->GetProperty(kViewKey)->id() : 0; | 71 return window ? WindowManagerApp::GetViewForWindow(window)->id() : 0; |
30 } | 72 } |
31 | 73 |
32 class WMFocusRules : public wm::FocusRules { | 74 class WMFocusRules : public wm::FocusRules { |
33 public: | 75 public: |
34 WMFocusRules() {} | 76 WMFocusRules() {} |
35 virtual ~WMFocusRules() {} | 77 virtual ~WMFocusRules() {} |
36 | 78 |
37 private: | 79 private: |
38 // Overridden from wm::FocusRules: | 80 // Overridden from wm::FocusRules: |
39 virtual bool IsToplevelWindow(aura::Window* window) const MOJO_OVERRIDE { | 81 virtual bool IsToplevelWindow(aura::Window* window) const MOJO_OVERRIDE { |
(...skipping 23 matching lines...) Expand all Loading... | |
63 } | 105 } |
64 | 106 |
65 DISALLOW_COPY_AND_ASSIGN(WMFocusRules); | 107 DISALLOW_COPY_AND_ASSIGN(WMFocusRules); |
66 }; | 108 }; |
67 | 109 |
68 } // namespace | 110 } // namespace |
69 | 111 |
70 //////////////////////////////////////////////////////////////////////////////// | 112 //////////////////////////////////////////////////////////////////////////////// |
71 // WindowManagerApp, public: | 113 // WindowManagerApp, public: |
72 | 114 |
73 WindowManagerApp::WindowManagerApp(ViewManagerDelegate* delegate) | 115 WindowManagerApp::WindowManagerApp( |
116 ViewManagerDelegate* view_manager_delegate, | |
117 WindowManagerDelegate* window_manager_delegate) | |
74 : window_manager_service_factory_(this), | 118 : window_manager_service_factory_(this), |
75 wrapped_delegate_(delegate), | 119 wrapped_view_manager_delegate_(view_manager_delegate), |
120 wrapped_window_manager_delegate_(window_manager_delegate), | |
76 view_manager_(NULL), | 121 view_manager_(NULL), |
77 view_manager_client_factory_(this), | 122 view_manager_client_factory_(this), |
78 root_(NULL) { | 123 root_(NULL), |
124 dummy_delegate_(new DummyDelegate) { | |
79 } | 125 } |
80 | 126 |
81 WindowManagerApp::~WindowManagerApp() {} | 127 WindowManagerApp::~WindowManagerApp() {} |
82 | 128 |
129 // static | |
130 View* WindowManagerApp::GetViewForWindow(aura::Window* window) { | |
131 return window->GetProperty(kViewKey); | |
132 } | |
133 | |
83 void WindowManagerApp::AddConnection(WindowManagerServiceImpl* connection) { | 134 void WindowManagerApp::AddConnection(WindowManagerServiceImpl* connection) { |
84 DCHECK(connections_.find(connection) == connections_.end()); | 135 DCHECK(connections_.find(connection) == connections_.end()); |
85 connections_.insert(connection); | 136 connections_.insert(connection); |
86 } | 137 } |
87 | 138 |
88 void WindowManagerApp::RemoveConnection(WindowManagerServiceImpl* connection) { | 139 void WindowManagerApp::RemoveConnection(WindowManagerServiceImpl* connection) { |
89 DCHECK(connections_.find(connection) != connections_.end()); | 140 DCHECK(connections_.find(connection) != connections_.end()); |
90 connections_.erase(connection); | 141 connections_.erase(connection); |
91 } | 142 } |
92 | 143 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 | 179 |
129 //////////////////////////////////////////////////////////////////////////////// | 180 //////////////////////////////////////////////////////////////////////////////// |
130 // WindowManagerApp, ViewManagerDelegate implementation: | 181 // WindowManagerApp, ViewManagerDelegate implementation: |
131 | 182 |
132 void WindowManagerApp::OnEmbed(ViewManager* view_manager, | 183 void WindowManagerApp::OnEmbed(ViewManager* view_manager, |
133 View* root, | 184 View* root, |
134 ServiceProviderImpl* exported_services, | 185 ServiceProviderImpl* exported_services, |
135 scoped_ptr<ServiceProvider> imported_services) { | 186 scoped_ptr<ServiceProvider> imported_services) { |
136 DCHECK(!view_manager_ && !root_); | 187 DCHECK(!view_manager_ && !root_); |
137 view_manager_ = view_manager; | 188 view_manager_ = view_manager; |
189 view_manager_->SetWindowManagerDelegate(this); | |
138 root_ = root; | 190 root_ = root; |
139 root_->AddObserver(this); | |
140 | 191 |
141 window_tree_host_.reset(new WindowTreeHostMojo(root_, this)); | 192 window_tree_host_.reset(new WindowTreeHostMojo(root_, this)); |
193 window_tree_host_->window()->SetBounds(root->bounds()); | |
194 window_tree_host_->window()->Show(); | |
142 | 195 |
143 RegisterSubtree(root_->id(), window_tree_host_->window()); | 196 RegisterSubtree(root_, window_tree_host_->window()); |
144 | 197 |
145 capture_client_.reset( | 198 capture_client_.reset( |
146 new wm::ScopedCaptureClient(window_tree_host_->window())); | 199 new wm::ScopedCaptureClient(window_tree_host_->window())); |
147 wm::FocusController* focus_controller = | 200 wm::FocusController* focus_controller = |
148 new wm::FocusController(new WMFocusRules); | 201 new wm::FocusController(new WMFocusRules); |
149 activation_client_ = focus_controller; | 202 activation_client_ = focus_controller; |
150 focus_client_.reset(focus_controller); | 203 focus_client_.reset(focus_controller); |
204 aura::client::SetFocusClient(window_tree_host_->window(), focus_controller); | |
151 | 205 |
152 focus_client_->AddObserver(this); | 206 focus_client_->AddObserver(this); |
153 activation_client_->AddObserver(this); | 207 activation_client_->AddObserver(this); |
154 | 208 |
155 if (wrapped_delegate_) { | 209 if (wrapped_view_manager_delegate_) { |
156 wrapped_delegate_->OnEmbed(view_manager, root, exported_services, | 210 wrapped_view_manager_delegate_->OnEmbed( |
157 imported_services.Pass()); | 211 view_manager, root, exported_services, imported_services.Pass()); |
158 } | 212 } |
159 | 213 |
160 for (Connections::const_iterator it = connections_.begin(); | 214 for (Connections::const_iterator it = connections_.begin(); |
161 it != connections_.end(); ++it) { | 215 it != connections_.end(); ++it) { |
162 (*it)->NotifyReady(); | 216 (*it)->NotifyReady(); |
163 } | 217 } |
164 } | 218 } |
165 | 219 |
166 void WindowManagerApp::OnViewManagerDisconnected( | 220 void WindowManagerApp::OnViewManagerDisconnected( |
167 ViewManager* view_manager) { | 221 ViewManager* view_manager) { |
168 DCHECK_EQ(view_manager_, view_manager); | 222 DCHECK_EQ(view_manager_, view_manager); |
169 if (wrapped_delegate_) | 223 if (wrapped_view_manager_delegate_) |
170 wrapped_delegate_->OnViewManagerDisconnected(view_manager); | 224 wrapped_view_manager_delegate_->OnViewManagerDisconnected(view_manager); |
171 view_manager_ = NULL; | 225 view_manager_ = NULL; |
172 base::MessageLoop::current()->Quit(); | 226 base::MessageLoop::current()->Quit(); |
173 } | 227 } |
174 | 228 |
175 //////////////////////////////////////////////////////////////////////////////// | 229 //////////////////////////////////////////////////////////////////////////////// |
230 // WindowManagerApp, WindowManagerDelegate implementation: | |
231 | |
232 void WindowManagerApp::Embed( | |
233 const String& url, | |
234 InterfaceRequest<ServiceProvider> service_provider) { | |
235 if (wrapped_window_manager_delegate_) | |
236 wrapped_window_manager_delegate_->Embed(url, service_provider.Pass()); | |
237 } | |
238 | |
239 void WindowManagerApp::DispatchEvent(EventPtr event) { | |
240 scoped_ptr<ui::Event> ui_event = | |
241 TypeConverter<EventPtr, scoped_ptr<ui::Event> >::ConvertTo(event); | |
242 if (ui_event) | |
243 window_tree_host_->SendEventToProcessor(ui_event.get()); | |
244 } | |
245 | |
246 //////////////////////////////////////////////////////////////////////////////// | |
176 // WindowManagerApp, ViewObserver implementation: | 247 // WindowManagerApp, ViewObserver implementation: |
177 | 248 |
178 void WindowManagerApp::OnTreeChanged( | 249 void WindowManagerApp::OnTreeChanged( |
179 const ViewObserver::TreeChangeParams& params) { | 250 const ViewObserver::TreeChangeParams& params) { |
180 DCHECK_EQ(params.receiver, root_); | 251 if (params.receiver != root_) |
252 return; | |
181 DCHECK(params.old_parent || params.new_parent); | 253 DCHECK(params.old_parent || params.new_parent); |
182 if (!params.target) | 254 if (!params.target) |
183 return; | 255 return; |
184 | 256 |
185 if (params.new_parent) { | 257 if (params.new_parent) { |
186 if (view_id_to_window_map_.find(params.target->id()) == | 258 if (view_id_to_window_map_.find(params.target->id()) == |
187 view_id_to_window_map_.end()) { | 259 view_id_to_window_map_.end()) { |
188 ViewIdToWindowMap::const_iterator it = | 260 ViewIdToWindowMap::const_iterator it = |
189 view_id_to_window_map_.find(params.new_parent->id()); | 261 view_id_to_window_map_.find(params.new_parent->id()); |
190 DCHECK(it != view_id_to_window_map_.end()); | 262 DCHECK(it != view_id_to_window_map_.end()); |
191 RegisterSubtree(params.target->id(), it->second); | 263 RegisterSubtree(params.target, it->second); |
192 } | 264 } |
193 } else if (params.old_parent) { | 265 } else if (params.old_parent) { |
194 UnregisterSubtree(params.target->id()); | 266 UnregisterSubtree(params.target); |
195 } | 267 } |
196 } | 268 } |
197 | 269 |
198 void WindowManagerApp::OnViewDestroyed(View* view) { | 270 void WindowManagerApp::OnViewDestroyed(View* view) { |
271 if (view != root_) | |
272 return; | |
199 root_ = NULL; | 273 root_ = NULL; |
200 STLDeleteValues(&view_id_to_window_map_); | 274 STLDeleteValues(&view_id_to_window_map_); |
201 if (focus_client_.get()) | 275 if (focus_client_.get()) |
202 focus_client_->RemoveObserver(this); | 276 focus_client_->RemoveObserver(this); |
203 if (activation_client_) | 277 if (activation_client_) |
204 activation_client_->RemoveObserver(this); | 278 activation_client_->RemoveObserver(this); |
205 window_tree_host_.reset(); | 279 window_tree_host_.reset(); |
206 } | 280 } |
207 | 281 |
282 void WindowManagerApp::OnViewBoundsChanged(View* view, | |
283 const gfx::Rect& old_bounds, | |
284 const gfx::Rect& new_bounds) { | |
285 aura::Window* window = GetWindowForViewId(view->id()); | |
286 window->SetBounds(new_bounds); | |
287 } | |
288 | |
208 //////////////////////////////////////////////////////////////////////////////// | 289 //////////////////////////////////////////////////////////////////////////////// |
209 // WindowManagerApp, WindowTreeHostMojoDelegate implementation: | 290 // WindowManagerApp, WindowTreeHostMojoDelegate implementation: |
210 | 291 |
211 void WindowManagerApp::CompositorContentsChanged(const SkBitmap& bitmap) { | 292 void WindowManagerApp::CompositorContentsChanged(const SkBitmap& bitmap) { |
212 // We draw nothing. | 293 // We draw nothing. |
213 NOTREACHED(); | 294 NOTREACHED(); |
214 } | 295 } |
215 | 296 |
216 //////////////////////////////////////////////////////////////////////////////// | 297 //////////////////////////////////////////////////////////////////////////////// |
298 // WindowManagerApp, ui::EventHandler implementation: | |
299 | |
300 void WindowManagerApp::OnEvent(ui::Event* event) { | |
301 aura::Window* window = static_cast<aura::Window*>(event->target()); | |
302 view_manager_->DispatchEvent( | |
303 GetViewForWindow(window), | |
304 TypeConverter<EventPtr, ui::Event>::ConvertFrom(*event)); | |
305 } | |
306 | |
307 //////////////////////////////////////////////////////////////////////////////// | |
217 // WindowManagerApp, aura::client::FocusChangeObserver implementation: | 308 // WindowManagerApp, aura::client::FocusChangeObserver implementation: |
218 | 309 |
219 void WindowManagerApp::OnWindowFocused(aura::Window* gained_focus, | 310 void WindowManagerApp::OnWindowFocused(aura::Window* gained_focus, |
220 aura::Window* lost_focus) { | 311 aura::Window* lost_focus) { |
221 for (Connections::const_iterator it = connections_.begin(); | 312 for (Connections::const_iterator it = connections_.begin(); |
222 it != connections_.end(); ++it) { | 313 it != connections_.end(); ++it) { |
223 (*it)->NotifyViewFocused(GetIdForWindow(gained_focus), | 314 (*it)->NotifyViewFocused(GetIdForWindow(gained_focus), |
224 GetIdForWindow(lost_focus)); | 315 GetIdForWindow(lost_focus)); |
225 } | 316 } |
226 } | 317 } |
(...skipping 11 matching lines...) Expand all Loading... | |
238 } | 329 } |
239 | 330 |
240 //////////////////////////////////////////////////////////////////////////////// | 331 //////////////////////////////////////////////////////////////////////////////// |
241 // WindowManagerApp, private: | 332 // WindowManagerApp, private: |
242 | 333 |
243 aura::Window* WindowManagerApp::GetWindowForViewId(Id view) const { | 334 aura::Window* WindowManagerApp::GetWindowForViewId(Id view) const { |
244 ViewIdToWindowMap::const_iterator it = view_id_to_window_map_.find(view); | 335 ViewIdToWindowMap::const_iterator it = view_id_to_window_map_.find(view); |
245 return it != view_id_to_window_map_.end() ? it->second : NULL; | 336 return it != view_id_to_window_map_.end() ? it->second : NULL; |
246 } | 337 } |
247 | 338 |
248 void WindowManagerApp::RegisterSubtree(Id id, | 339 void WindowManagerApp::RegisterSubtree(View* view, aura::Window* parent) { |
249 aura::Window* parent) { | 340 view->AddObserver(this); |
250 View* view = view_manager_->GetViewById(id); | 341 DCHECK(view_id_to_window_map_.find(view->id()) == |
251 DCHECK(view_id_to_window_map_.find(id) == view_id_to_window_map_.end()); | 342 view_id_to_window_map_.end()); |
252 aura::Window* window = new aura::Window(NULL); | 343 aura::Window* window = new aura::Window(dummy_delegate_.get()); |
344 window->set_id(view->id()); | |
253 window->SetProperty(kViewKey, view); | 345 window->SetProperty(kViewKey, view); |
346 // All events pass through the root during dispatch, so we only need a handler | |
347 // installed there. | |
348 if (view == root_) | |
349 window->AddPreTargetHandler(this); | |
sky
2014/08/15 18:03:56
RemovePreTargetHandler in destructor?
| |
254 parent->AddChild(window); | 350 parent->AddChild(window); |
255 view_id_to_window_map_[id] = window; | 351 window->SetBounds(view->bounds()); |
352 window->Show(); | |
353 view_id_to_window_map_[view->id()] = window; | |
256 View::Children::const_iterator it = view->children().begin(); | 354 View::Children::const_iterator it = view->children().begin(); |
257 for (; it != view->children().end(); ++it) | 355 for (; it != view->children().end(); ++it) |
258 RegisterSubtree((*it)->id(), window); | 356 RegisterSubtree(*it, window); |
259 } | 357 } |
260 | 358 |
261 void WindowManagerApp::UnregisterSubtree(Id id) { | 359 void WindowManagerApp::UnregisterSubtree(View* view) { |
262 View* view = view_manager_->GetViewById(id); | 360 view->RemoveObserver(this); |
263 ViewIdToWindowMap::iterator it = view_id_to_window_map_.find(id); | 361 ViewIdToWindowMap::iterator it = view_id_to_window_map_.find(view->id()); |
264 DCHECK(it != view_id_to_window_map_.end()); | 362 DCHECK(it != view_id_to_window_map_.end()); |
265 scoped_ptr<aura::Window> window(it->second); | 363 scoped_ptr<aura::Window> window(it->second); |
266 view_id_to_window_map_.erase(it); | 364 view_id_to_window_map_.erase(it); |
267 View::Children::const_iterator child = view->children().begin(); | 365 View::Children::const_iterator child = view->children().begin(); |
268 for (; child != view->children().end(); ++child) | 366 for (; child != view->children().end(); ++child) |
269 UnregisterSubtree((*child)->id()); | 367 UnregisterSubtree(*child); |
270 } | 368 } |
271 | 369 |
272 } // namespace mojo | 370 } // namespace mojo |
OLD | NEW |