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