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_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/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "mojo/public/cpp/application/application.h" | 9 #include "mojo/public/cpp/application/application.h" |
10 #include "mojo/public/cpp/application/connect.h" | 10 #include "mojo/public/cpp/application/connect.h" |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 service()->DeleteNode(node_id_, ActionCompletedCallback()); | 239 service()->DeleteNode(node_id_, ActionCompletedCallback()); |
240 } | 240 } |
241 virtual void DoActionCompleted(bool success) OVERRIDE { | 241 virtual void DoActionCompleted(bool success) OVERRIDE { |
242 // TODO(beng): recovery? | 242 // TODO(beng): recovery? |
243 } | 243 } |
244 | 244 |
245 const Id node_id_; | 245 const Id node_id_; |
246 DISALLOW_COPY_AND_ASSIGN(DestroyViewTreeNodeTransaction); | 246 DISALLOW_COPY_AND_ASSIGN(DestroyViewTreeNodeTransaction); |
247 }; | 247 }; |
248 | 248 |
249 class HierarchyTransaction : public ViewManagerTransaction { | 249 class AddChildTransaction : public ViewManagerTransaction { |
250 public: | 250 public: |
251 enum HierarchyChangeType { | 251 AddChildTransaction(Id child_id, |
252 TYPE_ADD, | 252 Id parent_id, |
253 TYPE_REMOVE | 253 ViewManagerSynchronizer* synchronizer) |
254 }; | |
255 HierarchyTransaction(HierarchyChangeType hierarchy_change_type, | |
256 Id child_id, | |
257 Id parent_id, | |
258 ViewManagerSynchronizer* synchronizer) | |
259 : ViewManagerTransaction(synchronizer), | 254 : ViewManagerTransaction(synchronizer), |
260 hierarchy_change_type_(hierarchy_change_type), | |
261 child_id_(child_id), | 255 child_id_(child_id), |
262 parent_id_(parent_id) {} | 256 parent_id_(parent_id) {} |
263 virtual ~HierarchyTransaction() {} | 257 virtual ~AddChildTransaction() {} |
264 | 258 |
265 private: | 259 private: |
266 // Overridden from ViewManagerTransaction: | 260 // Overridden from ViewManagerTransaction: |
267 virtual void DoCommit() OVERRIDE { | 261 virtual void DoCommit() OVERRIDE { |
268 switch (hierarchy_change_type_) { | 262 service()->AddNode(parent_id_, |
269 case TYPE_ADD: | 263 child_id_, |
270 service()->AddNode( | 264 GetAndAdvanceNextServerChangeId(), |
271 parent_id_, | 265 ActionCompletedCallback()); |
272 child_id_, | |
273 GetAndAdvanceNextServerChangeId(), | |
274 ActionCompletedCallback()); | |
275 break; | |
276 case TYPE_REMOVE: | |
277 service()->RemoveNodeFromParent( | |
278 child_id_, | |
279 GetAndAdvanceNextServerChangeId(), | |
280 ActionCompletedCallback()); | |
281 break; | |
282 } | |
283 } | 266 } |
284 | 267 |
285 virtual void DoActionCompleted(bool success) OVERRIDE { | 268 virtual void DoActionCompleted(bool success) OVERRIDE { |
286 // TODO(beng): Failure means either one of the nodes specified didn't exist, | 269 // TODO(beng): recovery? |
287 // or we passed the same node id for both params. Roll back? | |
288 } | 270 } |
289 | 271 |
290 const HierarchyChangeType hierarchy_change_type_; | |
291 const Id child_id_; | 272 const Id child_id_; |
292 const Id parent_id_; | 273 const Id parent_id_; |
293 | 274 |
294 DISALLOW_COPY_AND_ASSIGN(HierarchyTransaction); | 275 DISALLOW_COPY_AND_ASSIGN(AddChildTransaction); |
| 276 }; |
| 277 |
| 278 class RemoveChildTransaction : public ViewManagerTransaction { |
| 279 public: |
| 280 RemoveChildTransaction(Id child_id, |
| 281 ViewManagerSynchronizer* synchronizer) |
| 282 : ViewManagerTransaction(synchronizer), |
| 283 child_id_(child_id) {} |
| 284 virtual ~RemoveChildTransaction() {} |
| 285 |
| 286 private: |
| 287 // Overridden from ViewManagerTransaction: |
| 288 virtual void DoCommit() OVERRIDE { |
| 289 service()->RemoveNodeFromParent( |
| 290 child_id_, |
| 291 GetAndAdvanceNextServerChangeId(), |
| 292 ActionCompletedCallback()); |
| 293 } |
| 294 |
| 295 virtual void DoActionCompleted(bool success) OVERRIDE { |
| 296 // TODO(beng): recovery? |
| 297 } |
| 298 |
| 299 const Id child_id_; |
| 300 |
| 301 DISALLOW_COPY_AND_ASSIGN(RemoveChildTransaction); |
| 302 }; |
| 303 |
| 304 class ReorderNodeTransaction : public ViewManagerTransaction { |
| 305 public: |
| 306 ReorderNodeTransaction(Id node_id, |
| 307 Id relative_id, |
| 308 OrderDirection direction, |
| 309 ViewManagerSynchronizer* synchronizer) |
| 310 : ViewManagerTransaction(synchronizer), |
| 311 node_id_(node_id), |
| 312 relative_id_(relative_id), |
| 313 direction_(direction) {} |
| 314 virtual ~ReorderNodeTransaction() {} |
| 315 |
| 316 private: |
| 317 // Overridden from ViewManagerTransaction: |
| 318 virtual void DoCommit() OVERRIDE { |
| 319 service()->ReorderNode(node_id_, |
| 320 relative_id_, |
| 321 direction_, |
| 322 GetAndAdvanceNextServerChangeId(), |
| 323 ActionCompletedCallback()); |
| 324 } |
| 325 |
| 326 virtual void DoActionCompleted(bool success) OVERRIDE { |
| 327 // TODO(beng): recovery? |
| 328 } |
| 329 |
| 330 const Id node_id_; |
| 331 const Id relative_id_; |
| 332 const OrderDirection direction_; |
| 333 |
| 334 DISALLOW_COPY_AND_ASSIGN(ReorderNodeTransaction); |
295 }; | 335 }; |
296 | 336 |
297 class SetActiveViewTransaction : public ViewManagerTransaction { | 337 class SetActiveViewTransaction : public ViewManagerTransaction { |
298 public: | 338 public: |
299 SetActiveViewTransaction(Id node_id, | 339 SetActiveViewTransaction(Id node_id, |
300 Id view_id, | 340 Id view_id, |
301 ViewManagerSynchronizer* synchronizer) | 341 ViewManagerSynchronizer* synchronizer) |
302 : ViewManagerTransaction(synchronizer), | 342 : ViewManagerTransaction(synchronizer), |
303 node_id_(node_id), | 343 node_id_(node_id), |
304 view_id_(view_id) {} | 344 view_id_(view_id) {} |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 void ViewManagerSynchronizer::DestroyView(Id view_id) { | 528 void ViewManagerSynchronizer::DestroyView(Id view_id) { |
489 DCHECK(connected_); | 529 DCHECK(connected_); |
490 pending_transactions_.push_back(new DestroyViewTransaction(view_id, this)); | 530 pending_transactions_.push_back(new DestroyViewTransaction(view_id, this)); |
491 Sync(); | 531 Sync(); |
492 } | 532 } |
493 | 533 |
494 void ViewManagerSynchronizer::AddChild(Id child_id, | 534 void ViewManagerSynchronizer::AddChild(Id child_id, |
495 Id parent_id) { | 535 Id parent_id) { |
496 DCHECK(connected_); | 536 DCHECK(connected_); |
497 pending_transactions_.push_back( | 537 pending_transactions_.push_back( |
498 new HierarchyTransaction(HierarchyTransaction::TYPE_ADD, | 538 new AddChildTransaction(child_id, parent_id, this)); |
499 child_id, | |
500 parent_id, | |
501 this)); | |
502 Sync(); | 539 Sync(); |
503 } | 540 } |
504 | 541 |
505 void ViewManagerSynchronizer::RemoveChild(Id child_id, Id parent_id) { | 542 void ViewManagerSynchronizer::RemoveChild(Id child_id, Id parent_id) { |
506 DCHECK(connected_); | 543 DCHECK(connected_); |
507 pending_transactions_.push_back( | 544 pending_transactions_.push_back(new RemoveChildTransaction(child_id, this)); |
508 new HierarchyTransaction(HierarchyTransaction::TYPE_REMOVE, | |
509 child_id, | |
510 parent_id, | |
511 this)); | |
512 Sync(); | 545 Sync(); |
513 } | 546 } |
514 | 547 |
| 548 void ViewManagerSynchronizer::Reorder( |
| 549 Id node_id, |
| 550 Id relative_node_id, |
| 551 OrderDirection direction) { |
| 552 DCHECK(connected_); |
| 553 pending_transactions_.push_back( |
| 554 new ReorderNodeTransaction(node_id, relative_node_id, direction, this)); |
| 555 Sync(); |
| 556 } |
| 557 |
515 bool ViewManagerSynchronizer::OwnsNode(Id id) const { | 558 bool ViewManagerSynchronizer::OwnsNode(Id id) const { |
516 return HiWord(id) == connection_id_; | 559 return HiWord(id) == connection_id_; |
517 } | 560 } |
518 | 561 |
519 bool ViewManagerSynchronizer::OwnsView(Id id) const { | 562 bool ViewManagerSynchronizer::OwnsView(Id id) const { |
520 return HiWord(id) == connection_id_; | 563 return HiWord(id) == connection_id_; |
521 } | 564 } |
522 | 565 |
523 void ViewManagerSynchronizer::SetActiveView(Id node_id, Id view_id) { | 566 void ViewManagerSynchronizer::SetActiveView(Id node_id, Id view_id) { |
524 DCHECK(connected_); | 567 DCHECK(connected_); |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
631 ViewTreeNodePrivate(node).LocalSetBounds(old_bounds.To<gfx::Rect>(), | 674 ViewTreeNodePrivate(node).LocalSetBounds(old_bounds.To<gfx::Rect>(), |
632 new_bounds.To<gfx::Rect>()); | 675 new_bounds.To<gfx::Rect>()); |
633 } | 676 } |
634 | 677 |
635 void ViewManagerSynchronizer::OnNodeHierarchyChanged( | 678 void ViewManagerSynchronizer::OnNodeHierarchyChanged( |
636 Id node_id, | 679 Id node_id, |
637 Id new_parent_id, | 680 Id new_parent_id, |
638 Id old_parent_id, | 681 Id old_parent_id, |
639 Id server_change_id, | 682 Id server_change_id, |
640 mojo::Array<INodePtr> nodes) { | 683 mojo::Array<INodePtr> nodes) { |
641 // TODO: deal with |nodes|. | |
642 next_server_change_id_ = server_change_id + 1; | 684 next_server_change_id_ = server_change_id + 1; |
643 | 685 |
644 BuildNodeTree(this, nodes); | 686 BuildNodeTree(this, nodes); |
645 | 687 |
646 ViewTreeNode* new_parent = GetNodeById(new_parent_id); | 688 ViewTreeNode* new_parent = GetNodeById(new_parent_id); |
647 ViewTreeNode* old_parent = GetNodeById(old_parent_id); | 689 ViewTreeNode* old_parent = GetNodeById(old_parent_id); |
648 ViewTreeNode* node = GetNodeById(node_id); | 690 ViewTreeNode* node = GetNodeById(node_id); |
649 if (new_parent) | 691 if (new_parent) |
650 ViewTreeNodePrivate(new_parent).LocalAddChild(node); | 692 ViewTreeNodePrivate(new_parent).LocalAddChild(node); |
651 else | 693 else |
652 ViewTreeNodePrivate(old_parent).LocalRemoveChild(node); | 694 ViewTreeNodePrivate(old_parent).LocalRemoveChild(node); |
653 } | 695 } |
654 | 696 |
| 697 void ViewManagerSynchronizer::OnNodeReordered(Id node_id, |
| 698 Id relative_node_id, |
| 699 OrderDirection direction, |
| 700 Id server_change_id) { |
| 701 next_server_change_id_ = server_change_id + 1; |
| 702 |
| 703 ViewTreeNode* node = GetNodeById(node_id); |
| 704 ViewTreeNode* relative_node = GetNodeById(relative_node_id); |
| 705 if (node && relative_node) { |
| 706 ViewTreeNodePrivate(node).LocalReorder(relative_node, direction); |
| 707 } |
| 708 } |
| 709 |
655 void ViewManagerSynchronizer::OnNodeDeleted(Id node_id, Id server_change_id) { | 710 void ViewManagerSynchronizer::OnNodeDeleted(Id node_id, Id server_change_id) { |
656 next_server_change_id_ = server_change_id + 1; | 711 next_server_change_id_ = server_change_id + 1; |
657 | 712 |
658 ViewTreeNode* node = GetNodeById(node_id); | 713 ViewTreeNode* node = GetNodeById(node_id); |
659 if (node) | 714 if (node) |
660 ViewTreeNodePrivate(node).LocalDestroy(); | 715 ViewTreeNodePrivate(node).LocalDestroy(); |
661 } | 716 } |
662 | 717 |
663 void ViewManagerSynchronizer::OnNodeViewReplaced(Id node_id, | 718 void ViewManagerSynchronizer::OnNodeViewReplaced(Id node_id, |
664 Id new_view_id, | 719 Id new_view_id, |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
748 // ViewManager, public: | 803 // ViewManager, public: |
749 | 804 |
750 // static | 805 // static |
751 void ViewManager::Create(Application* application, | 806 void ViewManager::Create(Application* application, |
752 ViewManagerDelegate* delegate) { | 807 ViewManagerDelegate* delegate) { |
753 application->AddService<ViewManagerSynchronizer>(delegate); | 808 application->AddService<ViewManagerSynchronizer>(delegate); |
754 } | 809 } |
755 | 810 |
756 } // namespace view_manager | 811 } // namespace view_manager |
757 } // namespace mojo | 812 } // namespace mojo |
OLD | NEW |