OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef MOJO_SERVICES_VIEW_MANAGER_ROOT_NODE_MANAGER_H_ | |
6 #define MOJO_SERVICES_VIEW_MANAGER_ROOT_NODE_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "mojo/public/cpp/bindings/array.h" | |
13 #include "mojo/services/view_manager/display_manager.h" | |
14 #include "mojo/services/view_manager/ids.h" | |
15 #include "mojo/services/view_manager/node.h" | |
16 #include "mojo/services/view_manager/node_delegate.h" | |
17 #include "mojo/services/view_manager/view_manager_export.h" | |
18 | |
19 namespace ui { | |
20 class Event; | |
21 } | |
22 | |
23 namespace mojo { | |
24 | |
25 class ApplicationConnection; | |
26 | |
27 namespace service { | |
28 | |
29 class DisplayManagerDelegate; | |
30 class ViewManagerServiceImpl; | |
31 | |
32 // RootNodeManager is responsible for managing the set of | |
33 // ViewManagerServiceImpls as well as providing the root of the node hierarchy. | |
34 class MOJO_VIEW_MANAGER_EXPORT RootNodeManager : public NodeDelegate { | |
35 public: | |
36 // Create when a ViewManagerServiceImpl is about to make a change. Ensures | |
37 // clients are notified of the correct change id. | |
38 class ScopedChange { | |
39 public: | |
40 ScopedChange(ViewManagerServiceImpl* connection, | |
41 RootNodeManager* root, | |
42 bool is_delete_node); | |
43 ~ScopedChange(); | |
44 | |
45 ConnectionSpecificId connection_id() const { return connection_id_; } | |
46 bool is_delete_node() const { return is_delete_node_; } | |
47 | |
48 // Marks the connection with the specified id as having seen a message. | |
49 void MarkConnectionAsMessaged(ConnectionSpecificId connection_id) { | |
50 message_ids_.insert(connection_id); | |
51 } | |
52 | |
53 // Returns true if MarkConnectionAsMessaged(connection_id) was invoked. | |
54 bool DidMessageConnection(ConnectionSpecificId connection_id) const { | |
55 return message_ids_.count(connection_id) > 0; | |
56 } | |
57 | |
58 private: | |
59 RootNodeManager* root_; | |
60 const ConnectionSpecificId connection_id_; | |
61 const bool is_delete_node_; | |
62 | |
63 // See description of MarkConnectionAsMessaged/DidMessageConnection. | |
64 std::set<ConnectionSpecificId> message_ids_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(ScopedChange); | |
67 }; | |
68 | |
69 RootNodeManager(ApplicationConnection* app_connection, | |
70 DisplayManagerDelegate* display_manager_delegate, | |
71 const Callback<void()>& native_viewport_closed_callback); | |
72 virtual ~RootNodeManager(); | |
73 | |
74 // Returns the id for the next ViewManagerServiceImpl. | |
75 ConnectionSpecificId GetAndAdvanceNextConnectionId(); | |
76 | |
77 void AddConnection(ViewManagerServiceImpl* connection); | |
78 void RemoveConnection(ViewManagerServiceImpl* connection); | |
79 | |
80 // Establishes the initial client. Similar to Connect(), but the resulting | |
81 // client is allowed to do anything. | |
82 void EmbedRoot(const std::string& url, | |
83 InterfaceRequest<ServiceProvider> service_provider); | |
84 | |
85 // See description of ViewManagerService::Embed() for details. This assumes | |
86 // |transport_node_id| is valid. | |
87 void Embed(ConnectionSpecificId creator_id, | |
88 const String& url, | |
89 Id transport_node_id, | |
90 InterfaceRequest<ServiceProvider> service_provider); | |
91 | |
92 // Returns the connection by id. | |
93 ViewManagerServiceImpl* GetConnection(ConnectionSpecificId connection_id); | |
94 | |
95 // Returns the Node identified by |id|. | |
96 Node* GetNode(const NodeId& id); | |
97 | |
98 Node* root() { return root_.get(); } | |
99 | |
100 bool IsProcessingChange() const { return current_change_ != NULL; } | |
101 | |
102 bool is_processing_delete_node() const { | |
103 return current_change_ && current_change_->is_delete_node(); } | |
104 | |
105 // Invoked when a connection messages a client about the change. This is used | |
106 // to avoid sending ServerChangeIdAdvanced() unnecessarily. | |
107 void OnConnectionMessagedClient(ConnectionSpecificId id); | |
108 | |
109 // Returns true if OnConnectionMessagedClient() was invoked for id. | |
110 bool DidConnectionMessageClient(ConnectionSpecificId id) const; | |
111 | |
112 ViewManagerServiceImpl* GetConnectionByCreator( | |
113 ConnectionSpecificId creator_id, | |
114 const std::string& url) const; | |
115 | |
116 // Returns the ViewManagerServiceImpl that has |id| as a root. | |
117 ViewManagerServiceImpl* GetConnectionWithRoot(const NodeId& id) { | |
118 return const_cast<ViewManagerServiceImpl*>( | |
119 const_cast<const RootNodeManager*>(this)->GetConnectionWithRoot(id)); | |
120 } | |
121 const ViewManagerServiceImpl* GetConnectionWithRoot(const NodeId& id) const; | |
122 | |
123 void DispatchNodeInputEventToWindowManager(EventPtr event); | |
124 | |
125 // These functions trivially delegate to all ViewManagerServiceImpls, which in | |
126 // term notify their clients. | |
127 void ProcessNodeDestroyed(Node* node); | |
128 void ProcessNodeBoundsChanged(const Node* node, | |
129 const gfx::Rect& old_bounds, | |
130 const gfx::Rect& new_bounds); | |
131 void ProcessNodeHierarchyChanged(const Node* node, | |
132 const Node* new_parent, | |
133 const Node* old_parent); | |
134 void ProcessNodeReorder(const Node* node, | |
135 const Node* relative_node, | |
136 const OrderDirection direction); | |
137 void ProcessNodeDeleted(const NodeId& node); | |
138 | |
139 private: | |
140 // Used to setup any static state needed by RootNodeManager. | |
141 struct Context { | |
142 Context(); | |
143 ~Context(); | |
144 }; | |
145 | |
146 typedef std::map<ConnectionSpecificId, ViewManagerServiceImpl*> ConnectionMap; | |
147 | |
148 // Invoked when a connection is about to make a change. Subsequently followed | |
149 // by FinishChange() once the change is done. | |
150 // | |
151 // Changes should never nest, meaning each PrepareForChange() must be | |
152 // balanced with a call to FinishChange() with no PrepareForChange() | |
153 // in between. | |
154 void PrepareForChange(ScopedChange* change); | |
155 | |
156 // Balances a call to PrepareForChange(). | |
157 void FinishChange(); | |
158 | |
159 // Returns true if the specified connection originated the current change. | |
160 bool IsChangeSource(ConnectionSpecificId connection_id) const { | |
161 return current_change_ && current_change_->connection_id() == connection_id; | |
162 } | |
163 | |
164 // Implementation of the two embed variants. | |
165 ViewManagerServiceImpl* EmbedImpl( | |
166 ConnectionSpecificId creator_id, | |
167 const String& url, | |
168 const NodeId& root_id, | |
169 InterfaceRequest<ServiceProvider> service_provider); | |
170 | |
171 // Overridden from NodeDelegate: | |
172 virtual void OnNodeDestroyed(const Node* node) OVERRIDE; | |
173 virtual void OnNodeHierarchyChanged(const Node* node, | |
174 const Node* new_parent, | |
175 const Node* old_parent) OVERRIDE; | |
176 virtual void OnNodeBoundsChanged(const Node* node, | |
177 const gfx::Rect& old_bounds, | |
178 const gfx::Rect& new_bounds) OVERRIDE; | |
179 virtual void OnNodeBitmapChanged(const Node* node) OVERRIDE; | |
180 | |
181 Context context_; | |
182 | |
183 ApplicationConnection* app_connection_; | |
184 | |
185 // ID to use for next ViewManagerServiceImpl. | |
186 ConnectionSpecificId next_connection_id_; | |
187 | |
188 // Set of ViewManagerServiceImpls. | |
189 ConnectionMap connection_map_; | |
190 | |
191 DisplayManager display_manager_; | |
192 | |
193 // Root node. | |
194 scoped_ptr<Node> root_; | |
195 | |
196 // Set of ViewManagerServiceImpls created by way of Connect(). These have to | |
197 // be explicitly destroyed. | |
198 std::set<ViewManagerServiceImpl*> connections_created_by_connect_; | |
199 | |
200 // If non-null we're processing a change. The ScopedChange is not owned by us | |
201 // (it's created on the stack by ViewManagerServiceImpl). | |
202 ScopedChange* current_change_; | |
203 | |
204 DISALLOW_COPY_AND_ASSIGN(RootNodeManager); | |
205 }; | |
206 | |
207 } // namespace service | |
208 } // namespace mojo | |
209 | |
210 #endif // MOJO_SERVICES_VIEW_MANAGER_ROOT_NODE_MANAGER_H_ | |
OLD | NEW |