Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 5 | #include "services/ui/view_manager/view_state.h" |
Jeff Brown | 711b2f1 | 2016-01-26 15:51:21 -0800 | [diff] [blame] | 6 | |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 7 | #include "base/bind.h" |
Jeff Brown | 711b2f1 | 2016-01-26 15:51:21 -0800 | [diff] [blame] | 8 | #include "base/logging.h" |
| 9 | #include "base/strings/stringprintf.h" |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 10 | #include "services/ui/view_manager/view_host_impl.h" |
| 11 | #include "services/ui/view_manager/view_registry.h" |
| 12 | #include "services/ui/view_manager/view_stub.h" |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 13 | |
| 14 | namespace view_manager { |
| 15 | |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 16 | ViewState::ViewState( |
| 17 | ViewRegistry* registry, |
| 18 | mojo::ui::ViewPtr view, |
| 19 | mojo::ui::ViewTokenPtr view_token, |
| 20 | mojo::InterfaceRequest<mojo::ui::ViewHost> view_host_request, |
| 21 | const std::string& label) |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 22 | : view_(view.Pass()), |
Jeff Brown | 711b2f1 | 2016-01-26 15:51:21 -0800 | [diff] [blame] | 23 | view_token_(view_token.Pass()), |
| 24 | label_(label), |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 25 | impl_(new ViewHostImpl(registry, this)), |
| 26 | host_binding_(impl_.get(), view_host_request.Pass()), |
| 27 | owner_binding_(impl_.get()), |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 28 | weak_factory_(this) { |
| 29 | DCHECK(view_); |
Jeff Brown | 711b2f1 | 2016-01-26 15:51:21 -0800 | [diff] [blame] | 30 | DCHECK(view_token_); |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 31 | |
| 32 | view_.set_connection_error_handler( |
| 33 | base::Bind(&ViewRegistry::OnViewDied, base::Unretained(registry), |
| 34 | base::Unretained(this), "View connection closed")); |
| 35 | host_binding_.set_connection_error_handler( |
| 36 | base::Bind(&ViewRegistry::OnViewDied, base::Unretained(registry), |
| 37 | base::Unretained(this), "ViewHost connection closed")); |
| 38 | owner_binding_.set_connection_error_handler( |
| 39 | base::Bind(&ViewRegistry::OnViewDied, base::Unretained(registry), |
| 40 | base::Unretained(this), "ViewOwner connection closed")); |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | ViewState::~ViewState() {} |
| 44 | |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 45 | void ViewState::LinkChild(uint32_t key, std::unique_ptr<ViewStub> child) { |
| 46 | DCHECK(children_.find(key) == children_.end()); |
| 47 | DCHECK(child); |
| 48 | DCHECK(!child->is_linked()); |
| 49 | |
| 50 | child->SetParent(this, key); |
| 51 | children_.emplace(key, std::move(child)); |
| 52 | } |
| 53 | |
| 54 | std::unique_ptr<ViewStub> ViewState::UnlinkChild(uint32_t key) { |
| 55 | auto child_it = children_.find(key); |
| 56 | DCHECK(child_it != children_.end()); |
| 57 | std::unique_ptr<ViewStub> child(std::move(child_it->second)); |
| 58 | child->Unlink(); |
| 59 | children_.erase(child_it); |
| 60 | children_needing_layout_.erase(child->key()); |
| 61 | return child; |
| 62 | } |
| 63 | |
| 64 | std::vector<std::unique_ptr<ViewStub>> ViewState::UnlinkAllChildren() { |
| 65 | std::vector<std::unique_ptr<ViewStub>> stubs; |
| 66 | for (auto& pair : children_) { |
| 67 | pair.second->Unlink(); |
| 68 | stubs.push_back(std::move(pair.second)); |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 69 | } |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 70 | children_.clear(); |
| 71 | children_needing_layout_.clear(); |
| 72 | return stubs; |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 75 | void ViewState::BindOwner( |
| 76 | mojo::InterfaceRequest<mojo::ui::ViewOwner> view_owner_request) { |
| 77 | DCHECK(!owner_binding_.is_bound()); |
| 78 | owner_binding_.Bind(view_owner_request.Pass()); |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 81 | void ViewState::ReleaseOwner() { |
| 82 | DCHECK(owner_binding_.is_bound()); |
| 83 | owner_binding_.Close(); |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jeff Brown | 711b2f1 | 2016-01-26 15:51:21 -0800 | [diff] [blame] | 86 | mojo::ui::ViewLayoutInfoPtr ViewState::CreateLayoutInfo() { |
| 87 | if (!layout_result_ || !scene_token_) |
| 88 | return nullptr; |
| 89 | |
| 90 | auto info = mojo::ui::ViewLayoutInfo::New(); |
| 91 | info->size = layout_result_->size.Clone(); |
| 92 | info->scene_token = scene_token_.Clone(); |
| 93 | return info; |
| 94 | } |
| 95 | |
Jeff Brown | 83365b1 | 2016-02-10 11:08:14 -0800 | [diff] [blame^] | 96 | const std::string& ViewState::FormattedLabel() const { |
Jeff Brown | 711b2f1 | 2016-01-26 15:51:21 -0800 | [diff] [blame] | 97 | if (formatted_label_cache_.empty()) { |
| 98 | formatted_label_cache_ = |
| 99 | label_.empty() |
| 100 | ? base::StringPrintf("<%d>", view_token_->value) |
| 101 | : base::StringPrintf("<%d:%s>", view_token_->value, label_.c_str()); |
| 102 | } |
| 103 | return formatted_label_cache_; |
| 104 | } |
| 105 | |
| 106 | std::ostream& operator<<(std::ostream& os, ViewState* view_state) { |
| 107 | if (!view_state) |
| 108 | return os << "null"; |
| 109 | return os << view_state->FormattedLabel(); |
| 110 | } |
Jeff Brown | 0b1c404 | 2015-10-27 18:45:58 -0700 | [diff] [blame] | 111 | |
| 112 | } // namespace view_manager |