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/callback_helpers.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chromeos/device_event_log.h" | |
10 #include "dbus/message.h" | 11 #include "dbus/message.h" |
11 #include "dbus/object_proxy.h" | 12 #include "dbus/object_proxy.h" |
12 #include "dbus/values_util.h" | 13 #include "dbus/values_util.h" |
13 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
14 | 15 |
15 namespace chromeos { | 16 namespace chromeos { |
16 | 17 |
17 // Class to hold onto a reference to a ShillClientHelper. This calss | 18 // Class to hold onto a reference to a ShillClientHelper. This calss |
18 // is owned by callbacks and released once the callback completes. | 19 // is owned by callbacks and released once the callback completes. |
19 // Note: Only success callbacks hold the reference. If an error callback is | 20 // Note: Only success callbacks hold the reference. If an error callback is |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 std::string error_message; | 196 std::string error_message; |
196 if (response) { | 197 if (response) { |
197 // Error message may contain the error message as string. | 198 // Error message may contain the error message as string. |
198 dbus::MessageReader reader(response); | 199 dbus::MessageReader reader(response); |
199 error_name = response->GetErrorName(); | 200 error_name = response->GetErrorName(); |
200 reader.PopString(&error_message); | 201 reader.PopString(&error_message); |
201 } | 202 } |
202 error_callback.Run(error_name, error_message); | 203 error_callback.Run(error_name, error_message); |
203 } | 204 } |
204 | 205 |
206 void WriteStringDictionary(const base::DictionaryValue& dictionary, | |
207 dbus::MessageWriter* writer) { | |
208 dbus::MessageWriter variant_writer(NULL); | |
209 writer->OpenVariant("a{ss}", &variant_writer); | |
210 dbus::MessageWriter array_writer(NULL); | |
211 variant_writer.OpenArray("{ss}", &array_writer); | |
212 for (base::DictionaryValue::Iterator it(dictionary); !it.IsAtEnd(); | |
213 it.Advance()) { | |
214 dbus::MessageWriter entry_writer(NULL); | |
215 array_writer.OpenDictEntry(&entry_writer); | |
216 entry_writer.AppendString(it.key()); | |
217 const base::Value& value = it.value(); | |
218 std::string value_string; | |
219 if (!value.GetAsString(&value_string)) | |
220 NET_LOG(ERROR) << "Dictionary value not a string: " << it.key(); | |
221 entry_writer.AppendString(value_string); | |
222 array_writer.CloseContainer(&entry_writer); | |
223 } | |
224 variant_writer.CloseContainer(&array_writer); | |
225 writer->CloseContainer(&variant_writer); | |
226 } | |
stevenjb
2015/01/22 23:14:31
The above is copy/pasted from the original code (l
| |
227 | |
205 } // namespace | 228 } // namespace |
206 | 229 |
207 ShillClientHelper::ShillClientHelper(dbus::ObjectProxy* proxy) | 230 ShillClientHelper::ShillClientHelper(dbus::ObjectProxy* proxy) |
208 : proxy_(proxy), | 231 : proxy_(proxy), |
209 active_refs_(0), | 232 active_refs_(0), |
210 weak_ptr_factory_(this) { | 233 weak_ptr_factory_(this) { |
211 } | 234 } |
212 | 235 |
213 ShillClientHelper::~ShillClientHelper() { | 236 ShillClientHelper::~ShillClientHelper() { |
214 LOG_IF(ERROR, observer_list_.might_have_observers()) | 237 if (observer_list_.might_have_observers()) |
215 << "ShillClientHelper destroyed with active observers"; | 238 NET_LOG(ERROR) << "ShillClientHelper destroyed with active observers"; |
216 } | 239 } |
217 | 240 |
218 void ShillClientHelper::SetReleasedCallback(ReleasedCallback callback) { | 241 void ShillClientHelper::SetReleasedCallback(ReleasedCallback callback) { |
219 CHECK(released_callback_.is_null()); | 242 CHECK(released_callback_.is_null()); |
220 released_callback_ = callback; | 243 released_callback_ = callback; |
221 } | 244 } |
222 | 245 |
223 void ShillClientHelper::AddPropertyChangedObserver( | 246 void ShillClientHelper::AddPropertyChangedObserver( |
224 ShillPropertyChangedObserver* observer) { | 247 ShillPropertyChangedObserver* observer) { |
225 if (observer_list_.HasObserver(observer)) | 248 if (observer_list_.HasObserver(observer)) |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
390 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), | 413 base::Owned(new RefHolder(weak_ptr_factory_.GetWeakPtr())), |
391 callback, | 414 callback, |
392 error_callback), | 415 error_callback), |
393 base::Bind(&OnError, | 416 base::Bind(&OnError, |
394 error_callback)); | 417 error_callback)); |
395 } | 418 } |
396 | 419 |
397 // static | 420 // static |
398 void ShillClientHelper::AppendValueDataAsVariant(dbus::MessageWriter* writer, | 421 void ShillClientHelper::AppendValueDataAsVariant(dbus::MessageWriter* writer, |
399 const base::Value& value) { | 422 const base::Value& value) { |
423 AppendValueDataAsVariantEx(writer, value, | |
424 false /* use_string_dictionaries */); | |
stevenjb
2015/01/22 23:14:31
Note: this changes the default behavior, but this
hashimoto
2015/01/23 02:53:48
Isn't it possible to change Shill to accept a{sv}
stevenjb
2015/01/23 18:49:20
We're looking into that but it's going to be a pre
| |
425 } | |
426 | |
427 // static | |
428 void ShillClientHelper::AppendValueDataAsVariantEx( | |
hashimoto
2015/01/23 02:53:48
"Ex" suffix here doesn't look pretty.
How about ad
stevenjb
2015/01/23 18:49:21
I don't think it makes sense to change the signatu
| |
429 dbus::MessageWriter* writer, | |
430 const base::Value& value, | |
431 bool use_string_dictionaries) { | |
400 // Support basic types and string-to-string dictionary. | 432 // Support basic types and string-to-string dictionary. |
401 switch (value.GetType()) { | 433 switch (value.GetType()) { |
402 case base::Value::TYPE_DICTIONARY: { | 434 case base::Value::TYPE_DICTIONARY: { |
403 const base::DictionaryValue* dictionary = NULL; | 435 const base::DictionaryValue* dictionary = NULL; |
404 value.GetAsDictionary(&dictionary); | 436 value.GetAsDictionary(&dictionary); |
405 dbus::MessageWriter variant_writer(NULL); | 437 if (use_string_dictionaries) { |
406 writer->OpenVariant("a{ss}", &variant_writer); | 438 WriteStringDictionary(*dictionary, writer); |
407 dbus::MessageWriter array_writer(NULL); | 439 } else { |
408 variant_writer.OpenArray("{ss}", &array_writer); | 440 dbus::MessageWriter variant_writer(NULL); |
409 for (base::DictionaryValue::Iterator it(*dictionary); | 441 writer->OpenVariant("a{sv}", &variant_writer); |
410 !it.IsAtEnd(); | 442 AppendServicePropertiesDictionary(&variant_writer, *dictionary); |
411 it.Advance()) { | 443 writer->CloseContainer(&variant_writer); |
412 dbus::MessageWriter entry_writer(NULL); | |
413 array_writer.OpenDictEntry(&entry_writer); | |
414 entry_writer.AppendString(it.key()); | |
415 const base::Value& value = it.value(); | |
416 std::string value_string; | |
417 DLOG_IF(ERROR, value.GetType() != base::Value::TYPE_STRING) | |
418 << "Unexpected type " << value.GetType(); | |
419 value.GetAsString(&value_string); | |
420 entry_writer.AppendString(value_string); | |
421 array_writer.CloseContainer(&entry_writer); | |
422 } | 444 } |
423 variant_writer.CloseContainer(&array_writer); | |
424 writer->CloseContainer(&variant_writer); | |
425 break; | 445 break; |
426 } | 446 } |
427 case base::Value::TYPE_LIST: { | 447 case base::Value::TYPE_LIST: { |
428 const base::ListValue* list = NULL; | 448 const base::ListValue* list = NULL; |
429 value.GetAsList(&list); | 449 value.GetAsList(&list); |
430 dbus::MessageWriter variant_writer(NULL); | 450 dbus::MessageWriter variant_writer(NULL); |
431 writer->OpenVariant("as", &variant_writer); | 451 writer->OpenVariant("as", &variant_writer); |
432 dbus::MessageWriter array_writer(NULL); | 452 dbus::MessageWriter array_writer(NULL); |
433 variant_writer.OpenArray("s", &array_writer); | 453 variant_writer.OpenArray("s", &array_writer); |
434 for (base::ListValue::const_iterator it = list->begin(); | 454 for (base::ListValue::const_iterator it = list->begin(); |
435 it != list->end(); ++it) { | 455 it != list->end(); ++it) { |
436 const base::Value& value = **it; | 456 const base::Value& value = **it; |
437 LOG_IF(ERROR, value.GetType() != base::Value::TYPE_STRING) | |
438 << "Unexpected type " << value.GetType(); | |
439 std::string value_string; | 457 std::string value_string; |
440 value.GetAsString(&value_string); | 458 if (!value.GetAsString(&value_string)) |
459 NET_LOG(ERROR) << "List value not a string: " << value; | |
441 array_writer.AppendString(value_string); | 460 array_writer.AppendString(value_string); |
442 } | 461 } |
443 variant_writer.CloseContainer(&array_writer); | 462 variant_writer.CloseContainer(&array_writer); |
444 writer->CloseContainer(&variant_writer); | 463 writer->CloseContainer(&variant_writer); |
445 break; | 464 break; |
446 } | 465 } |
447 case base::Value::TYPE_BOOLEAN: | 466 case base::Value::TYPE_BOOLEAN: |
448 case base::Value::TYPE_INTEGER: | 467 case base::Value::TYPE_INTEGER: |
449 case base::Value::TYPE_DOUBLE: | 468 case base::Value::TYPE_DOUBLE: |
450 case base::Value::TYPE_STRING: | 469 case base::Value::TYPE_STRING: |
451 dbus::AppendBasicTypeValueDataAsVariant(writer, value); | 470 dbus::AppendBasicTypeValueDataAsVariant(writer, value); |
452 break; | 471 break; |
453 default: | 472 default: |
454 DLOG(ERROR) << "Unexpected type " << value.GetType(); | 473 NET_LOG(ERROR) << "Unexpected value type: " << value.GetType(); |
455 } | 474 } |
456 | 475 |
457 } | 476 } |
458 | 477 |
459 // static | 478 // static |
460 void ShillClientHelper::AppendServicePropertiesDictionary( | 479 void ShillClientHelper::AppendServicePropertiesDictionary( |
461 dbus::MessageWriter* writer, | 480 dbus::MessageWriter* writer, |
462 const base::DictionaryValue& dictionary) { | 481 const base::DictionaryValue& dictionary) { |
463 dbus::MessageWriter array_writer(NULL); | 482 dbus::MessageWriter array_writer(NULL); |
464 writer->OpenArray("{sv}", &array_writer); | 483 writer->OpenArray("{sv}", &array_writer); |
465 for (base::DictionaryValue::Iterator it(dictionary); | 484 for (base::DictionaryValue::Iterator it(dictionary); !it.IsAtEnd(); |
466 !it.IsAtEnd(); | |
467 it.Advance()) { | 485 it.Advance()) { |
468 dbus::MessageWriter entry_writer(NULL); | 486 dbus::MessageWriter entry_writer(NULL); |
469 array_writer.OpenDictEntry(&entry_writer); | 487 array_writer.OpenDictEntry(&entry_writer); |
470 entry_writer.AppendString(it.key()); | 488 entry_writer.AppendString(it.key()); |
471 ShillClientHelper::AppendValueDataAsVariant(&entry_writer, it.value()); | 489 // Shill expects Cellular.APN to be a string dictionary, a{ss}. All other |
490 // properties use a varient dictionary, a{sv}. | |
491 bool use_string_dictionaries = it.key() == shill::kCellularApnProperty; | |
492 ShillClientHelper::AppendValueDataAsVariantEx(&entry_writer, it.value(), | |
493 use_string_dictionaries); | |
472 array_writer.CloseContainer(&entry_writer); | 494 array_writer.CloseContainer(&entry_writer); |
473 } | 495 } |
474 writer->CloseContainer(&array_writer); | 496 writer->CloseContainer(&array_writer); |
475 } | 497 } |
476 | 498 |
477 void ShillClientHelper::AddRef() { | 499 void ShillClientHelper::AddRef() { |
478 ++active_refs_; | 500 ++active_refs_; |
479 } | 501 } |
480 | 502 |
481 void ShillClientHelper::Release() { | 503 void ShillClientHelper::Release() { |
482 --active_refs_; | 504 --active_refs_; |
483 if (active_refs_ == 0 && !released_callback_.is_null()) | 505 if (active_refs_ == 0 && !released_callback_.is_null()) |
484 base::ResetAndReturn(&released_callback_).Run(this); // May delete this | 506 base::ResetAndReturn(&released_callback_).Run(this); // May delete this |
485 } | 507 } |
486 | 508 |
487 void ShillClientHelper::OnSignalConnected(const std::string& interface, | 509 void ShillClientHelper::OnSignalConnected(const std::string& interface, |
488 const std::string& signal, | 510 const std::string& signal, |
489 bool success) { | 511 bool success) { |
490 LOG_IF(ERROR, !success) << "Connect to " << interface << " " << signal | 512 if (!success) |
491 << " failed."; | 513 NET_LOG(ERROR) << "Connect to " << interface << " " << signal << " failed."; |
492 } | 514 } |
493 | 515 |
494 void ShillClientHelper::OnPropertyChanged(dbus::Signal* signal) { | 516 void ShillClientHelper::OnPropertyChanged(dbus::Signal* signal) { |
495 if (!observer_list_.might_have_observers()) | 517 if (!observer_list_.might_have_observers()) |
496 return; | 518 return; |
497 | 519 |
498 dbus::MessageReader reader(signal); | 520 dbus::MessageReader reader(signal); |
499 std::string name; | 521 std::string name; |
500 if (!reader.PopString(&name)) | 522 if (!reader.PopString(&name)) |
501 return; | 523 return; |
502 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 524 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); |
503 if (!value.get()) | 525 if (!value.get()) |
504 return; | 526 return; |
505 | 527 |
506 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, observer_list_, | 528 FOR_EACH_OBSERVER(ShillPropertyChangedObserver, observer_list_, |
507 OnPropertyChanged(name, *value)); | 529 OnPropertyChanged(name, *value)); |
508 } | 530 } |
509 | 531 |
510 } // namespace chromeos | 532 } // namespace chromeos |
OLD | NEW |