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/animation_runner.h"
15 #include "components/view_manager/display_manager.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/public/interfaces/view_manager_root.mojom.h"
21 #include "components/view_manager/server_view_delegate.h"
22 #include "components/view_manager/server_view_observer.h"
23 #include "components/view_manager/view_manager_root_impl.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
;
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 FocusControllerDelegate
{
41 // Create when a ViewManagerServiceImpl is about to make a change. Ensures
42 // clients are notified correctly.
45 ScopedChange(ViewManagerServiceImpl
* 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
);
74 ConnectionManager(ConnectionManagerDelegate
* delegate
,
76 mojo::ApplicationImpl
* app_impl
,
77 const scoped_refptr
<gles2::GpuState
>& gpu_state
);
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 ViewId
& view_id
,
94 mojo::URLRequestPtr request
);
95 void EmbedAtView(mojo::ConnectionSpecificId creator_id
,
96 const ViewId
& view_id
,
97 mojo::ViewManagerClientPtr client
);
99 // Invoked when an accelerator has been triggered on a view tree with the
101 void OnAccelerator(ServerView
* root
, mojo::EventPtr event
);
103 // Returns the connection by id.
104 ViewManagerServiceImpl
* GetConnection(
105 mojo::ConnectionSpecificId connection_id
);
107 // Returns the View identified by |id|.
108 ServerView
* GetView(const ViewId
& id
);
110 void SetFocusedView(ServerView
* view
);
111 ServerView
* GetFocusedView();
113 // Returns whether |view| is a descendant of some root view but not itself a
115 bool IsViewAttachedToRoot(const ServerView
* view
) const;
117 // Schedules a paint for the specified region in the coordinates of |view|.
118 void SchedulePaint(const ServerView
* view
, const gfx::Rect
& bounds
);
120 bool IsProcessingChange() const { return current_change_
!= NULL
; }
122 bool is_processing_delete_view() const {
123 return current_change_
&& current_change_
->is_delete_view();
126 ViewManagerRootImpl
* view_manager_root() { return view_manager_root_
.get(); }
128 // Invoked when the ViewManagerRootImpl's display is closed.
129 void OnDisplayClosed();
131 // Invoked when a connection messages a client about the change. This is used
132 // to avoid sending ServerChangeIdAdvanced() unnecessarily.
133 void OnConnectionMessagedClient(mojo::ConnectionSpecificId id
);
135 // Returns true if OnConnectionMessagedClient() was invoked for id.
136 bool DidConnectionMessageClient(mojo::ConnectionSpecificId id
) const;
138 // Returns the metrics of the viewport where the provided |view| is displayed.
139 mojo::ViewportMetricsPtr
GetViewportMetricsForView(const ServerView
* view
);
141 // Returns the ViewManagerServiceImpl that has |id| as a root.
142 ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) {
143 return const_cast<ViewManagerServiceImpl
*>(
144 const_cast<const ConnectionManager
*>(this)->GetConnectionWithRoot(id
));
146 const ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) const;
148 // Returns the first ancestor of |service| that is marked as an embed root.
149 ViewManagerServiceImpl
* GetEmbedRoot(ViewManagerServiceImpl
* service
);
151 void SetWindowManagerClientConnection(
152 scoped_ptr
<ClientConnection
> connection
);
153 bool has_window_manager_client_connection() const {
154 return window_manager_client_connection_
!= nullptr;
157 mojo::ViewManagerClient
* GetWindowManagerViewManagerClient();
159 // ViewManagerRoot implementation helper; see mojom for details.
160 bool CloneAndAnimate(const ViewId
& view_id
);
162 // Dispatches |event| directly to the appropriate connection for |view|.
163 void DispatchInputEventToView(const ServerView
* view
, mojo::EventPtr event
);
165 void OnEvent(ViewManagerRootImpl
* root
, mojo::EventPtr event
);
167 void AddAccelerator(ViewManagerRootImpl
* root
,
168 mojo::KeyboardCode keyboard_code
,
169 mojo::EventFlags flags
);
171 void RemoveAccelerator(ViewManagerRootImpl
* root
,
172 mojo::KeyboardCode keyboard_code
,
173 mojo::EventFlags flags
);
175 // These functions trivially delegate to all ViewManagerServiceImpls, which in
176 // term notify their clients.
177 void ProcessViewDestroyed(ServerView
* view
);
178 void ProcessViewBoundsChanged(const ServerView
* view
,
179 const gfx::Rect
& old_bounds
,
180 const gfx::Rect
& new_bounds
);
181 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics
& old_metrics
,
182 const mojo::ViewportMetrics
& new_metrics
);
183 void ProcessWillChangeViewHierarchy(const ServerView
* view
,
184 const ServerView
* new_parent
,
185 const ServerView
* old_parent
);
186 void ProcessViewHierarchyChanged(const ServerView
* view
,
187 const ServerView
* new_parent
,
188 const ServerView
* old_parent
);
189 void ProcessViewReorder(const ServerView
* view
,
190 const ServerView
* relative_view
,
191 const mojo::OrderDirection direction
);
192 void ProcessViewDeleted(const ViewId
& view
);
195 using ConnectionMap
= std::map
<mojo::ConnectionSpecificId
, ClientConnection
*>;
197 // Invoked when a connection is about to make a change. Subsequently followed
198 // by FinishChange() once the change is done.
200 // Changes should never nest, meaning each PrepareForChange() must be
201 // balanced with a call to FinishChange() with no PrepareForChange()
203 void PrepareForChange(ScopedChange
* change
);
205 // Balances a call to PrepareForChange().
208 // Returns true if the specified connection originated the current change.
209 bool IsChangeSource(mojo::ConnectionSpecificId connection_id
) const {
210 return current_change_
&& current_change_
->connection_id() == connection_id
;
213 // Callback from animation timer.
214 // TODO(sky): make this real (move to a different class).
217 // Adds |connection| to internal maps.
218 void AddConnection(ClientConnection
* connection
);
220 // Overridden from ServerViewDelegate:
221 void PrepareToDestroyView(ServerView
* view
) override
;
222 void PrepareToChangeViewHierarchy(ServerView
* view
,
223 ServerView
* new_parent
,
224 ServerView
* old_parent
) override
;
225 void PrepareToChangeViewVisibility(ServerView
* view
) override
;
226 void OnScheduleViewPaint(const ServerView
* view
) override
;
227 const ServerView
* GetRootView(const ServerView
* view
) const override
;
229 // Overridden from ServerViewObserver:
230 void OnViewDestroyed(ServerView
* view
) override
;
231 void OnWillChangeViewHierarchy(ServerView
* view
,
232 ServerView
* new_parent
,
233 ServerView
* old_parent
) override
;
234 void OnViewHierarchyChanged(ServerView
* view
,
235 ServerView
* new_parent
,
236 ServerView
* old_parent
) override
;
237 void OnViewBoundsChanged(ServerView
* view
,
238 const gfx::Rect
& old_bounds
,
239 const gfx::Rect
& new_bounds
) override
;
240 void OnViewReordered(ServerView
* view
,
241 ServerView
* relative
,
242 mojo::OrderDirection direction
) override
;
243 void OnWillChangeViewVisibility(ServerView
* view
) override
;
244 void OnViewSharedPropertyChanged(
246 const std::string
& name
,
247 const std::vector
<uint8_t>* new_data
) override
;
249 void CloneAndAnimate(mojo::Id transport_view_id
);
251 // FocusControllerDelegate:
252 void OnFocusChanged(ServerView
* old_focused_view
,
253 ServerView
* new_focused_view
) override
;
255 ConnectionManagerDelegate
* delegate_
;
257 // The ClientConnection containing the ViewManagerService implementation
258 // provided to the initial connection (the WindowManager).
259 // NOTE: |window_manager_client_connection_| is also in |connection_map_|.
260 ClientConnection
* window_manager_client_connection_
;
262 // ID to use for next ViewManagerServiceImpl.
263 mojo::ConnectionSpecificId next_connection_id_
;
265 // Set of ViewManagerServiceImpls.
266 ConnectionMap connection_map_
;
268 // ID to use for next ViewManagerRootImpl.
269 uint16_t next_root_id_
;
271 EventDispatcher event_dispatcher_
;
273 // If non-null we're processing a change. The ScopedChange is not owned by us
274 // (it's created on the stack by ViewManagerServiceImpl).
275 ScopedChange
* current_change_
;
279 scoped_ptr
<ViewManagerRootImpl
> view_manager_root_
;
281 // TODO(sky): nuke! Just a proof of concept until get real animation api.
282 base::RepeatingTimer
<ConnectionManager
> animation_timer_
;
284 AnimationRunner animation_runner_
;
286 scoped_ptr
<FocusController
> focus_controller_
;
288 DISALLOW_COPY_AND_ASSIGN(ConnectionManager
);
291 } // namespace view_manager
293 #endif // COMPONENTS_VIEW_MANAGER_CONNECTION_MANAGER_H_