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/public/cpp/view_manager/lib/view_manager_client_impl.h" | 5 #include "mojo/services/public/cpp/view_manager/lib/view_manager_client_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/stl_util.h" |
9 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
10 #include "mojo/public/cpp/application/connect.h" | 11 #include "mojo/public/cpp/application/connect.h" |
11 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" | 12 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" |
12 #include "mojo/services/public/cpp/view_manager/lib/node_private.h" | 13 #include "mojo/services/public/cpp/view_manager/lib/node_private.h" |
13 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" | 14 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" |
14 #include "mojo/services/public/cpp/view_manager/node_observer.h" | 15 #include "mojo/services/public/cpp/view_manager/node_observer.h" |
15 #include "mojo/services/public/cpp/view_manager/util.h" | 16 #include "mojo/services/public/cpp/view_manager/util.h" |
16 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" |
17 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 18 #include "mojo/services/public/cpp/view_manager/view_observer.h" |
18 #include "mojo/services/public/cpp/view_manager/window_manager_delegate.h" | 19 #include "mojo/services/public/cpp/view_manager/window_manager_delegate.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 private_view.set_node(node); | 53 private_view.set_node(node); |
53 // TODO(beng): this broadcasts notifications locally... do we want this? I | 54 // TODO(beng): this broadcasts notifications locally... do we want this? I |
54 // don't think so. same story for LocalAddChild above! | 55 // don't think so. same story for LocalAddChild above! |
55 private_node.LocalSetActiveView(view); | 56 private_node.LocalSetActiveView(view); |
56 client->AddView(view); | 57 client->AddView(view); |
57 } | 58 } |
58 return node; | 59 return node; |
59 } | 60 } |
60 | 61 |
61 Node* BuildNodeTree(ViewManagerClientImpl* client, | 62 Node* BuildNodeTree(ViewManagerClientImpl* client, |
62 const Array<NodeDataPtr>& nodes) { | 63 const Array<NodeDataPtr>& nodes, |
| 64 Node* initial_parent) { |
63 std::vector<Node*> parents; | 65 std::vector<Node*> parents; |
64 Node* root = NULL; | 66 Node* root = NULL; |
65 Node* last_node = NULL; | 67 Node* last_node = NULL; |
| 68 if (initial_parent) |
| 69 parents.push_back(initial_parent); |
66 for (size_t i = 0; i < nodes.size(); ++i) { | 70 for (size_t i = 0; i < nodes.size(); ++i) { |
67 if (last_node && nodes[i]->parent_id == last_node->id()) { | 71 if (last_node && nodes[i]->parent_id == last_node->id()) { |
68 parents.push_back(last_node); | 72 parents.push_back(last_node); |
69 } else if (!parents.empty()) { | 73 } else if (!parents.empty()) { |
70 while (parents.back()->id() != nodes[i]->parent_id) | 74 while (parents.back()->id() != nodes[i]->parent_id) |
71 parents.pop_back(); | 75 parents.pop_back(); |
72 } | 76 } |
73 Node* node = AddNodeToViewManager( | 77 Node* node = AddNodeToViewManager( |
74 client, | 78 client, |
75 !parents.empty() ? parents.back() : NULL, | 79 !parents.empty() ? parents.back() : NULL, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 | 134 |
131 ViewManagerClientImpl::ViewManagerClientImpl(ViewManagerDelegate* delegate) | 135 ViewManagerClientImpl::ViewManagerClientImpl(ViewManagerDelegate* delegate) |
132 : connected_(false), | 136 : connected_(false), |
133 connection_id_(0), | 137 connection_id_(0), |
134 next_id_(1), | 138 next_id_(1), |
135 delegate_(delegate), | 139 delegate_(delegate), |
136 window_manager_delegate_(NULL) { | 140 window_manager_delegate_(NULL) { |
137 } | 141 } |
138 | 142 |
139 ViewManagerClientImpl::~ViewManagerClientImpl() { | 143 ViewManagerClientImpl::~ViewManagerClientImpl() { |
| 144 std::vector<Node*> non_owned; |
140 while (!nodes_.empty()) { | 145 while (!nodes_.empty()) { |
141 IdToNodeMap::iterator it = nodes_.begin(); | 146 IdToNodeMap::iterator it = nodes_.begin(); |
142 if (OwnsNode(it->second->id())) | 147 if (OwnsNode(it->second->id())) { |
143 it->second->Destroy(); | 148 it->second->Destroy(); |
144 else | 149 } else { |
| 150 non_owned.push_back(it->second); |
145 nodes_.erase(it); | 151 nodes_.erase(it); |
| 152 } |
146 } | 153 } |
147 while (!views_.empty()) { | 154 while (!views_.empty()) { |
148 IdToViewMap::iterator it = views_.begin(); | 155 IdToViewMap::iterator it = views_.begin(); |
149 if (OwnsView(it->second->id())) | 156 if (OwnsView(it->second->id())) |
150 it->second->Destroy(); | 157 it->second->Destroy(); |
151 else | 158 else |
152 views_.erase(it); | 159 views_.erase(it); |
153 } | 160 } |
| 161 // Delete the non-owned nodes last. In the typical case these are roots. The |
| 162 // exception is the window manager, which may know aboutother random nodes |
| 163 // that it doesn't own. |
| 164 // NOTE: we manually delete as we're a friend. |
| 165 for (size_t i = 0; i < non_owned.size(); ++i) |
| 166 delete non_owned[i]; |
154 delegate_->OnViewManagerDisconnected(this); | 167 delegate_->OnViewManagerDisconnected(this); |
155 } | 168 } |
156 | 169 |
157 Id ViewManagerClientImpl::CreateNode() { | 170 Id ViewManagerClientImpl::CreateNode() { |
158 DCHECK(connected_); | 171 DCHECK(connected_); |
159 const Id node_id = MakeTransportId(connection_id_, ++next_id_); | 172 const Id node_id = MakeTransportId(connection_id_, ++next_id_); |
160 service_->CreateNode(node_id, ActionCompletedCallbackWithErrorCode()); | 173 service_->CreateNode(node_id, ActionCompletedCallbackWithErrorCode()); |
161 return node_id; | 174 return node_id; |
162 } | 175 } |
163 | 176 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 //////////////////////////////////////////////////////////////////////////////// | 330 //////////////////////////////////////////////////////////////////////////////// |
318 // ViewManagerClientImpl, ViewManagerClient implementation: | 331 // ViewManagerClientImpl, ViewManagerClient implementation: |
319 | 332 |
320 void ViewManagerClientImpl::OnViewManagerConnectionEstablished( | 333 void ViewManagerClientImpl::OnViewManagerConnectionEstablished( |
321 ConnectionSpecificId connection_id, | 334 ConnectionSpecificId connection_id, |
322 const String& creator_url, | 335 const String& creator_url, |
323 Array<NodeDataPtr> nodes) { | 336 Array<NodeDataPtr> nodes) { |
324 connected_ = true; | 337 connected_ = true; |
325 connection_id_ = connection_id; | 338 connection_id_ = connection_id; |
326 creator_url_ = TypeConverter<String, std::string>::ConvertFrom(creator_url); | 339 creator_url_ = TypeConverter<String, std::string>::ConvertFrom(creator_url); |
327 AddRoot(BuildNodeTree(this, nodes)); | 340 AddRoot(BuildNodeTree(this, nodes, NULL)); |
328 } | 341 } |
329 | 342 |
330 void ViewManagerClientImpl::OnRootAdded(Array<NodeDataPtr> nodes) { | 343 void ViewManagerClientImpl::OnRootAdded(Array<NodeDataPtr> nodes) { |
331 AddRoot(BuildNodeTree(this, nodes)); | 344 AddRoot(BuildNodeTree(this, nodes, NULL)); |
332 } | 345 } |
333 | 346 |
334 void ViewManagerClientImpl::OnNodeBoundsChanged(Id node_id, | 347 void ViewManagerClientImpl::OnNodeBoundsChanged(Id node_id, |
335 RectPtr old_bounds, | 348 RectPtr old_bounds, |
336 RectPtr new_bounds) { | 349 RectPtr new_bounds) { |
337 Node* node = GetNodeById(node_id); | 350 Node* node = GetNodeById(node_id); |
338 NodePrivate(node).LocalSetBounds(old_bounds.To<gfx::Rect>(), | 351 NodePrivate(node).LocalSetBounds(old_bounds.To<gfx::Rect>(), |
339 new_bounds.To<gfx::Rect>()); | 352 new_bounds.To<gfx::Rect>()); |
340 } | 353 } |
341 | 354 |
342 void ViewManagerClientImpl::OnNodeHierarchyChanged( | 355 void ViewManagerClientImpl::OnNodeHierarchyChanged( |
343 Id node_id, | 356 Id node_id, |
344 Id new_parent_id, | 357 Id new_parent_id, |
345 Id old_parent_id, | 358 Id old_parent_id, |
346 mojo::Array<NodeDataPtr> nodes) { | 359 mojo::Array<NodeDataPtr> nodes) { |
347 BuildNodeTree(this, nodes); | 360 Node* initial_parent = nodes.size() ? |
| 361 GetNodeById(nodes[0]->parent_id) : NULL; |
| 362 |
| 363 BuildNodeTree(this, nodes, initial_parent); |
348 | 364 |
349 Node* new_parent = GetNodeById(new_parent_id); | 365 Node* new_parent = GetNodeById(new_parent_id); |
350 Node* old_parent = GetNodeById(old_parent_id); | 366 Node* old_parent = GetNodeById(old_parent_id); |
351 Node* node = GetNodeById(node_id); | 367 Node* node = GetNodeById(node_id); |
352 if (new_parent) | 368 if (new_parent) |
353 NodePrivate(new_parent).LocalAddChild(node); | 369 NodePrivate(new_parent).LocalAddChild(node); |
354 else | 370 else |
355 NodePrivate(old_parent).LocalRemoveChild(node); | 371 NodePrivate(old_parent).LocalRemoveChild(node); |
356 } | 372 } |
357 | 373 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
470 base::Unretained(this)); | 486 base::Unretained(this)); |
471 } | 487 } |
472 | 488 |
473 base::Callback<void(ErrorCode)> | 489 base::Callback<void(ErrorCode)> |
474 ViewManagerClientImpl::ActionCompletedCallbackWithErrorCode() { | 490 ViewManagerClientImpl::ActionCompletedCallbackWithErrorCode() { |
475 return base::Bind(&ViewManagerClientImpl::OnActionCompletedWithErrorCode, | 491 return base::Bind(&ViewManagerClientImpl::OnActionCompletedWithErrorCode, |
476 base::Unretained(this)); | 492 base::Unretained(this)); |
477 } | 493 } |
478 | 494 |
479 } // namespace mojo | 495 } // namespace mojo |
OLD | NEW |