Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / components / mus / view_tree_impl.h
blob8549585070791a68710945f1bca233e9559f7a94
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_MUS_VIEW_TREE_IMPL_H_
6 #define COMPONENTS_MUS_VIEW_TREE_IMPL_H_
8 #include <set>
9 #include <string>
10 #include <vector>
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/mus/access_policy_delegate.h"
17 #include "components/mus/ids.h"
18 #include "components/mus/public/interfaces/surface_id.mojom.h"
19 #include "components/mus/public/interfaces/view_tree.mojom.h"
21 namespace gfx {
22 class Rect;
25 namespace mus {
27 class AccessPolicy;
28 class ConnectionManager;
29 class ServerView;
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)
35 // as necessary.
36 class ViewTreeImpl : public mojo::ViewTree, public AccessPolicyDelegate {
37 public:
38 ViewTreeImpl(ConnectionManager* connection_manager,
39 ConnectionSpecificId creator_id,
40 const ViewId& root_id,
41 uint32_t policy_bitmask);
42 ~ViewTreeImpl() override;
44 // |services| and |exposed_services| are the ServiceProviders to pass to the
45 // client via OnEmbed().
46 void Init(mojo::ViewTreeClient* client, mojo::ViewTreePtr tree);
48 ConnectionSpecificId id() const { return id_; }
49 ConnectionSpecificId creator_id() const { return creator_id_; }
51 mojo::ViewTreeClient* client() { return client_; }
53 // Returns the View with the specified id.
54 ServerView* GetView(const ViewId& id) {
55 return const_cast<ServerView*>(
56 const_cast<const ViewTreeImpl*>(this)->GetView(id));
58 const ServerView* GetView(const ViewId& id) const;
60 // Returns true if this connection's root is |id|.
61 bool IsRoot(const ViewId& id) const;
63 // Returns the id of the root node. This is null if the root has been
64 // destroyed but the connection is still valid.
65 const ViewId* root() const { return root_.get(); }
67 bool is_embed_root() const { return is_embed_root_; }
69 ViewTreeHostImpl* GetHost();
71 // Invoked when a connection is about to be destroyed.
72 void OnWillDestroyViewTreeImpl(ViewTreeImpl* connection);
74 // These functions are synchronous variants of those defined in the mojom. The
75 // ViewTree implementations all call into these. See the mojom for details.
76 mojo::ErrorCode CreateView(const ViewId& view_id);
77 bool AddView(const ViewId& parent_id, const ViewId& child_id);
78 std::vector<const ServerView*> GetViewTree(const ViewId& view_id) const;
79 bool SetViewVisibility(const ViewId& view_id, bool visible);
80 bool Embed(const ViewId& view_id,
81 mojo::ViewTreeClientPtr client,
82 uint32_t policy_bitmask,
83 ConnectionSpecificId* connection_id);
84 void Embed(const ViewId& view_id, mojo::URLRequestPtr request);
86 // The following methods are invoked after the corresponding change has been
87 // processed. They do the appropriate bookkeeping and update the client as
88 // necessary.
89 void ProcessViewBoundsChanged(const ServerView* view,
90 const gfx::Rect& old_bounds,
91 const gfx::Rect& new_bounds,
92 bool originated_change);
93 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics& old_metrics,
94 const mojo::ViewportMetrics& new_metrics,
95 bool originated_change);
96 void ProcessWillChangeViewHierarchy(const ServerView* view,
97 const ServerView* new_parent,
98 const ServerView* old_parent,
99 bool originated_change);
100 void ProcessViewPropertyChanged(const ServerView* view,
101 const std::string& name,
102 const std::vector<uint8_t>* new_data,
103 bool originated_change);
104 void ProcessViewHierarchyChanged(const ServerView* view,
105 const ServerView* new_parent,
106 const ServerView* old_parent,
107 bool originated_change);
108 void ProcessViewReorder(const ServerView* view,
109 const ServerView* relative_view,
110 mojo::OrderDirection direction,
111 bool originated_change);
112 void ProcessViewDeleted(const ViewId& view, bool originated_change);
113 void ProcessWillChangeViewVisibility(const ServerView* view,
114 bool originated_change);
115 void ProcessFocusChanged(const ServerView* old_focused_view,
116 const ServerView* new_focused_view);
118 private:
119 using ViewIdSet = base::hash_set<Id>;
120 using ViewMap = std::map<ConnectionSpecificId, ServerView*>;
122 bool IsViewKnown(const ServerView* view) const;
124 // These functions return true if the corresponding mojom function is allowed
125 // for this connection.
126 bool CanReorderView(const ServerView* view,
127 const ServerView* relative_view,
128 mojo::OrderDirection direction) const;
130 // Deletes a view owned by this connection. Returns true on success. |source|
131 // is the connection that originated the change.
132 bool DeleteViewImpl(ViewTreeImpl* source, ServerView* view);
134 // If |view| is known (in |known_views_|) does nothing. Otherwise adds |view|
135 // to |views|, marks |view| as known and recurses.
136 void GetUnknownViewsFrom(const ServerView* view,
137 std::vector<const ServerView*>* views);
139 // Removes |view| and all its descendants from |known_views_|. This does not
140 // recurse through views that were created by this connection. All views owned
141 // by this connection are added to |local_views|.
142 void RemoveFromKnown(const ServerView* view,
143 std::vector<ServerView*>* local_views);
145 // Resets the root of this connection.
146 void RemoveRoot();
148 // Converts View(s) to ViewData(s) for transport. This assumes all the views
149 // are valid for the client. The parent of views the client is not allowed to
150 // see are set to NULL (in the returned ViewData(s)).
151 mojo::Array<mojo::ViewDataPtr> ViewsToViewDatas(
152 const std::vector<const ServerView*>& views);
153 mojo::ViewDataPtr ViewToViewData(const ServerView* view);
155 // Implementation of GetViewTree(). Adds |view| to |views| and recurses if
156 // CanDescendIntoViewForViewTree() returns true.
157 void GetViewTreeImpl(const ServerView* view,
158 std::vector<const ServerView*>* views) const;
160 // Notify the client if the drawn state of any of the roots changes.
161 // |view| is the view that is changing to the drawn state |new_drawn_value|.
162 void NotifyDrawnStateChanged(const ServerView* view, bool new_drawn_value);
164 // Deletes all Views we own.
165 void DestroyViews();
167 bool CanEmbed(const ViewId& view_id, uint32_t policy_bitmask) const;
168 void PrepareForEmbed(const ViewId& view_id);
169 void RemoveChildrenAsPartOfEmbed(const ViewId& view_id);
171 // ViewTree:
172 void CreateView(
173 Id transport_view_id,
174 const mojo::Callback<void(mojo::ErrorCode)>& callback) override;
175 void DeleteView(Id transport_view_id,
176 const mojo::Callback<void(bool)>& callback) override;
177 void AddView(Id parent_id,
178 Id child_id,
179 const mojo::Callback<void(bool)>& callback) override;
180 void RemoveViewFromParent(
181 Id view_id,
182 const mojo::Callback<void(bool)>& callback) override;
183 void ReorderView(Id view_id,
184 Id relative_view_id,
185 mojo::OrderDirection direction,
186 const mojo::Callback<void(bool)>& callback) override;
187 void GetViewTree(Id view_id,
188 const mojo::Callback<void(mojo::Array<mojo::ViewDataPtr>)>&
189 callback) override;
190 void SetViewBounds(Id view_id,
191 mojo::RectPtr bounds,
192 const mojo::Callback<void(bool)>& callback) override;
193 void SetViewVisibility(Id view_id,
194 bool visible,
195 const mojo::Callback<void(bool)>& callback) override;
196 void SetViewProperty(Id view_id,
197 const mojo::String& name,
198 mojo::Array<uint8_t> value,
199 const mojo::Callback<void(bool)>& callback) override;
200 void RequestSurface(Id view_id,
201 mojo::InterfaceRequest<mojo::Surface> surface,
202 mojo::SurfaceClientPtr client) override;
203 void Embed(Id transport_view_id,
204 mojo::ViewTreeClientPtr client,
205 uint32_t policy_bitmask,
206 const EmbedCallback& callback) override;
207 void SetFocus(uint32_t view_id) override;
208 void SetViewTextInputState(uint32_t view_id,
209 mojo::TextInputStatePtr state) override;
210 void SetImeVisibility(Id transport_view_id,
211 bool visible,
212 mojo::TextInputStatePtr state) 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 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 ConnectionSpecificId creator_id_;
230 mojo::ViewTreeClient* client_;
232 scoped_ptr<mus::AccessPolicy> access_policy_;
234 // The views created by this connection. This connection owns these objects.
235 ViewMap view_map_;
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_;
245 bool is_embed_root_;
247 DISALLOW_COPY_AND_ASSIGN(ViewTreeImpl);
250 } // namespace mus
252 #endif // COMPONENTS_MUS_VIEW_TREE_IMPL_H_