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_service_impl.h" | 5 #include "mojo/services/view_manager/view_manager_service_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" | 8 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" |
9 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" | 9 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h" |
10 #include "mojo/services/view_manager/node.h" | 10 #include "mojo/services/view_manager/node.h" |
(...skipping 19 matching lines...) Expand all Loading... |
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 ViewManagerServiceImpl::ViewManagerServiceImpl( | 36 ViewManagerServiceImpl::ViewManagerServiceImpl( |
37 RootNodeManager* root_node_manager, | 37 RootNodeManager* root_node_manager, |
38 ConnectionSpecificId creator_id, | 38 ConnectionSpecificId creator_id, |
39 const std::string& creator_url, | 39 const std::string& creator_url, |
40 const std::string& url) | 40 const std::string& url, |
| 41 const NodeId& root_id) |
41 : root_node_manager_(root_node_manager), | 42 : root_node_manager_(root_node_manager), |
42 id_(root_node_manager_->GetAndAdvanceNextConnectionId()), | 43 id_(root_node_manager_->GetAndAdvanceNextConnectionId()), |
43 url_(url), | 44 url_(url), |
44 creator_id_(creator_id), | 45 creator_id_(creator_id), |
45 creator_url_(creator_url), | 46 creator_url_(creator_url), |
46 delete_on_connection_error_(false) { | 47 delete_on_connection_error_(false) { |
| 48 if (root_id != InvalidNodeId()) { |
| 49 CHECK(GetNode(root_id)); |
| 50 roots_.insert(NodeIdToTransportId(root_id)); |
| 51 } |
47 } | 52 } |
48 | 53 |
49 ViewManagerServiceImpl::~ViewManagerServiceImpl() { | 54 ViewManagerServiceImpl::~ViewManagerServiceImpl() { |
50 // Delete any views we created. | 55 // Delete any views we created. |
51 while (!view_map_.empty()) { | 56 while (!view_map_.empty()) { |
52 bool result = DeleteViewImpl(this, view_map_.begin()->second->id()); | 57 bool result = DeleteViewImpl(this, view_map_.begin()->second->id()); |
53 DCHECK(result); | 58 DCHECK(result); |
54 } | 59 } |
55 | 60 |
56 // Ditto the nodes. | 61 // Ditto the nodes. |
(...skipping 17 matching lines...) Expand all Loading... |
74 } | 79 } |
75 | 80 |
76 const View* ViewManagerServiceImpl::GetView(const ViewId& id) const { | 81 const View* ViewManagerServiceImpl::GetView(const ViewId& id) const { |
77 if (id_ == id.connection_id) { | 82 if (id_ == id.connection_id) { |
78 ViewMap::const_iterator i = view_map_.find(id.view_id); | 83 ViewMap::const_iterator i = view_map_.find(id.view_id); |
79 return i == view_map_.end() ? NULL : i->second; | 84 return i == view_map_.end() ? NULL : i->second; |
80 } | 85 } |
81 return root_node_manager_->GetView(id); | 86 return root_node_manager_->GetView(id); |
82 } | 87 } |
83 | 88 |
84 void ViewManagerServiceImpl::SetRoots(const Array<Id>& node_ids) { | |
85 DCHECK(roots_.empty()); | |
86 NodeIdSet roots; | |
87 for (size_t i = 0; i < node_ids.size(); ++i) { | |
88 DCHECK(GetNode(NodeIdFromTransportId(node_ids[i]))); | |
89 roots.insert(node_ids[i]); | |
90 } | |
91 roots_.swap(roots); | |
92 } | |
93 | |
94 void ViewManagerServiceImpl::OnViewManagerServiceImplDestroyed( | 89 void ViewManagerServiceImpl::OnViewManagerServiceImplDestroyed( |
95 ConnectionSpecificId id) { | 90 ConnectionSpecificId id) { |
96 if (creator_id_ == id) | 91 if (creator_id_ == id) |
97 creator_id_ = kRootConnection; | 92 creator_id_ = kRootConnection; |
98 } | 93 } |
99 | 94 |
100 void ViewManagerServiceImpl::ProcessNodeBoundsChanged( | 95 void ViewManagerServiceImpl::ProcessNodeBoundsChanged( |
101 const Node* node, | 96 const Node* node, |
102 const gfx::Rect& old_bounds, | 97 const gfx::Rect& old_bounds, |
103 const gfx::Rect& new_bounds, | 98 const gfx::Rect& new_bounds, |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 bool ViewManagerServiceImpl::CanSetFocus(const Node* node) const { | 331 bool ViewManagerServiceImpl::CanSetFocus(const Node* node) const { |
337 // TODO(beng): security. | 332 // TODO(beng): security. |
338 return true; | 333 return true; |
339 } | 334 } |
340 | 335 |
341 bool ViewManagerServiceImpl::CanGetNodeTree(const Node* node) const { | 336 bool ViewManagerServiceImpl::CanGetNodeTree(const Node* node) const { |
342 return node && | 337 return node && |
343 (IsNodeDescendantOfRoots(node) || node->id().connection_id == id_); | 338 (IsNodeDescendantOfRoots(node) || node->id().connection_id == id_); |
344 } | 339 } |
345 | 340 |
346 bool ViewManagerServiceImpl::CanEmbed( | 341 bool ViewManagerServiceImpl::CanEmbed(Id transport_node_id) const { |
347 const mojo::Array<uint32_t>& node_ids) const { | 342 const Node* node = GetNode(NodeIdFromTransportId(transport_node_id)); |
348 for (size_t i = 0; i < node_ids.size(); ++i) { | 343 return node && node->id().connection_id == id_; |
349 const Node* node = GetNode(NodeIdFromTransportId(node_ids[i])); | |
350 if (!node || node->id().connection_id != id_) | |
351 return false; | |
352 } | |
353 return node_ids.size() > 0; | |
354 } | 344 } |
355 | 345 |
356 bool ViewManagerServiceImpl::CanSetNodeVisibility(const Node* node, | 346 bool ViewManagerServiceImpl::CanSetNodeVisibility(const Node* node, |
357 bool visibile) const { | 347 bool visibile) const { |
358 return node && | 348 return node && |
359 (node->id().connection_id == id_ || | 349 (node->id().connection_id == id_ || |
360 roots_.find(NodeIdToTransportId(node->id())) != roots_.end()) && | 350 roots_.find(NodeIdToTransportId(node->id())) != roots_.end()) && |
361 node->IsVisible() != visibile; | 351 node->IsVisible() != visibile; |
362 } | 352 } |
363 | 353 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 | 409 |
420 void ViewManagerServiceImpl::RemoveFromKnown(const Node* node) { | 410 void ViewManagerServiceImpl::RemoveFromKnown(const Node* node) { |
421 if (node->id().connection_id == id_) | 411 if (node->id().connection_id == id_) |
422 return; | 412 return; |
423 known_nodes_.erase(NodeIdToTransportId(node->id())); | 413 known_nodes_.erase(NodeIdToTransportId(node->id())); |
424 std::vector<const Node*> children = node->GetChildren(); | 414 std::vector<const Node*> children = node->GetChildren(); |
425 for (size_t i = 0; i < children.size(); ++i) | 415 for (size_t i = 0; i < children.size(); ++i) |
426 RemoveFromKnown(children[i]); | 416 RemoveFromKnown(children[i]); |
427 } | 417 } |
428 | 418 |
429 bool ViewManagerServiceImpl::AddRoots( | 419 bool ViewManagerServiceImpl::AddRoot(Id transport_node_id) { |
430 const std::vector<Id>& node_ids) { | 420 if (roots_.count(transport_node_id) > 0) |
| 421 return false; |
| 422 |
431 std::vector<const Node*> to_send; | 423 std::vector<const Node*> to_send; |
432 bool did_add_root = false; | 424 const NodeId node_id(NodeIdFromTransportId(transport_node_id)); |
433 for (size_t i = 0; i < node_ids.size(); ++i) { | 425 CHECK_EQ(creator_id_, node_id.connection_id); |
434 CHECK_EQ(creator_id_, NodeIdFromTransportId(node_ids[i]).connection_id); | 426 roots_.insert(transport_node_id); |
435 if (roots_.count(node_ids[i]) > 0) | 427 Node* node = GetNode(node_id); |
436 continue; | 428 CHECK(node); |
437 | 429 if (known_nodes_.count(transport_node_id) == 0) { |
438 did_add_root = true; | 430 GetUnknownNodesFrom(node, &to_send); |
439 roots_.insert(node_ids[i]); | 431 } else { |
440 Node* node = GetNode(NodeIdFromTransportId(node_ids[i])); | 432 // Even though the connection knows about the new root we need to tell it |
441 DCHECK(node); | 433 // |node| is now a root. |
442 if (known_nodes_.count(node_ids[i]) == 0) { | 434 to_send.push_back(node); |
443 GetUnknownNodesFrom(node, &to_send); | |
444 } else { | |
445 // Even though the connection knows about the new root we need to tell it | |
446 // |node| is now a root. | |
447 to_send.push_back(node); | |
448 } | |
449 } | 435 } |
450 | 436 |
451 if (!did_add_root) | 437 client()->OnRootAdded(NodesToNodeDatas(to_send)); |
452 return false; | |
453 | |
454 client()->OnRootsAdded(NodesToNodeDatas(to_send)); | |
455 return true; | 438 return true; |
456 } | 439 } |
457 | 440 |
458 bool ViewManagerServiceImpl::IsNodeDescendantOfRoots(const Node* node) const { | 441 bool ViewManagerServiceImpl::IsNodeDescendantOfRoots(const Node* node) const { |
459 if (roots_.empty()) | 442 if (roots_.empty()) |
460 return true; | 443 return true; |
461 if (!node) | 444 if (!node) |
462 return false; | 445 return false; |
463 const Id invalid_node_id = | 446 const Id invalid_node_id = |
464 NodeIdToTransportId(InvalidNodeId()); | 447 NodeIdToTransportId(InvalidNodeId()); |
(...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
743 const bool success = CanSetNodeVisibility(node, visible); | 726 const bool success = CanSetNodeVisibility(node, visible); |
744 if (success) { | 727 if (success) { |
745 DCHECK(node); | 728 DCHECK(node); |
746 node->SetVisible(visible); | 729 node->SetVisible(visible); |
747 } | 730 } |
748 // TODO(sky): need to notify of visibility changes. | 731 // TODO(sky): need to notify of visibility changes. |
749 callback.Run(success); | 732 callback.Run(success); |
750 } | 733 } |
751 | 734 |
752 void ViewManagerServiceImpl::Embed(const String& url, | 735 void ViewManagerServiceImpl::Embed(const String& url, |
753 Array<uint32_t> node_ids, | 736 Id transport_node_id, |
754 const Callback<void(bool)>& callback) { | 737 const Callback<void(bool)>& callback) { |
755 bool success = CanEmbed(node_ids); | 738 bool success = CanEmbed(transport_node_id); |
756 if (success) { | 739 if (success) { |
757 // We may already have this connection, if so reuse it. | 740 // We may already have this connection, if so reuse it. |
758 ViewManagerServiceImpl* existing_connection = | 741 ViewManagerServiceImpl* existing_connection = |
759 root_node_manager_->GetConnectionByCreator(id_, url.To<std::string>()); | 742 root_node_manager_->GetConnectionByCreator(id_, url.To<std::string>()); |
760 if (existing_connection) | 743 if (existing_connection) |
761 success = existing_connection->AddRoots(node_ids.storage()); | 744 success = existing_connection->AddRoot(transport_node_id); |
762 else | 745 else |
763 root_node_manager_->Embed(id_, url, node_ids); | 746 root_node_manager_->Embed(id_, url, transport_node_id); |
764 } | 747 } |
765 callback.Run(success); | 748 callback.Run(success); |
766 } | 749 } |
767 | 750 |
768 void ViewManagerServiceImpl::DispatchOnViewInputEvent(Id transport_view_id, | 751 void ViewManagerServiceImpl::DispatchOnViewInputEvent(Id transport_view_id, |
769 EventPtr event) { | 752 EventPtr event) { |
770 // We only allow the WM to dispatch events. At some point this function will | 753 // We only allow the WM to dispatch events. At some point this function will |
771 // move to a separate interface and the check can go away. | 754 // move to a separate interface and the check can go away. |
772 if (id_ != kWindowManagerConnection) | 755 if (id_ != kWindowManagerConnection) |
773 return; | 756 return; |
(...skipping 23 matching lines...) Expand all Loading... |
797 client()->OnViewManagerConnectionEstablished( | 780 client()->OnViewManagerConnectionEstablished( |
798 id_, | 781 id_, |
799 creator_url_, | 782 creator_url_, |
800 root_node_manager_->next_server_change_id(), | 783 root_node_manager_->next_server_change_id(), |
801 NodesToNodeDatas(to_send)); | 784 NodesToNodeDatas(to_send)); |
802 } | 785 } |
803 | 786 |
804 } // namespace service | 787 } // namespace service |
805 } // namespace view_manager | 788 } // namespace view_manager |
806 } // namespace mojo | 789 } // namespace mojo |
OLD | NEW |