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 const base::DictionaryValue* shill_ipconfig = NULL; | |
307 if (shill_dictionary_->GetDictionaryWithoutPathExpansion( | |
308 shill::kIPConfigProperty, &shill_ipconfig)) { | |
309 // ONC has a list of IPConfigs and Shill only a single IPConfig object. | |
310 // Create a list with a single entry. | |
stevenjb
2014/04/23 19:43:42
So, I think we should fudge this. In Shill, Servic
pneubeck (no reviews)
2014/04/25 10:07:34
Done.
You'll be able to use this translation
- if
| |
311 ShillToONCTranslator nested_translator(*shill_ipconfig, kIPConfigSignature); | |
312 scoped_ptr<base::DictionaryValue> onc_ipconfig = | |
313 nested_translator.CreateTranslatedONCObject(); | |
314 // If the nested object couldn't be parsed, simply omit it. | |
315 if (!onc_ipconfig->empty()) { | |
armansito
2014/04/23 18:46:36
Early return here would be better, i.e if (empty)
pneubeck (no reviews)
2014/04/25 10:07:34
I kept the pattern in this function, which doesn't
| |
316 scoped_ptr<base::ListValue> onc_ipconfigs(new base::ListValue()); | |
317 onc_ipconfigs->Append(onc_ipconfig.release()); | |
318 onc_object_->SetWithoutPathExpansion(::onc::network_config::kIPConfigs, | |
319 onc_ipconfigs.release()); | |
320 } | |
321 } | |
322 } | |
323 | |
324 void ShillToONCTranslator::TranslateIPConfig() { | |
325 CopyPropertiesAccordingToSignature(); | |
326 std::string ip_address; | |
327 if (shill_dictionary_->GetStringWithoutPathExpansion(shill::kAddressProperty, | |
328 &ip_address)) { | |
armansito
2014/04/23 18:46:36
nit: align "&ip_address" with "shill::kAddressProp
pneubeck (no reviews)
2014/04/25 10:07:34
Done.
| |
329 std::string type = ::onc::ipconfig::kIPv4; | |
330 if (ip_address.find(':') != std::string::npos) { | |
stevenjb
2014/04/23 19:43:42
Hmm, I guess wwe can use this for identifying ipv6
pneubeck (no reviews)
2014/04/25 10:07:34
Missed that one. Thanks.
| |
331 // ip_address is an IPv6 address and not IPv4. | |
332 type = ::onc::ipconfig::kIPv6; | |
333 } | |
334 onc_object_->SetStringWithoutPathExpansion(::onc::ipconfig::kType, type); | |
335 } | |
302 } | 336 } |
303 | 337 |
304 void ShillToONCTranslator::TranslateAndAddNestedObject( | 338 void ShillToONCTranslator::TranslateAndAddNestedObject( |
305 const std::string& onc_field_name) { | 339 const std::string& onc_field_name) { |
306 TranslateAndAddNestedObject(onc_field_name, *shill_dictionary_); | 340 TranslateAndAddNestedObject(onc_field_name, *shill_dictionary_); |
307 } | 341 } |
308 | 342 |
309 void ShillToONCTranslator::TranslateAndAddNestedObject( | 343 void ShillToONCTranslator::TranslateAndAddNestedObject( |
310 const std::string& onc_field_name, | 344 const std::string& onc_field_name, |
311 const base::DictionaryValue& dictionary) { | 345 const base::DictionaryValue& dictionary) { |
(...skipping 26 matching lines...) Expand all Loading... | |
338 for (base::ListValue::const_iterator it = list.begin(); | 372 for (base::ListValue::const_iterator it = list.begin(); |
339 it != list.end(); ++it) { | 373 it != list.end(); ++it) { |
340 const base::DictionaryValue* shill_value = NULL; | 374 const base::DictionaryValue* shill_value = NULL; |
341 if (!(*it)->GetAsDictionary(&shill_value)) | 375 if (!(*it)->GetAsDictionary(&shill_value)) |
342 continue; | 376 continue; |
343 ShillToONCTranslator nested_translator( | 377 ShillToONCTranslator nested_translator( |
344 *shill_value, | 378 *shill_value, |
345 *field_signature->value_signature->onc_array_entry_signature); | 379 *field_signature->value_signature->onc_array_entry_signature); |
346 scoped_ptr<base::DictionaryValue> nested_object = | 380 scoped_ptr<base::DictionaryValue> nested_object = |
347 nested_translator.CreateTranslatedONCObject(); | 381 nested_translator.CreateTranslatedONCObject(); |
382 // If the nested object couldn't be parsed, simply omit it. | |
348 if (nested_object->empty()) | 383 if (nested_object->empty()) |
349 // The nested object couldn't be parsed, so simply omit it. | |
350 continue; | 384 continue; |
351 result->Append(nested_object.release()); | 385 result->Append(nested_object.release()); |
352 } | 386 } |
387 // If there are no entries in the list, there is no need to expose this field. | |
353 if (result->empty()) | 388 if (result->empty()) |
354 // There are no entries in the list, so there is no need to expose this | |
355 // field. | |
356 return; | 389 return; |
357 onc_object_->SetWithoutPathExpansion(onc_field_name, result.release()); | 390 onc_object_->SetWithoutPathExpansion(onc_field_name, result.release()); |
358 } | 391 } |
359 | 392 |
360 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { | 393 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { |
361 CopyPropertiesAccordingToSignature(onc_signature_); | 394 CopyPropertiesAccordingToSignature(onc_signature_); |
362 } | 395 } |
363 | 396 |
364 void ShillToONCTranslator::CopyPropertiesAccordingToSignature( | 397 void ShillToONCTranslator::CopyPropertiesAccordingToSignature( |
365 const OncValueSignature* value_signature) { | 398 const OncValueSignature* value_signature) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
422 const base::DictionaryValue& shill_dictionary, | 455 const base::DictionaryValue& shill_dictionary, |
423 const OncValueSignature* onc_signature) { | 456 const OncValueSignature* onc_signature) { |
424 CHECK(onc_signature != NULL); | 457 CHECK(onc_signature != NULL); |
425 | 458 |
426 ShillToONCTranslator translator(shill_dictionary, *onc_signature); | 459 ShillToONCTranslator translator(shill_dictionary, *onc_signature); |
427 return translator.CreateTranslatedONCObject(); | 460 return translator.CreateTranslatedONCObject(); |
428 } | 461 } |
429 | 462 |
430 } // namespace onc | 463 } // namespace onc |
431 } // namespace chromeos | 464 } // namespace chromeos |
OLD | NEW |