OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| 6 #define COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| 7 |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/observer_list.h" |
| 14 #include "base/threading/thread_checker.h" |
| 15 #include "mojo/public/cpp/bindings/interface_ptr.h" |
| 16 |
| 17 namespace arc { |
| 18 |
| 19 // Holds a Mojo instance+version pair. This also allows for listening for state |
| 20 // changes for the particular instance. T should be a Mojo interface type |
| 21 // (arc::mojom::XxxInstance). |
| 22 template <typename T> |
| 23 class InstanceHolder { |
| 24 public: |
| 25 // Notifies about connection events for individual instances. |
| 26 class Observer { |
| 27 public: |
| 28 // Called once the instance is ready. |
| 29 virtual void OnInstanceReady() {} |
| 30 |
| 31 // Called when the connection to the instance is closed. |
| 32 virtual void OnInstanceClosed() {} |
| 33 |
| 34 protected: |
| 35 virtual ~Observer() = default; |
| 36 }; |
| 37 |
| 38 InstanceHolder() : weak_factory_(this) {} |
| 39 |
| 40 // Gets the Mojo interface for all the instance services. This will return |
| 41 // nullptr if that particular service is not ready yet. Use an Observer if you |
| 42 // want to be notified when this is ready. This can only be called on the |
| 43 // thread that this class was created on. |
| 44 T* instance() const { return raw_ptr_; } |
| 45 uint32_t version() const { return version_; } |
| 46 |
| 47 // Adds or removes observers. This can only be called on the thread that this |
| 48 // class was created on. RemoveObserver does nothing if |observer| is not in |
| 49 // the list. |
| 50 void AddObserver(Observer* observer) { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); |
| 52 observer_list_.AddObserver(observer); |
| 53 |
| 54 if (instance()) |
| 55 observer->OnInstanceReady(); |
| 56 } |
| 57 |
| 58 void RemoveObserver(Observer* observer) { |
| 59 DCHECK(thread_checker_.CalledOnValidThread()); |
| 60 observer_list_.RemoveObserver(observer); |
| 61 } |
| 62 |
| 63 // Called when the channel is closed. |
| 64 void CloseChannel() { |
| 65 if (!ptr_) |
| 66 return; |
| 67 |
| 68 ptr_.reset(); |
| 69 raw_ptr_ = nullptr; |
| 70 version_ = 0; |
| 71 if (observer_list_.might_have_observers()) { |
| 72 typename base::ObserverList<Observer>::Iterator it(&observer_list_); |
| 73 Observer* obs; |
| 74 while ((obs = it.GetNext()) != nullptr) |
| 75 obs->OnInstanceClosed(); |
| 76 } |
| 77 } |
| 78 |
| 79 // Sets the interface pointer to |ptr|, once the version is determined. This |
| 80 // will eventually invoke SetInstance(), which will notify the observers. |
| 81 void OnInstanceReady(mojo::InterfacePtr<T> ptr) { |
| 82 temporary_ptr_ = std::move(ptr); |
| 83 temporary_ptr_.QueryVersion(base::Bind(&InstanceHolder<T>::OnVersionReady, |
| 84 weak_factory_.GetWeakPtr())); |
| 85 } |
| 86 |
| 87 // 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 |
| 89 // also exposed so that tests can directly inject a raw pointer+version |
| 90 // combination. |
| 91 void SetInstance(T* raw_ptr, uint32_t raw_version = T::Version_) { |
| 92 raw_ptr_ = raw_ptr; |
| 93 version_ = raw_version; |
| 94 if (observer_list_.might_have_observers()) { |
| 95 typename base::ObserverList<Observer>::Iterator it(&observer_list_); |
| 96 Observer* obs; |
| 97 while ((obs = it.GetNext()) != nullptr) |
| 98 obs->OnInstanceReady(); |
| 99 } |
| 100 } |
| 101 |
| 102 private: |
| 103 void OnVersionReady(uint32_t version) { |
| 104 ptr_ = std::move(temporary_ptr_); |
| 105 ptr_.set_connection_error_handler(base::Bind( |
| 106 &InstanceHolder<T>::CloseChannel, weak_factory_.GetWeakPtr())); |
| 107 SetInstance(ptr_.get(), version); |
| 108 } |
| 109 |
| 110 // These two are copies of the contents of ptr_. They are provided here just |
| 111 // so that tests can provide non-mojo implementations. |
| 112 T* raw_ptr_ = nullptr; |
| 113 uint32_t version_ = 0; |
| 114 |
| 115 mojo::InterfacePtr<T> ptr_; |
| 116 |
| 117 // Temporary Mojo interfaces. After a Mojo interface pointer has been |
| 118 // received from the other endpoint, we still need to asynchronously query its |
| 119 // version. While that is going on, we should still return nullptr on the |
| 120 // instance() function. |
| 121 // To keep the instance() functions being trivial, store the instance pointer |
| 122 // in a temporary variable to avoid losing its reference. |
| 123 mojo::InterfacePtr<T> temporary_ptr_; |
| 124 |
| 125 base::ThreadChecker thread_checker_; |
| 126 base::ObserverList<Observer> observer_list_; |
| 127 |
| 128 // This needs to be the last member in order to cancel all inflight callbacks |
| 129 // before destroying any other members. |
| 130 base::WeakPtrFactory<InstanceHolder<T>> weak_factory_; |
| 131 |
| 132 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); |
| 133 }; |
| 134 |
| 135 } // namespace arc |
| 136 |
| 137 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
OLD | NEW |