| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/network_settings/onc_normalizer.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/chromeos/cros/onc_constants.h" |
| 12 #include "chrome/browser/chromeos/network_settings/onc_signature.h" |
| 13 |
| 14 namespace chromeos { |
| 15 namespace onc { |
| 16 |
| 17 Normalizer::Normalizer(bool remove_recommended_fields) |
| 18 : remove_recommended_fields_(remove_recommended_fields) { |
| 19 } |
| 20 |
| 21 scoped_ptr<base::DictionaryValue> Normalizer::NormalizeObject( |
| 22 const OncValueSignature* object_signature, |
| 23 const base::DictionaryValue& onc_object) { |
| 24 CHECK(object_signature != NULL); |
| 25 scoped_ptr<base::Value> result_value = |
| 26 MapValue(*object_signature, onc_object); |
| 27 base::DictionaryValue* result_dict = NULL; |
| 28 if (result_value.get()) { |
| 29 result_value.release()->GetAsDictionary(&result_dict); |
| 30 CHECK(result_dict != NULL); |
| 31 } |
| 32 return make_scoped_ptr(result_dict); |
| 33 } |
| 34 |
| 35 scoped_ptr<base::DictionaryValue> Normalizer::MapObject( |
| 36 const OncValueSignature& signature, |
| 37 const base::DictionaryValue& onc_object) { |
| 38 scoped_ptr<base::DictionaryValue> normalized = |
| 39 Mapper::MapObject(signature, onc_object); |
| 40 |
| 41 if (!normalized.get()) |
| 42 return scoped_ptr<base::DictionaryValue>(); |
| 43 |
| 44 if (remove_recommended_fields_) |
| 45 normalized->RemoveWithoutPathExpansion(kRecommended, NULL); |
| 46 |
| 47 if (&signature == &network_configuration_signature) |
| 48 NormalizeNetworkConfiguration(normalized.get()); |
| 49 else if (&signature == &vpn_signature) |
| 50 NormalizeVPN(normalized.get()); |
| 51 else if (&signature == &ipsec_signature) |
| 52 NormalizeIPsec(normalized.get()); |
| 53 |
| 54 return normalized.Pass(); |
| 55 } |
| 56 |
| 57 namespace { |
| 58 void RemoveEntryUnless(base::DictionaryValue* dict, |
| 59 const std::string path, |
| 60 bool condition) { |
| 61 if (!condition) |
| 62 dict->RemoveWithoutPathExpansion(path, NULL); |
| 63 } |
| 64 } // namespace |
| 65 |
| 66 void NormalizeIPsec(base::DictionaryValue* ipsec) { |
| 67 using namespace vpn; |
| 68 |
| 69 std::string auth_type = ""; |
| 70 ipsec->GetStringWithoutPathExpansion(kAuthenticationType, &auth_type); |
| 71 RemoveEntryUnless(ipsec, kClientCertType, auth_type == "Cert"); |
| 72 RemoveEntryUnless(ipsec, kServerCARef, auth_type == "Cert"); |
| 73 RemoveEntryUnless(ipsec, kPSK, auth_type == "PSK"); |
| 74 RemoveEntryUnless(ipsec, kSaveCredentials, auth_type == "PSK"); |
| 75 |
| 76 std::string clientcert_type = ""; |
| 77 ipsec->GetStringWithoutPathExpansion(kClientCertType, &clientcert_type); |
| 78 RemoveEntryUnless(ipsec, kClientCertPattern, clientcert_type == "Pattern"); |
| 79 RemoveEntryUnless(ipsec, kClientCertRef, clientcert_type == "Ref"); |
| 80 |
| 81 int ike_version = -1; |
| 82 ipsec->GetIntegerWithoutPathExpansion(kIKEVersion, &ike_version); |
| 83 RemoveEntryUnless(ipsec, kEAP, ike_version == 2); |
| 84 RemoveEntryUnless(ipsec, kGroup, ike_version == 1); |
| 85 RemoveEntryUnless(ipsec, kXAUTH, ike_version == 1); |
| 86 } |
| 87 |
| 88 void NormalizeVPN(base::DictionaryValue* vpn) { |
| 89 using namespace vpn; |
| 90 std::string type = ""; |
| 91 vpn->GetStringWithoutPathExpansion(vpn::kType, &type); |
| 92 RemoveEntryUnless(vpn, kOpenVPN, type == kOpenVPN); |
| 93 RemoveEntryUnless(vpn, kIPsec, type == kIPsec || type == "L2TP-IPsec"); |
| 94 RemoveEntryUnless(vpn, kL2TP, type == "L2TP-IPSec"); |
| 95 } |
| 96 |
| 97 void NormalizeNetworkConfiguration(base::DictionaryValue* network) { |
| 98 std::string type = ""; |
| 99 network->GetStringWithoutPathExpansion(kType, &type); |
| 100 RemoveEntryUnless(network, kEthernet, type == kEthernet); |
| 101 RemoveEntryUnless(network, kVPN, type == kVPN); |
| 102 RemoveEntryUnless(network, kWiFi, type == kWiFi); |
| 103 } |
| 104 |
| 105 } // namespace onc |
| 106 } // namespace chromeos |
| OLD | NEW |