Vectorize website settings icons in omnibox
[chromium-blink-merge.git] / components / view_manager / connection_manager.h
blobf49394150a1132811f3922b413dbb4ecb569fe24
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_tree.mojom.h"
19 #include "components/view_manager/public/interfaces/view_tree_host.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/surfaces/surfaces_state.h"
23 #include "components/view_manager/view_tree_host_impl.h"
24 #include "mojo/converters/surfaces/custom_surface_converter.h"
25 #include "third_party/mojo/src/mojo/public/cpp/bindings/array.h"
26 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
28 namespace view_manager {
30 class ClientConnection;
31 class ConnectionManagerDelegate;
32 class FocusController;
33 class ServerView;
34 class ViewTreeHostConnection;
35 class ViewTreeImpl;
37 // ConnectionManager manages the set of connections to the ViewManager (all the
38 // ViewTreeImpl) as well as providing the root of the hierarchy.
39 class ConnectionManager : public ServerViewDelegate,
40 public ServerViewObserver,
41 public FocusControllerDelegate,
42 public mojo::CustomSurfaceConverter {
43 public:
44 // Create when a ViewTreeImpl is about to make a change. Ensures clients are
45 // notified correctly.
46 class ScopedChange {
47 public:
48 ScopedChange(ViewTreeImpl* connection,
49 ConnectionManager* connection_manager,
50 bool is_delete_view);
51 ~ScopedChange();
53 mojo::ConnectionSpecificId connection_id() const { return connection_id_; }
54 bool is_delete_view() const { return is_delete_view_; }
56 // Marks the connection with the specified id as having seen a message.
57 void MarkConnectionAsMessaged(mojo::ConnectionSpecificId connection_id) {
58 message_ids_.insert(connection_id);
61 // Returns true if MarkConnectionAsMessaged(connection_id) was invoked.
62 bool DidMessageConnection(mojo::ConnectionSpecificId connection_id) const {
63 return message_ids_.count(connection_id) > 0;
66 private:
67 ConnectionManager* connection_manager_;
68 const mojo::ConnectionSpecificId connection_id_;
69 const bool is_delete_view_;
71 // See description of MarkConnectionAsMessaged/DidMessageConnection.
72 std::set<mojo::ConnectionSpecificId> message_ids_;
74 DISALLOW_COPY_AND_ASSIGN(ScopedChange);
77 ConnectionManager(
78 ConnectionManagerDelegate* delegate,
79 const scoped_refptr<surfaces::SurfacesState>& surfaces_state);
80 ~ConnectionManager() override;
82 // Adds a ViewTreeHost.
83 void AddHost(ViewTreeHostConnection* connection);
85 // Creates a new ServerView. The return value is owned by the caller, but must
86 // be destroyed before ConnectionManager.
87 ServerView* CreateServerView(const ViewId& id);
89 // Returns the id for the next ViewTreeImpl.
90 mojo::ConnectionSpecificId GetAndAdvanceNextConnectionId();
92 // Returns the id for the next ViewTreeHostImpl.
93 uint16_t GetAndAdvanceNextHostId();
95 // Invoked when a ViewTreeImpl's connection encounters an error.
96 void OnConnectionError(ClientConnection* connection);
98 // Invoked when a ViewTreeHostBindingOwnerBase's connection encounters an
99 // error or the associated Display window is closed.
100 void OnHostConnectionClosed(ViewTreeHostConnection* connection);
102 // See description of ViewTree::Embed() for details. This assumes
103 // |transport_view_id| is valid.
104 void EmbedAtView(mojo::ConnectionSpecificId creator_id,
105 const ViewId& view_id,
106 mojo::URLRequestPtr request);
107 ViewTreeImpl* EmbedAtView(mojo::ConnectionSpecificId creator_id,
108 const ViewId& view_id,
109 mojo::ViewTreeClientPtr client);
111 // Invoked when an accelerator has been triggered on a view tree with the
112 // provided |root|.
113 void OnAccelerator(ServerView* root, uint32 id, mojo::EventPtr event);
115 // Returns the connection by id.
116 ViewTreeImpl* GetConnection(
117 mojo::ConnectionSpecificId connection_id);
119 // Returns the View identified by |id|.
120 ServerView* GetView(const ViewId& id);
122 void SetFocusedView(ServerView* view);
123 ServerView* GetFocusedView();
125 // Returns whether |view| is a descendant of some root view but not itself a
126 // root view.
127 bool IsViewAttachedToRoot(const ServerView* view) const;
129 // Schedules a paint for the specified region in the coordinates of |view|.
130 void SchedulePaint(const ServerView* view, const gfx::Rect& bounds);
132 bool IsProcessingChange() const { return current_change_ != NULL; }
134 bool is_processing_delete_view() const {
135 return current_change_ && current_change_->is_delete_view();
138 // Invoked when the ViewTreeHostImpl's display is closed.
139 void OnDisplayClosed();
141 // Invoked when a connection messages a client about the change. This is used
142 // to avoid sending ServerChangeIdAdvanced() unnecessarily.
143 void OnConnectionMessagedClient(mojo::ConnectionSpecificId id);
145 // Returns true if OnConnectionMessagedClient() was invoked for id.
146 bool DidConnectionMessageClient(mojo::ConnectionSpecificId id) const;
148 // Returns the metrics of the viewport where the provided |view| is displayed.
149 mojo::ViewportMetricsPtr GetViewportMetricsForView(const ServerView* view);
151 // Returns the ViewTreeImpl that has |id| as a root.
152 ViewTreeImpl* GetConnectionWithRoot(const ViewId& id) {
153 return const_cast<ViewTreeImpl*>(
154 const_cast<const ConnectionManager*>(this)->GetConnectionWithRoot(id));
156 const ViewTreeImpl* GetConnectionWithRoot(const ViewId& id) const;
158 // Returns the first ancestor of |service| that is marked as an embed root.
159 ViewTreeImpl* GetEmbedRoot(ViewTreeImpl* service);
161 // ViewTreeHost implementation helper; see mojom for details.
162 bool CloneAndAnimate(const ViewId& view_id);
164 // Dispatches |event| directly to the appropriate connection for |view|.
165 void DispatchInputEventToView(const ServerView* view, mojo::EventPtr event);
167 void OnEvent(ViewTreeHostImpl* host, mojo::EventPtr event);
169 void AddAccelerator(ViewTreeHostImpl* host,
170 uint32_t id,
171 mojo::KeyboardCode keyboard_code,
172 mojo::EventFlags flags);
173 void RemoveAccelerator(ViewTreeHostImpl* host, uint32_t id);
175 // Set IME's visibility for the specified view. If the view is not the current
176 // focused view, this function will do nothing.
177 void SetImeVisibility(ServerView* view, bool visible);
179 // These functions trivially delegate to all ViewTreeImpls, which in
180 // term notify their clients.
181 void ProcessViewDestroyed(ServerView* view);
182 void ProcessViewBoundsChanged(const ServerView* view,
183 const gfx::Rect& old_bounds,
184 const gfx::Rect& new_bounds);
185 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics& old_metrics,
186 const mojo::ViewportMetrics& new_metrics);
187 void ProcessWillChangeViewHierarchy(const ServerView* view,
188 const ServerView* new_parent,
189 const ServerView* old_parent);
190 void ProcessViewHierarchyChanged(const ServerView* view,
191 const ServerView* new_parent,
192 const ServerView* old_parent);
193 void ProcessViewReorder(const ServerView* view,
194 const ServerView* relative_view,
195 const mojo::OrderDirection direction);
196 void ProcessViewDeleted(const ViewId& view);
198 private:
199 using ConnectionMap = std::map<mojo::ConnectionSpecificId, ClientConnection*>;
200 using HostConnectionMap =
201 std::map<ViewTreeHostImpl*, ViewTreeHostConnection*>;
203 // Invoked when a connection is about to make a change. Subsequently followed
204 // by FinishChange() once the change is done.
206 // Changes should never nest, meaning each PrepareForChange() must be
207 // balanced with a call to FinishChange() with no PrepareForChange()
208 // in between.
209 void PrepareForChange(ScopedChange* change);
211 // Balances a call to PrepareForChange().
212 void FinishChange();
214 // Returns true if the specified connection originated the current change.
215 bool IsChangeSource(mojo::ConnectionSpecificId connection_id) const {
216 return current_change_ && current_change_->connection_id() == connection_id;
219 // Callback from animation timer.
220 // TODO(sky): make this real (move to a different class).
221 void DoAnimation();
223 // Adds |connection| to internal maps.
224 void AddConnection(ClientConnection* connection);
226 ViewTreeHostImpl* GetViewTreeHostByView(const ServerView* view) const;
228 // Overridden from ServerViewDelegate:
229 scoped_ptr<cc::CompositorFrame> UpdateViewTreeFromCompositorFrame(
230 const mojo::CompositorFramePtr& input) override;
231 surfaces::SurfacesState* GetSurfacesState() override;
232 void PrepareToDestroyView(ServerView* view) override;
233 void PrepareToChangeViewHierarchy(ServerView* view,
234 ServerView* new_parent,
235 ServerView* old_parent) override;
236 void PrepareToChangeViewVisibility(ServerView* view) override;
237 void OnScheduleViewPaint(const ServerView* view) override;
238 const ServerView* GetRootView(const ServerView* view) const override;
240 // Overridden from ServerViewObserver:
241 void OnViewDestroyed(ServerView* view) override;
242 void OnWillChangeViewHierarchy(ServerView* view,
243 ServerView* new_parent,
244 ServerView* old_parent) override;
245 void OnViewHierarchyChanged(ServerView* view,
246 ServerView* new_parent,
247 ServerView* old_parent) override;
248 void OnViewBoundsChanged(ServerView* view,
249 const gfx::Rect& old_bounds,
250 const gfx::Rect& new_bounds) override;
251 void OnViewReordered(ServerView* view,
252 ServerView* relative,
253 mojo::OrderDirection direction) override;
254 void OnWillChangeViewVisibility(ServerView* view) override;
255 void OnViewSharedPropertyChanged(
256 ServerView* view,
257 const std::string& name,
258 const std::vector<uint8_t>* new_data) override;
259 void OnViewTextInputStateChanged(ServerView* view,
260 const ui::TextInputState& state) override;
262 void CloneAndAnimate(mojo::Id transport_view_id);
264 // FocusControllerDelegate:
265 void OnFocusChanged(ServerView* old_focused_view,
266 ServerView* new_focused_view) override;
268 // Overriden from CustomSurfaceConverter:
269 bool ConvertSurfaceDrawQuad(const mojo::QuadPtr& input,
270 cc::SharedQuadState* sqs,
271 cc::RenderPass* render_pass) override;
273 ConnectionManagerDelegate* delegate_;
275 // State for rendering into a Surface.
276 scoped_refptr<surfaces::SurfacesState> surfaces_state_;
278 // ID to use for next ViewTreeImpl.
279 mojo::ConnectionSpecificId next_connection_id_;
281 // ID to use for next ViewTreeHostImpl.
282 uint16_t next_host_id_;
284 EventDispatcher event_dispatcher_;
286 // Set of ViewTreeImpls.
287 ConnectionMap connection_map_;
289 // Set of ViewTreeHostImpls.
290 HostConnectionMap host_connection_map_;
292 // If non-null we're processing a change. The ScopedChange is not owned by us
293 // (it's created on the stack by ViewTreeImpl).
294 ScopedChange* current_change_;
296 bool in_destructor_;
298 // TODO(sky): nuke! Just a proof of concept until get real animation api.
299 base::RepeatingTimer<ConnectionManager> animation_timer_;
301 AnimationRunner animation_runner_;
303 scoped_ptr<FocusController> focus_controller_;
305 DISALLOW_COPY_AND_ASSIGN(ConnectionManager);
308 } // namespace view_manager
310 #endif // COMPONENTS_VIEW_MANAGER_CONNECTION_MANAGER_H_