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/event_dispatcher.h"
16 #include "components/view_manager/focus_controller_delegate.h"
17 #include "components/view_manager/ids.h"
18 #include "components/view_manager/public/interfaces/view_manager.mojom.h"
19 #include "components/view_manager/public/interfaces/view_manager_root.mojom.h"
20 #include "components/view_manager/server_view_delegate.h"
21 #include "components/view_manager/server_view_observer.h"
22 #include "components/view_manager/view_manager_root_impl.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
;
30 class FocusController
;
32 class ViewManagerRootConnection
;
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 explicit ConnectionManager(ConnectionManagerDelegate
* delegate
);
75 ~ConnectionManager() override
;
77 // Adds a ViewManagerRoot.
78 void AddRoot(ViewManagerRootConnection
* root_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 ViewManagerServiceImpl.
85 mojo::ConnectionSpecificId
GetAndAdvanceNextConnectionId();
87 // Returns the id for the next ViewManagerRootImpl.
88 uint16_t GetAndAdvanceNextRootId();
90 // Invoked when a ViewManagerServiceImpl's connection encounters an error.
91 void OnConnectionError(ClientConnection
* connection
);
93 // Invoked when a ViewManagerRootBindingOwnerBase's connection encounters an
94 // error or the associated Display window is closed.
95 void OnRootConnectionClosed(ViewManagerRootConnection
* connection
);
97 // See description of ViewManagerService::Embed() for details. This assumes
98 // |transport_view_id| is valid.
99 void EmbedAtView(mojo::ConnectionSpecificId creator_id
,
100 const ViewId
& view_id
,
101 mojo::URLRequestPtr request
);
102 ViewManagerServiceImpl
* EmbedAtView(
103 mojo::ConnectionSpecificId creator_id
,
104 const ViewId
& view_id
,
105 mojo::ViewManagerClientPtr client
);
107 // Invoked when an accelerator has been triggered on a view tree with the
109 void OnAccelerator(ServerView
* root
, mojo::EventPtr event
);
111 // Returns the connection by id.
112 ViewManagerServiceImpl
* GetConnection(
113 mojo::ConnectionSpecificId connection_id
);
115 // Returns the View identified by |id|.
116 ServerView
* GetView(const ViewId
& id
);
118 void SetFocusedView(ServerView
* view
);
119 ServerView
* GetFocusedView();
121 // Returns whether |view| is a descendant of some root view but not itself a
123 bool IsViewAttachedToRoot(const ServerView
* view
) const;
125 // Schedules a paint for the specified region in the coordinates of |view|.
126 void SchedulePaint(const ServerView
* view
, const gfx::Rect
& bounds
);
128 bool IsProcessingChange() const { return current_change_
!= NULL
; }
130 bool is_processing_delete_view() const {
131 return current_change_
&& current_change_
->is_delete_view();
134 // Invoked when the ViewManagerRootImpl's display is closed.
135 void OnDisplayClosed();
137 // Invoked when a connection messages a client about the change. This is used
138 // to avoid sending ServerChangeIdAdvanced() unnecessarily.
139 void OnConnectionMessagedClient(mojo::ConnectionSpecificId id
);
141 // Returns true if OnConnectionMessagedClient() was invoked for id.
142 bool DidConnectionMessageClient(mojo::ConnectionSpecificId id
) const;
144 // Returns the metrics of the viewport where the provided |view| is displayed.
145 mojo::ViewportMetricsPtr
GetViewportMetricsForView(const ServerView
* view
);
147 // Returns the ViewManagerServiceImpl that has |id| as a root.
148 ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) {
149 return const_cast<ViewManagerServiceImpl
*>(
150 const_cast<const ConnectionManager
*>(this)->GetConnectionWithRoot(id
));
152 const ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) const;
154 // Returns the first ancestor of |service| that is marked as an embed root.
155 ViewManagerServiceImpl
* GetEmbedRoot(ViewManagerServiceImpl
* service
);
157 // ViewManagerRoot implementation helper; see mojom for details.
158 bool CloneAndAnimate(const ViewId
& view_id
);
160 // Dispatches |event| directly to the appropriate connection for |view|.
161 void DispatchInputEventToView(const ServerView
* view
, mojo::EventPtr event
);
163 void OnEvent(ViewManagerRootImpl
* root
, mojo::EventPtr event
);
165 void AddAccelerator(ViewManagerRootImpl
* root
,
166 mojo::KeyboardCode keyboard_code
,
167 mojo::EventFlags flags
);
169 void RemoveAccelerator(ViewManagerRootImpl
* root
,
170 mojo::KeyboardCode keyboard_code
,
171 mojo::EventFlags flags
);
173 // Set IME's visibility for the specified view. If the view is not the current
174 // focused view, this function will do nothing.
175 void SetImeVisibility(ServerView
* view
, bool visible
);
177 // These functions trivially delegate to all ViewManagerServiceImpls, which in
178 // term notify their clients.
179 void ProcessViewDestroyed(ServerView
* view
);
180 void ProcessViewBoundsChanged(const ServerView
* view
,
181 const gfx::Rect
& old_bounds
,
182 const gfx::Rect
& new_bounds
);
183 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics
& old_metrics
,
184 const mojo::ViewportMetrics
& new_metrics
);
185 void ProcessWillChangeViewHierarchy(const ServerView
* view
,
186 const ServerView
* new_parent
,
187 const ServerView
* old_parent
);
188 void ProcessViewHierarchyChanged(const ServerView
* view
,
189 const ServerView
* new_parent
,
190 const ServerView
* old_parent
);
191 void ProcessViewReorder(const ServerView
* view
,
192 const ServerView
* relative_view
,
193 const mojo::OrderDirection direction
);
194 void ProcessViewDeleted(const ViewId
& view
);
197 using ConnectionMap
= std::map
<mojo::ConnectionSpecificId
, ClientConnection
*>;
198 using RootConnectionMap
=
199 std::map
<ViewManagerRootImpl
*, ViewManagerRootConnection
*>;
201 // Invoked when a connection is about to make a change. Subsequently followed
202 // by FinishChange() once the change is done.
204 // Changes should never nest, meaning each PrepareForChange() must be
205 // balanced with a call to FinishChange() with no PrepareForChange()
207 void PrepareForChange(ScopedChange
* change
);
209 // Balances a call to PrepareForChange().
212 // Returns true if the specified connection originated the current change.
213 bool IsChangeSource(mojo::ConnectionSpecificId connection_id
) const {
214 return current_change_
&& current_change_
->connection_id() == connection_id
;
217 // Callback from animation timer.
218 // TODO(sky): make this real (move to a different class).
221 // Adds |connection| to internal maps.
222 void AddConnection(ClientConnection
* connection
);
224 ViewManagerRootImpl
* GetViewManagerRootByView(const ServerView
* view
) const;
226 // Overridden from ServerViewDelegate:
227 void PrepareToDestroyView(ServerView
* view
) override
;
228 void PrepareToChangeViewHierarchy(ServerView
* view
,
229 ServerView
* new_parent
,
230 ServerView
* old_parent
) override
;
231 void PrepareToChangeViewVisibility(ServerView
* view
) override
;
232 void OnScheduleViewPaint(const ServerView
* view
) override
;
233 const ServerView
* GetRootView(const ServerView
* view
) const override
;
235 // Overridden from ServerViewObserver:
236 void OnViewDestroyed(ServerView
* view
) override
;
237 void OnWillChangeViewHierarchy(ServerView
* view
,
238 ServerView
* new_parent
,
239 ServerView
* old_parent
) override
;
240 void OnViewHierarchyChanged(ServerView
* view
,
241 ServerView
* new_parent
,
242 ServerView
* old_parent
) override
;
243 void OnViewBoundsChanged(ServerView
* view
,
244 const gfx::Rect
& old_bounds
,
245 const gfx::Rect
& new_bounds
) override
;
246 void OnViewReordered(ServerView
* view
,
247 ServerView
* relative
,
248 mojo::OrderDirection direction
) override
;
249 void OnWillChangeViewVisibility(ServerView
* view
) override
;
250 void OnViewSharedPropertyChanged(
252 const std::string
& name
,
253 const std::vector
<uint8_t>* new_data
) override
;
254 void OnViewTextInputStateChanged(ServerView
* view
,
255 const ui::TextInputState
& state
) override
;
257 void CloneAndAnimate(mojo::Id transport_view_id
);
259 // FocusControllerDelegate:
260 void OnFocusChanged(ServerView
* old_focused_view
,
261 ServerView
* new_focused_view
) override
;
263 ConnectionManagerDelegate
* delegate_
;
265 // ID to use for next ViewManagerServiceImpl.
266 mojo::ConnectionSpecificId next_connection_id_
;
268 // ID to use for next ViewManagerRootImpl.
269 uint16_t next_root_id_
;
271 EventDispatcher event_dispatcher_
;
273 // Set of ViewManagerServiceImpls.
274 ConnectionMap connection_map_
;
276 // Set of ViewManagerRootImpls.
277 RootConnectionMap root_connection_map_
;
279 // If non-null we're processing a change. The ScopedChange is not owned by us
280 // (it's created on the stack by ViewManagerServiceImpl).
281 ScopedChange
* current_change_
;
285 // TODO(sky): nuke! Just a proof of concept until get real animation api.
286 base::RepeatingTimer
<ConnectionManager
> animation_timer_
;
288 AnimationRunner animation_runner_
;
290 scoped_ptr
<FocusController
> focus_controller_
;
292 DISALLOW_COPY_AND_ASSIGN(ConnectionManager
);
295 } // namespace view_manager
297 #endif // COMPONENTS_VIEW_MANAGER_CONNECTION_MANAGER_H_