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/callback_helpers.h" |
8 #include "base/values.h" | 9 #include "base/values.h" |
9 #include "dbus/message.h" | 10 #include "dbus/message.h" |
10 #include "dbus/object_proxy.h" | 11 #include "dbus/object_proxy.h" |
11 #include "dbus/values_util.h" | 12 #include "dbus/values_util.h" |
12 #include "third_party/cros_system_api/dbus/service_constants.h" | 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
13 | 14 |
14 namespace chromeos { | 15 namespace chromeos { |
15 | 16 |
| 17 // Class to hold onto a reference to a ShillClientHelper. This calss |
| 18 // is owned by callbacks and released once the callback completes. |
| 19 // Note: Only success callbacks hold the reference. If an error callback is |
| 20 // invoked instead, the success callback will still be destroyed and the |
| 21 // RefHolder with it, once the callback chain completes. |
| 22 class ShillClientHelper::RefHolder { |
| 23 public: |
| 24 explicit RefHolder(base::WeakPtr<ShillClientHelper> helper) |
| 25 : helper_(helper) { |
| 26 helper_->AddRef(); |
| 27 } |
| 28 ~RefHolder() { |
| 29 if (helper_) |
| 30 helper_->Release(); |
| 31 } |
| 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 if (observer_list_.HasObserver(observer)) |
| 226 return; |
| 227 AddRef(); |
187 // Excecute all the pending MonitorPropertyChanged calls. | 228 // Excecute all the pending MonitorPropertyChanged calls. |
188 for (size_t i = 0; i < interfaces_to_be_monitored_.size(); ++i) { | 229 for (size_t i = 0; i < interfaces_to_be_monitored_.size(); ++i) { |
189 MonitorPropertyChangedInternal(interfaces_to_be_monitored_[i]); | 230 MonitorPropertyChangedInternal(interfaces_to_be_monitored_[i]); |
190 } | 231 } |
191 interfaces_to_be_monitored_.clear(); | 232 interfaces_to_be_monitored_.clear(); |
192 | 233 |
193 observer_list_.AddObserver(observer); | 234 observer_list_.AddObserver(observer); |
194 } | 235 } |
195 | 236 |
196 void ShillClientHelper::RemovePropertyChangedObserver( | 237 void ShillClientHelper::RemovePropertyChangedObserver( |
197 ShillPropertyChangedObserver* observer) { | 238 ShillPropertyChangedObserver* observer) { |
| 239 if (!observer_list_.HasObserver(observer)) |
| 240 return; |
198 observer_list_.RemoveObserver(observer); | 241 observer_list_.RemoveObserver(observer); |
| 242 Release(); |
199 } | 243 } |
200 | 244 |
201 void ShillClientHelper::MonitorPropertyChanged( | 245 void ShillClientHelper::MonitorPropertyChanged( |
202 const std::string& interface_name) { | 246 const std::string& interface_name) { |
203 if (observer_list_.might_have_observers()) { | 247 if (observer_list_.might_have_observers()) { |
204 // Effectively monitor the PropertyChanged now. | 248 // Effectively monitor the PropertyChanged now. |
205 MonitorPropertyChangedInternal(interface_name); | 249 MonitorPropertyChangedInternal(interface_name); |
206 } else { | 250 } else { |
207 // Delay the ConnectToSignal until an observer is added. | 251 // Delay the ConnectToSignal until an observer is added. |
208 interfaces_to_be_monitored_.push_back(interface_name); | 252 interfaces_to_be_monitored_.push_back(interface_name); |
209 } | 253 } |
210 } | 254 } |
211 | 255 |
212 void ShillClientHelper::MonitorPropertyChangedInternal( | 256 void ShillClientHelper::MonitorPropertyChangedInternal( |
213 const std::string& interface_name) { | 257 const std::string& interface_name) { |
214 // We are not using dbus::PropertySet to monitor PropertyChanged signal | 258 // We are not using dbus::PropertySet to monitor PropertyChanged signal |
215 // because the interface is not "org.freedesktop.DBus.Properties". | 259 // because the interface is not "org.freedesktop.DBus.Properties". |
216 proxy_->ConnectToSignal(interface_name, | 260 proxy_->ConnectToSignal(interface_name, |
217 shill::kMonitorPropertyChanged, | 261 shill::kMonitorPropertyChanged, |
218 base::Bind(&ShillClientHelper::OnPropertyChanged, | 262 base::Bind(&ShillClientHelper::OnPropertyChanged, |
219 weak_ptr_factory_.GetWeakPtr()), | 263 weak_ptr_factory_.GetWeakPtr()), |
220 base::Bind(&ShillClientHelper::OnSignalConnected, | 264 base::Bind(&ShillClientHelper::OnSignalConnected, |
221 weak_ptr_factory_.GetWeakPtr())); | 265 weak_ptr_factory_.GetWeakPtr())); |
222 } | 266 } |
223 | 267 |
224 void ShillClientHelper::CallVoidMethod( | 268 void ShillClientHelper::CallVoidMethod( |
225 dbus::MethodCall* method_call, | 269 dbus::MethodCall* method_call, |
226 const VoidDBusMethodCallback& callback) { | 270 const VoidDBusMethodCallback& callback) { |
227 DCHECK(!callback.is_null()); | 271 DCHECK(!callback.is_null()); |
228 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 272 proxy_->CallMethod( |
229 base::Bind(&OnVoidMethod, | 273 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
230 callback)); | 274 base::Bind(&OnVoidMethod, |
| 275 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
| 276 callback)); |
231 } | 277 } |
232 | 278 |
233 void ShillClientHelper::CallObjectPathMethod( | 279 void ShillClientHelper::CallObjectPathMethod( |
234 dbus::MethodCall* method_call, | 280 dbus::MethodCall* method_call, |
235 const ObjectPathDBusMethodCallback& callback) { | 281 const ObjectPathDBusMethodCallback& callback) { |
236 DCHECK(!callback.is_null()); | 282 DCHECK(!callback.is_null()); |
237 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 283 proxy_->CallMethod( |
238 base::Bind(&OnObjectPathMethod, | 284 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
239 callback)); | 285 base::Bind(&OnObjectPathMethod, |
| 286 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
| 287 callback)); |
240 } | 288 } |
241 | 289 |
242 void ShillClientHelper::CallObjectPathMethodWithErrorCallback( | 290 void ShillClientHelper::CallObjectPathMethodWithErrorCallback( |
243 dbus::MethodCall* method_call, | 291 dbus::MethodCall* method_call, |
244 const ObjectPathCallback& callback, | 292 const ObjectPathCallback& callback, |
245 const ErrorCallback& error_callback) { | 293 const ErrorCallback& error_callback) { |
246 DCHECK(!callback.is_null()); | 294 DCHECK(!callback.is_null()); |
247 DCHECK(!error_callback.is_null()); | 295 DCHECK(!error_callback.is_null()); |
248 proxy_->CallMethodWithErrorCallback( | 296 proxy_->CallMethodWithErrorCallback( |
249 method_call, | 297 method_call, |
250 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 298 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
251 base::Bind(&OnObjectPathMethodWithoutStatus, | 299 base::Bind(&OnObjectPathMethodWithoutStatus, |
| 300 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
252 callback, | 301 callback, |
253 error_callback), | 302 error_callback), |
254 base::Bind(&OnError, | 303 base::Bind(&OnError, |
255 error_callback)); | 304 error_callback)); |
256 } | 305 } |
257 | 306 |
258 void ShillClientHelper::CallDictionaryValueMethod( | 307 void ShillClientHelper::CallDictionaryValueMethod( |
259 dbus::MethodCall* method_call, | 308 dbus::MethodCall* method_call, |
260 const DictionaryValueCallback& callback) { | 309 const DictionaryValueCallback& callback) { |
261 DCHECK(!callback.is_null()); | 310 DCHECK(!callback.is_null()); |
262 proxy_->CallMethod(method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 311 proxy_->CallMethod( |
263 base::Bind(&OnDictionaryValueMethod, | 312 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
264 callback)); | 313 base::Bind(&OnDictionaryValueMethod, |
| 314 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
| 315 callback)); |
265 } | 316 } |
266 | 317 |
267 void ShillClientHelper::CallVoidMethodWithErrorCallback( | 318 void ShillClientHelper::CallVoidMethodWithErrorCallback( |
268 dbus::MethodCall* method_call, | 319 dbus::MethodCall* method_call, |
269 const base::Closure& callback, | 320 const base::Closure& callback, |
270 const ErrorCallback& error_callback) { | 321 const ErrorCallback& error_callback) { |
271 DCHECK(!callback.is_null()); | 322 DCHECK(!callback.is_null()); |
272 DCHECK(!error_callback.is_null()); | 323 DCHECK(!error_callback.is_null()); |
273 proxy_->CallMethodWithErrorCallback( | 324 proxy_->CallMethodWithErrorCallback( |
274 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 325 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
275 base::Bind(&OnVoidMethodWithErrorCallback, | 326 base::Bind(&OnVoidMethodWithErrorCallback, |
| 327 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
276 callback), | 328 callback), |
277 base::Bind(&OnError, | 329 base::Bind(&OnError, |
278 error_callback)); | 330 error_callback)); |
279 } | 331 } |
280 | 332 |
281 void ShillClientHelper::CallBooleanMethodWithErrorCallback( | 333 void ShillClientHelper::CallBooleanMethodWithErrorCallback( |
282 dbus::MethodCall* method_call, | 334 dbus::MethodCall* method_call, |
283 const BooleanCallback& callback, | 335 const BooleanCallback& callback, |
284 const ErrorCallback& error_callback) { | 336 const ErrorCallback& error_callback) { |
285 DCHECK(!callback.is_null()); | 337 DCHECK(!callback.is_null()); |
286 DCHECK(!error_callback.is_null()); | 338 DCHECK(!error_callback.is_null()); |
287 proxy_->CallMethodWithErrorCallback( | 339 proxy_->CallMethodWithErrorCallback( |
288 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 340 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
289 base::Bind(&OnBooleanMethodWithErrorCallback, | 341 base::Bind(&OnBooleanMethodWithErrorCallback, |
| 342 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
290 callback, | 343 callback, |
291 error_callback), | 344 error_callback), |
292 base::Bind(&OnError, | 345 base::Bind(&OnError, |
293 error_callback)); | 346 error_callback)); |
294 } | 347 } |
295 | 348 |
296 void ShillClientHelper::CallStringMethodWithErrorCallback( | 349 void ShillClientHelper::CallStringMethodWithErrorCallback( |
297 dbus::MethodCall* method_call, | 350 dbus::MethodCall* method_call, |
298 const StringCallback& callback, | 351 const StringCallback& callback, |
299 const ErrorCallback& error_callback) { | 352 const ErrorCallback& error_callback) { |
300 DCHECK(!callback.is_null()); | 353 DCHECK(!callback.is_null()); |
301 DCHECK(!error_callback.is_null()); | 354 DCHECK(!error_callback.is_null()); |
302 proxy_->CallMethodWithErrorCallback( | 355 proxy_->CallMethodWithErrorCallback( |
303 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 356 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
304 base::Bind(&OnStringMethodWithErrorCallback, | 357 base::Bind(&OnStringMethodWithErrorCallback, |
| 358 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
305 callback, | 359 callback, |
306 error_callback), | 360 error_callback), |
307 base::Bind(&OnError, | 361 base::Bind(&OnError, |
308 error_callback)); | 362 error_callback)); |
309 } | 363 } |
310 | 364 |
311 void ShillClientHelper::CallDictionaryValueMethodWithErrorCallback( | 365 void ShillClientHelper::CallDictionaryValueMethodWithErrorCallback( |
312 dbus::MethodCall* method_call, | 366 dbus::MethodCall* method_call, |
313 const DictionaryValueCallbackWithoutStatus& callback, | 367 const DictionaryValueCallbackWithoutStatus& callback, |
314 const ErrorCallback& error_callback) { | 368 const ErrorCallback& error_callback) { |
315 DCHECK(!callback.is_null()); | 369 DCHECK(!callback.is_null()); |
316 DCHECK(!error_callback.is_null()); | 370 DCHECK(!error_callback.is_null()); |
317 proxy_->CallMethodWithErrorCallback( | 371 proxy_->CallMethodWithErrorCallback( |
318 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 372 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
319 base::Bind( | 373 base::Bind(&OnDictionaryValueMethodWithErrorCallback, |
320 &OnDictionaryValueMethodWithErrorCallback, | 374 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
321 callback, | 375 callback, |
322 error_callback), | 376 error_callback), |
323 base::Bind(&OnError, | 377 base::Bind(&OnError, |
324 error_callback)); | 378 error_callback)); |
325 } | 379 } |
326 | 380 |
327 void ShillClientHelper::CallListValueMethodWithErrorCallback( | 381 void ShillClientHelper::CallListValueMethodWithErrorCallback( |
328 dbus::MethodCall* method_call, | 382 dbus::MethodCall* method_call, |
329 const ListValueCallback& callback, | 383 const ListValueCallback& callback, |
330 const ErrorCallback& error_callback) { | 384 const ErrorCallback& error_callback) { |
331 DCHECK(!callback.is_null()); | 385 DCHECK(!callback.is_null()); |
332 DCHECK(!error_callback.is_null()); | 386 DCHECK(!error_callback.is_null()); |
333 proxy_->CallMethodWithErrorCallback( | 387 proxy_->CallMethodWithErrorCallback( |
334 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 388 method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
335 base::Bind( | 389 base::Bind(&OnListValueMethodWithErrorCallback, |
336 &OnListValueMethodWithErrorCallback, | 390 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
337 callback, | 391 callback, |
338 error_callback), | 392 error_callback), |
339 base::Bind(&OnError, | 393 base::Bind(&OnError, |
340 error_callback)); | 394 error_callback)); |
341 } | 395 } |
342 | 396 |
343 // static | 397 // static |
344 void ShillClientHelper::AppendValueDataAsVariant(dbus::MessageWriter* writer, | 398 void ShillClientHelper::AppendValueDataAsVariant(dbus::MessageWriter* writer, |
345 const base::Value& value) { | 399 const base::Value& value) { |
346 // Support basic types and string-to-string dictionary. | 400 // Support basic types and string-to-string dictionary. |
347 switch (value.GetType()) { | 401 switch (value.GetType()) { |
348 case base::Value::TYPE_DICTIONARY: { | 402 case base::Value::TYPE_DICTIONARY: { |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 it.Advance()) { | 467 it.Advance()) { |
414 dbus::MessageWriter entry_writer(NULL); | 468 dbus::MessageWriter entry_writer(NULL); |
415 array_writer.OpenDictEntry(&entry_writer); | 469 array_writer.OpenDictEntry(&entry_writer); |
416 entry_writer.AppendString(it.key()); | 470 entry_writer.AppendString(it.key()); |
417 ShillClientHelper::AppendValueDataAsVariant(&entry_writer, it.value()); | 471 ShillClientHelper::AppendValueDataAsVariant(&entry_writer, it.value()); |
418 array_writer.CloseContainer(&entry_writer); | 472 array_writer.CloseContainer(&entry_writer); |
419 } | 473 } |
420 writer->CloseContainer(&array_writer); | 474 writer->CloseContainer(&array_writer); |
421 } | 475 } |
422 | 476 |
| 477 void ShillClientHelper::AddRef() { |
| 478 ++active_refs_; |
| 479 } |
| 480 |
| 481 void ShillClientHelper::Release() { |
| 482 --active_refs_; |
| 483 if (active_refs_ == 0 && !released_callback_.is_null()) |
| 484 base::ResetAndReturn(&released_callback_).Run(this); // May delete this |
| 485 } |
| 486 |
423 void ShillClientHelper::OnSignalConnected(const std::string& interface, | 487 void ShillClientHelper::OnSignalConnected(const std::string& interface, |
424 const std::string& signal, | 488 const std::string& signal, |
425 bool success) { | 489 bool success) { |
426 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal | 490 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal |
427 << " failed."; | 491 << " failed."; |
428 } | 492 } |
429 | 493 |
430 void ShillClientHelper::OnPropertyChanged(dbus::Signal* signal) { | 494 void ShillClientHelper::OnPropertyChanged(dbus::Signal* signal) { |
431 if (!observer_list_.might_have_observers()) | 495 if (!observer_list_.might_have_observers()) |
432 return; | 496 return; |
433 | 497 |
434 dbus::MessageReader reader(signal); | 498 dbus::MessageReader reader(signal); |
435 std::string name; | 499 std::string name; |
436 if (!reader.PopString(&name)) | 500 if (!reader.PopString(&name)) |
437 return; | 501 return; |
438 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 502 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
439 if (!value.get()) | 503 if (!value.get()) |
440 return; | 504 return; |
441 | 505 |
442 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, observer_list_, | 506 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, observer_list_, |
443 OnPropertyChanged(name, *value)); | 507 OnPropertyChanged(name, *value)); |
444 } | 508 } |
445 | 509 |
446 | |
447 } // namespace chromeos | 510 } // namespace chromeos |
OLD | NEW |