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 #ifndef COMPONENTS_VIEW_MANAGER_VIEW_TREE_IMPL_H_
6 #define COMPONENTS_VIEW_MANAGER_VIEW_TREE_IMPL_H_
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "components/view_manager/access_policy_delegate.h"
17 #include "components/view_manager/ids.h"
18 #include "components/view_manager/public/interfaces/surface_id.mojom.h"
19 #include "components/view_manager/public/interfaces/view_tree.mojom.h"
25 namespace view_manager
{
28 class ConnectionManager
;
30 class ViewTreeHostImpl
;
32 // An instance of ViewTreeImpl is created for every ViewTree request.
33 // ViewTreeImpl tracks all the state and views created by a client. ViewTreeImpl
34 // coordinates with ConnectionManager to update the client (and internal state)
36 class ViewTreeImpl
: public mojo::ViewTree
, public AccessPolicyDelegate
{
38 ViewTreeImpl(ConnectionManager
* connection_manager
,
39 mojo::ConnectionSpecificId creator_id
,
40 const ViewId
& root_id
);
41 ~ViewTreeImpl() override
;
43 // |services| and |exposed_services| are the ServiceProviders to pass to the
44 // client via OnEmbed().
45 void Init(mojo::ViewTreeClient
* client
, mojo::ViewTreePtr tree
);
47 mojo::ConnectionSpecificId
id() const { return id_
; }
48 mojo::ConnectionSpecificId
creator_id() const { return creator_id_
; }
50 mojo::ViewTreeClient
* client() { return client_
; }
52 // Returns the View with the specified id.
53 ServerView
* GetView(const ViewId
& id
) {
54 return const_cast<ServerView
*>(
55 const_cast<const ViewTreeImpl
*>(this)->GetView(id
));
57 const ServerView
* GetView(const ViewId
& id
) const;
59 // Returns true if this connection's root is |id|.
60 bool IsRoot(const ViewId
& id
) const;
62 // Returns the id of the root node. This is null if the root has been
63 // destroyed but the connection is still valid.
64 const ViewId
* root() const { return root_
.get(); }
66 bool is_embed_root() const { return is_embed_root_
; }
68 ViewTreeHostImpl
* GetHost();
70 // Invoked when a connection is about to be destroyed.
71 void OnWillDestroyViewTreeImpl(ViewTreeImpl
* connection
);
73 // These functions are synchronous variants of those defined in the mojom. The
74 // ViewTree implementations all call into these. See the mojom for details.
75 mojo::ErrorCode
CreateView(const ViewId
& view_id
);
76 bool AddView(const ViewId
& parent_id
, const ViewId
& child_id
);
77 std::vector
<const ServerView
*> GetViewTree(const ViewId
& view_id
) const;
78 bool SetViewVisibility(const ViewId
& view_id
, bool visible
);
79 bool Embed(const ViewId
& view_id
,
80 mojo::ViewTreeClientPtr client
,
81 mojo::ConnectionSpecificId
* connection_id
);
82 void Embed(const ViewId
& view_id
, mojo::URLRequestPtr request
);
84 // The following methods are invoked after the corresponding change has been
85 // processed. They do the appropriate bookkeeping and update the client as
87 void ProcessViewBoundsChanged(const ServerView
* view
,
88 const gfx::Rect
& old_bounds
,
89 const gfx::Rect
& new_bounds
,
90 bool originated_change
);
91 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics
& old_metrics
,
92 const mojo::ViewportMetrics
& new_metrics
,
93 bool originated_change
);
94 void ProcessWillChangeViewHierarchy(const ServerView
* view
,
95 const ServerView
* new_parent
,
96 const ServerView
* old_parent
,
97 bool originated_change
);
98 void ProcessViewPropertyChanged(const ServerView
* view
,
99 const std::string
& name
,
100 const std::vector
<uint8_t>* new_data
,
101 bool originated_change
);
102 void ProcessViewHierarchyChanged(const ServerView
* view
,
103 const ServerView
* new_parent
,
104 const ServerView
* old_parent
,
105 bool originated_change
);
106 void ProcessViewReorder(const ServerView
* view
,
107 const ServerView
* relative_view
,
108 mojo::OrderDirection direction
,
109 bool originated_change
);
110 void ProcessViewDeleted(const ViewId
& view
, bool originated_change
);
111 void ProcessWillChangeViewVisibility(const ServerView
* view
,
112 bool originated_change
);
113 void ProcessFocusChanged(const ServerView
* old_focused_view
,
114 const ServerView
* new_focused_view
);
117 using ViewIdSet
= base::hash_set
<mojo::Id
>;
118 using ViewMap
= std::map
<mojo::ConnectionSpecificId
, ServerView
*>;
120 bool IsViewKnown(const ServerView
* view
) const;
122 // These functions return true if the corresponding mojom function is allowed
123 // for this connection.
124 bool CanReorderView(const ServerView
* view
,
125 const ServerView
* relative_view
,
126 mojo::OrderDirection direction
) const;
128 // Deletes a view owned by this connection. Returns true on success. |source|
129 // is the connection that originated the change.
130 bool DeleteViewImpl(ViewTreeImpl
* source
, ServerView
* view
);
132 // If |view| is known (in |known_views_|) does nothing. Otherwise adds |view|
133 // to |views|, marks |view| as known and recurses.
134 void GetUnknownViewsFrom(const ServerView
* view
,
135 std::vector
<const ServerView
*>* views
);
137 // Removes |view| and all its descendants from |known_views_|. This does not
138 // recurse through views that were created by this connection. All views owned
139 // by this connection are added to |local_views|.
140 void RemoveFromKnown(const ServerView
* view
,
141 std::vector
<ServerView
*>* local_views
);
143 // Resets the root of this connection.
146 // Converts View(s) to ViewData(s) for transport. This assumes all the views
147 // are valid for the client. The parent of views the client is not allowed to
148 // see are set to NULL (in the returned ViewData(s)).
149 mojo::Array
<mojo::ViewDataPtr
> ViewsToViewDatas(
150 const std::vector
<const ServerView
*>& views
);
151 mojo::ViewDataPtr
ViewToViewData(const ServerView
* view
);
153 // Implementation of GetViewTree(). Adds |view| to |views| and recurses if
154 // CanDescendIntoViewForViewTree() returns true.
155 void GetViewTreeImpl(const ServerView
* view
,
156 std::vector
<const ServerView
*>* views
) const;
158 // Notify the client if the drawn state of any of the roots changes.
159 // |view| is the view that is changing to the drawn state |new_drawn_value|.
160 void NotifyDrawnStateChanged(const ServerView
* view
, bool new_drawn_value
);
162 // Deletes all Views we own.
165 bool CanEmbed(const ViewId
& view_id
) const;
166 void PrepareForEmbed(const ViewId
& view_id
);
167 void RemoveChildrenAsPartOfEmbed(const ViewId
& view_id
);
171 mojo::Id transport_view_id
,
172 const mojo::Callback
<void(mojo::ErrorCode
)>& callback
) override
;
173 void DeleteView(mojo::Id transport_view_id
,
174 const mojo::Callback
<void(bool)>& callback
) override
;
175 void AddView(mojo::Id parent_id
,
177 const mojo::Callback
<void(bool)>& callback
) override
;
178 void RemoveViewFromParent(
180 const mojo::Callback
<void(bool)>& callback
) override
;
181 void ReorderView(mojo::Id view_id
,
182 mojo::Id relative_view_id
,
183 mojo::OrderDirection direction
,
184 const mojo::Callback
<void(bool)>& callback
) override
;
185 void GetViewTree(mojo::Id view_id
,
186 const mojo::Callback
<void(mojo::Array
<mojo::ViewDataPtr
>)>&
188 void SetViewBounds(mojo::Id view_id
,
189 mojo::RectPtr bounds
,
190 const mojo::Callback
<void(bool)>& callback
) override
;
191 void SetViewVisibility(mojo::Id view_id
,
193 const mojo::Callback
<void(bool)>& callback
) override
;
194 void SetViewProperty(mojo::Id view_id
,
195 const mojo::String
& name
,
196 mojo::Array
<uint8_t> value
,
197 const mojo::Callback
<void(bool)>& callback
) override
;
200 mojo::InterfaceRequest
<mojo::Surface
> surface
,
201 mojo::SurfaceClientPtr client
) override
;
202 void Embed(mojo::Id transport_view_id
,
203 mojo::ViewTreeClientPtr client
,
204 const EmbedCallback
& callback
) override
;
205 void SetFocus(uint32_t view_id
) override
;
206 void SetViewTextInputState(uint32_t view_id
,
207 mojo::TextInputStatePtr state
) override
;
208 void SetImeVisibility(mojo::Id transport_view_id
,
210 mojo::TextInputStatePtr state
) override
;
211 void SetAccessPolicy(mojo::Id transport_view_id
,
212 uint32 policy_bitmask
) override
;
214 // AccessPolicyDelegate:
215 bool IsRootForAccessPolicy(const ViewId
& id
) const override
;
216 bool IsViewKnownForAccessPolicy(const ServerView
* view
) const override
;
217 bool IsViewRootOfAnotherConnectionForAccessPolicy(
218 const ServerView
* view
) const override
;
219 bool IsDescendantOfEmbedRoot(const ServerView
* view
) override
;
221 ConnectionManager
* connection_manager_
;
223 // Id of this connection as assigned by ConnectionManager.
224 const mojo::ConnectionSpecificId id_
;
226 // ID of the connection that created us. If 0 it indicates either we were
227 // created by the root, or the connection that created us has been destroyed.
228 mojo::ConnectionSpecificId creator_id_
;
230 mojo::ViewTreeClient
* client_
;
232 scoped_ptr
<view_manager::AccessPolicy
> access_policy_
;
234 // The views created by this connection. This connection owns these objects.
237 // The set of views that has been communicated to the client.
238 ViewIdSet known_views_
;
240 // The root of this connection. This is a scoped_ptr to reinforce the
241 // connection may have no root. A connection has no root if either the root
242 // is destroyed or Embed() is invoked on the root.
243 scoped_ptr
<ViewId
> root_
;
247 DISALLOW_COPY_AND_ASSIGN(ViewTreeImpl
);
250 } // namespace view_manager
252 #endif // COMPONENTS_VIEW_MANAGER_VIEW_TREE_IMPL_H_