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 HasInstance() const { return instance_; } |
Luis Héctor Chávez
2016/09/23 22:51:04
nit: has_instance()
Yusuke Sato
2016/09/24 00:15:22
Done.
| |
42 T* instance() const { return instance_; } | 43 |
43 uint32_t version() const { return version_; } | 44 // Gets the version of the instance. Use this only for logging purposes. To |
45 // get an instance pointer with a version check, use GetInstanceForMethod(). | |
46 uint32_t GetVersionForLogging() const { return version_; } | |
Luis Héctor Chávez
2016/09/23 22:51:04
This one is fine since we want to discourage its u
Luis Héctor Chávez
2016/09/23 23:22:45
Oh wait, it's not being used anywhere! Let's get r
Yusuke Sato
2016/09/24 00:15:22
Ok, removed.
Yusuke Sato
2016/09/24 00:15:22
Acknowledged.
| |
44 | 47 |
45 // Gets the Mojo interface that's intended to call for | 48 // Gets the Mojo interface that's intended to call for |
46 // |method_name_for_logging|, but only if its reported version is at least | 49 // |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 | 50 // |min_version|. Returns nullptr if the instance is either not ready or does |
48 // not have the requested version, and logs appropriately. | 51 // not have the requested version, and logs appropriately. |
49 T* GetInstanceForMethod(const char* method_name_for_logging, | 52 T* GetInstanceForMethod(const std::string& method_name_for_logging, |
50 uint32_t min_version) { | 53 uint32_t min_version) { |
51 if (!instance_) { | 54 if (!instance_) { |
52 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging | 55 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging |
53 << " not available."; | 56 << " not available."; |
54 return nullptr; | 57 return nullptr; |
55 } | 58 } |
56 if (version_ < min_version) { | 59 if (version_ < min_version) { |
57 VLOG(1) << "Instance for " << T::Name_ << "::" << method_name_for_logging | 60 LOG(ERROR) << "Instance for " << T::Name_ |
58 << " version mismatch. Expected " << min_version << " got " | 61 << "::" << method_name_for_logging |
59 << version_; | 62 << " version mismatch. Expected " << min_version << " got " |
63 << version_; | |
60 return nullptr; | 64 return nullptr; |
61 } | 65 } |
62 return instance_; | 66 return instance_; |
63 } | 67 } |
64 | 68 |
65 // Same as the above, but for the version zero. | 69 // Same as the above, but for the version zero. |
66 T* GetInstanceForMethod(const char* method_name_for_logging) { | 70 T* GetInstanceForMethod(const std::string& method_name_for_logging) { |
67 return GetInstanceForMethod(method_name_for_logging, 0u); | 71 return GetInstanceForMethod(method_name_for_logging, 0u); |
68 } | 72 } |
69 | 73 |
70 // Adds or removes observers. This can only be called on the thread that this | 74 // 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 | 75 // class was created on. RemoveObserver does nothing if |observer| is not in |
72 // the list. | 76 // the list. |
73 void AddObserver(Observer* observer) { | 77 void AddObserver(Observer* observer) { |
74 DCHECK(thread_checker_.CalledOnValidThread()); | 78 DCHECK(thread_checker_.CalledOnValidThread()); |
75 observer_list_.AddObserver(observer); | 79 observer_list_.AddObserver(observer); |
76 | 80 |
77 if (instance()) | 81 if (instance_) |
78 observer->OnInstanceReady(); | 82 observer->OnInstanceReady(); |
79 } | 83 } |
80 | 84 |
81 void RemoveObserver(Observer* observer) { | 85 void RemoveObserver(Observer* observer) { |
82 DCHECK(thread_checker_.CalledOnValidThread()); | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
83 observer_list_.RemoveObserver(observer); | 87 observer_list_.RemoveObserver(observer); |
84 } | 88 } |
85 | 89 |
86 // Sets |instance| with |version|. | 90 // Sets |instance| with |version|. |
87 // This can be called in both case; on ready, and on closed. | 91 // This can be called in both case; on ready, and on closed. |
(...skipping 24 matching lines...) Expand all Loading... | |
112 | 116 |
113 base::ThreadChecker thread_checker_; | 117 base::ThreadChecker thread_checker_; |
114 base::ObserverList<Observer> observer_list_; | 118 base::ObserverList<Observer> observer_list_; |
115 | 119 |
116 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); | 120 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); |
117 }; | 121 }; |
118 | 122 |
119 } // namespace arc | 123 } // namespace arc |
120 | 124 |
121 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ | 125 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ |
OLD | NEW |