| 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 bool ViewManagerServiceImpl::Embed( |
| 110 const std::string& url, |
| 111 const ViewId& view_id, |
| 112 InterfaceRequest<ServiceProvider> service_provider) { |
| 113 const ServerView* view = GetView(view_id); |
| 114 if (!view || !access_policy_->CanEmbed(view)) |
| 115 return false; |
| 116 |
| 117 // Only allow a node to be the root for one connection. |
| 118 ViewManagerServiceImpl* existing_owner = |
| 119 connection_manager_->GetConnectionWithRoot(view_id); |
| 120 |
| 121 ConnectionManager::ScopedChange change(this, connection_manager_, true); |
| 122 RemoveChildrenAsPartOfEmbed(view_id); |
| 123 if (existing_owner) { |
| 124 // Never message the originating connection. |
| 125 connection_manager_->OnConnectionMessagedClient(id_); |
| 126 existing_owner->RemoveRoot(); |
| 127 } |
| 128 connection_manager_->EmbedAtView(id_, url, view_id, service_provider.Pass()); |
| 129 return true; |
| 130 } |
| 131 |
| 132 void ViewManagerServiceImpl::GetViewTree( |
| 133 const ViewId& view_id, |
| 134 std::vector<const ServerView*>* views) { |
| 135 ServerView* view = GetView(view_id); |
| 136 if (view) |
| 137 GetViewTreeImpl(view, views); |
| 138 } |
| 139 |
| 140 bool ViewManagerServiceImpl::SetViewVisibility(const ViewId& view_id, |
| 141 bool visible) { |
| 142 ServerView* view = GetView(view_id); |
| 143 if (!view || view->visible() == visible || |
| 144 !access_policy_->CanChangeViewVisibility(view)) { |
| 145 return false; |
| 146 } |
| 147 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
| 148 view->SetVisible(visible); |
| 149 return true; |
| 150 } |
| 151 |
| 95 void ViewManagerServiceImpl::ProcessViewBoundsChanged( | 152 void ViewManagerServiceImpl::ProcessViewBoundsChanged( |
| 96 const ServerView* view, | 153 const ServerView* view, |
| 97 const gfx::Rect& old_bounds, | 154 const gfx::Rect& old_bounds, |
| 98 const gfx::Rect& new_bounds, | 155 const gfx::Rect& new_bounds, |
| 99 bool originated_change) { | 156 bool originated_change) { |
| 100 if (originated_change || !IsViewKnown(view)) | 157 if (originated_change || !IsViewKnown(view)) |
| 101 return; | 158 return; |
| 102 client()->OnViewBoundsChanged(ViewIdToTransportId(view->id()), | 159 client()->OnViewBoundsChanged(ViewIdToTransportId(view->id()), |
| 103 Rect::From(old_bounds), | 160 Rect::From(old_bounds), |
| 104 Rect::From(new_bounds)); | 161 Rect::From(new_bounds)); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 ViewIdToTransportId(relative_view->id()), | 238 ViewIdToTransportId(relative_view->id()), |
| 182 direction); | 239 direction); |
| 183 } | 240 } |
| 184 | 241 |
| 185 void ViewManagerServiceImpl::ProcessViewDeleted(const ViewId& view, | 242 void ViewManagerServiceImpl::ProcessViewDeleted(const ViewId& view, |
| 186 bool originated_change) { | 243 bool originated_change) { |
| 187 if (view.connection_id == id_) | 244 if (view.connection_id == id_) |
| 188 view_map_.erase(view.view_id); | 245 view_map_.erase(view.view_id); |
| 189 | 246 |
| 190 const bool in_known = known_views_.erase(ViewIdToTransportId(view)) > 0; | 247 const bool in_known = known_views_.erase(ViewIdToTransportId(view)) > 0; |
| 248 |
| 191 if (IsRoot(view)) | 249 if (IsRoot(view)) |
| 192 root_.reset(); | 250 root_.reset(); |
| 193 | 251 |
| 194 if (originated_change) | 252 if (originated_change) |
| 195 return; | 253 return; |
| 196 | 254 |
| 197 if (in_known) { | 255 if (in_known) { |
| 198 client()->OnViewDeleted(ViewIdToTransportId(view)); | 256 client()->OnViewDeleted(ViewIdToTransportId(view)); |
| 199 connection_manager_->OnConnectionMessagedClient(id_); | 257 connection_manager_->OnConnectionMessagedClient(id_); |
| 200 } | 258 } |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 connection_manager_->GetConnection(view->id().connection_id); | 466 connection_manager_->GetConnection(view->id().connection_id); |
| 409 success = connection && connection->DeleteViewImpl(this, view); | 467 success = connection && connection->DeleteViewImpl(this, view); |
| 410 } | 468 } |
| 411 callback.Run(success); | 469 callback.Run(success); |
| 412 } | 470 } |
| 413 | 471 |
| 414 void ViewManagerServiceImpl::AddView( | 472 void ViewManagerServiceImpl::AddView( |
| 415 Id parent_id, | 473 Id parent_id, |
| 416 Id child_id, | 474 Id child_id, |
| 417 const Callback<void(bool)>& callback) { | 475 const Callback<void(bool)>& callback) { |
| 418 bool success = false; | 476 callback.Run(AddView(ViewIdFromTransportId(parent_id), |
| 419 ServerView* parent = GetView(ViewIdFromTransportId(parent_id)); | 477 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 } | 478 } |
| 429 | 479 |
| 430 void ViewManagerServiceImpl::RemoveViewFromParent( | 480 void ViewManagerServiceImpl::RemoveViewFromParent( |
| 431 Id view_id, | 481 Id view_id, |
| 432 const Callback<void(bool)>& callback) { | 482 const Callback<void(bool)>& callback) { |
| 433 bool success = false; | 483 bool success = false; |
| 434 ServerView* view = GetView(ViewIdFromTransportId(view_id)); | 484 ServerView* view = GetView(ViewIdFromTransportId(view_id)); |
| 435 if (view && view->parent() && access_policy_->CanRemoveViewFromParent(view)) { | 485 if (view && view->parent() && access_policy_->CanRemoveViewFromParent(view)) { |
| 436 success = true; | 486 success = true; |
| 437 ConnectionManager::ScopedChange change(this, connection_manager_, false); | 487 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 452 ConnectionManager::ScopedChange change(this, connection_manager_, false); | 502 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
| 453 view->parent()->Reorder(view, relative_view, direction); | 503 view->parent()->Reorder(view, relative_view, direction); |
| 454 connection_manager_->ProcessViewReorder(view, relative_view, direction); | 504 connection_manager_->ProcessViewReorder(view, relative_view, direction); |
| 455 } | 505 } |
| 456 callback.Run(success); | 506 callback.Run(success); |
| 457 } | 507 } |
| 458 | 508 |
| 459 void ViewManagerServiceImpl::GetViewTree( | 509 void ViewManagerServiceImpl::GetViewTree( |
| 460 Id view_id, | 510 Id view_id, |
| 461 const Callback<void(Array<ViewDataPtr>)>& callback) { | 511 const Callback<void(Array<ViewDataPtr>)>& callback) { |
| 462 ServerView* view = GetView(ViewIdFromTransportId(view_id)); | |
| 463 std::vector<const ServerView*> views; | 512 std::vector<const ServerView*> views; |
| 464 if (view) { | 513 GetViewTree(ViewIdFromTransportId(view_id), &views); |
| 465 GetViewTreeImpl(view, &views); | |
| 466 // TODO(sky): this should map in views that weren't none. | |
| 467 } | |
| 468 callback.Run(ViewsToViewDatas(views)); | 514 callback.Run(ViewsToViewDatas(views)); |
| 469 } | 515 } |
| 470 | 516 |
| 471 void ViewManagerServiceImpl::SetViewSurfaceId( | 517 void ViewManagerServiceImpl::SetViewSurfaceId( |
| 472 Id view_id, | 518 Id view_id, |
| 473 SurfaceIdPtr surface_id, | 519 SurfaceIdPtr surface_id, |
| 474 const Callback<void(bool)>& callback) { | 520 const Callback<void(bool)>& callback) { |
| 475 // TODO(sky): add coverage of not being able to set for random node. | 521 // TODO(sky): add coverage of not being able to set for random node. |
| 476 ServerView* view = GetView(ViewIdFromTransportId(view_id)); | 522 ServerView* view = GetView(ViewIdFromTransportId(view_id)); |
| 477 if (!view || !access_policy_->CanSetViewSurfaceId(view)) { | 523 if (!view || !access_policy_->CanSetViewSurfaceId(view)) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 492 ConnectionManager::ScopedChange change(this, connection_manager_, false); | 538 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
| 493 view->SetBounds(bounds.To<gfx::Rect>()); | 539 view->SetBounds(bounds.To<gfx::Rect>()); |
| 494 } | 540 } |
| 495 callback.Run(success); | 541 callback.Run(success); |
| 496 } | 542 } |
| 497 | 543 |
| 498 void ViewManagerServiceImpl::SetViewVisibility( | 544 void ViewManagerServiceImpl::SetViewVisibility( |
| 499 Id transport_view_id, | 545 Id transport_view_id, |
| 500 bool visible, | 546 bool visible, |
| 501 const Callback<void(bool)>& callback) { | 547 const Callback<void(bool)>& callback) { |
| 502 ServerView* view = GetView(ViewIdFromTransportId(transport_view_id)); | 548 callback.Run( |
| 503 if (!view || view->visible() == visible || | 549 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 } | 550 } |
| 514 | 551 |
| 515 void ViewManagerServiceImpl::SetViewProperty( | 552 void ViewManagerServiceImpl::SetViewProperty( |
| 516 uint32_t view_id, | 553 uint32_t view_id, |
| 517 const mojo::String& name, | 554 const mojo::String& name, |
| 518 mojo::Array<uint8_t> value, | 555 mojo::Array<uint8_t> value, |
| 519 const mojo::Callback<void(bool)>& callback) { | 556 const mojo::Callback<void(bool)>& callback) { |
| 520 ServerView* view = GetView(ViewIdFromTransportId(view_id)); | 557 ServerView* view = GetView(ViewIdFromTransportId(view_id)); |
| 521 const bool success = view && access_policy_->CanSetViewProperties(view); | 558 const bool success = view && access_policy_->CanSetViewProperties(view); |
| 522 if (success) { | 559 if (success) { |
| 523 ConnectionManager::ScopedChange change(this, connection_manager_, false); | 560 ConnectionManager::ScopedChange change(this, connection_manager_, false); |
| 524 | 561 |
| 525 if (value.is_null()) { | 562 if (value.is_null()) { |
| 526 view->SetProperty(name, nullptr); | 563 view->SetProperty(name, nullptr); |
| 527 } else { | 564 } else { |
| 528 std::vector<uint8_t> data = value.To<std::vector<uint8_t>>(); | 565 std::vector<uint8_t> data = value.To<std::vector<uint8_t>>(); |
| 529 view->SetProperty(name, &data); | 566 view->SetProperty(name, &data); |
| 530 } | 567 } |
| 531 } | 568 } |
| 532 callback.Run(success); | 569 callback.Run(success); |
| 533 } | 570 } |
| 534 | 571 |
| 535 void ViewManagerServiceImpl::Embed( | 572 void ViewManagerServiceImpl::Embed( |
| 536 const String& url, | 573 const String& url, |
| 537 Id transport_view_id, | 574 Id transport_view_id, |
| 538 InterfaceRequest<ServiceProvider> service_provider, | 575 InterfaceRequest<ServiceProvider> service_provider, |
| 539 const Callback<void(bool)>& callback) { | 576 const Callback<void(bool)>& callback) { |
| 540 const ServerView* view = GetView(ViewIdFromTransportId(transport_view_id)); | 577 callback.Run(Embed(url.To<std::string>(), |
| 541 if (!view || !access_policy_->CanEmbed(view)) { | 578 ViewIdFromTransportId(transport_view_id), |
| 542 callback.Run(false); | 579 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 } | 580 } |
| 562 | 581 |
| 563 bool ViewManagerServiceImpl::IsRootForAccessPolicy(const ViewId& id) const { | 582 bool ViewManagerServiceImpl::IsRootForAccessPolicy(const ViewId& id) const { |
| 564 return IsRoot(id); | 583 return IsRoot(id); |
| 565 } | 584 } |
| 566 | 585 |
| 567 bool ViewManagerServiceImpl::IsViewKnownForAccessPolicy( | 586 bool ViewManagerServiceImpl::IsViewKnownForAccessPolicy( |
| 568 const ServerView* view) const { | 587 const ServerView* view) const { |
| 569 return IsViewKnown(view); | 588 return IsViewKnown(view); |
| 570 } | 589 } |
| 571 | 590 |
| 572 bool ViewManagerServiceImpl::IsViewRootOfAnotherConnectionForAccessPolicy( | 591 bool ViewManagerServiceImpl::IsViewRootOfAnotherConnectionForAccessPolicy( |
| 573 const ServerView* view) const { | 592 const ServerView* view) const { |
| 574 ViewManagerServiceImpl* connection = | 593 ViewManagerServiceImpl* connection = |
| 575 connection_manager_->GetConnectionWithRoot(view->id()); | 594 connection_manager_->GetConnectionWithRoot(view->id()); |
| 576 return connection && connection != this; | 595 return connection && connection != this; |
| 577 } | 596 } |
| 578 | 597 |
| 579 } // namespace service | 598 } // namespace service |
| 580 } // namespace mojo | 599 } // namespace mojo |
| OLD | NEW |