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 "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "mojo/converters/geometry/geometry_type_converters.h" | 9 #include "mojo/converters/geometry/geometry_type_converters.h" |
10 #include "mojo/converters/input_events/input_events_type_converters.h" | 10 #include "mojo/converters/input_events/input_events_type_converters.h" |
11 #include "mojo/converters/surfaces/surfaces_type_converters.h" | 11 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
| 12 #include "mojo/services/public/interfaces/window_manager/window_manager_internal
.mojom.h" |
12 #include "mojo/services/view_manager/connection_manager.h" | 13 #include "mojo/services/view_manager/connection_manager.h" |
13 #include "mojo/services/view_manager/default_access_policy.h" | 14 #include "mojo/services/view_manager/default_access_policy.h" |
14 #include "mojo/services/view_manager/server_view.h" | 15 #include "mojo/services/view_manager/server_view.h" |
15 #include "mojo/services/view_manager/window_manager_access_policy.h" | 16 #include "mojo/services/view_manager/window_manager_access_policy.h" |
16 | 17 |
17 namespace mojo { | 18 namespace mojo { |
18 namespace service { | 19 namespace service { |
19 | 20 |
20 ViewManagerServiceImpl::ViewManagerServiceImpl( | 21 ViewManagerServiceImpl::ViewManagerServiceImpl( |
21 ConnectionManager* connection_manager, | 22 ConnectionManager* connection_manager, |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 } | 59 } |
59 | 60 |
60 const ServerView* ViewManagerServiceImpl::GetView(const ViewId& id) const { | 61 const ServerView* ViewManagerServiceImpl::GetView(const ViewId& id) const { |
61 if (id_ == id.connection_id) { | 62 if (id_ == id.connection_id) { |
62 ViewMap::const_iterator i = view_map_.find(id.view_id); | 63 ViewMap::const_iterator i = view_map_.find(id.view_id); |
63 return i == view_map_.end() ? NULL : i->second; | 64 return i == view_map_.end() ? NULL : i->second; |
64 } | 65 } |
65 return connection_manager_->GetView(id); | 66 return connection_manager_->GetView(id); |
66 } | 67 } |
67 | 68 |
68 ErrorCode ViewManagerServiceImpl::CreateView(const ViewId& view_id) { | |
69 if (view_id.connection_id != id_) | |
70 return ERROR_CODE_ILLEGAL_ARGUMENT; | |
71 if (view_map_.find(view_id.view_id) != view_map_.end()) | |
72 return ERROR_CODE_VALUE_IN_USE; | |
73 view_map_[view_id.view_id] = new ServerView(connection_manager_, view_id); | |
74 known_views_.insert(ViewIdToTransportId(view_id)); | |
75 return ERROR_CODE_NONE; | |
76 } | |
77 | |
78 bool ViewManagerServiceImpl::IsRoot(const ViewId& id) const { | 69 bool ViewManagerServiceImpl::IsRoot(const ViewId& id) const { |
79 return root_.get() && *root_ == id; | 70 return root_.get() && *root_ == id; |
80 } | 71 } |
81 | 72 |
82 void ViewManagerServiceImpl::OnWillDestroyViewManagerServiceImpl( | 73 void ViewManagerServiceImpl::OnWillDestroyViewManagerServiceImpl( |
83 ViewManagerServiceImpl* connection) { | 74 ViewManagerServiceImpl* connection) { |
84 if (creator_id_ == connection->id()) | 75 if (creator_id_ == connection->id()) |
85 creator_id_ = kInvalidConnectionId; | 76 creator_id_ = kInvalidConnectionId; |
86 if (connection->root_ && connection->root_->connection_id == id_ && | 77 if (connection->root_ && connection->root_->connection_id == id_ && |
87 view_map_.count(connection->root_->view_id) > 0) { | 78 view_map_.count(connection->root_->view_id) > 0) { |
88 client()->OnEmbeddedAppDisconnected( | 79 client()->OnEmbeddedAppDisconnected( |
89 ViewIdToTransportId(*connection->root_)); | 80 ViewIdToTransportId(*connection->root_)); |
90 } | 81 } |
91 if (root_.get() && root_->connection_id == connection->id()) | 82 if (root_.get() && root_->connection_id == connection->id()) |
92 root_.reset(); | 83 root_.reset(); |
93 } | 84 } |
94 | 85 |
| 86 ErrorCode ViewManagerServiceImpl::CreateView(const ViewId& view_id) { |
| 87 if (view_id.connection_id != id_) |
| 88 return ERROR_CODE_ILLEGAL_ARGUMENT; |
| 89 if (view_map_.find(view_id.view_id) != view_map_.end()) |
| 90 return ERROR_CODE_VALUE_IN_USE; |
| 91 view_map_[view_id.view_id] = new ServerView(connection_manager_, view_id); |
| 92 known_views_.insert(ViewIdToTransportId(view_id)); |
| 93 return ERROR_CODE_NONE; |
| 94 } |
| 95 |
| 96 bool ViewManagerServiceImpl::AddView(const ViewId& parent_id, |
| 97 const ViewId& child_id) { |
| 98 ServerView* parent = GetView(parent_id); |
| 99 ServerView* child = GetView(child_id); |
| 100 if (parent && child && child->parent() != parent && |
| 101 !child->Contains(parent) && access_policy_->CanAddView(parent, child)) { |
| 102 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
| 103 parent->Add(child); |
| 104 return true; |
| 105 } |
| 106 return false; |
| 107 } |
| 108 |
| 109 std::vector<const ServerView*> ViewManagerServiceImpl::GetViewTree( |
| 110 const ViewId& view_id) const { |
| 111 const ServerView* view = GetView(view_id); |
| 112 std::vector<const ServerView*> views; |
| 113 if (view) |
| 114 GetViewTreeImpl(view, &views); |
| 115 return views; |
| 116 } |
| 117 |
| 118 bool ViewManagerServiceImpl::SetViewVisibility(const ViewId& view_id, |
| 119 bool visible) { |
| 120 ServerView* view = GetView(view_id); |
| 121 if (!view || view->visible() == visible || |
| 122 !access_policy_->CanChangeViewVisibility(view)) { |
| 123 return false; |
| 124 } |
| 125 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
| 126 view->SetVisible(visible); |
| 127 return true; |
| 128 } |
| 129 |
| 130 bool ViewManagerServiceImpl::Embed( |
| 131 const std::string& url, |
| 132 const ViewId& view_id, |
| 133 InterfaceRequest<ServiceProvider> service_provider) { |
| 134 const ServerView* view = GetView(view_id); |
| 135 if (!view || !access_policy_->CanEmbed(view)) |
| 136 return false; |
| 137 |
| 138 // Only allow a node to be the root for one connection. |
| 139 ViewManagerServiceImpl* existing_owner = |
| 140 connection_manager_->GetConnectionWithRoot(view_id); |
| 141 |
| 142 ConnectionManager::ScopedChange change(this, connection_manager_, true); |
| 143 RemoveChildrenAsPartOfEmbed(view_id); |
| 144 if (existing_owner) { |
| 145 // Never message the originating connection. |
| 146 connection_manager_->OnConnectionMessagedClient(id_); |
| 147 existing_owner->RemoveRoot(); |
| 148 } |
| 149 connection_manager_->EmbedAtView(id_, url, view_id, service_provider.Pass()); |
| 150 return true; |
| 151 } |
| 152 |
95 void ViewManagerServiceImpl::ProcessViewBoundsChanged( | 153 void ViewManagerServiceImpl::ProcessViewBoundsChanged( |
96 const ServerView* view, | 154 const ServerView* view, |
97 const gfx::Rect& old_bounds, | 155 const gfx::Rect& old_bounds, |
98 const gfx::Rect& new_bounds, | 156 const gfx::Rect& new_bounds, |
99 bool originated_change) { | 157 bool originated_change) { |
100 if (originated_change || !IsViewKnown(view)) | 158 if (originated_change || !IsViewKnown(view)) |
101 return; | 159 return; |
102 client()->OnViewBoundsChanged(ViewIdToTransportId(view->id()), | 160 client()->OnViewBoundsChanged(ViewIdToTransportId(view->id()), |
103 Rect::From(old_bounds), | 161 Rect::From(old_bounds), |
104 Rect::From(new_bounds)); | 162 Rect::From(new_bounds)); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 ViewIdToTransportId(relative_view->id()), | 239 ViewIdToTransportId(relative_view->id()), |
182 direction); | 240 direction); |
183 } | 241 } |
184 | 242 |
185 void ViewManagerServiceImpl::ProcessViewDeleted(const ViewId& view, | 243 void ViewManagerServiceImpl::ProcessViewDeleted(const ViewId& view, |
186 bool originated_change) { | 244 bool originated_change) { |
187 if (view.connection_id == id_) | 245 if (view.connection_id == id_) |
188 view_map_.erase(view.view_id); | 246 view_map_.erase(view.view_id); |
189 | 247 |
190 const bool in_known = known_views_.erase(ViewIdToTransportId(view)) > 0; | 248 const bool in_known = known_views_.erase(ViewIdToTransportId(view)) > 0; |
| 249 |
191 if (IsRoot(view)) | 250 if (IsRoot(view)) |
192 root_.reset(); | 251 root_.reset(); |
193 | 252 |
194 if (originated_change) | 253 if (originated_change) |
195 return; | 254 return; |
196 | 255 |
197 if (in_known) { | 256 if (in_known) { |
198 client()->OnViewDeleted(ViewIdToTransportId(view)); | 257 client()->OnViewDeleted(ViewIdToTransportId(view)); |
199 connection_manager_->OnConnectionMessagedClient(id_); | 258 connection_manager_->OnConnectionMessagedClient(id_); |
200 } | 259 } |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
408 connection_manager_->GetConnection(view->id().connection_id); | 467 connection_manager_->GetConnection(view->id().connection_id); |
409 success = connection && connection->DeleteViewImpl(this, view); | 468 success = connection && connection->DeleteViewImpl(this, view); |
410 } | 469 } |
411 callback.Run(success); | 470 callback.Run(success); |
412 } | 471 } |
413 | 472 |
414 void ViewManagerServiceImpl::AddView( | 473 void ViewManagerServiceImpl::AddView( |
415 Id parent_id, | 474 Id parent_id, |
416 Id child_id, | 475 Id child_id, |
417 const Callback<void(bool)>& callback) { | 476 const Callback<void(bool)>& callback) { |
418 bool success = false; | 477 callback.Run(AddView(ViewIdFromTransportId(parent_id), |
419 ServerView* parent = GetView(ViewIdFromTransportId(parent_id)); | 478 ViewIdFromTransportId(child_id))); |
420 ServerView* child = GetView(ViewIdFromTransportId(child_id)); | |
421 if (parent && child && child->parent() != parent && | |
422 !child->Contains(parent) && access_policy_->CanAddView(parent, child)) { | |
423 success = true; | |
424 ConnectionManager::ScopedChange change(this, connection_manager_, false); | |
425 parent->Add(child); | |
426 } | |
427 callback.Run(success); | |
428 } | 479 } |
429 | 480 |
430 void ViewManagerServiceImpl::RemoveViewFromParent( | 481 void ViewManagerServiceImpl::RemoveViewFromParent( |
431 Id view_id, | 482 Id view_id, |
432 const Callback<void(bool)>& callback) { | 483 const Callback<void(bool)>& callback) { |
433 bool success = false; | 484 bool success = false; |
434 ServerView* view = GetView(ViewIdFromTransportId(view_id)); | 485 ServerView* view = GetView(ViewIdFromTransportId(view_id)); |
435 if (view && view->parent() && access_policy_->CanRemoveViewFromParent(view)) { | 486 if (view && view->parent() && access_policy_->CanRemoveViewFromParent(view)) { |
436 success = true; | 487 success = true; |
437 ConnectionManager::ScopedChange change(this, connection_manager_, false); | 488 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
(...skipping 14 matching lines...) Expand all Loading... |
452 ConnectionManager::ScopedChange change(this, connection_manager_, false); | 503 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
453 view->parent()->Reorder(view, relative_view, direction); | 504 view->parent()->Reorder(view, relative_view, direction); |
454 connection_manager_->ProcessViewReorder(view, relative_view, direction); | 505 connection_manager_->ProcessViewReorder(view, relative_view, direction); |
455 } | 506 } |
456 callback.Run(success); | 507 callback.Run(success); |
457 } | 508 } |
458 | 509 |
459 void ViewManagerServiceImpl::GetViewTree( | 510 void ViewManagerServiceImpl::GetViewTree( |
460 Id view_id, | 511 Id view_id, |
461 const Callback<void(Array<ViewDataPtr>)>& callback) { | 512 const Callback<void(Array<ViewDataPtr>)>& callback) { |
462 ServerView* view = GetView(ViewIdFromTransportId(view_id)); | 513 std::vector<const ServerView*> views( |
463 std::vector<const ServerView*> views; | 514 GetViewTree(ViewIdFromTransportId(view_id))); |
464 if (view) { | |
465 GetViewTreeImpl(view, &views); | |
466 // TODO(sky): this should map in views that weren't none. | |
467 } | |
468 callback.Run(ViewsToViewDatas(views)); | 515 callback.Run(ViewsToViewDatas(views)); |
469 } | 516 } |
470 | 517 |
471 void ViewManagerServiceImpl::SetViewSurfaceId( | 518 void ViewManagerServiceImpl::SetViewSurfaceId( |
472 Id view_id, | 519 Id view_id, |
473 SurfaceIdPtr surface_id, | 520 SurfaceIdPtr surface_id, |
474 const Callback<void(bool)>& callback) { | 521 const Callback<void(bool)>& callback) { |
475 // TODO(sky): add coverage of not being able to set for random node. | 522 // TODO(sky): add coverage of not being able to set for random node. |
476 ServerView* view = GetView(ViewIdFromTransportId(view_id)); | 523 ServerView* view = GetView(ViewIdFromTransportId(view_id)); |
477 if (!view || !access_policy_->CanSetViewSurfaceId(view)) { | 524 if (!view || !access_policy_->CanSetViewSurfaceId(view)) { |
(...skipping 14 matching lines...) Expand all Loading... |
492 ConnectionManager::ScopedChange change(this, connection_manager_, false); | 539 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
493 view->SetBounds(bounds.To<gfx::Rect>()); | 540 view->SetBounds(bounds.To<gfx::Rect>()); |
494 } | 541 } |
495 callback.Run(success); | 542 callback.Run(success); |
496 } | 543 } |
497 | 544 |
498 void ViewManagerServiceImpl::SetViewVisibility( | 545 void ViewManagerServiceImpl::SetViewVisibility( |
499 Id transport_view_id, | 546 Id transport_view_id, |
500 bool visible, | 547 bool visible, |
501 const Callback<void(bool)>& callback) { | 548 const Callback<void(bool)>& callback) { |
502 ServerView* view = GetView(ViewIdFromTransportId(transport_view_id)); | 549 callback.Run( |
503 if (!view || view->visible() == visible || | 550 SetViewVisibility(ViewIdFromTransportId(transport_view_id), visible)); |
504 !access_policy_->CanChangeViewVisibility(view)) { | |
505 callback.Run(false); | |
506 return; | |
507 } | |
508 { | |
509 ConnectionManager::ScopedChange change(this, connection_manager_, false); | |
510 view->SetVisible(visible); | |
511 } | |
512 callback.Run(true); | |
513 } | 551 } |
514 | 552 |
515 void ViewManagerServiceImpl::SetViewProperty( | 553 void ViewManagerServiceImpl::SetViewProperty( |
516 uint32_t view_id, | 554 uint32_t view_id, |
517 const mojo::String& name, | 555 const mojo::String& name, |
518 mojo::Array<uint8_t> value, | 556 mojo::Array<uint8_t> value, |
519 const mojo::Callback<void(bool)>& callback) { | 557 const mojo::Callback<void(bool)>& callback) { |
520 ServerView* view = GetView(ViewIdFromTransportId(view_id)); | 558 ServerView* view = GetView(ViewIdFromTransportId(view_id)); |
521 const bool success = view && access_policy_->CanSetViewProperties(view); | 559 const bool success = view && access_policy_->CanSetViewProperties(view); |
522 if (success) { | 560 if (success) { |
523 ConnectionManager::ScopedChange change(this, connection_manager_, false); | 561 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
524 | 562 |
525 if (value.is_null()) { | 563 if (value.is_null()) { |
526 view->SetProperty(name, nullptr); | 564 view->SetProperty(name, nullptr); |
527 } else { | 565 } else { |
528 std::vector<uint8_t> data = value.To<std::vector<uint8_t>>(); | 566 std::vector<uint8_t> data = value.To<std::vector<uint8_t>>(); |
529 view->SetProperty(name, &data); | 567 view->SetProperty(name, &data); |
530 } | 568 } |
531 } | 569 } |
532 callback.Run(success); | 570 callback.Run(success); |
533 } | 571 } |
534 | 572 |
535 void ViewManagerServiceImpl::Embed( | 573 void ViewManagerServiceImpl::Embed( |
536 const String& url, | 574 const String& url, |
537 Id transport_view_id, | 575 Id transport_view_id, |
538 InterfaceRequest<ServiceProvider> service_provider, | 576 InterfaceRequest<ServiceProvider> service_provider, |
539 const Callback<void(bool)>& callback) { | 577 const Callback<void(bool)>& callback) { |
540 const ServerView* view = GetView(ViewIdFromTransportId(transport_view_id)); | 578 callback.Run(Embed(url.To<std::string>(), |
541 if (!view || !access_policy_->CanEmbed(view)) { | 579 ViewIdFromTransportId(transport_view_id), |
542 callback.Run(false); | 580 service_provider.Pass())); |
543 return; | |
544 } | |
545 | |
546 // Only allow a node to be the root for one connection. | |
547 const ViewId view_id(ViewIdFromTransportId(transport_view_id)); | |
548 ViewManagerServiceImpl* existing_owner = | |
549 connection_manager_->GetConnectionWithRoot(view_id); | |
550 | |
551 ConnectionManager::ScopedChange change(this, connection_manager_, true); | |
552 RemoveChildrenAsPartOfEmbed(view_id); | |
553 if (existing_owner) { | |
554 // Never message the originating connection. | |
555 connection_manager_->OnConnectionMessagedClient(id_); | |
556 existing_owner->RemoveRoot(); | |
557 } | |
558 connection_manager_->EmbedAtView(id_, url, transport_view_id, | |
559 service_provider.Pass()); | |
560 callback.Run(true); | |
561 } | 581 } |
562 | 582 |
563 bool ViewManagerServiceImpl::IsRootForAccessPolicy(const ViewId& id) const { | 583 bool ViewManagerServiceImpl::IsRootForAccessPolicy(const ViewId& id) const { |
564 return IsRoot(id); | 584 return IsRoot(id); |
565 } | 585 } |
566 | 586 |
567 bool ViewManagerServiceImpl::IsViewKnownForAccessPolicy( | 587 bool ViewManagerServiceImpl::IsViewKnownForAccessPolicy( |
568 const ServerView* view) const { | 588 const ServerView* view) const { |
569 return IsViewKnown(view); | 589 return IsViewKnown(view); |
570 } | 590 } |
571 | 591 |
572 bool ViewManagerServiceImpl::IsViewRootOfAnotherConnectionForAccessPolicy( | 592 bool ViewManagerServiceImpl::IsViewRootOfAnotherConnectionForAccessPolicy( |
573 const ServerView* view) const { | 593 const ServerView* view) const { |
574 ViewManagerServiceImpl* connection = | 594 ViewManagerServiceImpl* connection = |
575 connection_manager_->GetConnectionWithRoot(view->id()); | 595 connection_manager_->GetConnectionWithRoot(view->id()); |
576 return connection && connection != this; | 596 return connection && connection != this; |
577 } | 597 } |
578 | 598 |
579 } // namespace service | 599 } // namespace service |
580 } // namespace mojo | 600 } // namespace mojo |
OLD | NEW |