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 #include "chromeos/dbus/shill_client_helper.h" | 5 #include "chromeos/dbus/shill_client_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "dbus/message.h" | 9 #include "dbus/message.h" |
10 #include "dbus/object_proxy.h" | 10 #include "dbus/object_proxy.h" |
11 #include "dbus/values_util.h" | 11 #include "dbus/values_util.h" |
12 #include "third_party/cros_system_api/dbus/service_constants.h" | 12 #include "third_party/cros_system_api/dbus/service_constants.h" |
13 | 13 |
14 namespace chromeos { | 14 namespace chromeos { |
15 | 15 |
16 // Class to hold onto a reference to a ShillClientHelper. This calss | |
17 // is owned by callbacks and released once the callback completes. | |
18 // Note: Only success callbacks hold the reference. If an error callback is | |
19 // invoked instead, the success callback will still be destroyed and the | |
20 // RefHolder with it, once the callback chain completes. | |
21 class ShillClientHelper::RefHolder { | |
22 public: | |
23 explicit RefHolder(base::WeakPtr<ShillClientHelper> helper) | |
24 : helper_(helper) { | |
25 helper_->AddRef(); | |
26 } | |
27 ~RefHolder() { | |
28 if (helper_) | |
29 helper_->Release(); | |
30 } | |
31 ShillClientHelper* helper() { return helper_.get(); } | |
32 | |
33 private: | |
34 base::WeakPtr<ShillClientHelper> helper_; | |
35 }; | |
36 | |
16 namespace { | 37 namespace { |
17 | 38 |
18 const char kInvalidResponseErrorName[] = ""; // No error name. | 39 const char kInvalidResponseErrorName[] = ""; // No error name. |
19 const char kInvalidResponseErrorMessage[] = "Invalid response."; | 40 const char kInvalidResponseErrorMessage[] = "Invalid response."; |
20 | 41 |
42 // Note: here and below, |ref_holder| is unused in the function body. It only | |
43 // exists so that it will be destroyed (and the reference released) with the | |
44 // Callback object once completed. | |
21 void OnBooleanMethodWithErrorCallback( | 45 void OnBooleanMethodWithErrorCallback( |
46 ShillClientHelper::RefHolder* ref_holder, | |
22 const ShillClientHelper::BooleanCallback& callback, | 47 const ShillClientHelper::BooleanCallback& callback, |
23 const ShillClientHelper::ErrorCallback& error_callback, | 48 const ShillClientHelper::ErrorCallback& error_callback, |
24 dbus::Response* response) { | 49 dbus::Response* response) { |
25 if (!response) { | 50 if (!response) { |
26 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 51 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
27 return; | 52 return; |
28 } | 53 } |
29 dbus::MessageReader reader(response); | 54 dbus::MessageReader reader(response); |
30 bool result; | 55 bool result; |
31 if (!reader.PopBool(&result)) { | 56 if (!reader.PopBool(&result)) { |
32 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 57 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
33 return; | 58 return; |
34 } | 59 } |
35 callback.Run(result); | 60 callback.Run(result); |
36 } | 61 } |
37 | 62 |
38 void OnStringMethodWithErrorCallback( | 63 void OnStringMethodWithErrorCallback( |
64 ShillClientHelper::RefHolder* ref_holder, | |
39 const ShillClientHelper::StringCallback& callback, | 65 const ShillClientHelper::StringCallback& callback, |
40 const ShillClientHelper::ErrorCallback& error_callback, | 66 const ShillClientHelper::ErrorCallback& error_callback, |
41 dbus::Response* response) { | 67 dbus::Response* response) { |
42 if (!response) { | 68 if (!response) { |
43 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 69 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
44 return; | 70 return; |
45 } | 71 } |
46 dbus::MessageReader reader(response); | 72 dbus::MessageReader reader(response); |
47 std::string result; | 73 std::string result; |
48 if (!reader.PopString(&result)) { | 74 if (!reader.PopString(&result)) { |
49 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 75 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
50 return; | 76 return; |
51 } | 77 } |
52 callback.Run(result); | 78 callback.Run(result); |
53 } | 79 } |
54 | 80 |
55 // Handles responses for methods without results. | 81 // Handles responses for methods without results. |
56 void OnVoidMethod(const VoidDBusMethodCallback& callback, | 82 void OnVoidMethod(ShillClientHelper::RefHolder* ref_holder, |
83 const VoidDBusMethodCallback& callback, | |
57 dbus::Response* response) { | 84 dbus::Response* response) { |
58 if (!response) { | 85 if (!response) { |
59 callback.Run(DBUS_METHOD_CALL_FAILURE); | 86 callback.Run(DBUS_METHOD_CALL_FAILURE); |
60 return; | 87 return; |
61 } | 88 } |
62 callback.Run(DBUS_METHOD_CALL_SUCCESS); | 89 callback.Run(DBUS_METHOD_CALL_SUCCESS); |
63 } | 90 } |
64 | 91 |
65 // Handles responses for methods with ObjectPath results. | 92 // Handles responses for methods with ObjectPath results. |
66 void OnObjectPathMethod( | 93 void OnObjectPathMethod( |
94 ShillClientHelper::RefHolder* ref_holder, | |
67 const ObjectPathDBusMethodCallback& callback, | 95 const ObjectPathDBusMethodCallback& callback, |
68 dbus::Response* response) { | 96 dbus::Response* response) { |
69 if (!response) { | 97 if (!response) { |
70 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); | 98 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); |
71 return; | 99 return; |
72 } | 100 } |
73 dbus::MessageReader reader(response); | 101 dbus::MessageReader reader(response); |
74 dbus::ObjectPath result; | 102 dbus::ObjectPath result; |
75 if (!reader.PopObjectPath(&result)) { | 103 if (!reader.PopObjectPath(&result)) { |
76 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); | 104 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); |
77 return; | 105 return; |
78 } | 106 } |
79 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); | 107 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); |
80 } | 108 } |
81 | 109 |
82 // Handles responses for methods with ObjectPath results and no status. | 110 // Handles responses for methods with ObjectPath results and no status. |
83 void OnObjectPathMethodWithoutStatus( | 111 void OnObjectPathMethodWithoutStatus( |
112 ShillClientHelper::RefHolder* ref_holder, | |
84 const ObjectPathCallback& callback, | 113 const ObjectPathCallback& callback, |
85 const ShillClientHelper::ErrorCallback& error_callback, | 114 const ShillClientHelper::ErrorCallback& error_callback, |
86 dbus::Response* response) { | 115 dbus::Response* response) { |
87 if (!response) { | 116 if (!response) { |
88 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 117 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
89 return; | 118 return; |
90 } | 119 } |
91 dbus::MessageReader reader(response); | 120 dbus::MessageReader reader(response); |
92 dbus::ObjectPath result; | 121 dbus::ObjectPath result; |
93 if (!reader.PopObjectPath(&result)) { | 122 if (!reader.PopObjectPath(&result)) { |
94 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 123 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
95 return; | 124 return; |
96 } | 125 } |
97 callback.Run(result); | 126 callback.Run(result); |
98 } | 127 } |
99 | 128 |
100 // Handles responses for methods with DictionaryValue results. | 129 // Handles responses for methods with DictionaryValue results. |
101 void OnDictionaryValueMethod( | 130 void OnDictionaryValueMethod( |
131 ShillClientHelper::RefHolder* ref_holder, | |
102 const ShillClientHelper::DictionaryValueCallback& callback, | 132 const ShillClientHelper::DictionaryValueCallback& callback, |
103 dbus::Response* response) { | 133 dbus::Response* response) { |
104 if (!response) { | 134 if (!response) { |
105 base::DictionaryValue result; | 135 base::DictionaryValue result; |
106 callback.Run(DBUS_METHOD_CALL_FAILURE, result); | 136 callback.Run(DBUS_METHOD_CALL_FAILURE, result); |
107 return; | 137 return; |
108 } | 138 } |
109 dbus::MessageReader reader(response); | 139 dbus::MessageReader reader(response); |
110 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 140 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
111 base::DictionaryValue* result = NULL; | 141 base::DictionaryValue* result = NULL; |
112 if (!value.get() || !value->GetAsDictionary(&result)) { | 142 if (!value.get() || !value->GetAsDictionary(&result)) { |
113 base::DictionaryValue result; | 143 base::DictionaryValue result; |
114 callback.Run(DBUS_METHOD_CALL_FAILURE, result); | 144 callback.Run(DBUS_METHOD_CALL_FAILURE, result); |
115 return; | 145 return; |
116 } | 146 } |
117 callback.Run(DBUS_METHOD_CALL_SUCCESS, *result); | 147 callback.Run(DBUS_METHOD_CALL_SUCCESS, *result); |
118 } | 148 } |
119 | 149 |
120 // Handles responses for methods without results. | 150 // Handles responses for methods without results. |
121 void OnVoidMethodWithErrorCallback( | 151 void OnVoidMethodWithErrorCallback( |
152 ShillClientHelper::RefHolder* ref_holder, | |
122 const base::Closure& callback, | 153 const base::Closure& callback, |
123 dbus::Response* response) { | 154 dbus::Response* response) { |
124 callback.Run(); | 155 callback.Run(); |
125 } | 156 } |
126 | 157 |
127 // Handles responses for methods with DictionaryValue results. | 158 // Handles responses for methods with DictionaryValue results. |
128 // Used by CallDictionaryValueMethodWithErrorCallback(). | 159 // Used by CallDictionaryValueMethodWithErrorCallback(). |
129 void OnDictionaryValueMethodWithErrorCallback( | 160 void OnDictionaryValueMethodWithErrorCallback( |
161 ShillClientHelper::RefHolder* ref_holder, | |
130 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback, | 162 const ShillClientHelper::DictionaryValueCallbackWithoutStatus& callback, |
131 const ShillClientHelper::ErrorCallback& error_callback, | 163 const ShillClientHelper::ErrorCallback& error_callback, |
132 dbus::Response* response) { | 164 dbus::Response* response) { |
133 dbus::MessageReader reader(response); | 165 dbus::MessageReader reader(response); |
134 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 166 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
135 base::DictionaryValue* result = NULL; | 167 base::DictionaryValue* result = NULL; |
136 if (!value.get() || !value->GetAsDictionary(&result)) { | 168 if (!value.get() || !value->GetAsDictionary(&result)) { |
137 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 169 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
138 return; | 170 return; |
139 } | 171 } |
140 callback.Run(*result); | 172 callback.Run(*result); |
141 } | 173 } |
142 | 174 |
143 // Handles responses for methods with ListValue results. | 175 // Handles responses for methods with ListValue results. |
144 void OnListValueMethodWithErrorCallback( | 176 void OnListValueMethodWithErrorCallback( |
177 ShillClientHelper::RefHolder* ref_holder, | |
145 const ShillClientHelper::ListValueCallback& callback, | 178 const ShillClientHelper::ListValueCallback& callback, |
146 const ShillClientHelper::ErrorCallback& error_callback, | 179 const ShillClientHelper::ErrorCallback& error_callback, |
147 dbus::Response* response) { | 180 dbus::Response* response) { |
148 dbus::MessageReader reader(response); | 181 dbus::MessageReader reader(response); |
149 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 182 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
150 base::ListValue* result = NULL; | 183 base::ListValue* result = NULL; |
151 if (!value.get() || !value->GetAsList(&result)) { | 184 if (!value.get() || !value->GetAsList(&result)) { |
152 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 185 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
153 return; | 186 return; |
154 } | 187 } |
155 callback.Run(*result); | 188 callback.Run(*result); |
156 } | 189 } |
157 | 190 |
158 // Handles running appropriate error callbacks. | 191 // Handles running appropriate error callbacks. |
159 void OnError(const ShillClientHelper::ErrorCallback& error_callback, | 192 void OnError(const ShillClientHelper::ErrorCallback& error_callback, |
160 dbus::ErrorResponse* response) { | 193 dbus::ErrorResponse* response) { |
161 std::string error_name; | 194 std::string error_name; |
162 std::string error_message; | 195 std::string error_message; |
163 if (response) { | 196 if (response) { |
164 // Error message may contain the error message as string. | 197 // Error message may contain the error message as string. |
165 dbus::MessageReader reader(response); | 198 dbus::MessageReader reader(response); |
166 error_name = response->GetErrorName(); | 199 error_name = response->GetErrorName(); |
167 reader.PopString(&error_message); | 200 reader.PopString(&error_message); |
168 } | 201 } |
169 error_callback.Run(error_name, error_message); | 202 error_callback.Run(error_name, error_message); |
170 } | 203 } |
171 | 204 |
172 } // namespace | 205 } // namespace |
173 | 206 |
174 ShillClientHelper::ShillClientHelper(dbus::Bus* bus, | 207 ShillClientHelper::ShillClientHelper(dbus::ObjectProxy* proxy) |
175 dbus::ObjectProxy* proxy) | |
176 : proxy_(proxy), | 208 : proxy_(proxy), |
209 active_refs_(0), | |
177 weak_ptr_factory_(this) { | 210 weak_ptr_factory_(this) { |
178 } | 211 } |
179 | 212 |
180 ShillClientHelper::~ShillClientHelper() { | 213 ShillClientHelper::~ShillClientHelper() { |
181 LOG_IF(ERROR, observer_list_.might_have_observers()) | 214 LOG_IF(ERROR, observer_list_.might_have_observers()) |
182 << "ShillClientHelper destroyed with active observers"; | 215 << "ShillClientHelper destroyed with active observers"; |
183 } | 216 } |
184 | 217 |
218 void ShillClientHelper::SetReleasedCallback(ReleasedCallback callback) { | |
219 CHECK(released_callback_.is_null()); | |
220 released_callback_ = callback; | |
221 } | |
222 | |
185 void ShillClientHelper::AddPropertyChangedObserver( | 223 void ShillClientHelper::AddPropertyChangedObserver( |
186 ShillPropertyChangedObserver* observer) { | 224 ShillPropertyChangedObserver* observer) { |
225 AddRef(); | |
187 // Excecute all the pending MonitorPropertyChanged calls. | 226 // Excecute all the pending MonitorPropertyChanged calls. |
188 for (size_t i = 0; i < interfaces_to_be_monitored_.size(); ++i) { | 227 for (size_t i = 0; i < interfaces_to_be_monitored_.size(); ++i) { |
189 MonitorPropertyChangedInternal(interfaces_to_be_monitored_[i]); | 228 MonitorPropertyChangedInternal(interfaces_to_be_monitored_[i]); |
190 } | 229 } |
191 interfaces_to_be_monitored_.clear(); | 230 interfaces_to_be_monitored_.clear(); |
192 | 231 |
193 observer_list_.AddObserver(observer); | 232 observer_list_.AddObserver(observer); |
194 } | 233 } |
195 | 234 |
196 void ShillClientHelper::RemovePropertyChangedObserver( | 235 void ShillClientHelper::RemovePropertyChangedObserver( |
197 ShillPropertyChangedObserver* observer) { | 236 ShillPropertyChangedObserver* observer) { |
198 observer_list_.RemoveObserver(observer); | 237 observer_list_.RemoveObserver(observer); |
238 Release(); | |
hashimoto
2013/09/18 04:32:30
question: Can we be sure that the users of this cl
stevenjb
2013/09/18 16:58:28
Good suggestion. Done. (Note: ObserverList::AddObs
| |
199 } | 239 } |
200 | 240 |
201 void ShillClientHelper::MonitorPropertyChanged( | 241 void ShillClientHelper::MonitorPropertyChanged( |
202 const std::string& interface_name) { | 242 const std::string& interface_name) { |
203 if (observer_list_.might_have_observers()) { | 243 if (observer_list_.might_have_observers()) { |
204 // Effectively monitor the PropertyChanged now. | 244 // Effectively monitor the PropertyChanged now. |
205 MonitorPropertyChangedInternal(interface_name); | 245 MonitorPropertyChangedInternal(interface_name); |
206 } else { | 246 } else { |
207 // Delay the ConnectToSignal until an observer is added. | 247 // Delay the ConnectToSignal until an observer is added. |
208 interfaces_to_be_monitored_.push_back(interface_name); | 248 interfaces_to_be_monitored_.push_back(interface_name); |
209 } | 249 } |
210 } | 250 } |
211 | 251 |
212 void ShillClientHelper::MonitorPropertyChangedInternal( | 252 void ShillClientHelper::MonitorPropertyChangedInternal( |
213 const std::string& interface_name) { | 253 const std::string& interface_name) { |
214 // We are not using dbus::PropertySet to monitor PropertyChanged signal | 254 // We are not using dbus::PropertySet to monitor PropertyChanged signal |
215 // because the interface is not "org.freedesktop.DBus.Properties". | 255 // because the interface is not "org.freedesktop.DBus.Properties". |
216 proxy_->ConnectToSignal(interface_name, | 256 proxy_->ConnectToSignal(interface_name, |
217 flimflam::kMonitorPropertyChanged, | 257 flimflam::kMonitorPropertyChanged, |
218 base::Bind(&ShillClientHelper::OnPropertyChanged, | 258 base::Bind(&ShillClientHelper::OnPropertyChanged, |
219 weak_ptr_factory_.GetWeakPtr()), | 259 weak_ptr_factory_.GetWeakPtr()), |
220 base::Bind(&ShillClientHelper::OnSignalConnected, | 260 base::Bind(&ShillClientHelper::OnSignalConnected, |
221 weak_ptr_factory_.GetWeakPtr())); | 261 weak_ptr_factory_.GetWeakPtr())); |
222 } | 262 } |
223 | 263 |
224 void ShillClientHelper::CallVoidMethod( | 264 void ShillClientHelper::CallVoidMethod( |
225 dbus::MethodCall* method_call, | 265 dbus::MethodCall* method_call, |
226 const VoidDBusMethodCallback& callback) { | 266 const VoidDBusMethodCallback& callback) { |
227 DCHECK(!callback.is_null()); | 267 DCHECK(!callback.is_null()); |
228 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 268 proxy_->CallMethod( |
229 base::Bind(&OnVoidMethod, | 269 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
230 callback)); | 270 base::Bind(&OnVoidMethod, |
271 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), | |
272 callback)); | |
231 } | 273 } |
232 | 274 |
233 void ShillClientHelper::CallObjectPathMethod( | 275 void ShillClientHelper::CallObjectPathMethod( |
234 dbus::MethodCall* method_call, | 276 dbus::MethodCall* method_call, |
235 const ObjectPathDBusMethodCallback& callback) { | 277 const ObjectPathDBusMethodCallback& callback) { |
236 DCHECK(!callback.is_null()); | 278 DCHECK(!callback.is_null()); |
237 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 279 proxy_->CallMethod( |
238 base::Bind(&OnObjectPathMethod, | 280 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
239 callback)); | 281 base::Bind(&OnObjectPathMethod, |
282 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), | |
283 callback)); | |
240 } | 284 } |
241 | 285 |
242 void ShillClientHelper::CallObjectPathMethodWithErrorCallback( | 286 void ShillClientHelper::CallObjectPathMethodWithErrorCallback( |
243 dbus::MethodCall* method_call, | 287 dbus::MethodCall* method_call, |
244 const ObjectPathCallback& callback, | 288 const ObjectPathCallback& callback, |
245 const ErrorCallback& error_callback) { | 289 const ErrorCallback& error_callback) { |
246 DCHECK(!callback.is_null()); | 290 DCHECK(!callback.is_null()); |
247 DCHECK(!error_callback.is_null()); | 291 DCHECK(!error_callback.is_null()); |
248 proxy_->CallMethodWithErrorCallback( | 292 proxy_->CallMethodWithErrorCallback( |
249 method_call, | 293 method_call, |
250 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 294 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
251 base::Bind(&OnObjectPathMethodWithoutStatus, | 295 base::Bind(&OnObjectPathMethodWithoutStatus, |
296 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), | |
252 callback, | 297 callback, |
253 error_callback), | 298 error_callback), |
254 base::Bind(&OnError, | 299 base::Bind(&OnError, |
255 error_callback)); | 300 error_callback)); |
256 } | 301 } |
257 | 302 |
258 void ShillClientHelper::CallDictionaryValueMethod( | 303 void ShillClientHelper::CallDictionaryValueMethod( |
259 dbus::MethodCall* method_call, | 304 dbus::MethodCall* method_call, |
260 const DictionaryValueCallback& callback) { | 305 const DictionaryValueCallback& callback) { |
261 DCHECK(!callback.is_null()); | 306 DCHECK(!callback.is_null()); |
262 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 307 proxy_->CallMethod( |
263 base::Bind(&OnDictionaryValueMethod, | 308 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
264 callback)); | 309 base::Bind(&OnDictionaryValueMethod, |
310 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), | |
311 callback)); | |
265 } | 312 } |
266 | 313 |
267 void ShillClientHelper::CallVoidMethodWithErrorCallback( | 314 void ShillClientHelper::CallVoidMethodWithErrorCallback( |
268 dbus::MethodCall* method_call, | 315 dbus::MethodCall* method_call, |
269 const base::Closure& callback, | 316 const base::Closure& callback, |
270 const ErrorCallback& error_callback) { | 317 const ErrorCallback& error_callback) { |
271 DCHECK(!callback.is_null()); | 318 DCHECK(!callback.is_null()); |
272 DCHECK(!error_callback.is_null()); | 319 DCHECK(!error_callback.is_null()); |
273 proxy_->CallMethodWithErrorCallback( | 320 proxy_->CallMethodWithErrorCallback( |
274 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 321 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
275 base::Bind(&OnVoidMethodWithErrorCallback, | 322 base::Bind(&OnVoidMethodWithErrorCallback, |
323 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), | |
276 callback), | 324 callback), |
277 base::Bind(&OnError, | 325 base::Bind(&OnError, |
278 error_callback)); | 326 error_callback)); |
279 } | 327 } |
280 | 328 |
281 void ShillClientHelper::CallBooleanMethodWithErrorCallback( | 329 void ShillClientHelper::CallBooleanMethodWithErrorCallback( |
282 dbus::MethodCall* method_call, | 330 dbus::MethodCall* method_call, |
283 const BooleanCallback& callback, | 331 const BooleanCallback& callback, |
284 const ErrorCallback& error_callback) { | 332 const ErrorCallback& error_callback) { |
285 DCHECK(!callback.is_null()); | 333 DCHECK(!callback.is_null()); |
286 DCHECK(!error_callback.is_null()); | 334 DCHECK(!error_callback.is_null()); |
287 proxy_->CallMethodWithErrorCallback( | 335 proxy_->CallMethodWithErrorCallback( |
288 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 336 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
289 base::Bind(&OnBooleanMethodWithErrorCallback, | 337 base::Bind(&OnBooleanMethodWithErrorCallback, |
338 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), | |
290 callback, | 339 callback, |
291 error_callback), | 340 error_callback), |
292 base::Bind(&OnError, | 341 base::Bind(&OnError, |
293 error_callback)); | 342 error_callback)); |
294 } | 343 } |
295 | 344 |
296 void ShillClientHelper::CallStringMethodWithErrorCallback( | 345 void ShillClientHelper::CallStringMethodWithErrorCallback( |
297 dbus::MethodCall* method_call, | 346 dbus::MethodCall* method_call, |
298 const StringCallback& callback, | 347 const StringCallback& callback, |
299 const ErrorCallback& error_callback) { | 348 const ErrorCallback& error_callback) { |
300 DCHECK(!callback.is_null()); | 349 DCHECK(!callback.is_null()); |
301 DCHECK(!error_callback.is_null()); | 350 DCHECK(!error_callback.is_null()); |
302 proxy_->CallMethodWithErrorCallback( | 351 proxy_->CallMethodWithErrorCallback( |
303 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 352 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
304 base::Bind(&OnStringMethodWithErrorCallback, | 353 base::Bind(&OnStringMethodWithErrorCallback, |
354 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), | |
305 callback, | 355 callback, |
306 error_callback), | 356 error_callback), |
307 base::Bind(&OnError, | 357 base::Bind(&OnError, |
308 error_callback)); | 358 error_callback)); |
309 } | 359 } |
310 | 360 |
311 void ShillClientHelper::CallDictionaryValueMethodWithErrorCallback( | 361 void ShillClientHelper::CallDictionaryValueMethodWithErrorCallback( |
312 dbus::MethodCall* method_call, | 362 dbus::MethodCall* method_call, |
313 const DictionaryValueCallbackWithoutStatus& callback, | 363 const DictionaryValueCallbackWithoutStatus& callback, |
314 const ErrorCallback& error_callback) { | 364 const ErrorCallback& error_callback) { |
315 DCHECK(!callback.is_null()); | 365 DCHECK(!callback.is_null()); |
316 DCHECK(!error_callback.is_null()); | 366 DCHECK(!error_callback.is_null()); |
317 proxy_->CallMethodWithErrorCallback( | 367 proxy_->CallMethodWithErrorCallback( |
318 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 368 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
319 base::Bind( | 369 base::Bind(&OnDictionaryValueMethodWithErrorCallback, |
320 &OnDictionaryValueMethodWithErrorCallback, | 370 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
321 callback, | 371 callback, |
322 error_callback), | 372 error_callback), |
323 base::Bind(&OnError, | 373 base::Bind(&OnError, |
324 error_callback)); | 374 error_callback)); |
325 } | 375 } |
326 | 376 |
327 void ShillClientHelper::CallListValueMethodWithErrorCallback( | 377 void ShillClientHelper::CallListValueMethodWithErrorCallback( |
328 dbus::MethodCall* method_call, | 378 dbus::MethodCall* method_call, |
329 const ListValueCallback& callback, | 379 const ListValueCallback& callback, |
330 const ErrorCallback& error_callback) { | 380 const ErrorCallback& error_callback) { |
331 DCHECK(!callback.is_null()); | 381 DCHECK(!callback.is_null()); |
332 DCHECK(!error_callback.is_null()); | 382 DCHECK(!error_callback.is_null()); |
333 proxy_->CallMethodWithErrorCallback( | 383 proxy_->CallMethodWithErrorCallback( |
334 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 384 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
335 base::Bind( | 385 base::Bind(&OnListValueMethodWithErrorCallback, |
336 &OnListValueMethodWithErrorCallback, | 386 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
337 callback, | 387 callback, |
338 error_callback), | 388 error_callback), |
339 base::Bind(&OnError, | 389 base::Bind(&OnError, |
340 error_callback)); | 390 error_callback)); |
341 } | 391 } |
342 | 392 |
343 // static | 393 // static |
344 void ShillClientHelper::AppendValueDataAsVariant(dbus::MessageWriter* writer, | 394 void ShillClientHelper::AppendValueDataAsVariant(dbus::MessageWriter* writer, |
345 const base::Value& value) { | 395 const base::Value& value) { |
346 // Support basic types and string-to-string dictionary. | 396 // Support basic types and string-to-string dictionary. |
347 switch (value.GetType()) { | 397 switch (value.GetType()) { |
348 case base::Value::TYPE_DICTIONARY: { | 398 case base::Value::TYPE_DICTIONARY: { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
413 it.Advance()) { | 463 it.Advance()) { |
414 dbus::MessageWriter entry_writer(NULL); | 464 dbus::MessageWriter entry_writer(NULL); |
415 array_writer.OpenDictEntry(&entry_writer); | 465 array_writer.OpenDictEntry(&entry_writer); |
416 entry_writer.AppendString(it.key()); | 466 entry_writer.AppendString(it.key()); |
417 ShillClientHelper::AppendValueDataAsVariant(&entry_writer, it.value()); | 467 ShillClientHelper::AppendValueDataAsVariant(&entry_writer, it.value()); |
418 array_writer.CloseContainer(&entry_writer); | 468 array_writer.CloseContainer(&entry_writer); |
419 } | 469 } |
420 writer->CloseContainer(&array_writer); | 470 writer->CloseContainer(&array_writer); |
421 } | 471 } |
422 | 472 |
473 void ShillClientHelper::AddRef() { | |
474 ++active_refs_; | |
475 } | |
476 | |
477 void ShillClientHelper::Release() { | |
478 --active_refs_; | |
479 if (active_refs_ == 0 && !released_callback_.is_null()) | |
480 released_callback_.Run(this); // May delete this | |
hashimoto
2013/09/18 04:32:30
nit: base::ResetAndReturn(&released_callback_).Run
stevenjb
2013/09/18 16:58:28
Hmm, good suggestion. Hopefully that wouldn't be a
| |
481 } | |
482 | |
423 void ShillClientHelper::OnSignalConnected(const std::string& interface, | 483 void ShillClientHelper::OnSignalConnected(const std::string& interface, |
424 const std::string& signal, | 484 const std::string& signal, |
425 bool success) { | 485 bool success) { |
426 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal | 486 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal |
427 << " failed."; | 487 << " failed."; |
428 } | 488 } |
429 | 489 |
430 void ShillClientHelper::OnPropertyChanged(dbus::Signal* signal) { | 490 void ShillClientHelper::OnPropertyChanged(dbus::Signal* signal) { |
431 if (!observer_list_.might_have_observers()) | 491 if (!observer_list_.might_have_observers()) |
432 return; | 492 return; |
433 | 493 |
434 dbus::MessageReader reader(signal); | 494 dbus::MessageReader reader(signal); |
435 std::string name; | 495 std::string name; |
436 if (!reader.PopString(&name)) | 496 if (!reader.PopString(&name)) |
437 return; | 497 return; |
438 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 498 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
439 if (!value.get()) | 499 if (!value.get()) |
440 return; | 500 return; |
441 | 501 |
442 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, observer_list_, | 502 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, observer_list_, |
443 OnPropertyChanged(name, *value)); | 503 OnPropertyChanged(name, *value)); |
444 } | 504 } |
445 | 505 |
446 | |
447 } // namespace chromeos | 506 } // namespace chromeos |
OLD | NEW |