| 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/network/onc/onc_translator.h" | 5 #include "chromeos/network/onc/onc_translator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chromeos/network/network_profile_handler.h" |
| 15 #include "chromeos/network/network_state.h" | 16 #include "chromeos/network/network_state.h" |
| 16 #include "chromeos/network/network_util.h" | 17 #include "chromeos/network/network_util.h" |
| 17 #include "chromeos/network/onc/onc_signature.h" | 18 #include "chromeos/network/onc/onc_signature.h" |
| 18 #include "chromeos/network/onc/onc_translation_tables.h" | 19 #include "chromeos/network/onc/onc_translation_tables.h" |
| 19 #include "chromeos/network/shill_property_util.h" | 20 #include "chromeos/network/shill_property_util.h" |
| 20 #include "components/onc/onc_constants.h" | 21 #include "components/onc/onc_constants.h" |
| 21 #include "third_party/cros_system_api/dbus/service_constants.h" | 22 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 22 | 23 |
| 23 namespace chromeos { | 24 namespace chromeos { |
| 24 namespace onc { | 25 namespace onc { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 43 return make_scoped_ptr(value); | 44 return make_scoped_ptr(value); |
| 44 } | 45 } |
| 45 | 46 |
| 46 // This class implements the translation of properties from the given | 47 // This class implements the translation of properties from the given |
| 47 // |shill_dictionary| to a new ONC object of signature |onc_signature|. Using | 48 // |shill_dictionary| to a new ONC object of signature |onc_signature|. Using |
| 48 // recursive calls to CreateTranslatedONCObject of new instances, nested objects | 49 // recursive calls to CreateTranslatedONCObject of new instances, nested objects |
| 49 // are translated. | 50 // are translated. |
| 50 class ShillToONCTranslator { | 51 class ShillToONCTranslator { |
| 51 public: | 52 public: |
| 52 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, | 53 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, |
| 54 ::onc::ONCSource onc_source, |
| 53 const OncValueSignature& onc_signature) | 55 const OncValueSignature& onc_signature) |
| 54 : shill_dictionary_(&shill_dictionary), | 56 : shill_dictionary_(&shill_dictionary), |
| 57 onc_source_(onc_source), |
| 55 onc_signature_(&onc_signature) { | 58 onc_signature_(&onc_signature) { |
| 56 field_translation_table_ = GetFieldTranslationTable(onc_signature); | 59 field_translation_table_ = GetFieldTranslationTable(onc_signature); |
| 57 } | 60 } |
| 58 | 61 |
| 59 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, | 62 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, |
| 63 ::onc::ONCSource onc_source, |
| 60 const OncValueSignature& onc_signature, | 64 const OncValueSignature& onc_signature, |
| 61 const FieldTranslationEntry* field_translation_table) | 65 const FieldTranslationEntry* field_translation_table) |
| 62 : shill_dictionary_(&shill_dictionary), | 66 : shill_dictionary_(&shill_dictionary), |
| 67 onc_source_(onc_source), |
| 63 onc_signature_(&onc_signature), | 68 onc_signature_(&onc_signature), |
| 64 field_translation_table_(field_translation_table) { | 69 field_translation_table_(field_translation_table) { |
| 65 } | 70 } |
| 66 | 71 |
| 67 // Translates the associated Shill dictionary and creates an ONC object of the | 72 // Translates the associated Shill dictionary and creates an ONC object of the |
| 68 // given signature. | 73 // given signature. |
| 69 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject(); | 74 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject(); |
| 70 | 75 |
| 71 private: | 76 private: |
| 72 void TranslateEthernet(); | 77 void TranslateEthernet(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 // |onc_object_|. | 130 // |onc_object_|. |
| 126 void TranslateWithTableAndSet(const std::string& shill_property_name, | 131 void TranslateWithTableAndSet(const std::string& shill_property_name, |
| 127 const StringTranslationEntry table[], | 132 const StringTranslationEntry table[], |
| 128 const std::string& onc_field_name); | 133 const std::string& onc_field_name); |
| 129 | 134 |
| 130 // Returns the name of the Shill service provided in |shill_dictionary_| | 135 // Returns the name of the Shill service provided in |shill_dictionary_| |
| 131 // for debugging. | 136 // for debugging. |
| 132 std::string GetName(); | 137 std::string GetName(); |
| 133 | 138 |
| 134 const base::DictionaryValue* shill_dictionary_; | 139 const base::DictionaryValue* shill_dictionary_; |
| 140 ::onc::ONCSource onc_source_; |
| 135 const OncValueSignature* onc_signature_; | 141 const OncValueSignature* onc_signature_; |
| 136 const FieldTranslationEntry* field_translation_table_; | 142 const FieldTranslationEntry* field_translation_table_; |
| 137 scoped_ptr<base::DictionaryValue> onc_object_; | 143 scoped_ptr<base::DictionaryValue> onc_object_; |
| 138 | 144 |
| 139 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); | 145 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); |
| 140 }; | 146 }; |
| 141 | 147 |
| 142 scoped_ptr<base::DictionaryValue> | 148 scoped_ptr<base::DictionaryValue> |
| 143 ShillToONCTranslator::CreateTranslatedONCObject() { | 149 ShillToONCTranslator::CreateTranslatedONCObject() { |
| 144 onc_object_.reset(new base::DictionaryValue); | 150 onc_object_.reset(new base::DictionaryValue); |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 TranslateAndAddNestedObject(::onc::cellular::kLastGoodAPN, *dictionary); | 341 TranslateAndAddNestedObject(::onc::cellular::kLastGoodAPN, *dictionary); |
| 336 } | 342 } |
| 337 // Merge the Device dictionary with this one (Cellular) using the | 343 // Merge the Device dictionary with this one (Cellular) using the |
| 338 // CellularDevice signature. | 344 // CellularDevice signature. |
| 339 const base::DictionaryValue* device_dictionary = NULL; | 345 const base::DictionaryValue* device_dictionary = NULL; |
| 340 if (!shill_dictionary_->GetDictionaryWithoutPathExpansion( | 346 if (!shill_dictionary_->GetDictionaryWithoutPathExpansion( |
| 341 shill::kDeviceProperty, &device_dictionary)) { | 347 shill::kDeviceProperty, &device_dictionary)) { |
| 342 return; | 348 return; |
| 343 } | 349 } |
| 344 ShillToONCTranslator nested_translator(*device_dictionary, | 350 ShillToONCTranslator nested_translator(*device_dictionary, |
| 351 onc_source_, |
| 345 kCellularWithStateSignature, | 352 kCellularWithStateSignature, |
| 346 kCellularDeviceTable); | 353 kCellularDeviceTable); |
| 347 scoped_ptr<base::DictionaryValue> nested_object = | 354 scoped_ptr<base::DictionaryValue> nested_object = |
| 348 nested_translator.CreateTranslatedONCObject(); | 355 nested_translator.CreateTranslatedONCObject(); |
| 349 onc_object_->MergeDictionary(nested_object.get()); | 356 onc_object_->MergeDictionary(nested_object.get()); |
| 350 } | 357 } |
| 351 | 358 |
| 352 void ShillToONCTranslator::TranslateCellularDevice() { | 359 void ShillToONCTranslator::TranslateCellularDevice() { |
| 353 CopyPropertiesAccordingToSignature(); | 360 CopyPropertiesAccordingToSignature(); |
| 354 const base::DictionaryValue* shill_sim_lock_status = NULL; | 361 const base::DictionaryValue* shill_sim_lock_status = NULL; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 } | 415 } |
| 409 onc_object_->SetStringWithoutPathExpansion( | 416 onc_object_->SetStringWithoutPathExpansion( |
| 410 ::onc::network_config::kConnectionState, onc_state); | 417 ::onc::network_config::kConnectionState, onc_state); |
| 411 // Only set 'RestrictedConnectivity' if true. | 418 // Only set 'RestrictedConnectivity' if true. |
| 412 if (state == shill::kStatePortal) { | 419 if (state == shill::kStatePortal) { |
| 413 onc_object_->SetBooleanWithoutPathExpansion( | 420 onc_object_->SetBooleanWithoutPathExpansion( |
| 414 ::onc::network_config::kRestrictedConnectivity, true); | 421 ::onc::network_config::kRestrictedConnectivity, true); |
| 415 } | 422 } |
| 416 } | 423 } |
| 417 | 424 |
| 425 std::string profile_path; |
| 426 if (onc_source_ != ::onc::ONC_SOURCE_UNKNOWN && |
| 427 shill_dictionary_->GetStringWithoutPathExpansion(shill::kProfileProperty, |
| 428 &profile_path)) { |
| 429 std::string source; |
| 430 if (onc_source_ == ::onc::ONC_SOURCE_DEVICE_POLICY) |
| 431 source = ::onc::network_config::kSourceDevicePolicy; |
| 432 else if (onc_source_ == ::onc::ONC_SOURCE_USER_POLICY) |
| 433 source = ::onc::network_config::kSourceUserPolicy; |
| 434 else if (profile_path == NetworkProfileHandler::GetSharedProfilePath()) |
| 435 source = ::onc::network_config::kSourceDevice; |
| 436 else if (!profile_path.empty()) |
| 437 source = ::onc::network_config::kSourceUser; |
| 438 else |
| 439 source = ::onc::network_config::kSourceNone; |
| 440 onc_object_->SetStringWithoutPathExpansion( |
| 441 ::onc::network_config::kSource, source); |
| 442 } |
| 443 |
| 418 // Use a human-readable aa:bb format for any hardware MAC address. Note: | 444 // Use a human-readable aa:bb format for any hardware MAC address. Note: |
| 419 // this property is provided by the caller but is not part of the Shill | 445 // this property is provided by the caller but is not part of the Shill |
| 420 // Service properties (it is copied from the Device properties). | 446 // Service properties (it is copied from the Device properties). |
| 421 std::string address; | 447 std::string address; |
| 422 if (shill_dictionary_->GetStringWithoutPathExpansion(shill::kAddressProperty, | 448 if (shill_dictionary_->GetStringWithoutPathExpansion(shill::kAddressProperty, |
| 423 &address)) { | 449 &address)) { |
| 424 onc_object_->SetStringWithoutPathExpansion( | 450 onc_object_->SetStringWithoutPathExpansion( |
| 425 ::onc::network_config::kMacAddress, | 451 ::onc::network_config::kMacAddress, |
| 426 network_util::FormattedMacAddress(address)); | 452 network_util::FormattedMacAddress(address)); |
| 427 } | 453 } |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 500 | 526 |
| 501 void ShillToONCTranslator::TranslateAndAddNestedObject( | 527 void ShillToONCTranslator::TranslateAndAddNestedObject( |
| 502 const std::string& onc_field_name, | 528 const std::string& onc_field_name, |
| 503 const base::DictionaryValue& dictionary) { | 529 const base::DictionaryValue& dictionary) { |
| 504 const OncFieldSignature* field_signature = | 530 const OncFieldSignature* field_signature = |
| 505 GetFieldSignature(*onc_signature_, onc_field_name); | 531 GetFieldSignature(*onc_signature_, onc_field_name); |
| 506 if (!field_signature) { | 532 if (!field_signature) { |
| 507 NOTREACHED() << "Unable to find signature for field: " << onc_field_name; | 533 NOTREACHED() << "Unable to find signature for field: " << onc_field_name; |
| 508 return; | 534 return; |
| 509 } | 535 } |
| 510 ShillToONCTranslator nested_translator(dictionary, | 536 ShillToONCTranslator nested_translator( |
| 511 *field_signature->value_signature); | 537 dictionary, onc_source_, *field_signature->value_signature); |
| 512 scoped_ptr<base::DictionaryValue> nested_object = | 538 scoped_ptr<base::DictionaryValue> nested_object = |
| 513 nested_translator.CreateTranslatedONCObject(); | 539 nested_translator.CreateTranslatedONCObject(); |
| 514 if (nested_object->empty()) | 540 if (nested_object->empty()) |
| 515 return; | 541 return; |
| 516 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); | 542 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); |
| 517 } | 543 } |
| 518 | 544 |
| 519 void ShillToONCTranslator::SetNestedOncValue( | 545 void ShillToONCTranslator::SetNestedOncValue( |
| 520 const std::string& onc_dictionary_name, | 546 const std::string& onc_dictionary_name, |
| 521 const std::string& onc_field_name, | 547 const std::string& onc_field_name, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 542 } | 568 } |
| 543 DCHECK(field_signature->value_signature->onc_array_entry_signature); | 569 DCHECK(field_signature->value_signature->onc_array_entry_signature); |
| 544 scoped_ptr<base::ListValue> result(new base::ListValue()); | 570 scoped_ptr<base::ListValue> result(new base::ListValue()); |
| 545 for (base::ListValue::const_iterator it = list.begin(); | 571 for (base::ListValue::const_iterator it = list.begin(); |
| 546 it != list.end(); ++it) { | 572 it != list.end(); ++it) { |
| 547 const base::DictionaryValue* shill_value = NULL; | 573 const base::DictionaryValue* shill_value = NULL; |
| 548 if (!(*it)->GetAsDictionary(&shill_value)) | 574 if (!(*it)->GetAsDictionary(&shill_value)) |
| 549 continue; | 575 continue; |
| 550 ShillToONCTranslator nested_translator( | 576 ShillToONCTranslator nested_translator( |
| 551 *shill_value, | 577 *shill_value, |
| 578 onc_source_, |
| 552 *field_signature->value_signature->onc_array_entry_signature); | 579 *field_signature->value_signature->onc_array_entry_signature); |
| 553 scoped_ptr<base::DictionaryValue> nested_object = | 580 scoped_ptr<base::DictionaryValue> nested_object = |
| 554 nested_translator.CreateTranslatedONCObject(); | 581 nested_translator.CreateTranslatedONCObject(); |
| 555 // If the nested object couldn't be parsed, simply omit it. | 582 // If the nested object couldn't be parsed, simply omit it. |
| 556 if (nested_object->empty()) | 583 if (nested_object->empty()) |
| 557 continue; | 584 continue; |
| 558 result->Append(nested_object.release()); | 585 result->Append(nested_object.release()); |
| 559 } | 586 } |
| 560 // If there are no entries in the list, there is no need to expose this field. | 587 // If there are no entries in the list, there is no need to expose this field. |
| 561 if (result->empty()) | 588 if (result->empty()) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 629 DCHECK(shill_dictionary_); | 656 DCHECK(shill_dictionary_); |
| 630 std::string name; | 657 std::string name; |
| 631 shill_dictionary_->GetStringWithoutPathExpansion(shill::kNameProperty, &name); | 658 shill_dictionary_->GetStringWithoutPathExpansion(shill::kNameProperty, &name); |
| 632 return name; | 659 return name; |
| 633 } | 660 } |
| 634 | 661 |
| 635 } // namespace | 662 } // namespace |
| 636 | 663 |
| 637 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( | 664 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( |
| 638 const base::DictionaryValue& shill_dictionary, | 665 const base::DictionaryValue& shill_dictionary, |
| 666 ::onc::ONCSource onc_source, |
| 639 const OncValueSignature* onc_signature) { | 667 const OncValueSignature* onc_signature) { |
| 640 CHECK(onc_signature != NULL); | 668 CHECK(onc_signature != NULL); |
| 641 | 669 |
| 642 ShillToONCTranslator translator(shill_dictionary, *onc_signature); | 670 ShillToONCTranslator translator(shill_dictionary, onc_source, *onc_signature); |
| 643 return translator.CreateTranslatedONCObject(); | 671 return translator.CreateTranslatedONCObject(); |
| 644 } | 672 } |
| 645 | 673 |
| 646 } // namespace onc | 674 } // namespace onc |
| 647 } // namespace chromeos | 675 } // namespace chromeos |
| OLD | NEW |