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 onc_object_->SetStringWithoutPathExpansion( |
| 487 ::onc::network_config::kErrorState, network_state_->GetErrorState()); |
| 488 } |
| 489 |
477 std::string profile_path; | 490 std::string profile_path; |
478 if (onc_source_ != ::onc::ONC_SOURCE_UNKNOWN && | 491 if (onc_source_ != ::onc::ONC_SOURCE_UNKNOWN && |
479 shill_dictionary_->GetStringWithoutPathExpansion(shill::kProfileProperty, | 492 shill_dictionary_->GetStringWithoutPathExpansion(shill::kProfileProperty, |
480 &profile_path)) { | 493 &profile_path)) { |
481 std::string source; | 494 std::string source; |
482 if (onc_source_ == ::onc::ONC_SOURCE_DEVICE_POLICY) | 495 if (onc_source_ == ::onc::ONC_SOURCE_DEVICE_POLICY) |
483 source = ::onc::network_config::kSourceDevicePolicy; | 496 source = ::onc::network_config::kSourceDevicePolicy; |
484 else if (onc_source_ == ::onc::ONC_SOURCE_USER_POLICY) | 497 else if (onc_source_ == ::onc::ONC_SOURCE_USER_POLICY) |
485 source = ::onc::network_config::kSourceUserPolicy; | 498 source = ::onc::network_config::kSourceUserPolicy; |
486 else if (profile_path == NetworkProfileHandler::GetSharedProfilePath()) | 499 else if (profile_path == NetworkProfileHandler::GetSharedProfilePath()) |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 void ShillToONCTranslator::TranslateAndAddNestedObject( | 607 void ShillToONCTranslator::TranslateAndAddNestedObject( |
595 const std::string& onc_field_name, | 608 const std::string& onc_field_name, |
596 const base::DictionaryValue& dictionary) { | 609 const base::DictionaryValue& dictionary) { |
597 const OncFieldSignature* field_signature = | 610 const OncFieldSignature* field_signature = |
598 GetFieldSignature(*onc_signature_, onc_field_name); | 611 GetFieldSignature(*onc_signature_, onc_field_name); |
599 if (!field_signature) { | 612 if (!field_signature) { |
600 NOTREACHED() << "Unable to find signature for field: " << onc_field_name; | 613 NOTREACHED() << "Unable to find signature for field: " << onc_field_name; |
601 return; | 614 return; |
602 } | 615 } |
603 ShillToONCTranslator nested_translator(dictionary, onc_source_, | 616 ShillToONCTranslator nested_translator(dictionary, onc_source_, |
604 *field_signature->value_signature); | 617 *field_signature->value_signature, |
| 618 network_state_); |
605 scoped_ptr<base::DictionaryValue> nested_object = | 619 scoped_ptr<base::DictionaryValue> nested_object = |
606 nested_translator.CreateTranslatedONCObject(); | 620 nested_translator.CreateTranslatedONCObject(); |
607 if (nested_object->empty()) | 621 if (nested_object->empty()) |
608 return; | 622 return; |
609 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); | 623 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); |
610 } | 624 } |
611 | 625 |
612 void ShillToONCTranslator::SetNestedOncValue( | 626 void ShillToONCTranslator::SetNestedOncValue( |
613 const std::string& onc_dictionary_name, | 627 const std::string& onc_dictionary_name, |
614 const std::string& onc_field_name, | 628 const std::string& onc_field_name, |
(...skipping 20 matching lines...) Expand all Loading... |
635 } | 649 } |
636 DCHECK(field_signature->value_signature->onc_array_entry_signature); | 650 DCHECK(field_signature->value_signature->onc_array_entry_signature); |
637 scoped_ptr<base::ListValue> result(new base::ListValue()); | 651 scoped_ptr<base::ListValue> result(new base::ListValue()); |
638 for (base::ListValue::const_iterator it = list.begin(); it != list.end(); | 652 for (base::ListValue::const_iterator it = list.begin(); it != list.end(); |
639 ++it) { | 653 ++it) { |
640 const base::DictionaryValue* shill_value = NULL; | 654 const base::DictionaryValue* shill_value = NULL; |
641 if (!(*it)->GetAsDictionary(&shill_value)) | 655 if (!(*it)->GetAsDictionary(&shill_value)) |
642 continue; | 656 continue; |
643 ShillToONCTranslator nested_translator( | 657 ShillToONCTranslator nested_translator( |
644 *shill_value, onc_source_, | 658 *shill_value, onc_source_, |
645 *field_signature->value_signature->onc_array_entry_signature); | 659 *field_signature->value_signature->onc_array_entry_signature, |
| 660 network_state_); |
646 scoped_ptr<base::DictionaryValue> nested_object = | 661 scoped_ptr<base::DictionaryValue> nested_object = |
647 nested_translator.CreateTranslatedONCObject(); | 662 nested_translator.CreateTranslatedONCObject(); |
648 // If the nested object couldn't be parsed, simply omit it. | 663 // If the nested object couldn't be parsed, simply omit it. |
649 if (nested_object->empty()) | 664 if (nested_object->empty()) |
650 continue; | 665 continue; |
651 result->Append(nested_object.release()); | 666 result->Append(nested_object.release()); |
652 } | 667 } |
653 // If there are no entries in the list, there is no need to expose this field. | 668 // If there are no entries in the list, there is no need to expose this field. |
654 if (result->empty()) | 669 if (result->empty()) |
655 return; | 670 return; |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
721 std::string name; | 736 std::string name; |
722 shill_dictionary_->GetStringWithoutPathExpansion(shill::kNameProperty, &name); | 737 shill_dictionary_->GetStringWithoutPathExpansion(shill::kNameProperty, &name); |
723 return name; | 738 return name; |
724 } | 739 } |
725 | 740 |
726 } // namespace | 741 } // namespace |
727 | 742 |
728 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( | 743 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( |
729 const base::DictionaryValue& shill_dictionary, | 744 const base::DictionaryValue& shill_dictionary, |
730 ::onc::ONCSource onc_source, | 745 ::onc::ONCSource onc_source, |
731 const OncValueSignature* onc_signature) { | 746 const OncValueSignature* onc_signature, |
| 747 const NetworkState* network_state) { |
732 CHECK(onc_signature != NULL); | 748 CHECK(onc_signature != NULL); |
733 | 749 |
734 ShillToONCTranslator translator(shill_dictionary, onc_source, *onc_signature); | 750 ShillToONCTranslator translator(shill_dictionary, onc_source, *onc_signature, |
| 751 network_state); |
735 return translator.CreateTranslatedONCObject(); | 752 return translator.CreateTranslatedONCObject(); |
736 } | 753 } |
737 | 754 |
738 } // namespace onc | 755 } // namespace onc |
739 } // namespace chromeos | 756 } // namespace chromeos |
OLD | NEW |