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/run_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "mojo/public/cpp/application/connect.h" | 9 #include "mojo/public/cpp/application/connect.h" |
10 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" | 10 #include "mojo/public/interfaces/service_provider/service_provider.mojom.h" |
11 #include "mojo/services/public/cpp/view_manager/lib/view_manager_private.h" | 11 #include "mojo/services/public/cpp/view_manager/lib/view_manager_private.h" |
12 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" | 12 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" |
13 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" | 13 #include "mojo/services/public/cpp/view_manager/lib/view_tree_node_private.h" |
14 #include "mojo/services/public/cpp/view_manager/util.h" | 14 #include "mojo/services/public/cpp/view_manager/util.h" |
15 #include "third_party/skia/include/core/SkBitmap.h" | 15 #include "third_party/skia/include/core/SkBitmap.h" |
16 #include "ui/gfx/codec/png_codec.h" | 16 #include "ui/gfx/codec/png_codec.h" |
17 | 17 |
18 namespace mojo { | 18 namespace mojo { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 TYPE_CREATE_VIEW_TREE_NODE, | 101 TYPE_CREATE_VIEW_TREE_NODE, |
102 TYPE_DESTROY_VIEW_TREE_NODE, | 102 TYPE_DESTROY_VIEW_TREE_NODE, |
103 // Modifications to the hierarchy (addition of or removal of nodes from a | 103 // Modifications to the hierarchy (addition of or removal of nodes from a |
104 // parent.) | 104 // parent.) |
105 TYPE_HIERARCHY, | 105 TYPE_HIERARCHY, |
106 // View replacement. | 106 // View replacement. |
107 TYPE_SET_ACTIVE_VIEW, | 107 TYPE_SET_ACTIVE_VIEW, |
108 // Node bounds. | 108 // Node bounds. |
109 TYPE_SET_BOUNDS, | 109 TYPE_SET_BOUNDS, |
110 // View contents | 110 // View contents |
111 TYPE_SET_VIEW_CONTENTS | 111 TYPE_SET_VIEW_CONTENTS, |
| 112 // Embed. |
| 113 TYPE_EMBED |
112 }; | 114 }; |
113 | 115 |
114 ViewManagerTransaction(TransactionType transaction_type, | 116 ViewManagerTransaction(TransactionType transaction_type, |
115 ViewManagerSynchronizer* synchronizer) | 117 ViewManagerSynchronizer* synchronizer) |
116 : transaction_type_(transaction_type), | 118 : transaction_type_(transaction_type), |
117 committed_(false), | 119 committed_(false), |
118 synchronizer_(synchronizer) { | 120 synchronizer_(synchronizer) { |
119 } | 121 } |
120 | 122 |
121 // Overridden to perform transaction-specific commit actions. | 123 // Overridden to perform transaction-specific commit actions. |
122 virtual void DoCommit() = 0; | 124 virtual void DoCommit() = 0; |
123 | 125 |
124 // Overridden to perform transaction-specific cleanup on commit ack from the | 126 // Overridden to perform transaction-specific cleanup on commit ack from the |
125 // service. | 127 // service. |
126 virtual void DoActionCompleted(bool success) = 0; | 128 virtual void DoActionCompleted(bool success) = 0; |
127 | 129 |
128 IViewManager* service() { return synchronizer_->service_.get(); } | 130 IViewManager* service() { return synchronizer_->service_; } |
129 | 131 |
130 TransportChangeId GetAndAdvanceNextServerChangeId() { | 132 TransportChangeId GetAndAdvanceNextServerChangeId() { |
131 return synchronizer_->next_server_change_id_++; | 133 return synchronizer_->next_server_change_id_++; |
132 } | 134 } |
133 | 135 |
134 base::Callback<void(bool)> ActionCompletedCallback() { | 136 base::Callback<void(bool)> ActionCompletedCallback() { |
135 return base::Bind(&ViewManagerTransaction::OnActionCompleted, | 137 return base::Bind(&ViewManagerTransaction::OnActionCompleted, |
136 base::Unretained(this)); | 138 base::Unretained(this)); |
137 } | 139 } |
138 | 140 |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 return true; | 400 return true; |
399 } | 401 } |
400 | 402 |
401 const TransportViewId view_id_; | 403 const TransportViewId view_id_; |
402 const SkBitmap contents_; | 404 const SkBitmap contents_; |
403 ScopedSharedBufferHandle shared_state_handle_; | 405 ScopedSharedBufferHandle shared_state_handle_; |
404 | 406 |
405 DISALLOW_COPY_AND_ASSIGN(SetViewContentsTransaction); | 407 DISALLOW_COPY_AND_ASSIGN(SetViewContentsTransaction); |
406 }; | 408 }; |
407 | 409 |
| 410 class EmbedTransaction : public ViewManagerTransaction { |
| 411 public: |
| 412 EmbedTransaction(const String& url, |
| 413 TransportNodeId node_id, |
| 414 ViewManagerSynchronizer* synchronizer) |
| 415 : ViewManagerTransaction(TYPE_EMBED, synchronizer), |
| 416 url_(url), |
| 417 node_id_(node_id) {} |
| 418 virtual ~EmbedTransaction() {} |
| 419 |
| 420 private: |
| 421 // Overridden from ViewManagerTransaction: |
| 422 virtual void DoCommit() OVERRIDE { |
| 423 std::vector<TransportNodeId> ids; |
| 424 ids.push_back(node_id_); |
| 425 service()->Connect(url_, Array<TransportNodeId>::From(ids), |
| 426 ActionCompletedCallback()); |
| 427 } |
| 428 virtual void DoActionCompleted(bool success) OVERRIDE { |
| 429 // TODO(beng): recovery? |
| 430 } |
| 431 |
| 432 const String url_; |
| 433 const TransportNodeId node_id_; |
| 434 |
| 435 DISALLOW_COPY_AND_ASSIGN(EmbedTransaction); |
| 436 }; |
| 437 |
408 ViewManagerSynchronizer::ViewManagerSynchronizer(ViewManager* view_manager) | 438 ViewManagerSynchronizer::ViewManagerSynchronizer(ViewManager* view_manager) |
409 : view_manager_(view_manager), | 439 : view_manager_(view_manager), |
410 connected_(false), | 440 connected_(false), |
411 connection_id_(0), | 441 connection_id_(0), |
412 next_id_(1), | 442 next_id_(1), |
413 next_server_change_id_(0), | 443 next_server_change_id_(0), |
414 sync_factory_(this), | 444 sync_factory_(this) { |
415 init_loop_(NULL) { | 445 ViewManagerPrivate(view_manager).set_synchronizer(this); |
416 ConnectToService( | |
417 ViewManagerPrivate(view_manager_).service_provider(), | |
418 "mojo:mojo_view_manager", | |
419 &service_); | |
420 service_.set_client(this); | |
421 | |
422 // Start a runloop. This loop is quit when the server tells us about the | |
423 // connection (OnConnectionEstablished()). | |
424 base::RunLoop loop; | |
425 init_loop_ = &loop; | |
426 init_loop_->Run(); | |
427 init_loop_ = NULL; | |
428 } | 446 } |
429 | 447 |
430 ViewManagerSynchronizer::~ViewManagerSynchronizer() { | 448 ViewManagerSynchronizer::~ViewManagerSynchronizer() { |
431 Sync(); | 449 Sync(); |
432 } | 450 } |
433 | 451 |
434 TransportNodeId ViewManagerSynchronizer::CreateViewTreeNode() { | 452 TransportNodeId ViewManagerSynchronizer::CreateViewTreeNode() { |
435 DCHECK(connected_); | 453 DCHECK(connected_); |
436 const TransportNodeId node_id( | 454 const TransportNodeId node_id( |
437 MakeTransportId(connection_id_, ++next_id_)); | 455 MakeTransportId(connection_id_, ++next_id_)); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 } | 528 } |
511 | 529 |
512 void ViewManagerSynchronizer::SetViewContents(TransportViewId view_id, | 530 void ViewManagerSynchronizer::SetViewContents(TransportViewId view_id, |
513 const SkBitmap& contents) { | 531 const SkBitmap& contents) { |
514 DCHECK(connected_); | 532 DCHECK(connected_); |
515 pending_transactions_.push_back( | 533 pending_transactions_.push_back( |
516 new SetViewContentsTransaction(view_id, contents, this)); | 534 new SetViewContentsTransaction(view_id, contents, this)); |
517 Sync(); | 535 Sync(); |
518 } | 536 } |
519 | 537 |
| 538 void ViewManagerSynchronizer::Embed(const String& url, |
| 539 TransportNodeId node_id) { |
| 540 DCHECK(connected_); |
| 541 pending_transactions_.push_back(new EmbedTransaction(url, node_id, this)); |
| 542 Sync(); |
| 543 } |
| 544 |
| 545 //////////////////////////////////////////////////////////////////////////////// |
| 546 // ViewManagerSynchronizer, InterfaceImpl overrides: |
| 547 |
| 548 void ViewManagerSynchronizer::OnConnectionEstablished() { |
| 549 service_ = client(); |
| 550 } |
| 551 |
520 //////////////////////////////////////////////////////////////////////////////// | 552 //////////////////////////////////////////////////////////////////////////////// |
521 // ViewManagerSynchronizer, IViewManagerClient implementation: | 553 // ViewManagerSynchronizer, IViewManagerClient implementation: |
522 | 554 |
523 void ViewManagerSynchronizer::OnViewManagerConnectionEstablished( | 555 void ViewManagerSynchronizer::OnViewManagerConnectionEstablished( |
524 TransportConnectionId connection_id, | 556 TransportConnectionId connection_id, |
525 TransportChangeId next_server_change_id, | 557 TransportChangeId next_server_change_id, |
526 mojo::Array<INodePtr> nodes) { | 558 mojo::Array<INodePtr> nodes) { |
527 connected_ = true; | 559 connected_ = true; |
528 connection_id_ = connection_id; | 560 connection_id_ = connection_id; |
529 next_server_change_id_ = next_server_change_id; | 561 next_server_change_id_ = next_server_change_id; |
530 | 562 |
531 ViewManagerPrivate(view_manager_).set_root( | 563 ViewManagerPrivate(view_manager()).set_root( |
532 BuildNodeTree(view_manager_, nodes)); | 564 BuildNodeTree(view_manager(), nodes)); |
533 if (init_loop_) | |
534 init_loop_->Quit(); | |
535 | 565 |
536 Sync(); | 566 Sync(); |
| 567 |
| 568 base::MessageLoop::current()->Quit(); |
537 } | 569 } |
538 | 570 |
539 void ViewManagerSynchronizer::OnServerChangeIdAdvanced( | 571 void ViewManagerSynchronizer::OnServerChangeIdAdvanced( |
540 uint32_t next_server_change_id) { | 572 uint32_t next_server_change_id) { |
541 next_server_change_id_ = next_server_change_id; | 573 next_server_change_id_ = next_server_change_id; |
542 } | 574 } |
543 | 575 |
544 void ViewManagerSynchronizer::OnNodeBoundsChanged(uint32 node_id, | 576 void ViewManagerSynchronizer::OnNodeBoundsChanged(uint32 node_id, |
545 RectPtr old_bounds, | 577 RectPtr old_bounds, |
546 RectPtr new_bounds) { | 578 RectPtr new_bounds) { |
547 ViewTreeNode* node = view_manager_->GetNodeById(node_id); | 579 ViewTreeNode* node = view_manager()->GetNodeById(node_id); |
548 ViewTreeNodePrivate(node).LocalSetBounds(old_bounds.To<gfx::Rect>(), | 580 ViewTreeNodePrivate(node).LocalSetBounds(old_bounds.To<gfx::Rect>(), |
549 new_bounds.To<gfx::Rect>()); | 581 new_bounds.To<gfx::Rect>()); |
550 } | 582 } |
551 | 583 |
552 void ViewManagerSynchronizer::OnNodeHierarchyChanged( | 584 void ViewManagerSynchronizer::OnNodeHierarchyChanged( |
553 uint32_t node_id, | 585 uint32_t node_id, |
554 uint32_t new_parent_id, | 586 uint32_t new_parent_id, |
555 uint32_t old_parent_id, | 587 uint32_t old_parent_id, |
556 TransportChangeId server_change_id, | 588 TransportChangeId server_change_id, |
557 mojo::Array<INodePtr> nodes) { | 589 mojo::Array<INodePtr> nodes) { |
558 // TODO: deal with |nodes|. | 590 // TODO: deal with |nodes|. |
559 next_server_change_id_ = server_change_id + 1; | 591 next_server_change_id_ = server_change_id + 1; |
560 | 592 |
561 BuildNodeTree(view_manager_, nodes); | 593 BuildNodeTree(view_manager(), nodes); |
562 | 594 |
563 ViewTreeNode* new_parent = view_manager_->GetNodeById(new_parent_id); | 595 ViewTreeNode* new_parent = view_manager()->GetNodeById(new_parent_id); |
564 ViewTreeNode* old_parent = view_manager_->GetNodeById(old_parent_id); | 596 ViewTreeNode* old_parent = view_manager()->GetNodeById(old_parent_id); |
565 ViewTreeNode* node = view_manager_->GetNodeById(node_id); | 597 ViewTreeNode* node = view_manager()->GetNodeById(node_id); |
566 if (new_parent) | 598 if (new_parent) |
567 ViewTreeNodePrivate(new_parent).LocalAddChild(node); | 599 ViewTreeNodePrivate(new_parent).LocalAddChild(node); |
568 else | 600 else |
569 ViewTreeNodePrivate(old_parent).LocalRemoveChild(node); | 601 ViewTreeNodePrivate(old_parent).LocalRemoveChild(node); |
570 } | 602 } |
571 | 603 |
572 void ViewManagerSynchronizer::OnNodeDeleted(uint32_t node_id, | 604 void ViewManagerSynchronizer::OnNodeDeleted(uint32_t node_id, |
573 uint32_t server_change_id) { | 605 uint32_t server_change_id) { |
574 next_server_change_id_ = server_change_id + 1; | 606 next_server_change_id_ = server_change_id + 1; |
575 | 607 |
576 ViewTreeNode* node = view_manager_->GetNodeById(node_id); | 608 ViewTreeNode* node = view_manager()->GetNodeById(node_id); |
577 if (node) | 609 if (node) |
578 ViewTreeNodePrivate(node).LocalDestroy(); | 610 ViewTreeNodePrivate(node).LocalDestroy(); |
579 } | 611 } |
580 | 612 |
581 void ViewManagerSynchronizer::OnNodeViewReplaced(uint32_t node_id, | 613 void ViewManagerSynchronizer::OnNodeViewReplaced(uint32_t node_id, |
582 uint32_t new_view_id, | 614 uint32_t new_view_id, |
583 uint32_t old_view_id) { | 615 uint32_t old_view_id) { |
584 ViewTreeNode* node = view_manager_->GetNodeById(node_id); | 616 ViewTreeNode* node = view_manager()->GetNodeById(node_id); |
585 View* new_view = view_manager_->GetViewById(new_view_id); | 617 View* new_view = view_manager()->GetViewById(new_view_id); |
586 if (!new_view && new_view_id != 0) { | 618 if (!new_view && new_view_id != 0) { |
587 // This client wasn't aware of this View until now. | 619 // This client wasn't aware of this View until now. |
588 new_view = ViewPrivate::LocalCreate(); | 620 new_view = ViewPrivate::LocalCreate(); |
589 ViewPrivate private_view(new_view); | 621 ViewPrivate private_view(new_view); |
590 private_view.set_view_manager(view_manager_); | 622 private_view.set_view_manager(view_manager()); |
591 private_view.set_id(new_view_id); | 623 private_view.set_id(new_view_id); |
592 private_view.set_node(node); | 624 private_view.set_node(node); |
593 ViewManagerPrivate(view_manager_).AddView(new_view->id(), new_view); | 625 ViewManagerPrivate(view_manager()).AddView(new_view->id(), new_view); |
594 } | 626 } |
595 View* old_view = view_manager_->GetViewById(old_view_id); | 627 View* old_view = view_manager()->GetViewById(old_view_id); |
596 DCHECK_EQ(old_view, node->active_view()); | 628 DCHECK_EQ(old_view, node->active_view()); |
597 ViewTreeNodePrivate(node).LocalSetActiveView(new_view); | 629 ViewTreeNodePrivate(node).LocalSetActiveView(new_view); |
598 } | 630 } |
599 | 631 |
600 void ViewManagerSynchronizer::OnViewDeleted(uint32_t view_id) { | 632 void ViewManagerSynchronizer::OnViewDeleted(uint32_t view_id) { |
601 View* view = view_manager_->GetViewById(view_id); | 633 View* view = view_manager()->GetViewById(view_id); |
602 if (view) | 634 if (view) |
603 ViewPrivate(view).LocalDestroy(); | 635 ViewPrivate(view).LocalDestroy(); |
604 } | 636 } |
605 | 637 |
606 //////////////////////////////////////////////////////////////////////////////// | 638 //////////////////////////////////////////////////////////////////////////////// |
607 // ViewManagerSynchronizer, private: | 639 // ViewManagerSynchronizer, private: |
608 | 640 |
609 void ViewManagerSynchronizer::Sync() { | 641 void ViewManagerSynchronizer::Sync() { |
610 // The service connection may not be set up yet. OnConnectionEstablished() | 642 // The service connection may not be set up yet. OnConnectionEstablished() |
611 // will schedule another sync when it is. | 643 // will schedule another sync when it is. |
(...skipping 10 matching lines...) Expand all Loading... |
622 void ViewManagerSynchronizer::RemoveFromPendingQueue( | 654 void ViewManagerSynchronizer::RemoveFromPendingQueue( |
623 ViewManagerTransaction* transaction) { | 655 ViewManagerTransaction* transaction) { |
624 DCHECK_EQ(transaction, pending_transactions_.front()); | 656 DCHECK_EQ(transaction, pending_transactions_.front()); |
625 pending_transactions_.erase(pending_transactions_.begin()); | 657 pending_transactions_.erase(pending_transactions_.begin()); |
626 if (pending_transactions_.empty() && !changes_acked_callback_.is_null()) | 658 if (pending_transactions_.empty() && !changes_acked_callback_.is_null()) |
627 changes_acked_callback_.Run(); | 659 changes_acked_callback_.Run(); |
628 } | 660 } |
629 | 661 |
630 } // namespace view_manager | 662 } // namespace view_manager |
631 } // namespace mojo | 663 } // namespace mojo |
OLD | NEW |