| 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 #ifndef COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 5 #ifndef COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| 6 #define COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 6 #define COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| 7 | 7 |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 } | 61 } |
| 62 | 62 |
| 63 // Called when the channel is closed. | 63 // Called when the channel is closed. |
| 64 void CloseChannel() { | 64 void CloseChannel() { |
| 65 if (!ptr_) | 65 if (!ptr_) |
| 66 return; | 66 return; |
| 67 | 67 |
| 68 ptr_.reset(); | 68 ptr_.reset(); |
| 69 raw_ptr_ = nullptr; | 69 raw_ptr_ = nullptr; |
| 70 version_ = 0; | 70 version_ = 0; |
| 71 if (observer_list_.might_have_observers()) { | 71 FOR_EACH_OBSERVER(Observer, observer_list_, OnInstanceClosed()); |
| 72 typename base::ObserverList<Observer>::Iterator it(&observer_list_); | |
| 73 Observer* obs; | |
| 74 while ((obs = it.GetNext()) != nullptr) | |
| 75 obs->OnInstanceClosed(); | |
| 76 } | |
| 77 } | 72 } |
| 78 | 73 |
| 79 // Sets the interface pointer to |ptr|, once the version is determined. This | 74 // Sets the interface pointer to |ptr|, once the version is determined. This |
| 80 // will eventually invoke SetInstance(), which will notify the observers. | 75 // will eventually invoke SetInstance(), which will notify the observers. |
| 81 void OnInstanceReady(mojo::InterfacePtr<T> ptr) { | 76 void OnInstanceReady(mojo::InterfacePtr<T> ptr) { |
| 82 temporary_ptr_ = std::move(ptr); | 77 temporary_ptr_ = std::move(ptr); |
| 83 temporary_ptr_.QueryVersion(base::Bind(&InstanceHolder<T>::OnVersionReady, | 78 temporary_ptr_.QueryVersion(base::Bind(&InstanceHolder<T>::OnVersionReady, |
| 84 weak_factory_.GetWeakPtr())); | 79 weak_factory_.GetWeakPtr())); |
| 85 } | 80 } |
| 86 | 81 |
| 87 // This method is not intended to be called directly. Normally it is called by | 82 // This method is not intended to be called directly. Normally it is called by |
| 88 // OnInstanceReady once the version of the instance is determined, but it is | 83 // OnInstanceReady once the version of the instance is determined, but it is |
| 89 // also exposed so that tests can directly inject a raw pointer+version | 84 // also exposed so that tests can directly inject a raw pointer+version |
| 90 // combination. | 85 // combination. |
| 91 void SetInstance(T* raw_ptr, uint32_t raw_version = T::Version_) { | 86 void SetInstance(T* raw_ptr, uint32_t raw_version = T::Version_) { |
| 92 raw_ptr_ = raw_ptr; | 87 raw_ptr_ = raw_ptr; |
| 93 version_ = raw_version; | 88 version_ = raw_version; |
| 94 if (observer_list_.might_have_observers()) { | 89 FOR_EACH_OBSERVER(Observer, observer_list_, OnInstanceReady()); |
| 95 typename base::ObserverList<Observer>::Iterator it(&observer_list_); | |
| 96 Observer* obs; | |
| 97 while ((obs = it.GetNext()) != nullptr) | |
| 98 obs->OnInstanceReady(); | |
| 99 } | |
| 100 } | 90 } |
| 101 | 91 |
| 102 private: | 92 private: |
| 103 void OnVersionReady(uint32_t version) { | 93 void OnVersionReady(uint32_t version) { |
| 104 ptr_ = std::move(temporary_ptr_); | 94 ptr_ = std::move(temporary_ptr_); |
| 105 ptr_.set_connection_error_handler(base::Bind( | 95 ptr_.set_connection_error_handler(base::Bind( |
| 106 &InstanceHolder<T>::CloseChannel, weak_factory_.GetWeakPtr())); | 96 &InstanceHolder<T>::CloseChannel, weak_factory_.GetWeakPtr())); |
| 107 SetInstance(ptr_.get(), version); | 97 SetInstance(ptr_.get(), version); |
| 108 } | 98 } |
| 109 | 99 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 128 // This needs to be the last member in order to cancel all inflight callbacks | 118 // This needs to be the last member in order to cancel all inflight callbacks |
| 129 // before destroying any other members. | 119 // before destroying any other members. |
| 130 base::WeakPtrFactory<InstanceHolder<T>> weak_factory_; | 120 base::WeakPtrFactory<InstanceHolder<T>> weak_factory_; |
| 131 | 121 |
| 132 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); | 122 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); |
| 133 }; | 123 }; |
| 134 | 124 |
| 135 } // namespace arc | 125 } // namespace arc |
| 136 | 126 |
| 137 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 127 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| OLD | NEW |