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" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chromeos/network/network_state.h" | |
14 #include "chromeos/network/onc/onc_constants.h" | 15 #include "chromeos/network/onc/onc_constants.h" |
15 #include "chromeos/network/onc/onc_signature.h" | 16 #include "chromeos/network/onc/onc_signature.h" |
16 #include "chromeos/network/onc/onc_translation_tables.h" | 17 #include "chromeos/network/onc/onc_translation_tables.h" |
17 #include "third_party/cros_system_api/dbus/service_constants.h" | 18 #include "third_party/cros_system_api/dbus/service_constants.h" |
18 | 19 |
19 namespace chromeos { | 20 namespace chromeos { |
20 namespace onc { | 21 namespace onc { |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // Converts |str| to a base::Value of the given |type|. If the conversion fails, | 25 // Converts |str| to a base::Value of the given |type|. If the conversion fails, |
25 // returns NULL. | 26 // returns NULL. |
26 scoped_ptr<base::Value> ConvertStringToValue(const std::string& str, | 27 scoped_ptr<base::Value> ConvertStringToValue(const std::string& str, |
27 base::Value::Type type) { | 28 base::Value::Type type) { |
28 base::Value* value; | 29 base::Value* value; |
29 if (type == base::Value::TYPE_STRING) | 30 if (type == base::Value::TYPE_STRING) { |
30 value = base::Value::CreateStringValue(str); | 31 value = base::Value::CreateStringValue(str); |
31 else | 32 } else { |
32 value = base::JSONReader::Read(str); | 33 value = base::JSONReader::Read(str); |
34 } | |
33 | 35 |
34 if (value == NULL || value->GetType() != type) { | 36 if (value == NULL || value->GetType() != type) { |
35 delete value; | 37 delete value; |
36 value = NULL; | 38 value = NULL; |
37 } | 39 } |
38 return make_scoped_ptr(value); | 40 return make_scoped_ptr(value); |
39 } | 41 } |
40 | 42 |
41 // This class implements the translation of properties from the given | 43 // This class implements the translation of properties from the given |
42 // |shill_dictionary| to a new ONC object of signature |onc_signature|. Using | 44 // |shill_dictionary| to a new ONC object of signature |onc_signature|. Using |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 const base::DictionaryValue* shill_dictionary_; | 81 const base::DictionaryValue* shill_dictionary_; |
80 const OncValueSignature* onc_signature_; | 82 const OncValueSignature* onc_signature_; |
81 scoped_ptr<base::DictionaryValue> onc_object_; | 83 scoped_ptr<base::DictionaryValue> onc_object_; |
82 | 84 |
83 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); | 85 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); |
84 }; | 86 }; |
85 | 87 |
86 scoped_ptr<base::DictionaryValue> | 88 scoped_ptr<base::DictionaryValue> |
87 ShillToONCTranslator::CreateTranslatedONCObject() { | 89 ShillToONCTranslator::CreateTranslatedONCObject() { |
88 onc_object_.reset(new base::DictionaryValue); | 90 onc_object_.reset(new base::DictionaryValue); |
89 if (onc_signature_ == &kNetworkConfigurationSignature) | 91 if (onc_signature_ == &kNetworkConfigurationSignature) { |
90 TranslateNetworkConfiguration(); | 92 TranslateNetworkConfiguration(); |
91 else if (onc_signature_ == &kVPNSignature) | 93 } else if (onc_signature_ == &kVPNSignature) { |
92 TranslateVPN(); | 94 TranslateVPN(); |
93 else if (onc_signature_ == &kOpenVPNSignature) | 95 } else if (onc_signature_ == &kOpenVPNSignature) { |
94 TranslateOpenVPN(); | 96 TranslateOpenVPN(); |
95 else | 97 } else { |
96 CopyPropertiesAccordingToSignature(); | 98 CopyPropertiesAccordingToSignature(); |
99 } | |
97 return onc_object_.Pass(); | 100 return onc_object_.Pass(); |
98 } | 101 } |
99 | 102 |
100 void ShillToONCTranslator::TranslateOpenVPN() { | 103 void ShillToONCTranslator::TranslateOpenVPN() { |
101 // Shill supports only one RemoteCertKU but ONC requires a list. If existing, | 104 // Shill supports only one RemoteCertKU but ONC requires a list. If existing, |
102 // wraps the value into a list. | 105 // wraps the value into a list. |
103 std::string certKU; | 106 std::string certKU; |
104 if (shill_dictionary_->GetStringWithoutPathExpansion( | 107 if (shill_dictionary_->GetStringWithoutPathExpansion( |
105 flimflam::kOpenVPNRemoteCertKUProperty, &certKU)) { | 108 flimflam::kOpenVPNRemoteCertKUProperty, &certKU)) { |
106 scoped_ptr<base::ListValue> certKUs(new base::ListValue); | 109 scoped_ptr<base::ListValue> certKUs(new base::ListValue); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 LOG(ERROR) << "Shill property '" << field_signature->shill_property_name | 143 LOG(ERROR) << "Shill property '" << field_signature->shill_property_name |
141 << "' has value '" << shill_value | 144 << "' has value '" << shill_value |
142 << "', but expected a string"; | 145 << "', but expected a string"; |
143 } | 146 } |
144 onc_object_->SetWithoutPathExpansion(onc_field_name, translated.release()); | 147 onc_object_->SetWithoutPathExpansion(onc_field_name, translated.release()); |
145 } | 148 } |
146 } | 149 } |
147 | 150 |
148 void ShillToONCTranslator::TranslateVPN() { | 151 void ShillToONCTranslator::TranslateVPN() { |
149 TranslateWithTableAndSet(flimflam::kProviderTypeProperty, kVPNTypeTable, | 152 TranslateWithTableAndSet(flimflam::kProviderTypeProperty, kVPNTypeTable, |
150 kType); | 153 vpn::kType); |
151 CopyPropertiesAccordingToSignature(); | 154 CopyPropertiesAccordingToSignature(); |
152 | 155 |
153 std::string vpn_type; | 156 std::string vpn_type; |
154 if (onc_object_->GetStringWithoutPathExpansion(kType, &vpn_type)) { | 157 if (onc_object_->GetStringWithoutPathExpansion(vpn::kType, |
158 &vpn_type)) { | |
155 if (vpn_type == vpn::kTypeL2TP_IPsec) { | 159 if (vpn_type == vpn::kTypeL2TP_IPsec) { |
156 TranslateAndAddNestedObject(vpn::kIPsec); | 160 TranslateAndAddNestedObject(vpn::kIPsec); |
157 TranslateAndAddNestedObject(vpn::kL2TP); | 161 TranslateAndAddNestedObject(vpn::kL2TP); |
158 } else { | 162 } else { |
159 TranslateAndAddNestedObject(vpn_type); | 163 TranslateAndAddNestedObject(vpn_type); |
160 } | 164 } |
161 } | 165 } |
162 } | 166 } |
163 | 167 |
164 void ShillToONCTranslator::TranslateAndAddNestedObject( | 168 void ShillToONCTranslator::TranslateAndAddNestedObject( |
165 const std::string& onc_field_name) { | 169 const std::string& onc_field_name) { |
166 const OncFieldSignature* field_signature = | 170 const OncFieldSignature* field_signature = |
167 GetFieldSignature(*onc_signature_, onc_field_name); | 171 GetFieldSignature(*onc_signature_, onc_field_name); |
168 ShillToONCTranslator nested_translator(*shill_dictionary_, | 172 ShillToONCTranslator nested_translator(*shill_dictionary_, |
169 *field_signature->value_signature); | 173 *field_signature->value_signature); |
170 scoped_ptr<base::DictionaryValue> nested_object = | 174 scoped_ptr<base::DictionaryValue> nested_object = |
171 nested_translator.CreateTranslatedONCObject(); | 175 nested_translator.CreateTranslatedONCObject(); |
172 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); | 176 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); |
173 } | 177 } |
174 | 178 |
175 void ShillToONCTranslator::TranslateNetworkConfiguration() { | 179 void ShillToONCTranslator::TranslateNetworkConfiguration() { |
176 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable, kType); | 180 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable, |
181 network_config::kType); | |
177 CopyPropertiesAccordingToSignature(); | 182 CopyPropertiesAccordingToSignature(); |
178 | 183 |
179 std::string network_type; | 184 std::string network_type; |
180 if (onc_object_->GetStringWithoutPathExpansion(kType, &network_type)) | 185 if (onc_object_->GetStringWithoutPathExpansion(network_config::kType, |
186 &network_type)) | |
181 TranslateAndAddNestedObject(network_type); | 187 TranslateAndAddNestedObject(network_type); |
182 | 188 |
183 if (network_type == kVPN) { | 189 // Since Name is a read only field in Shill unless it's a VPN, it is copied |
184 std::string name; | 190 // here, but not when going the other direction (if it's not a VPN). |
185 shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kNameProperty, | 191 std::string name; |
186 &name); | 192 shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kNameProperty, |
187 onc_object_->SetStringWithoutPathExpansion(kName, name); | 193 &name); |
194 onc_object_->SetStringWithoutPathExpansion(network_config::kName, name); | |
195 | |
196 std::string state; | |
197 if (shill_dictionary_->GetString(flimflam::kStateProperty, &state)) { | |
pneubeck (no reviews)
2013/01/21 08:32:18
GetStringWithoutPathExpansion (I know that it's ug
Greg Spencer (Chromium)
2013/01/22 19:10:54
Done.
| |
198 std::string onc_state = status::kNotConnected; | |
199 if (NetworkState::StateIsConnected(state)) { | |
200 onc_state = status::kConnected; | |
201 } else if (NetworkState::StateIsConnecting(state)) { | |
202 onc_state = status::kConnecting; | |
203 } | |
204 onc_object_->SetString(network_config::kState, onc_state); | |
188 } | 205 } |
189 } | 206 } |
190 | 207 |
191 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { | 208 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { |
192 for (const OncFieldSignature* field_signature = onc_signature_->fields; | 209 for (const OncFieldSignature* field_signature = onc_signature_->fields; |
193 field_signature->onc_field_name != NULL; ++field_signature) { | 210 field_signature->onc_field_name != NULL; ++field_signature) { |
194 const base::Value* shill_value; | 211 const base::Value* shill_value; |
195 if (field_signature->shill_property_name == NULL || | 212 if (field_signature->shill_property_name == NULL || |
196 !shill_dictionary_->GetWithoutPathExpansion( | 213 !shill_dictionary_->GetWithoutPathExpansion( |
197 field_signature->shill_property_name, &shill_value)) { | 214 field_signature->shill_property_name, &shill_value)) { |
198 continue; | 215 continue; |
199 } | 216 } |
200 onc_object_->SetWithoutPathExpansion( | 217 onc_object_->SetWithoutPathExpansion( |
201 field_signature->onc_field_name, shill_value->DeepCopy()); | 218 field_signature->onc_field_name, shill_value->DeepCopy()); |
202 } | 219 } |
203 } | 220 } |
204 | 221 |
205 void ShillToONCTranslator::TranslateWithTableAndSet( | 222 void ShillToONCTranslator::TranslateWithTableAndSet( |
206 const std::string& shill_property_name, | 223 const std::string& shill_property_name, |
207 const StringTranslationEntry table[], | 224 const StringTranslationEntry table[], |
208 const std::string& onc_field_name) { | 225 const std::string& onc_field_name) { |
209 std::string shill_value; | 226 std::string shill_value; |
210 if (!shill_dictionary_->GetStringWithoutPathExpansion(shill_property_name, | 227 if (!shill_dictionary_->GetStringWithoutPathExpansion(shill_property_name, |
211 &shill_value)) { | 228 &shill_value)) { |
212 return; | 229 return; |
213 } | 230 } |
214 | 231 std::string onc_value; |
215 for (int i = 0; table[i].onc_value != NULL; ++i) { | 232 if (TranslateStringToONC(table, shill_value, &onc_value)) { |
216 if (shill_value != table[i].shill_value) | 233 onc_object_->SetStringWithoutPathExpansion(onc_field_name, onc_value); |
217 continue; | |
218 onc_object_->SetStringWithoutPathExpansion(onc_field_name, | |
219 table[i].onc_value); | |
220 return; | 234 return; |
221 } | 235 } |
222 LOG(ERROR) << "Shill property '" << shill_property_name << "' with value '" | 236 LOG(ERROR) << "Shill property '" << shill_property_name << "' with value '" |
223 << shill_value << "' couldn't be translated to ONC"; | 237 << shill_value << "' couldn't be translated to ONC"; |
224 } | 238 } |
225 | 239 |
226 } // namespace | 240 } // namespace |
227 | 241 |
228 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( | 242 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( |
229 const base::DictionaryValue& shill_dictionary, | 243 const base::DictionaryValue& shill_dictionary, |
230 const OncValueSignature* onc_signature) { | 244 const OncValueSignature* onc_signature) { |
231 CHECK(onc_signature != NULL); | 245 CHECK(onc_signature != NULL); |
232 | 246 |
233 ShillToONCTranslator translator(shill_dictionary, *onc_signature); | 247 ShillToONCTranslator translator(shill_dictionary, *onc_signature); |
234 return translator.CreateTranslatedONCObject(); | 248 return translator.CreateTranslatedONCObject(); |
235 } | 249 } |
236 | 250 |
237 } // namespace onc | 251 } // namespace onc |
238 } // namespace chromeos | 252 } // namespace chromeos |
OLD | NEW |