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 (shill_dictionary_->GetStringWithoutPathExpansion(shill::kProfileProperty, | |
427 &profile_path)) { | |
428 std::string source; | |
429 if (onc_source_ == ::onc::ONC_SOURCE_DEVICE_POLICY) | |
430 source = ::onc::network_config::kSourceDevicePolicy; | |
431 else if (onc_source_ == ::onc::ONC_SOURCE_USER_POLICY) | |
432 source = ::onc::network_config::kSourceUserPolicy; | |
433 else if (profile_path == NetworkProfileHandler::GetSharedProfilePath()) | |
434 source = ::onc::network_config::kSourceDevice; | |
435 else if (!profile_path.empty()) | |
436 source = ::onc::network_config::kSourceUser; | |
437 else | |
pneubeck (no reviews)
2014/09/10 10:49:38
please add a comment that ONC_SOURCE_USER_IMPORT (
stevenjb
2014/09/10 17:32:15
Yeah, I have no clue what USER_IMPORT is even for.
pneubeck (no reviews)
2014/09/10 20:22:38
USER_IMPORT is set if a user imports an ONC file f
| |
438 source = ::onc::network_config::kSourceNone; | |
439 onc_object_->SetStringWithoutPathExpansion( | |
440 ::onc::network_config::kSource, source); | |
441 } | |
442 | |
418 // Use a human-readable aa:bb format for any hardware MAC address. Note: | 443 // 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 | 444 // this property is provided by the caller but is not part of the Shill |
420 // Service properties (it is copied from the Device properties). | 445 // Service properties (it is copied from the Device properties). |
421 std::string address; | 446 std::string address; |
422 if (shill_dictionary_->GetStringWithoutPathExpansion(shill::kAddressProperty, | 447 if (shill_dictionary_->GetStringWithoutPathExpansion(shill::kAddressProperty, |
423 &address)) { | 448 &address)) { |
424 onc_object_->SetStringWithoutPathExpansion( | 449 onc_object_->SetStringWithoutPathExpansion( |
425 ::onc::network_config::kMacAddress, | 450 ::onc::network_config::kMacAddress, |
426 network_util::FormattedMacAddress(address)); | 451 network_util::FormattedMacAddress(address)); |
427 } | 452 } |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
500 | 525 |
501 void ShillToONCTranslator::TranslateAndAddNestedObject( | 526 void ShillToONCTranslator::TranslateAndAddNestedObject( |
502 const std::string& onc_field_name, | 527 const std::string& onc_field_name, |
503 const base::DictionaryValue& dictionary) { | 528 const base::DictionaryValue& dictionary) { |
504 const OncFieldSignature* field_signature = | 529 const OncFieldSignature* field_signature = |
505 GetFieldSignature(*onc_signature_, onc_field_name); | 530 GetFieldSignature(*onc_signature_, onc_field_name); |
506 if (!field_signature) { | 531 if (!field_signature) { |
507 NOTREACHED() << "Unable to find signature for field: " << onc_field_name; | 532 NOTREACHED() << "Unable to find signature for field: " << onc_field_name; |
508 return; | 533 return; |
509 } | 534 } |
510 ShillToONCTranslator nested_translator(dictionary, | 535 ShillToONCTranslator nested_translator( |
511 *field_signature->value_signature); | 536 dictionary, onc_source_, *field_signature->value_signature); |
512 scoped_ptr<base::DictionaryValue> nested_object = | 537 scoped_ptr<base::DictionaryValue> nested_object = |
513 nested_translator.CreateTranslatedONCObject(); | 538 nested_translator.CreateTranslatedONCObject(); |
514 if (nested_object->empty()) | 539 if (nested_object->empty()) |
515 return; | 540 return; |
516 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); | 541 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); |
517 } | 542 } |
518 | 543 |
519 void ShillToONCTranslator::SetNestedOncValue( | 544 void ShillToONCTranslator::SetNestedOncValue( |
520 const std::string& onc_dictionary_name, | 545 const std::string& onc_dictionary_name, |
521 const std::string& onc_field_name, | 546 const std::string& onc_field_name, |
(...skipping 20 matching lines...) Expand all Loading... | |
542 } | 567 } |
543 DCHECK(field_signature->value_signature->onc_array_entry_signature); | 568 DCHECK(field_signature->value_signature->onc_array_entry_signature); |
544 scoped_ptr<base::ListValue> result(new base::ListValue()); | 569 scoped_ptr<base::ListValue> result(new base::ListValue()); |
545 for (base::ListValue::const_iterator it = list.begin(); | 570 for (base::ListValue::const_iterator it = list.begin(); |
546 it != list.end(); ++it) { | 571 it != list.end(); ++it) { |
547 const base::DictionaryValue* shill_value = NULL; | 572 const base::DictionaryValue* shill_value = NULL; |
548 if (!(*it)->GetAsDictionary(&shill_value)) | 573 if (!(*it)->GetAsDictionary(&shill_value)) |
549 continue; | 574 continue; |
550 ShillToONCTranslator nested_translator( | 575 ShillToONCTranslator nested_translator( |
551 *shill_value, | 576 *shill_value, |
577 onc_source_, | |
552 *field_signature->value_signature->onc_array_entry_signature); | 578 *field_signature->value_signature->onc_array_entry_signature); |
553 scoped_ptr<base::DictionaryValue> nested_object = | 579 scoped_ptr<base::DictionaryValue> nested_object = |
554 nested_translator.CreateTranslatedONCObject(); | 580 nested_translator.CreateTranslatedONCObject(); |
555 // If the nested object couldn't be parsed, simply omit it. | 581 // If the nested object couldn't be parsed, simply omit it. |
556 if (nested_object->empty()) | 582 if (nested_object->empty()) |
557 continue; | 583 continue; |
558 result->Append(nested_object.release()); | 584 result->Append(nested_object.release()); |
559 } | 585 } |
560 // If there are no entries in the list, there is no need to expose this field. | 586 // If there are no entries in the list, there is no need to expose this field. |
561 if (result->empty()) | 587 if (result->empty()) |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 DCHECK(shill_dictionary_); | 655 DCHECK(shill_dictionary_); |
630 std::string name; | 656 std::string name; |
631 shill_dictionary_->GetStringWithoutPathExpansion(shill::kNameProperty, &name); | 657 shill_dictionary_->GetStringWithoutPathExpansion(shill::kNameProperty, &name); |
632 return name; | 658 return name; |
633 } | 659 } |
634 | 660 |
635 } // namespace | 661 } // namespace |
636 | 662 |
637 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( | 663 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( |
638 const base::DictionaryValue& shill_dictionary, | 664 const base::DictionaryValue& shill_dictionary, |
665 ::onc::ONCSource onc_source, | |
639 const OncValueSignature* onc_signature) { | 666 const OncValueSignature* onc_signature) { |
640 CHECK(onc_signature != NULL); | 667 CHECK(onc_signature != NULL); |
641 | 668 |
642 ShillToONCTranslator translator(shill_dictionary, *onc_signature); | 669 ShillToONCTranslator translator(shill_dictionary, onc_source, *onc_signature); |
643 return translator.CreateTranslatedONCObject(); | 670 return translator.CreateTranslatedONCObject(); |
644 } | 671 } |
645 | 672 |
646 } // namespace onc | 673 } // namespace onc |
647 } // namespace chromeos | 674 } // namespace chromeos |
OLD | NEW |