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_MANAGER_SERVICE_IMPL_H_
6 #define COMPONENTS_VIEW_MANAGER_VIEW_MANAGER_SERVICE_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_manager.mojom.h"
25 namespace view_manager
{
28 class ConnectionManager
;
31 // An instance of ViewManagerServiceImpl is created for every ViewManagerService
32 // request. ViewManagerServiceImpl tracks all the state and views created by a
33 // client. ViewManagerServiceImpl coordinates with ConnectionManager to update
34 // the client (and internal state) as necessary.
35 class ViewManagerServiceImpl
: public mojo::ViewManagerService
,
36 public AccessPolicyDelegate
{
38 ViewManagerServiceImpl(ConnectionManager
* connection_manager
,
39 mojo::ConnectionSpecificId creator_id
,
40 const ViewId
& root_id
);
41 ~ViewManagerServiceImpl() override
;
43 // |services| and |exposed_services| are the ServiceProviders to pass to the
44 // client via OnEmbed().
45 void Init(mojo::ViewManagerClient
* client
,
46 mojo::ViewManagerServicePtr service_ptr
);
48 mojo::ConnectionSpecificId
id() const { return id_
; }
49 mojo::ConnectionSpecificId
creator_id() const { return creator_id_
; }
51 mojo::ViewManagerClient
* 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 ViewManagerServiceImpl
*>(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 // Invoked when a connection is about to be destroyed.
70 void OnWillDestroyViewManagerServiceImpl(ViewManagerServiceImpl
* connection
);
72 // These functions are synchronous variants of those defined in the mojom. The
73 // ViewManagerService implementations all call into these. See the mojom for
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 void EmbedAllowingReembed(const ViewId
& view_id
,
80 mojo::URLRequestPtr request
,
81 const mojo::Callback
<void(bool)>& callback
);
82 bool Embed(const ViewId
& view_id
, mojo::ViewManagerClientPtr client
);
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
*>;
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(ViewManagerServiceImpl
* 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.
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.
167 bool CanEmbed(const ViewId
& view_id
) const;
168 void PrepareForEmbed(const ViewId
& view_id
);
169 void RemoveChildrenAsPartOfEmbed(const ViewId
& view_id
);
170 void OnEmbedForDescendantDone(scoped_refptr
<PendingEmbed
> pending_embed
,
171 mojo::ViewManagerClientPtr client
);
173 // Invalidates any PendingEmbeds with |connection| as the embed root.
174 void InvalidatePendingEmbedForConnection(ViewManagerServiceImpl
* connection
);
176 // Invalidates any PendingEmbemds targetting |view_id|.
177 void InvalidatePendingEmbedForView(const ViewId
& view_id
);
179 // Runs the callback for |embed| and releases it.
180 void RemovePendingEmbedAndNotifyCallback(scoped_refptr
<PendingEmbed
> embed
,
183 // ViewManagerService:
185 mojo::Id transport_view_id
,
186 const mojo::Callback
<void(mojo::ErrorCode
)>& callback
) override
;
187 void DeleteView(mojo::Id transport_view_id
,
188 const mojo::Callback
<void(bool)>& callback
) override
;
189 void AddView(mojo::Id parent_id
,
191 const mojo::Callback
<void(bool)>& callback
) override
;
192 void RemoveViewFromParent(
194 const mojo::Callback
<void(bool)>& callback
) override
;
195 void ReorderView(mojo::Id view_id
,
196 mojo::Id relative_view_id
,
197 mojo::OrderDirection direction
,
198 const mojo::Callback
<void(bool)>& callback
) override
;
199 void GetViewTree(mojo::Id view_id
,
200 const mojo::Callback
<void(mojo::Array
<mojo::ViewDataPtr
>)>&
202 void SetViewSurfaceId(mojo::Id view_id
,
203 mojo::SurfaceIdPtr surface_id
,
204 const mojo::Callback
<void(bool)>& callback
) override
;
205 void SetViewBounds(mojo::Id view_id
,
206 mojo::RectPtr bounds
,
207 const mojo::Callback
<void(bool)>& callback
) override
;
208 void SetViewVisibility(mojo::Id view_id
,
210 const mojo::Callback
<void(bool)>& callback
) override
;
211 void SetViewProperty(mojo::Id view_id
,
212 const mojo::String
& name
,
213 mojo::Array
<uint8_t> value
,
214 const mojo::Callback
<void(bool)>& callback
) override
;
215 void SetEmbedRoot() override
;
216 void Embed(mojo::Id transport_view_id
,
217 mojo::ViewManagerClientPtr client
,
218 const mojo::Callback
<void(bool)>& callback
) override
;
219 void EmbedAllowingReembed(
220 mojo::Id transport_view_id
,
221 mojo::URLRequestPtr request
,
222 const mojo::Callback
<void(bool)>& callback
) override
;
223 void SetFocus(uint32_t view_id
, const SetFocusCallback
& callback
) override
;
224 void SetViewTextInputState(uint32_t view_id
,
225 mojo::TextInputStatePtr state
) override
;
226 void SetImeVisibility(uint32_t view_id
,
228 mojo::TextInputStatePtr state
) override
;
231 // AccessPolicyDelegate:
232 bool IsRootForAccessPolicy(const ViewId
& id
) const override
;
233 bool IsViewKnownForAccessPolicy(const ServerView
* view
) const override
;
234 bool IsViewRootOfAnotherConnectionForAccessPolicy(
235 const ServerView
* view
) const override
;
236 bool IsDescendantOfEmbedRoot(const ServerView
* view
) override
;
238 ConnectionManager
* connection_manager_
;
240 // Id of this connection as assigned by ConnectionManager.
241 const mojo::ConnectionSpecificId id_
;
243 // ID of the connection that created us. If 0 it indicates either we were
244 // created by the root, or the connection that created us has been destroyed.
245 mojo::ConnectionSpecificId creator_id_
;
247 mojo::ViewManagerClient
* client_
;
249 scoped_ptr
<AccessPolicy
> access_policy_
;
251 // The views created by this connection. This connection owns these objects.
254 // The set of views that has been communicated to the client.
255 ViewIdSet known_views_
;
257 // The root of this connection. This is a scoped_ptr to reinforce the
258 // connection may have no root. A connection has no root if either the root
259 // is destroyed or Embed() is invoked on the root.
260 scoped_ptr
<ViewId
> root_
;
264 // If we have an ancestor marked as an embed root then we have to query it
265 // before completing the embed. |pending_embeds_| contains such requests.
266 // The requests are removed if the embed becomes invalid, for example, the
267 // view embed was called for is removed.
268 std::set
<scoped_refptr
<PendingEmbed
>> pending_embeds_
;
270 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceImpl
);
273 } // namespace view_manager
275 #endif // COMPONENTS_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_