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" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject(); | 59 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject(); |
60 | 60 |
61 private: | 61 private: |
62 void TranslateEthernet(); | 62 void TranslateEthernet(); |
63 void TranslateOpenVPN(); | 63 void TranslateOpenVPN(); |
64 void TranslateIPsec(); | 64 void TranslateIPsec(); |
65 void TranslateVPN(); | 65 void TranslateVPN(); |
66 void TranslateWiFiWithState(); | 66 void TranslateWiFiWithState(); |
67 void TranslateCellularWithState(); | 67 void TranslateCellularWithState(); |
68 void TranslateNetworkWithState(); | 68 void TranslateNetworkWithState(); |
| 69 void TranslateIPConfig(); |
69 | 70 |
70 // Creates an ONC object from |dictionary| according to the signature | 71 // Creates an ONC object from |dictionary| according to the signature |
71 // associated to |onc_field_name| and adds it to |onc_object_| at | 72 // associated to |onc_field_name| and adds it to |onc_object_| at |
72 // |onc_field_name|. | 73 // |onc_field_name|. |
73 void TranslateAndAddNestedObject(const std::string& onc_field_name, | 74 void TranslateAndAddNestedObject(const std::string& onc_field_name, |
74 const base::DictionaryValue& dictionary); | 75 const base::DictionaryValue& dictionary); |
75 | 76 |
76 // Creates an ONC object from |shill_dictionary_| according to the signature | 77 // Creates an ONC object from |shill_dictionary_| according to the signature |
77 // associated to |onc_field_name| and adds it to |onc_object_| at | 78 // associated to |onc_field_name| and adds it to |onc_object_| at |
78 // |onc_field_name|. | 79 // |onc_field_name|. |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 } else if (onc_signature_ == &kVPNSignature) { | 125 } else if (onc_signature_ == &kVPNSignature) { |
125 TranslateVPN(); | 126 TranslateVPN(); |
126 } else if (onc_signature_ == &kOpenVPNSignature) { | 127 } else if (onc_signature_ == &kOpenVPNSignature) { |
127 TranslateOpenVPN(); | 128 TranslateOpenVPN(); |
128 } else if (onc_signature_ == &kIPsecSignature) { | 129 } else if (onc_signature_ == &kIPsecSignature) { |
129 TranslateIPsec(); | 130 TranslateIPsec(); |
130 } else if (onc_signature_ == &kWiFiWithStateSignature) { | 131 } else if (onc_signature_ == &kWiFiWithStateSignature) { |
131 TranslateWiFiWithState(); | 132 TranslateWiFiWithState(); |
132 } else if (onc_signature_ == &kCellularWithStateSignature) { | 133 } else if (onc_signature_ == &kCellularWithStateSignature) { |
133 TranslateCellularWithState(); | 134 TranslateCellularWithState(); |
| 135 } else if (onc_signature_ == &kIPConfigSignature) { |
| 136 TranslateIPConfig(); |
134 } else { | 137 } else { |
135 CopyPropertiesAccordingToSignature(); | 138 CopyPropertiesAccordingToSignature(); |
136 } | 139 } |
137 return onc_object_.Pass(); | 140 return onc_object_.Pass(); |
138 } | 141 } |
139 | 142 |
140 void ShillToONCTranslator::TranslateEthernet() { | 143 void ShillToONCTranslator::TranslateEthernet() { |
141 std::string shill_network_type; | 144 std::string shill_network_type; |
142 shill_dictionary_->GetStringWithoutPathExpansion(shill::kTypeProperty, | 145 shill_dictionary_->GetStringWithoutPathExpansion(shill::kTypeProperty, |
143 &shill_network_type); | 146 &shill_network_type); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 CopyPropertiesAccordingToSignature(); | 250 CopyPropertiesAccordingToSignature(); |
248 const base::DictionaryValue* dictionary = NULL; | 251 const base::DictionaryValue* dictionary = NULL; |
249 if (shill_dictionary_->GetDictionaryWithoutPathExpansion( | 252 if (shill_dictionary_->GetDictionaryWithoutPathExpansion( |
250 shill::kServingOperatorProperty, &dictionary)) { | 253 shill::kServingOperatorProperty, &dictionary)) { |
251 TranslateAndAddNestedObject(::onc::cellular::kServingOperator, *dictionary); | 254 TranslateAndAddNestedObject(::onc::cellular::kServingOperator, *dictionary); |
252 } | 255 } |
253 if (shill_dictionary_->GetDictionaryWithoutPathExpansion( | 256 if (shill_dictionary_->GetDictionaryWithoutPathExpansion( |
254 shill::kCellularApnProperty, &dictionary)) { | 257 shill::kCellularApnProperty, &dictionary)) { |
255 TranslateAndAddNestedObject(::onc::cellular::kAPN, *dictionary); | 258 TranslateAndAddNestedObject(::onc::cellular::kAPN, *dictionary); |
256 } | 259 } |
257 const base::ListValue* list = NULL; | 260 const base::ListValue* shill_apns = NULL; |
258 if (shill_dictionary_->GetListWithoutPathExpansion( | 261 if (shill_dictionary_->GetListWithoutPathExpansion( |
259 shill::kCellularApnListProperty, &list)) { | 262 shill::kCellularApnListProperty, &shill_apns)) { |
260 TranslateAndAddListOfObjects(::onc::cellular::kAPNList, *list); | 263 TranslateAndAddListOfObjects(::onc::cellular::kAPNList, *shill_apns); |
261 } | 264 } |
262 } | 265 } |
263 | 266 |
264 void ShillToONCTranslator::TranslateNetworkWithState() { | 267 void ShillToONCTranslator::TranslateNetworkWithState() { |
265 CopyPropertiesAccordingToSignature(); | 268 CopyPropertiesAccordingToSignature(); |
266 | 269 |
267 std::string shill_network_type; | 270 std::string shill_network_type; |
268 shill_dictionary_->GetStringWithoutPathExpansion(shill::kTypeProperty, | 271 shill_dictionary_->GetStringWithoutPathExpansion(shill::kTypeProperty, |
269 &shill_network_type); | 272 &shill_network_type); |
270 std::string onc_network_type = ::onc::network_type::kEthernet; | 273 std::string onc_network_type = ::onc::network_type::kEthernet; |
(...skipping 21 matching lines...) Expand all Loading... |
292 &state)) { | 295 &state)) { |
293 std::string onc_state = ::onc::connection_state::kNotConnected; | 296 std::string onc_state = ::onc::connection_state::kNotConnected; |
294 if (NetworkState::StateIsConnected(state)) { | 297 if (NetworkState::StateIsConnected(state)) { |
295 onc_state = ::onc::connection_state::kConnected; | 298 onc_state = ::onc::connection_state::kConnected; |
296 } else if (NetworkState::StateIsConnecting(state)) { | 299 } else if (NetworkState::StateIsConnecting(state)) { |
297 onc_state = ::onc::connection_state::kConnecting; | 300 onc_state = ::onc::connection_state::kConnecting; |
298 } | 301 } |
299 onc_object_->SetStringWithoutPathExpansion( | 302 onc_object_->SetStringWithoutPathExpansion( |
300 ::onc::network_config::kConnectionState, onc_state); | 303 ::onc::network_config::kConnectionState, onc_state); |
301 } | 304 } |
| 305 |
| 306 // Shill's Service has an IPConfig property (note the singular, and not a |
| 307 // IPConfigs property). However, we require the caller of the translation to |
| 308 // patch the Shill dictionary before passing it to the translator. |
| 309 const base::ListValue* shill_ipconfigs = NULL; |
| 310 if (shill_dictionary_->GetListWithoutPathExpansion(shill::kIPConfigsProperty, |
| 311 &shill_ipconfigs)) { |
| 312 TranslateAndAddListOfObjects(::onc::network_config::kIPConfigs, |
| 313 *shill_ipconfigs); |
| 314 } |
| 315 } |
| 316 |
| 317 void ShillToONCTranslator::TranslateIPConfig() { |
| 318 CopyPropertiesAccordingToSignature(); |
| 319 std::string shill_ip_method; |
| 320 shill_dictionary_->GetStringWithoutPathExpansion(shill::kMethodProperty, |
| 321 &shill_ip_method); |
| 322 if (shill_ip_method != shill::kTypeIPv4 && |
| 323 shill_ip_method != shill::kTypeIPv6) { |
| 324 LOG(ERROR) << "Unhandled IPConfig Method value " << shill_ip_method; |
| 325 return; |
| 326 } |
| 327 |
| 328 std::string type = ::onc::ipconfig::kIPv4; |
| 329 if (shill_ip_method == shill::kTypeIPv6) |
| 330 type = ::onc::ipconfig::kIPv6; |
| 331 onc_object_->SetStringWithoutPathExpansion(::onc::ipconfig::kType, type); |
302 } | 332 } |
303 | 333 |
304 void ShillToONCTranslator::TranslateAndAddNestedObject( | 334 void ShillToONCTranslator::TranslateAndAddNestedObject( |
305 const std::string& onc_field_name) { | 335 const std::string& onc_field_name) { |
306 TranslateAndAddNestedObject(onc_field_name, *shill_dictionary_); | 336 TranslateAndAddNestedObject(onc_field_name, *shill_dictionary_); |
307 } | 337 } |
308 | 338 |
309 void ShillToONCTranslator::TranslateAndAddNestedObject( | 339 void ShillToONCTranslator::TranslateAndAddNestedObject( |
310 const std::string& onc_field_name, | 340 const std::string& onc_field_name, |
311 const base::DictionaryValue& dictionary) { | 341 const base::DictionaryValue& dictionary) { |
(...skipping 26 matching lines...) Expand all Loading... |
338 for (base::ListValue::const_iterator it = list.begin(); | 368 for (base::ListValue::const_iterator it = list.begin(); |
339 it != list.end(); ++it) { | 369 it != list.end(); ++it) { |
340 const base::DictionaryValue* shill_value = NULL; | 370 const base::DictionaryValue* shill_value = NULL; |
341 if (!(*it)->GetAsDictionary(&shill_value)) | 371 if (!(*it)->GetAsDictionary(&shill_value)) |
342 continue; | 372 continue; |
343 ShillToONCTranslator nested_translator( | 373 ShillToONCTranslator nested_translator( |
344 *shill_value, | 374 *shill_value, |
345 *field_signature->value_signature->onc_array_entry_signature); | 375 *field_signature->value_signature->onc_array_entry_signature); |
346 scoped_ptr<base::DictionaryValue> nested_object = | 376 scoped_ptr<base::DictionaryValue> nested_object = |
347 nested_translator.CreateTranslatedONCObject(); | 377 nested_translator.CreateTranslatedONCObject(); |
| 378 // If the nested object couldn't be parsed, simply omit it. |
348 if (nested_object->empty()) | 379 if (nested_object->empty()) |
349 // The nested object couldn't be parsed, so simply omit it. | |
350 continue; | 380 continue; |
351 result->Append(nested_object.release()); | 381 result->Append(nested_object.release()); |
352 } | 382 } |
| 383 // If there are no entries in the list, there is no need to expose this field. |
353 if (result->empty()) | 384 if (result->empty()) |
354 // There are no entries in the list, so there is no need to expose this | |
355 // field. | |
356 return; | 385 return; |
357 onc_object_->SetWithoutPathExpansion(onc_field_name, result.release()); | 386 onc_object_->SetWithoutPathExpansion(onc_field_name, result.release()); |
358 } | 387 } |
359 | 388 |
360 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { | 389 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { |
361 CopyPropertiesAccordingToSignature(onc_signature_); | 390 CopyPropertiesAccordingToSignature(onc_signature_); |
362 } | 391 } |
363 | 392 |
364 void ShillToONCTranslator::CopyPropertiesAccordingToSignature( | 393 void ShillToONCTranslator::CopyPropertiesAccordingToSignature( |
365 const OncValueSignature* value_signature) { | 394 const OncValueSignature* value_signature) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 const base::DictionaryValue& shill_dictionary, | 451 const base::DictionaryValue& shill_dictionary, |
423 const OncValueSignature* onc_signature) { | 452 const OncValueSignature* onc_signature) { |
424 CHECK(onc_signature != NULL); | 453 CHECK(onc_signature != NULL); |
425 | 454 |
426 ShillToONCTranslator translator(shill_dictionary, *onc_signature); | 455 ShillToONCTranslator translator(shill_dictionary, *onc_signature); |
427 return translator.CreateTranslatedONCObject(); | 456 return translator.CreateTranslatedONCObject(); |
428 } | 457 } |
429 | 458 |
430 } // namespace onc | 459 } // namespace onc |
431 } // namespace chromeos | 460 } // namespace chromeos |
OLD | NEW |