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 <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 33 virtual ~Observer() = default; | 33 virtual ~Observer() = default; |
| 34 }; | 34 }; |
| 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_; } |
|
Yusuke Sato
2016/09/16 23:58:48
Can we remove this now? If not, what's preventing
Luis Héctor Chávez
2016/09/17 00:30:53
GetIntentHelperInstanceWithErrorCode() still needs
| |
| 44 | 44 |
| 45 // Gets the Mojo interface for the instance, but only if its reported version | |
| 46 // is at least |min_version|. Returns nullptr if the instance is either not | |
| 47 // ready or does not have the requested version, and logs appropriately. | |
| 48 T* GetInstanceForVersion(uint32_t min_version) { | |
| 49 if (!instance_) { | |
| 50 VLOG(2) << __func__ << " Instance not available."; | |
| 51 return nullptr; | |
| 52 } | |
| 53 if (version_ < min_version) { | |
| 54 VLOG(2) << __func__ << " Instance version mismatch. Expected " | |
| 55 << min_version << " got " << version_; | |
| 56 return nullptr; | |
| 57 } | |
| 58 return instance_; | |
| 59 } | |
| 60 | |
| 61 // Same as the above, but with an explicit method name for better logging. | |
| 62 T* GetInstanceForVersion(uint32_t min_version, const char* method_name) { | |
|
Yusuke Sato
2016/09/16 23:58:48
Passing the method_name every time only for loggin
Luis Héctor Chávez
2016/09/17 00:30:53
We can't query the method name from |min_version|
Yusuke Sato
2016/09/17 01:38:56
Acknowledged. I guess typeid() won't always work a
| |
| 63 if (!instance_) { | |
| 64 VLOG(2) << __func__ << " Instance not available to call " << method_name | |
| 65 << "."; | |
| 66 return nullptr; | |
| 67 } | |
| 68 if (version_ < min_version) { | |
| 69 VLOG(2) << __func__ << " Instance version mismatch to call " | |
| 70 << method_name << ". Expected " << min_version << " got " | |
| 71 << version_; | |
| 72 return nullptr; | |
| 73 } | |
| 74 return instance_; | |
| 75 } | |
| 76 | |
| 45 // Adds or removes observers. This can only be called on the thread that this | 77 // 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 | 78 // class was created on. RemoveObserver does nothing if |observer| is not in |
| 47 // the list. | 79 // the list. |
| 48 void AddObserver(Observer* observer) { | 80 void AddObserver(Observer* observer) { |
| 49 DCHECK(thread_checker_.CalledOnValidThread()); | 81 DCHECK(thread_checker_.CalledOnValidThread()); |
| 50 observer_list_.AddObserver(observer); | 82 observer_list_.AddObserver(observer); |
| 51 | 83 |
| 52 if (instance()) | 84 if (instance()) |
| 53 observer->OnInstanceReady(); | 85 observer->OnInstanceReady(); |
| 54 } | 86 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 | 119 |
| 88 base::ThreadChecker thread_checker_; | 120 base::ThreadChecker thread_checker_; |
| 89 base::ObserverList<Observer> observer_list_; | 121 base::ObserverList<Observer> observer_list_; |
| 90 | 122 |
| 91 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); | 123 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); |
| 92 }; | 124 }; |
| 93 | 125 |
| 94 } // namespace arc | 126 } // namespace arc |
| 95 | 127 |
| 96 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 128 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
| OLD | NEW |