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