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
, mojo::ViewTreeClientPtr client
);
80 void Embed(const ViewId
& view_id
, mojo::URLRequestPtr request
);
82 // The following methods are invoked after the corresponding change has been
83 // processed. They do the appropriate bookkeeping and update the client as
85 void ProcessViewBoundsChanged(const ServerView
* view
,
86 const gfx::Rect
& old_bounds
,
87 const gfx::Rect
& new_bounds
,
88 bool originated_change
);
89 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics
& old_metrics
,
90 const mojo::ViewportMetrics
& new_metrics
,
91 bool originated_change
);
92 void ProcessWillChangeViewHierarchy(const ServerView
* view
,
93 const ServerView
* new_parent
,
94 const ServerView
* old_parent
,
95 bool originated_change
);
96 void ProcessViewPropertyChanged(const ServerView
* view
,
97 const std::string
& name
,
98 const std::vector
<uint8_t>* new_data
,
99 bool originated_change
);
100 void ProcessViewHierarchyChanged(const ServerView
* view
,
101 const ServerView
* new_parent
,
102 const ServerView
* old_parent
,
103 bool originated_change
);
104 void ProcessViewReorder(const ServerView
* view
,
105 const ServerView
* relative_view
,
106 mojo::OrderDirection direction
,
107 bool originated_change
);
108 void ProcessViewDeleted(const ViewId
& view
, bool originated_change
);
109 void ProcessWillChangeViewVisibility(const ServerView
* view
,
110 bool originated_change
);
111 void ProcessFocusChanged(const ServerView
* old_focused_view
,
112 const ServerView
* new_focused_view
);
115 using ViewIdSet
= base::hash_set
<mojo::Id
>;
116 using ViewMap
= std::map
<mojo::ConnectionSpecificId
, ServerView
*>;
118 bool IsViewKnown(const ServerView
* view
) const;
120 // These functions return true if the corresponding mojom function is allowed
121 // for this connection.
122 bool CanReorderView(const ServerView
* view
,
123 const ServerView
* relative_view
,
124 mojo::OrderDirection direction
) const;
126 // Deletes a view owned by this connection. Returns true on success. |source|
127 // is the connection that originated the change.
128 bool DeleteViewImpl(ViewTreeImpl
* source
, ServerView
* view
);
130 // If |view| is known (in |known_views_|) does nothing. Otherwise adds |view|
131 // to |views|, marks |view| as known and recurses.
132 void GetUnknownViewsFrom(const ServerView
* view
,
133 std::vector
<const ServerView
*>* views
);
135 // Removes |view| and all its descendants from |known_views_|. This does not
136 // recurse through views that were created by this connection. All views owned
137 // by this connection are added to |local_views|.
138 void RemoveFromKnown(const ServerView
* view
,
139 std::vector
<ServerView
*>* local_views
);
141 // Resets the root of this connection.
144 // Converts View(s) to ViewData(s) for transport. This assumes all the views
145 // are valid for the client. The parent of views the client is not allowed to
146 // see are set to NULL (in the returned ViewData(s)).
147 mojo::Array
<mojo::ViewDataPtr
> ViewsToViewDatas(
148 const std::vector
<const ServerView
*>& views
);
149 mojo::ViewDataPtr
ViewToViewData(const ServerView
* view
);
151 // Implementation of GetViewTree(). Adds |view| to |views| and recurses if
152 // CanDescendIntoViewForViewTree() returns true.
153 void GetViewTreeImpl(const ServerView
* view
,
154 std::vector
<const ServerView
*>* views
) const;
156 // Notify the client if the drawn state of any of the roots changes.
157 // |view| is the view that is changing to the drawn state |new_drawn_value|.
158 void NotifyDrawnStateChanged(const ServerView
* view
, bool new_drawn_value
);
160 // Deletes all Views we own.
163 bool CanEmbed(const ViewId
& view_id
) const;
164 void PrepareForEmbed(const ViewId
& view_id
);
165 void RemoveChildrenAsPartOfEmbed(const ViewId
& view_id
);
169 mojo::Id transport_view_id
,
170 const mojo::Callback
<void(mojo::ErrorCode
)>& callback
) override
;
171 void DeleteView(mojo::Id transport_view_id
,
172 const mojo::Callback
<void(bool)>& callback
) override
;
173 void AddView(mojo::Id parent_id
,
175 const mojo::Callback
<void(bool)>& callback
) override
;
176 void RemoveViewFromParent(
178 const mojo::Callback
<void(bool)>& callback
) override
;
179 void ReorderView(mojo::Id view_id
,
180 mojo::Id relative_view_id
,
181 mojo::OrderDirection direction
,
182 const mojo::Callback
<void(bool)>& callback
) override
;
183 void GetViewTree(mojo::Id view_id
,
184 const mojo::Callback
<void(mojo::Array
<mojo::ViewDataPtr
>)>&
186 void SetViewBounds(mojo::Id view_id
,
187 mojo::RectPtr bounds
,
188 const mojo::Callback
<void(bool)>& callback
) override
;
189 void SetViewVisibility(mojo::Id view_id
,
191 const mojo::Callback
<void(bool)>& callback
) override
;
192 void SetViewProperty(mojo::Id view_id
,
193 const mojo::String
& name
,
194 mojo::Array
<uint8_t> value
,
195 const mojo::Callback
<void(bool)>& callback
) override
;
198 mojo::InterfaceRequest
<mojo::Surface
> surface
,
199 mojo::SurfaceClientPtr client
) override
;
200 void Embed(mojo::Id transport_view_id
,
201 mojo::ViewTreeClientPtr client
,
202 const mojo::Callback
<void(bool)>& callback
) override
;
203 void SetFocus(uint32_t view_id
) override
;
204 void SetViewTextInputState(uint32_t view_id
,
205 mojo::TextInputStatePtr state
) override
;
206 void SetImeVisibility(mojo::Id transport_view_id
,
208 mojo::TextInputStatePtr state
) override
;
209 void SetAccessPolicy(mojo::Id transport_view_id
,
210 uint32 policy_bitmask
) override
;
212 // AccessPolicyDelegate:
213 bool IsRootForAccessPolicy(const ViewId
& id
) const override
;
214 bool IsViewKnownForAccessPolicy(const ServerView
* view
) const override
;
215 bool IsViewRootOfAnotherConnectionForAccessPolicy(
216 const ServerView
* view
) const override
;
217 bool IsDescendantOfEmbedRoot(const ServerView
* view
) override
;
219 ConnectionManager
* connection_manager_
;
221 // Id of this connection as assigned by ConnectionManager.
222 const mojo::ConnectionSpecificId id_
;
224 // ID of the connection that created us. If 0 it indicates either we were
225 // created by the root, or the connection that created us has been destroyed.
226 mojo::ConnectionSpecificId creator_id_
;
228 mojo::ViewTreeClient
* client_
;
230 scoped_ptr
<view_manager::AccessPolicy
> access_policy_
;
232 // The views created by this connection. This connection owns these objects.
235 // The set of views that has been communicated to the client.
236 ViewIdSet known_views_
;
238 // The root of this connection. This is a scoped_ptr to reinforce the
239 // connection may have no root. A connection has no root if either the root
240 // is destroyed or Embed() is invoked on the root.
241 scoped_ptr
<ViewId
> root_
;
245 DISALLOW_COPY_AND_ASSIGN(ViewTreeImpl
);
248 } // namespace view_manager
250 #endif // COMPONENTS_VIEW_MANAGER_VIEW_TREE_IMPL_H_