| 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/public/cpp/view_manager/lib/view_manager_client_impl.h" | 5 #include "mojo/services/view_manager/public/cpp/lib/view_manager_client_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "mojo/public/cpp/application/application_impl.h" | 10 #include "mojo/public/cpp/application/application_impl.h" |
| 11 #include "mojo/public/cpp/application/connect.h" | 11 #include "mojo/public/cpp/application/connect.h" |
| 12 #include "mojo/public/cpp/application/service_provider_impl.h" | 12 #include "mojo/public/cpp/application/service_provider_impl.h" |
| 13 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 13 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
| 14 #include "mojo/public/interfaces/application/shell.mojom.h" | 14 #include "mojo/public/interfaces/application/shell.mojom.h" |
| 15 #include "mojo/services/public/cpp/view_manager/lib/view_private.h" | 15 #include "mojo/services/view_manager/public/cpp/lib/view_private.h" |
| 16 #include "mojo/services/public/cpp/view_manager/util.h" | 16 #include "mojo/services/view_manager/public/cpp/util.h" |
| 17 #include "mojo/services/public/cpp/view_manager/view_manager_delegate.h" | 17 #include "mojo/services/view_manager/public/cpp/view_manager_delegate.h" |
| 18 #include "mojo/services/public/cpp/view_manager/view_observer.h" | 18 #include "mojo/services/view_manager/public/cpp/view_observer.h" |
| 19 | 19 |
| 20 namespace mojo { | 20 namespace mojo { |
| 21 | 21 |
| 22 Id MakeTransportId(ConnectionSpecificId connection_id, | 22 Id MakeTransportId(ConnectionSpecificId connection_id, |
| 23 ConnectionSpecificId local_id) { | 23 ConnectionSpecificId local_id) { |
| 24 return (connection_id << 16) | local_id; | 24 return (connection_id << 16) | local_id; |
| 25 } | 25 } |
| 26 | 26 |
| 27 // Helper called to construct a local view object from transport data. | 27 // Helper called to construct a local view object from transport data. |
| 28 View* AddViewToViewManager(ViewManagerClientImpl* client, | 28 View* AddViewToViewManager(ViewManagerClientImpl* client, |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 class RootObserver : public ViewObserver { | 74 class RootObserver : public ViewObserver { |
| 75 public: | 75 public: |
| 76 explicit RootObserver(View* root) : root_(root) {} | 76 explicit RootObserver(View* root) : root_(root) {} |
| 77 ~RootObserver() override {} | 77 ~RootObserver() override {} |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 // Overridden from ViewObserver: | 80 // Overridden from ViewObserver: |
| 81 void OnViewDestroyed(View* view) override { | 81 void OnViewDestroyed(View* view) override { |
| 82 DCHECK_EQ(view, root_); | 82 DCHECK_EQ(view, root_); |
| 83 static_cast<ViewManagerClientImpl*>( | 83 static_cast<ViewManagerClientImpl*>( |
| 84 ViewPrivate(root_).view_manager())->RemoveRoot(root_); | 84 ViewPrivate(root_).view_manager())->RootDestroyed(root_); |
| 85 view->RemoveObserver(this); | 85 view->RemoveObserver(this); |
| 86 delete this; | 86 delete this; |
| 87 } | 87 } |
| 88 | 88 |
| 89 View* root_; | 89 View* root_; |
| 90 | 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(RootObserver); | 91 DISALLOW_COPY_AND_ASSIGN(RootObserver); |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 ViewManagerClientImpl::ViewManagerClientImpl(ViewManagerDelegate* delegate, | 94 ViewManagerClientImpl::ViewManagerClientImpl(ViewManagerDelegate* delegate, |
| 95 Shell* shell, | 95 Shell* shell, |
| 96 ScopedMessagePipeHandle handle, | 96 ScopedMessagePipeHandle handle, |
| 97 bool delete_on_error) | 97 bool delete_on_error) |
| 98 : connected_(false), | 98 : connected_(false), |
| 99 connection_id_(0), | 99 connection_id_(0), |
| 100 next_id_(1), | 100 next_id_(1), |
| 101 delegate_(delegate), | 101 delegate_(delegate), |
| 102 root_(nullptr), |
| 103 focused_view_(nullptr), |
| 102 binding_(this, handle.Pass()), | 104 binding_(this, handle.Pass()), |
| 103 service_(binding_.client()), | 105 service_(binding_.client()), |
| 104 delete_on_error_(delete_on_error) { | 106 delete_on_error_(delete_on_error) { |
| 105 } | 107 } |
| 106 | 108 |
| 107 ViewManagerClientImpl::~ViewManagerClientImpl() { | 109 ViewManagerClientImpl::~ViewManagerClientImpl() { |
| 108 std::vector<View*> non_owned; | 110 std::vector<View*> non_owned; |
| 109 while (!views_.empty()) { | 111 while (!views_.empty()) { |
| 110 IdToViewMap::iterator it = views_.begin(); | 112 IdToViewMap::iterator it = views_.begin(); |
| 111 if (OwnsView(it->second->id())) { | 113 if (OwnsView(it->second->id())) { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 views_.erase(it); | 225 views_.erase(it); |
| 224 } | 226 } |
| 225 | 227 |
| 226 //////////////////////////////////////////////////////////////////////////////// | 228 //////////////////////////////////////////////////////////////////////////////// |
| 227 // ViewManagerClientImpl, ViewManager implementation: | 229 // ViewManagerClientImpl, ViewManager implementation: |
| 228 | 230 |
| 229 const std::string& ViewManagerClientImpl::GetEmbedderURL() const { | 231 const std::string& ViewManagerClientImpl::GetEmbedderURL() const { |
| 230 return creator_url_; | 232 return creator_url_; |
| 231 } | 233 } |
| 232 | 234 |
| 233 const std::vector<View*>& ViewManagerClientImpl::GetRoots() const { | 235 View* ViewManagerClientImpl::GetRoot() { |
| 234 return roots_; | 236 return root_; |
| 235 } | 237 } |
| 236 | 238 |
| 237 View* ViewManagerClientImpl::GetViewById(Id id) { | 239 View* ViewManagerClientImpl::GetViewById(Id id) { |
| 238 IdToViewMap::const_iterator it = views_.find(id); | 240 IdToViewMap::const_iterator it = views_.find(id); |
| 239 return it != views_.end() ? it->second : NULL; | 241 return it != views_.end() ? it->second : NULL; |
| 240 } | 242 } |
| 241 | 243 |
| 244 View* ViewManagerClientImpl::GetFocusedView() { |
| 245 return focused_view_; |
| 246 } |
| 247 |
| 242 //////////////////////////////////////////////////////////////////////////////// | 248 //////////////////////////////////////////////////////////////////////////////// |
| 243 // ViewManagerClientImpl, ViewManagerClient implementation: | 249 // ViewManagerClientImpl, ViewManagerClient implementation: |
| 244 | 250 |
| 245 void ViewManagerClientImpl::OnEmbed( | 251 void ViewManagerClientImpl::OnEmbed( |
| 246 ConnectionSpecificId connection_id, | 252 ConnectionSpecificId connection_id, |
| 247 const String& creator_url, | 253 const String& creator_url, |
| 248 ViewDataPtr root_data, | 254 ViewDataPtr root_data, |
| 249 InterfaceRequest<ServiceProvider> parent_services, | 255 InterfaceRequest<ServiceProvider> parent_services, |
| 250 ScopedMessagePipeHandle window_manager_pipe) { | 256 ScopedMessagePipeHandle window_manager_pipe) { |
| 251 if (!connected_) { | 257 DCHECK(!connected_); |
| 252 connected_ = true; | 258 connected_ = true; |
| 253 connection_id_ = connection_id; | 259 connection_id_ = connection_id; |
| 254 creator_url_ = String::From(creator_url); | 260 creator_url_ = String::From(creator_url); |
| 255 } else { | |
| 256 DCHECK_EQ(connection_id_, connection_id); | |
| 257 DCHECK_EQ(creator_url_, creator_url); | |
| 258 } | |
| 259 | 261 |
| 260 // A new root must not already exist as a root or be contained by an existing | 262 DCHECK(!root_); |
| 261 // hierarchy visible to this view manager. | 263 root_ = AddViewToViewManager(this, nullptr, root_data); |
| 262 View* root = AddViewToViewManager(this, NULL, root_data); | 264 root_->AddObserver(new RootObserver(root_)); |
| 263 roots_.push_back(root); | |
| 264 root->AddObserver(new RootObserver(root)); | |
| 265 | 265 |
| 266 ServiceProviderImpl* exported_services = nullptr; | 266 ServiceProviderImpl* exported_services = nullptr; |
| 267 scoped_ptr<ServiceProvider> remote; | 267 scoped_ptr<ServiceProvider> remote; |
| 268 | 268 |
| 269 if (parent_services.is_pending()) { | 269 if (parent_services.is_pending()) { |
| 270 // BindToRequest() binds the lifetime of |exported_services| to the pipe. | 270 // BindToRequest() binds the lifetime of |exported_services| to the pipe. |
| 271 exported_services = new ServiceProviderImpl; | 271 exported_services = new ServiceProviderImpl; |
| 272 BindToRequest(exported_services, &parent_services); | 272 BindToRequest(exported_services, &parent_services); |
| 273 remote.reset(exported_services->CreateRemoteServiceProvider()); | 273 remote.reset(exported_services->CreateRemoteServiceProvider()); |
| 274 } | 274 } |
| 275 window_manager_.Bind(window_manager_pipe.Pass()); | 275 window_manager_.Bind(window_manager_pipe.Pass()); |
| 276 window_manager_.set_client(this); | 276 window_manager_.set_client(this); |
| 277 delegate_->OnEmbed(this, root, exported_services, remote.Pass()); | 277 // base::Unretained() is safe here as |window_manager_| is bound to our |
| 278 // lifetime. |
| 279 window_manager_->GetFocusedAndActiveViews( |
| 280 base::Bind(&ViewManagerClientImpl::OnGotFocusedAndActiveViews, |
| 281 base::Unretained(this))); |
| 282 delegate_->OnEmbed(this, root_, exported_services, remote.Pass()); |
| 278 } | 283 } |
| 279 | 284 |
| 280 void ViewManagerClientImpl::OnEmbeddedAppDisconnected(Id view_id) { | 285 void ViewManagerClientImpl::OnEmbeddedAppDisconnected(Id view_id) { |
| 281 View* view = GetViewById(view_id); | 286 View* view = GetViewById(view_id); |
| 282 if (view) { | 287 if (view) { |
| 283 FOR_EACH_OBSERVER(ViewObserver, *ViewPrivate(view).observers(), | 288 FOR_EACH_OBSERVER(ViewObserver, *ViewPrivate(view).observers(), |
| 284 OnViewEmbeddedAppDisconnected(view)); | 289 OnViewEmbeddedAppDisconnected(view)); |
| 285 } | 290 } |
| 286 } | 291 } |
| 287 | 292 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 | 384 |
| 380 void ViewManagerClientImpl::OnFocusChanged(Id old_focused_view_id, | 385 void ViewManagerClientImpl::OnFocusChanged(Id old_focused_view_id, |
| 381 Id new_focused_view_id) { | 386 Id new_focused_view_id) { |
| 382 View* focused = GetViewById(new_focused_view_id); | 387 View* focused = GetViewById(new_focused_view_id); |
| 383 View* blurred = GetViewById(old_focused_view_id); | 388 View* blurred = GetViewById(old_focused_view_id); |
| 384 if (blurred) { | 389 if (blurred) { |
| 385 FOR_EACH_OBSERVER(ViewObserver, | 390 FOR_EACH_OBSERVER(ViewObserver, |
| 386 *ViewPrivate(blurred).observers(), | 391 *ViewPrivate(blurred).observers(), |
| 387 OnViewFocusChanged(focused, blurred)); | 392 OnViewFocusChanged(focused, blurred)); |
| 388 } | 393 } |
| 394 focused_view_ = focused; |
| 389 if (focused) { | 395 if (focused) { |
| 390 FOR_EACH_OBSERVER(ViewObserver, | 396 FOR_EACH_OBSERVER(ViewObserver, |
| 391 *ViewPrivate(focused).observers(), | 397 *ViewPrivate(focused).observers(), |
| 392 OnViewFocusChanged(focused, blurred)); | 398 OnViewFocusChanged(focused, blurred)); |
| 393 } | 399 } |
| 394 } | 400 } |
| 395 | 401 |
| 396 void ViewManagerClientImpl::OnActiveWindowChanged(Id old_focused_window, | 402 void ViewManagerClientImpl::OnActiveWindowChanged(Id old_focused_window, |
| 397 Id new_focused_window) {} | 403 Id new_focused_window) {} |
| 398 | 404 |
| 399 //////////////////////////////////////////////////////////////////////////////// | 405 //////////////////////////////////////////////////////////////////////////////// |
| 400 // OnConnectionError, private: | 406 // OnConnectionError, private: |
| 401 void ViewManagerClientImpl::OnConnectionError() { | 407 void ViewManagerClientImpl::OnConnectionError() { |
| 402 if (delete_on_error_) | 408 if (delete_on_error_) |
| 403 delete this; | 409 delete this; |
| 404 } | 410 } |
| 405 | 411 |
| 406 //////////////////////////////////////////////////////////////////////////////// | 412 //////////////////////////////////////////////////////////////////////////////// |
| 407 // ViewManagerClientImpl, private: | 413 // ViewManagerClientImpl, private: |
| 408 | 414 |
| 409 void ViewManagerClientImpl::RemoveRoot(View* root) { | 415 void ViewManagerClientImpl::RootDestroyed(View* root) { |
| 410 std::vector<View*>::iterator it = | 416 DCHECK_EQ(root, root_); |
| 411 std::find(roots_.begin(), roots_.end(), root); | 417 root_ = nullptr; |
| 412 if (it != roots_.end()) | |
| 413 roots_.erase(it); | |
| 414 } | 418 } |
| 415 | 419 |
| 416 void ViewManagerClientImpl::OnActionCompleted(bool success) { | 420 void ViewManagerClientImpl::OnActionCompleted(bool success) { |
| 417 if (!change_acked_callback_.is_null()) | 421 if (!change_acked_callback_.is_null()) |
| 418 change_acked_callback_.Run(); | 422 change_acked_callback_.Run(); |
| 419 } | 423 } |
| 420 | 424 |
| 421 void ViewManagerClientImpl::OnActionCompletedWithErrorCode(ErrorCode code) { | 425 void ViewManagerClientImpl::OnActionCompletedWithErrorCode(ErrorCode code) { |
| 422 OnActionCompleted(code == ERROR_CODE_NONE); | 426 OnActionCompleted(code == ERROR_CODE_NONE); |
| 423 } | 427 } |
| 424 | 428 |
| 425 base::Callback<void(bool)> ViewManagerClientImpl::ActionCompletedCallback() { | 429 base::Callback<void(bool)> ViewManagerClientImpl::ActionCompletedCallback() { |
| 426 return base::Bind(&ViewManagerClientImpl::OnActionCompleted, | 430 return base::Bind(&ViewManagerClientImpl::OnActionCompleted, |
| 427 base::Unretained(this)); | 431 base::Unretained(this)); |
| 428 } | 432 } |
| 429 | 433 |
| 430 base::Callback<void(ErrorCode)> | 434 base::Callback<void(ErrorCode)> |
| 431 ViewManagerClientImpl::ActionCompletedCallbackWithErrorCode() { | 435 ViewManagerClientImpl::ActionCompletedCallbackWithErrorCode() { |
| 432 return base::Bind(&ViewManagerClientImpl::OnActionCompletedWithErrorCode, | 436 return base::Bind(&ViewManagerClientImpl::OnActionCompletedWithErrorCode, |
| 433 base::Unretained(this)); | 437 base::Unretained(this)); |
| 434 } | 438 } |
| 435 | 439 |
| 440 void ViewManagerClientImpl::OnGotFocusedAndActiveViews(uint32 focused_view_id, |
| 441 uint32 active_view_id) { |
| 442 if (GetViewById(focused_view_id) != focused_view_) |
| 443 OnFocusChanged(focused_view_ ? focused_view_->id() : 0, focused_view_id); |
| 444 } |
| 445 |
| 436 } // namespace mojo | 446 } // namespace mojo |
| OLD | NEW |