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 SERVICES_VIEW_MANAGER_CONNECTION_MANAGER_H_
6 #define SERVICES_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 "mojo/public/cpp/bindings/array.h"
15 #include "mojo/services/view_manager/animation_runner.h"
16 #include "mojo/services/view_manager/ids.h"
17 #include "mojo/services/view_manager/server_view_delegate.h"
18 #include "third_party/mojo_services/src/view_manager/public/interfaces/view_manager.mojom.h"
19 #include "third_party/mojo_services/src/window_manager/public/interfaces/window_manager_internal.mojom.h"
21 namespace view_manager
{
23 class ClientConnection
;
24 class ConnectionManagerDelegate
;
27 class ViewManagerServiceImpl
;
29 // ConnectionManager manages the set of connections to the ViewManager (all the
30 // ViewManagerServiceImpls) as well as providing the root of the hierarchy.
31 class ConnectionManager
: public ServerViewDelegate
,
32 public mojo::WindowManagerInternalClient
{
34 // Create when a ViewManagerServiceImpl is about to make a change. Ensures
35 // clients are notified correctly.
38 ScopedChange(ViewManagerServiceImpl
* connection
,
39 ConnectionManager
* connection_manager
,
43 mojo::ConnectionSpecificId
connection_id() const { return connection_id_
; }
44 bool is_delete_view() const { return is_delete_view_
; }
46 // Marks the connection with the specified id as having seen a message.
47 void MarkConnectionAsMessaged(mojo::ConnectionSpecificId connection_id
) {
48 message_ids_
.insert(connection_id
);
51 // Returns true if MarkConnectionAsMessaged(connection_id) was invoked.
52 bool DidMessageConnection(mojo::ConnectionSpecificId connection_id
) const {
53 return message_ids_
.count(connection_id
) > 0;
57 ConnectionManager
* connection_manager_
;
58 const mojo::ConnectionSpecificId connection_id_
;
59 const bool is_delete_view_
;
61 // See description of MarkConnectionAsMessaged/DidMessageConnection.
62 std::set
<mojo::ConnectionSpecificId
> message_ids_
;
64 DISALLOW_COPY_AND_ASSIGN(ScopedChange
);
67 ConnectionManager(ConnectionManagerDelegate
* delegate
,
68 scoped_ptr
<DisplayManager
> display_manager
,
69 mojo::WindowManagerInternal
* wm_internal
);
70 ~ConnectionManager() override
;
72 // Returns the id for the next ViewManagerServiceImpl.
73 mojo::ConnectionSpecificId
GetAndAdvanceNextConnectionId();
75 // Invoked when a ViewManagerServiceImpl's connection encounters an error.
76 void OnConnectionError(ClientConnection
* connection
);
78 // See description of ViewManagerService::Embed() for details. This assumes
79 // |transport_view_id| is valid.
80 void EmbedAtView(mojo::ConnectionSpecificId creator_id
,
81 const std::string
& url
,
82 const ViewId
& view_id
,
83 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
84 mojo::ServiceProviderPtr exposed_services
);
85 void EmbedAtView(mojo::ConnectionSpecificId creator_id
,
86 const ViewId
& view_id
,
87 mojo::ViewManagerClientPtr client
);
89 // Returns the connection by id.
90 ViewManagerServiceImpl
* GetConnection(
91 mojo::ConnectionSpecificId connection_id
);
93 // Returns the View identified by |id|.
94 ServerView
* GetView(const ViewId
& id
);
96 ServerView
* root() { return root_
.get(); }
97 DisplayManager
* display_manager() { return display_manager_
.get(); }
99 bool IsProcessingChange() const { return current_change_
!= NULL
; }
101 bool is_processing_delete_view() const {
102 return current_change_
&& current_change_
->is_delete_view();
105 // Invoked when a connection messages a client about the change. This is used
106 // to avoid sending ServerChangeIdAdvanced() unnecessarily.
107 void OnConnectionMessagedClient(mojo::ConnectionSpecificId id
);
109 // Returns true if OnConnectionMessagedClient() was invoked for id.
110 bool DidConnectionMessageClient(mojo::ConnectionSpecificId id
) const;
112 // Returns the ViewManagerServiceImpl that has |id| as a root.
113 ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) {
114 return const_cast<ViewManagerServiceImpl
*>(
115 const_cast<const ConnectionManager
*>(this)->GetConnectionWithRoot(id
));
117 const ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) const;
119 mojo::WindowManagerInternal
* wm_internal() { return wm_internal_
; }
121 void SetWindowManagerClientConnection(
122 scoped_ptr
<ClientConnection
> connection
);
123 bool has_window_manager_client_connection() const {
124 return window_manager_client_connection_
!= nullptr;
127 mojo::ViewManagerClient
* GetWindowManagerViewManagerClient();
129 // WindowManagerInternalClient implementation helper; see mojom for details.
130 bool CloneAndAnimate(const ViewId
& view_id
);
132 // These functions trivially delegate to all ViewManagerServiceImpls, which in
133 // term notify their clients.
134 void ProcessViewDestroyed(ServerView
* view
);
135 void ProcessViewBoundsChanged(const ServerView
* view
,
136 const gfx::Rect
& old_bounds
,
137 const gfx::Rect
& new_bounds
);
138 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics
& old_metrics
,
139 const mojo::ViewportMetrics
& new_metrics
);
140 void ProcessWillChangeViewHierarchy(const ServerView
* view
,
141 const ServerView
* new_parent
,
142 const ServerView
* old_parent
);
143 void ProcessViewHierarchyChanged(const ServerView
* view
,
144 const ServerView
* new_parent
,
145 const ServerView
* old_parent
);
146 void ProcessViewReorder(const ServerView
* view
,
147 const ServerView
* relative_view
,
148 const mojo::OrderDirection direction
);
149 void ProcessViewDeleted(const ViewId
& view
);
152 typedef std::map
<mojo::ConnectionSpecificId
, ClientConnection
*> ConnectionMap
;
154 // Invoked when a connection is about to make a change. Subsequently followed
155 // by FinishChange() once the change is done.
157 // Changes should never nest, meaning each PrepareForChange() must be
158 // balanced with a call to FinishChange() with no PrepareForChange()
160 void PrepareForChange(ScopedChange
* change
);
162 // Balances a call to PrepareForChange().
165 // Returns true if the specified connection originated the current change.
166 bool IsChangeSource(mojo::ConnectionSpecificId connection_id
) const {
167 return current_change_
&& current_change_
->connection_id() == connection_id
;
170 // Adds |connection| to internal maps.
171 void AddConnection(ClientConnection
* connection
);
173 // Callback from animation timer.
174 // TODO(sky): make this real (move to a different class).
177 // Overridden from ServerViewDelegate:
178 void OnWillDestroyView(ServerView
* view
) override
;
179 void OnViewDestroyed(const ServerView
* view
) override
;
180 void OnWillChangeViewHierarchy(ServerView
* view
,
181 ServerView
* new_parent
,
182 ServerView
* old_parent
) override
;
183 void OnViewHierarchyChanged(const ServerView
* view
,
184 const ServerView
* new_parent
,
185 const ServerView
* old_parent
) override
;
186 void OnViewBoundsChanged(const ServerView
* view
,
187 const gfx::Rect
& old_bounds
,
188 const gfx::Rect
& new_bounds
) override
;
189 void OnViewSurfaceIdChanged(const ServerView
* view
) override
;
190 void OnViewReordered(const ServerView
* view
,
191 const ServerView
* relative
,
192 mojo::OrderDirection direction
) override
;
193 void OnWillChangeViewVisibility(ServerView
* view
) override
;
194 void OnViewSharedPropertyChanged(
195 const ServerView
* view
,
196 const std::string
& name
,
197 const std::vector
<uint8_t>* new_data
) override
;
198 void OnScheduleViewPaint(const ServerView
* view
) override
;
200 // WindowManagerInternalClient:
201 void DispatchInputEventToView(mojo::Id transport_view_id
,
202 mojo::EventPtr event
) override
;
203 void SetViewportSize(mojo::SizePtr size
) override
;
204 void CloneAndAnimate(mojo::Id transport_view_id
) override
;
206 ConnectionManagerDelegate
* delegate_
;
208 // The ClientConnection containing the ViewManagerService implementation
209 // provided to the initial connection (the WindowManager).
210 // NOTE: |window_manager_client_connection_| is also in |connection_map_|.
211 ClientConnection
* window_manager_client_connection_
;
213 // ID to use for next ViewManagerServiceImpl.
214 mojo::ConnectionSpecificId next_connection_id_
;
216 // Set of ViewManagerServiceImpls.
217 ConnectionMap connection_map_
;
219 scoped_ptr
<DisplayManager
> display_manager_
;
221 scoped_ptr
<ServerView
> root_
;
223 mojo::WindowManagerInternal
* wm_internal_
;
225 // If non-null we're processing a change. The ScopedChange is not owned by us
226 // (it's created on the stack by ViewManagerServiceImpl).
227 ScopedChange
* current_change_
;
231 // TODO(sky): nuke! Just a proof of concept until get real animation api.
232 base::RepeatingTimer
<ConnectionManager
> animation_timer_
;
234 AnimationRunner animation_runner_
;
236 DISALLOW_COPY_AND_ASSIGN(ConnectionManager
);
239 } // namespace view_manager
241 #endif // SERVICES_VIEW_MANAGER_CONNECTION_MANAGER_H_