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
;
31 // An instance of ViewTreeImpl is created for every ViewTree request.
32 // ViewTreeImpl tracks all the state and views created by a client. ViewTreeImpl
33 // coordinates with ConnectionManager to update the client (and internal state)
35 class ViewTreeImpl
: public mojo::ViewTree
, public AccessPolicyDelegate
{
37 ViewTreeImpl(ConnectionManager
* connection_manager
,
38 mojo::ConnectionSpecificId creator_id
,
39 const ViewId
& root_id
);
40 ~ViewTreeImpl() override
;
42 // |services| and |exposed_services| are the ServiceProviders to pass to the
43 // client via OnEmbed().
44 void Init(mojo::ViewTreeClient
* client
, mojo::ViewTreePtr tree
);
46 mojo::ConnectionSpecificId
id() const { return id_
; }
47 mojo::ConnectionSpecificId
creator_id() const { return creator_id_
; }
49 mojo::ViewTreeClient
* client() { return client_
; }
51 // Returns the View with the specified id.
52 ServerView
* GetView(const ViewId
& id
) {
53 return const_cast<ServerView
*>(
54 const_cast<const ViewTreeImpl
*>(this)->GetView(id
));
56 const ServerView
* GetView(const ViewId
& id
) const;
58 // Returns true if this connection's root is |id|.
59 bool IsRoot(const ViewId
& id
) const;
61 // Returns the id of the root node. This is null if the root has been
62 // destroyed but the connection is still valid.
63 const ViewId
* root() const { return root_
.get(); }
65 bool is_embed_root() const { return is_embed_root_
; }
67 // Invoked when a connection is about to be destroyed.
68 void OnWillDestroyViewTreeImpl(ViewTreeImpl
* connection
);
70 // These functions are synchronous variants of those defined in the mojom. The
71 // ViewTree implementations all call into these. See the mojom for details.
72 mojo::ErrorCode
CreateView(const ViewId
& view_id
);
73 bool AddView(const ViewId
& parent_id
, const ViewId
& child_id
);
74 std::vector
<const ServerView
*> GetViewTree(const ViewId
& view_id
) const;
75 bool SetViewVisibility(const ViewId
& view_id
, bool visible
);
76 bool Embed(const ViewId
& view_id
, mojo::ViewTreeClientPtr client
);
77 void Embed(const ViewId
& view_id
, mojo::URLRequestPtr request
);
79 // The following methods are invoked after the corresponding change has been
80 // processed. They do the appropriate bookkeeping and update the client as
82 void ProcessViewBoundsChanged(const ServerView
* view
,
83 const gfx::Rect
& old_bounds
,
84 const gfx::Rect
& new_bounds
,
85 bool originated_change
);
86 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics
& old_metrics
,
87 const mojo::ViewportMetrics
& new_metrics
,
88 bool originated_change
);
89 void ProcessWillChangeViewHierarchy(const ServerView
* view
,
90 const ServerView
* new_parent
,
91 const ServerView
* old_parent
,
92 bool originated_change
);
93 void ProcessViewPropertyChanged(const ServerView
* view
,
94 const std::string
& name
,
95 const std::vector
<uint8_t>* new_data
,
96 bool originated_change
);
97 void ProcessViewHierarchyChanged(const ServerView
* view
,
98 const ServerView
* new_parent
,
99 const ServerView
* old_parent
,
100 bool originated_change
);
101 void ProcessViewReorder(const ServerView
* view
,
102 const ServerView
* relative_view
,
103 mojo::OrderDirection direction
,
104 bool originated_change
);
105 void ProcessViewDeleted(const ViewId
& view
, bool originated_change
);
106 void ProcessWillChangeViewVisibility(const ServerView
* view
,
107 bool originated_change
);
108 void ProcessFocusChanged(const ServerView
* old_focused_view
,
109 const ServerView
* new_focused_view
);
112 using ViewIdSet
= base::hash_set
<mojo::Id
>;
113 using ViewMap
= std::map
<mojo::ConnectionSpecificId
, ServerView
*>;
115 bool IsViewKnown(const ServerView
* view
) const;
117 // These functions return true if the corresponding mojom function is allowed
118 // for this connection.
119 bool CanReorderView(const ServerView
* view
,
120 const ServerView
* relative_view
,
121 mojo::OrderDirection direction
) const;
123 // Deletes a view owned by this connection. Returns true on success. |source|
124 // is the connection that originated the change.
125 bool DeleteViewImpl(ViewTreeImpl
* source
, ServerView
* view
);
127 // If |view| is known (in |known_views_|) does nothing. Otherwise adds |view|
128 // to |views|, marks |view| as known and recurses.
129 void GetUnknownViewsFrom(const ServerView
* view
,
130 std::vector
<const ServerView
*>* views
);
132 // Removes |view| and all its descendants from |known_views_|. This does not
133 // recurse through views that were created by this connection. All views owned
134 // by this connection are added to |local_views|.
135 void RemoveFromKnown(const ServerView
* view
,
136 std::vector
<ServerView
*>* local_views
);
138 // Resets the root of this connection.
141 // Converts View(s) to ViewData(s) for transport. This assumes all the views
142 // are valid for the client. The parent of views the client is not allowed to
143 // see are set to NULL (in the returned ViewData(s)).
144 mojo::Array
<mojo::ViewDataPtr
> ViewsToViewDatas(
145 const std::vector
<const ServerView
*>& views
);
146 mojo::ViewDataPtr
ViewToViewData(const ServerView
* view
);
148 // Implementation of GetViewTree(). Adds |view| to |views| and recurses if
149 // CanDescendIntoViewForViewTree() returns true.
150 void GetViewTreeImpl(const ServerView
* view
,
151 std::vector
<const ServerView
*>* views
) const;
153 // Notify the client if the drawn state of any of the roots changes.
154 // |view| is the view that is changing to the drawn state |new_drawn_value|.
155 void NotifyDrawnStateChanged(const ServerView
* view
, bool new_drawn_value
);
157 // Deletes all Views we own.
160 bool CanEmbed(const ViewId
& view_id
) const;
161 void PrepareForEmbed(const ViewId
& view_id
);
162 void RemoveChildrenAsPartOfEmbed(const ViewId
& view_id
);
166 mojo::Id transport_view_id
,
167 const mojo::Callback
<void(mojo::ErrorCode
)>& callback
) override
;
168 void DeleteView(mojo::Id transport_view_id
,
169 const mojo::Callback
<void(bool)>& callback
) override
;
170 void AddView(mojo::Id parent_id
,
172 const mojo::Callback
<void(bool)>& callback
) override
;
173 void RemoveViewFromParent(
175 const mojo::Callback
<void(bool)>& callback
) override
;
176 void ReorderView(mojo::Id view_id
,
177 mojo::Id relative_view_id
,
178 mojo::OrderDirection direction
,
179 const mojo::Callback
<void(bool)>& callback
) override
;
180 void GetViewTree(mojo::Id view_id
,
181 const mojo::Callback
<void(mojo::Array
<mojo::ViewDataPtr
>)>&
183 void SetViewBounds(mojo::Id view_id
,
184 mojo::RectPtr bounds
,
185 const mojo::Callback
<void(bool)>& callback
) override
;
186 void SetViewVisibility(mojo::Id view_id
,
188 const mojo::Callback
<void(bool)>& callback
) override
;
189 void SetViewProperty(mojo::Id view_id
,
190 const mojo::String
& name
,
191 mojo::Array
<uint8_t> value
,
192 const mojo::Callback
<void(bool)>& callback
) override
;
195 mojo::InterfaceRequest
<mojo::Surface
> surface
,
196 mojo::SurfaceClientPtr client
) override
;
197 void SetEmbedRoot() override
;
198 void Embed(mojo::Id transport_view_id
,
199 mojo::ViewTreeClientPtr client
,
200 const mojo::Callback
<void(bool)>& callback
) override
;
201 void SetFocus(uint32_t view_id
, const SetFocusCallback
& callback
) override
;
202 void SetViewTextInputState(uint32_t view_id
,
203 mojo::TextInputStatePtr state
) override
;
204 void SetImeVisibility(uint32_t view_id
,
206 mojo::TextInputStatePtr state
) override
;
209 // AccessPolicyDelegate:
210 bool IsRootForAccessPolicy(const ViewId
& id
) const override
;
211 bool IsViewKnownForAccessPolicy(const ServerView
* view
) const override
;
212 bool IsViewRootOfAnotherConnectionForAccessPolicy(
213 const ServerView
* view
) const override
;
214 bool IsDescendantOfEmbedRoot(const ServerView
* view
) override
;
216 ConnectionManager
* connection_manager_
;
218 // Id of this connection as assigned by ConnectionManager.
219 const mojo::ConnectionSpecificId id_
;
221 // ID of the connection that created us. If 0 it indicates either we were
222 // created by the root, or the connection that created us has been destroyed.
223 mojo::ConnectionSpecificId creator_id_
;
225 mojo::ViewTreeClient
* client_
;
227 scoped_ptr
<AccessPolicy
> access_policy_
;
229 // The views created by this connection. This connection owns these objects.
232 // The set of views that has been communicated to the client.
233 ViewIdSet known_views_
;
235 // The root of this connection. This is a scoped_ptr to reinforce the
236 // connection may have no root. A connection has no root if either the root
237 // is destroyed or Embed() is invoked on the root.
238 scoped_ptr
<ViewId
> root_
;
242 DISALLOW_COPY_AND_ASSIGN(ViewTreeImpl
);
245 } // namespace view_manager
247 #endif // COMPONENTS_VIEW_MANAGER_VIEW_TREE_IMPL_H_