Roll src/third_party/WebKit 6b63e20:35e1984 (svn 201060:201061)
[chromium-blink-merge.git] / components / view_manager / view_manager_service_impl.cc
blob83af914a6e2078d8cdddb0fd2ebf3c9b449409a9
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
3 // found in the LICENSE file.
5 #include "components/view_manager/view_manager_service_impl.h"
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "components/view_manager/connection_manager.h"
10 #include "components/view_manager/default_access_policy.h"
11 #include "components/view_manager/display_manager.h"
12 #include "components/view_manager/server_view.h"
13 #include "components/view_manager/window_manager_access_policy.h"
14 #include "mojo/converters/geometry/geometry_type_converters.h"
15 #include "mojo/converters/ime/ime_type_converters.h"
16 #include "mojo/converters/input_events/input_events_type_converters.h"
17 #include "mojo/converters/surfaces/surfaces_type_converters.h"
18 #include "ui/platform_window/text_input_state.h"
20 using mojo::Array;
21 using mojo::Callback;
22 using mojo::Id;
23 using mojo::InterfaceRequest;
24 using mojo::OrderDirection;
25 using mojo::Rect;
26 using mojo::ServiceProvider;
27 using mojo::ServiceProviderPtr;
28 using mojo::String;
29 using mojo::ViewDataPtr;
31 namespace view_manager {
33 // Contains information needed to complete an Embed(). See description of
34 // |pending_embeds_| for more details.
35 struct ViewManagerServiceImpl::PendingEmbed
36 : public base::RefCounted<PendingEmbed> {
37 PendingEmbed() : embed_root(nullptr) {}
39 ViewManagerServiceImpl* embed_root;
40 ViewId view_id;
41 mojo::Callback<void(bool)> callback;
43 private:
44 friend class base::RefCounted<PendingEmbed>;
46 ~PendingEmbed() {}
49 ViewManagerServiceImpl::ViewManagerServiceImpl(
50 ConnectionManager* connection_manager,
51 mojo::ConnectionSpecificId creator_id,
52 const ViewId& root_id)
53 : connection_manager_(connection_manager),
54 id_(connection_manager_->GetAndAdvanceNextConnectionId()),
55 creator_id_(creator_id),
56 client_(nullptr),
57 is_embed_root_(false) {
58 ServerView* view = GetView(root_id);
59 CHECK(view);
60 root_.reset(new ViewId(root_id));
61 if (view->GetRoot() == view)
62 access_policy_.reset(new WindowManagerAccessPolicy(id_, this));
63 else
64 access_policy_.reset(new DefaultAccessPolicy(id_, this));
67 ViewManagerServiceImpl::~ViewManagerServiceImpl() {
68 DestroyViews();
71 void ViewManagerServiceImpl::Init(mojo::ViewManagerClient* client,
72 mojo::ViewManagerServicePtr service_ptr) {
73 DCHECK(!client_);
74 client_ = client;
75 std::vector<const ServerView*> to_send;
76 if (root_.get())
77 GetUnknownViewsFrom(GetView(*root_), &to_send);
79 const ServerView* focused_view = connection_manager_->GetFocusedView();
80 if (focused_view)
81 focused_view = access_policy_->GetViewForFocusChange(focused_view);
82 const mojo::Id focused_view_transport_id(
83 ViewIdToTransportId(focused_view ? focused_view->id() : ViewId()));
85 client->OnEmbed(id_, ViewToViewData(to_send.front()), service_ptr.Pass(),
86 focused_view_transport_id);
89 const ServerView* ViewManagerServiceImpl::GetView(const ViewId& id) const {
90 if (id_ == id.connection_id) {
91 ViewMap::const_iterator i = view_map_.find(id.view_id);
92 return i == view_map_.end() ? NULL : i->second;
94 return connection_manager_->GetView(id);
97 bool ViewManagerServiceImpl::IsRoot(const ViewId& id) const {
98 return root_.get() && *root_ == id;
101 void ViewManagerServiceImpl::OnWillDestroyViewManagerServiceImpl(
102 ViewManagerServiceImpl* connection) {
103 InvalidatePendingEmbedForConnection(connection);
104 if (creator_id_ == connection->id())
105 creator_id_ = kInvalidConnectionId;
106 if (connection->root_ && connection->root_->connection_id == id_ &&
107 view_map_.count(connection->root_->view_id) > 0) {
108 client()->OnEmbeddedAppDisconnected(
109 ViewIdToTransportId(*connection->root_));
111 if (root_.get() && root_->connection_id == connection->id())
112 root_.reset();
115 mojo::ErrorCode ViewManagerServiceImpl::CreateView(const ViewId& view_id) {
116 if (view_id.connection_id != id_)
117 return mojo::ERROR_CODE_ILLEGAL_ARGUMENT;
118 if (view_map_.find(view_id.view_id) != view_map_.end())
119 return mojo::ERROR_CODE_VALUE_IN_USE;
120 view_map_[view_id.view_id] = connection_manager_->CreateServerView(view_id);
121 known_views_.insert(ViewIdToTransportId(view_id));
122 return mojo::ERROR_CODE_NONE;
125 bool ViewManagerServiceImpl::AddView(const ViewId& parent_id,
126 const ViewId& child_id) {
127 ServerView* parent = GetView(parent_id);
128 ServerView* child = GetView(child_id);
129 if (parent && child && child->parent() != parent &&
130 !child->Contains(parent) && access_policy_->CanAddView(parent, child)) {
131 ConnectionManager::ScopedChange change(this, connection_manager_, false);
132 parent->Add(child);
133 return true;
135 return false;
138 std::vector<const ServerView*> ViewManagerServiceImpl::GetViewTree(
139 const ViewId& view_id) const {
140 const ServerView* view = GetView(view_id);
141 std::vector<const ServerView*> views;
142 if (view)
143 GetViewTreeImpl(view, &views);
144 return views;
147 bool ViewManagerServiceImpl::SetViewVisibility(const ViewId& view_id,
148 bool visible) {
149 ServerView* view = GetView(view_id);
150 if (!view || view->visible() == visible ||
151 !access_policy_->CanChangeViewVisibility(view)) {
152 return false;
154 ConnectionManager::ScopedChange change(this, connection_manager_, false);
155 view->SetVisible(visible);
156 return true;
159 void ViewManagerServiceImpl::EmbedAllowingReembed(
160 const ViewId& view_id,
161 mojo::URLRequestPtr request,
162 const mojo::Callback<void(bool)>& callback) {
163 if (!CanEmbed(view_id)) {
164 callback.Run(false);
165 return;
168 ViewManagerServiceImpl* embed_root = nullptr;
170 ServerView* view = GetView(view_id);
171 DCHECK(view); // CanEmbed() returns true only if |view_id| is valid.
172 if (view->id().connection_id == id_) {
173 view->set_allows_reembed(true);
175 // Only consult the embed root if the creator is doing the embed. If someone
176 // other than the creator is doing the embed they were granted embed access.
177 embed_root = connection_manager_->GetEmbedRoot(this);
180 if (!embed_root) {
181 PrepareForEmbed(view_id);
182 connection_manager_->EmbedAtView(id_, view_id, request.Pass());
183 callback.Run(true);
184 return;
187 // There is an embed root. We have to query it before completing the embed.
188 scoped_refptr<PendingEmbed> pending_embed(new PendingEmbed);
189 pending_embeds_.insert(pending_embed);
190 pending_embed->embed_root = embed_root;
191 pending_embed->view_id = view_id;
192 pending_embed->callback = callback;
193 pending_embeds_.insert(pending_embed);
194 embed_root->client()->OnEmbedForDescendant(
195 ViewIdToTransportId(view_id), request.Pass(),
196 base::Bind(&ViewManagerServiceImpl::OnEmbedForDescendantDone,
197 base::Unretained(this), pending_embed));
200 bool ViewManagerServiceImpl::Embed(const ViewId& view_id,
201 mojo::ViewManagerClientPtr client) {
202 if (!client.get() || !CanEmbed(view_id))
203 return false;
204 PrepareForEmbed(view_id);
205 ::ignore_result(
206 connection_manager_->EmbedAtView(id_, view_id, client.Pass()));
207 return true;
210 void ViewManagerServiceImpl::ProcessViewBoundsChanged(
211 const ServerView* view,
212 const gfx::Rect& old_bounds,
213 const gfx::Rect& new_bounds,
214 bool originated_change) {
215 if (originated_change || !IsViewKnown(view))
216 return;
217 client()->OnViewBoundsChanged(ViewIdToTransportId(view->id()),
218 Rect::From(old_bounds),
219 Rect::From(new_bounds));
222 void ViewManagerServiceImpl::ProcessViewportMetricsChanged(
223 const mojo::ViewportMetrics& old_metrics,
224 const mojo::ViewportMetrics& new_metrics,
225 bool originated_change) {
226 client()->OnViewViewportMetricsChanged(old_metrics.Clone(),
227 new_metrics.Clone());
230 void ViewManagerServiceImpl::ProcessWillChangeViewHierarchy(
231 const ServerView* view,
232 const ServerView* new_parent,
233 const ServerView* old_parent,
234 bool originated_change) {
235 if (originated_change)
236 return;
238 const bool old_drawn = view->IsDrawn();
239 const bool new_drawn = view->visible() && new_parent && new_parent->IsDrawn();
240 if (old_drawn == new_drawn)
241 return;
243 NotifyDrawnStateChanged(view, new_drawn);
246 void ViewManagerServiceImpl::ProcessViewPropertyChanged(
247 const ServerView* view,
248 const std::string& name,
249 const std::vector<uint8_t>* new_data,
250 bool originated_change) {
251 if (originated_change)
252 return;
254 Array<uint8_t> data;
255 if (new_data)
256 data = Array<uint8_t>::From(*new_data);
258 client()->OnViewSharedPropertyChanged(ViewIdToTransportId(view->id()),
259 String(name), data.Pass());
262 void ViewManagerServiceImpl::ProcessViewHierarchyChanged(
263 const ServerView* view,
264 const ServerView* new_parent,
265 const ServerView* old_parent,
266 bool originated_change) {
267 if (originated_change && !IsViewKnown(view) && new_parent &&
268 IsViewKnown(new_parent)) {
269 std::vector<const ServerView*> unused;
270 GetUnknownViewsFrom(view, &unused);
272 if (originated_change || connection_manager_->is_processing_delete_view() ||
273 connection_manager_->DidConnectionMessageClient(id_)) {
274 return;
277 if (!access_policy_->ShouldNotifyOnHierarchyChange(
278 view, &new_parent, &old_parent)) {
279 return;
281 // Inform the client of any new views and update the set of views we know
282 // about.
283 std::vector<const ServerView*> to_send;
284 if (!IsViewKnown(view))
285 GetUnknownViewsFrom(view, &to_send);
286 const ViewId new_parent_id(new_parent ? new_parent->id() : ViewId());
287 const ViewId old_parent_id(old_parent ? old_parent->id() : ViewId());
288 client()->OnViewHierarchyChanged(ViewIdToTransportId(view->id()),
289 ViewIdToTransportId(new_parent_id),
290 ViewIdToTransportId(old_parent_id),
291 ViewsToViewDatas(to_send));
292 connection_manager_->OnConnectionMessagedClient(id_);
295 void ViewManagerServiceImpl::ProcessViewReorder(const ServerView* view,
296 const ServerView* relative_view,
297 OrderDirection direction,
298 bool originated_change) {
299 if (originated_change || !IsViewKnown(view) || !IsViewKnown(relative_view))
300 return;
302 client()->OnViewReordered(ViewIdToTransportId(view->id()),
303 ViewIdToTransportId(relative_view->id()),
304 direction);
307 void ViewManagerServiceImpl::ProcessViewDeleted(const ViewId& view,
308 bool originated_change) {
309 if (view.connection_id == id_)
310 view_map_.erase(view.view_id);
312 const bool in_known = known_views_.erase(ViewIdToTransportId(view)) > 0;
314 if (IsRoot(view))
315 root_.reset();
317 InvalidatePendingEmbedForView(view);
319 if (originated_change)
320 return;
322 if (in_known) {
323 client()->OnViewDeleted(ViewIdToTransportId(view));
324 connection_manager_->OnConnectionMessagedClient(id_);
328 void ViewManagerServiceImpl::ProcessWillChangeViewVisibility(
329 const ServerView* view,
330 bool originated_change) {
331 if (originated_change)
332 return;
334 if (IsViewKnown(view)) {
335 client()->OnViewVisibilityChanged(ViewIdToTransportId(view->id()),
336 !view->visible());
337 return;
340 bool view_target_drawn_state;
341 if (view->visible()) {
342 // View is being hidden, won't be drawn.
343 view_target_drawn_state = false;
344 } else {
345 // View is being shown. View will be drawn if its parent is drawn.
346 view_target_drawn_state = view->parent() && view->parent()->IsDrawn();
349 NotifyDrawnStateChanged(view, view_target_drawn_state);
352 void ViewManagerServiceImpl::ProcessFocusChanged(
353 const ServerView* old_focused_view,
354 const ServerView* new_focused_view) {
355 const ServerView* view =
356 new_focused_view ? access_policy_->GetViewForFocusChange(new_focused_view)
357 : nullptr;
358 client()->OnViewFocused(view ? ViewIdToTransportId(view->id())
359 : ViewIdToTransportId(ViewId()));
362 bool ViewManagerServiceImpl::IsViewKnown(const ServerView* view) const {
363 return known_views_.count(ViewIdToTransportId(view->id())) > 0;
366 bool ViewManagerServiceImpl::CanReorderView(const ServerView* view,
367 const ServerView* relative_view,
368 OrderDirection direction) const {
369 if (!view || !relative_view)
370 return false;
372 if (!view->parent() || view->parent() != relative_view->parent())
373 return false;
375 if (!access_policy_->CanReorderView(view, relative_view, direction))
376 return false;
378 std::vector<const ServerView*> children = view->parent()->GetChildren();
379 const size_t child_i =
380 std::find(children.begin(), children.end(), view) - children.begin();
381 const size_t target_i =
382 std::find(children.begin(), children.end(), relative_view) -
383 children.begin();
384 if ((direction == mojo::ORDER_DIRECTION_ABOVE && child_i == target_i + 1) ||
385 (direction == mojo::ORDER_DIRECTION_BELOW && child_i + 1 == target_i)) {
386 return false;
389 return true;
392 bool ViewManagerServiceImpl::DeleteViewImpl(ViewManagerServiceImpl* source,
393 ServerView* view) {
394 DCHECK(view);
395 DCHECK_EQ(view->id().connection_id, id_);
396 ConnectionManager::ScopedChange change(source, connection_manager_, true);
397 delete view;
398 return true;
401 void ViewManagerServiceImpl::GetUnknownViewsFrom(
402 const ServerView* view,
403 std::vector<const ServerView*>* views) {
404 if (IsViewKnown(view) || !access_policy_->CanGetViewTree(view))
405 return;
406 views->push_back(view);
407 known_views_.insert(ViewIdToTransportId(view->id()));
408 if (!access_policy_->CanDescendIntoViewForViewTree(view))
409 return;
410 std::vector<const ServerView*> children(view->GetChildren());
411 for (size_t i = 0 ; i < children.size(); ++i)
412 GetUnknownViewsFrom(children[i], views);
415 void ViewManagerServiceImpl::RemoveFromKnown(
416 const ServerView* view,
417 std::vector<ServerView*>* local_views) {
418 if (view->id().connection_id == id_) {
419 if (local_views)
420 local_views->push_back(GetView(view->id()));
421 return;
423 known_views_.erase(ViewIdToTransportId(view->id()));
424 std::vector<const ServerView*> children = view->GetChildren();
425 for (size_t i = 0; i < children.size(); ++i)
426 RemoveFromKnown(children[i], local_views);
429 void ViewManagerServiceImpl::RemoveRoot() {
430 CHECK(root_.get());
431 const ViewId root_id(*root_);
432 root_.reset();
433 // No need to do anything if we created the view.
434 if (root_id.connection_id == id_)
435 return;
437 client()->OnUnembed();
438 client()->OnViewDeleted(ViewIdToTransportId(root_id));
439 connection_manager_->OnConnectionMessagedClient(id_);
441 // This connection no longer knows about the view. Unparent any views that
442 // were parented to views in the root.
443 std::vector<ServerView*> local_views;
444 RemoveFromKnown(GetView(root_id), &local_views);
445 for (size_t i = 0; i < local_views.size(); ++i)
446 local_views[i]->parent()->Remove(local_views[i]);
449 Array<ViewDataPtr> ViewManagerServiceImpl::ViewsToViewDatas(
450 const std::vector<const ServerView*>& views) {
451 Array<ViewDataPtr> array(views.size());
452 for (size_t i = 0; i < views.size(); ++i)
453 array[i] = ViewToViewData(views[i]).Pass();
454 return array.Pass();
457 ViewDataPtr ViewManagerServiceImpl::ViewToViewData(const ServerView* view) {
458 DCHECK(IsViewKnown(view));
459 const ServerView* parent = view->parent();
460 // If the parent isn't known, it means the parent is not visible to us (not
461 // in roots), and should not be sent over.
462 if (parent && !IsViewKnown(parent))
463 parent = NULL;
464 ViewDataPtr view_data(mojo::ViewData::New());
465 view_data->parent_id = ViewIdToTransportId(parent ? parent->id() : ViewId());
466 view_data->view_id = ViewIdToTransportId(view->id());
467 view_data->bounds = Rect::From(view->bounds());
468 view_data->properties =
469 mojo::Map<String, Array<uint8_t>>::From(view->properties());
470 view_data->visible = view->visible();
471 view_data->drawn = view->IsDrawn();
472 view_data->viewport_metrics =
473 connection_manager_->GetViewportMetricsForView(view);
474 return view_data.Pass();
477 void ViewManagerServiceImpl::GetViewTreeImpl(
478 const ServerView* view,
479 std::vector<const ServerView*>* views) const {
480 DCHECK(view);
482 if (!access_policy_->CanGetViewTree(view))
483 return;
485 views->push_back(view);
487 if (!access_policy_->CanDescendIntoViewForViewTree(view))
488 return;
490 std::vector<const ServerView*> children(view->GetChildren());
491 for (size_t i = 0 ; i < children.size(); ++i)
492 GetViewTreeImpl(children[i], views);
495 void ViewManagerServiceImpl::NotifyDrawnStateChanged(const ServerView* view,
496 bool new_drawn_value) {
497 // Even though we don't know about view, it may be an ancestor of our root, in
498 // which case the change may effect our roots drawn state.
499 if (!root_.get())
500 return;
502 const ServerView* root = GetView(*root_);
503 DCHECK(root);
504 if (view->Contains(root) && (new_drawn_value != root->IsDrawn())) {
505 client()->OnViewDrawnStateChanged(ViewIdToTransportId(root->id()),
506 new_drawn_value);
510 void ViewManagerServiceImpl::DestroyViews() {
511 if (!view_map_.empty()) {
512 ConnectionManager::ScopedChange change(this, connection_manager_, true);
513 // If we get here from the destructor we're not going to get
514 // ProcessViewDeleted(). Copy the map and delete from the copy so that we
515 // don't have to worry about whether |view_map_| changes or not.
516 ViewMap view_map_copy;
517 view_map_.swap(view_map_copy);
518 STLDeleteValues(&view_map_copy);
522 bool ViewManagerServiceImpl::CanEmbed(const ViewId& view_id) const {
523 const ServerView* view = GetView(view_id);
524 return view && access_policy_->CanEmbed(view);
527 void ViewManagerServiceImpl::PrepareForEmbed(const ViewId& view_id) {
528 const ServerView* view = GetView(view_id);
529 DCHECK(view && access_policy_->CanEmbed(view));
531 // Only allow a node to be the root for one connection.
532 ViewManagerServiceImpl* existing_owner =
533 connection_manager_->GetConnectionWithRoot(view_id);
535 ConnectionManager::ScopedChange change(this, connection_manager_, true);
536 RemoveChildrenAsPartOfEmbed(view_id);
537 if (existing_owner) {
538 // Never message the originating connection.
539 connection_manager_->OnConnectionMessagedClient(id_);
540 existing_owner->RemoveRoot();
544 void ViewManagerServiceImpl::RemoveChildrenAsPartOfEmbed(
545 const ViewId& view_id) {
546 ServerView* view = GetView(view_id);
547 CHECK(view);
548 CHECK(view->id().connection_id == view_id.connection_id);
549 std::vector<ServerView*> children = view->GetChildren();
550 for (size_t i = 0; i < children.size(); ++i)
551 view->Remove(children[i]);
554 void ViewManagerServiceImpl::OnEmbedForDescendantDone(
555 scoped_refptr<PendingEmbed> pending_embed,
556 mojo::ViewManagerClientPtr client) {
557 if (!pending_embeds_.count(pending_embed.get()))
558 return;
560 const bool allow_embed = client.get() && CanEmbed(pending_embed->view_id);
561 if (allow_embed) {
562 PrepareForEmbed(pending_embed->view_id);
563 connection_manager_->EmbedAtView(id_, pending_embed->view_id,
564 client.Pass());
566 RemovePendingEmbedAndNotifyCallback(pending_embed.get(), allow_embed);
569 void ViewManagerServiceImpl::InvalidatePendingEmbedForConnection(
570 ViewManagerServiceImpl* connection) {
571 if (pending_embeds_.empty())
572 return;
573 std::set<scoped_refptr<PendingEmbed>> copy(pending_embeds_);
574 for (auto& embed : copy) {
575 if (embed->embed_root == connection)
576 RemovePendingEmbedAndNotifyCallback(embed, false);
580 void ViewManagerServiceImpl::InvalidatePendingEmbedForView(
581 const ViewId& view_id) {
582 if (pending_embeds_.empty())
583 return;
584 std::set<scoped_refptr<PendingEmbed>> copy(pending_embeds_);
585 for (auto& embed : copy) {
586 if (embed->view_id == view_id)
587 RemovePendingEmbedAndNotifyCallback(embed, false);
591 void ViewManagerServiceImpl::RemovePendingEmbedAndNotifyCallback(
592 scoped_refptr<PendingEmbed> embed,
593 bool success) {
594 pending_embeds_.erase(embed);
595 embed->callback.Run(success);
598 void ViewManagerServiceImpl::CreateView(
599 Id transport_view_id,
600 const Callback<void(mojo::ErrorCode)>& callback) {
601 callback.Run(CreateView(ViewIdFromTransportId(transport_view_id)));
604 void ViewManagerServiceImpl::DeleteView(
605 Id transport_view_id,
606 const Callback<void(bool)>& callback) {
607 ServerView* view = GetView(ViewIdFromTransportId(transport_view_id));
608 bool success = false;
609 if (view && access_policy_->CanDeleteView(view)) {
610 ViewManagerServiceImpl* connection =
611 connection_manager_->GetConnection(view->id().connection_id);
612 success = connection && connection->DeleteViewImpl(this, view);
614 callback.Run(success);
617 void ViewManagerServiceImpl::AddView(
618 Id parent_id,
619 Id child_id,
620 const Callback<void(bool)>& callback) {
621 callback.Run(AddView(ViewIdFromTransportId(parent_id),
622 ViewIdFromTransportId(child_id)));
625 void ViewManagerServiceImpl::RemoveViewFromParent(
626 Id view_id,
627 const Callback<void(bool)>& callback) {
628 bool success = false;
629 ServerView* view = GetView(ViewIdFromTransportId(view_id));
630 if (view && view->parent() && access_policy_->CanRemoveViewFromParent(view)) {
631 success = true;
632 ConnectionManager::ScopedChange change(this, connection_manager_, false);
633 view->parent()->Remove(view);
635 callback.Run(success);
638 void ViewManagerServiceImpl::ReorderView(Id view_id,
639 Id relative_view_id,
640 OrderDirection direction,
641 const Callback<void(bool)>& callback) {
642 bool success = false;
643 ServerView* view = GetView(ViewIdFromTransportId(view_id));
644 ServerView* relative_view = GetView(ViewIdFromTransportId(relative_view_id));
645 if (CanReorderView(view, relative_view, direction)) {
646 success = true;
647 ConnectionManager::ScopedChange change(this, connection_manager_, false);
648 view->parent()->Reorder(view, relative_view, direction);
649 connection_manager_->ProcessViewReorder(view, relative_view, direction);
651 callback.Run(success);
654 void ViewManagerServiceImpl::GetViewTree(
655 Id view_id,
656 const Callback<void(Array<ViewDataPtr>)>& callback) {
657 std::vector<const ServerView*> views(
658 GetViewTree(ViewIdFromTransportId(view_id)));
659 callback.Run(ViewsToViewDatas(views));
662 void ViewManagerServiceImpl::SetViewSurfaceId(
663 Id view_id,
664 mojo::SurfaceIdPtr surface_id,
665 const Callback<void(bool)>& callback) {
666 // TODO(sky): add coverage of not being able to set for random node.
667 ServerView* view = GetView(ViewIdFromTransportId(view_id));
668 if (!view || !access_policy_->CanSetViewSurfaceId(view)) {
669 callback.Run(false);
670 return;
672 view->SetSurfaceId(surface_id.To<cc::SurfaceId>());
673 callback.Run(true);
676 void ViewManagerServiceImpl::SetViewBounds(
677 Id view_id,
678 mojo::RectPtr bounds,
679 const Callback<void(bool)>& callback) {
680 ServerView* view = GetView(ViewIdFromTransportId(view_id));
681 const bool success = view && access_policy_->CanSetViewBounds(view);
682 if (success) {
683 ConnectionManager::ScopedChange change(this, connection_manager_, false);
684 view->SetBounds(bounds.To<gfx::Rect>());
686 callback.Run(success);
689 void ViewManagerServiceImpl::SetViewVisibility(
690 Id transport_view_id,
691 bool visible,
692 const Callback<void(bool)>& callback) {
693 callback.Run(
694 SetViewVisibility(ViewIdFromTransportId(transport_view_id), visible));
697 void ViewManagerServiceImpl::SetViewProperty(
698 uint32_t view_id,
699 const mojo::String& name,
700 mojo::Array<uint8_t> value,
701 const mojo::Callback<void(bool)>& callback) {
702 ServerView* view = GetView(ViewIdFromTransportId(view_id));
703 const bool success = view && access_policy_->CanSetViewProperties(view);
704 if (success) {
705 ConnectionManager::ScopedChange change(this, connection_manager_, false);
707 if (value.is_null()) {
708 view->SetProperty(name, nullptr);
709 } else {
710 std::vector<uint8_t> data = value.To<std::vector<uint8_t>>();
711 view->SetProperty(name, &data);
714 callback.Run(success);
717 void ViewManagerServiceImpl::SetViewTextInputState(
718 uint32_t view_id,
719 mojo::TextInputStatePtr state) {
720 ServerView* view = GetView(ViewIdFromTransportId(view_id));
721 bool success = view && access_policy_->CanSetViewTextInputState(view);
722 if (success)
723 view->SetTextInputState(state.To<ui::TextInputState>());
726 void ViewManagerServiceImpl::SetImeVisibility(uint32_t view_id,
727 bool visible,
728 mojo::TextInputStatePtr state) {
729 ServerView* view = GetView(ViewIdFromTransportId(view_id));
730 bool success = view && access_policy_->CanSetViewTextInputState(view);
731 if (success) {
732 if (!state.is_null())
733 view->SetTextInputState(state.To<ui::TextInputState>());
734 connection_manager_->SetImeVisibility(view, visible);
738 void ViewManagerServiceImpl::SetEmbedRoot() {
739 is_embed_root_ = true;
742 void ViewManagerServiceImpl::Embed(mojo::Id transport_view_id,
743 mojo::ViewManagerClientPtr client,
744 const mojo::Callback<void(bool)>& callback) {
745 callback.Run(Embed(ViewIdFromTransportId(transport_view_id), client.Pass()));
748 void ViewManagerServiceImpl::EmbedAllowingReembed(
749 mojo::Id transport_view_id,
750 mojo::URLRequestPtr request,
751 const mojo::Callback<void(bool)>& callback) {
752 EmbedAllowingReembed(ViewIdFromTransportId(transport_view_id), request.Pass(),
753 callback);
756 void ViewManagerServiceImpl::SetFocus(uint32_t view_id,
757 const SetFocusCallback& callback) {
758 ServerView* view = GetView(ViewIdFromTransportId(view_id));
759 bool success = view && view->IsDrawn() && access_policy_->CanSetFocus(view);
760 if (success) {
761 ConnectionManager::ScopedChange change(this, connection_manager_, false);
762 connection_manager_->SetFocusedView(view);
764 callback.Run(success);
767 bool ViewManagerServiceImpl::IsRootForAccessPolicy(const ViewId& id) const {
768 return IsRoot(id);
771 bool ViewManagerServiceImpl::IsViewKnownForAccessPolicy(
772 const ServerView* view) const {
773 return IsViewKnown(view);
776 bool ViewManagerServiceImpl::IsViewRootOfAnotherConnectionForAccessPolicy(
777 const ServerView* view) const {
778 ViewManagerServiceImpl* connection =
779 connection_manager_->GetConnectionWithRoot(view->id());
780 return connection && connection != this;
783 bool ViewManagerServiceImpl::IsDescendantOfEmbedRoot(const ServerView* view) {
784 return is_embed_root_ && root_ && GetView(*root_)->Contains(view);
787 } // namespace view_manager