| 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 macro to call InstanceHolder<T>::GetInstanceForVersionDoNotCallDirectly(). |
| 18 // In order to avoid exposing method names from within the Mojo bindings, we | 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 | 19 // will rely on stringification and the fact that the method min versions have a |
| 20 // consistent naming scheme. | 20 // consistent naming scheme. |
| 21 #define ARC_GET_INSTANCE_FOR_METHOD(holder, method_name) \ | 21 #define ARC_GET_INSTANCE_FOR_METHOD(holder, method_name) \ |
| 22 (holder)->GetInstanceForVersion( \ | 22 (holder)->GetInstanceForVersionDoNotCallDirectly( \ |
| 23 std::remove_pointer<decltype( \ | 23 std::remove_pointer<decltype( \ |
| 24 holder)>::type::Instance::k##method_name##MinVersion, \ | 24 holder)>::type::Instance::k##method_name##MinVersion, \ |
| 25 #method_name) | 25 #method_name) |
| 26 | 26 |
| 27 namespace arc { | 27 namespace arc { |
| 28 | 28 |
| 29 // 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 |
| 30 // 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 |
| 31 // (arc::mojom::XxxInstance). | 31 // (arc::mojom::XxxInstance). |
| 32 template <typename T> | 32 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. | 53 // 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. | 54 // This can only be called on the thread that this class was created on. |
| 55 bool has_instance() const { return instance_; } | 55 bool has_instance() const { return instance_; } |
| 56 | 56 |
| 57 // Gets the Mojo interface that's intended to call for | 57 // Gets the Mojo interface that's intended to call for |
| 58 // |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 |
| 59 // |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 |
| 60 // not have the requested version, and logs appropriately. | 60 // not have the requested version, and logs appropriately. |
| 61 // This function should not be called directly. Instead, use the | 61 // This function should not be called directly. Instead, use the |
| 62 // ARC_GET_INSTANCE_FOR_METHOD() macro. | 62 // ARC_GET_INSTANCE_FOR_METHOD() macro. |
| 63 T* GetInstanceForVersion(uint32_t min_version, | 63 T* GetInstanceForVersionDoNotCallDirectly( |
| 64 const char method_name_for_logging[]) { | 64 uint32_t min_version, |
| 65 const char method_name_for_logging[]) { |
| 65 if (!instance_) { | 66 if (!instance_) { |
| 66 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging | 67 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging |
| 67 << " not available."; | 68 << " not available."; |
| 68 return nullptr; | 69 return nullptr; |
| 69 } | 70 } |
| 70 if (version_ < min_version) { | 71 if (version_ < min_version) { |
| 71 LOG(ERROR) << "Instance for " << T::Name_ | 72 LOG(ERROR) << "Instance for " << T::Name_ |
| 72 << "::" << method_name_for_logging | 73 << "::" << method_name_for_logging |
| 73 << " version mismatch. Expected " << min_version << " got " | 74 << " version mismatch. Expected " << min_version << " got " |
| 74 << version_; | 75 << version_; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 | 125 |
| 125 base::ThreadChecker thread_checker_; | 126 base::ThreadChecker thread_checker_; |
| 126 base::ObserverList<Observer> observer_list_; | 127 base::ObserverList<Observer> observer_list_; |
| 127 | 128 |
| 128 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); | 129 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); |
| 129 }; | 130 }; |
| 130 | 131 |
| 131 } // namespace arc | 132 } // namespace arc |
| 132 | 133 |
| 133 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 134 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| OLD | NEW |