Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(60)

Side by Side Diff: mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.cc

Issue 290703002: Map new subtrees when they are attached to the node hierarchy visible to a connection. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_synchronizer.h" 5 #include "mojo/services/public/cpp/view_manager/lib/view_manager_synchronizer.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "mojo/public/cpp/shell/connect.h" 9 #include "mojo/public/cpp/shell/connect.h"
10 #include "mojo/public/interfaces/shell/shell.mojom.h" 10 #include "mojo/public/interfaces/shell/shell.mojom.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 private_view.set_id(view_id); 44 private_view.set_id(view_id);
45 private_view.set_node(node); 45 private_view.set_node(node);
46 // TODO(beng): this broadcasts notifications locally... do we want this? I 46 // TODO(beng): this broadcasts notifications locally... do we want this? I
47 // don't think so. same story for LocalAddChild above! 47 // don't think so. same story for LocalAddChild above!
48 private_node.LocalSetActiveView(view); 48 private_node.LocalSetActiveView(view);
49 private_manager.AddView(view->id(), view); 49 private_manager.AddView(view->id(), view);
50 } 50 }
51 return node; 51 return node;
52 } 52 }
53 53
54 ViewTreeNode* BuildNodeTree(ViewManager* manager,
55 const Array<INode>& nodes) {
56 std::vector<ViewTreeNode*> parents;
57 ViewTreeNode* root = NULL;
58 ViewTreeNode* last_node = NULL;
59 for (size_t i = 0; i < nodes.size(); ++i) {
60 if (last_node && nodes[i].parent_id() == last_node->id()) {
61 parents.push_back(last_node);
62 } else if (!parents.empty()) {
63 while (parents.back()->id() != nodes[i].parent_id())
64 parents.pop_back();
65 }
66 ViewTreeNode* node = AddNodeToViewManager(
67 manager,
68 !parents.empty() ? parents.back() : NULL,
69 nodes[i].node_id(),
70 nodes[i].view_id());
71 if (!last_node)
72 root = node;
73 last_node = node;
74 }
75 return root;
76 }
77
54 class ViewManagerTransaction { 78 class ViewManagerTransaction {
55 public: 79 public:
56 virtual ~ViewManagerTransaction() {} 80 virtual ~ViewManagerTransaction() {}
57 81
58 void Commit() { 82 void Commit() {
59 DCHECK(!committed_); 83 DCHECK(!committed_);
60 DoCommit(); 84 DoCommit();
61 committed_ = true; 85 committed_ = true;
62 } 86 }
63 87
64 bool committed() const { return committed_; } 88 bool committed() const { return committed_; }
65 89
66 // General callback to be used for commits to the service.
67 void OnActionCompleted(bool success) {
68 DCHECK(success);
69 DoActionCompleted(success);
70 synchronizer_->RemoveFromPendingQueue(this);
71 }
72
73 protected: 90 protected:
74 enum TransactionType { 91 enum TransactionType {
75 // View creation and destruction. 92 // View creation and destruction.
76 TYPE_CREATE_VIEW, 93 TYPE_CREATE_VIEW,
77 TYPE_DESTROY_VIEW, 94 TYPE_DESTROY_VIEW,
78 // Node creation and destruction. 95 // Node creation and destruction.
79 TYPE_CREATE_VIEW_TREE_NODE, 96 TYPE_CREATE_VIEW_TREE_NODE,
80 TYPE_DESTROY_VIEW_TREE_NODE, 97 TYPE_DESTROY_VIEW_TREE_NODE,
81 // Modifications to the hierarchy (addition of or removal of nodes from a 98 // Modifications to the hierarchy (addition of or removal of nodes from a
82 // parent.) 99 // parent.)
(...skipping 15 matching lines...) Expand all
98 // Overridden to perform transaction-specific cleanup on commit ack from the 115 // Overridden to perform transaction-specific cleanup on commit ack from the
99 // service. 116 // service.
100 virtual void DoActionCompleted(bool success) = 0; 117 virtual void DoActionCompleted(bool success) = 0;
101 118
102 IViewManager* service() { return synchronizer_->service_.get(); } 119 IViewManager* service() { return synchronizer_->service_.get(); }
103 120
104 TransportChangeId GetAndAdvanceNextServerChangeId() { 121 TransportChangeId GetAndAdvanceNextServerChangeId() {
105 return synchronizer_->next_server_change_id_++; 122 return synchronizer_->next_server_change_id_++;
106 } 123 }
107 124
125 base::Callback<void(bool)> ActionCompletedCallback() {
126 return base::Bind(&ViewManagerTransaction::OnActionCompleted,
127 base::Unretained(this));
128 }
129
108 private: 130 private:
131 // General callback to be used for commits to the service.
132 void OnActionCompleted(bool success) {
133 DCHECK(success);
134 DoActionCompleted(success);
135 synchronizer_->RemoveFromPendingQueue(this);
136 }
137
109 const TransactionType transaction_type_; 138 const TransactionType transaction_type_;
110 bool committed_; 139 bool committed_;
111 ViewManagerSynchronizer* synchronizer_; 140 ViewManagerSynchronizer* synchronizer_;
112 141
113 DISALLOW_COPY_AND_ASSIGN(ViewManagerTransaction); 142 DISALLOW_COPY_AND_ASSIGN(ViewManagerTransaction);
114 }; 143 };
115 144
116 class CreateViewTransaction : public ViewManagerTransaction { 145 class CreateViewTransaction : public ViewManagerTransaction {
117 public: 146 public:
118 CreateViewTransaction(TransportViewId view_id, 147 CreateViewTransaction(TransportViewId view_id,
119 ViewManagerSynchronizer* synchronizer) 148 ViewManagerSynchronizer* synchronizer)
120 : ViewManagerTransaction(TYPE_CREATE_VIEW, synchronizer), 149 : ViewManagerTransaction(TYPE_CREATE_VIEW, synchronizer),
121 view_id_(view_id) {} 150 view_id_(view_id) {}
122 virtual ~CreateViewTransaction() {} 151 virtual ~CreateViewTransaction() {}
123 152
124 private: 153 private:
125 // Overridden from ViewManagerTransaction: 154 // Overridden from ViewManagerTransaction:
126 virtual void DoCommit() OVERRIDE { 155 virtual void DoCommit() OVERRIDE {
127 service()->CreateView( 156 service()->CreateView(view_id_, ActionCompletedCallback());
128 view_id_,
129 base::Bind(&ViewManagerTransaction::OnActionCompleted,
130 base::Unretained(this)));
131 } 157 }
132 virtual void DoActionCompleted(bool success) OVERRIDE { 158 virtual void DoActionCompleted(bool success) OVERRIDE {
133 // TODO(beng): failure. 159 // TODO(beng): failure.
134 } 160 }
135 161
136 const TransportViewId view_id_; 162 const TransportViewId view_id_;
137 163
138 DISALLOW_COPY_AND_ASSIGN(CreateViewTransaction); 164 DISALLOW_COPY_AND_ASSIGN(CreateViewTransaction);
139 }; 165 };
140 166
141 class DestroyViewTransaction : public ViewManagerTransaction { 167 class DestroyViewTransaction : public ViewManagerTransaction {
142 public: 168 public:
143 DestroyViewTransaction(TransportViewId view_id, 169 DestroyViewTransaction(TransportViewId view_id,
144 ViewManagerSynchronizer* synchronizer) 170 ViewManagerSynchronizer* synchronizer)
145 : ViewManagerTransaction(TYPE_DESTROY_VIEW, synchronizer), 171 : ViewManagerTransaction(TYPE_DESTROY_VIEW, synchronizer),
146 view_id_(view_id) {} 172 view_id_(view_id) {}
147 virtual ~DestroyViewTransaction() {} 173 virtual ~DestroyViewTransaction() {}
148 174
149 private: 175 private:
150 // Overridden from ViewManagerTransaction: 176 // Overridden from ViewManagerTransaction:
151 virtual void DoCommit() OVERRIDE { 177 virtual void DoCommit() OVERRIDE {
152 service()->DeleteView( 178 service()->DeleteView(view_id_, ActionCompletedCallback());
153 view_id_,
154 base::Bind(&ViewManagerTransaction::OnActionCompleted,
155 base::Unretained(this)));
156 } 179 }
157 virtual void DoActionCompleted(bool success) OVERRIDE { 180 virtual void DoActionCompleted(bool success) OVERRIDE {
158 // TODO(beng): recovery? 181 // TODO(beng): recovery?
159 } 182 }
160 183
161 const TransportViewId view_id_; 184 const TransportViewId view_id_;
162 185
163 DISALLOW_COPY_AND_ASSIGN(DestroyViewTransaction); 186 DISALLOW_COPY_AND_ASSIGN(DestroyViewTransaction);
164 }; 187 };
165 188
166 class CreateViewTreeNodeTransaction : public ViewManagerTransaction { 189 class CreateViewTreeNodeTransaction : public ViewManagerTransaction {
167 public: 190 public:
168 CreateViewTreeNodeTransaction(TransportNodeId node_id, 191 CreateViewTreeNodeTransaction(TransportNodeId node_id,
169 ViewManagerSynchronizer* synchronizer) 192 ViewManagerSynchronizer* synchronizer)
170 : ViewManagerTransaction(TYPE_CREATE_VIEW_TREE_NODE, synchronizer), 193 : ViewManagerTransaction(TYPE_CREATE_VIEW_TREE_NODE, synchronizer),
171 node_id_(node_id) {} 194 node_id_(node_id) {}
172 virtual ~CreateViewTreeNodeTransaction() {} 195 virtual ~CreateViewTreeNodeTransaction() {}
173 196
174 private: 197 private:
175 // Overridden from ViewManagerTransaction: 198 // Overridden from ViewManagerTransaction:
176 virtual void DoCommit() OVERRIDE { 199 virtual void DoCommit() OVERRIDE {
177 service()->CreateNode( 200 service()->CreateNode(node_id_, ActionCompletedCallback());
178 node_id_,
179 base::Bind(&ViewManagerTransaction::OnActionCompleted,
180 base::Unretained(this)));
181 } 201 }
182 virtual void DoActionCompleted(bool success) OVERRIDE { 202 virtual void DoActionCompleted(bool success) OVERRIDE {
183 // TODO(beng): Failure means we tried to create with an extant id for this 203 // TODO(beng): Failure means we tried to create with an extant id for this
184 // connection. It also could mean we tried to do something 204 // connection. It also could mean we tried to do something
185 // invalid, or we tried applying a change out of order. Figure 205 // invalid, or we tried applying a change out of order. Figure
186 // out what to do. 206 // out what to do.
187 } 207 }
188 208
189 const TransportNodeId node_id_; 209 const TransportNodeId node_id_;
190 210
191 DISALLOW_COPY_AND_ASSIGN(CreateViewTreeNodeTransaction); 211 DISALLOW_COPY_AND_ASSIGN(CreateViewTreeNodeTransaction);
192 }; 212 };
193 213
194 class DestroyViewTreeNodeTransaction : public ViewManagerTransaction { 214 class DestroyViewTreeNodeTransaction : public ViewManagerTransaction {
195 public: 215 public:
196 DestroyViewTreeNodeTransaction(TransportNodeId node_id, 216 DestroyViewTreeNodeTransaction(TransportNodeId node_id,
197 ViewManagerSynchronizer* synchronizer) 217 ViewManagerSynchronizer* synchronizer)
198 : ViewManagerTransaction(TYPE_DESTROY_VIEW_TREE_NODE, synchronizer), 218 : ViewManagerTransaction(TYPE_DESTROY_VIEW_TREE_NODE, synchronizer),
199 node_id_(node_id) {} 219 node_id_(node_id) {}
200 virtual ~DestroyViewTreeNodeTransaction() {} 220 virtual ~DestroyViewTreeNodeTransaction() {}
201 221
202 private: 222 private:
203 // Overridden from ViewManagerTransaction: 223 // Overridden from ViewManagerTransaction:
204 virtual void DoCommit() OVERRIDE { 224 virtual void DoCommit() OVERRIDE {
205 service()->DeleteNode( 225 service()->DeleteNode(node_id_, ActionCompletedCallback());
206 node_id_,
207 base::Bind(&ViewManagerTransaction::OnActionCompleted,
208 base::Unretained(this)));
209 } 226 }
210 virtual void DoActionCompleted(bool success) OVERRIDE { 227 virtual void DoActionCompleted(bool success) OVERRIDE {
211 // TODO(beng): recovery? 228 // TODO(beng): recovery?
212 } 229 }
213 230
214 const TransportNodeId node_id_; 231 const TransportNodeId node_id_;
215 DISALLOW_COPY_AND_ASSIGN(DestroyViewTreeNodeTransaction); 232 DISALLOW_COPY_AND_ASSIGN(DestroyViewTreeNodeTransaction);
216 }; 233 };
217 234
218 class HierarchyTransaction : public ViewManagerTransaction { 235 class HierarchyTransaction : public ViewManagerTransaction {
(...skipping 14 matching lines...) Expand all
233 250
234 private: 251 private:
235 // Overridden from ViewManagerTransaction: 252 // Overridden from ViewManagerTransaction:
236 virtual void DoCommit() OVERRIDE { 253 virtual void DoCommit() OVERRIDE {
237 switch (hierarchy_change_type_) { 254 switch (hierarchy_change_type_) {
238 case TYPE_ADD: 255 case TYPE_ADD:
239 service()->AddNode( 256 service()->AddNode(
240 parent_id_, 257 parent_id_,
241 child_id_, 258 child_id_,
242 GetAndAdvanceNextServerChangeId(), 259 GetAndAdvanceNextServerChangeId(),
243 base::Bind(&ViewManagerTransaction::OnActionCompleted, 260 ActionCompletedCallback());
244 base::Unretained(this)));
245 break; 261 break;
246 case TYPE_REMOVE: 262 case TYPE_REMOVE:
247 service()->RemoveNodeFromParent( 263 service()->RemoveNodeFromParent(
248 child_id_, 264 child_id_,
249 GetAndAdvanceNextServerChangeId(), 265 GetAndAdvanceNextServerChangeId(),
250 base::Bind(&ViewManagerTransaction::OnActionCompleted, 266 ActionCompletedCallback());
251 base::Unretained(this)));
252 break; 267 break;
253 } 268 }
254 } 269 }
255 270
256 virtual void DoActionCompleted(bool success) OVERRIDE { 271 virtual void DoActionCompleted(bool success) OVERRIDE {
257 // TODO(beng): Failure means either one of the nodes specified didn't exist, 272 // TODO(beng): Failure means either one of the nodes specified didn't exist,
258 // or we passed the same node id for both params. Roll back? 273 // or we passed the same node id for both params. Roll back?
259 } 274 }
260 275
261 const HierarchyChangeType hierarchy_change_type_; 276 const HierarchyChangeType hierarchy_change_type_;
262 const TransportNodeId child_id_; 277 const TransportNodeId child_id_;
263 const TransportNodeId parent_id_; 278 const TransportNodeId parent_id_;
264 279
265 DISALLOW_COPY_AND_ASSIGN(HierarchyTransaction); 280 DISALLOW_COPY_AND_ASSIGN(HierarchyTransaction);
266 }; 281 };
267 282
268 class SetActiveViewTransaction : public ViewManagerTransaction { 283 class SetActiveViewTransaction : public ViewManagerTransaction {
269 public: 284 public:
270 SetActiveViewTransaction(TransportNodeId node_id, 285 SetActiveViewTransaction(TransportNodeId node_id,
271 TransportViewId view_id, 286 TransportViewId view_id,
272 ViewManagerSynchronizer* synchronizer) 287 ViewManagerSynchronizer* synchronizer)
273 : ViewManagerTransaction(TYPE_SET_ACTIVE_VIEW, synchronizer), 288 : ViewManagerTransaction(TYPE_SET_ACTIVE_VIEW, synchronizer),
274 node_id_(node_id), 289 node_id_(node_id),
275 view_id_(view_id) {} 290 view_id_(view_id) {}
276 virtual ~SetActiveViewTransaction() {} 291 virtual ~SetActiveViewTransaction() {}
277 292
278 private: 293 private:
279 // Overridden from ViewManagerTransaction: 294 // Overridden from ViewManagerTransaction:
280 virtual void DoCommit() OVERRIDE { 295 virtual void DoCommit() OVERRIDE {
281 service()->SetView( 296 service()->SetView(node_id_, view_id_, ActionCompletedCallback());
282 node_id_,
283 view_id_,
284 base::Bind(&ViewManagerTransaction::OnActionCompleted,
285 base::Unretained(this)));
286 } 297 }
287 virtual void DoActionCompleted(bool success) OVERRIDE { 298 virtual void DoActionCompleted(bool success) OVERRIDE {
288 // TODO(beng): recovery? 299 // TODO(beng): recovery?
289 } 300 }
290 301
291 const TransportNodeId node_id_; 302 const TransportNodeId node_id_;
292 const TransportViewId view_id_; 303 const TransportViewId view_id_;
293 304
294 DISALLOW_COPY_AND_ASSIGN(SetActiveViewTransaction); 305 DISALLOW_COPY_AND_ASSIGN(SetActiveViewTransaction);
295 }; 306 };
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 new SetActiveViewTransaction(node_id, view_id, this)); 398 new SetActiveViewTransaction(node_id, view_id, this));
388 Sync(); 399 Sync();
389 } 400 }
390 401
391 //////////////////////////////////////////////////////////////////////////////// 402 ////////////////////////////////////////////////////////////////////////////////
392 // ViewManagerSynchronizer, IViewManagerClient implementation: 403 // ViewManagerSynchronizer, IViewManagerClient implementation:
393 404
394 void ViewManagerSynchronizer::OnConnectionEstablished( 405 void ViewManagerSynchronizer::OnConnectionEstablished(
395 TransportConnectionId connection_id, 406 TransportConnectionId connection_id,
396 TransportChangeId next_server_change_id, 407 TransportChangeId next_server_change_id,
397 const mojo::Array<INode>& nodes) { 408 const Array<INode>& nodes) {
398 connected_ = true; 409 connected_ = true;
399 connection_id_ = connection_id; 410 connection_id_ = connection_id;
400 next_server_change_id_ = next_server_change_id; 411 next_server_change_id_ = next_server_change_id;
401 412
402 ViewManagerPrivate private_manager(view_manager_); 413 ViewManagerPrivate(view_manager_).set_root(
403 std::vector<ViewTreeNode*> parents; 414 BuildNodeTree(view_manager_, nodes));
404 ViewTreeNode* root = NULL;
405 ViewTreeNode* last_node = NULL;
406 for (size_t i = 0; i < nodes.size(); ++i) {
407 if (last_node && nodes[i].parent_id() == last_node->id()) {
408 parents.push_back(last_node);
409 } else if (!parents.empty()) {
410 while (parents.back()->id() != nodes[i].parent_id())
411 parents.pop_back();
412 }
413 ViewTreeNode* node =
414 AddNodeToViewManager(view_manager_,
415 !parents.empty() ? parents.back() : NULL,
416 nodes[i].node_id(),
417 nodes[i].view_id());
418 if (!last_node)
419 root = node;
420 last_node = node;
421 }
422 private_manager.set_root(root);
423 if (init_loop_) 415 if (init_loop_)
424 init_loop_->Quit(); 416 init_loop_->Quit();
425 417
426 Sync(); 418 Sync();
427 } 419 }
428 420
429 void ViewManagerSynchronizer::OnServerChangeIdAdvanced( 421 void ViewManagerSynchronizer::OnServerChangeIdAdvanced(
430 uint32_t next_server_change_id) { 422 uint32_t next_server_change_id) {
431 next_server_change_id_ = next_server_change_id; 423 next_server_change_id_ = next_server_change_id;
432 } 424 }
433 425
434 void ViewManagerSynchronizer::OnNodeHierarchyChanged( 426 void ViewManagerSynchronizer::OnNodeHierarchyChanged(
435 uint32_t node_id, 427 uint32_t node_id,
436 uint32_t new_parent_id, 428 uint32_t new_parent_id,
437 uint32_t old_parent_id, 429 uint32_t old_parent_id,
438 TransportChangeId server_change_id, 430 TransportChangeId server_change_id,
439 const mojo::Array<INode>& nodes) { 431 const Array<INode>& nodes) {
440 // TODO: deal with |nodes|. 432 // TODO: deal with |nodes|.
441 next_server_change_id_ = server_change_id + 1; 433 next_server_change_id_ = server_change_id + 1;
442 434
443 ViewTreeNode* new_parent = 435 BuildNodeTree(view_manager_, nodes);
444 view_manager_->tree()->GetChildById(new_parent_id);
445 ViewTreeNode* old_parent =
446 view_manager_->tree()->GetChildById(old_parent_id);
447 ViewTreeNode* node = NULL;
448 if (old_parent) {
449 // Existing node, mapped in this connection's tree.
450 // TODO(beng): verify this is actually true.
451 node = view_manager_->GetNodeById(node_id);
452 DCHECK_EQ(node->parent(), old_parent);
453 } else {
454 // New node, originating from another connection.
455 node = ViewTreeNodePrivate::LocalCreate();
456 ViewTreeNodePrivate private_node(node);
457 private_node.set_view_manager(view_manager_);
458 private_node.set_id(node_id);
459 ViewManagerPrivate(view_manager_).AddNode(node->id(), node);
460 436
461 // TODO(beng): view changes. 437 ViewTreeNode* new_parent = view_manager_->GetNodeById(new_parent_id);
462 } 438 ViewTreeNode* old_parent = view_manager_->GetNodeById(old_parent_id);
439 ViewTreeNode* node = view_manager_->GetNodeById(node_id);
463 if (new_parent) 440 if (new_parent)
464 ViewTreeNodePrivate(new_parent).LocalAddChild(node); 441 ViewTreeNodePrivate(new_parent).LocalAddChild(node);
465 else 442 else
466 ViewTreeNodePrivate(old_parent).LocalRemoveChild(node); 443 ViewTreeNodePrivate(old_parent).LocalRemoveChild(node);
467 } 444 }
468 445
469 void ViewManagerSynchronizer::OnNodeDeleted(uint32_t node_id, 446 void ViewManagerSynchronizer::OnNodeDeleted(uint32_t node_id,
470 uint32_t server_change_id) { 447 uint32_t server_change_id) {
471 next_server_change_id_ = server_change_id + 1; 448 next_server_change_id_ = server_change_id + 1;
472 449
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 for (; it != pending_transactions_.end(); ++it) { 490 for (; it != pending_transactions_.end(); ++it) {
514 if (!(*it)->committed()) 491 if (!(*it)->committed())
515 (*it)->Commit(); 492 (*it)->Commit();
516 } 493 }
517 } 494 }
518 495
519 void ViewManagerSynchronizer::RemoveFromPendingQueue( 496 void ViewManagerSynchronizer::RemoveFromPendingQueue(
520 ViewManagerTransaction* transaction) { 497 ViewManagerTransaction* transaction) {
521 DCHECK_EQ(transaction, pending_transactions_.front()); 498 DCHECK_EQ(transaction, pending_transactions_.front());
522 pending_transactions_.erase(pending_transactions_.begin()); 499 pending_transactions_.erase(pending_transactions_.begin());
500 if (pending_transactions_.empty() && !changes_acked_callback_.is_null())
501 changes_acked_callback_.Run();
523 } 502 }
524 503
525 } // namespace view_manager 504 } // namespace view_manager
526 } // namespace mojo 505 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698