OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/net/onc_utils.h" | |
6 | |
7 #include "base/values.h" | |
8 #include "chrome/browser/chromeos/cros/network_ui_data.h" | |
9 #include "chrome/browser/chromeos/proxy_config_service_impl.h" | |
10 #include "chrome/browser/prefs/proxy_config_dictionary.h" | |
11 #include "chromeos/network/onc/onc_signature.h" | |
12 #include "chromeos/network/onc/onc_utils.h" | |
13 #include "net/proxy/proxy_server.h" | |
14 | |
15 namespace chromeos { | |
16 namespace onc { | |
17 | |
18 namespace { | |
19 | |
20 net::ProxyServer ConvertOncProxyLocationToHostPort( | |
21 net::ProxyServer::Scheme default_proxy_scheme, | |
22 const base::DictionaryValue& onc_proxy_location) { | |
23 std::string host; | |
24 onc_proxy_location.GetString(onc::proxy::kHost, &host); | |
25 // Parse |host| according to the format [<scheme>"://"]<server>[":"<port>]. | |
26 net::ProxyServer proxy_server = | |
27 net::ProxyServer::FromURI(host, default_proxy_scheme); | |
28 int port = 0; | |
29 onc_proxy_location.GetInteger(onc::proxy::kPort, &port); | |
stevenjb
2012/12/26 21:37:00
If onc_proxy_location.GetInteger() fails is using
pneubeck (no reviews)
2013/01/08 13:39:44
The port field is required by the ONC spec and the
| |
30 | |
31 // Replace the port parsed from |host| by the provided |port|. | |
32 return net::ProxyServer( | |
33 proxy_server.scheme(), | |
34 net::HostPortPair(proxy_server.host_port_pair().host(), | |
35 static_cast<uint16>(port))); | |
36 } | |
37 | |
38 void AppendProxyServerForScheme( | |
39 const base::DictionaryValue& onc_manual, | |
40 const std::string& onc_scheme, | |
41 std::string* spec) { | |
42 const base::DictionaryValue* onc_proxy_location = NULL; | |
43 if (!onc_manual.GetDictionaryWithoutPathExpansion(onc_scheme, | |
44 &onc_proxy_location)) { | |
45 return; | |
46 } | |
47 | |
48 net::ProxyServer::Scheme default_proxy_scheme = net::ProxyServer::SCHEME_HTTP; | |
49 std::string url_scheme; | |
50 if (onc_scheme == proxy::kFtp) { | |
51 url_scheme = "ftp"; | |
52 } else if (onc_scheme == proxy::kHttp) { | |
53 url_scheme = "http"; | |
54 } else if (onc_scheme == proxy::kHttps) { | |
55 url_scheme = "https"; | |
56 } else if (onc_scheme == proxy::kSocks) { | |
57 default_proxy_scheme = net::ProxyServer::SCHEME_SOCKS4; | |
58 url_scheme = "socks"; | |
59 } else { | |
60 NOTREACHED(); | |
61 } | |
62 | |
63 net::ProxyServer proxy_server = ConvertOncProxyLocationToHostPort( | |
64 default_proxy_scheme, *onc_proxy_location); | |
65 | |
66 ProxyConfigServiceImpl::ProxyConfig::EncodeAndAppendProxyServer( | |
67 url_scheme, proxy_server, spec); | |
68 } | |
69 | |
70 net::ProxyBypassRules ConvertOncExcludeDomainsToBypassRules( | |
71 const base::ListValue& onc_exclude_domains) { | |
72 net::ProxyBypassRules rules; | |
73 for (base::ListValue::const_iterator it = onc_exclude_domains.begin(); | |
74 it != onc_exclude_domains.end(); ++it) { | |
75 std::string rule; | |
76 (*it)->GetAsString(&rule); | |
77 rules.AddRuleFromString(rule); | |
78 } | |
79 return rules; | |
80 } | |
81 | |
82 } // namespace | |
83 | |
84 scoped_ptr<base::DictionaryValue> ConvertOncProxySettingsToProxyConfig( | |
85 const base::DictionaryValue& onc_proxy_settings) { | |
86 std::string type; | |
87 onc_proxy_settings.GetStringWithoutPathExpansion(proxy::kType, &type); | |
88 scoped_ptr<DictionaryValue> proxy_dict; | |
89 | |
90 if (type == proxy::kDirect) { | |
91 proxy_dict.reset(ProxyConfigDictionary::CreateDirect()); | |
92 } else if (type == proxy::kWPAD) { | |
93 proxy_dict.reset(ProxyConfigDictionary::CreateAutoDetect()); | |
94 } else if (type == proxy::kPAC) { | |
95 std::string pac_url; | |
96 onc_proxy_settings.GetStringWithoutPathExpansion(proxy::kPAC, &pac_url); | |
97 GURL url(pac_url); | |
98 DCHECK(url.is_valid()) | |
99 << "PAC field is invalid for this ProxySettings.Type"; | |
100 proxy_dict.reset(ProxyConfigDictionary::CreatePacScript(url.spec(), | |
101 false)); | |
102 } else if (type == proxy::kManual) { | |
103 const base::DictionaryValue* manual_dict = NULL; | |
104 onc_proxy_settings.GetDictionaryWithoutPathExpansion(proxy::kManual, | |
105 &manual_dict); | |
106 std::string manual_spec; | |
107 AppendProxyServerForScheme(*manual_dict, proxy::kFtp, &manual_spec); | |
108 AppendProxyServerForScheme(*manual_dict, proxy::kHttp, &manual_spec); | |
109 AppendProxyServerForScheme(*manual_dict, proxy::kSocks, &manual_spec); | |
110 AppendProxyServerForScheme(*manual_dict, proxy::kHttps, &manual_spec); | |
111 | |
112 const base::ListValue* exclude_domains = NULL; | |
113 net::ProxyBypassRules bypass_rules; | |
114 if (manual_dict->GetListWithoutPathExpansion(proxy::kExcludeDomains, | |
115 &exclude_domains)) { | |
116 bypass_rules.AssignFrom( | |
117 ConvertOncExcludeDomainsToBypassRules(*exclude_domains)); | |
118 } | |
119 proxy_dict.reset(ProxyConfigDictionary::CreateFixedServers( | |
120 manual_spec, bypass_rules.ToString())); | |
121 } else { | |
122 NOTREACHED(); | |
123 } | |
124 return proxy_dict.Pass(); | |
125 } | |
126 | |
127 namespace { | |
128 | |
129 void TranslateClientCertType(const std::string& client_cert_type, | |
130 NetworkUIData* ui_data) { | |
131 ClientCertType type; | |
132 if (client_cert_type == certificate::kNone) { | |
133 type = CLIENT_CERT_TYPE_NONE; | |
134 } else if (client_cert_type == certificate::kRef) { | |
135 type = CLIENT_CERT_TYPE_REF; | |
136 } else if (client_cert_type == certificate::kPattern) { | |
137 type = CLIENT_CERT_TYPE_PATTERN; | |
138 } else { | |
139 type = CLIENT_CERT_TYPE_NONE; | |
140 NOTREACHED(); | |
stevenjb
2012/12/26 21:37:00
This should be LOG(ERROR) if the input might be da
pneubeck (no reviews)
2013/01/08 13:39:44
The else part should logically never occur as the
stevenjb
2013/01/08 18:38:22
If it logically shouldn't happen, then NOTREACHED
| |
141 } | |
142 | |
143 ui_data->set_certificate_type(type); | |
144 } | |
145 | |
146 void TranslateCertificatePattern(const base::DictionaryValue& onc_object, | |
147 NetworkUIData* ui_data) { | |
148 CertificatePattern pattern; | |
149 bool success = pattern.CopyFromDictionary(onc_object); | |
150 DCHECK(success); | |
stevenjb
2012/12/26 21:37:00
Also LOG(ERROR) if data driven.
pneubeck (no reviews)
2013/01/08 13:39:44
Same as above.
You mentioned here https://coderev
| |
151 ui_data->set_certificate_pattern(pattern); | |
152 } | |
153 | |
154 void TranslateEAP(const base::DictionaryValue& eap, | |
155 NetworkUIData* ui_data) { | |
156 std::string client_cert_type; | |
157 if (eap.GetStringWithoutPathExpansion(eap::kClientCertType, | |
158 &client_cert_type)) { | |
159 TranslateClientCertType(client_cert_type, ui_data); | |
160 } | |
161 } | |
162 | |
163 void TranslateIPsec(const base::DictionaryValue& ipsec, | |
164 NetworkUIData* ui_data) { | |
165 std::string client_cert_type; | |
166 if (ipsec.GetStringWithoutPathExpansion(vpn::kClientCertType, | |
167 &client_cert_type)) { | |
168 TranslateClientCertType(client_cert_type, ui_data); | |
169 } | |
170 } | |
171 | |
172 void TranslateOpenVPN(const base::DictionaryValue& openvpn, | |
173 NetworkUIData* ui_data) { | |
174 std::string client_cert_type; | |
175 if (openvpn.GetStringWithoutPathExpansion(vpn::kClientCertType, | |
176 &client_cert_type)) { | |
177 TranslateClientCertType(client_cert_type, ui_data); | |
178 } | |
179 } | |
180 | |
181 void TranslateONCHierarchy(const OncValueSignature& signature, | |
182 const base::DictionaryValue& onc_object, | |
183 NetworkUIData* ui_data) { | |
184 if (&signature == &kCertificatePatternSignature) | |
185 TranslateCertificatePattern(onc_object, ui_data); | |
186 else if (&signature == &kEAPSignature) | |
187 TranslateEAP(onc_object, ui_data); | |
188 else if (&signature == &kIPsecSignature) | |
189 TranslateIPsec(onc_object, ui_data); | |
190 else if (&signature == &kOpenVPNSignature) | |
191 TranslateOpenVPN(onc_object, ui_data); | |
192 | |
193 // Recurse into nested objects. | |
194 for (base::DictionaryValue::Iterator it(onc_object); it.HasNext(); | |
195 it.Advance()) { | |
196 const base::DictionaryValue* inner_object; | |
197 if (!it.value().GetAsDictionary(&inner_object)) | |
198 continue; | |
199 | |
200 const OncFieldSignature* field_signature = | |
201 GetFieldSignature(signature, it.key()); | |
202 | |
203 TranslateONCHierarchy(*field_signature->value_signature, *inner_object, | |
204 ui_data); | |
205 } | |
206 } | |
207 | |
208 } // namespace | |
209 | |
210 scoped_ptr<base::DictionaryValue> CreateUIData( | |
211 ONCSource onc_source, | |
212 const base::DictionaryValue& onc_network) { | |
213 NetworkUIData ui_data; | |
214 TranslateONCHierarchy(kNetworkConfigurationSignature, onc_network, | |
215 &ui_data); | |
216 | |
217 ui_data.set_onc_source(onc_source); | |
218 | |
219 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | |
220 ui_data.FillDictionary(result.get()); | |
221 return result.Pass(); | |
222 } | |
223 | |
224 } // onc | |
225 } // chromeos | |
OLD | NEW |