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 COMPONENTS_MUS_CONNECTION_MANAGER_H_ | |
6 #define COMPONENTS_MUS_CONNECTION_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 #include <set> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/timer/timer.h" | |
14 #include "components/mus/focus_controller_delegate.h" | |
15 #include "components/mus/ids.h" | |
16 #include "components/mus/public/interfaces/view_tree.mojom.h" | |
17 #include "components/mus/public/interfaces/view_tree_host.mojom.h" | |
18 #include "components/mus/server_view_delegate.h" | |
19 #include "components/mus/server_view_observer.h" | |
20 #include "components/mus/surfaces/surfaces_state.h" | |
21 #include "components/mus/view_tree_host_impl.h" | |
22 #include "mojo/converters/surfaces/custom_surface_converter.h" | |
23 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h" | |
24 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" | |
25 | |
26 namespace mus { | |
27 | |
28 class ClientConnection; | |
29 class ConnectionManagerDelegate; | |
30 class ServerView; | |
31 class ViewTreeHostConnection; | |
32 class ViewTreeImpl; | |
33 | |
34 // ConnectionManager manages the set of connections to the ViewManager (all the | |
35 // ViewTreeImpl) as well as providing the root of the hierarchy. | |
36 class ConnectionManager : public ServerViewDelegate, | |
37 public ServerViewObserver, | |
38 public mojo::CustomSurfaceConverter { | |
39 public: | |
40 // Create when a ViewTreeImpl is about to make a change. Ensures clients are | |
41 // notified correctly. | |
42 class ScopedChange { | |
43 public: | |
44 ScopedChange(ViewTreeImpl* connection, | |
45 ConnectionManager* connection_manager, | |
46 bool is_delete_view); | |
47 ~ScopedChange(); | |
48 | |
49 ConnectionSpecificId connection_id() const { return connection_id_; } | |
50 bool is_delete_view() const { return is_delete_view_; } | |
51 | |
52 // Marks the connection with the specified id as having seen a message. | |
53 void MarkConnectionAsMessaged(ConnectionSpecificId connection_id) { | |
54 message_ids_.insert(connection_id); | |
55 } | |
56 | |
57 // Returns true if MarkConnectionAsMessaged(connection_id) was invoked. | |
58 bool DidMessageConnection(ConnectionSpecificId connection_id) const { | |
59 return message_ids_.count(connection_id) > 0; | |
60 } | |
61 | |
62 private: | |
63 ConnectionManager* connection_manager_; | |
64 const ConnectionSpecificId connection_id_; | |
65 const bool is_delete_view_; | |
66 | |
67 // See description of MarkConnectionAsMessaged/DidMessageConnection. | |
68 std::set<ConnectionSpecificId> message_ids_; | |
69 | |
70 DISALLOW_COPY_AND_ASSIGN(ScopedChange); | |
71 }; | |
72 | |
73 ConnectionManager(ConnectionManagerDelegate* delegate, | |
74 const scoped_refptr<SurfacesState>& surfaces_state); | |
75 ~ConnectionManager() override; | |
76 | |
77 // Adds a ViewTreeHost. | |
78 void AddHost(ViewTreeHostConnection* connection); | |
79 | |
80 // Creates a new ServerView. The return value is owned by the caller, but must | |
81 // be destroyed before ConnectionManager. | |
82 ServerView* CreateServerView(const ViewId& id); | |
83 | |
84 // Returns the id for the next ViewTreeImpl. | |
85 ConnectionSpecificId GetAndAdvanceNextConnectionId(); | |
86 | |
87 // Returns the id for the next ViewTreeHostImpl. | |
88 uint16_t GetAndAdvanceNextHostId(); | |
89 | |
90 // Invoked when a ViewTreeImpl's connection encounters an error. | |
91 void OnConnectionError(ClientConnection* connection); | |
92 | |
93 // Invoked when a ViewTreeHostBindingOwnerBase's connection encounters an | |
94 // error or the associated Display window is closed. | |
95 void OnHostConnectionClosed(ViewTreeHostConnection* connection); | |
96 | |
97 // See description of ViewTree::Embed() for details. This assumes | |
98 // |transport_view_id| is valid. | |
99 void EmbedAtView(ConnectionSpecificId creator_id, | |
100 const ViewId& view_id, | |
101 uint32_t policy_bitmask, | |
102 mojo::URLRequestPtr request); | |
103 ViewTreeImpl* EmbedAtView(ConnectionSpecificId creator_id, | |
104 const ViewId& view_id, | |
105 uint32_t policy_bitmask, | |
106 mojo::ViewTreeClientPtr client); | |
107 | |
108 // Returns the connection by id. | |
109 ViewTreeImpl* GetConnection(ConnectionSpecificId connection_id); | |
110 | |
111 // Returns the View identified by |id|. | |
112 ServerView* GetView(const ViewId& id); | |
113 | |
114 // Returns whether |view| is a descendant of some root view but not itself a | |
115 // root view. | |
116 bool IsViewAttachedToRoot(const ServerView* view) const; | |
117 | |
118 // Schedules a paint for the specified region in the coordinates of |view|. | |
119 void SchedulePaint(const ServerView* view, const gfx::Rect& bounds); | |
120 | |
121 bool IsProcessingChange() const { return current_change_ != NULL; } | |
122 | |
123 bool is_processing_delete_view() const { | |
124 return current_change_ && current_change_->is_delete_view(); | |
125 } | |
126 | |
127 // Invoked when the ViewTreeHostImpl's display is closed. | |
128 void OnDisplayClosed(); | |
129 | |
130 // Invoked when a connection messages a client about the change. This is used | |
131 // to avoid sending ServerChangeIdAdvanced() unnecessarily. | |
132 void OnConnectionMessagedClient(ConnectionSpecificId id); | |
133 | |
134 // Returns true if OnConnectionMessagedClient() was invoked for id. | |
135 bool DidConnectionMessageClient(ConnectionSpecificId id) const; | |
136 | |
137 // Returns the metrics of the viewport where the provided |view| is displayed. | |
138 mojo::ViewportMetricsPtr GetViewportMetricsForView(const ServerView* view); | |
139 | |
140 // Returns the ViewTreeImpl that has |id| as a root. | |
141 ViewTreeImpl* GetConnectionWithRoot(const ViewId& id) { | |
142 return const_cast<ViewTreeImpl*>( | |
143 const_cast<const ConnectionManager*>(this)->GetConnectionWithRoot(id)); | |
144 } | |
145 const ViewTreeImpl* GetConnectionWithRoot(const ViewId& id) const; | |
146 | |
147 ViewTreeHostImpl* GetViewTreeHostByView(const ServerView* view); | |
148 const ViewTreeHostImpl* GetViewTreeHostByView(const ServerView* view) const; | |
149 | |
150 // Returns the first ancestor of |service| that is marked as an embed root. | |
151 ViewTreeImpl* GetEmbedRoot(ViewTreeImpl* service); | |
152 | |
153 // ViewTreeHost implementation helper; see mojom for details. | |
154 bool CloneAndAnimate(const ViewId& view_id); | |
155 | |
156 // These functions trivially delegate to all ViewTreeImpls, which in | |
157 // term notify their clients. | |
158 void ProcessViewDestroyed(ServerView* view); | |
159 void ProcessViewBoundsChanged(const ServerView* view, | |
160 const gfx::Rect& old_bounds, | |
161 const gfx::Rect& new_bounds); | |
162 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics& old_metrics, | |
163 const mojo::ViewportMetrics& new_metrics); | |
164 void ProcessWillChangeViewHierarchy(const ServerView* view, | |
165 const ServerView* new_parent, | |
166 const ServerView* old_parent); | |
167 void ProcessViewHierarchyChanged(const ServerView* view, | |
168 const ServerView* new_parent, | |
169 const ServerView* old_parent); | |
170 void ProcessViewReorder(const ServerView* view, | |
171 const ServerView* relative_view, | |
172 const mojo::OrderDirection direction); | |
173 void ProcessViewDeleted(const ViewId& view); | |
174 | |
175 private: | |
176 using ConnectionMap = std::map<ConnectionSpecificId, ClientConnection*>; | |
177 using HostConnectionMap = | |
178 std::map<ViewTreeHostImpl*, ViewTreeHostConnection*>; | |
179 | |
180 // Invoked when a connection is about to make a change. Subsequently followed | |
181 // by FinishChange() once the change is done. | |
182 // | |
183 // Changes should never nest, meaning each PrepareForChange() must be | |
184 // balanced with a call to FinishChange() with no PrepareForChange() | |
185 // in between. | |
186 void PrepareForChange(ScopedChange* change); | |
187 | |
188 // Balances a call to PrepareForChange(). | |
189 void FinishChange(); | |
190 | |
191 // Returns true if the specified connection originated the current change. | |
192 bool IsChangeSource(ConnectionSpecificId connection_id) const { | |
193 return current_change_ && current_change_->connection_id() == connection_id; | |
194 } | |
195 | |
196 // Adds |connection| to internal maps. | |
197 void AddConnection(ClientConnection* connection); | |
198 | |
199 // Overridden from ServerViewDelegate: | |
200 scoped_ptr<cc::CompositorFrame> UpdateViewTreeFromCompositorFrame( | |
201 const mojo::CompositorFramePtr& input) override; | |
202 SurfacesState* GetSurfacesState() override; | |
203 void OnScheduleViewPaint(const ServerView* view) override; | |
204 const ServerView* GetRootView(const ServerView* view) const override; | |
205 | |
206 // Overridden from ServerViewObserver: | |
207 void OnViewDestroyed(ServerView* view) override; | |
208 void OnWillChangeViewHierarchy(ServerView* view, | |
209 ServerView* new_parent, | |
210 ServerView* old_parent) override; | |
211 void OnViewHierarchyChanged(ServerView* view, | |
212 ServerView* new_parent, | |
213 ServerView* old_parent) override; | |
214 void OnViewBoundsChanged(ServerView* view, | |
215 const gfx::Rect& old_bounds, | |
216 const gfx::Rect& new_bounds) override; | |
217 void OnViewReordered(ServerView* view, | |
218 ServerView* relative, | |
219 mojo::OrderDirection direction) override; | |
220 void OnWillChangeViewVisibility(ServerView* view) override; | |
221 void OnViewSharedPropertyChanged( | |
222 ServerView* view, | |
223 const std::string& name, | |
224 const std::vector<uint8_t>* new_data) override; | |
225 void OnViewTextInputStateChanged(ServerView* view, | |
226 const ui::TextInputState& state) override; | |
227 | |
228 // Overriden from CustomSurfaceConverter: | |
229 bool ConvertSurfaceDrawQuad(const mojo::QuadPtr& input, | |
230 const mojo::CompositorFrameMetadataPtr& metadata, | |
231 cc::SharedQuadState* sqs, | |
232 cc::RenderPass* render_pass) override; | |
233 | |
234 ConnectionManagerDelegate* delegate_; | |
235 | |
236 // State for rendering into a Surface. | |
237 scoped_refptr<SurfacesState> surfaces_state_; | |
238 | |
239 // ID to use for next ViewTreeImpl. | |
240 ConnectionSpecificId next_connection_id_; | |
241 | |
242 // ID to use for next ViewTreeHostImpl. | |
243 uint16_t next_host_id_; | |
244 | |
245 // Set of ViewTreeImpls. | |
246 ConnectionMap connection_map_; | |
247 | |
248 // Set of ViewTreeHostImpls. | |
249 HostConnectionMap host_connection_map_; | |
250 | |
251 // If non-null we're processing a change. The ScopedChange is not owned by us | |
252 // (it's created on the stack by ViewTreeImpl). | |
253 ScopedChange* current_change_; | |
254 | |
255 bool in_destructor_; | |
256 | |
257 DISALLOW_COPY_AND_ASSIGN(ConnectionManager); | |
258 }; | |
259 | |
260 } // namespace mus | |
261 | |
262 #endif // COMPONENTS_MUS_CONNECTION_MANAGER_H_ | |
OLD | NEW |