Chromium Code Reviews| 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 "base/stl_util.h" |
| 9 #include "mojo/converters/geometry/geometry_type_converters.h" | 9 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 10 #include "mojo/converters/input_events/input_events_type_converters.h" | 10 #include "mojo/converters/input_events/input_events_type_converters.h" |
| 11 #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/client_connection.h" | 12 #include "mojo/services/view_manager/client_connection.h" |
| 13 #include "mojo/services/view_manager/connection_manager_delegate.h" | 13 #include "mojo/services/view_manager/connection_manager_delegate.h" |
| 14 #include "mojo/services/view_manager/display_manager.h" | 14 #include "mojo/services/view_manager/display_manager.h" |
| 15 #include "mojo/services/view_manager/server_view.h" | |
| 16 #include "mojo/services/view_manager/view_coordinate_conversions.h" | |
| 15 #include "mojo/services/view_manager/view_manager_service_impl.h" | 17 #include "mojo/services/view_manager/view_manager_service_impl.h" |
| 16 | 18 |
| 17 namespace mojo { | 19 namespace mojo { |
| 18 namespace service { | 20 namespace service { |
| 21 namespace { | |
| 22 | |
| 23 // Creates a copy of |view|. The copied view has |delegate| as its delegate. | |
| 24 // This does not recurse. | |
| 25 ServerView* CloneView(const ServerView* view, ServerViewDelegate* delegate) { | |
| 26 ServerView* clone = new ServerView(delegate, ClonedViewId()); | |
|
msw
2014/11/18 23:37:42
q: should the clone copy any/all ServerView::prope
sky
2014/11/19 00:49:39
Yes, I missed opacity. No need to visibility as we
msw
2014/11/19 01:27:51
Good catch, but I was wondering about the std::map
sky
2014/11/19 03:28:17
In theory those shouldn't matter as the only possi
msw
2014/11/19 18:18:29
Acknowledged.
| |
| 27 clone->SetBounds(view->bounds()); | |
| 28 clone->SetSurfaceId(view->surface_id()); | |
| 29 return clone; | |
| 30 } | |
| 31 | |
| 32 // Creates copies of all the visible children of |parent|. Newly cloned views | |
| 33 // are | |
|
msw
2014/11/18 23:37:42
nit: fix line break
sky
2014/11/19 00:49:39
Done.
| |
| 34 // added to |cloned_parent| and have |delegate| as their delegate. | |
| 35 void CloneAllViews(const ServerView* parent, | |
|
msw
2014/11/18 23:37:42
nit: CloneViewTree?
sky
2014/11/19 00:49:39
Done.
| |
| 36 ServerView* cloned_parent, | |
| 37 ServerViewDelegate* delegate) { | |
| 38 DCHECK(parent->visible()); | |
| 39 for (const ServerView* to_clone : parent->GetChildren()) { | |
|
msw
2014/11/18 23:37:42
q: will cloned views preserve the ordering of the
sky
2014/11/19 00:49:39
Yes. I added a comment indicating this.
| |
| 40 if (to_clone->visible()) { | |
| 41 ServerView* cloned = CloneView(to_clone, delegate); | |
| 42 cloned_parent->Add(cloned); | |
| 43 CloneAllViews(to_clone, cloned, delegate); | |
| 44 } | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 // Recurses through all the children of |view| moving any cloned views to | |
| 49 // |add_to| stacked above |stack_above|. |stack_above| is updated as views are | |
| 50 // moved. | |
| 51 void ReparentClonedViews(ServerView* add_to, | |
|
msw
2014/11/18 23:37:41
nit: s/add_to/new_parent/?
sky
2014/11/19 00:49:39
Done.
| |
| 52 ServerView** stack_above, | |
| 53 ServerView* view) { | |
| 54 if (view->id() == ClonedViewId()) { | |
| 55 const gfx::Rect new_bounds(ConvertRectBetweenViews( | |
| 56 view, add_to, gfx::Rect(view->bounds().size()))); | |
| 57 add_to->Add(view); | |
| 58 add_to->Reorder(view, *stack_above, ORDER_DIRECTION_ABOVE); | |
| 59 view->SetBounds(new_bounds); | |
| 60 *stack_above = view; | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 for (ServerView* child : view->GetChildren()) | |
| 65 ReparentClonedViews(add_to, stack_above, child); | |
| 66 } | |
| 67 | |
| 68 // Deletes |view| and all its descendants. | |
| 69 void DeleteAllViews(ServerView* view) { | |
|
msw
2014/11/18 23:37:41
nit: DeleteViewTree?
sky
2014/11/19 00:49:39
Done.
| |
| 70 for (ServerView* child : view->GetChildren()) | |
| 71 DeleteAllViews(child); | |
| 72 | |
| 73 delete view; | |
| 74 } | |
| 75 | |
| 76 // TODO(sky): nuke, proof of concept. | |
| 77 bool DecrementAnimatingViewsOpacity(ServerView* view) { | |
| 78 if (view->id() == ClonedViewId()) { | |
| 79 const float new_opacity = view->opacity() - .05f; | |
| 80 if (new_opacity <= 0) | |
| 81 DeleteAllViews(view); | |
| 82 else | |
| 83 view->SetOpacity(new_opacity); | |
| 84 return true; | |
| 85 } | |
| 86 bool ret_value = false; | |
| 87 for (ServerView* child : view->GetChildren()) { | |
| 88 if (DecrementAnimatingViewsOpacity(child)) | |
| 89 ret_value = true; | |
| 90 } | |
| 91 return ret_value; | |
| 92 } | |
| 93 | |
| 94 } // namespace | |
| 19 | 95 |
| 20 ConnectionManager::ScopedChange::ScopedChange( | 96 ConnectionManager::ScopedChange::ScopedChange( |
| 21 ViewManagerServiceImpl* connection, | 97 ViewManagerServiceImpl* connection, |
| 22 ConnectionManager* connection_manager, | 98 ConnectionManager* connection_manager, |
| 23 bool is_delete_view) | 99 bool is_delete_view) |
| 24 : connection_manager_(connection_manager), | 100 : connection_manager_(connection_manager), |
| 25 connection_id_(connection->id()), | 101 connection_id_(connection->id()), |
| 26 is_delete_view_(is_delete_view) { | 102 is_delete_view_(is_delete_view) { |
| 27 connection_manager_->PrepareForChange(this); | 103 connection_manager_->PrepareForChange(this); |
| 28 } | 104 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 | 151 |
| 76 // Notify remaining connections so that they can cleanup. | 152 // Notify remaining connections so that they can cleanup. |
| 77 for (auto& pair : connection_map_) { | 153 for (auto& pair : connection_map_) { |
| 78 pair.second->service()->OnWillDestroyViewManagerServiceImpl( | 154 pair.second->service()->OnWillDestroyViewManagerServiceImpl( |
| 79 connection->service()); | 155 connection->service()); |
| 80 } | 156 } |
| 81 } | 157 } |
| 82 | 158 |
| 83 void ConnectionManager::EmbedAtView( | 159 void ConnectionManager::EmbedAtView( |
| 84 ConnectionSpecificId creator_id, | 160 ConnectionSpecificId creator_id, |
| 85 const String& url, | 161 const std::string& url, |
| 86 Id transport_view_id, | 162 const ViewId& view_id, |
| 87 InterfaceRequest<ServiceProvider> service_provider) { | 163 InterfaceRequest<ServiceProvider> service_provider) { |
| 88 std::string creator_url; | 164 std::string creator_url; |
| 89 ConnectionMap::const_iterator it = connection_map_.find(creator_id); | 165 ConnectionMap::const_iterator it = connection_map_.find(creator_id); |
| 90 if (it != connection_map_.end()) | 166 if (it != connection_map_.end()) |
| 91 creator_url = it->second->service()->url(); | 167 creator_url = it->second->service()->url(); |
| 92 | 168 |
| 93 ClientConnection* client_connection = | 169 ClientConnection* client_connection = |
| 94 delegate_->CreateClientConnectionForEmbedAtView( | 170 delegate_->CreateClientConnectionForEmbedAtView( |
| 95 this, creator_id, creator_url, url.To<std::string>(), | 171 this, creator_id, creator_url, url, view_id); |
| 96 ViewIdFromTransportId(transport_view_id)); | |
| 97 AddConnection(client_connection); | 172 AddConnection(client_connection); |
| 98 client_connection->service()->Init(client_connection->client(), | 173 client_connection->service()->Init(client_connection->client(), |
| 99 service_provider.Pass()); | 174 service_provider.Pass()); |
| 100 OnConnectionMessagedClient(client_connection->service()->id()); | 175 OnConnectionMessagedClient(client_connection->service()->id()); |
| 101 } | 176 } |
| 102 | 177 |
| 103 ViewManagerServiceImpl* ConnectionManager::GetConnection( | 178 ViewManagerServiceImpl* ConnectionManager::GetConnection( |
| 104 ConnectionSpecificId connection_id) { | 179 ConnectionSpecificId connection_id) { |
| 105 ConnectionMap::iterator i = connection_map_.find(connection_id); | 180 ConnectionMap::iterator i = connection_map_.find(connection_id); |
| 106 return i == connection_map_.end() ? nullptr : i->second->service(); | 181 return i == connection_map_.end() ? nullptr : i->second->service(); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 135 void ConnectionManager::SetWindowManagerClientConnection( | 210 void ConnectionManager::SetWindowManagerClientConnection( |
| 136 scoped_ptr<ClientConnection> connection) { | 211 scoped_ptr<ClientConnection> connection) { |
| 137 CHECK(!window_manager_client_connection_); | 212 CHECK(!window_manager_client_connection_); |
| 138 window_manager_client_connection_ = connection.release(); | 213 window_manager_client_connection_ = connection.release(); |
| 139 AddConnection(window_manager_client_connection_); | 214 AddConnection(window_manager_client_connection_); |
| 140 window_manager_client_connection_->service()->Init( | 215 window_manager_client_connection_->service()->Init( |
| 141 window_manager_client_connection_->client(), | 216 window_manager_client_connection_->client(), |
| 142 InterfaceRequest<ServiceProvider>()); | 217 InterfaceRequest<ServiceProvider>()); |
| 143 } | 218 } |
| 144 | 219 |
| 220 bool ConnectionManager::CloneAndAnimate(const ViewId& view_id) { | |
| 221 ServerView* view = GetView(view_id); | |
| 222 if (!view || !view->IsDrawn(root_.get()) || view == root_.get()) | |
| 223 return false; | |
| 224 if (!animation_timer_.IsRunning()) { | |
| 225 animation_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(100), | |
| 226 this, &ConnectionManager::DoAnimation); | |
| 227 } | |
| 228 ServerView* clone = CloneView(view, this); | |
| 229 CloneAllViews(view, clone, this); | |
| 230 view->parent()->Add(clone); | |
| 231 view->parent()->Reorder(clone, view, ORDER_DIRECTION_ABOVE); | |
| 232 return true; | |
| 233 } | |
| 234 | |
| 145 void ConnectionManager::ProcessViewBoundsChanged(const ServerView* view, | 235 void ConnectionManager::ProcessViewBoundsChanged(const ServerView* view, |
| 146 const gfx::Rect& old_bounds, | 236 const gfx::Rect& old_bounds, |
| 147 const gfx::Rect& new_bounds) { | 237 const gfx::Rect& new_bounds) { |
| 148 for (auto& pair : connection_map_) { | 238 for (auto& pair : connection_map_) { |
| 149 pair.second->service()->ProcessViewBoundsChanged( | 239 pair.second->service()->ProcessViewBoundsChanged( |
| 150 view, old_bounds, new_bounds, IsChangeSource(pair.first)); | 240 view, old_bounds, new_bounds, IsChangeSource(pair.first)); |
| 151 } | 241 } |
| 152 } | 242 } |
| 153 | 243 |
| 154 void ConnectionManager::ProcessWillChangeViewHierarchy( | 244 void ConnectionManager::ProcessWillChangeViewHierarchy( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 192 CHECK(!current_change_); | 282 CHECK(!current_change_); |
| 193 current_change_ = change; | 283 current_change_ = change; |
| 194 } | 284 } |
| 195 | 285 |
| 196 void ConnectionManager::FinishChange() { | 286 void ConnectionManager::FinishChange() { |
| 197 // PrepareForChange/FinishChange should be balanced. | 287 // PrepareForChange/FinishChange should be balanced. |
| 198 CHECK(current_change_); | 288 CHECK(current_change_); |
| 199 current_change_ = NULL; | 289 current_change_ = NULL; |
| 200 } | 290 } |
| 201 | 291 |
| 292 void ConnectionManager::DoAnimation() { | |
| 293 if (!DecrementAnimatingViewsOpacity(root())) | |
| 294 animation_timer_.Stop(); | |
| 295 } | |
| 296 | |
| 202 void ConnectionManager::AddConnection(ClientConnection* connection) { | 297 void ConnectionManager::AddConnection(ClientConnection* connection) { |
| 203 DCHECK_EQ(0u, connection_map_.count(connection->service()->id())); | 298 DCHECK_EQ(0u, connection_map_.count(connection->service()->id())); |
| 204 connection_map_[connection->service()->id()] = connection; | 299 connection_map_[connection->service()->id()] = connection; |
| 205 } | 300 } |
| 206 | 301 |
| 302 void ConnectionManager::OnWillDestroyView(ServerView* view) { | |
| 303 if (!in_destructor_ && root_->Contains(view) && view != root_.get() && | |
|
msw
2014/11/18 23:37:42
Add a comment here explaining why this is useful,
| |
| 304 view->id() != ClonedViewId()) { | |
| 305 ServerView* parent_above = view; | |
| 306 ReparentClonedViews(view->parent(), &parent_above, view); | |
|
msw
2014/11/18 23:37:41
Add a comment here explaining why this is useful,
msw
2014/11/18 23:37:42
I worry a bit about potential performance implicat
sky
2014/11/19 00:49:39
The bubbling at most happens for the depth of the
msw
2014/11/19 01:27:51
Acknowledged.
| |
| 307 } | |
| 308 } | |
| 309 | |
| 207 void ConnectionManager::OnViewDestroyed(const ServerView* view) { | 310 void ConnectionManager::OnViewDestroyed(const ServerView* view) { |
| 208 if (!in_destructor_) | 311 if (!in_destructor_) |
| 209 ProcessViewDeleted(view->id()); | 312 ProcessViewDeleted(view->id()); |
| 210 } | 313 } |
| 211 | 314 |
| 212 void ConnectionManager::OnWillChangeViewHierarchy( | 315 void ConnectionManager::OnWillChangeViewHierarchy(ServerView* view, |
| 213 const ServerView* view, | 316 ServerView* new_parent, |
| 214 const ServerView* new_parent, | 317 ServerView* old_parent) { |
| 215 const ServerView* old_parent) { | 318 if (view->id() == ClonedViewId() || in_destructor_) |
| 216 if (!in_destructor_) | 319 return; |
| 217 ProcessWillChangeViewHierarchy(view, new_parent, old_parent); | 320 |
| 321 if (root_->Contains(view) && view != root_.get()) { | |
| 322 ServerView* parent_above = view; | |
| 323 ReparentClonedViews(view->parent(), &parent_above, view); | |
|
msw
2014/11/18 23:37:42
I'm confused; does this reparent cloned views to |
sky
2014/11/19 00:49:39
Added similar comment to above, let me know if sti
msw
2014/11/19 01:27:51
I guess it makes sense, re-parenting an ancestor o
sky
2014/11/19 03:28:17
I think we'll just have to see how this pans out.
msw
2014/11/19 18:18:29
Acknowledged.
| |
| 324 } | |
| 325 | |
| 326 ProcessWillChangeViewHierarchy(view, new_parent, old_parent); | |
| 218 } | 327 } |
| 219 | 328 |
| 220 void ConnectionManager::OnViewHierarchyChanged(const ServerView* view, | 329 void ConnectionManager::OnViewHierarchyChanged(const ServerView* view, |
| 221 const ServerView* new_parent, | 330 const ServerView* new_parent, |
| 222 const ServerView* old_parent) { | 331 const ServerView* old_parent) { |
| 223 if (in_destructor_) | 332 if (in_destructor_) |
| 224 return; | 333 return; |
| 225 | 334 |
| 226 ProcessViewHierarchyChanged(view, new_parent, old_parent); | 335 ProcessViewHierarchyChanged(view, new_parent, old_parent); |
| 227 | 336 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 256 display_manager_->SchedulePaint(view, gfx::Rect(view->bounds().size())); | 365 display_manager_->SchedulePaint(view, gfx::Rect(view->bounds().size())); |
| 257 } | 366 } |
| 258 | 367 |
| 259 void ConnectionManager::OnViewReordered(const ServerView* view, | 368 void ConnectionManager::OnViewReordered(const ServerView* view, |
| 260 const ServerView* relative, | 369 const ServerView* relative, |
| 261 OrderDirection direction) { | 370 OrderDirection direction) { |
| 262 if (!in_destructor_) | 371 if (!in_destructor_) |
| 263 display_manager_->SchedulePaint(view, gfx::Rect(view->bounds().size())); | 372 display_manager_->SchedulePaint(view, gfx::Rect(view->bounds().size())); |
| 264 } | 373 } |
| 265 | 374 |
| 266 void ConnectionManager::OnWillChangeViewVisibility(const ServerView* view) { | 375 void ConnectionManager::OnWillChangeViewVisibility(ServerView* view) { |
| 267 if (in_destructor_) | 376 if (in_destructor_) |
| 268 return; | 377 return; |
| 269 | 378 |
| 379 if (view != root_.get() && view->id() != ClonedViewId() && | |
| 380 root_->Contains(view) && view->IsDrawn(root_.get())) { | |
| 381 // View is going to hide. Promote any cloned views. | |
| 382 ServerView* parent_above = view; | |
| 383 ReparentClonedViews(view->parent(), &parent_above, view); | |
| 384 } | |
| 385 | |
| 270 for (auto& pair : connection_map_) { | 386 for (auto& pair : connection_map_) { |
| 271 pair.second->service()->ProcessWillChangeViewVisibility( | 387 pair.second->service()->ProcessWillChangeViewVisibility( |
| 272 view, IsChangeSource(pair.first)); | 388 view, IsChangeSource(pair.first)); |
| 273 } | 389 } |
| 274 } | 390 } |
| 275 | 391 |
| 276 void ConnectionManager::OnViewSharedPropertyChanged( | 392 void ConnectionManager::OnViewSharedPropertyChanged( |
| 277 const ServerView* view, | 393 const ServerView* view, |
| 278 const std::string& name, | 394 const std::string& name, |
| 279 const std::vector<uint8_t>* new_data) { | 395 const std::vector<uint8_t>* new_data) { |
| 280 for (auto& pair : connection_map_) { | 396 for (auto& pair : connection_map_) { |
| 281 pair.second->service()->ProcessViewPropertyChanged( | 397 pair.second->service()->ProcessViewPropertyChanged( |
| 282 view, name, new_data, IsChangeSource(pair.first)); | 398 view, name, new_data, IsChangeSource(pair.first)); |
| 283 } | 399 } |
| 284 } | 400 } |
| 285 | 401 |
| 286 void ConnectionManager::SetViewportSize(SizePtr size) { | 402 void ConnectionManager::OnScheduleViewPaint(const ServerView* view) { |
| 287 gfx::Size new_size = size.To<gfx::Size>(); | 403 if (!in_destructor_) |
| 288 display_manager_->SetViewportSize(new_size); | 404 display_manager_->SchedulePaint(view, gfx::Rect(view->bounds().size())); |
| 289 } | 405 } |
| 290 | 406 |
| 291 void ConnectionManager::DispatchInputEventToView(Id transport_view_id, | 407 void ConnectionManager::DispatchInputEventToView(Id transport_view_id, |
| 292 EventPtr event) { | 408 EventPtr event) { |
| 293 const ViewId view_id(ViewIdFromTransportId(transport_view_id)); | 409 const ViewId view_id(ViewIdFromTransportId(transport_view_id)); |
| 294 | 410 |
| 295 ViewManagerServiceImpl* connection = GetConnectionWithRoot(view_id); | 411 ViewManagerServiceImpl* connection = GetConnectionWithRoot(view_id); |
| 296 if (!connection) | 412 if (!connection) |
| 297 connection = GetConnection(view_id.connection_id); | 413 connection = GetConnection(view_id.connection_id); |
| 298 if (connection) { | 414 if (connection) { |
| 299 connection->client()->OnViewInputEvent( | 415 connection->client()->OnViewInputEvent( |
| 300 transport_view_id, event.Pass(), base::Bind(&base::DoNothing)); | 416 transport_view_id, event.Pass(), base::Bind(&base::DoNothing)); |
| 301 } | 417 } |
| 302 } | 418 } |
| 303 | 419 |
| 420 void ConnectionManager::SetViewportSize(SizePtr size) { | |
| 421 gfx::Size new_size = size.To<gfx::Size>(); | |
| 422 display_manager_->SetViewportSize(new_size); | |
| 423 } | |
| 424 | |
| 425 void ConnectionManager::CloneAndAnimate(Id transport_view_id, | |
| 426 const Callback<void(bool)>& callback) { | |
| 427 callback.Run(CloneAndAnimate(ViewIdFromTransportId(transport_view_id))); | |
|
msw
2014/11/18 23:37:42
Add a comment describing the expected lifetime of
msw
2014/11/18 23:37:42
optional nit: it might be more straightforward to
sky
2014/11/19 00:49:39
This code is all going to change drastically (see
msw
2014/11/19 01:27:51
Acknowledged.
| |
| 428 } | |
| 429 | |
| 304 } // namespace service | 430 } // namespace service |
| 305 } // namespace mojo | 431 } // namespace mojo |
| OLD | NEW |