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. It is guaranteed that |instance| will | |
29 // not be null. | |
30 virtual void OnInstanceReady(T* instance, uint32_t version) {} | |
hidehiko
2016/07/12 16:25:14
I prefer to standardize the way to access T*.
If t
Luis Héctor Chávez
2016/07/12 18:00:24
Args were dropped.
| |
31 | |
32 // The parameter is always nullptr, but is required to be able to | |
33 // distinguish between implementations of Observer for different types. | |
34 virtual void OnInstanceClosed(T*) {} | |
hidehiko
2016/07/12 16:25:14
Ditto. Could you add TODO to drop T*?
Luis Héctor Chávez
2016/07/12 18:00:24
Dropped.
| |
35 | |
36 protected: | |
37 virtual ~Observer() = default; | |
38 }; | |
39 | |
40 InstanceHolder() : weak_factory_(this) {} | |
41 | |
42 // Gets the Mojo interface for all the instance services. This will return | |
43 // nullptr if that particular service is not ready yet. Use an Observer if you | |
44 // want to be notified when this is ready. This can only be called on the | |
45 // thread that this class was created on. | |
46 T* instance() const { return raw_ptr_; } | |
47 uint32_t version() const { return version_; } | |
48 | |
49 // Adds or removes observers. This can only be called on the thread that this | |
50 // class was created on. RemoveObserver does nothing if |observer| is not in | |
51 // the list. | |
52 void AddObserver(Observer* observer) { | |
53 DCHECK(thread_checker_.CalledOnValidThread()); | |
54 observer_list_.AddObserver(observer); | |
55 | |
56 if (instance()) | |
57 observer->OnInstanceReady(instance(), version()); | |
58 } | |
59 | |
60 void RemoveObserver(Observer* observer) { | |
61 DCHECK(thread_checker_.CalledOnValidThread()); | |
62 observer_list_.RemoveObserver(observer); | |
63 } | |
64 | |
65 // Called when the channel is closed. | |
66 void CloseChannel() { | |
67 if (!ptr_) | |
68 return; | |
69 | |
70 ptr_.reset(); | |
71 raw_ptr_ = nullptr; | |
72 version_ = 0; | |
73 if (observer_list_.might_have_observers()) { | |
74 typename base::ObserverList<Observer>::Iterator it(&observer_list_); | |
75 Observer* obs; | |
76 while ((obs = it.GetNext()) != nullptr) | |
77 obs->OnInstanceClosed(instance()); | |
78 } | |
79 } | |
80 | |
81 // Sets the interface pointer to |ptr|, once the version is determined. This | |
82 // will eventually invoke SetInstance(), which will notify the observers. | |
83 void OnInstanceReady(mojo::InterfacePtr<T> ptr) { | |
84 temporary_ptr_ = std::move(ptr); | |
85 temporary_ptr_.QueryVersion(base::Bind(&InstanceHolder<T>::OnVersionReady, | |
86 weak_factory_.GetWeakPtr())); | |
87 } | |
88 | |
89 // This method is not intended to be called directly. Normally it is called by | |
90 // OnInstanceReady once the version of the instance is determined, but it is | |
91 // also exposed so that tests can directly inject a raw pointer+version | |
92 // combination. | |
93 void SetInstance(T* raw_ptr, uint32_t raw_version = T::Version_) { | |
94 raw_ptr_ = raw_ptr; | |
95 version_ = raw_version; | |
96 if (observer_list_.might_have_observers()) { | |
97 typename base::ObserverList<Observer>::Iterator it(&observer_list_); | |
98 Observer* obs; | |
99 while ((obs = it.GetNext()) != nullptr) | |
100 obs->OnInstanceReady(instance(), version()); | |
101 } | |
102 } | |
103 | |
104 private: | |
105 void OnVersionReady(uint32_t version) { | |
106 ptr_ = std::move(temporary_ptr_); | |
107 ptr_.set_connection_error_handler(base::Bind( | |
108 &InstanceHolder<T>::CloseChannel, weak_factory_.GetWeakPtr())); | |
109 SetInstance(ptr_.get(), version); | |
110 } | |
111 | |
112 // These two are copies of the contents of ptr_. They are provided here just | |
113 // so that tests can provide non-mojo implementations. | |
114 T* raw_ptr_ = nullptr; | |
115 uint32_t version_ = 0; | |
116 | |
117 mojo::InterfacePtr<T> ptr_; | |
118 | |
119 // Temporary Mojo interfaces. After a Mojo interface pointer has been | |
120 // received from the other endpoint, we still need to asynchronously query its | |
121 // version. While that is going on, we should still return nullptr on the | |
122 // instance() function. | |
123 // To keep the instance() functions being trivial, store the instance pointer | |
124 // in a temporary variable to avoid losing its reference. | |
125 mojo::InterfacePtr<T> temporary_ptr_; | |
126 | |
127 base::ThreadChecker thread_checker_; | |
128 base::ObserverList<Observer> observer_list_; | |
129 | |
130 // This needs to be the last member in order to cancel all inflight callbacks | |
131 // before destroying any other members. | |
132 base::WeakPtrFactory<InstanceHolder<T>> weak_factory_; | |
133 | |
134 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); | |
135 }; | |
136 | |
137 } // namespace arc | |
138 | |
139 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ | |
OLD | NEW |