| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/services/view_manager/connection_manager.h" | 5 #include "mojo/services/view_manager/connection_manager.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" |
| 8 #include "mojo/converters/input_events/input_events_type_converters.h" | 9 #include "mojo/converters/input_events/input_events_type_converters.h" |
| 9 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
| 10 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 11 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
| 12 #include "mojo/services/view_manager/connection_manager_delegate.h" |
| 11 #include "mojo/services/view_manager/view_manager_service_impl.h" | 13 #include "mojo/services/view_manager/view_manager_service_impl.h" |
| 12 | 14 |
| 13 namespace mojo { | 15 namespace mojo { |
| 14 namespace service { | 16 namespace service { |
| 15 | 17 |
| 16 ConnectionManager::ScopedChange::ScopedChange( | 18 ConnectionManager::ScopedChange::ScopedChange( |
| 17 ViewManagerServiceImpl* connection, | 19 ViewManagerServiceImpl* connection, |
| 18 ConnectionManager* connection_manager, | 20 ConnectionManager* connection_manager, |
| 19 bool is_delete_view) | 21 bool is_delete_view) |
| 20 : connection_manager_(connection_manager), | 22 : connection_manager_(connection_manager), |
| 21 connection_id_(connection->id()), | 23 connection_id_(connection->id()), |
| 22 is_delete_view_(is_delete_view) { | 24 is_delete_view_(is_delete_view) { |
| 23 connection_manager_->PrepareForChange(this); | 25 connection_manager_->PrepareForChange(this); |
| 24 } | 26 } |
| 25 | 27 |
| 26 ConnectionManager::ScopedChange::~ScopedChange() { | 28 ConnectionManager::ScopedChange::~ScopedChange() { |
| 27 connection_manager_->FinishChange(); | 29 connection_manager_->FinishChange(); |
| 28 } | 30 } |
| 29 | 31 |
| 30 ConnectionManager::ConnectionManager( | 32 ConnectionManager::ConnectionManager(ApplicationConnection* app_connection, |
| 31 ApplicationConnection* app_connection, | 33 ConnectionManagerDelegate* delegate) |
| 32 const Callback<void()>& native_viewport_closed_callback) | |
| 33 : app_connection_(app_connection), | 34 : app_connection_(app_connection), |
| 35 delegate_(delegate), |
| 36 window_manager_vm_service_(nullptr), |
| 34 next_connection_id_(1), | 37 next_connection_id_(1), |
| 35 display_manager_(app_connection, this, native_viewport_closed_callback), | 38 display_manager_( |
| 39 app_connection, |
| 40 this, |
| 41 base::Bind(&ConnectionManagerDelegate::OnNativeViewportDestroyed, |
| 42 base::Unretained(delegate))), |
| 36 root_(new ServerView(this, RootViewId())), | 43 root_(new ServerView(this, RootViewId())), |
| 37 current_change_(NULL) { | 44 current_change_(NULL), |
| 45 in_destructor_(false) { |
| 38 app_connection->ConnectToService(&window_manager_); | 46 app_connection->ConnectToService(&window_manager_); |
| 39 window_manager_.set_client(this); | 47 window_manager_.set_client(this); |
| 40 window_manager_.set_error_handler(this); | 48 window_manager_.set_error_handler(this); |
| 41 // |app_connection| originates from the WindowManager. Let it connect | 49 // |app_connection| originates from the WindowManager. Let it connect |
| 42 // directly to the ViewManager. | 50 // directly to the ViewManager. |
| 43 app_connection->AddService(this); | 51 app_connection->AddService(this); |
| 44 root_->SetBounds(gfx::Rect(800, 600)); | 52 root_->SetBounds(gfx::Rect(800, 600)); |
| 45 } | 53 } |
| 46 | 54 |
| 47 ConnectionManager::~ConnectionManager() { | 55 ConnectionManager::~ConnectionManager() { |
| 48 while (!connections_created_by_connect_.empty()) | 56 in_destructor_ = true; |
| 49 delete *(connections_created_by_connect_.begin()); | 57 |
| 58 STLDeleteValues(&connection_map_); |
| 50 // All the connections should have been destroyed. | 59 // All the connections should have been destroyed. |
| 51 DCHECK(connection_map_.empty()); | 60 DCHECK(connection_map_.empty()); |
| 52 root_.reset(); | 61 root_.reset(); |
| 53 } | 62 } |
| 54 | 63 |
| 55 ConnectionSpecificId ConnectionManager::GetAndAdvanceNextConnectionId() { | 64 ConnectionSpecificId ConnectionManager::GetAndAdvanceNextConnectionId() { |
| 56 const ConnectionSpecificId id = next_connection_id_++; | 65 const ConnectionSpecificId id = next_connection_id_++; |
| 57 DCHECK_LT(id, next_connection_id_); | 66 DCHECK_LT(id, next_connection_id_); |
| 58 return id; | 67 return id; |
| 59 } | 68 } |
| 60 | 69 |
| 61 void ConnectionManager::AddConnection(ViewManagerServiceImpl* connection) { | 70 void ConnectionManager::OnConnectionError(ViewManagerServiceImpl* connection) { |
| 62 DCHECK_EQ(0u, connection_map_.count(connection->id())); | 71 scoped_ptr<ViewManagerServiceImpl> connection_owner(connection); |
| 63 connection_map_[connection->id()] = connection; | |
| 64 } | |
| 65 | 72 |
| 66 void ConnectionManager::RemoveConnection(ViewManagerServiceImpl* connection) { | |
| 67 connection_map_.erase(connection->id()); | 73 connection_map_.erase(connection->id()); |
| 68 connections_created_by_connect_.erase(connection); | |
| 69 | 74 |
| 70 // Notify remaining connections so that they can cleanup. | 75 // Notify remaining connections so that they can cleanup. |
| 71 for (ConnectionMap::const_iterator i = connection_map_.begin(); | 76 for (ConnectionMap::const_iterator i = connection_map_.begin(); |
| 72 i != connection_map_.end(); | 77 i != connection_map_.end(); |
| 73 ++i) { | 78 ++i) { |
| 74 i->second->OnViewManagerServiceImplDestroyed(connection->id()); | 79 i->second->OnViewManagerServiceImplDestroyed(connection->id()); |
| 75 } | 80 } |
| 81 |
| 82 if (connection == window_manager_vm_service_) { |
| 83 window_manager_vm_service_ = nullptr; |
| 84 delegate_->OnLostConnectionToWindowManager(); |
| 85 } |
| 76 } | 86 } |
| 77 | 87 |
| 78 void ConnectionManager::EmbedAtView( | 88 void ConnectionManager::EmbedAtView( |
| 79 ConnectionSpecificId creator_id, | 89 ConnectionSpecificId creator_id, |
| 80 const String& url, | 90 const String& url, |
| 81 Id transport_view_id, | 91 Id transport_view_id, |
| 82 InterfaceRequest<ServiceProvider> service_provider) { | 92 InterfaceRequest<ServiceProvider> service_provider) { |
| 83 EmbedImpl(creator_id, | 93 MessagePipe pipe; |
| 84 url, | 94 |
| 85 ViewIdFromTransportId(transport_view_id), | 95 ServiceProvider* view_manager_service_provider = |
| 86 service_provider.Pass())->set_delete_on_connection_error(); | 96 app_connection_->ConnectToApplication(url)->GetServiceProvider(); |
| 97 |
| 98 view_manager_service_provider->ConnectToService( |
| 99 ViewManagerServiceImpl::Client::Name_, pipe.handle1.Pass()); |
| 100 |
| 101 std::string creator_url; |
| 102 ConnectionMap::const_iterator it = connection_map_.find(creator_id); |
| 103 if (it != connection_map_.end()) |
| 104 creator_url = it->second->url(); |
| 105 |
| 106 ViewManagerServiceImpl* connection = |
| 107 new ViewManagerServiceImpl(this, |
| 108 creator_id, |
| 109 creator_url, |
| 110 url.To<std::string>(), |
| 111 ViewIdFromTransportId(transport_view_id), |
| 112 service_provider.Pass()); |
| 113 AddConnection(connection); |
| 114 WeakBindToPipe(connection, pipe.handle0.Pass()); |
| 115 OnConnectionMessagedClient(connection->id()); |
| 87 } | 116 } |
| 88 | 117 |
| 89 ViewManagerServiceImpl* ConnectionManager::GetConnection( | 118 ViewManagerServiceImpl* ConnectionManager::GetConnection( |
| 90 ConnectionSpecificId connection_id) { | 119 ConnectionSpecificId connection_id) { |
| 91 ConnectionMap::iterator i = connection_map_.find(connection_id); | 120 ConnectionMap::iterator i = connection_map_.find(connection_id); |
| 92 return i == connection_map_.end() ? NULL : i->second; | 121 return i == connection_map_.end() ? NULL : i->second; |
| 93 } | 122 } |
| 94 | 123 |
| 95 ServerView* ConnectionManager::GetView(const ViewId& id) { | 124 ServerView* ConnectionManager::GetView(const ViewId& id) { |
| 96 if (id == root_->id()) | 125 if (id == root_->id()) |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 CHECK(!current_change_); | 212 CHECK(!current_change_); |
| 184 current_change_ = change; | 213 current_change_ = change; |
| 185 } | 214 } |
| 186 | 215 |
| 187 void ConnectionManager::FinishChange() { | 216 void ConnectionManager::FinishChange() { |
| 188 // PrepareForChange/FinishChange should be balanced. | 217 // PrepareForChange/FinishChange should be balanced. |
| 189 CHECK(current_change_); | 218 CHECK(current_change_); |
| 190 current_change_ = NULL; | 219 current_change_ = NULL; |
| 191 } | 220 } |
| 192 | 221 |
| 193 ViewManagerServiceImpl* ConnectionManager::EmbedImpl( | 222 void ConnectionManager::AddConnection(ViewManagerServiceImpl* connection) { |
| 194 const ConnectionSpecificId creator_id, | 223 DCHECK_EQ(0u, connection_map_.count(connection->id())); |
| 195 const String& url, | 224 connection_map_[connection->id()] = connection; |
| 196 const ViewId& root_id, | |
| 197 InterfaceRequest<ServiceProvider> service_provider) { | |
| 198 MessagePipe pipe; | |
| 199 | |
| 200 ServiceProvider* view_manager_service_provider = | |
| 201 app_connection_->ConnectToApplication(url)->GetServiceProvider(); | |
| 202 | |
| 203 view_manager_service_provider->ConnectToService( | |
| 204 ViewManagerServiceImpl::Client::Name_, pipe.handle1.Pass()); | |
| 205 | |
| 206 std::string creator_url; | |
| 207 ConnectionMap::const_iterator it = connection_map_.find(creator_id); | |
| 208 if (it != connection_map_.end()) | |
| 209 creator_url = it->second->url(); | |
| 210 | |
| 211 ViewManagerServiceImpl* connection = | |
| 212 new ViewManagerServiceImpl(this, | |
| 213 creator_id, | |
| 214 creator_url, | |
| 215 url.To<std::string>(), | |
| 216 root_id, | |
| 217 service_provider.Pass()); | |
| 218 WeakBindToPipe(connection, pipe.handle0.Pass()); | |
| 219 connections_created_by_connect_.insert(connection); | |
| 220 OnConnectionMessagedClient(connection->id()); | |
| 221 return connection; | |
| 222 } | 225 } |
| 223 | 226 |
| 224 void ConnectionManager::OnViewDestroyed(const ServerView* view) { | 227 void ConnectionManager::OnViewDestroyed(const ServerView* view) { |
| 225 ProcessViewDeleted(view->id()); | 228 if (!in_destructor_) |
| 229 ProcessViewDeleted(view->id()); |
| 226 } | 230 } |
| 227 | 231 |
| 228 void ConnectionManager::OnWillChangeViewHierarchy( | 232 void ConnectionManager::OnWillChangeViewHierarchy( |
| 229 const ServerView* view, | 233 const ServerView* view, |
| 230 const ServerView* new_parent, | 234 const ServerView* new_parent, |
| 231 const ServerView* old_parent) { | 235 const ServerView* old_parent) { |
| 232 if (!display_manager_.in_setup()) | 236 if (!in_destructor_ && !display_manager_.in_setup()) |
| 233 ProcessWillChangeViewHierarchy(view, new_parent, old_parent); | 237 ProcessWillChangeViewHierarchy(view, new_parent, old_parent); |
| 234 } | 238 } |
| 235 | 239 |
| 236 void ConnectionManager::OnViewHierarchyChanged(const ServerView* view, | 240 void ConnectionManager::OnViewHierarchyChanged(const ServerView* view, |
| 237 const ServerView* new_parent, | 241 const ServerView* new_parent, |
| 238 const ServerView* old_parent) { | 242 const ServerView* old_parent) { |
| 243 if (in_destructor_) |
| 244 return; |
| 245 |
| 239 if (!display_manager_.in_setup()) | 246 if (!display_manager_.in_setup()) |
| 240 ProcessViewHierarchyChanged(view, new_parent, old_parent); | 247 ProcessViewHierarchyChanged(view, new_parent, old_parent); |
| 248 |
| 241 // TODO(beng): optimize. | 249 // TODO(beng): optimize. |
| 242 if (old_parent) { | 250 if (old_parent) { |
| 243 display_manager_.SchedulePaint(old_parent, | 251 display_manager_.SchedulePaint(old_parent, |
| 244 gfx::Rect(old_parent->bounds().size())); | 252 gfx::Rect(old_parent->bounds().size())); |
| 245 } | 253 } |
| 246 if (new_parent) { | 254 if (new_parent) { |
| 247 display_manager_.SchedulePaint(new_parent, | 255 display_manager_.SchedulePaint(new_parent, |
| 248 gfx::Rect(new_parent->bounds().size())); | 256 gfx::Rect(new_parent->bounds().size())); |
| 249 } | 257 } |
| 250 } | 258 } |
| 251 | 259 |
| 252 void ConnectionManager::OnViewBoundsChanged(const ServerView* view, | 260 void ConnectionManager::OnViewBoundsChanged(const ServerView* view, |
| 253 const gfx::Rect& old_bounds, | 261 const gfx::Rect& old_bounds, |
| 254 const gfx::Rect& new_bounds) { | 262 const gfx::Rect& new_bounds) { |
| 263 if (in_destructor_) |
| 264 return; |
| 265 |
| 255 ProcessViewBoundsChanged(view, old_bounds, new_bounds); | 266 ProcessViewBoundsChanged(view, old_bounds, new_bounds); |
| 256 if (!view->parent()) | 267 if (!view->parent()) |
| 257 return; | 268 return; |
| 258 | 269 |
| 259 // TODO(sky): optimize this. | 270 // TODO(sky): optimize this. |
| 260 display_manager_.SchedulePaint(view->parent(), old_bounds); | 271 display_manager_.SchedulePaint(view->parent(), old_bounds); |
| 261 display_manager_.SchedulePaint(view->parent(), new_bounds); | 272 display_manager_.SchedulePaint(view->parent(), new_bounds); |
| 262 } | 273 } |
| 263 | 274 |
| 264 void ConnectionManager::OnViewSurfaceIdChanged(const ServerView* view) { | 275 void ConnectionManager::OnViewSurfaceIdChanged(const ServerView* view) { |
| 265 display_manager_.SchedulePaint(view, gfx::Rect(view->bounds().size())); | 276 if (!in_destructor_) |
| 277 display_manager_.SchedulePaint(view, gfx::Rect(view->bounds().size())); |
| 266 } | 278 } |
| 267 | 279 |
| 268 void ConnectionManager::OnViewReordered(const ServerView* view, | 280 void ConnectionManager::OnViewReordered(const ServerView* view, |
| 269 const ServerView* relative, | 281 const ServerView* relative, |
| 270 OrderDirection direction) { | 282 OrderDirection direction) { |
| 271 display_manager_.SchedulePaint(view, gfx::Rect(view->bounds().size())); | 283 if (!in_destructor_) |
| 284 display_manager_.SchedulePaint(view, gfx::Rect(view->bounds().size())); |
| 272 } | 285 } |
| 273 | 286 |
| 274 void ConnectionManager::OnWillChangeViewVisibility(const ServerView* view) { | 287 void ConnectionManager::OnWillChangeViewVisibility(const ServerView* view) { |
| 288 if (in_destructor_) |
| 289 return; |
| 290 |
| 275 for (ConnectionMap::iterator i = connection_map_.begin(); | 291 for (ConnectionMap::iterator i = connection_map_.begin(); |
| 276 i != connection_map_.end(); | 292 i != connection_map_.end(); |
| 277 ++i) { | 293 ++i) { |
| 278 i->second->ProcessWillChangeViewVisibility(view, IsChangeSource(i->first)); | 294 i->second->ProcessWillChangeViewVisibility(view, IsChangeSource(i->first)); |
| 279 } | 295 } |
| 280 } | 296 } |
| 281 | 297 |
| 282 void ConnectionManager::OnViewPropertyChanged( | 298 void ConnectionManager::OnViewPropertyChanged( |
| 283 const ServerView* view, | 299 const ServerView* view, |
| 284 const std::string& name, | 300 const std::string& name, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 297 if (!connection) | 313 if (!connection) |
| 298 connection = GetConnection(view_id.connection_id); | 314 connection = GetConnection(view_id.connection_id); |
| 299 if (connection) { | 315 if (connection) { |
| 300 connection->client()->OnViewInputEvent( | 316 connection->client()->OnViewInputEvent( |
| 301 transport_view_id, event.Pass(), base::Bind(&base::DoNothing)); | 317 transport_view_id, event.Pass(), base::Bind(&base::DoNothing)); |
| 302 } | 318 } |
| 303 } | 319 } |
| 304 | 320 |
| 305 void ConnectionManager::Create(ApplicationConnection* connection, | 321 void ConnectionManager::Create(ApplicationConnection* connection, |
| 306 InterfaceRequest<ViewManagerService> request) { | 322 InterfaceRequest<ViewManagerService> request) { |
| 307 // TODO(sky): If we lose this connection we should tear down. | 323 if (window_manager_vm_service_) { |
| 308 ViewManagerServiceImpl* service = | 324 VLOG(1) << "ViewManager interface requested more than once."; |
| 325 return; |
| 326 } |
| 327 |
| 328 window_manager_vm_service_ = |
| 309 new ViewManagerServiceImpl(this, | 329 new ViewManagerServiceImpl(this, |
| 310 kInvalidConnectionId, | 330 kInvalidConnectionId, |
| 311 std::string(), | 331 std::string(), |
| 312 std::string("mojo:window_manager"), | 332 std::string("mojo:window_manager"), |
| 313 RootViewId(), | 333 RootViewId(), |
| 314 InterfaceRequest<ServiceProvider>()); | 334 InterfaceRequest<ServiceProvider>()); |
| 315 BindToRequest(service, &request); | 335 AddConnection(window_manager_vm_service_); |
| 336 WeakBindToRequest(window_manager_vm_service_, &request); |
| 316 } | 337 } |
| 317 | 338 |
| 318 void ConnectionManager::OnConnectionError() { | 339 void ConnectionManager::OnConnectionError() { |
| 319 // We've lost the connection to the WindowManager. | 340 delegate_->OnLostConnectionToWindowManager(); |
| 320 } | 341 } |
| 321 | 342 |
| 322 } // namespace service | 343 } // namespace service |
| 323 } // namespace mojo | 344 } // namespace mojo |
| OLD | NEW |