| 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" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 } | 45 } |
| 46 | 46 |
| 47 // This class implements the translation of properties from the given | 47 // This class implements the translation of properties from the given |
| 48 // |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 |
| 49 // recursive calls to CreateTranslatedONCObject of new instances, nested objects | 49 // recursive calls to CreateTranslatedONCObject of new instances, nested objects |
| 50 // are translated. | 50 // are translated. |
| 51 class ShillToONCTranslator { | 51 class ShillToONCTranslator { |
| 52 public: | 52 public: |
| 53 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, | 53 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, |
| 54 ::onc::ONCSource onc_source, | 54 ::onc::ONCSource onc_source, |
| 55 const OncValueSignature& onc_signature) | 55 const OncValueSignature& onc_signature, |
| 56 const NetworkState* network_state) |
| 56 : shill_dictionary_(&shill_dictionary), | 57 : shill_dictionary_(&shill_dictionary), |
| 57 onc_source_(onc_source), | 58 onc_source_(onc_source), |
| 58 onc_signature_(&onc_signature) { | 59 onc_signature_(&onc_signature), |
| 60 network_state_(network_state) { |
| 59 field_translation_table_ = GetFieldTranslationTable(onc_signature); | 61 field_translation_table_ = GetFieldTranslationTable(onc_signature); |
| 60 } | 62 } |
| 61 | 63 |
| 62 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, | 64 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, |
| 63 ::onc::ONCSource onc_source, | 65 ::onc::ONCSource onc_source, |
| 64 const OncValueSignature& onc_signature, | 66 const OncValueSignature& onc_signature, |
| 65 const FieldTranslationEntry* field_translation_table) | 67 const FieldTranslationEntry* field_translation_table, |
| 68 const NetworkState* network_state) |
| 66 : shill_dictionary_(&shill_dictionary), | 69 : shill_dictionary_(&shill_dictionary), |
| 67 onc_source_(onc_source), | 70 onc_source_(onc_source), |
| 68 onc_signature_(&onc_signature), | 71 onc_signature_(&onc_signature), |
| 69 field_translation_table_(field_translation_table) {} | 72 field_translation_table_(field_translation_table), |
| 73 network_state_(network_state) {} |
| 70 | 74 |
| 71 // Translates the associated Shill dictionary and creates an ONC object of the | 75 // Translates the associated Shill dictionary and creates an ONC object of the |
| 72 // given signature. | 76 // given signature. |
| 73 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject(); | 77 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject(); |
| 74 | 78 |
| 75 private: | 79 private: |
| 76 void TranslateEthernet(); | 80 void TranslateEthernet(); |
| 77 void TranslateOpenVPN(); | 81 void TranslateOpenVPN(); |
| 78 void TranslateIPsec(); | 82 void TranslateIPsec(); |
| 79 void TranslateThirdPartyVPN(); | 83 void TranslateThirdPartyVPN(); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 139 |
| 136 // Returns the name of the Shill service provided in |shill_dictionary_| | 140 // Returns the name of the Shill service provided in |shill_dictionary_| |
| 137 // for debugging. | 141 // for debugging. |
| 138 std::string GetName(); | 142 std::string GetName(); |
| 139 | 143 |
| 140 const base::DictionaryValue* shill_dictionary_; | 144 const base::DictionaryValue* shill_dictionary_; |
| 141 ::onc::ONCSource onc_source_; | 145 ::onc::ONCSource onc_source_; |
| 142 const OncValueSignature* onc_signature_; | 146 const OncValueSignature* onc_signature_; |
| 143 const FieldTranslationEntry* field_translation_table_; | 147 const FieldTranslationEntry* field_translation_table_; |
| 144 scoped_ptr<base::DictionaryValue> onc_object_; | 148 scoped_ptr<base::DictionaryValue> onc_object_; |
| 149 const NetworkState* network_state_; |
| 145 | 150 |
| 146 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); | 151 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); |
| 147 }; | 152 }; |
| 148 | 153 |
| 149 scoped_ptr<base::DictionaryValue> | 154 scoped_ptr<base::DictionaryValue> |
| 150 ShillToONCTranslator::CreateTranslatedONCObject() { | 155 ShillToONCTranslator::CreateTranslatedONCObject() { |
| 151 onc_object_.reset(new base::DictionaryValue); | 156 onc_object_.reset(new base::DictionaryValue); |
| 152 if (onc_signature_ == &kNetworkWithStateSignature) { | 157 if (onc_signature_ == &kNetworkWithStateSignature) { |
| 153 TranslateNetworkWithState(); | 158 TranslateNetworkWithState(); |
| 154 } else if (onc_signature_ == &kEthernetSignature) { | 159 } else if (onc_signature_ == &kEthernetSignature) { |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 TranslateAndAddNestedObject(::onc::cellular::kPaymentPortal, *dictionary); | 380 TranslateAndAddNestedObject(::onc::cellular::kPaymentPortal, *dictionary); |
| 376 } | 381 } |
| 377 | 382 |
| 378 const base::DictionaryValue* device_dictionary = NULL; | 383 const base::DictionaryValue* device_dictionary = NULL; |
| 379 bool requires_roaming = false; | 384 bool requires_roaming = false; |
| 380 shill_dictionary_->GetDictionaryWithoutPathExpansion(shill::kDeviceProperty, | 385 shill_dictionary_->GetDictionaryWithoutPathExpansion(shill::kDeviceProperty, |
| 381 &device_dictionary); | 386 &device_dictionary); |
| 382 if (device_dictionary) { | 387 if (device_dictionary) { |
| 383 // Merge the Device dictionary with this one (Cellular) using the | 388 // Merge the Device dictionary with this one (Cellular) using the |
| 384 // CellularDevice signature. | 389 // CellularDevice signature. |
| 385 ShillToONCTranslator nested_translator(*device_dictionary, onc_source_, | 390 ShillToONCTranslator nested_translator( |
| 386 kCellularWithStateSignature, | 391 *device_dictionary, onc_source_, kCellularWithStateSignature, |
| 387 kCellularDeviceTable); | 392 kCellularDeviceTable, network_state_); |
| 388 scoped_ptr<base::DictionaryValue> nested_object = | 393 scoped_ptr<base::DictionaryValue> nested_object = |
| 389 nested_translator.CreateTranslatedONCObject(); | 394 nested_translator.CreateTranslatedONCObject(); |
| 390 onc_object_->MergeDictionary(nested_object.get()); | 395 onc_object_->MergeDictionary(nested_object.get()); |
| 391 | 396 |
| 392 /// Get the requires_roaming from the Device dictionary. | 397 /// Get the requires_roaming from the Device dictionary. |
| 393 device_dictionary->GetBooleanWithoutPathExpansion( | 398 device_dictionary->GetBooleanWithoutPathExpansion( |
| 394 shill::kProviderRequiresRoamingProperty, &requires_roaming); | 399 shill::kProviderRequiresRoamingProperty, &requires_roaming); |
| 395 } | 400 } |
| 396 if (requires_roaming) { | 401 if (requires_roaming) { |
| 397 onc_object_->SetStringWithoutPathExpansion( | 402 onc_object_->SetStringWithoutPathExpansion( |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 467 } | 472 } |
| 468 onc_object_->SetStringWithoutPathExpansion( | 473 onc_object_->SetStringWithoutPathExpansion( |
| 469 ::onc::network_config::kConnectionState, onc_state); | 474 ::onc::network_config::kConnectionState, onc_state); |
| 470 // Only set 'RestrictedConnectivity' if captive portal state is true. | 475 // Only set 'RestrictedConnectivity' if captive portal state is true. |
| 471 if (NetworkState::NetworkStateIsCaptivePortal(*shill_dictionary_)) { | 476 if (NetworkState::NetworkStateIsCaptivePortal(*shill_dictionary_)) { |
| 472 onc_object_->SetBooleanWithoutPathExpansion( | 477 onc_object_->SetBooleanWithoutPathExpansion( |
| 473 ::onc::network_config::kRestrictedConnectivity, true); | 478 ::onc::network_config::kRestrictedConnectivity, true); |
| 474 } | 479 } |
| 475 } | 480 } |
| 476 | 481 |
| 482 // 'ErrorState' reflects the most recent error maintained in NetworkState |
| 483 // (which may not match Shill's Error or PreviousError properties). Non |
| 484 // visible networks (with null network_state_) do not set ErrorState. |
| 485 if (network_state_) { |
| 486 std::string error_state = network_state_->GetErrorState(); |
| 487 if (!error_state.empty()) { |
| 488 onc_object_->SetStringWithoutPathExpansion( |
| 489 ::onc::network_config::kErrorState, error_state); |
| 490 } |
| 491 } |
| 492 |
| 477 std::string profile_path; | 493 std::string profile_path; |
| 478 if (onc_source_ != ::onc::ONC_SOURCE_UNKNOWN && | 494 if (onc_source_ != ::onc::ONC_SOURCE_UNKNOWN && |
| 479 shill_dictionary_->GetStringWithoutPathExpansion(shill::kProfileProperty, | 495 shill_dictionary_->GetStringWithoutPathExpansion(shill::kProfileProperty, |
| 480 &profile_path)) { | 496 &profile_path)) { |
| 481 std::string source; | 497 std::string source; |
| 482 if (onc_source_ == ::onc::ONC_SOURCE_DEVICE_POLICY) | 498 if (onc_source_ == ::onc::ONC_SOURCE_DEVICE_POLICY) |
| 483 source = ::onc::network_config::kSourceDevicePolicy; | 499 source = ::onc::network_config::kSourceDevicePolicy; |
| 484 else if (onc_source_ == ::onc::ONC_SOURCE_USER_POLICY) | 500 else if (onc_source_ == ::onc::ONC_SOURCE_USER_POLICY) |
| 485 source = ::onc::network_config::kSourceUserPolicy; | 501 source = ::onc::network_config::kSourceUserPolicy; |
| 486 else if (profile_path == NetworkProfileHandler::GetSharedProfilePath()) | 502 else if (profile_path == NetworkProfileHandler::GetSharedProfilePath()) |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 594 void ShillToONCTranslator::TranslateAndAddNestedObject( | 610 void ShillToONCTranslator::TranslateAndAddNestedObject( |
| 595 const std::string& onc_field_name, | 611 const std::string& onc_field_name, |
| 596 const base::DictionaryValue& dictionary) { | 612 const base::DictionaryValue& dictionary) { |
| 597 const OncFieldSignature* field_signature = | 613 const OncFieldSignature* field_signature = |
| 598 GetFieldSignature(*onc_signature_, onc_field_name); | 614 GetFieldSignature(*onc_signature_, onc_field_name); |
| 599 if (!field_signature) { | 615 if (!field_signature) { |
| 600 NOTREACHED() << "Unable to find signature for field: " << onc_field_name; | 616 NOTREACHED() << "Unable to find signature for field: " << onc_field_name; |
| 601 return; | 617 return; |
| 602 } | 618 } |
| 603 ShillToONCTranslator nested_translator(dictionary, onc_source_, | 619 ShillToONCTranslator nested_translator(dictionary, onc_source_, |
| 604 *field_signature->value_signature); | 620 *field_signature->value_signature, |
| 621 network_state_); |
| 605 scoped_ptr<base::DictionaryValue> nested_object = | 622 scoped_ptr<base::DictionaryValue> nested_object = |
| 606 nested_translator.CreateTranslatedONCObject(); | 623 nested_translator.CreateTranslatedONCObject(); |
| 607 if (nested_object->empty()) | 624 if (nested_object->empty()) |
| 608 return; | 625 return; |
| 609 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); | 626 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); |
| 610 } | 627 } |
| 611 | 628 |
| 612 void ShillToONCTranslator::SetNestedOncValue( | 629 void ShillToONCTranslator::SetNestedOncValue( |
| 613 const std::string& onc_dictionary_name, | 630 const std::string& onc_dictionary_name, |
| 614 const std::string& onc_field_name, | 631 const std::string& onc_field_name, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 635 } | 652 } |
| 636 DCHECK(field_signature->value_signature->onc_array_entry_signature); | 653 DCHECK(field_signature->value_signature->onc_array_entry_signature); |
| 637 scoped_ptr<base::ListValue> result(new base::ListValue()); | 654 scoped_ptr<base::ListValue> result(new base::ListValue()); |
| 638 for (base::ListValue::const_iterator it = list.begin(); it != list.end(); | 655 for (base::ListValue::const_iterator it = list.begin(); it != list.end(); |
| 639 ++it) { | 656 ++it) { |
| 640 const base::DictionaryValue* shill_value = NULL; | 657 const base::DictionaryValue* shill_value = NULL; |
| 641 if (!(*it)->GetAsDictionary(&shill_value)) | 658 if (!(*it)->GetAsDictionary(&shill_value)) |
| 642 continue; | 659 continue; |
| 643 ShillToONCTranslator nested_translator( | 660 ShillToONCTranslator nested_translator( |
| 644 *shill_value, onc_source_, | 661 *shill_value, onc_source_, |
| 645 *field_signature->value_signature->onc_array_entry_signature); | 662 *field_signature->value_signature->onc_array_entry_signature, |
| 663 network_state_); |
| 646 scoped_ptr<base::DictionaryValue> nested_object = | 664 scoped_ptr<base::DictionaryValue> nested_object = |
| 647 nested_translator.CreateTranslatedONCObject(); | 665 nested_translator.CreateTranslatedONCObject(); |
| 648 // If the nested object couldn't be parsed, simply omit it. | 666 // If the nested object couldn't be parsed, simply omit it. |
| 649 if (nested_object->empty()) | 667 if (nested_object->empty()) |
| 650 continue; | 668 continue; |
| 651 result->Append(nested_object.release()); | 669 result->Append(nested_object.release()); |
| 652 } | 670 } |
| 653 // If there are no entries in the list, there is no need to expose this field. | 671 // If there are no entries in the list, there is no need to expose this field. |
| 654 if (result->empty()) | 672 if (result->empty()) |
| 655 return; | 673 return; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 721 std::string name; | 739 std::string name; |
| 722 shill_dictionary_->GetStringWithoutPathExpansion(shill::kNameProperty, &name); | 740 shill_dictionary_->GetStringWithoutPathExpansion(shill::kNameProperty, &name); |
| 723 return name; | 741 return name; |
| 724 } | 742 } |
| 725 | 743 |
| 726 } // namespace | 744 } // namespace |
| 727 | 745 |
| 728 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( | 746 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( |
| 729 const base::DictionaryValue& shill_dictionary, | 747 const base::DictionaryValue& shill_dictionary, |
| 730 ::onc::ONCSource onc_source, | 748 ::onc::ONCSource onc_source, |
| 731 const OncValueSignature* onc_signature) { | 749 const OncValueSignature* onc_signature, |
| 750 const NetworkState* network_state) { |
| 732 CHECK(onc_signature != NULL); | 751 CHECK(onc_signature != NULL); |
| 733 | 752 |
| 734 ShillToONCTranslator translator(shill_dictionary, onc_source, *onc_signature); | 753 ShillToONCTranslator translator(shill_dictionary, onc_source, *onc_signature, |
| 754 network_state); |
| 735 return translator.CreateTranslatedONCObject(); | 755 return translator.CreateTranslatedONCObject(); |
| 736 } | 756 } |
| 737 | 757 |
| 738 } // namespace onc | 758 } // namespace onc |
| 739 } // namespace chromeos | 759 } // namespace chromeos |
| OLD | NEW |