Chromium Code Reviews| 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 <string> | 8 #include <string> |
| 9 #include <type_traits> | |
| 9 #include <utility> | 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
| 14 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 15 | 16 |
| 17 // A convenience macro to call arc::InstanceHolder<T>::GetInstanceForVersion(). | |
| 18 // In order to avoid exposing method names from within the Mojo bindings, we | |
| 19 // will rely on stringification and the fact that the method min versions have a | |
| 20 // consistent naming scheme. | |
| 21 #define GET_INSTANCE_FOR_METHOD(holder, method_name) \ | |
|
Yusuke Sato
2017/01/04 19:59:12
nit: I'm a bit concerned about defining a macro wi
Luis Héctor Chávez
2017/01/04 20:38:56
sgtm, used the former.
| |
| 22 (holder)->GetInstanceForVersion( \ | |
| 23 std::remove_pointer<decltype( \ | |
| 24 holder)>::type::Instance::k##method_name##MinVersion, \ | |
| 25 #method_name) | |
| 26 | |
| 16 namespace arc { | 27 namespace arc { |
| 17 | 28 |
| 18 // Holds a Mojo instance+version pair. This also allows for listening for state | 29 // Holds a Mojo instance+version pair. This also allows for listening for state |
| 19 // changes for the particular instance. T should be a Mojo interface type | 30 // changes for the particular instance. T should be a Mojo interface type |
| 20 // (arc::mojom::XxxInstance). | 31 // (arc::mojom::XxxInstance). |
| 21 template <typename T> | 32 template <typename T> |
| 22 class InstanceHolder { | 33 class InstanceHolder { |
| 23 public: | 34 public: |
| 24 // Notifies about connection events for individual instances. | 35 // Notifies about connection events for individual instances. |
| 25 class Observer { | 36 class Observer { |
| 26 public: | 37 public: |
| 27 // Called once the instance is ready. | 38 // Called once the instance is ready. |
| 28 virtual void OnInstanceReady() {} | 39 virtual void OnInstanceReady() {} |
| 29 | 40 |
| 30 // Called when the connection to the instance is closed. | 41 // Called when the connection to the instance is closed. |
| 31 virtual void OnInstanceClosed() {} | 42 virtual void OnInstanceClosed() {} |
| 32 | 43 |
| 33 protected: | 44 protected: |
| 34 virtual ~Observer() = default; | 45 virtual ~Observer() = default; |
| 35 }; | 46 }; |
| 36 | 47 |
| 48 using Instance = T; | |
| 49 | |
| 37 InstanceHolder() = default; | 50 InstanceHolder() = default; |
| 38 | 51 |
| 39 // Returns true if the Mojo interface is ready at least for its version 0 | 52 // Returns true if the Mojo interface is ready at least for its version 0 |
| 40 // interface. Use an Observer if you want to be notified when this is ready. | 53 // interface. Use an Observer if you want to be notified when this is ready. |
| 41 // This can only be called on the thread that this class was created on. | 54 // This can only be called on the thread that this class was created on. |
| 42 bool has_instance() const { return instance_; } | 55 bool has_instance() const { return instance_; } |
| 43 | 56 |
| 44 // Gets the Mojo interface that's intended to call for | 57 // Gets the Mojo interface that's intended to call for |
| 45 // |method_name_for_logging|, but only if its reported version is at least | 58 // |method_name_for_logging|, but only if its reported version is at least |
| 46 // |min_version|. Returns nullptr if the instance is either not ready or does | 59 // |min_version|. Returns nullptr if the instance is either not ready or does |
| 47 // not have the requested version, and logs appropriately. | 60 // not have the requested version, and logs appropriately. |
| 48 // TODO(lhchavez): Improve the API. (crbug.com/649782) | 61 // This function should not be called directly. Instead, use the |
| 49 T* GetInstanceForMethod(const std::string& method_name_for_logging, | 62 // GET_INSTANCE_FOR_METHOD() macro. |
| 50 uint32_t min_version) { | 63 T* GetInstanceForVersion(uint32_t min_version, |
| 64 const char method_name_for_logging[]) { | |
| 51 if (!instance_) { | 65 if (!instance_) { |
| 52 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging | 66 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging |
| 53 << " not available."; | 67 << " not available."; |
| 54 return nullptr; | 68 return nullptr; |
| 55 } | 69 } |
| 56 if (version_ < min_version) { | 70 if (version_ < min_version) { |
| 57 LOG(ERROR) << "Instance for " << T::Name_ | 71 LOG(ERROR) << "Instance for " << T::Name_ |
| 58 << "::" << method_name_for_logging | 72 << "::" << method_name_for_logging |
| 59 << " version mismatch. Expected " << min_version << " got " | 73 << " version mismatch. Expected " << min_version << " got " |
| 60 << version_; | 74 << version_; |
| 61 return nullptr; | 75 return nullptr; |
| 62 } | 76 } |
| 63 return instance_; | 77 return instance_; |
| 64 } | 78 } |
| 65 | 79 |
| 66 // Same as the above, but for the version zero. | |
| 67 T* GetInstanceForMethod(const std::string& method_name_for_logging) { | |
| 68 return GetInstanceForMethod(method_name_for_logging, 0u); | |
| 69 } | |
| 70 | |
| 71 // Adds or removes observers. This can only be called on the thread that this | 80 // Adds or removes observers. This can only be called on the thread that this |
| 72 // class was created on. RemoveObserver does nothing if |observer| is not in | 81 // class was created on. RemoveObserver does nothing if |observer| is not in |
| 73 // the list. | 82 // the list. |
| 74 void AddObserver(Observer* observer) { | 83 void AddObserver(Observer* observer) { |
| 75 DCHECK(thread_checker_.CalledOnValidThread()); | 84 DCHECK(thread_checker_.CalledOnValidThread()); |
| 76 observer_list_.AddObserver(observer); | 85 observer_list_.AddObserver(observer); |
| 77 | 86 |
| 78 if (instance_) | 87 if (instance_) |
| 79 observer->OnInstanceReady(); | 88 observer->OnInstanceReady(); |
| 80 } | 89 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 | 124 |
| 116 base::ThreadChecker thread_checker_; | 125 base::ThreadChecker thread_checker_; |
| 117 base::ObserverList<Observer> observer_list_; | 126 base::ObserverList<Observer> observer_list_; |
| 118 | 127 |
| 119 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); | 128 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); |
| 120 }; | 129 }; |
| 121 | 130 |
| 122 } // namespace arc | 131 } // namespace arc |
| 123 | 132 |
| 124 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 133 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| OLD | NEW |