| 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 <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
| 13 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
| 14 | 15 |
| 15 namespace arc { | 16 namespace arc { |
| 16 | 17 |
| 17 // Holds a Mojo instance+version pair. This also allows for listening for state | 18 // Holds a Mojo instance+version pair. This also allows for listening for state |
| (...skipping 10 matching lines...) Expand all Loading... |
| 28 | 29 |
| 29 // Called when the connection to the instance is closed. | 30 // Called when the connection to the instance is closed. |
| 30 virtual void OnInstanceClosed() {} | 31 virtual void OnInstanceClosed() {} |
| 31 | 32 |
| 32 protected: | 33 protected: |
| 33 virtual ~Observer() = default; | 34 virtual ~Observer() = default; |
| 34 }; | 35 }; |
| 35 | 36 |
| 36 InstanceHolder() = default; | 37 InstanceHolder() = default; |
| 37 | 38 |
| 38 // Gets the Mojo interface for all the instance services. This will return | 39 // Returns true if the Mojo interface is ready at least for its version 0 |
| 39 // nullptr if that particular service is not ready yet. Use an Observer if you | 40 // interface. Use an Observer if you want to be notified when this is ready. |
| 40 // want to be notified when this is ready. This can only be called on the | 41 // This can only be called on the thread that this class was created on. |
| 41 // thread that this class was created on. | 42 bool has_instance() const { return instance_; } |
| 42 T* instance() const { return instance_; } | |
| 43 uint32_t version() const { return version_; } | |
| 44 | 43 |
| 45 // Gets the Mojo interface that's intended to call for | 44 // Gets the Mojo interface that's intended to call for |
| 46 // |method_name_for_logging|, but only if its reported version is at least | 45 // |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 | 46 // |min_version|. Returns nullptr if the instance is either not ready or does |
| 48 // not have the requested version, and logs appropriately. | 47 // not have the requested version, and logs appropriately. |
| 49 T* GetInstanceForMethod(const char* method_name_for_logging, | 48 // TODO(lhchavez): Improve the API. (crbug.com/649782) |
| 49 T* GetInstanceForMethod(const std::string& method_name_for_logging, |
| 50 uint32_t min_version) { | 50 uint32_t min_version) { |
| 51 if (!instance_) { | 51 if (!instance_) { |
| 52 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging | 52 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging |
| 53 << " not available."; | 53 << " not available."; |
| 54 return nullptr; | 54 return nullptr; |
| 55 } | 55 } |
| 56 if (version_ < min_version) { | 56 if (version_ < min_version) { |
| 57 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging | 57 LOG(ERROR) << "Instance for " << T::Name_ |
| 58 << " version mismatch. Expected " << min_version << " got " | 58 << "::" << method_name_for_logging |
| 59 << version_; | 59 << " version mismatch. Expected " << min_version << " got " |
| 60 << version_; |
| 60 return nullptr; | 61 return nullptr; |
| 61 } | 62 } |
| 62 return instance_; | 63 return instance_; |
| 63 } | 64 } |
| 64 | 65 |
| 65 // Same as the above, but for the version zero. | 66 // Same as the above, but for the version zero. |
| 66 T* GetInstanceForMethod(const char* method_name_for_logging) { | 67 T* GetInstanceForMethod(const std::string& method_name_for_logging) { |
| 67 return GetInstanceForMethod(method_name_for_logging, 0u); | 68 return GetInstanceForMethod(method_name_for_logging, 0u); |
| 68 } | 69 } |
| 69 | 70 |
| 70 // Adds or removes observers. This can only be called on the thread that this | 71 // Adds or removes observers. This can only be called on the thread that this |
| 71 // class was created on. RemoveObserver does nothing if |observer| is not in | 72 // class was created on. RemoveObserver does nothing if |observer| is not in |
| 72 // the list. | 73 // the list. |
| 73 void AddObserver(Observer* observer) { | 74 void AddObserver(Observer* observer) { |
| 74 DCHECK(thread_checker_.CalledOnValidThread()); | 75 DCHECK(thread_checker_.CalledOnValidThread()); |
| 75 observer_list_.AddObserver(observer); | 76 observer_list_.AddObserver(observer); |
| 76 | 77 |
| 77 if (instance()) | 78 if (instance_) |
| 78 observer->OnInstanceReady(); | 79 observer->OnInstanceReady(); |
| 79 } | 80 } |
| 80 | 81 |
| 81 void RemoveObserver(Observer* observer) { | 82 void RemoveObserver(Observer* observer) { |
| 82 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 83 observer_list_.RemoveObserver(observer); | 84 observer_list_.RemoveObserver(observer); |
| 84 } | 85 } |
| 85 | 86 |
| 86 // Sets |instance| with |version|. | 87 // Sets |instance| with |version|. |
| 87 // This can be called in both case; on ready, and on closed. | 88 // This can be called in both case; on ready, and on closed. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 112 | 113 |
| 113 base::ThreadChecker thread_checker_; | 114 base::ThreadChecker thread_checker_; |
| 114 base::ObserverList<Observer> observer_list_; | 115 base::ObserverList<Observer> observer_list_; |
| 115 | 116 |
| 116 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); | 117 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); |
| 117 }; | 118 }; |
| 118 | 119 |
| 119 } // namespace arc | 120 } // namespace arc |
| 120 | 121 |
| 121 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 122 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| OLD | NEW |