Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: components/arc/instance_holder.h

Issue 2711033002: Fix Arc integration test. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 macro to call InstanceHolder<T>::GetInstanceForVersionDoNotCallDirectly(). 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)->GetInstanceForVersionDoNotCallDirectly( \ 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 {
28 bool is_instance_for_sync_test = false;
29 } // namespace
30
27 namespace arc { 31 namespace arc {
28 32
29 // Holds a Mojo instance+version pair. This also allows for listening for state 33 // 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 34 // changes for the particular instance. T should be a Mojo interface type
31 // (arc::mojom::XxxInstance). 35 // (arc::mojom::XxxInstance).
32 template <typename T> 36 template <typename T>
33 class InstanceHolder { 37 class InstanceHolder {
34 public: 38 public:
35 // Notifies about connection events for individual instances. 39 // Notifies about connection events for individual instances.
36 class Observer { 40 class Observer {
37 public: 41 public:
38 // Called once the instance is ready. 42 // Called once the instance is ready.
39 virtual void OnInstanceReady() {} 43 virtual void OnInstanceReady() {}
40 44
41 // Called when the connection to the instance is closed. 45 // Called when the connection to the instance is closed.
42 virtual void OnInstanceClosed() {} 46 virtual void OnInstanceClosed() {}
43 47
44 protected: 48 protected:
45 virtual ~Observer() = default; 49 virtual ~Observer() = default;
46 }; 50 };
47 51
48 using Instance = T; 52 using Instance = T;
49 53
54 // In sync integration test, mutiple instances are allowed to be created for
55 // different profiles.
56 static void SetInstanceHolderForSyncTest() {
57 is_instance_for_sync_test = true;
58 }
59
50 InstanceHolder() = default; 60 InstanceHolder() = default;
51 61
52 // Returns true if the Mojo interface is ready at least for its version 0 62 // Returns true if the Mojo interface is ready at least for its version 0
53 // interface. Use an Observer if you want to be notified when this is ready. 63 // 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. 64 // This can only be called on the thread that this class was created on.
55 bool has_instance() const { return instance_; } 65 bool has_instance() const { return instance_; }
56 66
57 // Gets the Mojo interface that's intended to call for 67 // Gets the Mojo interface that's intended to call for
58 // |method_name_for_logging|, but only if its reported version is at least 68 // |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 69 // |min_version|. Returns nullptr if the instance is either not ready or does
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 void RemoveObserver(Observer* observer) { 102 void RemoveObserver(Observer* observer) {
93 DCHECK(thread_checker_.CalledOnValidThread()); 103 DCHECK(thread_checker_.CalledOnValidThread());
94 observer_list_.RemoveObserver(observer); 104 observer_list_.RemoveObserver(observer);
95 } 105 }
96 106
97 // Sets |instance| with |version|. 107 // Sets |instance| with |version|.
98 // This can be called in both case; on ready, and on closed. 108 // This can be called in both case; on ready, and on closed.
99 // Passing nullptr to |instance| means closing. 109 // Passing nullptr to |instance| means closing.
100 void SetInstance(T* instance, uint32_t version = T::Version_) { 110 void SetInstance(T* instance, uint32_t version = T::Version_) {
101 DCHECK(thread_checker_.CalledOnValidThread()); 111 DCHECK(thread_checker_.CalledOnValidThread());
102 DCHECK(instance == nullptr || instance_ == nullptr); 112 DCHECK(instance == nullptr || instance_ == nullptr ||
113 is_instance_for_sync_test);
103 114
104 // Note: This can be called with nullptr even if |instance_| is still 115 // Note: This can be called with nullptr even if |instance_| is still
105 // nullptr for just in case clean up purpose. No-op in such a case. 116 // nullptr for just in case clean up purpose. No-op in such a case.
106 if (instance == instance_) 117 if (instance == instance_)
107 return; 118 return;
108 119
109 instance_ = instance; 120 instance_ = instance;
110 version_ = version; 121 version_ = version;
111 if (instance_) { 122 if (instance_) {
112 for (auto& observer : observer_list_) 123 for (auto& observer : observer_list_)
(...skipping 12 matching lines...) Expand all
125 136
126 base::ThreadChecker thread_checker_; 137 base::ThreadChecker thread_checker_;
127 base::ObserverList<Observer> observer_list_; 138 base::ObserverList<Observer> observer_list_;
128 139
129 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>); 140 DISALLOW_COPY_AND_ASSIGN(InstanceHolder<T>);
130 }; 141 };
131 142
132 } // namespace arc 143 } // namespace arc
133 144
134 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_ 145 #endif // COMPONENTS_ARC_INSTANCE_HOLDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698