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.
5 #include "chromeos/network/onc/onc_normalizer.h"
9 #include "base/logging.h"
10 #include "base/values.h"
11 #include "chromeos/network/onc/onc_constants.h"
12 #include "chromeos/network/onc/onc_signature.h"
17 Normalizer::Normalizer(bool remove_recommended_fields
)
18 : remove_recommended_fields_(remove_recommended_fields
) {
21 Normalizer::~Normalizer() {
24 scoped_ptr
<base::DictionaryValue
> Normalizer::NormalizeObject(
25 const OncValueSignature
* object_signature
,
26 const base::DictionaryValue
& onc_object
) {
27 CHECK(object_signature
!= NULL
);
29 scoped_ptr
<base::DictionaryValue
> result
=
30 MapObject(*object_signature
, onc_object
, &error
);
35 scoped_ptr
<base::DictionaryValue
> Normalizer::MapObject(
36 const OncValueSignature
& signature
,
37 const base::DictionaryValue
& onc_object
,
39 scoped_ptr
<base::DictionaryValue
> normalized
=
40 Mapper::MapObject(signature
, onc_object
, error
);
42 if (normalized
.get() == NULL
)
43 return scoped_ptr
<base::DictionaryValue
>();
45 if (remove_recommended_fields_
)
46 normalized
->RemoveWithoutPathExpansion(kRecommended
, NULL
);
48 if (&signature
== &kNetworkConfigurationSignature
)
49 NormalizeNetworkConfiguration(normalized
.get());
50 else if (&signature
== &kVPNSignature
)
51 NormalizeVPN(normalized
.get());
52 else if (&signature
== &kIPsecSignature
)
53 NormalizeIPsec(normalized
.get());
55 return normalized
.Pass();
59 void RemoveEntryUnless(base::DictionaryValue
* dict
,
60 const std::string path
,
63 dict
->RemoveWithoutPathExpansion(path
, NULL
);
67 void Normalizer::NormalizeIPsec(base::DictionaryValue
* ipsec
) {
70 std::string auth_type
;
71 ipsec
->GetStringWithoutPathExpansion(kAuthenticationType
, &auth_type
);
72 RemoveEntryUnless(ipsec
, kClientCertType
, auth_type
== kCert
);
73 RemoveEntryUnless(ipsec
, kServerCARef
, auth_type
== kCert
);
74 RemoveEntryUnless(ipsec
, kPSK
, auth_type
== kPSK
);
75 RemoveEntryUnless(ipsec
, kSaveCredentials
, auth_type
== kPSK
);
77 std::string clientcert_type
;
78 ipsec
->GetStringWithoutPathExpansion(kClientCertType
, &clientcert_type
);
79 RemoveEntryUnless(ipsec
, kClientCertPattern
,
80 clientcert_type
== certificate::kPattern
);
81 RemoveEntryUnless(ipsec
, kClientCertRef
,
82 clientcert_type
== certificate::kRef
);
85 ipsec
->GetIntegerWithoutPathExpansion(kIKEVersion
, &ike_version
);
86 RemoveEntryUnless(ipsec
, kEAP
, ike_version
== 2);
87 RemoveEntryUnless(ipsec
, kGroup
, ike_version
== 1);
88 RemoveEntryUnless(ipsec
, kXAUTH
, ike_version
== 1);
91 void Normalizer::NormalizeVPN(base::DictionaryValue
* vpn
) {
94 vpn
->GetStringWithoutPathExpansion(vpn::kType
, &type
);
95 RemoveEntryUnless(vpn
, kOpenVPN
, type
== kOpenVPN
);
96 RemoveEntryUnless(vpn
, kIPsec
, type
== kIPsec
|| type
== kTypeL2TP_IPsec
);
97 RemoveEntryUnless(vpn
, kL2TP
, type
== kTypeL2TP_IPsec
);
100 void Normalizer::NormalizeNetworkConfiguration(base::DictionaryValue
* network
) {
102 network
->GetStringWithoutPathExpansion(kType
, &type
);
103 RemoveEntryUnless(network
, kEthernet
, type
== kEthernet
);
104 RemoveEntryUnless(network
, kVPN
, type
== kVPN
);
105 RemoveEntryUnless(network
, kWiFi
, type
== kWiFi
);
109 } // namespace chromeos