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_MUS_CONNECTION_MANAGER_H_
6 #define COMPONENTS_MUS_CONNECTION_MANAGER_H_
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"
28 class ClientConnection
;
29 class ConnectionManagerDelegate
;
31 class ViewTreeHostConnection
;
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
{
40 // Create when a ViewTreeImpl is about to make a change. Ensures clients are
41 // notified correctly.
44 ScopedChange(ViewTreeImpl
* connection
,
45 ConnectionManager
* connection_manager
,
49 ConnectionSpecificId
connection_id() const { return connection_id_
; }
50 bool is_delete_view() const { return is_delete_view_
; }
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
);
57 // Returns true if MarkConnectionAsMessaged(connection_id) was invoked.
58 bool DidMessageConnection(ConnectionSpecificId connection_id
) const {
59 return message_ids_
.count(connection_id
) > 0;
63 ConnectionManager
* connection_manager_
;
64 const ConnectionSpecificId connection_id_
;
65 const bool is_delete_view_
;
67 // See description of MarkConnectionAsMessaged/DidMessageConnection.
68 std::set
<ConnectionSpecificId
> message_ids_
;
70 DISALLOW_COPY_AND_ASSIGN(ScopedChange
);
73 ConnectionManager(ConnectionManagerDelegate
* delegate
,
74 const scoped_refptr
<SurfacesState
>& surfaces_state
);
75 ~ConnectionManager() override
;
77 // Adds a ViewTreeHost.
78 void AddHost(ViewTreeHostConnection
* connection
);
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
);
84 // Returns the id for the next ViewTreeImpl.
85 ConnectionSpecificId
GetAndAdvanceNextConnectionId();
87 // Returns the id for the next ViewTreeHostImpl.
88 uint16_t GetAndAdvanceNextHostId();
90 // Invoked when a ViewTreeImpl's connection encounters an error.
91 void OnConnectionError(ClientConnection
* connection
);
93 // Invoked when a ViewTreeHostBindingOwnerBase's connection encounters an
94 // error or the associated Display window is closed.
95 void OnHostConnectionClosed(ViewTreeHostConnection
* connection
);
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
);
108 // Returns the connection by id.
109 ViewTreeImpl
* GetConnection(ConnectionSpecificId connection_id
);
111 // Returns the View identified by |id|.
112 ServerView
* GetView(const ViewId
& id
);
114 // Returns whether |view| is a descendant of some root view but not itself a
116 bool IsViewAttachedToRoot(const ServerView
* view
) const;
118 // Schedules a paint for the specified region in the coordinates of |view|.
119 void SchedulePaint(const ServerView
* view
, const gfx::Rect
& bounds
);
121 bool IsProcessingChange() const { return current_change_
!= NULL
; }
123 bool is_processing_delete_view() const {
124 return current_change_
&& current_change_
->is_delete_view();
127 // Invoked when the ViewTreeHostImpl's display is closed.
128 void OnDisplayClosed();
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
);
134 // Returns true if OnConnectionMessagedClient() was invoked for id.
135 bool DidConnectionMessageClient(ConnectionSpecificId id
) const;
137 // Returns the metrics of the viewport where the provided |view| is displayed.
138 mojo::ViewportMetricsPtr
GetViewportMetricsForView(const ServerView
* view
);
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
));
145 const ViewTreeImpl
* GetConnectionWithRoot(const ViewId
& id
) const;
147 ViewTreeHostImpl
* GetViewTreeHostByView(const ServerView
* view
);
148 const ViewTreeHostImpl
* GetViewTreeHostByView(const ServerView
* view
) const;
150 // Returns the first ancestor of |service| that is marked as an embed root.
151 ViewTreeImpl
* GetEmbedRoot(ViewTreeImpl
* service
);
153 // ViewTreeHost implementation helper; see mojom for details.
154 bool CloneAndAnimate(const ViewId
& view_id
);
156 // Dispatches |event| directly to the appropriate connection for |view|.
157 void DispatchInputEventToView(const ServerView
* view
, mojo::EventPtr event
);
159 // These functions trivially delegate to all ViewTreeImpls, which in
160 // term notify their clients.
161 void ProcessViewDestroyed(ServerView
* view
);
162 void ProcessViewBoundsChanged(const ServerView
* view
,
163 const gfx::Rect
& old_bounds
,
164 const gfx::Rect
& new_bounds
);
165 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics
& old_metrics
,
166 const mojo::ViewportMetrics
& new_metrics
);
167 void ProcessWillChangeViewHierarchy(const ServerView
* view
,
168 const ServerView
* new_parent
,
169 const ServerView
* old_parent
);
170 void ProcessViewHierarchyChanged(const ServerView
* view
,
171 const ServerView
* new_parent
,
172 const ServerView
* old_parent
);
173 void ProcessViewReorder(const ServerView
* view
,
174 const ServerView
* relative_view
,
175 const mojo::OrderDirection direction
);
176 void ProcessViewDeleted(const ViewId
& view
);
179 using ConnectionMap
= std::map
<ConnectionSpecificId
, ClientConnection
*>;
180 using HostConnectionMap
=
181 std::map
<ViewTreeHostImpl
*, ViewTreeHostConnection
*>;
183 // Invoked when a connection is about to make a change. Subsequently followed
184 // by FinishChange() once the change is done.
186 // Changes should never nest, meaning each PrepareForChange() must be
187 // balanced with a call to FinishChange() with no PrepareForChange()
189 void PrepareForChange(ScopedChange
* change
);
191 // Balances a call to PrepareForChange().
194 // Returns true if the specified connection originated the current change.
195 bool IsChangeSource(ConnectionSpecificId connection_id
) const {
196 return current_change_
&& current_change_
->connection_id() == connection_id
;
199 // Adds |connection| to internal maps.
200 void AddConnection(ClientConnection
* connection
);
202 // Overridden from ServerViewDelegate:
203 scoped_ptr
<cc::CompositorFrame
> UpdateViewTreeFromCompositorFrame(
204 const mojo::CompositorFramePtr
& input
) override
;
205 SurfacesState
* GetSurfacesState() override
;
206 void OnScheduleViewPaint(const ServerView
* view
) override
;
207 const ServerView
* GetRootView(const ServerView
* view
) const override
;
209 // Overridden from ServerViewObserver:
210 void OnViewDestroyed(ServerView
* view
) override
;
211 void OnWillChangeViewHierarchy(ServerView
* view
,
212 ServerView
* new_parent
,
213 ServerView
* old_parent
) override
;
214 void OnViewHierarchyChanged(ServerView
* view
,
215 ServerView
* new_parent
,
216 ServerView
* old_parent
) override
;
217 void OnViewBoundsChanged(ServerView
* view
,
218 const gfx::Rect
& old_bounds
,
219 const gfx::Rect
& new_bounds
) override
;
220 void OnViewReordered(ServerView
* view
,
221 ServerView
* relative
,
222 mojo::OrderDirection direction
) override
;
223 void OnWillChangeViewVisibility(ServerView
* view
) override
;
224 void OnViewSharedPropertyChanged(
226 const std::string
& name
,
227 const std::vector
<uint8_t>* new_data
) override
;
228 void OnViewTextInputStateChanged(ServerView
* view
,
229 const ui::TextInputState
& state
) override
;
231 // Overriden from CustomSurfaceConverter:
232 bool ConvertSurfaceDrawQuad(const mojo::QuadPtr
& input
,
233 const mojo::CompositorFrameMetadataPtr
& metadata
,
234 cc::SharedQuadState
* sqs
,
235 cc::RenderPass
* render_pass
) override
;
237 ConnectionManagerDelegate
* delegate_
;
239 // State for rendering into a Surface.
240 scoped_refptr
<SurfacesState
> surfaces_state_
;
242 // ID to use for next ViewTreeImpl.
243 ConnectionSpecificId next_connection_id_
;
245 // ID to use for next ViewTreeHostImpl.
246 uint16_t next_host_id_
;
248 // Set of ViewTreeImpls.
249 ConnectionMap connection_map_
;
251 // Set of ViewTreeHostImpls.
252 HostConnectionMap host_connection_map_
;
254 // If non-null we're processing a change. The ScopedChange is not owned by us
255 // (it's created on the stack by ViewTreeImpl).
256 ScopedChange
* current_change_
;
260 DISALLOW_COPY_AND_ASSIGN(ConnectionManager
);
265 #endif // COMPONENTS_MUS_CONNECTION_MANAGER_H_