Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Side by Side Diff: chromeos/network/onc/onc_translator_shill_to_onc.cc

Issue 12390017: Separating ONC<->Shill translation from the ONC signature. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed browser tests. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 // This class implements the translation of properties from the given 43 // This class implements the translation of properties from the given
44 // |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
45 // recursive calls to CreateTranslatedONCObject of new instances, nested objects 45 // recursive calls to CreateTranslatedONCObject of new instances, nested objects
46 // are translated. 46 // are translated.
47 class ShillToONCTranslator { 47 class ShillToONCTranslator {
48 public: 48 public:
49 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary, 49 ShillToONCTranslator(const base::DictionaryValue& shill_dictionary,
50 const OncValueSignature& onc_signature) 50 const OncValueSignature& onc_signature)
51 : shill_dictionary_(&shill_dictionary), 51 : shill_dictionary_(&shill_dictionary),
52 onc_signature_(&onc_signature) { 52 onc_signature_(&onc_signature) {
53 field_translation_table_ = GetFieldTranslationTable(onc_signature);
53 } 54 }
54 55
55 // Translates the associated Shill dictionary and creates an ONC object of the 56 // Translates the associated Shill dictionary and creates an ONC object of the
56 // given signature. 57 // given signature.
57 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject(); 58 scoped_ptr<base::DictionaryValue> CreateTranslatedONCObject();
58 59
59 private: 60 private:
60 void TranslateOpenVPN(); 61 void TranslateOpenVPN();
61 void TranslateVPN(); 62 void TranslateVPN();
62 void TranslateWiFi(); 63 void TranslateWiFiWithState();
63 void TranslateNetworkConfiguration(); 64 void TranslateNetworkWithState();
64 65
65 // Creates an ONC object from |shill_dictionary| according to the signature 66 // Creates an ONC object from |shill_dictionary| according to the signature
66 // associated to |onc_field_name| and adds it to |onc_object_| at 67 // associated to |onc_field_name| and adds it to |onc_object_| at
67 // |onc_field_name|. 68 // |onc_field_name|.
68 void TranslateAndAddNestedObject(const std::string& onc_field_name); 69 void TranslateAndAddNestedObject(const std::string& onc_field_name);
69 70
70 // Applies function CopyProperty to each field of |onc_object_|. 71 // Applies function CopyProperty to each field of |value_signature| and its
72 // base signatures.
73 void CopyPropertiesAccordingToSignature(
74 const OncValueSignature* value_signature);
75
76 // Applies function CopyProperty to each field of |onc_signature_| and its
77 // base signatures.
71 void CopyPropertiesAccordingToSignature(); 78 void CopyPropertiesAccordingToSignature();
72 79
73 // If |shill_property_name| is defined in |field_signature|, copies this 80 // If |shill_property_name| is defined in |field_signature|, copies this
74 // entry from |shill_dictionary_| to |onc_object_| if it exists. 81 // entry from |shill_dictionary_| to |onc_object_| if it exists.
75 void CopyProperty(const OncFieldSignature* field_signature); 82 void CopyProperty(const OncFieldSignature* field_signature);
76 83
77 // If existent, translates the entry at |shill_property_name| in 84 // If existent, translates the entry at |shill_property_name| in
78 // |shill_dictionary_| using |table|. It is an error if no matching table 85 // |shill_dictionary_| using |table|. It is an error if no matching table
79 // entry is found. Writes the result as entry at |onc_field_name| in 86 // entry is found. Writes the result as entry at |onc_field_name| in
80 // |onc_object_|. 87 // |onc_object_|.
81 void TranslateWithTableAndSet(const std::string& shill_property_name, 88 void TranslateWithTableAndSet(const std::string& shill_property_name,
82 const StringTranslationEntry table[], 89 const StringTranslationEntry table[],
83 const std::string& onc_field_name); 90 const std::string& onc_field_name);
84 91
85 const base::DictionaryValue* shill_dictionary_; 92 const base::DictionaryValue* shill_dictionary_;
86 const OncValueSignature* onc_signature_; 93 const OncValueSignature* onc_signature_;
94 const FieldTranslationEntry* field_translation_table_;
87 scoped_ptr<base::DictionaryValue> onc_object_; 95 scoped_ptr<base::DictionaryValue> onc_object_;
88 96
89 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator); 97 DISALLOW_COPY_AND_ASSIGN(ShillToONCTranslator);
90 }; 98 };
91 99
92 scoped_ptr<base::DictionaryValue> 100 scoped_ptr<base::DictionaryValue>
93 ShillToONCTranslator::CreateTranslatedONCObject() { 101 ShillToONCTranslator::CreateTranslatedONCObject() {
94 onc_object_.reset(new base::DictionaryValue); 102 onc_object_.reset(new base::DictionaryValue);
95 if (onc_signature_ == &kNetworkConfigurationSignature) { 103 if (onc_signature_ == &kNetworkWithStateSignature) {
96 TranslateNetworkConfiguration(); 104 TranslateNetworkWithState();
97 } else if (onc_signature_ == &kVPNSignature) { 105 } else if (onc_signature_ == &kVPNSignature) {
98 TranslateVPN(); 106 TranslateVPN();
99 } else if (onc_signature_ == &kOpenVPNSignature) { 107 } else if (onc_signature_ == &kOpenVPNSignature) {
100 TranslateOpenVPN(); 108 TranslateOpenVPN();
101 } else if (onc_signature_ == &kWiFiSignature) { 109 } else if (onc_signature_ == &kWiFiWithStateSignature) {
102 TranslateWiFi(); 110 TranslateWiFiWithState();
103 } else { 111 } else {
104 CopyPropertiesAccordingToSignature(); 112 CopyPropertiesAccordingToSignature();
105 } 113 }
106 return onc_object_.Pass(); 114 return onc_object_.Pass();
107 } 115 }
108 116
109 void ShillToONCTranslator::TranslateOpenVPN() { 117 void ShillToONCTranslator::TranslateOpenVPN() {
110 // Shill supports only one RemoteCertKU but ONC requires a list. If existing, 118 // Shill supports only one RemoteCertKU but ONC requires a list. If existing,
111 // wraps the value into a list. 119 // wraps the value into a list.
112 std::string certKU; 120 std::string certKU;
113 if (shill_dictionary_->GetStringWithoutPathExpansion( 121 if (shill_dictionary_->GetStringWithoutPathExpansion(
114 flimflam::kOpenVPNRemoteCertKUProperty, &certKU)) { 122 flimflam::kOpenVPNRemoteCertKUProperty, &certKU)) {
115 scoped_ptr<base::ListValue> certKUs(new base::ListValue); 123 scoped_ptr<base::ListValue> certKUs(new base::ListValue);
116 certKUs->AppendString(certKU); 124 certKUs->AppendString(certKU);
117 onc_object_->SetWithoutPathExpansion(vpn::kRemoteCertKU, certKUs.release()); 125 onc_object_->SetWithoutPathExpansion(vpn::kRemoteCertKU, certKUs.release());
118 } 126 }
119 127
120 for (const OncFieldSignature* field_signature = onc_signature_->fields; 128 for (const OncFieldSignature* field_signature = onc_signature_->fields;
121 field_signature->onc_field_name != NULL; ++field_signature) { 129 field_signature->onc_field_name != NULL; ++field_signature) {
122 const std::string& onc_field_name = field_signature->onc_field_name; 130 const std::string& onc_field_name = field_signature->onc_field_name;
123 if (onc_field_name == vpn::kSaveCredentials || 131 if (onc_field_name == vpn::kSaveCredentials ||
124 onc_field_name == vpn::kRemoteCertKU) { 132 onc_field_name == vpn::kRemoteCertKU) {
125 CopyProperty(field_signature); 133 CopyProperty(field_signature);
126 continue; 134 continue;
127 } 135 }
128 136
137 std::string shill_property_name;
129 const base::Value* shill_value = NULL; 138 const base::Value* shill_value = NULL;
130 if (field_signature->shill_property_name == NULL || 139 if (!field_translation_table_ ||
131 !shill_dictionary_->GetWithoutPathExpansion( 140 !GetShillPropertyName(field_signature->onc_field_name,
132 field_signature->shill_property_name, &shill_value)) { 141 field_translation_table_,
142 &shill_property_name) ||
143 !shill_dictionary_->GetWithoutPathExpansion(shill_property_name,
144 &shill_value)) {
133 continue; 145 continue;
134 } 146 }
135 147
136 scoped_ptr<base::Value> translated; 148 scoped_ptr<base::Value> translated;
137 std::string shill_str; 149 std::string shill_str;
138 if (shill_value->GetAsString(&shill_str)) { 150 if (shill_value->GetAsString(&shill_str)) {
139 // Shill wants all Provider/VPN fields to be strings. Translates these 151 // Shill wants all Provider/VPN fields to be strings. Translates these
140 // strings back to the correct ONC type. 152 // strings back to the correct ONC type.
141 translated = ConvertStringToValue( 153 translated = ConvertStringToValue(
142 shill_str, 154 shill_str,
143 field_signature->value_signature->onc_type); 155 field_signature->value_signature->onc_type);
144 156
145 if (translated.get() == NULL) { 157 if (translated.get() == NULL) {
146 LOG(ERROR) << "Shill property '" << field_signature->shill_property_name 158 LOG(ERROR) << "Shill property '" << shill_property_name
147 << "' with value " << *shill_value 159 << "' with value " << *shill_value
148 << " couldn't be converted to base::Value::Type " 160 << " couldn't be converted to base::Value::Type "
149 << field_signature->value_signature->onc_type; 161 << field_signature->value_signature->onc_type;
150 } else { 162 } else {
151 onc_object_->SetWithoutPathExpansion(onc_field_name, 163 onc_object_->SetWithoutPathExpansion(onc_field_name,
152 translated.release()); 164 translated.release());
153 } 165 }
154 } else { 166 } else {
155 LOG(ERROR) << "Shill property '" << field_signature->shill_property_name 167 LOG(ERROR) << "Shill property '" << shill_property_name
156 << "' has value " << *shill_value 168 << "' has value " << *shill_value
157 << ", but expected a string"; 169 << ", but expected a string";
158 } 170 }
159 } 171 }
160 } 172 }
161 173
162 void ShillToONCTranslator::TranslateVPN() { 174 void ShillToONCTranslator::TranslateVPN() {
163 TranslateWithTableAndSet(flimflam::kProviderTypeProperty, kVPNTypeTable, 175 TranslateWithTableAndSet(flimflam::kProviderTypeProperty, kVPNTypeTable,
164 vpn::kType); 176 vpn::kType);
165 CopyPropertiesAccordingToSignature(); 177 CopyPropertiesAccordingToSignature();
166 178
167 std::string vpn_type; 179 std::string vpn_type;
168 if (onc_object_->GetStringWithoutPathExpansion(vpn::kType, 180 if (onc_object_->GetStringWithoutPathExpansion(vpn::kType,
169 &vpn_type)) { 181 &vpn_type)) {
170 if (vpn_type == vpn::kTypeL2TP_IPsec) { 182 if (vpn_type == vpn::kTypeL2TP_IPsec) {
171 TranslateAndAddNestedObject(vpn::kIPsec); 183 TranslateAndAddNestedObject(vpn::kIPsec);
172 TranslateAndAddNestedObject(vpn::kL2TP); 184 TranslateAndAddNestedObject(vpn::kL2TP);
173 } else { 185 } else {
174 TranslateAndAddNestedObject(vpn_type); 186 TranslateAndAddNestedObject(vpn_type);
175 } 187 }
176 } 188 }
177 } 189 }
178 190
179 void ShillToONCTranslator::TranslateWiFi() { 191 void ShillToONCTranslator::TranslateWiFiWithState() {
180 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable, 192 TranslateWithTableAndSet(flimflam::kSecurityProperty, kWiFiSecurityTable,
181 network_config::kType); 193 wifi::kSecurity);
182 CopyPropertiesAccordingToSignature(); 194 CopyPropertiesAccordingToSignature();
183
184 std::string bssid;
185 if (shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kWifiBSsid,
186 &bssid)) {
187 onc_object_->SetStringWithoutPathExpansion(wifi::kBSSID, bssid);
188 }
189 } 195 }
190 196
191 void ShillToONCTranslator::TranslateAndAddNestedObject( 197 void ShillToONCTranslator::TranslateAndAddNestedObject(
192 const std::string& onc_field_name) { 198 const std::string& onc_field_name) {
193 const OncFieldSignature* field_signature = 199 const OncFieldSignature* field_signature =
194 GetFieldSignature(*onc_signature_, onc_field_name); 200 GetFieldSignature(*onc_signature_, onc_field_name);
195 ShillToONCTranslator nested_translator(*shill_dictionary_, 201 ShillToONCTranslator nested_translator(*shill_dictionary_,
196 *field_signature->value_signature); 202 *field_signature->value_signature);
197 scoped_ptr<base::DictionaryValue> nested_object = 203 scoped_ptr<base::DictionaryValue> nested_object =
198 nested_translator.CreateTranslatedONCObject(); 204 nested_translator.CreateTranslatedONCObject();
205 if (nested_object->empty())
206 return;
199 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release()); 207 onc_object_->SetWithoutPathExpansion(onc_field_name, nested_object.release());
200 } 208 }
201 209
202 void ShillToONCTranslator::TranslateNetworkConfiguration() { 210 void ShillToONCTranslator::TranslateNetworkWithState() {
203 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable, 211 TranslateWithTableAndSet(flimflam::kTypeProperty, kNetworkTypeTable,
204 network_config::kType); 212 network_config::kType);
205 CopyPropertiesAccordingToSignature(); 213 CopyPropertiesAccordingToSignature();
206 214
207 std::string network_type; 215 std::string network_type;
208 if (onc_object_->GetStringWithoutPathExpansion(network_config::kType, 216 if (onc_object_->GetStringWithoutPathExpansion(network_config::kType,
209 &network_type)) 217 &network_type)) {
210 TranslateAndAddNestedObject(network_type); 218 TranslateAndAddNestedObject(network_type);
219 }
211 220
212 // Since Name is a read only field in Shill unless it's a VPN, it is copied 221 // Since Name is a read only field in Shill unless it's a VPN, it is copied
213 // here, but not when going the other direction (if it's not a VPN). 222 // here, but not when going the other direction (if it's not a VPN).
214 std::string name; 223 std::string name;
215 shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kNameProperty, 224 shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kNameProperty,
216 &name); 225 &name);
217 onc_object_->SetStringWithoutPathExpansion(network_config::kName, name); 226 onc_object_->SetStringWithoutPathExpansion(network_config::kName, name);
218 227
219 std::string state; 228 std::string state;
220 if (shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kStateProperty, 229 if (shill_dictionary_->GetStringWithoutPathExpansion(flimflam::kStateProperty,
221 &state)) { 230 &state)) {
222 std::string onc_state = connection_state::kNotConnected; 231 std::string onc_state = connection_state::kNotConnected;
223 if (NetworkState::StateIsConnected(state)) { 232 if (NetworkState::StateIsConnected(state)) {
224 onc_state = connection_state::kConnected; 233 onc_state = connection_state::kConnected;
225 } else if (NetworkState::StateIsConnecting(state)) { 234 } else if (NetworkState::StateIsConnecting(state)) {
226 onc_state = connection_state::kConnecting; 235 onc_state = connection_state::kConnecting;
227 } 236 }
228 onc_object_->SetStringWithoutPathExpansion(network_config::kConnectionState, 237 onc_object_->SetStringWithoutPathExpansion(network_config::kConnectionState,
229 onc_state); 238 onc_state);
230 } 239 }
231 } 240 }
232 241
233 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() { 242 void ShillToONCTranslator::CopyPropertiesAccordingToSignature() {
234 for (const OncFieldSignature* field_signature = onc_signature_->fields; 243 CopyPropertiesAccordingToSignature(onc_signature_);
244 }
245
246 void ShillToONCTranslator::CopyPropertiesAccordingToSignature(
247 const OncValueSignature* value_signature) {
248 if (value_signature->base_signature)
249 CopyPropertiesAccordingToSignature(value_signature->base_signature);
250 for (const OncFieldSignature* field_signature = value_signature->fields;
235 field_signature->onc_field_name != NULL; ++field_signature) { 251 field_signature->onc_field_name != NULL; ++field_signature) {
236 CopyProperty(field_signature); 252 CopyProperty(field_signature);
237 } 253 }
238 } 254 }
239 255
240 void ShillToONCTranslator::CopyProperty( 256 void ShillToONCTranslator::CopyProperty(
241 const OncFieldSignature* field_signature) { 257 const OncFieldSignature* field_signature) {
258 std::string shill_property_name;
242 const base::Value* shill_value = NULL; 259 const base::Value* shill_value = NULL;
243 if (field_signature->shill_property_name == NULL || 260 if (!field_translation_table_ ||
244 !shill_dictionary_->GetWithoutPathExpansion( 261 !GetShillPropertyName(field_signature->onc_field_name,
245 field_signature->shill_property_name, 262 field_translation_table_,
246 &shill_value)) { 263 &shill_property_name) ||
264 !shill_dictionary_->GetWithoutPathExpansion(shill_property_name,
265 &shill_value)) {
247 return; 266 return;
248 } 267 }
249 268
250 if (shill_value->GetType() != field_signature->value_signature->onc_type) { 269 if (shill_value->GetType() != field_signature->value_signature->onc_type) {
251 LOG(ERROR) << "Shill property '" << field_signature->shill_property_name 270 LOG(ERROR) << "Shill property '" << shill_property_name
252 << "' with value " << *shill_value 271 << "' with value " << *shill_value
253 << " has base::Value::Type " << shill_value->GetType() 272 << " has base::Value::Type " << shill_value->GetType()
254 << " but ONC field '" << field_signature->onc_field_name 273 << " but ONC field '" << field_signature->onc_field_name
255 << "' requires type " 274 << "' requires type "
256 << field_signature->value_signature->onc_type << "."; 275 << field_signature->value_signature->onc_type << ".";
257 return; 276 return;
258 } 277 }
259 278
260 onc_object_->SetWithoutPathExpansion(field_signature->onc_field_name, 279 onc_object_->SetWithoutPathExpansion(field_signature->onc_field_name,
261 shill_value->DeepCopy()); 280 shill_value->DeepCopy());
(...skipping 23 matching lines...) Expand all
285 const base::DictionaryValue& shill_dictionary, 304 const base::DictionaryValue& shill_dictionary,
286 const OncValueSignature* onc_signature) { 305 const OncValueSignature* onc_signature) {
287 CHECK(onc_signature != NULL); 306 CHECK(onc_signature != NULL);
288 307
289 ShillToONCTranslator translator(shill_dictionary, *onc_signature); 308 ShillToONCTranslator translator(shill_dictionary, *onc_signature);
290 return translator.CreateTranslatedONCObject(); 309 return translator.CreateTranslatedONCObject();
291 } 310 }
292 311
293 } // namespace onc 312 } // namespace onc
294 } // namespace chromeos 313 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/onc/onc_translator_onc_to_shill.cc ('k') | chromeos/network/onc/onc_translator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698