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.
4
5#include "mojo/services/view_manager/default_access_policy.h"
6
7#include "mojo/services/view_manager/access_policy_delegate.h"
8#include "mojo/services/view_manager/server_view.h"
9
10namespace mojo {
11namespace service {
12
13DefaultAccessPolicy::DefaultAccessPolicy(ConnectionSpecificId connection_id,
14                                         AccessPolicyDelegate* delegate)
15    : connection_id_(connection_id),
16      delegate_(delegate) {
17}
18
19DefaultAccessPolicy::~DefaultAccessPolicy() {
20}
21
22bool DefaultAccessPolicy::CanRemoveViewFromParent(
23    const ServerView* view) const {
24  if (!WasCreatedByThisConnection(view))
25    return false;  // Can only unparent views we created.
26
27  return IsViewInRoots(view->parent()) ||
28         WasCreatedByThisConnection(view->parent());
29}
30
31bool DefaultAccessPolicy::CanAddView(const ServerView* parent,
32                                     const ServerView* child) const {
33  return WasCreatedByThisConnection(child) &&
34         (IsViewInRoots(parent) ||
35          (WasCreatedByThisConnection(parent) &&
36           !delegate_->IsViewRootOfAnotherConnectionForAccessPolicy(parent)));
37}
38
39bool DefaultAccessPolicy::CanReorderView(const ServerView* view,
40                                         const ServerView* relative_view,
41                                         OrderDirection direction) const {
42  return WasCreatedByThisConnection(view) &&
43         WasCreatedByThisConnection(relative_view);
44}
45
46bool DefaultAccessPolicy::CanDeleteView(const ServerView* view) const {
47  return WasCreatedByThisConnection(view);
48}
49
50bool DefaultAccessPolicy::CanGetViewTree(const ServerView* view) const {
51  return WasCreatedByThisConnection(view) || IsViewInRoots(view);
52}
53
54bool DefaultAccessPolicy::CanDescendIntoViewForViewTree(
55    const ServerView* view) const {
56  return WasCreatedByThisConnection(view) &&
57         !delegate_->IsViewRootOfAnotherConnectionForAccessPolicy(view);
58}
59
60bool DefaultAccessPolicy::CanEmbed(const ServerView* view) const {
61  return WasCreatedByThisConnection(view);
62}
63
64bool DefaultAccessPolicy::CanChangeViewVisibility(
65    const ServerView* view) const {
66  return WasCreatedByThisConnection(view) || IsViewInRoots(view);
67}
68
69bool DefaultAccessPolicy::CanSetViewSurfaceId(const ServerView* view) const {
70  // Once a view embeds another app, the embedder app is no longer able to
71  // call SetViewSurfaceId() - this ability is transferred to the embedded app.
72  if (delegate_->IsViewRootOfAnotherConnectionForAccessPolicy(view))
73    return false;
74  return WasCreatedByThisConnection(view) || IsViewInRoots(view);
75}
76
77bool DefaultAccessPolicy::CanSetViewBounds(const ServerView* view) const {
78  return WasCreatedByThisConnection(view);
79}
80
81bool DefaultAccessPolicy::ShouldNotifyOnHierarchyChange(
82    const ServerView* view,
83    const ServerView** new_parent,
84    const ServerView** old_parent) const {
85  if (!WasCreatedByThisConnection(view))
86    return false;
87
88  if (*new_parent && !WasCreatedByThisConnection(*new_parent) &&
89      !IsViewInRoots(*new_parent)) {
90    *new_parent = NULL;
91  }
92
93  if (*old_parent && !WasCreatedByThisConnection(*old_parent) &&
94      !IsViewInRoots(*old_parent)) {
95    *old_parent = NULL;
96  }
97  return true;
98}
99
100bool DefaultAccessPolicy::IsViewInRoots(const ServerView* view) const {
101  return delegate_->GetRootsForAccessPolicy().count(
102             ViewIdToTransportId(view->id())) > 0;
103}
104
105}  // namespace service
106}  // namespace mojo
107