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

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

Issue 482243002: Use Managed properties for Preferred and Provider. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address feedback, fix unittests Created 6 years, 4 months 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 | Annotate | Revision Log
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 #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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 // associated to |onc_field_name| and adds it to |onc_object_| at 82 // associated to |onc_field_name| and adds it to |onc_object_| at
83 // |onc_field_name|. 83 // |onc_field_name|.
84 void TranslateAndAddNestedObject(const std::string& onc_field_name, 84 void TranslateAndAddNestedObject(const std::string& onc_field_name,
85 const base::DictionaryValue& dictionary); 85 const base::DictionaryValue& dictionary);
86 86
87 // Creates an ONC object from |shill_dictionary_| according to the signature 87 // Creates an ONC object from |shill_dictionary_| according to the signature
88 // associated to |onc_field_name| and adds it to |onc_object_| at 88 // associated to |onc_field_name| and adds it to |onc_object_| at
89 // |onc_field_name|. 89 // |onc_field_name|.
90 void TranslateAndAddNestedObject(const std::string& onc_field_name); 90 void TranslateAndAddNestedObject(const std::string& onc_field_name);
91 91
92 // Sets |onc_field_name| in dictionary |onc_dictionary_name| in |onc_object_|
93 // to |value| if the dictionary exists.
94 void SetNestedOncValue(const std::string& onc_dictionary_name,
95 const std::string& onc_field_name,
96 const base::Value& value);
97
92 // Translates a list of nested objects and adds the list to |onc_object_| at 98 // Translates a list of nested objects and adds the list to |onc_object_| at
93 // |onc_field_name|. If there are errors while parsing individual objects or 99 // |onc_field_name|. If there are errors while parsing individual objects or
94 // if the resulting list contains no entries, the result will not be added to 100 // if the resulting list contains no entries, the result will not be added to
95 // |onc_object_|. 101 // |onc_object_|.
96 void TranslateAndAddListOfObjects(const std::string& onc_field_name, 102 void TranslateAndAddListOfObjects(const std::string& onc_field_name,
97 const base::ListValue& list); 103 const base::ListValue& list);
98 104
99 // Applies function CopyProperty to each field of |value_signature| and its 105 // Applies function CopyProperty to each field of |value_signature| and its
100 // base signatures. 106 // base signatures.
101 void CopyPropertiesAccordingToSignature( 107 void CopyPropertiesAccordingToSignature(
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 shill::kOpenVPNRemoteCertKUProperty, &certKU)) { 181 shill::kOpenVPNRemoteCertKUProperty, &certKU)) {
176 scoped_ptr<base::ListValue> certKUs(new base::ListValue); 182 scoped_ptr<base::ListValue> certKUs(new base::ListValue);
177 certKUs->AppendString(certKU); 183 certKUs->AppendString(certKU);
178 onc_object_->SetWithoutPathExpansion(::onc::openvpn::kRemoteCertKU, 184 onc_object_->SetWithoutPathExpansion(::onc::openvpn::kRemoteCertKU,
179 certKUs.release()); 185 certKUs.release());
180 } 186 }
181 187
182 for (const OncFieldSignature* field_signature = onc_signature_->fields; 188 for (const OncFieldSignature* field_signature = onc_signature_->fields;
183 field_signature->onc_field_name != NULL; ++field_signature) { 189 field_signature->onc_field_name != NULL; ++field_signature) {
184 const std::string& onc_field_name = field_signature->onc_field_name; 190 const std::string& onc_field_name = field_signature->onc_field_name;
185 if (onc_field_name == ::onc::vpn::kSaveCredentials || 191 if (onc_field_name == ::onc::openvpn::kRemoteCertKU ||
186 onc_field_name == ::onc::openvpn::kRemoteCertKU ||
187 onc_field_name == ::onc::openvpn::kServerCAPEMs) { 192 onc_field_name == ::onc::openvpn::kServerCAPEMs) {
188 CopyProperty(field_signature); 193 CopyProperty(field_signature);
189 continue; 194 continue;
190 } 195 }
191 196
192 std::string shill_property_name; 197 std::string shill_property_name;
193 const base::Value* shill_value = NULL; 198 const base::Value* shill_value = NULL;
194 if (!field_translation_table_ || 199 if (!field_translation_table_ ||
195 !GetShillPropertyName(field_signature->onc_field_name, 200 !GetShillPropertyName(field_signature->onc_field_name,
196 field_translation_table_, 201 field_translation_table_,
(...skipping 29 matching lines...) Expand all
226 } 231 }
227 } 232 }
228 233
229 void ShillToONCTranslator::TranslateIPsec() { 234 void ShillToONCTranslator::TranslateIPsec() {
230 CopyPropertiesAccordingToSignature(); 235 CopyPropertiesAccordingToSignature();
231 if (shill_dictionary_->HasKey(shill::kL2tpIpsecXauthUserProperty)) 236 if (shill_dictionary_->HasKey(shill::kL2tpIpsecXauthUserProperty))
232 TranslateAndAddNestedObject(::onc::ipsec::kXAUTH); 237 TranslateAndAddNestedObject(::onc::ipsec::kXAUTH);
233 } 238 }
234 239
235 void ShillToONCTranslator::TranslateVPN() { 240 void ShillToONCTranslator::TranslateVPN() {
236 TranslateWithTableAndSet(
237 shill::kProviderTypeProperty, kVPNTypeTable, ::onc::vpn::kType);
238 CopyPropertiesAccordingToSignature(); 241 CopyPropertiesAccordingToSignature();
239 242
240 std::string vpn_type; 243 // Parse Shill Provider dictionary.
241 if (onc_object_->GetStringWithoutPathExpansion(::onc::vpn::kType, 244 const base::DictionaryValue* provider = NULL;
242 &vpn_type)) { 245 if (!shill_dictionary_->GetDictionaryWithoutPathExpansion(
243 if (vpn_type == ::onc::vpn::kTypeL2TP_IPsec) { 246 shill::kProviderProperty, &provider)) {
244 TranslateAndAddNestedObject(::onc::vpn::kIPsec); 247 LOG(ERROR) << "Shill VPN object with no Provider dictionary";
245 TranslateAndAddNestedObject(::onc::vpn::kL2TP); 248 return;
246 } else { 249 }
247 TranslateAndAddNestedObject(vpn_type); 250 std::string shill_provider_type, onc_provider_type;
251 if (!provider->GetStringWithoutPathExpansion(shill::kTypeProperty,
252 &shill_provider_type)) {
253 LOG(ERROR) << "Shill VPN object with no Provider.Type";
pneubeck (no reviews) 2014/08/20 08:16:16 alternatively, instead of catching a missing input
stevenjb 2014/08/20 17:18:37 Done.
254 return;
255 }
256 TranslateStringToONC(kVPNTypeTable, shill_provider_type, &onc_provider_type);
257 onc_object_->SetStringWithoutPathExpansion(::onc::vpn::kType,
258 onc_provider_type);
259 std::string provider_host;
260 if (provider->GetStringWithoutPathExpansion(shill::kHostProperty,
pneubeck (no reviews) 2014/08/20 08:16:16 The translation tables were meant for the cases wh
stevenjb 2014/08/20 17:18:37 Ugh. Yeah, I understand your point, but this is te
pneubeck (no reviews) 2014/08/20 19:49:36 Yes, it's tedious. To get rid of it, I would prefe
261 &provider_host)) {
262 onc_object_->SetStringWithoutPathExpansion(::onc::vpn::kHost,
263 provider_host);
264 }
265
266 const base::Value* save_credentials = NULL;
267 shill_dictionary_->GetWithoutPathExpansion(shill::kSaveCredentialsProperty,
pneubeck (no reviews) 2014/08/20 08:16:16 like the Host property above, please comment out i
stevenjb 2014/08/20 17:18:37 Done.
268 &save_credentials);
269 if (save_credentials && !save_credentials->IsType(base::Value::TYPE_BOOLEAN))
270 save_credentials = NULL;
271
272 // Translate the nested dictionary.
273 if (onc_provider_type == ::onc::vpn::kTypeL2TP_IPsec) {
274 TranslateAndAddNestedObject(::onc::vpn::kIPsec, *provider);
275 TranslateAndAddNestedObject(::onc::vpn::kL2TP, *provider);
276 if (save_credentials) {
277 SetNestedOncValue(
278 ::onc::vpn::kIPsec, ::onc::vpn::kSaveCredentials, *save_credentials);
279 SetNestedOncValue(
pneubeck (no reviews) 2014/08/20 08:16:16 nit: wasn't set before and could be removed.
stevenjb 2014/08/20 17:18:37 Done.
280 ::onc::vpn::kL2TP, ::onc::vpn::kSaveCredentials, *save_credentials);
281 }
282 } else {
283 TranslateAndAddNestedObject(onc_provider_type, *provider);
284 if (save_credentials) {
285 SetNestedOncValue(
286 onc_provider_type, ::onc::vpn::kSaveCredentials, *save_credentials);
248 } 287 }
249 } 288 }
250 } 289 }
251 290
252 void ShillToONCTranslator::TranslateWiFiWithState() { 291 void ShillToONCTranslator::TranslateWiFiWithState() {
253 TranslateWithTableAndSet( 292 TranslateWithTableAndSet(
254 shill::kSecurityProperty, kWiFiSecurityTable, ::onc::wifi::kSecurity); 293 shill::kSecurityProperty, kWiFiSecurityTable, ::onc::wifi::kSecurity);
255 std::string ssid = shill_property_util::GetSSIDFromProperties( 294 std::string ssid = shill_property_util::GetSSIDFromProperties(
256 *shill_dictionary_, NULL /* ignore unknown encoding */); 295 *shill_dictionary_, NULL /* ignore unknown encoding */);
257 if (!ssid.empty()) 296 if (!ssid.empty())
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 const std::string& onc_field_name, 436 const std::string& onc_field_name,
398 const base::DictionaryValue& dictionary) { 437 const base::DictionaryValue& dictionary) {
399 const OncFieldSignature* field_signature = 438 const OncFieldSignature* field_signature =
400 GetFieldSignature(*onc_signature_, onc_field_name); 439 GetFieldSignature(*onc_signature_, onc_field_name);
401 DCHECK(field_signature) << "Unable to find signature for field " 440 DCHECK(field_signature) << "Unable to find signature for field "
402 << onc_field_name << "."; 441 << onc_field_name << ".";
403 ShillToONCTranslator nested_translator(dictionary, 442 ShillToONCTranslator nested_translator(dictionary,
404 *field_signature->value_signature); 443 *field_signature->value_signature);
405 scoped_ptr<base::DictionaryValue> nested_object = 444 scoped_ptr<base::DictionaryValue> nested_object =
406 nested_translator.CreateTranslatedONCObject(); 445 nested_translator.CreateTranslatedONCObject();
407 if (nested_object->empty()) 446 // Note: this may create an empty nested object which is necessary in case
pneubeck (no reviews) 2014/08/20 08:16:16 Instead of potentially creating empty nested objec
stevenjb 2014/08/20 17:18:38 I was trying to avoid code duplication since an em
408 return; 447 // properties are added later with SetNestedOncValue.
409 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); 448 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release());
410 } 449 }
411 450
451 void ShillToONCTranslator::SetNestedOncValue(
452 const std::string& onc_dictionary_name,
453 const std::string& onc_field_name,
454 const base::Value& value) {
455 base::DictionaryValue* nested;
456 if (!onc_object_->GetDictionaryWithoutPathExpansion(
457 onc_dictionary_name, &nested)) {
458 NOTREACHED();
459 }
460 nested->SetWithoutPathExpansion(onc_field_name, value.DeepCopy());
461 }
462
412 void ShillToONCTranslator::TranslateAndAddListOfObjects( 463 void ShillToONCTranslator::TranslateAndAddListOfObjects(
413 const std::string& onc_field_name, 464 const std::string& onc_field_name,
414 const base::ListValue& list) { 465 const base::ListValue& list) {
415 const OncFieldSignature* field_signature = 466 const OncFieldSignature* field_signature =
416 GetFieldSignature(*onc_signature_, onc_field_name); 467 GetFieldSignature(*onc_signature_, onc_field_name);
417 if (field_signature->value_signature->onc_type != base::Value::TYPE_LIST) { 468 if (field_signature->value_signature->onc_type != base::Value::TYPE_LIST) {
418 LOG(ERROR) << "ONC Field name: '" << onc_field_name << "' has type '" 469 LOG(ERROR) << "ONC Field name: '" << onc_field_name << "' has type '"
419 << field_signature->value_signature->onc_type 470 << field_signature->value_signature->onc_type
420 << "', expected: base::Value::TYPE_LIST."; 471 << "', expected: base::Value::TYPE_LIST.";
421 return; 472 return;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
508 const base::DictionaryValue& shill_dictionary, 559 const base::DictionaryValue& shill_dictionary,
509 const OncValueSignature* onc_signature) { 560 const OncValueSignature* onc_signature) {
510 CHECK(onc_signature != NULL); 561 CHECK(onc_signature != NULL);
511 562
512 ShillToONCTranslator translator(shill_dictionary, *onc_signature); 563 ShillToONCTranslator translator(shill_dictionary, *onc_signature);
513 return translator.CreateTranslatedONCObject(); 564 return translator.CreateTranslatedONCObject();
514 } 565 }
515 566
516 } // namespace onc 567 } // namespace onc
517 } // namespace chromeos 568 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698