summaryrefslogtreecommitdiffstats
path: root/components/mus/default_access_policy.cc
blob: f5ffd78dbdb46c510e7077e7d6e31b77ea7a9361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/mus/default_access_policy.h"

#include "components/mus/access_policy_delegate.h"
#include "components/mus/server_view.h"

namespace mus {

DefaultAccessPolicy::DefaultAccessPolicy(ConnectionSpecificId connection_id,
                                         AccessPolicyDelegate* delegate)
    : connection_id_(connection_id), delegate_(delegate) {}

DefaultAccessPolicy::~DefaultAccessPolicy() {}

bool DefaultAccessPolicy::CanRemoveViewFromParent(
    const ServerView* view) const {
  if (!WasCreatedByThisConnection(view))
    return false;  // Can only unparent views we created.

  return delegate_->IsRootForAccessPolicy(view->parent()->id()) ||
         WasCreatedByThisConnection(view->parent());
}

bool DefaultAccessPolicy::CanAddView(const ServerView* parent,
                                     const ServerView* child) const {
  return WasCreatedByThisConnection(child) &&
         (delegate_->IsRootForAccessPolicy(parent->id()) ||
          (WasCreatedByThisConnection(parent) &&
           !delegate_->IsViewRootOfAnotherConnectionForAccessPolicy(parent)));
}

bool DefaultAccessPolicy::CanReorderView(const ServerView* view,
                                         const ServerView* relative_view,
                                         mojo::OrderDirection direction) const {
  return WasCreatedByThisConnection(view) &&
         WasCreatedByThisConnection(relative_view);
}

bool DefaultAccessPolicy::CanDeleteView(const ServerView* view) const {
  return WasCreatedByThisConnection(view);
}

bool DefaultAccessPolicy::CanGetViewTree(const ServerView* view) const {
  return WasCreatedByThisConnection(view) ||
         delegate_->IsRootForAccessPolicy(view->id()) ||
         IsDescendantOfEmbedRoot(view);
}

bool DefaultAccessPolicy::CanDescendIntoViewForViewTree(
    const ServerView* view) const {
  return (WasCreatedByThisConnection(view) &&
          !delegate_->IsViewRootOfAnotherConnectionForAccessPolicy(view)) ||
         delegate_->IsRootForAccessPolicy(view->id()) ||
         delegate_->IsDescendantOfEmbedRoot(view);
}

bool DefaultAccessPolicy::CanEmbed(const ServerView* view,
                                   uint32_t policy_bitmask) const {
  if (policy_bitmask != mojo::ViewTree::ACCESS_POLICY_DEFAULT)
    return false;
  return WasCreatedByThisConnection(view) ||
         (delegate_->IsViewKnownForAccessPolicy(view) &&
          IsDescendantOfEmbedRoot(view) &&
          !delegate_->IsRootForAccessPolicy(view->id()));
}

bool DefaultAccessPolicy::CanChangeViewVisibility(
    const ServerView* view) const {
  return WasCreatedByThisConnection(view) ||
         delegate_->IsRootForAccessPolicy(view->id());
}

bool DefaultAccessPolicy::CanSetViewSurfaceId(const ServerView* view) const {
  // Once a view embeds another app, the embedder app is no longer able to
  // call SetViewSurfaceId() - this ability is transferred to the embedded app.
  if (delegate_->IsViewRootOfAnotherConnectionForAccessPolicy(view))
    return false;
  return WasCreatedByThisConnection(view) ||
         delegate_->IsRootForAccessPolicy(view->id());
}

bool DefaultAccessPolicy::CanSetViewBounds(const ServerView* view) const {
  return WasCreatedByThisConnection(view);
}

bool DefaultAccessPolicy::CanSetViewProperties(const ServerView* view) const {
  return WasCreatedByThisConnection(view);
}

bool DefaultAccessPolicy::CanSetViewTextInputState(
    const ServerView* view) const {
  return WasCreatedByThisConnection(view) ||
         delegate_->IsRootForAccessPolicy(view->id());
}

bool DefaultAccessPolicy::CanSetFocus(const ServerView* view) const {
  return WasCreatedByThisConnection(view) ||
         delegate_->IsRootForAccessPolicy(view->id());
}

bool DefaultAccessPolicy::ShouldNotifyOnHierarchyChange(
    const ServerView* view,
    const ServerView** new_parent,
    const ServerView** old_parent) const {
  if (!WasCreatedByThisConnection(view) && !IsDescendantOfEmbedRoot(view) &&
      (!*new_parent || !IsDescendantOfEmbedRoot(*new_parent)) &&
      (!*old_parent || !IsDescendantOfEmbedRoot(*old_parent))) {
    return false;
  }

  if (*new_parent && !WasCreatedByThisConnection(*new_parent) &&
      !delegate_->IsRootForAccessPolicy((*new_parent)->id()) &&
      !delegate_->IsDescendantOfEmbedRoot(*new_parent)) {
    *new_parent = nullptr;
  }

  if (*old_parent && !WasCreatedByThisConnection(*old_parent) &&
      !delegate_->IsRootForAccessPolicy((*old_parent)->id()) &&
      !delegate_->IsDescendantOfEmbedRoot(*new_parent)) {
    *old_parent = nullptr;
  }
  return true;
}

const ServerView* DefaultAccessPolicy::GetViewForFocusChange(
    const ServerView* focused) {
  if (WasCreatedByThisConnection(focused) ||
      delegate_->IsRootForAccessPolicy(focused->id()))
    return focused;
  return nullptr;
}

bool DefaultAccessPolicy::WasCreatedByThisConnection(
    const ServerView* view) const {
  return view->id().connection_id == connection_id_;
}

bool DefaultAccessPolicy::IsDescendantOfEmbedRoot(
    const ServerView* view) const {
  return delegate_->IsDescendantOfEmbedRoot(view);
}

}  // namespace mus