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 namespace { | 16 namespace { |
17 | 17 |
18 const char kInvalidResponseErrorName[] = ""; // No error name. | 18 const char kInvalidResponseErrorName[] = ""; // No error name. |
19 const char kInvalidResponseErrorMessage[] = "Invalid response."; | 19 const char kInvalidResponseErrorMessage[] = "Invalid response."; |
20 | 20 |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 ShillClientHelper::ShillClientHelper(dbus::Bus* bus, | 23 ShillClientHelper::ShillClientHelper(dbus::Bus* bus, |
24 dbus::ObjectProxy* proxy) | 24 dbus::ObjectProxy* proxy) |
25 : blocking_method_caller_(bus, proxy), | 25 : blocking_method_caller_(bus, proxy), |
26 proxy_(proxy), | 26 proxy_(proxy), |
27 ongoing_calls(0), | |
27 weak_ptr_factory_(this) { | 28 weak_ptr_factory_(this) { |
hashimoto
2013/02/06 03:29:06
nit: Could you add ALLOW_THIS_IN_INITIALIZER_LIST
deymo
2013/02/06 05:44:45
Done.
| |
28 } | 29 } |
29 | 30 |
30 ShillClientHelper::~ShillClientHelper() { | 31 ShillClientHelper::~ShillClientHelper() { |
31 LOG_IF(ERROR, observer_list_.size() != 0u) | 32 LOG_IF(ERROR, observer_list_.size() != 0u) |
32 << "ShillClientHelper destroyed with active observers: " | 33 << "ShillClientHelper destroyed with active observers: " |
33 << observer_list_.size(); | 34 << observer_list_.size(); |
34 } | 35 } |
35 | 36 |
37 bool ShillClientHelper::IsObserved() const { | |
38 return observer_list_.size() > 0; | |
39 } | |
40 | |
41 bool ShillClientHelper::IsWaitingResponse() const { | |
42 return ongoing_calls > 0; | |
hashimoto
2013/02/06 03:29:06
How about adding another base::WeakPtrFactory dedi
deymo
2013/02/06 05:44:45
I need this to return false in the case of the las
hashimoto
2013/02/07 05:20:31
You're correct.
| |
43 } | |
44 | |
36 void ShillClientHelper::AddPropertyChangedObserver( | 45 void ShillClientHelper::AddPropertyChangedObserver( |
37 ShillPropertyChangedObserver* observer) { | 46 ShillPropertyChangedObserver* observer) { |
38 observer_list_.AddObserver(observer); | 47 observer_list_.AddObserver(observer); |
39 } | 48 } |
40 | 49 |
41 void ShillClientHelper::RemovePropertyChangedObserver( | 50 void ShillClientHelper::RemovePropertyChangedObserver( |
42 ShillPropertyChangedObserver* observer) { | 51 ShillPropertyChangedObserver* observer) { |
43 observer_list_.RemoveObserver(observer); | 52 observer_list_.RemoveObserver(observer); |
44 } | 53 } |
45 | 54 |
46 void ShillClientHelper::MonitorPropertyChanged( | 55 void ShillClientHelper::MonitorPropertyChanged( |
47 const std::string& interface_name) { | 56 const std::string& interface_name) { |
48 // We are not using dbus::PropertySet to monitor PropertyChanged signal | 57 // We are not using dbus::PropertySet to monitor PropertyChanged signal |
49 // because the interface is not "org.freedesktop.DBus.Properties". | 58 // because the interface is not "org.freedesktop.DBus.Properties". |
50 proxy_->ConnectToSignal(interface_name, | 59 proxy_->ConnectToSignal(interface_name, |
51 flimflam::kMonitorPropertyChanged, | 60 flimflam::kMonitorPropertyChanged, |
52 base::Bind(&ShillClientHelper::OnPropertyChanged, | 61 base::Bind(&ShillClientHelper::OnPropertyChanged, |
53 weak_ptr_factory_.GetWeakPtr()), | 62 weak_ptr_factory_.GetWeakPtr()), |
54 base::Bind(&ShillClientHelper::OnSignalConnected, | 63 base::Bind(&ShillClientHelper::OnSignalConnected, |
55 weak_ptr_factory_.GetWeakPtr())); | 64 weak_ptr_factory_.GetWeakPtr())); |
56 } | 65 } |
57 | 66 |
58 void ShillClientHelper::CallVoidMethod( | 67 void ShillClientHelper::CallVoidMethod( |
59 dbus::MethodCall* method_call, | 68 dbus::MethodCall* method_call, |
60 const VoidDBusMethodCallback& callback) { | 69 const VoidDBusMethodCallback& callback) { |
70 ++ongoing_calls; | |
61 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 71 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
62 base::Bind(&ShillClientHelper::OnVoidMethod, | 72 base::Bind(&ShillClientHelper::OnVoidMethod, |
63 weak_ptr_factory_.GetWeakPtr(), | 73 weak_ptr_factory_.GetWeakPtr(), |
64 callback)); | 74 callback)); |
65 } | 75 } |
66 | 76 |
67 void ShillClientHelper::CallObjectPathMethod( | 77 void ShillClientHelper::CallObjectPathMethod( |
68 dbus::MethodCall* method_call, | 78 dbus::MethodCall* method_call, |
69 const ObjectPathDBusMethodCallback& callback) { | 79 const ObjectPathDBusMethodCallback& callback) { |
80 ++ongoing_calls; | |
70 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 81 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
71 base::Bind(&ShillClientHelper::OnObjectPathMethod, | 82 base::Bind(&ShillClientHelper::OnObjectPathMethod, |
72 weak_ptr_factory_.GetWeakPtr(), | 83 weak_ptr_factory_.GetWeakPtr(), |
73 callback)); | 84 callback)); |
74 } | 85 } |
75 | 86 |
76 void ShillClientHelper::CallObjectPathMethodWithErrorCallback( | 87 void ShillClientHelper::CallObjectPathMethodWithErrorCallback( |
77 dbus::MethodCall* method_call, | 88 dbus::MethodCall* method_call, |
78 const ObjectPathCallback& callback, | 89 const ObjectPathCallback& callback, |
79 const ErrorCallback& error_callback) { | 90 const ErrorCallback& error_callback) { |
91 ++ongoing_calls; | |
80 proxy_->CallMethodWithErrorCallback( | 92 proxy_->CallMethodWithErrorCallback( |
81 method_call, | 93 method_call, |
82 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 94 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
83 base::Bind(&ShillClientHelper::OnObjectPathMethodWithoutStatus, | 95 base::Bind(&ShillClientHelper::OnObjectPathMethodWithoutStatus, |
84 weak_ptr_factory_.GetWeakPtr(), | 96 weak_ptr_factory_.GetWeakPtr(), |
85 callback, | 97 callback, |
86 error_callback), | 98 error_callback), |
87 base::Bind(&ShillClientHelper::OnError, | 99 base::Bind(&ShillClientHelper::OnError, |
88 weak_ptr_factory_.GetWeakPtr(), | 100 weak_ptr_factory_.GetWeakPtr(), |
89 error_callback)); | 101 error_callback)); |
90 } | 102 } |
91 | 103 |
92 void ShillClientHelper::CallDictionaryValueMethod( | 104 void ShillClientHelper::CallDictionaryValueMethod( |
93 dbus::MethodCall* method_call, | 105 dbus::MethodCall* method_call, |
94 const DictionaryValueCallback& callback) { | 106 const DictionaryValueCallback& callback) { |
107 ++ongoing_calls; | |
95 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 108 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
96 base::Bind(&ShillClientHelper::OnDictionaryValueMethod, | 109 base::Bind(&ShillClientHelper::OnDictionaryValueMethod, |
97 weak_ptr_factory_.GetWeakPtr(), | 110 weak_ptr_factory_.GetWeakPtr(), |
98 callback)); | 111 callback)); |
99 } | 112 } |
100 | 113 |
101 void ShillClientHelper::CallVoidMethodWithErrorCallback( | 114 void ShillClientHelper::CallVoidMethodWithErrorCallback( |
102 dbus::MethodCall* method_call, | 115 dbus::MethodCall* method_call, |
103 const base::Closure& callback, | 116 const base::Closure& callback, |
104 const ErrorCallback& error_callback) { | 117 const ErrorCallback& error_callback) { |
118 ++ongoing_calls; | |
105 proxy_->CallMethodWithErrorCallback( | 119 proxy_->CallMethodWithErrorCallback( |
106 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 120 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
107 base::Bind(&ShillClientHelper::OnVoidMethodWithErrorCallback, | 121 base::Bind(&ShillClientHelper::OnVoidMethodWithErrorCallback, |
108 weak_ptr_factory_.GetWeakPtr(), | 122 weak_ptr_factory_.GetWeakPtr(), |
109 callback), | 123 callback), |
110 base::Bind(&ShillClientHelper::OnError, | 124 base::Bind(&ShillClientHelper::OnError, |
111 weak_ptr_factory_.GetWeakPtr(), | 125 weak_ptr_factory_.GetWeakPtr(), |
112 error_callback)); | 126 error_callback)); |
113 } | 127 } |
114 | 128 |
115 void ShillClientHelper::CallDictionaryValueMethodWithErrorCallback( | 129 void ShillClientHelper::CallDictionaryValueMethodWithErrorCallback( |
116 dbus::MethodCall* method_call, | 130 dbus::MethodCall* method_call, |
117 const DictionaryValueCallbackWithoutStatus& callback, | 131 const DictionaryValueCallbackWithoutStatus& callback, |
118 const ErrorCallback& error_callback) { | 132 const ErrorCallback& error_callback) { |
133 ++ongoing_calls; | |
119 proxy_->CallMethodWithErrorCallback( | 134 proxy_->CallMethodWithErrorCallback( |
120 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 135 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
121 base::Bind( | 136 base::Bind( |
122 &ShillClientHelper::OnDictionaryValueMethodWithErrorCallback, | 137 &ShillClientHelper::OnDictionaryValueMethodWithErrorCallback, |
123 weak_ptr_factory_.GetWeakPtr(), | 138 weak_ptr_factory_.GetWeakPtr(), |
124 callback, | 139 callback, |
125 error_callback), | 140 error_callback), |
126 base::Bind(&ShillClientHelper::OnError, | 141 base::Bind(&ShillClientHelper::OnError, |
127 weak_ptr_factory_.GetWeakPtr(), | 142 weak_ptr_factory_.GetWeakPtr(), |
128 error_callback)); | 143 error_callback)); |
129 } | 144 } |
130 | 145 |
131 void ShillClientHelper::CallListValueMethodWithErrorCallback( | 146 void ShillClientHelper::CallListValueMethodWithErrorCallback( |
132 dbus::MethodCall* method_call, | 147 dbus::MethodCall* method_call, |
133 const ListValueCallback& callback, | 148 const ListValueCallback& callback, |
134 const ErrorCallback& error_callback) { | 149 const ErrorCallback& error_callback) { |
150 ++ongoing_calls; | |
135 proxy_->CallMethodWithErrorCallback( | 151 proxy_->CallMethodWithErrorCallback( |
136 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 152 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
137 base::Bind( | 153 base::Bind( |
138 &ShillClientHelper::OnListValueMethodWithErrorCallback, | 154 &ShillClientHelper::OnListValueMethodWithErrorCallback, |
139 weak_ptr_factory_.GetWeakPtr(), | 155 weak_ptr_factory_.GetWeakPtr(), |
140 callback, | 156 callback, |
141 error_callback), | 157 error_callback), |
142 base::Bind(&ShillClientHelper::OnError, | 158 base::Bind(&ShillClientHelper::OnError, |
143 weak_ptr_factory_.GetWeakPtr(), | 159 weak_ptr_factory_.GetWeakPtr(), |
144 error_callback)); | 160 error_callback)); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 246 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
231 if (!value.get()) | 247 if (!value.get()) |
232 return; | 248 return; |
233 | 249 |
234 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, observer_list_, | 250 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, observer_list_, |
235 OnPropertyChanged(name, *value)); | 251 OnPropertyChanged(name, *value)); |
236 } | 252 } |
237 | 253 |
238 void ShillClientHelper::OnVoidMethod(const VoidDBusMethodCallback& callback, | 254 void ShillClientHelper::OnVoidMethod(const VoidDBusMethodCallback& callback, |
239 dbus::Response* response) { | 255 dbus::Response* response) { |
256 --ongoing_calls; | |
240 if (!response) { | 257 if (!response) { |
241 callback.Run(DBUS_METHOD_CALL_FAILURE); | 258 callback.Run(DBUS_METHOD_CALL_FAILURE); |
242 return; | 259 return; |
243 } | 260 } |
244 callback.Run(DBUS_METHOD_CALL_SUCCESS); | 261 callback.Run(DBUS_METHOD_CALL_SUCCESS); |
245 } | 262 } |
246 | 263 |
247 void ShillClientHelper::OnObjectPathMethod( | 264 void ShillClientHelper::OnObjectPathMethod( |
248 const ObjectPathDBusMethodCallback& callback, | 265 const ObjectPathDBusMethodCallback& callback, |
249 dbus::Response* response) { | 266 dbus::Response* response) { |
267 --ongoing_calls; | |
250 if (!response) { | 268 if (!response) { |
251 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); | 269 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); |
252 return; | 270 return; |
253 } | 271 } |
254 dbus::MessageReader reader(response); | 272 dbus::MessageReader reader(response); |
255 dbus::ObjectPath result; | 273 dbus::ObjectPath result; |
256 if (!reader.PopObjectPath(&result)) { | 274 if (!reader.PopObjectPath(&result)) { |
257 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); | 275 callback.Run(DBUS_METHOD_CALL_FAILURE, dbus::ObjectPath()); |
258 return; | 276 return; |
259 } | 277 } |
260 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); | 278 callback.Run(DBUS_METHOD_CALL_SUCCESS, result); |
261 } | 279 } |
262 | 280 |
263 void ShillClientHelper::OnObjectPathMethodWithoutStatus( | 281 void ShillClientHelper::OnObjectPathMethodWithoutStatus( |
264 const ObjectPathCallback& callback, | 282 const ObjectPathCallback& callback, |
265 const ErrorCallback& error_callback, | 283 const ErrorCallback& error_callback, |
266 dbus::Response* response) { | 284 dbus::Response* response) { |
285 --ongoing_calls; | |
267 if (!response) { | 286 if (!response) { |
268 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 287 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
269 return; | 288 return; |
270 } | 289 } |
271 dbus::MessageReader reader(response); | 290 dbus::MessageReader reader(response); |
272 dbus::ObjectPath result; | 291 dbus::ObjectPath result; |
273 if (!reader.PopObjectPath(&result)) { | 292 if (!reader.PopObjectPath(&result)) { |
274 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 293 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
275 return; | 294 return; |
276 } | 295 } |
277 callback.Run(result); | 296 callback.Run(result); |
278 } | 297 } |
279 | 298 |
280 void ShillClientHelper::OnDictionaryValueMethod( | 299 void ShillClientHelper::OnDictionaryValueMethod( |
281 const DictionaryValueCallback& callback, | 300 const DictionaryValueCallback& callback, |
282 dbus::Response* response) { | 301 dbus::Response* response) { |
302 --ongoing_calls; | |
283 if (!response) { | 303 if (!response) { |
284 base::DictionaryValue result; | 304 base::DictionaryValue result; |
285 callback.Run(DBUS_METHOD_CALL_FAILURE, result); | 305 callback.Run(DBUS_METHOD_CALL_FAILURE, result); |
286 return; | 306 return; |
287 } | 307 } |
288 dbus::MessageReader reader(response); | 308 dbus::MessageReader reader(response); |
289 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 309 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
290 base::DictionaryValue* result = NULL; | 310 base::DictionaryValue* result = NULL; |
291 if (!value.get() || !value->GetAsDictionary(&result)) { | 311 if (!value.get() || !value->GetAsDictionary(&result)) { |
292 base::DictionaryValue result; | 312 base::DictionaryValue result; |
293 callback.Run(DBUS_METHOD_CALL_FAILURE, result); | 313 callback.Run(DBUS_METHOD_CALL_FAILURE, result); |
294 return; | 314 return; |
295 } | 315 } |
296 callback.Run(DBUS_METHOD_CALL_SUCCESS, *result); | 316 callback.Run(DBUS_METHOD_CALL_SUCCESS, *result); |
297 } | 317 } |
298 | 318 |
299 void ShillClientHelper::OnVoidMethodWithErrorCallback( | 319 void ShillClientHelper::OnVoidMethodWithErrorCallback( |
300 const base::Closure& callback, | 320 const base::Closure& callback, |
301 dbus::Response* response) { | 321 dbus::Response* response) { |
322 --ongoing_calls; | |
302 callback.Run(); | 323 callback.Run(); |
303 } | 324 } |
304 | 325 |
305 void ShillClientHelper::OnDictionaryValueMethodWithErrorCallback( | 326 void ShillClientHelper::OnDictionaryValueMethodWithErrorCallback( |
306 const DictionaryValueCallbackWithoutStatus& callback, | 327 const DictionaryValueCallbackWithoutStatus& callback, |
307 const ErrorCallback& error_callback, | 328 const ErrorCallback& error_callback, |
308 dbus::Response* response) { | 329 dbus::Response* response) { |
330 --ongoing_calls; | |
309 dbus::MessageReader reader(response); | 331 dbus::MessageReader reader(response); |
310 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 332 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
311 base::DictionaryValue* result = NULL; | 333 base::DictionaryValue* result = NULL; |
312 if (!value.get() || !value->GetAsDictionary(&result)) { | 334 if (!value.get() || !value->GetAsDictionary(&result)) { |
313 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 335 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
314 return; | 336 return; |
315 } | 337 } |
316 callback.Run(*result); | 338 callback.Run(*result); |
317 } | 339 } |
318 | 340 |
319 void ShillClientHelper::OnListValueMethodWithErrorCallback( | 341 void ShillClientHelper::OnListValueMethodWithErrorCallback( |
320 const ListValueCallback& callback, | 342 const ListValueCallback& callback, |
321 const ErrorCallback& error_callback, | 343 const ErrorCallback& error_callback, |
322 dbus::Response* response) { | 344 dbus::Response* response) { |
345 --ongoing_calls; | |
323 dbus::MessageReader reader(response); | 346 dbus::MessageReader reader(response); |
324 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 347 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
325 base::ListValue* result = NULL; | 348 base::ListValue* result = NULL; |
326 if (!value.get() || !value->GetAsList(&result)) { | 349 if (!value.get() || !value->GetAsList(&result)) { |
327 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); | 350 error_callback.Run(kInvalidResponseErrorName, kInvalidResponseErrorMessage); |
328 return; | 351 return; |
329 } | 352 } |
330 callback.Run(*result); | 353 callback.Run(*result); |
331 } | 354 } |
332 | 355 |
333 void ShillClientHelper::OnError(const ErrorCallback& error_callback, | 356 void ShillClientHelper::OnError(const ErrorCallback& error_callback, |
334 dbus::ErrorResponse* response) { | 357 dbus::ErrorResponse* response) { |
358 --ongoing_calls; | |
335 std::string error_name; | 359 std::string error_name; |
336 std::string error_message; | 360 std::string error_message; |
337 if (response) { | 361 if (response) { |
338 // Error message may contain the error message as string. | 362 // Error message may contain the error message as string. |
339 dbus::MessageReader reader(response); | 363 dbus::MessageReader reader(response); |
340 error_name = response->GetErrorName(); | 364 error_name = response->GetErrorName(); |
341 reader.PopString(&error_message); | 365 reader.PopString(&error_message); |
342 } | 366 } |
343 error_callback.Run(error_name, error_message); | 367 error_callback.Run(error_name, error_message); |
344 } | 368 } |
345 | 369 |
346 } // namespace chromeos | 370 } // namespace chromeos |
OLD | NEW |