OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ | 5 #ifndef CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ |
6 #define CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ | 6 #define CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 class Response; | 34 class Response; |
35 class Signal; | 35 class Signal; |
36 | 36 |
37 } // namespace dbus | 37 } // namespace dbus |
38 | 38 |
39 namespace chromeos { | 39 namespace chromeos { |
40 | 40 |
41 // A class to help implement Shill clients. | 41 // A class to help implement Shill clients. |
42 class ShillClientHelper { | 42 class ShillClientHelper { |
43 public: | 43 public: |
44 class RefHolder; | |
45 | |
46 // A callback to handle PropertyChanged signals. | 44 // A callback to handle PropertyChanged signals. |
47 typedef base::Callback<void(const std::string& name, | 45 typedef base::Callback<void(const std::string& name, |
48 const base::Value& value)> PropertyChangedHandler; | 46 const base::Value& value)> PropertyChangedHandler; |
49 | 47 |
50 // A callback to handle responses for methods with DictionaryValue results. | 48 // A callback to handle responses for methods with DictionaryValue results. |
51 typedef base::Callback<void( | 49 typedef base::Callback<void( |
52 DBusMethodCallStatus call_status, | 50 DBusMethodCallStatus call_status, |
53 const base::DictionaryValue& result)> DictionaryValueCallback; | 51 const base::DictionaryValue& result)> DictionaryValueCallback; |
54 | 52 |
55 // A callback to handle responses for methods with DictionaryValue results. | 53 // A callback to handle responses for methods with DictionaryValue results. |
56 // This is used by CallDictionaryValueMethodWithErrorCallback. | 54 // This is used by CallDictionaryValueMethodWithErrorCallback. |
57 typedef base::Callback<void(const base::DictionaryValue& result)> | 55 typedef base::Callback<void(const base::DictionaryValue& result)> |
58 DictionaryValueCallbackWithoutStatus; | 56 DictionaryValueCallbackWithoutStatus; |
59 | 57 |
60 // A callback to handle responses of methods returning a ListValue. | 58 // A callback to handle responses of methods returning a ListValue. |
61 typedef base::Callback<void(const base::ListValue& result)> ListValueCallback; | 59 typedef base::Callback<void(const base::ListValue& result)> ListValueCallback; |
62 | 60 |
63 // A callback to handle errors for method call. | 61 // A callback to handle errors for method call. |
64 typedef base::Callback<void(const std::string& error_name, | 62 typedef base::Callback<void(const std::string& error_name, |
65 const std::string& error_message)> ErrorCallback; | 63 const std::string& error_message)> ErrorCallback; |
66 | 64 |
67 // A callback that handles responses for methods with string results. | 65 // A callback that handles responses for methods with string results. |
68 typedef base::Callback<void(const std::string& result)> StringCallback; | 66 typedef base::Callback<void(const std::string& result)> StringCallback; |
69 | 67 |
70 // A callback that handles responses for methods with boolean results. | 68 // A callback that handles responses for methods with boolean results. |
71 typedef base::Callback<void(bool result)> BooleanCallback; | 69 typedef base::Callback<void(bool result)> BooleanCallback; |
72 | 70 |
73 // Callback used to notify owner when this can be safely released. | |
74 typedef base::Callback<void(ShillClientHelper* helper)> ReleasedCallback; | |
75 | 71 |
76 explicit ShillClientHelper(dbus::ObjectProxy* proxy); | 72 ShillClientHelper(dbus::Bus* bus, dbus::ObjectProxy* proxy); |
77 | 73 |
78 virtual ~ShillClientHelper(); | 74 virtual ~ShillClientHelper(); |
79 | 75 |
80 // Sets |released_callback_|. This is optional and should only be called at | |
81 // most once. | |
82 void SetReleasedCallback(ReleasedCallback callback); | |
83 | |
84 // Adds an |observer| of the PropertyChanged signal. | 76 // Adds an |observer| of the PropertyChanged signal. |
85 void AddPropertyChangedObserver(ShillPropertyChangedObserver* observer); | 77 void AddPropertyChangedObserver(ShillPropertyChangedObserver* observer); |
86 | 78 |
87 // Removes an |observer| of the PropertyChanged signal. | 79 // Removes an |observer| of the PropertyChanged signal. |
88 void RemovePropertyChangedObserver(ShillPropertyChangedObserver* observer); | 80 void RemovePropertyChangedObserver(ShillPropertyChangedObserver* observer); |
89 | 81 |
90 // Starts monitoring PropertyChanged signal. If there aren't observers for the | 82 // Starts monitoring PropertyChanged signal. If there aren't observers for the |
91 // PropertyChanged signal, the actual monitoring will be delayed until the | 83 // PropertyChanged signal, the actual monitoring will be delayed until the |
92 // first observer is added. | 84 // first observer is added. |
93 void MonitorPropertyChanged(const std::string& interface_name); | 85 void MonitorPropertyChanged(const std::string& interface_name); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 dbus::MethodCall* method_call, | 124 dbus::MethodCall* method_call, |
133 const DictionaryValueCallbackWithoutStatus& callback, | 125 const DictionaryValueCallbackWithoutStatus& callback, |
134 const ErrorCallback& error_callback); | 126 const ErrorCallback& error_callback); |
135 | 127 |
136 // Calls a method with a boolean array result with error callback. | 128 // Calls a method with a boolean array result with error callback. |
137 void CallListValueMethodWithErrorCallback( | 129 void CallListValueMethodWithErrorCallback( |
138 dbus::MethodCall* method_call, | 130 dbus::MethodCall* method_call, |
139 const ListValueCallback& callback, | 131 const ListValueCallback& callback, |
140 const ErrorCallback& error_callback); | 132 const ErrorCallback& error_callback); |
141 | 133 |
142 const dbus::ObjectProxy* object_proxy() const { return proxy_; } | |
143 | |
144 // Appends the value (basic types and string-to-string dictionary) to the | 134 // Appends the value (basic types and string-to-string dictionary) to the |
145 // writer as a variant. | 135 // writer as a variant. |
146 static void AppendValueDataAsVariant(dbus::MessageWriter* writer, | 136 static void AppendValueDataAsVariant(dbus::MessageWriter* writer, |
147 const base::Value& value); | 137 const base::Value& value); |
148 | 138 |
149 // Appends a string-to-variant dictionary to the writer. | 139 // Appends a string-to-variant dictionary to the writer. |
150 static void AppendServicePropertiesDictionary( | 140 static void AppendServicePropertiesDictionary( |
151 dbus::MessageWriter* writer, | 141 dbus::MessageWriter* writer, |
152 const base::DictionaryValue& dictionary); | 142 const base::DictionaryValue& dictionary); |
153 | 143 |
154 protected: | |
155 // Reference / Ownership management. If the number of active refs (observers | |
156 // + in-progress method calls) becomes 0, |released_callback_| (if set) will | |
157 // be called. | |
158 void AddRef(); | |
159 void Release(); | |
160 | |
161 private: | 144 private: |
162 // Starts monitoring PropertyChanged signal. | 145 // Starts monitoring PropertyChanged signal. |
163 void MonitorPropertyChangedInternal(const std::string& interface_name); | 146 void MonitorPropertyChangedInternal(const std::string& interface_name); |
164 | 147 |
165 // Handles the result of signal connection setup. | 148 // Handles the result of signal connection setup. |
166 void OnSignalConnected(const std::string& interface, | 149 void OnSignalConnected(const std::string& interface, |
167 const std::string& signal, | 150 const std::string& signal, |
168 bool success); | 151 bool success); |
169 | 152 |
170 // Handles PropertyChanged signal. | 153 // Handles PropertyChanged signal. |
171 void OnPropertyChanged(dbus::Signal* signal); | 154 void OnPropertyChanged(dbus::Signal* signal); |
172 | 155 |
173 dbus::ObjectProxy* proxy_; | 156 dbus::ObjectProxy* proxy_; |
174 ReleasedCallback released_callback_; | |
175 int active_refs_; | |
176 PropertyChangedHandler property_changed_handler_; | 157 PropertyChangedHandler property_changed_handler_; |
177 ObserverList<ShillPropertyChangedObserver, true /* check_empty */> | 158 ObserverList<ShillPropertyChangedObserver, true /* check_empty */> |
178 observer_list_; | 159 observer_list_; |
179 std::vector<std::string> interfaces_to_be_monitored_; | 160 std::vector<std::string> interfaces_to_be_monitored_; |
180 | 161 |
181 // Note: This should remain the last member so it'll be destroyed and | 162 // Note: This should remain the last member so it'll be destroyed and |
182 // invalidate its weak pointers before any other members are destroyed. | 163 // invalidate its weak pointers before any other members are destroyed. |
183 base::WeakPtrFactory<ShillClientHelper> weak_ptr_factory_; | 164 base::WeakPtrFactory<ShillClientHelper> weak_ptr_factory_; |
184 | 165 |
185 DISALLOW_COPY_AND_ASSIGN(ShillClientHelper); | 166 DISALLOW_COPY_AND_ASSIGN(ShillClientHelper); |
186 }; | 167 }; |
187 | 168 |
188 } // namespace chromeos | 169 } // namespace chromeos |
189 | 170 |
190 #endif // CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ | 171 #endif // CHROMEOS_DBUS_SHILL_CLIENT_HELPER_H_ |
OLD | NEW |