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/mus/default_access_policy.h"
7 #include "components/mus/access_policy_delegate.h"
8 #include "components/mus/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
) {}
17 DefaultAccessPolicy::~DefaultAccessPolicy() {}
19 bool DefaultAccessPolicy::CanRemoveViewFromParent(
20 const ServerView
* view
) const {
21 if (!WasCreatedByThisConnection(view
))
22 return false; // Can only unparent views we created.
24 return delegate_
->IsRootForAccessPolicy(view
->parent()->id()) ||
25 WasCreatedByThisConnection(view
->parent());
28 bool DefaultAccessPolicy::CanAddView(const ServerView
* parent
,
29 const ServerView
* child
) const {
30 return WasCreatedByThisConnection(child
) &&
31 (delegate_
->IsRootForAccessPolicy(parent
->id()) ||
32 (WasCreatedByThisConnection(parent
) &&
33 !delegate_
->IsViewRootOfAnotherConnectionForAccessPolicy(parent
)));
36 bool DefaultAccessPolicy::CanReorderView(const ServerView
* view
,
37 const ServerView
* relative_view
,
38 mojo::OrderDirection direction
) const {
39 return WasCreatedByThisConnection(view
) &&
40 WasCreatedByThisConnection(relative_view
);
43 bool DefaultAccessPolicy::CanDeleteView(const ServerView
* view
) const {
44 return WasCreatedByThisConnection(view
);
47 bool DefaultAccessPolicy::CanGetViewTree(const ServerView
* view
) const {
48 return WasCreatedByThisConnection(view
) ||
49 delegate_
->IsRootForAccessPolicy(view
->id()) ||
50 IsDescendantOfEmbedRoot(view
);
53 bool DefaultAccessPolicy::CanDescendIntoViewForViewTree(
54 const ServerView
* view
) const {
55 return (WasCreatedByThisConnection(view
) &&
56 !delegate_
->IsViewRootOfAnotherConnectionForAccessPolicy(view
)) ||
57 delegate_
->IsRootForAccessPolicy(view
->id()) ||
58 delegate_
->IsDescendantOfEmbedRoot(view
);
61 bool DefaultAccessPolicy::CanEmbed(const ServerView
* view
,
62 uint32_t policy_bitmask
) const {
63 if (policy_bitmask
!= mojo::ViewTree::ACCESS_POLICY_DEFAULT
)
65 return WasCreatedByThisConnection(view
) ||
66 (delegate_
->IsViewKnownForAccessPolicy(view
) &&
67 IsDescendantOfEmbedRoot(view
) &&
68 !delegate_
->IsRootForAccessPolicy(view
->id()));
71 bool DefaultAccessPolicy::CanChangeViewVisibility(
72 const ServerView
* view
) const {
73 return WasCreatedByThisConnection(view
) ||
74 delegate_
->IsRootForAccessPolicy(view
->id());
77 bool DefaultAccessPolicy::CanSetViewSurfaceId(const ServerView
* view
) const {
78 // Once a view embeds another app, the embedder app is no longer able to
79 // call SetViewSurfaceId() - this ability is transferred to the embedded app.
80 if (delegate_
->IsViewRootOfAnotherConnectionForAccessPolicy(view
))
82 return WasCreatedByThisConnection(view
) ||
83 delegate_
->IsRootForAccessPolicy(view
->id());
86 bool DefaultAccessPolicy::CanSetViewBounds(const ServerView
* view
) const {
87 return WasCreatedByThisConnection(view
);
90 bool DefaultAccessPolicy::CanSetViewProperties(const ServerView
* view
) const {
91 return WasCreatedByThisConnection(view
);
94 bool DefaultAccessPolicy::CanSetViewTextInputState(
95 const ServerView
* view
) const {
96 return WasCreatedByThisConnection(view
) ||
97 delegate_
->IsRootForAccessPolicy(view
->id());
100 bool DefaultAccessPolicy::CanSetFocus(const ServerView
* view
) const {
101 return WasCreatedByThisConnection(view
) ||
102 delegate_
->IsRootForAccessPolicy(view
->id());
105 bool DefaultAccessPolicy::ShouldNotifyOnHierarchyChange(
106 const ServerView
* view
,
107 const ServerView
** new_parent
,
108 const ServerView
** old_parent
) const {
109 if (!WasCreatedByThisConnection(view
) && !IsDescendantOfEmbedRoot(view
) &&
110 (!*new_parent
|| !IsDescendantOfEmbedRoot(*new_parent
)) &&
111 (!*old_parent
|| !IsDescendantOfEmbedRoot(*old_parent
))) {
115 if (*new_parent
&& !WasCreatedByThisConnection(*new_parent
) &&
116 !delegate_
->IsRootForAccessPolicy((*new_parent
)->id()) &&
117 !delegate_
->IsDescendantOfEmbedRoot(*new_parent
)) {
118 *new_parent
= nullptr;
121 if (*old_parent
&& !WasCreatedByThisConnection(*old_parent
) &&
122 !delegate_
->IsRootForAccessPolicy((*old_parent
)->id()) &&
123 !delegate_
->IsDescendantOfEmbedRoot(*new_parent
)) {
124 *old_parent
= nullptr;
129 const ServerView
* DefaultAccessPolicy::GetViewForFocusChange(
130 const ServerView
* focused
) {
131 if (WasCreatedByThisConnection(focused
) ||
132 delegate_
->IsRootForAccessPolicy(focused
->id()))
137 bool DefaultAccessPolicy::WasCreatedByThisConnection(
138 const ServerView
* view
) const {
139 return view
->id().connection_id
== connection_id_
;
142 bool DefaultAccessPolicy::IsDescendantOfEmbedRoot(
143 const ServerView
* view
) const {
144 return delegate_
->IsDescendantOfEmbedRoot(view
);
147 } // namespace view_manager