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/default_access_policy.h"
7 #include "components/view_manager/access_policy_delegate.h"
8 #include "components/view_manager/server_view.h"
10 namespace view_manager
{
12 DefaultAccessPolicy::DefaultAccessPolicy(
13 mojo::ConnectionSpecificId connection_id
,
14 AccessPolicyDelegate
* delegate
)
15 : connection_id_(connection_id
), delegate_(delegate
) {
18 DefaultAccessPolicy::~DefaultAccessPolicy() {
21 bool DefaultAccessPolicy::CanRemoveViewFromParent(
22 const ServerView
* view
) const {
23 if (!WasCreatedByThisConnection(view
))
24 return false; // Can only unparent views we created.
26 return delegate_
->IsRootForAccessPolicy(view
->parent()->id()) ||
27 WasCreatedByThisConnection(view
->parent());
30 bool DefaultAccessPolicy::CanAddView(const ServerView
* parent
,
31 const ServerView
* child
) const {
32 return WasCreatedByThisConnection(child
) &&
33 (delegate_
->IsRootForAccessPolicy(parent
->id()) ||
34 (WasCreatedByThisConnection(parent
) &&
35 !delegate_
->IsViewRootOfAnotherConnectionForAccessPolicy(parent
)));
38 bool DefaultAccessPolicy::CanReorderView(const ServerView
* view
,
39 const ServerView
* relative_view
,
40 mojo::OrderDirection direction
) const {
41 return WasCreatedByThisConnection(view
) &&
42 WasCreatedByThisConnection(relative_view
);
45 bool DefaultAccessPolicy::CanDeleteView(const ServerView
* view
) const {
46 return WasCreatedByThisConnection(view
);
49 bool DefaultAccessPolicy::CanGetViewTree(const ServerView
* view
) const {
50 return WasCreatedByThisConnection(view
) ||
51 delegate_
->IsRootForAccessPolicy(view
->id()) ||
52 IsDescendantOfEmbedRoot(view
);
55 bool DefaultAccessPolicy::CanDescendIntoViewForViewTree(
56 const ServerView
* view
) const {
57 return (WasCreatedByThisConnection(view
) &&
58 !delegate_
->IsViewRootOfAnotherConnectionForAccessPolicy(view
)) ||
59 delegate_
->IsRootForAccessPolicy(view
->id()) ||
60 delegate_
->IsDescendantOfEmbedRoot(view
);
63 bool DefaultAccessPolicy::CanEmbed(const ServerView
* view
) const {
64 return WasCreatedByThisConnection(view
) ||
65 (delegate_
->IsViewKnownForAccessPolicy(view
) &&
66 IsDescendantOfEmbedRoot(view
));
69 bool DefaultAccessPolicy::CanChangeViewVisibility(
70 const ServerView
* view
) const {
71 return WasCreatedByThisConnection(view
) ||
72 delegate_
->IsRootForAccessPolicy(view
->id());
75 bool DefaultAccessPolicy::CanSetViewSurfaceId(const ServerView
* view
) const {
76 // Once a view embeds another app, the embedder app is no longer able to
77 // call SetViewSurfaceId() - this ability is transferred to the embedded app.
78 if (delegate_
->IsViewRootOfAnotherConnectionForAccessPolicy(view
))
80 return WasCreatedByThisConnection(view
) ||
81 delegate_
->IsRootForAccessPolicy(view
->id());
84 bool DefaultAccessPolicy::CanSetViewBounds(const ServerView
* view
) const {
85 return WasCreatedByThisConnection(view
);
88 bool DefaultAccessPolicy::CanSetViewProperties(const ServerView
* view
) const {
89 return WasCreatedByThisConnection(view
);
92 bool DefaultAccessPolicy::CanSetViewTextInputState(
93 const ServerView
* view
) const {
94 return WasCreatedByThisConnection(view
) ||
95 delegate_
->IsRootForAccessPolicy(view
->id());
98 bool DefaultAccessPolicy::CanSetFocus(const ServerView
* view
) const {
99 return WasCreatedByThisConnection(view
) ||
100 delegate_
->IsRootForAccessPolicy(view
->id());
103 bool DefaultAccessPolicy::ShouldNotifyOnHierarchyChange(
104 const ServerView
* view
,
105 const ServerView
** new_parent
,
106 const ServerView
** old_parent
) const {
107 if (!WasCreatedByThisConnection(view
) && !IsDescendantOfEmbedRoot(view
) &&
108 (!*new_parent
|| !IsDescendantOfEmbedRoot(*new_parent
)) &&
109 (!*old_parent
|| !IsDescendantOfEmbedRoot(*old_parent
))) {
113 if (*new_parent
&& !WasCreatedByThisConnection(*new_parent
) &&
114 !delegate_
->IsRootForAccessPolicy((*new_parent
)->id()) &&
115 !delegate_
->IsDescendantOfEmbedRoot(*new_parent
)) {
116 *new_parent
= nullptr;
119 if (*old_parent
&& !WasCreatedByThisConnection(*old_parent
) &&
120 !delegate_
->IsRootForAccessPolicy((*old_parent
)->id()) &&
121 !delegate_
->IsDescendantOfEmbedRoot(*new_parent
)) {
122 *old_parent
= nullptr;
127 const ServerView
* DefaultAccessPolicy::GetViewForFocusChange(
128 const ServerView
* focused
) {
129 if (WasCreatedByThisConnection(focused
) ||
130 delegate_
->IsRootForAccessPolicy(focused
->id()))
135 bool DefaultAccessPolicy::WasCreatedByThisConnection(
136 const ServerView
* view
) const {
137 return view
->id().connection_id
== connection_id_
;
140 bool DefaultAccessPolicy::IsDescendantOfEmbedRoot(
141 const ServerView
* view
) const {
142 return delegate_
->IsDescendantOfEmbedRoot(view
);
145 } // namespace view_manager