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 24 matching lines...) Expand all Loading... |
35 | 35 |
36 InstanceHolder() = default; | 36 InstanceHolder() = default; |
37 | 37 |
38 // Gets the Mojo interface for all the instance services. This will return | 38 // Gets the Mojo interface for all the instance services. This will return |
39 // nullptr if that particular service is not ready yet. Use an Observer if you | 39 // nullptr if that particular service is not ready yet. Use an Observer if you |
40 // want to be notified when this is ready. This can only be called on the | 40 // want to be notified when this is ready. This can only be called on the |
41 // thread that this class was created on. | 41 // thread that this class was created on. |
42 T* instance() const { return instance_; } | 42 T* instance() const { return instance_; } |
43 uint32_t version() const { return version_; } | 43 uint32_t version() const { return version_; } |
44 | 44 |
| 45 // Gets the Mojo interface that's intended to call for |
| 46 // |method_name_for_logging|, but only if its reported version is at least |
| 47 // |min_version|. Returns nullptr if the instance is either not ready or does |
| 48 // not have the requested version, and logs appropriately. |
| 49 T* GetInstanceForMethod(const char* method_name_for_logging, |
| 50 uint32_t min_version) { |
| 51 if (!instance_) { |
| 52 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging |
| 53 << " not available."; |
| 54 return nullptr; |
| 55 } |
| 56 if (version_ < min_version) { |
| 57 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging |
| 58 << " version mismatch. Expected " << min_version << " got " |
| 59 << version_; |
| 60 return nullptr; |
| 61 } |
| 62 return instance_; |
| 63 } |
| 64 |
| 65 // Same as the above, but for the version zero. |
| 66 T* GetInstanceForMethod(const char* method_name_for_logging) { |
| 67 return GetInstanceForMethod(method_name_for_logging, 0u); |
| 68 } |
| 69 |
45 // Adds or removes observers. This can only be called on the thread that this | 70 // Adds or removes observers. This can only be called on the thread that this |
46 // class was created on. RemoveObserver does nothing if |observer| is not in | 71 // class was created on. RemoveObserver does nothing if |observer| is not in |
47 // the list. | 72 // the list. |
48 void AddObserver(Observer* observer) { | 73 void AddObserver(Observer* observer) { |
49 DCHECK(thread_checker_.CalledOnValidThread()); | 74 DCHECK(thread_checker_.CalledOnValidThread()); |
50 observer_list_.AddObserver(observer); | 75 observer_list_.AddObserver(observer); |
51 | 76 |
52 if (instance()) | 77 if (instance()) |
53 observer->OnInstanceReady(); | 78 observer->OnInstanceReady(); |
54 } | 79 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 | 112 |
88 base::ThreadChecker thread_checker_; | 113 base::ThreadChecker thread_checker_; |
89 base::ObserverList<Observer> observer_list_; | 114 base::ObserverList<Observer> observer_list_; |
90 | 115 |
91 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); | 116 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); |
92 }; | 117 }; |
93 | 118 |
94 } // namespace arc | 119 } // namespace arc |
95 | 120 |
96 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 121 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
OLD | NEW |