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 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
79 const base::DictionaryValue* shill_dictionary_; | 80 const base::DictionaryValue* shill_dictionary_; |
80 const OncValueSignature* onc_signature_; | 81 const OncValueSignature* onc_signature_; |
81 scoped_ptr<base::DictionaryValue> onc_object_; | 82 scoped_ptr<base::DictionaryValue> onc_object_; |
82 | 83 |
83 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); | 84 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); |
84 }; | 85 }; |
85 | 86 |
86 scoped_ptr<base::DictionaryValue> | 87 scoped_ptr<base::DictionaryValue> |
87 ShillToONCTranslator::CreateTranslatedONCObject() { | 88 ShillToONCTranslator::CreateTranslatedONCObject() { |
88 onc_object_.reset(new base::DictionaryValue); | 89 onc_object_.reset(new base::DictionaryValue); |
89 if (onc_signature_ == &kNetworkConfigurationSignature) | 90 if (onc_signature_ == &kNetworkConfigurationSignature) |
pneubeck (no reviews)
2013/01/18 10:27:41
to make it consistent with further down, add brace
Greg Spencer (Chromium)
2013/01/18 22:27:44
Done.
| |
90 TranslateNetworkConfiguration(); | 91 TranslateNetworkConfiguration(); |
91 else if (onc_signature_ == &kVPNSignature) | 92 else if (onc_signature_ == &kVPNSignature) |
92 TranslateVPN(); | 93 TranslateVPN(); |
93 else if (onc_signature_ == &kOpenVPNSignature) | 94 else if (onc_signature_ == &kOpenVPNSignature) |
94 TranslateOpenVPN(); | 95 TranslateOpenVPN(); |
95 else | 96 else |
96 CopyPropertiesAccordingToSignature(); | 97 CopyPropertiesAccordingToSignature(); |
97 return onc_object_.Pass(); | 98 return onc_object_.Pass(); |
98 } | 99 } |
99 | 100 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
140 LOG(ERROR) << "Shill property '" << field_signature->shill_property_name | 141 LOG(ERROR) << "Shill property '" << field_signature->shill_property_name |
141 << "' has value '" << shill_value | 142 << "' has value '" << shill_value |
142 << "', but expected a string"; | 143 << "', but expected a string"; |
143 } | 144 } |
144 onc_object_->SetWithoutPathExpansion(onc_field_name, translated.release()); | 145 onc_object_->SetWithoutPathExpansion(onc_field_name, translated.release()); |
145 } | 146 } |
146 } | 147 } |
147 | 148 |
148 void ShillToONCTranslator::TranslateVPN() { | 149 void ShillToONCTranslator::TranslateVPN() { |
149 TranslateWithTableAndSet(flimflam::kProviderTypeProperty, kVPNTypeTable, | 150 TranslateWithTableAndSet(flimflam::kProviderTypeProperty, kVPNTypeTable, |
150 kType); | 151 network_config::kType); |
pneubeck (no reviews)
2013/01/18 10:27:41
vpn::kType
Greg Spencer (Chromium)
2013/01/18 22:27:44
Done.
| |
151 CopyPropertiesAccordingToSignature(); | 152 CopyPropertiesAccordingToSignature(); |
152 | 153 |
153 std::string vpn_type; | 154 std::string vpn_type; |
154 if (onc_object_->GetStringWithoutPathExpansion(kType, &vpn_type)) { | 155 if (onc_object_->GetStringWithoutPathExpansion(network_config::kType, |
pneubeck (no reviews)
2013/01/18 10:27:41
vpn::kType
Greg Spencer (Chromium)
2013/01/18 22:27:44
Done.
| |
156 &vpn_type)) { | |
155 if (vpn_type == vpn::kTypeL2TP_IPsec) { | 157 if (vpn_type == vpn::kTypeL2TP_IPsec) { |
156 TranslateAndAddNestedObject(vpn::kIPsec); | 158 TranslateAndAddNestedObject(vpn::kIPsec); |
157 TranslateAndAddNestedObject(vpn::kL2TP); | 159 TranslateAndAddNestedObject(vpn::kL2TP); |
158 } else { | 160 } else { |
159 TranslateAndAddNestedObject(vpn_type); | 161 TranslateAndAddNestedObject(vpn_type); |
160 } | 162 } |
161 } | 163 } |
162 } | 164 } |
163 | 165 |
164 void ShillToONCTranslator::TranslateAndAddNestedObject( | 166 void ShillToONCTranslator::TranslateAndAddNestedObject( |
165 const std::string& onc_field_name) { | 167 const std::string& onc_field_name) { |
166 const OncFieldSignature* field_signature = | 168 const OncFieldSignature* field_signature = |
167 GetFieldSignature(*onc_signature_, onc_field_name); | 169 GetFieldSignature(*onc_signature_, onc_field_name); |
168 ShillToONCTranslator nested_translator(*shill_dictionary_, | 170 ShillToONCTranslator nested_translator(*shill_dictionary_, |
169 *field_signature->value_signature); | 171 *field_signature->value_signature); |
170 scoped_ptr<base::DictionaryValue> nested_object = | 172 scoped_ptr<base::DictionaryValue> nested_object = |
171 nested_translator.CreateTranslatedONCObject(); | 173 nested_translator.CreateTranslatedONCObject(); |
172 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); | 174 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); |
173 } | 175 } |
174 | 176 |
175 void ShillToONCTranslator::TranslateNetworkConfiguration() { | 177 void ShillToONCTranslator::TranslateNetworkConfiguration() { |
176 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable, kType); | 178 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable, |
179 network_config::kType); | |
177 CopyPropertiesAccordingToSignature(); | 180 CopyPropertiesAccordingToSignature(); |
178 | 181 |
179 std::string network_type; | 182 std::string network_type; |
180 if (onc_object_->GetStringWithoutPathExpansion(kType, &network_type)) | 183 if (onc_object_->GetStringWithoutPathExpansion(network_config::kType, |
184 &network_type)) | |
181 TranslateAndAddNestedObject(network_type); | 185 TranslateAndAddNestedObject(network_type); |
182 | 186 |
183 if (network_type == kVPN) { | 187 // Since Name is a read only field in Shill unless it's a VPN, is is copied |
pneubeck (no reviews)
2013/01/18 10:27:41
is is -> it is
Greg Spencer (Chromium)
2013/01/18 22:27:44
Done.
| |
184 std::string name; | 188 // here, but not when going the other direction (if it's not a VPN). |
185 shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kNameProperty, | 189 std::string name; |
186 &name); | 190 shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kNameProperty, |
187 onc_object_->SetStringWithoutPathExpansion(kName, name); | 191 &name); |
192 onc_object_->SetStringWithoutPathExpansion(network_config::kName, name); | |
193 | |
194 std::string state; | |
195 if (shill_dictionary_->GetString(flimflam::kStateProperty, &state)) { | |
196 if (NetworkState::StateIsConnected(state)) { | |
197 onc_object_->SetString(network_config::kState, status::kConnected); | |
pneubeck (no reviews)
2013/01/18 10:27:41
better use a helper variable shill_state and set o
Greg Spencer (Chromium)
2013/01/18 22:27:44
Done.
| |
198 } else if (NetworkState::StateIsConnecting(state)) { | |
199 onc_object_->SetString(network_config::kState, status::kConnecting); | |
200 } else { | |
201 onc_object_->SetString(network_config::kState, status::kNotConnected); | |
202 } | |
188 } | 203 } |
189 } | 204 } |
190 | 205 |
191 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { | 206 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { |
192 for (const OncFieldSignature* field_signature = onc_signature_->fields; | 207 for (const OncFieldSignature* field_signature = onc_signature_->fields; |
193 field_signature->onc_field_name != NULL; ++field_signature) { | 208 field_signature->onc_field_name != NULL; ++field_signature) { |
194 const base::Value* shill_value; | 209 const base::Value* shill_value; |
195 if (field_signature->shill_property_name == NULL || | 210 if (field_signature->shill_property_name == NULL || |
196 !shill_dictionary_->GetWithoutPathExpansion( | 211 !shill_dictionary_->GetWithoutPathExpansion( |
197 field_signature->shill_property_name, &shill_value)) { | 212 field_signature->shill_property_name, &shill_value)) { |
198 continue; | 213 continue; |
199 } | 214 } |
200 onc_object_->SetWithoutPathExpansion( | 215 onc_object_->SetWithoutPathExpansion( |
201 field_signature->onc_field_name, shill_value->DeepCopy()); | 216 field_signature->onc_field_name, shill_value->DeepCopy()); |
202 } | 217 } |
203 } | 218 } |
204 | 219 |
205 void ShillToONCTranslator::TranslateWithTableAndSet( | 220 void ShillToONCTranslator::TranslateWithTableAndSet( |
206 const std::string& shill_property_name, | 221 const std::string& shill_property_name, |
207 const StringTranslationEntry table[], | 222 const StringTranslationEntry table[], |
208 const std::string& onc_field_name) { | 223 const std::string& onc_field_name) { |
209 std::string shill_value; | 224 std::string shill_value; |
210 if (!shill_dictionary_->GetStringWithoutPathExpansion(shill_property_name, | 225 if (!shill_dictionary_->GetStringWithoutPathExpansion(shill_property_name, |
211 &shill_value)) { | 226 &shill_value)) { |
212 return; | 227 return; |
213 } | 228 } |
214 | 229 std::string onc_value; |
215 for (int i = 0; table[i].onc_value != NULL; ++i) { | 230 if (TranslateStringToONC(table, shill_value, &onc_value)) { |
216 if (shill_value != table[i].shill_value) | 231 onc_object_->SetStringWithoutPathExpansion(onc_field_name, onc_value); |
217 continue; | |
218 onc_object_->SetStringWithoutPathExpansion(onc_field_name, | |
219 table[i].onc_value); | |
220 return; | 232 return; |
221 } | 233 } |
222 LOG(ERROR) << "Shill property '" << shill_property_name << "' with value '" | 234 LOG(ERROR) << "Shill property '" << shill_property_name << "' with value '" |
223 << shill_value << "' couldn't be translated to ONC"; | 235 << shill_value << "' couldn't be translated to ONC"; |
224 } | 236 } |
225 | 237 |
226 } // namespace | 238 } // namespace |
227 | 239 |
228 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( | 240 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( |
229 const base::DictionaryValue& shill_dictionary, | 241 const base::DictionaryValue& shill_dictionary, |
230 const OncValueSignature* onc_signature) { | 242 const OncValueSignature* onc_signature) { |
231 CHECK(onc_signature != NULL); | 243 CHECK(onc_signature != NULL); |
232 | 244 |
233 ShillToONCTranslator translator(shill_dictionary, *onc_signature); | 245 ShillToONCTranslator translator(shill_dictionary, *onc_signature); |
234 return translator.CreateTranslatedONCObject(); | 246 return translator.CreateTranslatedONCObject(); |
235 } | 247 } |
236 | 248 |
237 } // namespace onc | 249 } // namespace onc |
238 } // namespace chromeos | 250 } // namespace chromeos |
OLD | NEW |