Move StartsWith[ASCII] to base namespace.
[chromium-blink-merge.git] / components / view_manager / connection_manager.h
blobcdc133e9d27930b4e5a6064e86e2709e22de4496
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_
8 #include <map>
9 #include <set>
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 "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
23 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
25 namespace view_manager {
27 class ClientConnection;
28 class ConnectionManagerDelegate;
29 class DisplayManager;
30 class FocusController;
31 class ServerView;
32 class ViewManagerServiceImpl;
34 // ConnectionManager manages the set of connections to the ViewManager (all the
35 // ViewManagerServiceImpls) as well as providing the root of the hierarchy.
36 class ConnectionManager : public ServerViewDelegate,
37 public ServerViewObserver,
38 public mojo::ViewManagerRoot,
39 public FocusControllerDelegate {
40 public:
41 // Create when a ViewManagerServiceImpl is about to make a change. Ensures
42 // clients are notified correctly.
43 class ScopedChange {
44 public:
45 ScopedChange(ViewManagerServiceImpl* connection,
46 ConnectionManager* connection_manager,
47 bool is_delete_view);
48 ~ScopedChange();
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;
63 private:
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,
75 scoped_ptr<DisplayManager> display_manager);
76 ~ConnectionManager() override;
78 // Creates a new ServerView. The return value is owned by the caller, but must
79 // be destroyed before ConnectionManager.
80 ServerView* CreateServerView(const ViewId& id);
82 // Returns the id for the next ViewManagerServiceImpl.
83 mojo::ConnectionSpecificId GetAndAdvanceNextConnectionId();
85 // Invoked when a ViewManagerServiceImpl's connection encounters an error.
86 void OnConnectionError(ClientConnection* connection);
88 // See description of ViewManagerService::Embed() for details. This assumes
89 // |transport_view_id| is valid.
90 void EmbedAtView(mojo::ConnectionSpecificId creator_id,
91 const ViewId& view_id,
92 mojo::URLRequestPtr request);
93 void EmbedAtView(mojo::ConnectionSpecificId creator_id,
94 const ViewId& view_id,
95 mojo::ViewManagerClientPtr client);
97 // Returns the connection by id.
98 ViewManagerServiceImpl* GetConnection(
99 mojo::ConnectionSpecificId connection_id);
101 // Returns the View identified by |id|.
102 ServerView* GetView(const ViewId& id);
104 void SetFocusedView(ServerView* view);
105 ServerView* GetFocusedView();
107 ServerView* root() { return root_.get(); }
109 // Returns whether |view| is a descendant of some root view but not itself a
110 // root view.
111 bool IsViewAttachedToRoot(const ServerView* view) const;
113 DisplayManager* display_manager() { return display_manager_.get(); }
115 bool IsProcessingChange() const { return current_change_ != NULL; }
117 bool is_processing_delete_view() const {
118 return current_change_ && current_change_->is_delete_view();
121 // Invoked when a connection messages a client about the change. This is used
122 // to avoid sending ServerChangeIdAdvanced() unnecessarily.
123 void OnConnectionMessagedClient(mojo::ConnectionSpecificId id);
125 // Returns true if OnConnectionMessagedClient() was invoked for id.
126 bool DidConnectionMessageClient(mojo::ConnectionSpecificId id) const;
128 // Returns the ViewManagerServiceImpl that has |id| as a root.
129 ViewManagerServiceImpl* GetConnectionWithRoot(const ViewId& id) {
130 return const_cast<ViewManagerServiceImpl*>(
131 const_cast<const ConnectionManager*>(this)->GetConnectionWithRoot(id));
133 const ViewManagerServiceImpl* GetConnectionWithRoot(const ViewId& id) const;
135 // Returns the first ancestor of |service| that is marked as an embed root.
136 ViewManagerServiceImpl* GetEmbedRoot(ViewManagerServiceImpl* service);
138 mojo::ViewManagerRootClient* view_manager_root_client() {
139 return view_manager_root_client_.get();
142 void SetWindowManagerClientConnection(
143 scoped_ptr<ClientConnection> connection);
144 bool has_window_manager_client_connection() const {
145 return window_manager_client_connection_ != nullptr;
148 mojo::ViewManagerClient* GetWindowManagerViewManagerClient();
150 // ViewManagerRoot implementation helper; see mojom for details.
151 bool CloneAndAnimate(const ViewId& view_id);
153 // Processes an event, potentially changing focus.
154 void ProcessEvent(mojo::EventPtr event);
156 // Dispatches |event| directly to the appropriate connection for |view|.
157 void DispatchInputEventToView(const ServerView* view, mojo::EventPtr event);
159 // These functions trivially delegate to all ViewManagerServiceImpls, which in
160 // term notify their clients.
161 void ProcessViewDestroyed(ServerView* view);
162 void ProcessViewBoundsChanged(const ServerView* view,
163 const gfx::Rect& old_bounds,
164 const gfx::Rect& new_bounds);
165 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics& old_metrics,
166 const mojo::ViewportMetrics& new_metrics);
167 void ProcessWillChangeViewHierarchy(const ServerView* view,
168 const ServerView* new_parent,
169 const ServerView* old_parent);
170 void ProcessViewHierarchyChanged(const ServerView* view,
171 const ServerView* new_parent,
172 const ServerView* old_parent);
173 void ProcessViewReorder(const ServerView* view,
174 const ServerView* relative_view,
175 const mojo::OrderDirection direction);
176 void ProcessViewDeleted(const ViewId& view);
178 private:
179 using ConnectionMap = std::map<mojo::ConnectionSpecificId, ClientConnection*>;
181 // Invoked when a connection is about to make a change. Subsequently followed
182 // by FinishChange() once the change is done.
184 // Changes should never nest, meaning each PrepareForChange() must be
185 // balanced with a call to FinishChange() with no PrepareForChange()
186 // in between.
187 void PrepareForChange(ScopedChange* change);
189 // Balances a call to PrepareForChange().
190 void FinishChange();
192 // Returns true if the specified connection originated the current change.
193 bool IsChangeSource(mojo::ConnectionSpecificId connection_id) const {
194 return current_change_ && current_change_->connection_id() == connection_id;
197 // Callback from animation timer.
198 // TODO(sky): make this real (move to a different class).
199 void DoAnimation();
201 // Adds |connection| to internal maps.
202 void AddConnection(ClientConnection* connection);
204 // Overridden from ServerViewDelegate:
205 void PrepareToDestroyView(ServerView* view) override;
206 void PrepareToChangeViewHierarchy(ServerView* view,
207 ServerView* new_parent,
208 ServerView* old_parent) override;
209 void PrepareToChangeViewVisibility(ServerView* view) override;
210 void OnScheduleViewPaint(const ServerView* view) override;
211 bool IsViewDrawn(const ServerView* view) const override;
213 // Overridden from ServerViewObserver:
214 void OnViewDestroyed(ServerView* view) override;
215 void OnWillChangeViewHierarchy(ServerView* view,
216 ServerView* new_parent,
217 ServerView* old_parent) override;
218 void OnViewHierarchyChanged(ServerView* view,
219 ServerView* new_parent,
220 ServerView* old_parent) override;
221 void OnViewBoundsChanged(ServerView* view,
222 const gfx::Rect& old_bounds,
223 const gfx::Rect& new_bounds) override;
224 void OnViewReordered(ServerView* view,
225 ServerView* relative,
226 mojo::OrderDirection direction) override;
227 void OnWillChangeViewVisibility(ServerView* view) override;
228 void OnViewSharedPropertyChanged(
229 ServerView* view,
230 const std::string& name,
231 const std::vector<uint8_t>* new_data) override;
233 // ViewManagerRoot:
234 void SetViewManagerRootClient(mojo::ViewManagerRootClientPtr client) override;
235 void SetViewportSize(mojo::SizePtr size) override;
236 void CloneAndAnimate(mojo::Id transport_view_id) override;
237 void AddAccelerator(mojo::KeyboardCode keyboard_code,
238 mojo::EventFlags flags) override;
239 void RemoveAccelerator(mojo::KeyboardCode keyboard_code,
240 mojo::EventFlags flags) override;
242 // FocusControllerDelegate:
243 void OnFocusChanged(ServerView* old_focused_view,
244 ServerView* new_focused_view) override;
246 ConnectionManagerDelegate* delegate_;
248 // The ClientConnection containing the ViewManagerService implementation
249 // provided to the initial connection (the WindowManager).
250 // NOTE: |window_manager_client_connection_| is also in |connection_map_|.
251 ClientConnection* window_manager_client_connection_;
253 // ID to use for next ViewManagerServiceImpl.
254 mojo::ConnectionSpecificId next_connection_id_;
256 // Set of ViewManagerServiceImpls.
257 ConnectionMap connection_map_;
259 // DisplayManager holds a raw pointer to EventDispatcher and so it must be
260 // destroyed after DisplayManager (and thus created before).
261 EventDispatcher event_dispatcher_;
263 scoped_ptr<DisplayManager> display_manager_;
265 scoped_ptr<ServerView> root_;
267 // If non-null we're processing a change. The ScopedChange is not owned by us
268 // (it's created on the stack by ViewManagerServiceImpl).
269 ScopedChange* current_change_;
271 bool in_destructor_;
273 // TODO(sky): nuke! Just a proof of concept until get real animation api.
274 base::RepeatingTimer<ConnectionManager> animation_timer_;
276 AnimationRunner animation_runner_;
278 scoped_ptr<FocusController> focus_controller_;
280 mojo::ViewManagerRootClientPtr view_manager_root_client_;
282 DISALLOW_COPY_AND_ASSIGN(ConnectionManager);
285 } // namespace view_manager
287 #endif // COMPONENTS_VIEW_MANAGER_CONNECTION_MANAGER_H_