Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: chromeos/network/onc/onc_translator_onc_to_shill.cc

Issue 11664005: Extending the translation from ONC to Shill. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Initial patch. Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // The implementation of TranslateONCObjectToShill is structured in two parts: 5 // The implementation of TranslateONCObjectToShill is structured in two parts:
6 // - The recursion through the existing ONC hierarchy 6 // - The recursion through the existing ONC hierarchy
7 // see TranslateONCHierarchy 7 // see TranslateONCHierarchy
8 // - The local translation of an object depending on the associated signature 8 // - The local translation of an object depending on the associated signature
9 // see LocalTranslator::TranslateFields 9 // see LocalTranslator::TranslateFields
10 10
11 #include "chromeos/network/onc/onc_translator.h" 11 #include "chromeos/network/onc/onc_translator.h"
12 12
13 #include <string> 13 #include <string>
14 14
15 #include "base/json/json_reader.h" 15 #include "base/json/json_reader.h"
16 #include "base/json/json_writer.h" 16 #include "base/json/json_writer.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "chromeos/network/onc/onc_constants.h" 19 #include "chromeos/network/onc/onc_constants.h"
20 #include "chromeos/network/onc/onc_signature.h" 20 #include "chromeos/network/onc/onc_signature.h"
21 #include "chromeos/network/onc/onc_translation_tables.h" 21 #include "chromeos/network/onc/onc_translation_tables.h"
22 #include "chromeos/network/onc/onc_utils.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h" 23 #include "third_party/cros_system_api/dbus/service_constants.h"
23 24
24 namespace chromeos { 25 namespace chromeos {
25 namespace onc { 26 namespace onc {
26 27
27 namespace { 28 namespace {
28 29
29 scoped_ptr<base::StringValue> ConvertValueToString(const base::Value& value) { 30 scoped_ptr<base::StringValue> ConvertValueToString(const base::Value& value) {
30 std::string str; 31 std::string str;
31 if (!value.GetAsString(&str)) 32 if (!value.GetAsString(&str))
(...skipping 14 matching lines...) Expand all
46 : onc_signature_(&onc_signature), 47 : onc_signature_(&onc_signature),
47 onc_object_(&onc_object), 48 onc_object_(&onc_object),
48 shill_dictionary_(shill_dictionary) { 49 shill_dictionary_(shill_dictionary) {
49 } 50 }
50 51
51 void TranslateFields(); 52 void TranslateFields();
52 53
53 private: 54 private:
54 void TranslateOpenVPN(); 55 void TranslateOpenVPN();
55 void TranslateVPN(); 56 void TranslateVPN();
57 void TranslateWiFi();
58 void TranslateProxySettings();
59 void TranslateEAP();
56 void TranslateNetworkConfiguration(); 60 void TranslateNetworkConfiguration();
57 61
58 // Copies all entries from |onc_object_| to |shill_dictionary_| for which a 62 // Copies all entries from |onc_object_| to |shill_dictionary_| for which a
59 // translation (shill_property_name) is defined by |onc_signature_|. 63 // translation (shill_property_name) is defined by |onc_signature_|.
60 void CopyFieldsAccordingToSignature(); 64 void CopyFieldsAccordingToSignature();
61 65
62 // Adds |value| to |shill_dictionary| at the field shill_property_name given 66 // Adds |value| to |shill_dictionary| at the field shill_property_name given
63 // by the associated signature. Takes ownership of |value|. Does nothing if 67 // by the associated signature. Takes ownership of |value|. Does nothing if
64 // |value| is NULL or the property name cannot be read from the signature. 68 // |value| is NULL or the property name cannot be read from the signature.
65 void AddValueAccordingToSignature(const std::string& onc_field_name, 69 void AddValueAccordingToSignature(const std::string& onc_field_name,
(...skipping 13 matching lines...) Expand all
79 DISALLOW_COPY_AND_ASSIGN(LocalTranslator); 83 DISALLOW_COPY_AND_ASSIGN(LocalTranslator);
80 }; 84 };
81 85
82 void LocalTranslator::TranslateFields() { 86 void LocalTranslator::TranslateFields() {
83 if (onc_signature_ == &kNetworkConfigurationSignature) 87 if (onc_signature_ == &kNetworkConfigurationSignature)
84 TranslateNetworkConfiguration(); 88 TranslateNetworkConfiguration();
85 else if (onc_signature_ == &kVPNSignature) 89 else if (onc_signature_ == &kVPNSignature)
86 TranslateVPN(); 90 TranslateVPN();
87 else if (onc_signature_ == &kOpenVPNSignature) 91 else if (onc_signature_ == &kOpenVPNSignature)
88 TranslateOpenVPN(); 92 TranslateOpenVPN();
93 else if (onc_signature_ == &kWiFiSignature)
94 TranslateWiFi();
95 else if (onc_signature_ == &kEAPSignature)
96 TranslateEAP();
89 else 97 else
90 CopyFieldsAccordingToSignature(); 98 CopyFieldsAccordingToSignature();
91 } 99 }
92 100
93 void LocalTranslator::TranslateOpenVPN() { 101 void LocalTranslator::TranslateOpenVPN() {
94 // Shill supports only one RemoteCertKU but ONC a list. 102 // Shill supports only one RemoteCertKU but ONC a list.
95 // Copy only the first entry if existing. 103 // Copy only the first entry if existing.
96 const base::ListValue* certKUs; 104 const base::ListValue* certKUs;
97 std::string certKU; 105 std::string certKU;
98 if (onc_object_->GetListWithoutPathExpansion(vpn::kRemoteCertKU, &certKUs) && 106 if (onc_object_->GetListWithoutPathExpansion(vpn::kRemoteCertKU, &certKUs) &&
99 certKUs->GetString(0, &certKU)) { 107 certKUs->GetString(0, &certKU)) {
100 shill_dictionary_->SetStringWithoutPathExpansion( 108 shill_dictionary_->SetStringWithoutPathExpansion(
101 flimflam::kOpenVPNRemoteCertKUProperty, certKU); 109 flimflam::kOpenVPNRemoteCertKUProperty, certKU);
102 } 110 }
103 111
104 for (base::DictionaryValue::Iterator it(*onc_object_); it.HasNext(); 112 for (base::DictionaryValue::Iterator it(*onc_object_); it.HasNext();
105 it.Advance()) { 113 it.Advance()) {
106 scoped_ptr<base::Value> translated; 114 scoped_ptr<base::Value> translated;
107 if (it.key() == vpn::kSaveCredentials || it.key() == vpn::kRemoteCertKU) { 115 if (it.key() == vpn::kSaveCredentials || it.key() == vpn::kRemoteCertKU) {
108 translated.reset(it.value().DeepCopy()); 116 translated.reset(it.value().DeepCopy());
109 } else { 117 } else {
110 // Shill wants all Provider/VPN fields to be strings. 118 // Shill wants all Provider/VPN fields to be strings.
111 translated = ConvertValueToString(it.value()); 119 translated = ConvertValueToString(it.value());
112 } 120 }
113 AddValueAccordingToSignature(it.key(), translated.Pass()); 121 AddValueAccordingToSignature(it.key(), translated.Pass());
114 } 122 }
115 } 123 }
116 124
117 void LocalTranslator::TranslateVPN() { 125 void LocalTranslator::TranslateVPN() {
118 TranslateWithTableAndSet(kType, kVPNTypeTable, 126 std::string type;
127 onc_object_->GetStringWithoutPathExpansion(kType, &type);
128 TranslateWithTableAndSet(type, kVPNTypeTable,
119 flimflam::kProviderTypeProperty); 129 flimflam::kProviderTypeProperty);
130
131 CopyFieldsAccordingToSignature();
132 }
133
134 void LocalTranslator::TranslateWiFi() {
135 std::string security;
136 onc_object_->GetStringWithoutPathExpansion(wifi::kSecurity, &security);
137 TranslateWithTableAndSet(security, kWiFiSecurityTable,
138 flimflam::kSecurityProperty);
139
140 CopyFieldsAccordingToSignature();
141 }
142
143 void LocalTranslator::TranslateEAP() {
144 std::string outer;
145 onc_object_->GetStringWithoutPathExpansion(eap::kOuter, &outer);
146 TranslateWithTableAndSet(outer, kEAPOuterTable, flimflam::kEapMethodProperty);
147
148 // Translate the inner protocol only for outer tunneling protocols.
149 if (outer == eap::kPEAP || outer == eap::kEAP_TTLS) {
150 // In ONC the Inner protocol defaults to "Automatic".
151 std::string inner = eap::kAutomatic;
152 // ONC's Inner == "Automatic" translates to omitting the Phase2 property in
153 // Shill.
154 onc_object_->GetStringWithoutPathExpansion(eap::kInner, &inner);
155 if (inner != eap::kAutomatic) {
156 const StringTranslationEntry* table =
157 outer == eap::kPEAP ? kEAP_PEAP_InnerTable : kEAP_TTLS_InnerTable;
158 TranslateWithTableAndSet(inner, table, flimflam::kEapPhase2AuthProperty);
159 }
160 }
161
120 CopyFieldsAccordingToSignature(); 162 CopyFieldsAccordingToSignature();
121 } 163 }
122 164
123 void LocalTranslator::TranslateNetworkConfiguration() { 165 void LocalTranslator::TranslateNetworkConfiguration() {
124 TranslateWithTableAndSet(kType, kNetworkTypeTable, flimflam::kTypeProperty); 166 std::string type;
167 onc_object_->GetStringWithoutPathExpansion(kType, &type);
168 TranslateWithTableAndSet(type, kNetworkTypeTable, flimflam::kTypeProperty);
169
170 // Shill doesn't allow setting the name for non-VPN networks.
171 if (type == kVPN) {
172 std::string name;
173 onc_object_->GetStringWithoutPathExpansion(kName, &name);
174 shill_dictionary_->SetStringWithoutPathExpansion(
175 flimflam::kNameProperty, name);
176 }
177
125 CopyFieldsAccordingToSignature(); 178 CopyFieldsAccordingToSignature();
126 } 179 }
127 180
128 void LocalTranslator::CopyFieldsAccordingToSignature() { 181 void LocalTranslator::CopyFieldsAccordingToSignature() {
129 for (base::DictionaryValue::Iterator it(*onc_object_); it.HasNext(); 182 for (base::DictionaryValue::Iterator it(*onc_object_); it.HasNext();
130 it.Advance()) { 183 it.Advance()) {
131 AddValueAccordingToSignature(it.key(), 184 AddValueAccordingToSignature(it.key(),
132 make_scoped_ptr(it.value().DeepCopy())); 185 make_scoped_ptr(it.value().DeepCopy()));
133 } 186 }
134 } 187 }
135 188
136 void LocalTranslator::AddValueAccordingToSignature( 189 void LocalTranslator::AddValueAccordingToSignature(
137 const std::string& onc_name, 190 const std::string& onc_name,
138 scoped_ptr<base::Value> value) { 191 scoped_ptr<base::Value> value) {
139 if (value.get() == NULL) 192 if (value.get() == NULL)
140 return; 193 return;
141 const OncFieldSignature* field_signature = 194 const OncFieldSignature* field_signature =
142 GetFieldSignature(*onc_signature_, onc_name); 195 GetFieldSignature(*onc_signature_, onc_name);
143 DCHECK(field_signature != NULL); 196 DCHECK(field_signature != NULL);
144 if (field_signature == NULL || field_signature->shill_property_name == NULL) 197 if (field_signature == NULL || field_signature->shill_property_name == NULL)
145 return; 198 return;
146 199
147 shill_dictionary_->SetWithoutPathExpansion( 200 shill_dictionary_->SetWithoutPathExpansion(
148 field_signature->shill_property_name, value.release()); 201 field_signature->shill_property_name, value.release());
149 } 202 }
150 203
151 void LocalTranslator::TranslateWithTableAndSet( 204 void LocalTranslator::TranslateWithTableAndSet(
152 const std::string& onc_field_name, 205 const std::string& onc_value,
153 const StringTranslationEntry table[], 206 const StringTranslationEntry table[],
154 const std::string& shill_property_name) { 207 const std::string& shill_property_name) {
155 std::string onc_value;
156 if (!onc_object_->GetStringWithoutPathExpansion(onc_field_name, &onc_value))
157 return;
158
159 for (int i = 0; table[i].onc_value != NULL; ++i) { 208 for (int i = 0; table[i].onc_value != NULL; ++i) {
160 if (onc_value != table[i].onc_value) 209 if (onc_value != table[i].onc_value)
161 continue; 210 continue;
162 shill_dictionary_->SetStringWithoutPathExpansion(shill_property_name, 211 shill_dictionary_->SetStringWithoutPathExpansion(shill_property_name,
163 table[i].shill_value); 212 table[i].shill_value);
164 return; 213 return;
165 } 214 }
166 // As we previously validate ONC, this case should never occur. If it still 215 // As we previously validate ONC, this case should never occur. If it still
167 // occurs, we should check here. Otherwise the failure will only show up much 216 // occurs, we should check here. Otherwise the failure will only show up much
168 // later in Shill. 217 // later in Shill.
169 LOG(ERROR) << "Value '" << onc_value << "' for field '" 218 LOG(ERROR) << "Value '" << onc_value << "cannot be translated to Shill";
170 << onc_field_name << "' cannot be translated to Shill";
171 } 219 }
172 220
173 // Iterates recursively over |onc_object| and its |signature|. At each object 221 // Iterates recursively over |onc_object| and its |signature|. At each object
174 // applies the local translation using LocalTranslator::TranslateFields. The 222 // applies the local translation using LocalTranslator::TranslateFields. The
175 // results are written to |shill_dictionary|. 223 // results are written to |shill_dictionary|.
176 void TranslateONCHierarchy(const OncValueSignature& signature, 224 void TranslateONCHierarchy(const OncValueSignature& signature,
177 const base::DictionaryValue& onc_object, 225 const base::DictionaryValue& onc_object,
178 base::DictionaryValue* shill_dictionary) { 226 base::DictionaryValue* shill_dictionary) {
179 // Translates fields of |onc_object| and writes them to |shill_dictionary_|. 227 // Translates fields of |onc_object| and writes them to |shill_dictionary_|.
180 LocalTranslator translator(signature, onc_object, shill_dictionary); 228 LocalTranslator translator(signature, onc_object, shill_dictionary);
(...skipping 20 matching lines...) Expand all
201 const OncValueSignature* onc_signature, 249 const OncValueSignature* onc_signature,
202 const base::DictionaryValue& onc_object) { 250 const base::DictionaryValue& onc_object) {
203 CHECK(onc_signature != NULL); 251 CHECK(onc_signature != NULL);
204 scoped_ptr<base::DictionaryValue> shill_dictionary(new base::DictionaryValue); 252 scoped_ptr<base::DictionaryValue> shill_dictionary(new base::DictionaryValue);
205 TranslateONCHierarchy(*onc_signature, onc_object, shill_dictionary.get()); 253 TranslateONCHierarchy(*onc_signature, onc_object, shill_dictionary.get());
206 return shill_dictionary.Pass(); 254 return shill_dictionary.Pass();
207 } 255 }
208 256
209 } // namespace onc 257 } // namespace onc
210 } // namespace chromeos 258 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698