Move StartsWith[ASCII] to base namespace.
[chromium-blink-merge.git] / components / view_manager / connection_manager.cc
blobbcc224d64c8e9f11aef158a865c0548ee1ad54f4
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
3 // found in the LICENSE file.
5 #include "components/view_manager/connection_manager.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "components/view_manager/client_connection.h"
10 #include "components/view_manager/connection_manager_delegate.h"
11 #include "components/view_manager/display_manager.h"
12 #include "components/view_manager/focus_controller.h"
13 #include "components/view_manager/server_view.h"
14 #include "components/view_manager/view_coordinate_conversions.h"
15 #include "components/view_manager/view_manager_service_impl.h"
16 #include "mojo/application/public/interfaces/service_provider.mojom.h"
17 #include "mojo/converters/geometry/geometry_type_converters.h"
18 #include "mojo/converters/input_events/input_events_type_converters.h"
20 using mojo::ConnectionSpecificId;
22 namespace view_manager {
23 namespace {
25 // Creates a copy of |view|. The copied view has |delegate| as its delegate.
26 // This does not recurse.
27 ServerView* CloneView(const ServerView* view, ServerViewDelegate* delegate) {
28 ServerView* clone = new ServerView(delegate, ClonedViewId());
29 clone->SetBounds(view->bounds());
30 clone->SetSurfaceId(view->surface_id());
31 clone->SetOpacity(view->opacity());
32 return clone;
35 // Creates copies of all the visible children of |parent|. Newly cloned views
36 // are added to |cloned_parent| and have |delegate| as their delegate. The
37 // stacking order of the cloned views is preseved.
38 void CloneViewTree(const ServerView* parent,
39 ServerView* cloned_parent,
40 ServerViewDelegate* delegate) {
41 DCHECK(parent->visible());
42 for (const ServerView* to_clone : parent->GetChildren()) {
43 if (to_clone->visible()) {
44 ServerView* cloned = CloneView(to_clone, delegate);
45 cloned_parent->Add(cloned);
46 CloneViewTree(to_clone, cloned, delegate);
51 // Recurses through all the children of |view| moving any cloned views to
52 // |new_parent| stacked above |stack_above|. |stack_above| is updated as views
53 // are moved.
54 void ReparentClonedViews(ServerView* new_parent,
55 ServerView** stack_above,
56 ServerView* view) {
57 if (view->id() == ClonedViewId()) {
58 const gfx::Rect new_bounds(ConvertRectBetweenViews(
59 view, new_parent, gfx::Rect(view->bounds().size())));
60 new_parent->Add(view);
61 new_parent->Reorder(view, *stack_above, mojo::ORDER_DIRECTION_ABOVE);
62 view->SetBounds(new_bounds);
63 *stack_above = view;
64 return;
67 for (ServerView* child : view->GetChildren())
68 ReparentClonedViews(new_parent, stack_above, child);
71 // Deletes |view| and all its descendants.
72 void DeleteViewTree(ServerView* view) {
73 for (ServerView* child : view->GetChildren())
74 DeleteViewTree(child);
76 delete view;
79 // TODO(sky): nuke, proof of concept.
80 bool DecrementAnimatingViewsOpacity(ServerView* view) {
81 if (view->id() == ClonedViewId()) {
82 const float new_opacity = view->opacity() - .05f;
83 if (new_opacity <= 0)
84 DeleteViewTree(view);
85 else
86 view->SetOpacity(new_opacity);
87 return true;
89 bool ret_value = false;
90 for (ServerView* child : view->GetChildren()) {
91 if (DecrementAnimatingViewsOpacity(child))
92 ret_value = true;
94 return ret_value;
97 } // namespace
99 ConnectionManager::ScopedChange::ScopedChange(
100 ViewManagerServiceImpl* connection,
101 ConnectionManager* connection_manager,
102 bool is_delete_view)
103 : connection_manager_(connection_manager),
104 connection_id_(connection->id()),
105 is_delete_view_(is_delete_view) {
106 connection_manager_->PrepareForChange(this);
109 ConnectionManager::ScopedChange::~ScopedChange() {
110 connection_manager_->FinishChange();
113 ConnectionManager::ConnectionManager(ConnectionManagerDelegate* delegate,
114 scoped_ptr<DisplayManager> display_manager)
115 : delegate_(delegate),
116 window_manager_client_connection_(nullptr),
117 next_connection_id_(1),
118 event_dispatcher_(this),
119 display_manager_(display_manager.Pass()),
120 root_(CreateServerView(RootViewId(0))),
121 current_change_(nullptr),
122 in_destructor_(false),
123 animation_runner_(base::TimeTicks::Now()),
124 focus_controller_(new FocusController(this)) {
125 root_->SetBounds(gfx::Rect(800, 600));
126 root_->SetVisible(true);
128 display_manager_->Init(this, &event_dispatcher_);
131 ConnectionManager::~ConnectionManager() {
132 in_destructor_ = true;
134 // Deleting views will attempt to advance focus. When we're being destroyed
135 // that is not necessary. Additionally |focus_controller_| needs to be
136 // destroyed before |root_|.
137 focus_controller_.reset();
139 STLDeleteValues(&connection_map_);
140 // All the connections should have been destroyed.
141 DCHECK(connection_map_.empty());
142 root_.reset();
145 ServerView* ConnectionManager::CreateServerView(const ViewId& id) {
146 ServerView* view = new ServerView(this, id);
147 view->AddObserver(this);
148 return view;
151 ConnectionSpecificId ConnectionManager::GetAndAdvanceNextConnectionId() {
152 const ConnectionSpecificId id = next_connection_id_++;
153 DCHECK_LT(id, next_connection_id_);
154 return id;
157 void ConnectionManager::OnConnectionError(ClientConnection* connection) {
158 if (connection == window_manager_client_connection_) {
159 window_manager_client_connection_ = nullptr;
160 delegate_->OnLostConnectionToWindowManager();
161 // Assume we've been destroyed.
162 return;
165 scoped_ptr<ClientConnection> connection_owner(connection);
167 connection_map_.erase(connection->service()->id());
169 // TODO(sky): I may want to advance focus differently if focus is in
170 // |connection|.
172 // Notify remaining connections so that they can cleanup.
173 for (auto& pair : connection_map_) {
174 pair.second->service()->OnWillDestroyViewManagerServiceImpl(
175 connection->service());
179 void ConnectionManager::EmbedAtView(mojo::ConnectionSpecificId creator_id,
180 const ViewId& view_id,
181 mojo::URLRequestPtr request) {
182 mojo::ViewManagerServicePtr service_ptr;
183 ClientConnection* client_connection =
184 delegate_->CreateClientConnectionForEmbedAtView(
185 this, GetProxy(&service_ptr), creator_id, request.Pass(), view_id);
186 AddConnection(client_connection);
187 client_connection->service()->Init(client_connection->client(),
188 service_ptr.Pass());
189 OnConnectionMessagedClient(client_connection->service()->id());
192 void ConnectionManager::EmbedAtView(mojo::ConnectionSpecificId creator_id,
193 const ViewId& view_id,
194 mojo::ViewManagerClientPtr client) {
195 mojo::ViewManagerServicePtr service_ptr;
196 ClientConnection* client_connection =
197 delegate_->CreateClientConnectionForEmbedAtView(
198 this, GetProxy(&service_ptr), creator_id, view_id, client.Pass());
199 AddConnection(client_connection);
200 client_connection->service()->Init(client_connection->client(),
201 service_ptr.Pass());
202 OnConnectionMessagedClient(client_connection->service()->id());
205 ViewManagerServiceImpl* ConnectionManager::GetConnection(
206 ConnectionSpecificId connection_id) {
207 ConnectionMap::iterator i = connection_map_.find(connection_id);
208 return i == connection_map_.end() ? nullptr : i->second->service();
211 ServerView* ConnectionManager::GetView(const ViewId& id) {
212 if (id == root_->id())
213 return root_.get();
214 ViewManagerServiceImpl* service = GetConnection(id.connection_id);
215 return service ? service->GetView(id) : nullptr;
218 void ConnectionManager::SetFocusedView(ServerView* view) {
219 ServerView* old_focused = GetFocusedView();
220 if (old_focused == view)
221 return;
222 focus_controller_->SetFocusedView(view);
223 OnFocusChanged(old_focused, view);
226 ServerView* ConnectionManager::GetFocusedView() {
227 return focus_controller_->GetFocusedView();
230 bool ConnectionManager::IsViewAttachedToRoot(const ServerView* view) const {
231 // TODO(fsamuel): Iterate over all roots once we support multiple roots.
232 return root_->Contains(view) && view != root_.get();
235 void ConnectionManager::OnConnectionMessagedClient(ConnectionSpecificId id) {
236 if (current_change_)
237 current_change_->MarkConnectionAsMessaged(id);
240 bool ConnectionManager::DidConnectionMessageClient(
241 ConnectionSpecificId id) const {
242 return current_change_ && current_change_->DidMessageConnection(id);
245 const ViewManagerServiceImpl* ConnectionManager::GetConnectionWithRoot(
246 const ViewId& id) const {
247 for (auto& pair : connection_map_) {
248 if (pair.second->service()->IsRoot(id))
249 return pair.second->service();
251 return nullptr;
254 ViewManagerServiceImpl* ConnectionManager::GetEmbedRoot(
255 ViewManagerServiceImpl* service) {
256 while (service) {
257 const ViewId* root_id = service->root();
258 if (!root_id || root_id->connection_id == service->id())
259 return nullptr;
261 ViewManagerServiceImpl* parent_service =
262 GetConnection(root_id->connection_id);
263 service = parent_service;
264 if (service && service->is_embed_root())
265 return service;
267 return nullptr;
270 void ConnectionManager::SetWindowManagerClientConnection(
271 scoped_ptr<ClientConnection> connection) {
272 CHECK(!window_manager_client_connection_);
273 window_manager_client_connection_ = connection.release();
274 AddConnection(window_manager_client_connection_);
275 window_manager_client_connection_->service()->Init(
276 window_manager_client_connection_->client(), nullptr);
279 mojo::ViewManagerClient*
280 ConnectionManager::GetWindowManagerViewManagerClient() {
281 CHECK(window_manager_client_connection_);
282 return window_manager_client_connection_->client();
285 bool ConnectionManager::CloneAndAnimate(const ViewId& view_id) {
286 ServerView* view = GetView(view_id);
287 if (!view || !view->IsDrawn() || view == root_.get())
288 return false;
289 if (!animation_timer_.IsRunning()) {
290 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(100),
291 this, &ConnectionManager::DoAnimation);
293 ServerView* clone = CloneView(view, this);
294 CloneViewTree(view, clone, this);
295 view->parent()->Add(clone);
296 view->parent()->Reorder(clone, view, mojo::ORDER_DIRECTION_ABOVE);
297 return true;
300 void ConnectionManager::ProcessEvent(mojo::EventPtr event) {
301 event_dispatcher_.OnEvent(event.Pass());
304 void ConnectionManager::DispatchInputEventToView(const ServerView* view,
305 mojo::EventPtr event) {
306 // It's possible for events to flow through here from the platform_window
307 // before any connections are established with the view_manager.
308 if (!has_window_manager_client_connection())
309 return;
311 // If the view is an embed root, forward to the embedded view, not the owner.
312 ViewManagerServiceImpl* connection = GetConnectionWithRoot(view->id());
313 if (!connection)
314 connection = GetConnection(view->id().connection_id);
315 CHECK(connection);
316 connection->client()->OnViewInputEvent(ViewIdToTransportId(view->id()),
317 event.Pass(),
318 base::Bind(&base::DoNothing));
321 void ConnectionManager::ProcessViewBoundsChanged(const ServerView* view,
322 const gfx::Rect& old_bounds,
323 const gfx::Rect& new_bounds) {
324 for (auto& pair : connection_map_) {
325 pair.second->service()->ProcessViewBoundsChanged(
326 view, old_bounds, new_bounds, IsChangeSource(pair.first));
330 void ConnectionManager::ProcessViewportMetricsChanged(
331 const mojo::ViewportMetrics& old_metrics,
332 const mojo::ViewportMetrics& new_metrics) {
333 for (auto& pair : connection_map_) {
334 pair.second->service()->ProcessViewportMetricsChanged(
335 old_metrics, new_metrics, IsChangeSource(pair.first));
339 void ConnectionManager::ProcessWillChangeViewHierarchy(
340 const ServerView* view,
341 const ServerView* new_parent,
342 const ServerView* old_parent) {
343 for (auto& pair : connection_map_) {
344 pair.second->service()->ProcessWillChangeViewHierarchy(
345 view, new_parent, old_parent, IsChangeSource(pair.first));
349 void ConnectionManager::ProcessViewHierarchyChanged(
350 const ServerView* view,
351 const ServerView* new_parent,
352 const ServerView* old_parent) {
353 for (auto& pair : connection_map_) {
354 pair.second->service()->ProcessViewHierarchyChanged(
355 view, new_parent, old_parent, IsChangeSource(pair.first));
359 void ConnectionManager::ProcessViewReorder(
360 const ServerView* view,
361 const ServerView* relative_view,
362 const mojo::OrderDirection direction) {
363 for (auto& pair : connection_map_) {
364 pair.second->service()->ProcessViewReorder(view, relative_view, direction,
365 IsChangeSource(pair.first));
369 void ConnectionManager::ProcessViewDeleted(const ViewId& view) {
370 for (auto& pair : connection_map_) {
371 pair.second->service()->ProcessViewDeleted(view,
372 IsChangeSource(pair.first));
376 void ConnectionManager::PrepareForChange(ScopedChange* change) {
377 // Should only ever have one change in flight.
378 CHECK(!current_change_);
379 current_change_ = change;
382 void ConnectionManager::FinishChange() {
383 // PrepareForChange/FinishChange should be balanced.
384 CHECK(current_change_);
385 current_change_ = NULL;
388 void ConnectionManager::DoAnimation() {
389 if (!DecrementAnimatingViewsOpacity(root()))
390 animation_timer_.Stop();
393 void ConnectionManager::AddConnection(ClientConnection* connection) {
394 DCHECK_EQ(0u, connection_map_.count(connection->service()->id()));
395 connection_map_[connection->service()->id()] = connection;
398 void ConnectionManager::PrepareToDestroyView(ServerView* view) {
399 if (!in_destructor_ && IsViewAttachedToRoot(view) &&
400 view->id() != ClonedViewId()) {
401 // We're about to destroy a view. Any cloned views need to be reparented
402 // else the animation would no longer be visible. By moving to a visible
403 // view, view->parent(), we ensure the animation is still visible.
404 ServerView* parent_above = view;
405 ReparentClonedViews(view->parent(), &parent_above, view);
408 animation_runner_.CancelAnimationForView(view);
411 void ConnectionManager::PrepareToChangeViewHierarchy(ServerView* view,
412 ServerView* new_parent,
413 ServerView* old_parent) {
414 if (view->id() == ClonedViewId() || in_destructor_)
415 return;
417 if (IsViewAttachedToRoot(view)) {
418 // We're about to reparent a view. Any cloned views need to be reparented
419 // else the animation may be effected in unusual ways. For example, the view
420 // could move to a new location such that the animation is entirely clipped.
421 // By moving to view->parent() we ensure the animation is still visible.
422 ServerView* parent_above = view;
423 ReparentClonedViews(view->parent(), &parent_above, view);
426 animation_runner_.CancelAnimationForView(view);
429 void ConnectionManager::PrepareToChangeViewVisibility(ServerView* view) {
430 if (in_destructor_)
431 return;
433 if (IsViewAttachedToRoot(view) && view->id() != ClonedViewId() &&
434 view->IsDrawn()) {
435 // We're about to hide |view|, this would implicitly make any cloned views
436 // hide too. Reparent so that animations are still visible.
437 ServerView* parent_above = view;
438 ReparentClonedViews(view->parent(), &parent_above, view);
441 const bool is_parent_drawn = view->parent() && view->parent()->IsDrawn();
442 if (!is_parent_drawn || !view->visible())
443 animation_runner_.CancelAnimationForView(view);
446 void ConnectionManager::OnScheduleViewPaint(const ServerView* view) {
447 if (!in_destructor_)
448 display_manager_->SchedulePaint(view, gfx::Rect(view->bounds().size()));
451 bool ConnectionManager::IsViewDrawn(const ServerView* view) const {
452 // TODO(fsamuel): Iterate over all roots once we support multiple roots.
453 if (!root_->visible())
454 return false;
455 while (view && view != root_.get() && view->visible())
456 view = view->parent();
457 return view == root_.get();
460 void ConnectionManager::OnViewDestroyed(ServerView* view) {
461 if (!in_destructor_)
462 ProcessViewDeleted(view->id());
465 void ConnectionManager::OnWillChangeViewHierarchy(ServerView* view,
466 ServerView* new_parent,
467 ServerView* old_parent) {
468 if (view->id() == ClonedViewId() || in_destructor_)
469 return;
471 ProcessWillChangeViewHierarchy(view, new_parent, old_parent);
474 void ConnectionManager::OnViewHierarchyChanged(ServerView* view,
475 ServerView* new_parent,
476 ServerView* old_parent) {
477 if (in_destructor_)
478 return;
480 ProcessViewHierarchyChanged(view, new_parent, old_parent);
482 // TODO(beng): optimize.
483 if (old_parent) {
484 display_manager_->SchedulePaint(old_parent,
485 gfx::Rect(old_parent->bounds().size()));
487 if (new_parent) {
488 display_manager_->SchedulePaint(new_parent,
489 gfx::Rect(new_parent->bounds().size()));
493 void ConnectionManager::OnViewBoundsChanged(ServerView* view,
494 const gfx::Rect& old_bounds,
495 const gfx::Rect& new_bounds) {
496 if (in_destructor_)
497 return;
499 ProcessViewBoundsChanged(view, old_bounds, new_bounds);
500 if (!view->parent())
501 return;
503 // TODO(sky): optimize this.
504 display_manager_->SchedulePaint(view->parent(), old_bounds);
505 display_manager_->SchedulePaint(view->parent(), new_bounds);
508 void ConnectionManager::OnViewReordered(ServerView* view,
509 ServerView* relative,
510 mojo::OrderDirection direction) {
511 if (!in_destructor_)
512 display_manager_->SchedulePaint(view, gfx::Rect(view->bounds().size()));
515 void ConnectionManager::OnWillChangeViewVisibility(ServerView* view) {
516 if (in_destructor_)
517 return;
519 // Need to repaint if the view was drawn (which means it's in the process of
520 // hiding) or the view is transitioning to drawn.
521 if (view->parent() && (view->IsDrawn() ||
522 (!view->visible() && view->parent()->IsDrawn()))) {
523 display_manager_->SchedulePaint(view->parent(), view->bounds());
526 for (auto& pair : connection_map_) {
527 pair.second->service()->ProcessWillChangeViewVisibility(
528 view, IsChangeSource(pair.first));
532 void ConnectionManager::OnViewSharedPropertyChanged(
533 ServerView* view,
534 const std::string& name,
535 const std::vector<uint8_t>* new_data) {
536 for (auto& pair : connection_map_) {
537 pair.second->service()->ProcessViewPropertyChanged(
538 view, name, new_data, IsChangeSource(pair.first));
542 void ConnectionManager::SetViewManagerRootClient(
543 mojo::ViewManagerRootClientPtr client) {
544 view_manager_root_client_ = client.Pass();
547 void ConnectionManager::SetViewportSize(mojo::SizePtr size) {
548 display_manager_->SetViewportSize(size.To<gfx::Size>());
551 void ConnectionManager::CloneAndAnimate(mojo::Id transport_view_id) {
552 CloneAndAnimate(ViewIdFromTransportId(transport_view_id));
555 void ConnectionManager::AddAccelerator(mojo::KeyboardCode keyboard_code,
556 mojo::EventFlags flags) {
557 event_dispatcher_.AddAccelerator(keyboard_code, flags);
560 void ConnectionManager::RemoveAccelerator(mojo::KeyboardCode keyboard_code,
561 mojo::EventFlags flags) {
562 event_dispatcher_.RemoveAccelerator(keyboard_code, flags);
565 void ConnectionManager::OnFocusChanged(ServerView* old_focused_view,
566 ServerView* new_focused_view) {
567 // There are up to four connections that need to be notified:
568 // . the connection containing |old_focused_view|.
569 // . the connection with |old_focused_view| as its root.
570 // . the connection containing |new_focused_view|.
571 // . the connection with |new_focused_view| as its root.
572 // Some of these connections may be the same. The following takes care to
573 // notify each only once.
574 ViewManagerServiceImpl* owning_connection_old = nullptr;
575 ViewManagerServiceImpl* embedded_connection_old = nullptr;
577 if (old_focused_view) {
578 owning_connection_old = GetConnection(old_focused_view->id().connection_id);
579 if (owning_connection_old) {
580 owning_connection_old->ProcessFocusChanged(old_focused_view,
581 new_focused_view);
583 embedded_connection_old = GetConnectionWithRoot(old_focused_view->id());
584 if (embedded_connection_old) {
585 DCHECK_NE(owning_connection_old, embedded_connection_old);
586 embedded_connection_old->ProcessFocusChanged(old_focused_view,
587 new_focused_view);
590 ViewManagerServiceImpl* owning_connection_new = nullptr;
591 ViewManagerServiceImpl* embedded_connection_new = nullptr;
592 if (new_focused_view) {
593 owning_connection_new = GetConnection(new_focused_view->id().connection_id);
594 if (owning_connection_new &&
595 owning_connection_new != owning_connection_old &&
596 owning_connection_new != embedded_connection_old) {
597 owning_connection_new->ProcessFocusChanged(old_focused_view,
598 new_focused_view);
600 embedded_connection_new = GetConnectionWithRoot(new_focused_view->id());
601 if (embedded_connection_new &&
602 embedded_connection_new != owning_connection_old &&
603 embedded_connection_new != embedded_connection_old) {
604 DCHECK_NE(owning_connection_new, embedded_connection_new);
605 embedded_connection_new->ProcessFocusChanged(old_focused_view,
606 new_focused_view);
610 if (has_window_manager_client_connection()) {
611 // Window manager should always be notified of focus change.
612 ViewManagerServiceImpl* wm_connection =
613 window_manager_client_connection_->service();
614 if (wm_connection != owning_connection_old &&
615 wm_connection != embedded_connection_old &&
616 wm_connection != owning_connection_new &&
617 wm_connection != embedded_connection_new) {
618 wm_connection->ProcessFocusChanged(old_focused_view, new_focused_view);
623 } // namespace view_manager