| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef SERVICES_VIEW_MANAGER_CONNECTION_MANAGER_H_ | |
| 6 #define SERVICES_VIEW_MANAGER_CONNECTION_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <set> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/timer/timer.h" | |
| 14 #include "mojo/public/cpp/bindings/array.h" | |
| 15 #include "mojo/services/view_manager/interfaces/view_manager.mojom.h" | |
| 16 #include "mojo/services/window_manager/interfaces/window_manager_internal.mojom.
h" | |
| 17 #include "services/view_manager/animation_runner.h" | |
| 18 #include "services/view_manager/ids.h" | |
| 19 #include "services/view_manager/server_view_delegate.h" | |
| 20 #include "services/view_manager/server_view_observer.h" | |
| 21 | |
| 22 namespace view_manager { | |
| 23 | |
| 24 class ClientConnection; | |
| 25 class ConnectionManagerDelegate; | |
| 26 class DisplayManager; | |
| 27 class ServerView; | |
| 28 class ViewManagerServiceImpl; | |
| 29 | |
| 30 // ConnectionManager manages the set of connections to the ViewManager (all the | |
| 31 // ViewManagerServiceImpls) as well as providing the root of the hierarchy. | |
| 32 class ConnectionManager : public ServerViewDelegate, | |
| 33 public ServerViewObserver, | |
| 34 public mojo::WindowManagerInternalClient { | |
| 35 public: | |
| 36 // Create when a ViewManagerServiceImpl is about to make a change. Ensures | |
| 37 // clients are notified correctly. | |
| 38 class ScopedChange { | |
| 39 public: | |
| 40 ScopedChange(ViewManagerServiceImpl* connection, | |
| 41 ConnectionManager* connection_manager, | |
| 42 bool is_delete_view); | |
| 43 ~ScopedChange(); | |
| 44 | |
| 45 mojo::ConnectionSpecificId connection_id() const { return connection_id_; } | |
| 46 bool is_delete_view() const { return is_delete_view_; } | |
| 47 | |
| 48 // Marks the connection with the specified id as having seen a message. | |
| 49 void MarkConnectionAsMessaged(mojo::ConnectionSpecificId connection_id) { | |
| 50 message_ids_.insert(connection_id); | |
| 51 } | |
| 52 | |
| 53 // Returns true if MarkConnectionAsMessaged(connection_id) was invoked. | |
| 54 bool DidMessageConnection(mojo::ConnectionSpecificId connection_id) const { | |
| 55 return message_ids_.count(connection_id) > 0; | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 ConnectionManager* connection_manager_; | |
| 60 const mojo::ConnectionSpecificId connection_id_; | |
| 61 const bool is_delete_view_; | |
| 62 | |
| 63 // See description of MarkConnectionAsMessaged/DidMessageConnection. | |
| 64 std::set<mojo::ConnectionSpecificId> message_ids_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(ScopedChange); | |
| 67 }; | |
| 68 | |
| 69 ConnectionManager(ConnectionManagerDelegate* delegate, | |
| 70 scoped_ptr<DisplayManager> display_manager, | |
| 71 mojo::WindowManagerInternal* wm_internal); | |
| 72 ~ConnectionManager() override; | |
| 73 | |
| 74 // Creates a new ServerView. The return value is owned by the caller, but must | |
| 75 // be destroyed before ConnectionManager. | |
| 76 ServerView* CreateServerView(const ViewId& id); | |
| 77 | |
| 78 // Returns the id for the next ViewManagerServiceImpl. | |
| 79 mojo::ConnectionSpecificId GetAndAdvanceNextConnectionId(); | |
| 80 | |
| 81 // Invoked when a ViewManagerServiceImpl's connection encounters an error. | |
| 82 void OnConnectionError(ClientConnection* connection); | |
| 83 | |
| 84 // See description of ViewManagerService::Embed() for details. This assumes | |
| 85 // |transport_view_id| is valid. | |
| 86 void EmbedAtView(mojo::ConnectionSpecificId creator_id, | |
| 87 const std::string& url, | |
| 88 const ViewId& view_id, | |
| 89 mojo::InterfaceRequest<mojo::ServiceProvider> services, | |
| 90 mojo::ServiceProviderPtr exposed_services); | |
| 91 void EmbedAtView(mojo::ConnectionSpecificId creator_id, | |
| 92 const ViewId& view_id, | |
| 93 mojo::ViewManagerClientPtr client); | |
| 94 | |
| 95 // Returns the connection by id. | |
| 96 ViewManagerServiceImpl* GetConnection( | |
| 97 mojo::ConnectionSpecificId connection_id); | |
| 98 | |
| 99 // Returns the View identified by |id|. | |
| 100 ServerView* GetView(const ViewId& id); | |
| 101 | |
| 102 ServerView* root() { return root_.get(); } | |
| 103 DisplayManager* display_manager() { return display_manager_.get(); } | |
| 104 | |
| 105 bool IsProcessingChange() const { return current_change_ != NULL; } | |
| 106 | |
| 107 bool is_processing_delete_view() const { | |
| 108 return current_change_ && current_change_->is_delete_view(); | |
| 109 } | |
| 110 | |
| 111 // Invoked when a connection messages a client about the change. This is used | |
| 112 // to avoid sending ServerChangeIdAdvanced() unnecessarily. | |
| 113 void OnConnectionMessagedClient(mojo::ConnectionSpecificId id); | |
| 114 | |
| 115 // Returns true if OnConnectionMessagedClient() was invoked for id. | |
| 116 bool DidConnectionMessageClient(mojo::ConnectionSpecificId id) const; | |
| 117 | |
| 118 // Returns the ViewManagerServiceImpl that has |id| as a root. | |
| 119 ViewManagerServiceImpl* GetConnectionWithRoot(const ViewId& id) { | |
| 120 return const_cast<ViewManagerServiceImpl*>( | |
| 121 const_cast<const ConnectionManager*>(this)->GetConnectionWithRoot(id)); | |
| 122 } | |
| 123 const ViewManagerServiceImpl* GetConnectionWithRoot(const ViewId& id) const; | |
| 124 | |
| 125 mojo::WindowManagerInternal* wm_internal() { return wm_internal_; } | |
| 126 | |
| 127 void SetWindowManagerClientConnection( | |
| 128 scoped_ptr<ClientConnection> connection); | |
| 129 bool has_window_manager_client_connection() const { | |
| 130 return window_manager_client_connection_ != nullptr; | |
| 131 } | |
| 132 | |
| 133 mojo::ViewManagerClient* GetWindowManagerViewManagerClient(); | |
| 134 | |
| 135 // WindowManagerInternalClient implementation helper; see mojom for details. | |
| 136 bool CloneAndAnimate(const ViewId& view_id); | |
| 137 | |
| 138 // These functions trivially delegate to all ViewManagerServiceImpls, which in | |
| 139 // term notify their clients. | |
| 140 void ProcessViewDestroyed(ServerView* view); | |
| 141 void ProcessViewBoundsChanged(const ServerView* view, | |
| 142 const gfx::Rect& old_bounds, | |
| 143 const gfx::Rect& new_bounds); | |
| 144 void ProcessViewportMetricsChanged(const mojo::ViewportMetrics& old_metrics, | |
| 145 const mojo::ViewportMetrics& new_metrics); | |
| 146 void ProcessWillChangeViewHierarchy(const ServerView* view, | |
| 147 const ServerView* new_parent, | |
| 148 const ServerView* old_parent); | |
| 149 void ProcessViewHierarchyChanged(const ServerView* view, | |
| 150 const ServerView* new_parent, | |
| 151 const ServerView* old_parent); | |
| 152 void ProcessViewReorder(const ServerView* view, | |
| 153 const ServerView* relative_view, | |
| 154 const mojo::OrderDirection direction); | |
| 155 void ProcessViewDeleted(const ViewId& view); | |
| 156 | |
| 157 private: | |
| 158 typedef std::map<mojo::ConnectionSpecificId, ClientConnection*> ConnectionMap; | |
| 159 | |
| 160 // Invoked when a connection is about to make a change. Subsequently followed | |
| 161 // by FinishChange() once the change is done. | |
| 162 // | |
| 163 // Changes should never nest, meaning each PrepareForChange() must be | |
| 164 // balanced with a call to FinishChange() with no PrepareForChange() | |
| 165 // in between. | |
| 166 void PrepareForChange(ScopedChange* change); | |
| 167 | |
| 168 // Balances a call to PrepareForChange(). | |
| 169 void FinishChange(); | |
| 170 | |
| 171 // Returns true if the specified connection originated the current change. | |
| 172 bool IsChangeSource(mojo::ConnectionSpecificId connection_id) const { | |
| 173 return current_change_ && current_change_->connection_id() == connection_id; | |
| 174 } | |
| 175 | |
| 176 // Adds |connection| to internal maps. | |
| 177 void AddConnection(ClientConnection* connection); | |
| 178 | |
| 179 // Callback from animation timer. | |
| 180 // TODO(sky): make this real (move to a different class). | |
| 181 void DoAnimation(); | |
| 182 | |
| 183 // Overridden from ServerViewDelegate: | |
| 184 void PrepareToDestroyView(ServerView* view) override; | |
| 185 void PrepareToChangeViewHierarchy(ServerView* view, | |
| 186 ServerView* new_parent, | |
| 187 ServerView* old_parent) override; | |
| 188 void PrepareToChangeViewVisibility(ServerView* view) override; | |
| 189 void OnScheduleViewPaint(const ServerView* view) override; | |
| 190 | |
| 191 // Overridden from ServerViewObserver: | |
| 192 void OnViewDestroyed(ServerView* view) override; | |
| 193 void OnWillChangeViewHierarchy(ServerView* view, | |
| 194 ServerView* new_parent, | |
| 195 ServerView* old_parent) override; | |
| 196 void OnViewHierarchyChanged(ServerView* view, | |
| 197 ServerView* new_parent, | |
| 198 ServerView* old_parent) override; | |
| 199 void OnViewBoundsChanged(ServerView* view, | |
| 200 const gfx::Rect& old_bounds, | |
| 201 const gfx::Rect& new_bounds) override; | |
| 202 void OnViewReordered(ServerView* view, | |
| 203 ServerView* relative, | |
| 204 mojo::OrderDirection direction) override; | |
| 205 void OnWillChangeViewVisibility(ServerView* view) override; | |
| 206 void OnViewSharedPropertyChanged( | |
| 207 ServerView* view, | |
| 208 const std::string& name, | |
| 209 const std::vector<uint8_t>* new_data) override; | |
| 210 | |
| 211 // WindowManagerInternalClient: | |
| 212 void DispatchInputEventToView(mojo::Id transport_view_id, | |
| 213 mojo::EventPtr event) override; | |
| 214 void SetViewportSize(mojo::SizePtr size) override; | |
| 215 void CloneAndAnimate(mojo::Id transport_view_id) override; | |
| 216 | |
| 217 ConnectionManagerDelegate* delegate_; | |
| 218 | |
| 219 // The ClientConnection containing the ViewManagerService implementation | |
| 220 // provided to the initial connection (the WindowManager). | |
| 221 // NOTE: |window_manager_client_connection_| is also in |connection_map_|. | |
| 222 ClientConnection* window_manager_client_connection_; | |
| 223 | |
| 224 // ID to use for next ViewManagerServiceImpl. | |
| 225 mojo::ConnectionSpecificId next_connection_id_; | |
| 226 | |
| 227 // Set of ViewManagerServiceImpls. | |
| 228 ConnectionMap connection_map_; | |
| 229 | |
| 230 scoped_ptr<DisplayManager> display_manager_; | |
| 231 | |
| 232 scoped_ptr<ServerView> root_; | |
| 233 | |
| 234 mojo::WindowManagerInternal* wm_internal_; | |
| 235 | |
| 236 // If non-null we're processing a change. The ScopedChange is not owned by us | |
| 237 // (it's created on the stack by ViewManagerServiceImpl). | |
| 238 ScopedChange* current_change_; | |
| 239 | |
| 240 bool in_destructor_; | |
| 241 | |
| 242 // TODO(sky): nuke! Just a proof of concept until get real animation api. | |
| 243 base::RepeatingTimer<ConnectionManager> animation_timer_; | |
| 244 | |
| 245 AnimationRunner animation_runner_; | |
| 246 | |
| 247 DISALLOW_COPY_AND_ASSIGN(ConnectionManager); | |
| 248 }; | |
| 249 | |
| 250 } // namespace view_manager | |
| 251 | |
| 252 #endif // SERVICES_VIEW_MANAGER_CONNECTION_MANAGER_H_ | |
| OLD | NEW |