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/scoped_ptr.h"
15 #include "components/surfaces/public/interfaces/surface_id.mojom.h"
16 #include "components/view_manager/access_policy_delegate.h"
17 #include "components/view_manager/ids.h"
18 #include "components/view_manager/public/interfaces/view_manager.mojom.h"
24 namespace view_manager
{
27 class ConnectionManager
;
30 // An instance of ViewManagerServiceImpl is created for every ViewManagerService
31 // request. ViewManagerServiceImpl tracks all the state and views created by a
32 // client. ViewManagerServiceImpl coordinates with ConnectionManager to update
33 // the client (and internal state) as necessary.
34 class ViewManagerServiceImpl
: public mojo::ViewManagerService
,
35 public AccessPolicyDelegate
{
37 using ViewIdSet
= base::hash_set
<mojo::Id
>;
39 ViewManagerServiceImpl(ConnectionManager
* connection_manager
,
40 mojo::ConnectionSpecificId creator_id
,
41 const std::string
& creator_url
,
42 const std::string
& url
,
43 const ViewId
& root_id
);
44 ~ViewManagerServiceImpl() override
;
46 // |services| and |exposed_services| are the ServiceProviders to pass to the
47 // client via OnEmbed().
48 void Init(mojo::ViewManagerClient
* client
,
49 mojo::ViewManagerServicePtr service_ptr
,
50 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
51 mojo::ServiceProviderPtr exposed_services
);
53 mojo::ConnectionSpecificId
id() const { return id_
; }
54 mojo::ConnectionSpecificId
creator_id() const { return creator_id_
; }
55 const std::string
& url() const { return url_
; }
57 mojo::ViewManagerClient
* client() { return client_
; }
59 // Returns the View with the specified id.
60 ServerView
* GetView(const ViewId
& id
) {
61 return const_cast<ServerView
*>(
62 const_cast<const ViewManagerServiceImpl
*>(this)->GetView(id
));
64 const ServerView
* GetView(const ViewId
& id
) const;
66 // Returns true if this connection's root is |id|.
67 bool IsRoot(const ViewId
& id
) const;
69 // Returns the id of the root node. This is null if the root has been
70 // destroyed but the connection is still valid.
71 const ViewId
* root() const { return root_
.get(); }
73 // Invoked when a connection is about to be destroyed.
74 void OnWillDestroyViewManagerServiceImpl(ViewManagerServiceImpl
* connection
);
76 // These functions are synchronous variants of those defined in the mojom. The
77 // ViewManagerService implementations all call into these. See the mojom for
79 mojo::ErrorCode
CreateView(const ViewId
& view_id
);
80 bool AddView(const ViewId
& parent_id
, const ViewId
& child_id
);
81 std::vector
<const ServerView
*> GetViewTree(const ViewId
& view_id
) const;
82 bool SetViewVisibility(const ViewId
& view_id
, bool visible
);
83 bool EmbedUrl(const std::string
& url
,
84 const ViewId
& view_id
,
85 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
86 mojo::ServiceProviderPtr exposed_services
);
87 bool Embed(const ViewId
& view_id
, mojo::ViewManagerClientPtr client
);
89 // The following methods are invoked after the corresponding change has been
90 // processed. They do the appropriate bookkeeping and update the client as
92 void ProcessViewBoundsChanged(const ServerView
* view
,
93 const gfx::Rect
& old_bounds
,
94 const gfx::Rect
& new_bounds
,
95 bool originated_change
);
96 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics
& old_metrics
,
97 const mojo::ViewportMetrics
& new_metrics
,
98 bool originated_change
);
99 void ProcessWillChangeViewHierarchy(const ServerView
* view
,
100 const ServerView
* new_parent
,
101 const ServerView
* old_parent
,
102 bool originated_change
);
103 void ProcessViewPropertyChanged(const ServerView
* view
,
104 const std::string
& name
,
105 const std::vector
<uint8_t>* new_data
,
106 bool originated_change
);
107 void ProcessViewHierarchyChanged(const ServerView
* view
,
108 const ServerView
* new_parent
,
109 const ServerView
* old_parent
,
110 bool originated_change
);
111 void ProcessViewReorder(const ServerView
* view
,
112 const ServerView
* relative_view
,
113 mojo::OrderDirection direction
,
114 bool originated_change
);
115 void ProcessViewDeleted(const ViewId
& view
, bool originated_change
);
116 void ProcessWillChangeViewVisibility(const ServerView
* view
,
117 bool originated_change
);
118 void ProcessFocusChanged(const ServerView
* old_focused_view
,
119 const ServerView
* new_focused_view
);
122 typedef std::map
<mojo::ConnectionSpecificId
, ServerView
*> ViewMap
;
124 bool IsViewKnown(const ServerView
* view
) const;
126 // These functions return true if the corresponding mojom function is allowed
127 // for this connection.
128 bool CanReorderView(const ServerView
* view
,
129 const ServerView
* relative_view
,
130 mojo::OrderDirection direction
) const;
132 // Deletes a view owned by this connection. Returns true on success. |source|
133 // is the connection that originated the change.
134 bool DeleteViewImpl(ViewManagerServiceImpl
* source
, ServerView
* view
);
136 // If |view| is known (in |known_views_|) does nothing. Otherwise adds |view|
137 // to |views|, marks |view| as known and recurses.
138 void GetUnknownViewsFrom(const ServerView
* view
,
139 std::vector
<const ServerView
*>* views
);
141 // Removes |view| and all its descendants from |known_views_|. This does not
142 // recurse through views that were created by this connection. All views owned
143 // by this connection are added to |local_views|.
144 void RemoveFromKnown(const ServerView
* view
,
145 std::vector
<ServerView
*>* local_views
);
147 // Resets the root of this connection.
150 void RemoveChildrenAsPartOfEmbed(const ViewId
& view_id
);
152 // Converts View(s) to ViewData(s) for transport. This assumes all the views
153 // are valid for the client. The parent of views the client is not allowed to
154 // see are set to NULL (in the returned ViewData(s)).
155 mojo::Array
<mojo::ViewDataPtr
> ViewsToViewDatas(
156 const std::vector
<const ServerView
*>& views
);
157 mojo::ViewDataPtr
ViewToViewData(const ServerView
* view
);
159 // Implementation of GetViewTree(). Adds |view| to |views| and recurses if
160 // CanDescendIntoViewForViewTree() returns true.
161 void GetViewTreeImpl(const ServerView
* view
,
162 std::vector
<const ServerView
*>* views
) const;
164 // Notify the client if the drawn state of any of the roots changes.
165 // |view| is the view that is changing to the drawn state |new_drawn_value|.
166 void NotifyDrawnStateChanged(const ServerView
* view
, bool new_drawn_value
);
168 // Deletes all Views we own.
171 bool PrepareForEmbed(const ViewId
& view_id
);
173 // ViewManagerService:
175 mojo::Id transport_view_id
,
176 const mojo::Callback
<void(mojo::ErrorCode
)>& callback
) override
;
177 void DeleteView(mojo::Id transport_view_id
,
178 const mojo::Callback
<void(bool)>& callback
) override
;
179 void AddView(mojo::Id parent_id
,
181 const mojo::Callback
<void(bool)>& callback
) override
;
182 void RemoveViewFromParent(
184 const mojo::Callback
<void(bool)>& callback
) override
;
185 void ReorderView(mojo::Id view_id
,
186 mojo::Id relative_view_id
,
187 mojo::OrderDirection direction
,
188 const mojo::Callback
<void(bool)>& callback
) override
;
189 void GetViewTree(mojo::Id view_id
,
190 const mojo::Callback
<void(mojo::Array
<mojo::ViewDataPtr
>)>&
192 void SetViewSurfaceId(mojo::Id view_id
,
193 mojo::SurfaceIdPtr surface_id
,
194 const mojo::Callback
<void(bool)>& callback
) override
;
195 void SetViewBounds(mojo::Id view_id
,
196 mojo::RectPtr bounds
,
197 const mojo::Callback
<void(bool)>& callback
) override
;
198 void SetViewVisibility(mojo::Id view_id
,
200 const mojo::Callback
<void(bool)>& callback
) override
;
201 void SetViewProperty(mojo::Id view_id
,
202 const mojo::String
& name
,
203 mojo::Array
<uint8_t> value
,
204 const mojo::Callback
<void(bool)>& callback
) override
;
205 void EmbedUrl(const mojo::String
& url
,
206 mojo::Id transport_view_id
,
207 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
208 mojo::ServiceProviderPtr exposed_services
,
209 const mojo::Callback
<void(bool)>& callback
) override
;
210 void Embed(mojo::Id transport_view_id
,
211 mojo::ViewManagerClientPtr client
,
212 const mojo::Callback
<void(bool)>& callback
) override
;
213 void SetFocus(uint32_t view_id
, const SetFocusCallback
& callback
) override
;
215 // AccessPolicyDelegate:
216 bool IsRootForAccessPolicy(const ViewId
& id
) const override
;
217 bool IsViewKnownForAccessPolicy(const ServerView
* view
) const override
;
218 bool IsViewRootOfAnotherConnectionForAccessPolicy(
219 const ServerView
* view
) const override
;
221 ConnectionManager
* connection_manager_
;
223 // Id of this connection as assigned by ConnectionManager.
224 const mojo::ConnectionSpecificId id_
;
226 // URL this connection was created for.
227 const std::string url_
;
229 // ID of the connection that created us. If 0 it indicates either we were
230 // created by the root, or the connection that created us has been destroyed.
231 mojo::ConnectionSpecificId creator_id_
;
233 // The URL of the app that embedded the app this connection was created for.
234 // NOTE: this is empty if the connection was created by way of directly
235 // supplying the ViewManagerClient.
236 const std::string creator_url_
;
238 mojo::ViewManagerClient
* client_
;
240 scoped_ptr
<AccessPolicy
> access_policy_
;
242 // The views created by this connection. This connection owns these objects.
245 // The set of views that has been communicated to the client.
246 ViewIdSet known_views_
;
248 // The root of this connection. This is a scoped_ptr to reinforce the
249 // connection may have no root. A connection has no root if either the root
250 // is destroyed or Embed() is invoked on the root.
251 scoped_ptr
<ViewId
> root_
;
253 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceImpl
);
256 } // namespace view_manager
258 #endif // COMPONENTS_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_