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 <type_traits> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
| 15 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 | 16 |
| 17 // A convenience macro to call arc::InstanceHolder<T>::GetInstanceForVersion(). | 17 // A convenience macro to call |
|
Luis Héctor Chávez
2017/01/09 18:41:02
This is now not a convenience macro, but the only
Yusuke Sato
2017/01/09 19:10:31
Done.
| |
| 18 // arc::InstanceHolder<T>::GetInstanceForVersionDoNotCallDirectly(). | |
| 18 // In order to avoid exposing method names from within the Mojo bindings, we | 19 // 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 // will rely on stringification and the fact that the method min versions have a |
| 20 // consistent naming scheme. | 21 // consistent naming scheme. |
| 21 #define ARC_GET_INSTANCE_FOR_METHOD(holder, method_name) \ | 22 #define ARC_GET_INSTANCE_FOR_METHOD(holder, method_name) \ |
| 22 (holder)->GetInstanceForVersion( \ | 23 (holder)->GetInstanceForVersionDoNotCallDirectly( \ |
| 23 std::remove_pointer<decltype( \ | 24 std::remove_pointer<decltype( \ |
| 24 holder)>::type::Instance::k##method_name##MinVersion, \ | 25 holder)>::type::Instance::k##method_name##MinVersion, \ |
| 25 #method_name) | 26 #method_name) |
| 26 | 27 |
| 27 namespace arc { | 28 namespace arc { |
| 28 | 29 |
| 29 // Holds a Mojo instance+version pair. This also allows for listening for state | 30 // Holds a Mojo instance+version pair. This also allows for listening for state |
| 30 // changes for the particular instance. T should be a Mojo interface type | 31 // changes for the particular instance. T should be a Mojo interface type |
| 31 // (arc::mojom::XxxInstance). | 32 // (arc::mojom::XxxInstance). |
| 32 template <typename T> | 33 template <typename T> |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 53 // interface. Use an Observer if you want to be notified when this is ready. | 54 // interface. Use an Observer if you want to be notified when this is ready. |
| 54 // This can only be called on the thread that this class was created on. | 55 // This can only be called on the thread that this class was created on. |
| 55 bool has_instance() const { return instance_; } | 56 bool has_instance() const { return instance_; } |
| 56 | 57 |
| 57 // Gets the Mojo interface that's intended to call for | 58 // Gets the Mojo interface that's intended to call for |
| 58 // |method_name_for_logging|, but only if its reported version is at least | 59 // |method_name_for_logging|, but only if its reported version is at least |
| 59 // |min_version|. Returns nullptr if the instance is either not ready or does | 60 // |min_version|. Returns nullptr if the instance is either not ready or does |
| 60 // not have the requested version, and logs appropriately. | 61 // not have the requested version, and logs appropriately. |
| 61 // This function should not be called directly. Instead, use the | 62 // This function should not be called directly. Instead, use the |
| 62 // ARC_GET_INSTANCE_FOR_METHOD() macro. | 63 // ARC_GET_INSTANCE_FOR_METHOD() macro. |
| 63 T* GetInstanceForVersion(uint32_t min_version, | 64 T* GetInstanceForVersionDoNotCallDirectly( |
| 64 const char method_name_for_logging[]) { | 65 uint32_t min_version, |
| 66 const char method_name_for_logging[]) { | |
| 65 if (!instance_) { | 67 if (!instance_) { |
| 66 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging | 68 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging |
| 67 << " not available."; | 69 << " not available."; |
| 68 return nullptr; | 70 return nullptr; |
| 69 } | 71 } |
| 70 if (version_ < min_version) { | 72 if (version_ < min_version) { |
| 71 LOG(ERROR) << "Instance for " << T::Name_ | 73 LOG(ERROR) << "Instance for " << T::Name_ |
| 72 << "::" << method_name_for_logging | 74 << "::" << method_name_for_logging |
| 73 << " version mismatch. Expected " << min_version << " got " | 75 << " version mismatch. Expected " << min_version << " got " |
| 74 << version_; | 76 << version_; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 124 | 126 |
| 125 base::ThreadChecker thread_checker_; | 127 base::ThreadChecker thread_checker_; |
| 126 base::ObserverList<Observer> observer_list_; | 128 base::ObserverList<Observer> observer_list_; |
| 127 | 129 |
| 128 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); | 130 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); |
| 129 }; | 131 }; |
| 130 | 132 |
| 131 } // namespace arc | 133 } // namespace arc |
| 132 | 134 |
| 133 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 135 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| OLD | NEW |