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

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

Issue 11664005: Extending the translation from ONC to Shill. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Rebased. Created 7 years, 11 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
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 // The implementation of TranslateONCObjectToShill is structured in two parts: 5 // The implementation of TranslateONCObjectToShill is structured in two parts:
6 // - The recursion through the existing ONC hierarchy 6 // - The recursion through the existing ONC hierarchy
7 // see TranslateONCHierarchy 7 // see TranslateONCHierarchy
8 // - The local translation of an object depending on the associated signature 8 // - The local translation of an object depending on the associated signature
9 // see LocalTranslator::TranslateFields 9 // see LocalTranslator::TranslateFields
10 10
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 : onc_signature_(&onc_signature), 46 : onc_signature_(&onc_signature),
47 onc_object_(&onc_object), 47 onc_object_(&onc_object),
48 shill_dictionary_(shill_dictionary) { 48 shill_dictionary_(shill_dictionary) {
49 } 49 }
50 50
51 void TranslateFields(); 51 void TranslateFields();
52 52
53 private: 53 private:
54 void TranslateOpenVPN(); 54 void TranslateOpenVPN();
55 void TranslateVPN(); 55 void TranslateVPN();
56 void TranslateWiFi();
57 void TranslateEAP();
56 void TranslateNetworkConfiguration(); 58 void TranslateNetworkConfiguration();
57 59
58 // Copies all entries from |onc_object_| to |shill_dictionary_| for which a 60 // Copies all entries from |onc_object_| to |shill_dictionary_| for which a
59 // translation (shill_property_name) is defined by |onc_signature_|. 61 // translation (shill_property_name) is defined by |onc_signature_|.
60 void CopyFieldsAccordingToSignature(); 62 void CopyFieldsAccordingToSignature();
61 63
62 // Adds |value| to |shill_dictionary| at the field shill_property_name given 64 // Adds |value| to |shill_dictionary| at the field shill_property_name given
63 // by the associated signature. Takes ownership of |value|. Does nothing if 65 // by the associated signature. Takes ownership of |value|. Does nothing if
64 // |value| is NULL or the property name cannot be read from the signature. 66 // |value| is NULL or the property name cannot be read from the signature.
65 void AddValueAccordingToSignature(const std::string& onc_field_name, 67 void AddValueAccordingToSignature(const std::string& onc_field_name,
(...skipping 13 matching lines...) Expand all
79 DISALLOW_COPY_AND_ASSIGN(LocalTranslator); 81 DISALLOW_COPY_AND_ASSIGN(LocalTranslator);
80 }; 82 };
81 83
82 void LocalTranslator::TranslateFields() { 84 void LocalTranslator::TranslateFields() {
83 if (onc_signature_ == &kNetworkConfigurationSignature) 85 if (onc_signature_ == &kNetworkConfigurationSignature)
84 TranslateNetworkConfiguration(); 86 TranslateNetworkConfiguration();
85 else if (onc_signature_ == &kVPNSignature) 87 else if (onc_signature_ == &kVPNSignature)
86 TranslateVPN(); 88 TranslateVPN();
87 else if (onc_signature_ == &kOpenVPNSignature) 89 else if (onc_signature_ == &kOpenVPNSignature)
88 TranslateOpenVPN(); 90 TranslateOpenVPN();
91 else if (onc_signature_ == &kWiFiSignature)
92 TranslateWiFi();
93 else if (onc_signature_ == &kEAPSignature)
94 TranslateEAP();
89 else 95 else
90 CopyFieldsAccordingToSignature(); 96 CopyFieldsAccordingToSignature();
91 } 97 }
92 98
93 void LocalTranslator::TranslateOpenVPN() { 99 void LocalTranslator::TranslateOpenVPN() {
94 // Shill supports only one RemoteCertKU but ONC a list. 100 // Shill supports only one RemoteCertKU but ONC a list.
95 // Copy only the first entry if existing. 101 // Copy only the first entry if existing.
96 const base::ListValue* certKUs; 102 const base::ListValue* certKUs;
97 std::string certKU; 103 std::string certKU;
98 if (onc_object_->GetListWithoutPathExpansion(vpn::kRemoteCertKU, &certKUs) && 104 if (onc_object_->GetListWithoutPathExpansion(vpn::kRemoteCertKU, &certKUs) &&
99 certKUs->GetString(0, &certKU)) { 105 certKUs->GetString(0, &certKU)) {
100 shill_dictionary_->SetStringWithoutPathExpansion( 106 shill_dictionary_->SetStringWithoutPathExpansion(
101 flimflam::kOpenVPNRemoteCertKUProperty, certKU); 107 flimflam::kOpenVPNRemoteCertKUProperty, certKU);
102 } 108 }
103 109
104 for (base::DictionaryValue::Iterator it(*onc_object_); it.HasNext(); 110 for (base::DictionaryValue::Iterator it(*onc_object_); it.HasNext();
105 it.Advance()) { 111 it.Advance()) {
106 scoped_ptr<base::Value> translated; 112 scoped_ptr<base::Value> translated;
107 if (it.key() == vpn::kSaveCredentials || it.key() == vpn::kRemoteCertKU) { 113 if (it.key() == vpn::kSaveCredentials || it.key() == vpn::kRemoteCertKU) {
108 translated.reset(it.value().DeepCopy()); 114 translated.reset(it.value().DeepCopy());
109 } else { 115 } else {
110 // Shill wants all Provider/VPN fields to be strings. 116 // Shill wants all Provider/VPN fields to be strings.
111 translated = ConvertValueToString(it.value()); 117 translated = ConvertValueToString(it.value());
112 } 118 }
113 AddValueAccordingToSignature(it.key(), translated.Pass()); 119 AddValueAccordingToSignature(it.key(), translated.Pass());
114 } 120 }
115 } 121 }
116 122
117 void LocalTranslator::TranslateVPN() { 123 void LocalTranslator::TranslateVPN() {
118 TranslateWithTableAndSet(kType, kVPNTypeTable, 124 std::string type;
125 onc_object_->GetStringWithoutPathExpansion(kType, &type);
126 TranslateWithTableAndSet(type, kVPNTypeTable,
119 flimflam::kProviderTypeProperty); 127 flimflam::kProviderTypeProperty);
128
129 CopyFieldsAccordingToSignature();
130 }
131
132 void LocalTranslator::TranslateWiFi() {
133 std::string security;
134 onc_object_->GetStringWithoutPathExpansion(wifi::kSecurity, &security);
135 TranslateWithTableAndSet(security, kWiFiSecurityTable,
136 flimflam::kSecurityProperty);
137
138 CopyFieldsAccordingToSignature();
139 }
140
141 void LocalTranslator::TranslateEAP() {
142 std::string outer;
143 onc_object_->GetStringWithoutPathExpansion(eap::kOuter, &outer);
144 TranslateWithTableAndSet(outer, kEAPOuterTable, flimflam::kEapMethodProperty);
145
146 // Translate the inner protocol only for outer tunneling protocols.
147 if (outer == eap::kPEAP || outer == eap::kEAP_TTLS) {
148 // In ONC the Inner protocol defaults to "Automatic".
149 std::string inner = eap::kAutomatic;
150 // ONC's Inner == "Automatic" translates to omitting the Phase2 property in
151 // Shill.
152 onc_object_->GetStringWithoutPathExpansion(eap::kInner, &inner);
153 if (inner != eap::kAutomatic) {
154 const StringTranslationEntry* table =
155 outer == eap::kPEAP ? kEAP_PEAP_InnerTable : kEAP_TTLS_InnerTable;
156 TranslateWithTableAndSet(inner, table, flimflam::kEapPhase2AuthProperty);
157 }
158 }
159
120 CopyFieldsAccordingToSignature(); 160 CopyFieldsAccordingToSignature();
121 } 161 }
122 162
123 void LocalTranslator::TranslateNetworkConfiguration() { 163 void LocalTranslator::TranslateNetworkConfiguration() {
124 TranslateWithTableAndSet(kType, kNetworkTypeTable, flimflam::kTypeProperty); 164 std::string type;
165 onc_object_->GetStringWithoutPathExpansion(kType, &type);
166 TranslateWithTableAndSet(type, kNetworkTypeTable, flimflam::kTypeProperty);
167
168 // Shill doesn't allow setting the name for non-VPN networks.
169 if (type == kVPN) {
170 std::string name;
171 onc_object_->GetStringWithoutPathExpansion(kName, &name);
172 shill_dictionary_->SetStringWithoutPathExpansion(
173 flimflam::kNameProperty, name);
174 }
175
125 CopyFieldsAccordingToSignature(); 176 CopyFieldsAccordingToSignature();
126 } 177 }
127 178
128 void LocalTranslator::CopyFieldsAccordingToSignature() { 179 void LocalTranslator::CopyFieldsAccordingToSignature() {
129 for (base::DictionaryValue::Iterator it(*onc_object_); it.HasNext(); 180 for (base::DictionaryValue::Iterator it(*onc_object_); it.HasNext();
130 it.Advance()) { 181 it.Advance()) {
131 AddValueAccordingToSignature(it.key(), 182 AddValueAccordingToSignature(it.key(),
132 make_scoped_ptr(it.value().DeepCopy())); 183 make_scoped_ptr(it.value().DeepCopy()));
133 } 184 }
134 } 185 }
135 186
136 void LocalTranslator::AddValueAccordingToSignature( 187 void LocalTranslator::AddValueAccordingToSignature(
137 const std::string& onc_name, 188 const std::string& onc_name,
138 scoped_ptr<base::Value> value) { 189 scoped_ptr<base::Value> value) {
139 if (value.get() == NULL) 190 if (value.get() == NULL)
140 return; 191 return;
141 const OncFieldSignature* field_signature = 192 const OncFieldSignature* field_signature =
142 GetFieldSignature(*onc_signature_, onc_name); 193 GetFieldSignature(*onc_signature_, onc_name);
143 DCHECK(field_signature != NULL); 194 DCHECK(field_signature != NULL);
144 if (field_signature == NULL || field_signature->shill_property_name == NULL) 195 if (field_signature == NULL || field_signature->shill_property_name == NULL)
145 return; 196 return;
146 197
147 shill_dictionary_->SetWithoutPathExpansion( 198 shill_dictionary_->SetWithoutPathExpansion(
148 field_signature->shill_property_name, value.release()); 199 field_signature->shill_property_name, value.release());
149 } 200 }
150 201
151 void LocalTranslator::TranslateWithTableAndSet( 202 void LocalTranslator::TranslateWithTableAndSet(
152 const std::string& onc_field_name, 203 const std::string& onc_value,
153 const StringTranslationEntry table[], 204 const StringTranslationEntry table[],
154 const std::string& shill_property_name) { 205 const std::string& shill_property_name) {
155 std::string onc_value;
156 if (!onc_object_->GetStringWithoutPathExpansion(onc_field_name, &onc_value))
157 return;
158
159 for (int i = 0; table[i].onc_value != NULL; ++i) { 206 for (int i = 0; table[i].onc_value != NULL; ++i) {
160 if (onc_value != table[i].onc_value) 207 if (onc_value != table[i].onc_value)
161 continue; 208 continue;
162 shill_dictionary_->SetStringWithoutPathExpansion(shill_property_name, 209 shill_dictionary_->SetStringWithoutPathExpansion(shill_property_name,
163 table[i].shill_value); 210 table[i].shill_value);
164 return; 211 return;
165 } 212 }
166 // As we previously validate ONC, this case should never occur. If it still 213 // As we previously validate ONC, this case should never occur. If it still
167 // occurs, we should check here. Otherwise the failure will only show up much 214 // occurs, we should check here. Otherwise the failure will only show up much
168 // later in Shill. 215 // later in Shill.
169 LOG(ERROR) << "Value '" << onc_value << "' for field '" 216 LOG(ERROR) << "Value '" << onc_value << "cannot be translated to Shill";
170 << onc_field_name << "' cannot be translated to Shill";
171 } 217 }
172 218
173 // Iterates recursively over |onc_object| and its |signature|. At each object 219 // Iterates recursively over |onc_object| and its |signature|. At each object
174 // applies the local translation using LocalTranslator::TranslateFields. The 220 // applies the local translation using LocalTranslator::TranslateFields. The
175 // results are written to |shill_dictionary|. 221 // results are written to |shill_dictionary|.
176 void TranslateONCHierarchy(const OncValueSignature& signature, 222 void TranslateONCHierarchy(const OncValueSignature& signature,
177 const base::DictionaryValue& onc_object, 223 const base::DictionaryValue& onc_object,
178 base::DictionaryValue* shill_dictionary) { 224 base::DictionaryValue* shill_dictionary) {
179 // Translates fields of |onc_object| and writes them to |shill_dictionary_|. 225 // Translates fields of |onc_object| and writes them to |shill_dictionary_|.
180 LocalTranslator translator(signature, onc_object, shill_dictionary); 226 LocalTranslator translator(signature, onc_object, shill_dictionary);
(...skipping 20 matching lines...) Expand all
201 const OncValueSignature* onc_signature, 247 const OncValueSignature* onc_signature,
202 const base::DictionaryValue& onc_object) { 248 const base::DictionaryValue& onc_object) {
203 CHECK(onc_signature != NULL); 249 CHECK(onc_signature != NULL);
204 scoped_ptr<base::DictionaryValue> shill_dictionary(new base::DictionaryValue); 250 scoped_ptr<base::DictionaryValue> shill_dictionary(new base::DictionaryValue);
205 TranslateONCHierarchy(*onc_signature, onc_object, shill_dictionary.get()); 251 TranslateONCHierarchy(*onc_signature, onc_object, shill_dictionary.get());
206 return shill_dictionary.Pass(); 252 return shill_dictionary.Pass();
207 } 253 }
208 254
209 } // namespace onc 255 } // namespace onc
210 } // namespace chromeos 256 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/onc/onc_translation_tables.cc ('k') | chromeos/network/onc/onc_translator_shill_to_onc.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698