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/view_manager/view_manager_connection.h" | 5 #include "mojo/services/view_manager/view_manager_connection.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "mojo/public/cpp/bindings/allocation_scope.h" | 8 #include "mojo/public/cpp/bindings/allocation_scope.h" |
9 #include "mojo/services/view_manager/node.h" | 9 #include "mojo/services/view_manager/node.h" |
10 #include "mojo/services/view_manager/root_node_manager.h" | 10 #include "mojo/services/view_manager/root_node_manager.h" |
11 #include "mojo/services/view_manager/type_converters.h" | 11 #include "mojo/services/view_manager/type_converters.h" |
12 #include "mojo/services/view_manager/view.h" | 12 #include "mojo/services/view_manager/view.h" |
13 #include "third_party/skia/include/core/SkBitmap.h" | 13 #include "third_party/skia/include/core/SkBitmap.h" |
14 #include "ui/aura/window.h" | 14 #include "ui/aura/window.h" |
15 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
16 | 16 |
17 namespace mojo { | 17 namespace mojo { |
18 namespace view_manager { | 18 namespace view_manager { |
19 namespace service { | 19 namespace service { |
20 namespace { | 20 namespace { |
21 | 21 |
22 // Places |node| in |nodes| and recurses through the children. | 22 // Places |node| in |nodes| and recurses through the children. |
23 void GetDescendants(Node* node, std::vector<Node*>* nodes) { | 23 void GetDescendants(const Node* node, std::vector<const Node*>* nodes) { |
24 if (!node) | 24 if (!node) |
25 return; | 25 return; |
26 | 26 |
27 nodes->push_back(node); | 27 nodes->push_back(node); |
28 | 28 |
29 std::vector<Node*> children(node->GetChildren()); | 29 std::vector<const Node*> children(node->GetChildren()); |
30 for (size_t i = 0 ; i < children.size(); ++i) | 30 for (size_t i = 0 ; i < children.size(); ++i) |
31 GetDescendants(children[i], nodes); | 31 GetDescendants(children[i], nodes); |
32 } | 32 } |
33 | 33 |
34 } // namespace | 34 } // namespace |
35 | 35 |
36 ViewManagerConnection::ViewManagerConnection(RootNodeManager* root_node_manager) | 36 ViewManagerConnection::ViewManagerConnection(RootNodeManager* root_node_manager) |
37 : root_node_manager_(root_node_manager), | 37 : root_node_manager_(root_node_manager), |
38 id_(0) { | 38 id_(0) { |
39 } | 39 } |
(...skipping 14 matching lines...) Expand all Loading... |
54 } | 54 } |
55 | 55 |
56 STLDeleteContainerPairSecondPointers(node_map_.begin(), node_map_.end()); | 56 STLDeleteContainerPairSecondPointers(node_map_.begin(), node_map_.end()); |
57 root_node_manager_->RemoveConnection(this); | 57 root_node_manager_->RemoveConnection(this); |
58 } | 58 } |
59 | 59 |
60 void ViewManagerConnection::OnConnectionEstablished() { | 60 void ViewManagerConnection::OnConnectionEstablished() { |
61 DCHECK_EQ(0, id_); // Should only get OnConnectionEstablished() once. | 61 DCHECK_EQ(0, id_); // Should only get OnConnectionEstablished() once. |
62 id_ = root_node_manager_->GetAndAdvanceNextConnectionId(); | 62 id_ = root_node_manager_->GetAndAdvanceNextConnectionId(); |
63 root_node_manager_->AddConnection(this); | 63 root_node_manager_->AddConnection(this); |
64 std::vector<Node*> to_send; | 64 std::vector<const Node*> to_send; |
65 GetUnknownNodesFrom(root_node_manager_->root(), &to_send); | 65 GetUnknownNodesFrom(root_node_manager_->root(), &to_send); |
66 AllocationScope allocation_scope; | 66 AllocationScope allocation_scope; |
67 client()->OnConnectionEstablished( | 67 client()->OnConnectionEstablished( |
68 id_, | 68 id_, |
69 root_node_manager_->next_server_change_id(), | 69 root_node_manager_->next_server_change_id(), |
70 Array<INode>::From(to_send)); | 70 Array<INode>::From(to_send)); |
71 } | 71 } |
72 | 72 |
73 void ViewManagerConnection::OnConnectionError() {} | 73 void ViewManagerConnection::OnConnectionError() {} |
74 | 74 |
75 Node* ViewManagerConnection::GetNode(const NodeId& id) { | 75 Node* ViewManagerConnection::GetNode(const NodeId& id) { |
76 if (id_ == id.connection_id) { | 76 if (id_ == id.connection_id) { |
77 NodeMap::iterator i = node_map_.find(id.node_id); | 77 NodeMap::iterator i = node_map_.find(id.node_id); |
78 return i == node_map_.end() ? NULL : i->second; | 78 return i == node_map_.end() ? NULL : i->second; |
79 } | 79 } |
80 return root_node_manager_->GetNode(id); | 80 return root_node_manager_->GetNode(id); |
81 } | 81 } |
82 | 82 |
83 View* ViewManagerConnection::GetView(const ViewId& id) { | 83 View* ViewManagerConnection::GetView(const ViewId& id) { |
84 if (id_ == id.connection_id) { | 84 if (id_ == id.connection_id) { |
85 ViewMap::const_iterator i = view_map_.find(id.view_id); | 85 ViewMap::const_iterator i = view_map_.find(id.view_id); |
86 return i == view_map_.end() ? NULL : i->second; | 86 return i == view_map_.end() ? NULL : i->second; |
87 } | 87 } |
88 return root_node_manager_->GetView(id); | 88 return root_node_manager_->GetView(id); |
89 } | 89 } |
90 | 90 |
91 void ViewManagerConnection::ProcessNodeHierarchyChanged( | 91 void ViewManagerConnection::ProcessNodeHierarchyChanged( |
92 const NodeId& node_id, | 92 const Node* node, |
93 const NodeId& new_parent_id, | 93 const Node* new_parent, |
94 const NodeId& old_parent_id, | 94 const Node* old_parent, |
95 TransportChangeId server_change_id, | 95 TransportChangeId server_change_id, |
96 bool originated_change) { | 96 bool originated_change) { |
97 if (originated_change || root_node_manager_->is_processing_delete_node()) | 97 if (originated_change || root_node_manager_->is_processing_delete_node()) |
98 return; | 98 return; |
99 std::vector<Node*> to_send; | 99 std::vector<const Node*> to_send; |
100 if (!ShouldNotifyOnHierarchyChange(node_id, new_parent_id, old_parent_id, | 100 if (!ShouldNotifyOnHierarchyChange(node, new_parent, old_parent, &to_send)) { |
101 &to_send)) { | |
102 if (root_node_manager_->IsProcessingChange()) { | 101 if (root_node_manager_->IsProcessingChange()) { |
103 client()->OnServerChangeIdAdvanced( | 102 client()->OnServerChangeIdAdvanced( |
104 root_node_manager_->next_server_change_id() + 1); | 103 root_node_manager_->next_server_change_id() + 1); |
105 } | 104 } |
106 return; | 105 return; |
107 } | 106 } |
108 AllocationScope allocation_scope; | 107 AllocationScope allocation_scope; |
109 client()->OnNodeHierarchyChanged(NodeIdToTransportId(node_id), | 108 const NodeId new_parent_id(new_parent ? new_parent->id() : NodeId()); |
| 109 const NodeId old_parent_id(old_parent ? old_parent->id() : NodeId()); |
| 110 client()->OnNodeHierarchyChanged(NodeIdToTransportId(node->id()), |
110 NodeIdToTransportId(new_parent_id), | 111 NodeIdToTransportId(new_parent_id), |
111 NodeIdToTransportId(old_parent_id), | 112 NodeIdToTransportId(old_parent_id), |
112 server_change_id, | 113 server_change_id, |
113 Array<INode>::From(to_send)); | 114 Array<INode>::From(to_send)); |
114 } | 115 } |
115 | 116 |
116 void ViewManagerConnection::ProcessNodeViewReplaced( | 117 void ViewManagerConnection::ProcessNodeViewReplaced( |
117 const NodeId& node, | 118 const Node* node, |
118 const ViewId& new_view_id, | 119 const View* new_view, |
119 const ViewId& old_view_id, | 120 const View* old_view, |
120 bool originated_change) { | 121 bool originated_change) { |
121 if (originated_change) | 122 if (originated_change) |
122 return; | 123 return; |
123 client()->OnNodeViewReplaced(NodeIdToTransportId(node), | 124 const TransportViewId new_view_id = new_view ? |
124 ViewIdToTransportId(new_view_id), | 125 ViewIdToTransportId(new_view->id()) : 0; |
125 ViewIdToTransportId(old_view_id)); | 126 const TransportViewId old_view_id = old_view ? |
| 127 ViewIdToTransportId(old_view->id()) : 0; |
| 128 client()->OnNodeViewReplaced(NodeIdToTransportId(node->id()), |
| 129 new_view_id, old_view_id); |
126 } | 130 } |
127 | 131 |
128 void ViewManagerConnection::ProcessNodeDeleted( | 132 void ViewManagerConnection::ProcessNodeDeleted( |
129 const NodeId& node, | 133 const NodeId& node, |
130 TransportChangeId server_change_id, | 134 TransportChangeId server_change_id, |
131 bool originated_change) { | 135 bool originated_change) { |
132 const bool in_known = known_nodes_.erase(NodeIdToTransportId(node)) > 0; | 136 const bool in_known = known_nodes_.erase(NodeIdToTransportId(node)) > 0; |
133 | 137 |
134 if (originated_change) | 138 if (originated_change) |
135 return; | 139 return; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 if (!view && view_id != ViewId()) | 204 if (!view && view_id != ViewId()) |
201 return false; | 205 return false; |
202 RootNodeManager::ScopedChange change( | 206 RootNodeManager::ScopedChange change( |
203 this, root_node_manager_, | 207 this, root_node_manager_, |
204 RootNodeManager::CHANGE_TYPE_DONT_ADVANCE_SERVER_CHANGE_ID, false); | 208 RootNodeManager::CHANGE_TYPE_DONT_ADVANCE_SERVER_CHANGE_ID, false); |
205 node->SetView(view); | 209 node->SetView(view); |
206 return true; | 210 return true; |
207 } | 211 } |
208 | 212 |
209 void ViewManagerConnection::GetUnknownNodesFrom( | 213 void ViewManagerConnection::GetUnknownNodesFrom( |
210 Node* node, | 214 const Node* node, |
211 std::vector<Node*>* nodes) { | 215 std::vector<const Node*>* nodes) { |
212 const TransportNodeId transport_id = NodeIdToTransportId(node->id()); | 216 const TransportNodeId transport_id = NodeIdToTransportId(node->id()); |
213 if (known_nodes_.count(transport_id) == 1) | 217 if (known_nodes_.count(transport_id) == 1) |
214 return; | 218 return; |
215 nodes->push_back(node); | 219 nodes->push_back(node); |
216 known_nodes_.insert(transport_id); | 220 known_nodes_.insert(transport_id); |
217 std::vector<Node*> children(node->GetChildren()); | 221 std::vector<const Node*> children(node->GetChildren()); |
218 for (size_t i = 0 ; i < children.size(); ++i) | 222 for (size_t i = 0 ; i < children.size(); ++i) |
219 GetUnknownNodesFrom(children[i], nodes); | 223 GetUnknownNodesFrom(children[i], nodes); |
220 } | 224 } |
221 | 225 |
222 bool ViewManagerConnection::ShouldNotifyOnHierarchyChange( | 226 bool ViewManagerConnection::ShouldNotifyOnHierarchyChange( |
223 const NodeId& node_id, | 227 const Node* node, |
224 const NodeId& new_parent_id, | 228 const Node* new_parent, |
225 const NodeId& old_parent_id, | 229 const Node* old_parent, |
226 std::vector<Node*>* to_send) { | 230 std::vector<const Node*>* to_send) { |
227 Node* new_parent = GetNode(new_parent_id); | |
228 if (new_parent) { | 231 if (new_parent) { |
229 // On getting a new parent we may need to communicate new nodes to the | 232 // On getting a new parent we may need to communicate new nodes to the |
230 // client. We do that in the following cases: | 233 // client. We do that in the following cases: |
231 // . New parent is a descendant of the root. In this case the client already | 234 // . New parent is a descendant of the root. In this case the client already |
232 // knows all ancestors, so we only have to communicate descendants of node | 235 // knows all ancestors, so we only have to communicate descendants of node |
233 // the client doesn't know about. | 236 // the client doesn't know about. |
234 // . If the client knew about the parent, we have to do the same. | 237 // . If the client knew about the parent, we have to do the same. |
235 // . If the client knows about the node and is added to a tree the client | 238 // . If the client knows about the node and is added to a tree the client |
236 // doesn't know about we have to communicate from the root down (the | 239 // doesn't know about we have to communicate from the root down (the |
237 // client is learning about a new root). | 240 // client is learning about a new root). |
238 if (root_node_manager_->root()->Contains(new_parent) || | 241 if (root_node_manager_->root()->Contains(new_parent) || |
239 known_nodes_.count(NodeIdToTransportId(new_parent_id))) { | 242 known_nodes_.count(NodeIdToTransportId(new_parent->id()))) { |
240 GetUnknownNodesFrom(GetNode(node_id), to_send); | 243 GetUnknownNodesFrom(node, to_send); |
241 return true; | 244 return true; |
242 } | 245 } |
243 // If parent wasn't known we have to communicate from the root down. | 246 // If parent wasn't known we have to communicate from the root down. |
244 if (known_nodes_.count(NodeIdToTransportId(node_id))) { | 247 if (known_nodes_.count(NodeIdToTransportId(node->id()))) { |
245 GetUnknownNodesFrom(new_parent->GetRoot(), to_send); | 248 GetUnknownNodesFrom(new_parent->GetRoot(), to_send); |
246 return true; | 249 return true; |
247 } | 250 } |
248 } | 251 } |
249 // Otherwise only communicate the change if the node was known. We shouldn't | 252 // Otherwise only communicate the change if the node was known. We shouldn't |
250 // need to communicate any nodes on a remove. | 253 // need to communicate any nodes on a remove. |
251 return known_nodes_.count(NodeIdToTransportId(node_id)) > 0; | 254 return known_nodes_.count(NodeIdToTransportId(node->id())) > 0; |
252 } | 255 } |
253 | 256 |
254 void ViewManagerConnection::CreateNode( | 257 void ViewManagerConnection::CreateNode( |
255 TransportNodeId transport_node_id, | 258 TransportNodeId transport_node_id, |
256 const Callback<void(bool)>& callback) { | 259 const Callback<void(bool)>& callback) { |
257 const NodeId node_id(NodeIdFromTransportId(transport_node_id)); | 260 const NodeId node_id(NodeIdFromTransportId(transport_node_id)); |
258 if (node_id.connection_id != id_ || | 261 if (node_id.connection_id != id_ || |
259 node_map_.find(node_id.node_id) != node_map_.end()) { | 262 node_map_.find(node_id.node_id) != node_map_.end()) { |
260 callback.Run(false); | 263 callback.Run(false); |
261 return; | 264 return; |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 } | 315 } |
313 } | 316 } |
314 callback.Run(success); | 317 callback.Run(success); |
315 } | 318 } |
316 | 319 |
317 void ViewManagerConnection::GetNodeTree( | 320 void ViewManagerConnection::GetNodeTree( |
318 TransportNodeId node_id, | 321 TransportNodeId node_id, |
319 const Callback<void(Array<INode>)>& callback) { | 322 const Callback<void(Array<INode>)>& callback) { |
320 AllocationScope allocation_scope; | 323 AllocationScope allocation_scope; |
321 Node* node = GetNode(NodeIdFromTransportId(node_id)); | 324 Node* node = GetNode(NodeIdFromTransportId(node_id)); |
322 std::vector<Node*> nodes; | 325 std::vector<const Node*> nodes; |
323 GetDescendants(node, &nodes); | 326 GetDescendants(node, &nodes); |
324 for (size_t i = 0; i < nodes.size(); ++i) | 327 for (size_t i = 0; i < nodes.size(); ++i) |
325 known_nodes_.insert(NodeIdToTransportId(nodes[i]->id())); | 328 known_nodes_.insert(NodeIdToTransportId(nodes[i]->id())); |
326 callback.Run(Array<INode>::From(nodes)); | 329 callback.Run(Array<INode>::From(nodes)); |
327 } | 330 } |
328 | 331 |
329 void ViewManagerConnection::CreateView( | 332 void ViewManagerConnection::CreateView( |
330 TransportViewId transport_view_id, | 333 TransportViewId transport_view_id, |
331 const Callback<void(bool)>& callback) { | 334 const Callback<void(bool)>& callback) { |
332 const ViewId view_id(ViewIdFromTransportId(transport_view_id)); | 335 const ViewId view_id(ViewIdFromTransportId(transport_view_id)); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 MOJO_MAP_BUFFER_FLAG_NONE) != MOJO_RESULT_OK) { | 370 MOJO_MAP_BUFFER_FLAG_NONE) != MOJO_RESULT_OK) { |
368 return; | 371 return; |
369 } | 372 } |
370 SkBitmap bitmap; | 373 SkBitmap bitmap; |
371 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(handle_data), | 374 gfx::PNGCodec::Decode(static_cast<const unsigned char*>(handle_data), |
372 buffer_size, &bitmap); | 375 buffer_size, &bitmap); |
373 view->SetBitmap(bitmap); | 376 view->SetBitmap(bitmap); |
374 UnmapBuffer(handle_data); | 377 UnmapBuffer(handle_data); |
375 } | 378 } |
376 | 379 |
377 void ViewManagerConnection::OnNodeHierarchyChanged(const NodeId& node, | 380 void ViewManagerConnection::OnNodeHierarchyChanged(const Node* node, |
378 const NodeId& new_parent, | 381 const Node* new_parent, |
379 const NodeId& old_parent) { | 382 const Node* old_parent) { |
380 root_node_manager_->ProcessNodeHierarchyChanged(node, new_parent, old_parent); | 383 root_node_manager_->ProcessNodeHierarchyChanged(node, new_parent, old_parent); |
381 } | 384 } |
382 | 385 |
383 void ViewManagerConnection::OnNodeViewReplaced(const NodeId& node, | 386 void ViewManagerConnection::OnNodeViewReplaced(const Node* node, |
384 const ViewId& new_view_id, | 387 const View* new_view, |
385 const ViewId& old_view_id) { | 388 const View* old_view) { |
386 root_node_manager_->ProcessNodeViewReplaced(node, new_view_id, old_view_id); | 389 root_node_manager_->ProcessNodeViewReplaced(node, new_view, old_view); |
387 } | 390 } |
388 | 391 |
389 } // namespace service | 392 } // namespace service |
390 } // namespace view_manager | 393 } // namespace view_manager |
391 } // namespace mojo | 394 } // namespace mojo |
OLD | NEW |