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/native_viewport/public/interfaces/native_viewport.mojom.h"
15 #include "components/view_manager/animation_runner.h"
16 #include "components/view_manager/event_dispatcher.h"
17 #include "components/view_manager/focus_controller_delegate.h"
18 #include "components/view_manager/ids.h"
19 #include "components/view_manager/public/interfaces/view_manager.mojom.h"
20 #include "components/view_manager/server_view_delegate.h"
21 #include "components/view_manager/server_view_observer.h"
22 #include "components/window_manager/public/interfaces/window_manager_internal.mojom.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"
26 namespace view_manager
{
28 class ClientConnection
;
29 class ConnectionManagerDelegate
;
31 class FocusController
;
33 class ViewManagerServiceImpl
;
35 // ConnectionManager manages the set of connections to the ViewManager (all the
36 // ViewManagerServiceImpls) as well as providing the root of the hierarchy.
37 class ConnectionManager
: public ServerViewDelegate
,
38 public ServerViewObserver
,
39 public mojo::WindowManagerInternalClient
,
40 public FocusControllerDelegate
{
42 // Create when a ViewManagerServiceImpl is about to make a change. Ensures
43 // clients are notified correctly.
46 ScopedChange(ViewManagerServiceImpl
* connection
,
47 ConnectionManager
* connection_manager
,
51 mojo::ConnectionSpecificId
connection_id() const { return connection_id_
; }
52 bool is_delete_view() const { return is_delete_view_
; }
54 // Marks the connection with the specified id as having seen a message.
55 void MarkConnectionAsMessaged(mojo::ConnectionSpecificId connection_id
) {
56 message_ids_
.insert(connection_id
);
59 // Returns true if MarkConnectionAsMessaged(connection_id) was invoked.
60 bool DidMessageConnection(mojo::ConnectionSpecificId connection_id
) const {
61 return message_ids_
.count(connection_id
) > 0;
65 ConnectionManager
* connection_manager_
;
66 const mojo::ConnectionSpecificId connection_id_
;
67 const bool is_delete_view_
;
69 // See description of MarkConnectionAsMessaged/DidMessageConnection.
70 std::set
<mojo::ConnectionSpecificId
> message_ids_
;
72 DISALLOW_COPY_AND_ASSIGN(ScopedChange
);
75 ConnectionManager(ConnectionManagerDelegate
* delegate
,
76 scoped_ptr
<DisplayManager
> display_manager
,
77 mojo::WindowManagerInternal
* wm_internal
);
78 ~ConnectionManager() override
;
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 ViewManagerServiceImpl.
85 mojo::ConnectionSpecificId
GetAndAdvanceNextConnectionId();
87 // Invoked when a ViewManagerServiceImpl's connection encounters an error.
88 void OnConnectionError(ClientConnection
* connection
);
90 // See description of ViewManagerService::Embed() for details. This assumes
91 // |transport_view_id| is valid.
92 void EmbedAtView(mojo::ConnectionSpecificId creator_id
,
93 const std::string
& url
,
94 const ViewId
& view_id
,
95 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
96 mojo::ServiceProviderPtr exposed_services
);
97 void EmbedAtView(mojo::ConnectionSpecificId creator_id
,
98 const ViewId
& view_id
,
99 mojo::ViewManagerClientPtr client
);
101 // Returns the connection by id.
102 ViewManagerServiceImpl
* GetConnection(
103 mojo::ConnectionSpecificId connection_id
);
105 // Returns the View identified by |id|.
106 ServerView
* GetView(const ViewId
& id
);
108 void SetFocusedView(ServerView
* view
);
109 ServerView
* GetFocusedView();
111 ServerView
* root() { return root_
.get(); }
112 DisplayManager
* display_manager() { return display_manager_
.get(); }
114 bool IsProcessingChange() const { return current_change_
!= NULL
; }
116 bool is_processing_delete_view() const {
117 return current_change_
&& current_change_
->is_delete_view();
120 // Invoked when a connection messages a client about the change. This is used
121 // to avoid sending ServerChangeIdAdvanced() unnecessarily.
122 void OnConnectionMessagedClient(mojo::ConnectionSpecificId id
);
124 // Returns true if OnConnectionMessagedClient() was invoked for id.
125 bool DidConnectionMessageClient(mojo::ConnectionSpecificId id
) const;
127 // Returns the ViewManagerServiceImpl that has |id| as a root.
128 ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) {
129 return const_cast<ViewManagerServiceImpl
*>(
130 const_cast<const ConnectionManager
*>(this)->GetConnectionWithRoot(id
));
132 const ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) const;
134 mojo::WindowManagerInternal
* wm_internal() { return wm_internal_
; }
136 void SetWindowManagerClientConnection(
137 scoped_ptr
<ClientConnection
> connection
);
138 bool has_window_manager_client_connection() const {
139 return window_manager_client_connection_
!= nullptr;
142 mojo::ViewManagerClient
* GetWindowManagerViewManagerClient();
144 // WindowManagerInternalClient implementation helper; see mojom for details.
145 bool CloneAndAnimate(const ViewId
& view_id
);
147 // Processes an event, potentially changing focus.
148 void ProcessEvent(mojo::EventPtr event
);
150 // Dispatches |event| directly to the appropriate connection for |view|.
151 void DispatchInputEventToView(const ServerView
* view
, mojo::EventPtr event
);
153 // These functions trivially delegate to all ViewManagerServiceImpls, which in
154 // term notify their clients.
155 void ProcessViewDestroyed(ServerView
* view
);
156 void ProcessViewBoundsChanged(const ServerView
* view
,
157 const gfx::Rect
& old_bounds
,
158 const gfx::Rect
& new_bounds
);
159 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics
& old_metrics
,
160 const mojo::ViewportMetrics
& new_metrics
);
161 void ProcessWillChangeViewHierarchy(const ServerView
* view
,
162 const ServerView
* new_parent
,
163 const ServerView
* old_parent
);
164 void ProcessViewHierarchyChanged(const ServerView
* view
,
165 const ServerView
* new_parent
,
166 const ServerView
* old_parent
);
167 void ProcessViewReorder(const ServerView
* view
,
168 const ServerView
* relative_view
,
169 const mojo::OrderDirection direction
);
170 void ProcessViewDeleted(const ViewId
& view
);
173 typedef std::map
<mojo::ConnectionSpecificId
, ClientConnection
*> ConnectionMap
;
175 // Invoked when a connection is about to make a change. Subsequently followed
176 // by FinishChange() once the change is done.
178 // Changes should never nest, meaning each PrepareForChange() must be
179 // balanced with a call to FinishChange() with no PrepareForChange()
181 void PrepareForChange(ScopedChange
* change
);
183 // Balances a call to PrepareForChange().
186 // Returns true if the specified connection originated the current change.
187 bool IsChangeSource(mojo::ConnectionSpecificId connection_id
) const {
188 return current_change_
&& current_change_
->connection_id() == connection_id
;
191 // Adds |connection| to internal maps.
192 void AddConnection(ClientConnection
* connection
);
194 // Callback from animation timer.
195 // TODO(sky): make this real (move to a different class).
198 // Overridden from ServerViewDelegate:
199 void PrepareToDestroyView(ServerView
* view
) override
;
200 void PrepareToChangeViewHierarchy(ServerView
* view
,
201 ServerView
* new_parent
,
202 ServerView
* old_parent
) override
;
203 void PrepareToChangeViewVisibility(ServerView
* view
) override
;
204 void OnScheduleViewPaint(const ServerView
* view
) override
;
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(
223 const std::string
& name
,
224 const std::vector
<uint8_t>* new_data
) override
;
226 // WindowManagerInternalClient:
227 void DispatchInputEventToViewDEPRECATED(mojo::Id transport_view_id
,
228 mojo::EventPtr event
) override
;
229 void SetViewportSize(mojo::SizePtr size
) override
;
230 void CloneAndAnimate(mojo::Id transport_view_id
) override
;
231 void AddAccelerator(mojo::KeyboardCode keyboard_code
,
232 mojo::EventFlags flags
) override
;
233 void RemoveAccelerator(mojo::KeyboardCode keyboard_code
,
234 mojo::EventFlags flags
) override
;
236 // FocusControllerDelegate:
237 void OnFocusChanged(ServerView
* old_focused_view
,
238 ServerView
* new_focused_view
) override
;
240 ConnectionManagerDelegate
* delegate_
;
242 // The ClientConnection containing the ViewManagerService implementation
243 // provided to the initial connection (the WindowManager).
244 // NOTE: |window_manager_client_connection_| is also in |connection_map_|.
245 ClientConnection
* window_manager_client_connection_
;
247 // ID to use for next ViewManagerServiceImpl.
248 mojo::ConnectionSpecificId next_connection_id_
;
250 // Set of ViewManagerServiceImpls.
251 ConnectionMap connection_map_
;
253 scoped_ptr
<DisplayManager
> display_manager_
;
255 scoped_ptr
<ServerView
> root_
;
257 mojo::WindowManagerInternal
* wm_internal_
;
259 // If non-null we're processing a change. The ScopedChange is not owned by us
260 // (it's created on the stack by ViewManagerServiceImpl).
261 ScopedChange
* current_change_
;
265 // TODO(sky): nuke! Just a proof of concept until get real animation api.
266 base::RepeatingTimer
<ConnectionManager
> animation_timer_
;
268 AnimationRunner animation_runner_
;
270 EventDispatcher event_dispatcher_
;
272 mojo::Binding
<mojo::NativeViewportEventDispatcher
> event_dispatcher_binding_
;
274 scoped_ptr
<FocusController
> focus_controller_
;
276 DISALLOW_COPY_AND_ASSIGN(ConnectionManager
);
279 } // namespace view_manager
281 #endif // COMPONENTS_VIEW_MANAGER_CONNECTION_MANAGER_H_