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 MOJO_SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_
6 #define MOJO_SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "mojo/services/public/interfaces/view_manager/view_manager.mojom.h"
17 #include "mojo/services/view_manager/access_policy_delegate.h"
18 #include "mojo/services/view_manager/ids.h"
19 #include "mojo/services/view_manager/view_manager_export.h"
30 class RootNodeManager
;
33 // Equivalent of NON_EXPORTED_BASE which does not work with the template snafu
36 #pragma warning(disable : 4275)
39 // Manages a connection from the client.
40 class MOJO_VIEW_MANAGER_EXPORT ViewManagerServiceImpl
41 : public InterfaceImpl
<ViewManagerService
>,
42 public AccessPolicyDelegate
{
44 ViewManagerServiceImpl(RootNodeManager
* root_node_manager
,
45 ConnectionSpecificId creator_id
,
46 const std::string
& creator_url
,
47 const std::string
& url
,
48 const NodeId
& root_id
,
49 InterfaceRequest
<ServiceProvider
> service_provider
);
50 virtual ~ViewManagerServiceImpl();
52 // Used to mark this connection as originating from a call to
53 // ViewManagerService::Connect(). When set OnConnectionError() deletes |this|.
54 void set_delete_on_connection_error() { delete_on_connection_error_
= true; }
56 ConnectionSpecificId
id() const { return id_
; }
57 ConnectionSpecificId
creator_id() const { return creator_id_
; }
58 const std::string
& url() const { return url_
; }
60 // Returns the Node with the specified id.
61 Node
* GetNode(const NodeId
& id
) {
62 return const_cast<Node
*>(
63 const_cast<const ViewManagerServiceImpl
*>(this)->GetNode(id
));
65 const Node
* GetNode(const NodeId
& id
) const;
67 // Returns true if this has |id| as a root.
68 bool HasRoot(const NodeId
& id
) const;
70 // Invoked when a connection is destroyed.
71 void OnViewManagerServiceImplDestroyed(ConnectionSpecificId id
);
73 // The following methods are invoked after the corresponding change has been
74 // processed. They do the appropriate bookkeeping and update the client as
76 void ProcessNodeBoundsChanged(const Node
* node
,
77 const gfx::Rect
& old_bounds
,
78 const gfx::Rect
& new_bounds
,
79 bool originated_change
);
80 void ProcessNodeHierarchyChanged(const Node
* node
,
81 const Node
* new_parent
,
82 const Node
* old_parent
,
83 bool originated_change
);
84 void ProcessNodeReorder(const Node
* node
,
85 const Node
* relative_node
,
86 OrderDirection direction
,
87 bool originated_change
);
88 void ProcessNodeDeleted(const NodeId
& node
, bool originated_change
);
90 // TODO(sky): move this to private section (currently can't because of
92 // InterfaceImp overrides:
93 virtual void OnConnectionError() MOJO_OVERRIDE
;
96 typedef std::map
<ConnectionSpecificId
, Node
*> NodeMap
;
97 typedef base::hash_set
<Id
> NodeIdSet
;
99 bool IsNodeKnown(const Node
* node
) const;
101 // These functions return true if the corresponding mojom function is allowed
102 // for this connection.
103 bool CanReorderNode(const Node
* node
,
104 const Node
* relative_node
,
105 OrderDirection direction
) const;
107 // Deletes a node owned by this connection. Returns true on success. |source|
108 // is the connection that originated the change.
109 bool DeleteNodeImpl(ViewManagerServiceImpl
* source
, Node
* node
);
111 // If |node| is known (in |known_nodes_|) does nothing. Otherwise adds |node|
112 // to |nodes|, marks |node| as known and recurses.
113 void GetUnknownNodesFrom(const Node
* node
, std::vector
<const Node
*>* nodes
);
115 // Removes |node| and all its descendants from |known_nodes_|. This does not
116 // recurse through nodes that were created by this connection. All nodes owned
117 // by this connection are added to |local_nodes|.
118 void RemoveFromKnown(const Node
* node
, std::vector
<Node
*>* local_nodes
);
120 // Adds |node_id| to the set of roots this connection knows about. The caller
121 // should have verified |node_id| is not among the roots of this connection.
122 void AddRoot(const NodeId
& node_id
,
123 InterfaceRequest
<ServiceProvider
> service_provider
);
125 // Removes |node_id| from the set of roots this connection knows about.
126 void RemoveRoot(const NodeId
& node_id
);
128 void RemoveChildrenAsPartOfEmbed(const NodeId
& node_id
);
130 // Converts Node(s) to ViewData(s) for transport. This assumes all the nodes
131 // are valid for the client. The parent of nodes the client is not allowed to
132 // see are set to NULL (in the returned ViewData(s)).
133 Array
<ViewDataPtr
> NodesToViewDatas(const std::vector
<const Node
*>& nodes
);
134 ViewDataPtr
NodeToViewData(const Node
* node
);
136 // Implementation of GetNodeTree(). Adds |node| to |nodes| and recurses if
137 // CanDescendIntoNodeForNodeTree() returns true.
138 void GetNodeTreeImpl(const Node
* node
, std::vector
<const Node
*>* nodes
) const;
140 // ViewManagerService:
141 virtual void CreateView(Id transport_view_id
,
142 const Callback
<void(ErrorCode
)>& callback
) OVERRIDE
;
143 virtual void DeleteView(Id transport_view_id
,
144 const Callback
<void(bool)>& callback
) OVERRIDE
;
145 virtual void AddView(Id parent_id
,
147 const Callback
<void(bool)>& callback
) OVERRIDE
;
148 virtual void RemoveViewFromParent(
150 const Callback
<void(bool)>& callback
) OVERRIDE
;
151 virtual void ReorderView(Id view_id
,
153 OrderDirection direction
,
154 const Callback
<void(bool)>& callback
) OVERRIDE
;
155 virtual void GetViewTree(
157 const Callback
<void(Array
<ViewDataPtr
>)>& callback
) OVERRIDE
;
158 virtual void SetViewContents(Id view_id
,
159 ScopedSharedBufferHandle buffer
,
160 uint32_t buffer_size
,
161 const Callback
<void(bool)>& callback
) OVERRIDE
;
162 virtual void SetViewBounds(Id view_id
,
164 const Callback
<void(bool)>& callback
) OVERRIDE
;
165 virtual void SetViewVisibility(Id view_id
,
167 const Callback
<void(bool)>& callback
) OVERRIDE
;
168 virtual void Embed(const String
& url
,
170 ServiceProviderPtr service_provider
,
171 const Callback
<void(bool)>& callback
) OVERRIDE
;
172 virtual void DispatchOnViewInputEvent(Id view_id
, EventPtr event
) OVERRIDE
;
175 virtual void OnConnectionEstablished() MOJO_OVERRIDE
;
177 // AccessPolicyDelegate:
178 virtual const base::hash_set
<Id
>& GetRootsForAccessPolicy() const OVERRIDE
;
179 virtual bool IsNodeKnownForAccessPolicy(const Node
* node
) const OVERRIDE
;
180 virtual bool IsNodeRootOfAnotherConnectionForAccessPolicy(
181 const Node
* node
) const OVERRIDE
;
183 RootNodeManager
* root_node_manager_
;
185 // Id of this connection as assigned by RootNodeManager.
186 const ConnectionSpecificId id_
;
188 // URL this connection was created for.
189 const std::string url_
;
191 // ID of the connection that created us. If 0 it indicates either we were
192 // created by the root, or the connection that created us has been destroyed.
193 ConnectionSpecificId creator_id_
;
195 // The URL of the app that embedded the app this connection was created for.
196 const std::string creator_url_
;
198 scoped_ptr
<AccessPolicy
> access_policy_
;
200 // The nodes and views created by this connection. This connection owns these
204 // The set of nodes that has been communicated to the client.
205 NodeIdSet known_nodes_
;
207 // Set of root nodes from other connections. More specifically any time
208 // Embed() is invoked the id of the node is added to this set for the child
209 // connection. The connection Embed() was invoked on (the parent) doesn't
210 // directly track which connections are attached to which of its nodes. That
211 // information can be found by looking through the |roots_| of all
215 // See description above setter.
216 bool delete_on_connection_error_
;
218 InterfaceRequest
<ServiceProvider
> service_provider_
;
220 DISALLOW_COPY_AND_ASSIGN(ViewManagerServiceImpl
);
227 } // namespace service
230 #endif // MOJO_SERVICES_VIEW_MANAGER_VIEW_MANAGER_SERVICE_IMPL_H_