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 |
43 // recursive calls to TranslateShillServiceToONCPart, nested objects are | 45 // recursive calls to TranslateShillServiceToONCPart, nested objects are |
44 // translated. | 46 // translated. |
45 class ShillToONCTranslator { | 47 class ShillToONCTranslator { |
46 public: | 48 public: |
47 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, | 49 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, |
48 const OncValueSignature& onc_signature) | 50 const OncValueSignature& onc_signature) |
49 : shill_dictionary_(&shill_dictionary), | 51 : shill_dictionary_(&shill_dictionary), |
50 onc_signature_(&onc_signature) { | 52 onc_signature_(&onc_signature) { |
51 } | 53 } |
52 | 54 |
53 // Translates the associated Shill dictionary and creates an ONC object of the | 55 // Translates the associated Shill dictionary and creates an ONC object of the |
54 // given signature. | 56 // given signature. |
55 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject(); | 57 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject(); |
56 | 58 |
57 private: | 59 private: |
58 void TranslateOpenVPN(); | 60 void TranslateOpenVPN(); |
59 void TranslateVPN(); | 61 void TranslateVPN(); |
| 62 void TranslateWiFi(); |
60 void TranslateNetworkConfiguration(); | 63 void TranslateNetworkConfiguration(); |
61 | 64 |
62 // Creates an ONC object from |shill_dictionary| according to the signature | 65 // Creates an ONC object from |shill_dictionary| according to the signature |
63 // associated to |onc_field_name| and adds it to |onc_object_| at | 66 // associated to |onc_field_name| and adds it to |onc_object_| at |
64 // |onc_field_name|. | 67 // |onc_field_name|. |
65 void TranslateAndAddNestedObject(const std::string& onc_field_name); | 68 void TranslateAndAddNestedObject(const std::string& onc_field_name); |
66 | 69 |
67 // Copies all entries from |shill_dictionary_| to |onc_object_| for which a | 70 // Copies all entries from |shill_dictionary_| to |onc_object_| for which a |
68 // translation (shill_property_name) is defined by |onc_signature_|. | 71 // translation (shill_property_name) is defined by |onc_signature_|. |
69 void CopyPropertiesAccordingToSignature(); | 72 void CopyPropertiesAccordingToSignature(); |
70 | 73 |
71 // If existent, translates the entry at |shill_property_name| in | 74 // If existent, translates the entry at |shill_property_name| in |
72 // |shill_dictionary_| using |table|. It is an error if no matching table | 75 // |shill_dictionary_| using |table|. It is an error if no matching table |
73 // entry is found. Writes the result as entry at |onc_field_name| in | 76 // entry is found. Writes the result as entry at |onc_field_name| in |
74 // |onc_object_|. | 77 // |onc_object_|. |
75 void TranslateWithTableAndSet(const std::string& shill_property_name, | 78 void TranslateWithTableAndSet(const std::string& shill_property_name, |
76 const StringTranslationEntry table[], | 79 const StringTranslationEntry table[], |
77 const std::string& onc_field_name); | 80 const std::string& onc_field_name); |
78 | 81 |
79 const base::DictionaryValue* shill_dictionary_; | 82 const base::DictionaryValue* shill_dictionary_; |
80 const OncValueSignature* onc_signature_; | 83 const OncValueSignature* onc_signature_; |
81 scoped_ptr<base::DictionaryValue> onc_object_; | 84 scoped_ptr<base::DictionaryValue> onc_object_; |
82 | 85 |
83 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); | 86 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); |
84 }; | 87 }; |
85 | 88 |
86 scoped_ptr<base::DictionaryValue> | 89 scoped_ptr<base::DictionaryValue> |
87 ShillToONCTranslator::CreateTranslatedONCObject() { | 90 ShillToONCTranslator::CreateTranslatedONCObject() { |
88 onc_object_.reset(new base::DictionaryValue); | 91 onc_object_.reset(new base::DictionaryValue); |
89 if (onc_signature_ == &kNetworkConfigurationSignature) | 92 if (onc_signature_ == &kNetworkConfigurationSignature) { |
90 TranslateNetworkConfiguration(); | 93 TranslateNetworkConfiguration(); |
91 else if (onc_signature_ == &kVPNSignature) | 94 } else if (onc_signature_ == &kVPNSignature) { |
92 TranslateVPN(); | 95 TranslateVPN(); |
93 else if (onc_signature_ == &kOpenVPNSignature) | 96 } else if (onc_signature_ == &kOpenVPNSignature) { |
94 TranslateOpenVPN(); | 97 TranslateOpenVPN(); |
95 else | 98 } else if (onc_signature_ == &kWiFiSignature) { |
| 99 TranslateWiFi(); |
| 100 } else { |
96 CopyPropertiesAccordingToSignature(); | 101 CopyPropertiesAccordingToSignature(); |
| 102 } |
97 return onc_object_.Pass(); | 103 return onc_object_.Pass(); |
98 } | 104 } |
99 | 105 |
100 void ShillToONCTranslator::TranslateOpenVPN() { | 106 void ShillToONCTranslator::TranslateOpenVPN() { |
101 // Shill supports only one RemoteCertKU but ONC requires a list. If existing, | 107 // Shill supports only one RemoteCertKU but ONC requires a list. If existing, |
102 // wraps the value into a list. | 108 // wraps the value into a list. |
103 std::string certKU; | 109 std::string certKU; |
104 if (shill_dictionary_->GetStringWithoutPathExpansion( | 110 if (shill_dictionary_->GetStringWithoutPathExpansion( |
105 flimflam::kOpenVPNRemoteCertKUProperty, &certKU)) { | 111 flimflam::kOpenVPNRemoteCertKUProperty, &certKU)) { |
106 scoped_ptr<base::ListValue> certKUs(new base::ListValue); | 112 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 | 146 LOG(ERROR) << "Shill property '" << field_signature->shill_property_name |
141 << "' has value '" << shill_value | 147 << "' has value '" << shill_value |
142 << "', but expected a string"; | 148 << "', but expected a string"; |
143 } | 149 } |
144 onc_object_->SetWithoutPathExpansion(onc_field_name, translated.release()); | 150 onc_object_->SetWithoutPathExpansion(onc_field_name, translated.release()); |
145 } | 151 } |
146 } | 152 } |
147 | 153 |
148 void ShillToONCTranslator::TranslateVPN() { | 154 void ShillToONCTranslator::TranslateVPN() { |
149 TranslateWithTableAndSet(flimflam::kProviderTypeProperty, kVPNTypeTable, | 155 TranslateWithTableAndSet(flimflam::kProviderTypeProperty, kVPNTypeTable, |
150 kType); | 156 vpn::kType); |
151 CopyPropertiesAccordingToSignature(); | 157 CopyPropertiesAccordingToSignature(); |
152 | 158 |
153 std::string vpn_type; | 159 std::string vpn_type; |
154 if (onc_object_->GetStringWithoutPathExpansion(kType, &vpn_type)) { | 160 if (onc_object_->GetStringWithoutPathExpansion(vpn::kType, |
| 161 &vpn_type)) { |
155 if (vpn_type == vpn::kTypeL2TP_IPsec) { | 162 if (vpn_type == vpn::kTypeL2TP_IPsec) { |
156 TranslateAndAddNestedObject(vpn::kIPsec); | 163 TranslateAndAddNestedObject(vpn::kIPsec); |
157 TranslateAndAddNestedObject(vpn::kL2TP); | 164 TranslateAndAddNestedObject(vpn::kL2TP); |
158 } else { | 165 } else { |
159 TranslateAndAddNestedObject(vpn_type); | 166 TranslateAndAddNestedObject(vpn_type); |
160 } | 167 } |
161 } | 168 } |
162 } | 169 } |
163 | 170 |
| 171 void ShillToONCTranslator::TranslateWiFi() { |
| 172 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable, |
| 173 network_config::kType); |
| 174 CopyPropertiesAccordingToSignature(); |
| 175 |
| 176 std::string bssid; |
| 177 if (shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kWifiBSsid, |
| 178 &bssid)) { |
| 179 onc_object_->SetString(wifi::kBSSID, bssid); |
| 180 } |
| 181 } |
| 182 |
164 void ShillToONCTranslator::TranslateAndAddNestedObject( | 183 void ShillToONCTranslator::TranslateAndAddNestedObject( |
165 const std::string& onc_field_name) { | 184 const std::string& onc_field_name) { |
166 const OncFieldSignature* field_signature = | 185 const OncFieldSignature* field_signature = |
167 GetFieldSignature(*onc_signature_, onc_field_name); | 186 GetFieldSignature(*onc_signature_, onc_field_name); |
168 ShillToONCTranslator nested_translator(*shill_dictionary_, | 187 ShillToONCTranslator nested_translator(*shill_dictionary_, |
169 *field_signature->value_signature); | 188 *field_signature->value_signature); |
170 scoped_ptr<base::DictionaryValue> nested_object = | 189 scoped_ptr<base::DictionaryValue> nested_object = |
171 nested_translator.CreateTranslatedONCObject(); | 190 nested_translator.CreateTranslatedONCObject(); |
172 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); | 191 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); |
173 } | 192 } |
174 | 193 |
175 void ShillToONCTranslator::TranslateNetworkConfiguration() { | 194 void ShillToONCTranslator::TranslateNetworkConfiguration() { |
176 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable, kType); | 195 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable, |
| 196 network_config::kType); |
177 CopyPropertiesAccordingToSignature(); | 197 CopyPropertiesAccordingToSignature(); |
178 | 198 |
179 std::string network_type; | 199 std::string network_type; |
180 if (onc_object_->GetStringWithoutPathExpansion(kType, &network_type)) | 200 if (onc_object_->GetStringWithoutPathExpansion(network_config::kType, |
| 201 &network_type)) |
181 TranslateAndAddNestedObject(network_type); | 202 TranslateAndAddNestedObject(network_type); |
182 | 203 |
183 if (network_type == kVPN) { | 204 // Since Name is a read only field in Shill unless it's a VPN, it is copied |
184 std::string name; | 205 // here, but not when going the other direction (if it's not a VPN). |
185 shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kNameProperty, | 206 std::string name; |
186 &name); | 207 shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kNameProperty, |
187 onc_object_->SetStringWithoutPathExpansion(kName, name); | 208 &name); |
| 209 onc_object_->SetStringWithoutPathExpansion(network_config::kName, name); |
| 210 |
| 211 std::string state; |
| 212 if (shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kStateProperty, |
| 213 &state)) { |
| 214 std::string onc_state = connection_state::kNotConnected; |
| 215 if (NetworkState::StateIsConnected(state)) { |
| 216 onc_state = connection_state::kConnected; |
| 217 } else if (NetworkState::StateIsConnecting(state)) { |
| 218 onc_state = connection_state::kConnecting; |
| 219 } |
| 220 onc_object_->SetString(network_config::kConnectionState, onc_state); |
188 } | 221 } |
189 } | 222 } |
190 | 223 |
191 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { | 224 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { |
192 for (const OncFieldSignature* field_signature = onc_signature_->fields; | 225 for (const OncFieldSignature* field_signature = onc_signature_->fields; |
193 field_signature->onc_field_name != NULL; ++field_signature) { | 226 field_signature->onc_field_name != NULL; ++field_signature) { |
194 const base::Value* shill_value; | 227 const base::Value* shill_value; |
195 if (field_signature->shill_property_name == NULL || | 228 if (field_signature->shill_property_name == NULL || |
196 !shill_dictionary_->GetWithoutPathExpansion( | 229 !shill_dictionary_->GetWithoutPathExpansion( |
197 field_signature->shill_property_name, &shill_value)) { | 230 field_signature->shill_property_name, &shill_value)) { |
198 continue; | 231 continue; |
199 } | 232 } |
200 onc_object_->SetWithoutPathExpansion( | 233 onc_object_->SetWithoutPathExpansion( |
201 field_signature->onc_field_name, shill_value->DeepCopy()); | 234 field_signature->onc_field_name, shill_value->DeepCopy()); |
202 } | 235 } |
203 } | 236 } |
204 | 237 |
205 void ShillToONCTranslator::TranslateWithTableAndSet( | 238 void ShillToONCTranslator::TranslateWithTableAndSet( |
206 const std::string& shill_property_name, | 239 const std::string& shill_property_name, |
207 const StringTranslationEntry table[], | 240 const StringTranslationEntry table[], |
208 const std::string& onc_field_name) { | 241 const std::string& onc_field_name) { |
209 std::string shill_value; | 242 std::string shill_value; |
210 if (!shill_dictionary_->GetStringWithoutPathExpansion(shill_property_name, | 243 if (!shill_dictionary_->GetStringWithoutPathExpansion(shill_property_name, |
211 &shill_value)) { | 244 &shill_value)) { |
212 return; | 245 return; |
213 } | 246 } |
214 | 247 std::string onc_value; |
215 for (int i = 0; table[i].onc_value != NULL; ++i) { | 248 if (TranslateStringToONC(table, shill_value, &onc_value)) { |
216 if (shill_value != table[i].shill_value) | 249 onc_object_->SetStringWithoutPathExpansion(onc_field_name, onc_value); |
217 continue; | |
218 onc_object_->SetStringWithoutPathExpansion(onc_field_name, | |
219 table[i].onc_value); | |
220 return; | 250 return; |
221 } | 251 } |
222 LOG(ERROR) << "Shill property '" << shill_property_name << "' with value '" | 252 LOG(ERROR) << "Shill property '" << shill_property_name << "' with value '" |
223 << shill_value << "' couldn't be translated to ONC"; | 253 << shill_value << "' couldn't be translated to ONC"; |
224 } | 254 } |
225 | 255 |
226 } // namespace | 256 } // namespace |
227 | 257 |
228 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( | 258 scoped_ptr<base::DictionaryValue> TranslateShillServiceToONCPart( |
229 const base::DictionaryValue& shill_dictionary, | 259 const base::DictionaryValue& shill_dictionary, |
230 const OncValueSignature* onc_signature) { | 260 const OncValueSignature* onc_signature) { |
231 CHECK(onc_signature != NULL); | 261 CHECK(onc_signature != NULL); |
232 | 262 |
233 ShillToONCTranslator translator(shill_dictionary, *onc_signature); | 263 ShillToONCTranslator translator(shill_dictionary, *onc_signature); |
234 return translator.CreateTranslatedONCObject(); | 264 return translator.CreateTranslatedONCObject(); |
235 } | 265 } |
236 | 266 |
237 } // namespace onc | 267 } // namespace onc |
238 } // namespace chromeos | 268 } // namespace chromeos |
OLD | NEW |