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 MOJO_SERVICES_VIEW_MANAGER_CONNECTION_MANAGER_H_
6 #define MOJO_SERVICES_VIEW_MANAGER_CONNECTION_MANAGER_H_
11 #include "base/basictypes.h"
12 #include "mojo/public/cpp/bindings/array.h"
13 #include "mojo/services/view_manager/display_manager.h"
14 #include "mojo/services/view_manager/ids.h"
15 #include "mojo/services/view_manager/server_view.h"
16 #include "mojo/services/view_manager/server_view_delegate.h"
17 #include "mojo/services/view_manager/view_manager_export.h"
25 class ApplicationConnection
;
29 class DisplayManagerDelegate
;
30 class ViewManagerServiceImpl
;
32 // ConnectionManager manages the set of connections to the ViewManager (all the
33 // ViewManagerServiceImpls) as well as providing the root of the hierarchy.
34 class MOJO_VIEW_MANAGER_EXPORT ConnectionManager
: public ServerViewDelegate
{
36 // Create when a ViewManagerServiceImpl is about to make a change. Ensures
37 // clients are notified correctly.
40 ScopedChange(ViewManagerServiceImpl
* connection
,
41 ConnectionManager
* connection_manager
,
45 ConnectionSpecificId
connection_id() const { return connection_id_
; }
46 bool is_delete_view() const { return is_delete_view_
; }
48 // Marks the connection with the specified id as having seen a message.
49 void MarkConnectionAsMessaged(ConnectionSpecificId connection_id
) {
50 message_ids_
.insert(connection_id
);
53 // Returns true if MarkConnectionAsMessaged(connection_id) was invoked.
54 bool DidMessageConnection(ConnectionSpecificId connection_id
) const {
55 return message_ids_
.count(connection_id
) > 0;
59 ConnectionManager
* connection_manager_
;
60 const ConnectionSpecificId connection_id_
;
61 const bool is_delete_view_
;
63 // See description of MarkConnectionAsMessaged/DidMessageConnection.
64 std::set
<ConnectionSpecificId
> message_ids_
;
66 DISALLOW_COPY_AND_ASSIGN(ScopedChange
);
69 ConnectionManager(ApplicationConnection
* app_connection
,
70 DisplayManagerDelegate
* display_manager_delegate
,
71 const Callback
<void()>& native_viewport_closed_callback
);
72 virtual ~ConnectionManager();
74 // Returns the id for the next ViewManagerServiceImpl.
75 ConnectionSpecificId
GetAndAdvanceNextConnectionId();
77 void AddConnection(ViewManagerServiceImpl
* connection
);
78 void RemoveConnection(ViewManagerServiceImpl
* connection
);
80 // Establishes the initial client. Similar to Connect(), but the resulting
81 // client is allowed to do anything.
82 void EmbedRoot(const std::string
& url
,
83 InterfaceRequest
<ServiceProvider
> service_provider
);
85 // See description of ViewManagerService::Embed() for details. This assumes
86 // |transport_view_id| is valid.
87 void Embed(ConnectionSpecificId creator_id
,
90 InterfaceRequest
<ServiceProvider
> service_provider
);
92 // Returns the connection by id.
93 ViewManagerServiceImpl
* GetConnection(ConnectionSpecificId connection_id
);
95 // Returns the View identified by |id|.
96 ServerView
* GetView(const ViewId
& id
);
98 ServerView
* root() { return root_
.get(); }
100 bool IsProcessingChange() const { return current_change_
!= NULL
; }
102 bool is_processing_delete_view() const {
103 return current_change_
&& current_change_
->is_delete_view();
106 // Invoked when a connection messages a client about the change. This is used
107 // to avoid sending ServerChangeIdAdvanced() unnecessarily.
108 void OnConnectionMessagedClient(ConnectionSpecificId id
);
110 // Returns true if OnConnectionMessagedClient() was invoked for id.
111 bool DidConnectionMessageClient(ConnectionSpecificId id
) const;
113 ViewManagerServiceImpl
* GetConnectionByCreator(
114 ConnectionSpecificId creator_id
,
115 const std::string
& url
) const;
117 // Returns the ViewManagerServiceImpl that has |id| as a root.
118 ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) {
119 return const_cast<ViewManagerServiceImpl
*>(
120 const_cast<const ConnectionManager
*>(this)->GetConnectionWithRoot(id
));
122 const ViewManagerServiceImpl
* GetConnectionWithRoot(const ViewId
& id
) const;
124 void DispatchViewInputEventToWindowManager(EventPtr event
);
126 // These functions trivially delegate to all ViewManagerServiceImpls, which in
127 // term notify their clients.
128 void ProcessViewDestroyed(ServerView
* view
);
129 void ProcessViewBoundsChanged(const ServerView
* view
,
130 const gfx::Rect
& old_bounds
,
131 const gfx::Rect
& new_bounds
);
132 void ProcessViewHierarchyChanged(const ServerView
* view
,
133 const ServerView
* new_parent
,
134 const ServerView
* old_parent
);
135 void ProcessViewReorder(const ServerView
* view
,
136 const ServerView
* relative_view
,
137 const OrderDirection direction
);
138 void ProcessViewDeleted(const ViewId
& view
);
141 // Used to setup any static state needed by ConnectionManager.
147 typedef std::map
<ConnectionSpecificId
, ViewManagerServiceImpl
*> ConnectionMap
;
149 // Invoked when a connection is about to make a change. Subsequently followed
150 // by FinishChange() once the change is done.
152 // Changes should never nest, meaning each PrepareForChange() must be
153 // balanced with a call to FinishChange() with no PrepareForChange()
155 void PrepareForChange(ScopedChange
* change
);
157 // Balances a call to PrepareForChange().
160 // Returns true if the specified connection originated the current change.
161 bool IsChangeSource(ConnectionSpecificId connection_id
) const {
162 return current_change_
&& current_change_
->connection_id() == connection_id
;
165 // Implementation of the two embed variants.
166 ViewManagerServiceImpl
* EmbedImpl(
167 ConnectionSpecificId creator_id
,
169 const ViewId
& root_id
,
170 InterfaceRequest
<ServiceProvider
> service_provider
);
172 // Overridden from ServerViewDelegate:
173 virtual void OnViewDestroyed(const ServerView
* view
) OVERRIDE
;
174 virtual void OnViewHierarchyChanged(const ServerView
* view
,
175 const ServerView
* new_parent
,
176 const ServerView
* old_parent
) OVERRIDE
;
177 virtual void OnViewBoundsChanged(const ServerView
* view
,
178 const gfx::Rect
& old_bounds
,
179 const gfx::Rect
& new_bounds
) OVERRIDE
;
180 virtual void OnViewBitmapChanged(const ServerView
* view
) OVERRIDE
;
184 ApplicationConnection
* app_connection_
;
186 // ID to use for next ViewManagerServiceImpl.
187 ConnectionSpecificId next_connection_id_
;
189 // Set of ViewManagerServiceImpls.
190 ConnectionMap connection_map_
;
192 DisplayManager display_manager_
;
194 scoped_ptr
<ServerView
> root_
;
196 // Set of ViewManagerServiceImpls created by way of Connect(). These have to
197 // be explicitly destroyed.
198 std::set
<ViewManagerServiceImpl
*> connections_created_by_connect_
;
200 // If non-null we're processing a change. The ScopedChange is not owned by us
201 // (it's created on the stack by ViewManagerServiceImpl).
202 ScopedChange
* current_change_
;
204 DISALLOW_COPY_AND_ASSIGN(ConnectionManager
);
207 } // namespace service
210 #endif // MOJO_SERVICES_VIEW_MANAGER_CONNECTION_MANAGER_H_