OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "services/ui/view_manager/view_stub.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "services/ui/view_manager/view_registry.h" |
| 10 #include "services/ui/view_manager/view_state.h" |
| 11 #include "services/ui/view_manager/view_tree_state.h" |
| 12 |
| 13 namespace view_manager { |
| 14 |
| 15 ViewStub::ViewStub(ViewRegistry* registry, mojo::ui::ViewOwnerPtr owner) |
| 16 : registry_(registry), owner_(owner.Pass()) { |
| 17 DCHECK(registry_); |
| 18 DCHECK(owner_); |
| 19 |
| 20 owner_.set_connection_error_handler( |
| 21 base::Bind(&ViewStub::OnViewResolved, base::Unretained(this), nullptr)); |
| 22 owner_->GetToken( |
| 23 base::Bind(&ViewStub::OnViewResolved, base::Unretained(this))); |
| 24 } |
| 25 |
| 26 ViewStub::~ViewStub() { |
| 27 // Ensure that everything was properly released before this object was |
| 28 // destroyed. The |ViewRegistry| is responsible for maintaining the |
| 29 // invariant that all |ViewState| objects are owned so by the time we |
| 30 // get here, the view should have found a new owner or been unregistered. |
| 31 DCHECK(is_unavailable()); |
| 32 } |
| 33 |
| 34 void ViewStub::AttachView(ViewState* state) { |
| 35 DCHECK(state); |
| 36 DCHECK(!state->view_stub()); |
| 37 DCHECK(is_pending()); |
| 38 |
| 39 state_ = state; |
| 40 state_->set_view_stub(this); |
| 41 } |
| 42 |
| 43 ViewState* ViewStub::ReleaseView() { |
| 44 if (is_unavailable()) |
| 45 return nullptr; |
| 46 |
| 47 ViewState* state = state_; |
| 48 if (state) { |
| 49 DCHECK(state->view_stub() == this); |
| 50 state->set_view_stub(nullptr); |
| 51 state_ = nullptr; |
| 52 } |
| 53 unavailable_ = true; |
| 54 return state; |
| 55 } |
| 56 |
| 57 void ViewStub::SetTree(ViewTreeState* tree, uint32_t key) { |
| 58 DCHECK(tree); |
| 59 DCHECK(!tree_ && !parent_); |
| 60 |
| 61 key_ = key; |
| 62 SetTreeRecursively(tree); |
| 63 } |
| 64 |
| 65 void ViewStub::SetParent(ViewState* parent, uint32_t key) { |
| 66 DCHECK(parent); |
| 67 DCHECK(!tree_ && !parent_); |
| 68 |
| 69 parent_ = parent; |
| 70 key_ = key; |
| 71 if (parent->view_stub()) |
| 72 SetTreeRecursively(parent->view_stub()->tree()); |
| 73 } |
| 74 |
| 75 void ViewStub::Unlink() { |
| 76 parent_ = nullptr; |
| 77 key_ = 0; |
| 78 SetTreeRecursively(nullptr); |
| 79 } |
| 80 |
| 81 void ViewStub::SetTreeRecursively(ViewTreeState* tree) { |
| 82 if (tree_ == tree) |
| 83 return; |
| 84 tree_ = tree; |
| 85 if (state_) { |
| 86 for (const auto& pair : state_->children()) { |
| 87 pair.second->SetTreeRecursively(tree); |
| 88 } |
| 89 } |
| 90 } |
| 91 |
| 92 void ViewStub::OnViewResolved(mojo::ui::ViewTokenPtr view_token) { |
| 93 DCHECK(owner_); |
| 94 owner_.reset(); |
| 95 registry_->OnViewResolved(this, view_token.Pass()); |
| 96 } |
| 97 |
| 98 } // namespace view_manager |
OLD | NEW |