OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "net/proxy/mojo_proxy_struct_traits.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "net/base/host_port_pair.h" | |
9 #include "net/proxy/proxy_info.h" | |
10 #include "net/proxy/proxy_server.h" | |
11 | |
12 namespace mojo { | |
13 | |
14 net::interfaces::ProxyScheme | |
15 EnumTraits<net::interfaces::ProxyScheme, net::ProxyServer::Scheme>::ToMojom( | |
16 net::ProxyServer::Scheme scheme) { | |
17 using net::ProxyServer; | |
18 switch (scheme) { | |
19 case ProxyServer::SCHEME_INVALID: | |
20 return net::interfaces::ProxyScheme::INVALID; | |
21 case ProxyServer::SCHEME_DIRECT: | |
22 return net::interfaces::ProxyScheme::DIRECT; | |
23 case ProxyServer::SCHEME_HTTP: | |
24 return net::interfaces::ProxyScheme::HTTP; | |
25 case ProxyServer::SCHEME_SOCKS4: | |
26 return net::interfaces::ProxyScheme::SOCKS4; | |
27 case ProxyServer::SCHEME_SOCKS5: | |
28 return net::interfaces::ProxyScheme::SOCKS5; | |
29 case ProxyServer::SCHEME_HTTPS: | |
30 return net::interfaces::ProxyScheme::HTTPS; | |
31 case ProxyServer::SCHEME_QUIC: | |
32 return net::interfaces::ProxyScheme::QUIC; | |
33 } | |
34 NOTREACHED(); | |
35 return net::interfaces::ProxyScheme::INVALID; | |
36 } | |
37 | |
38 bool EnumTraits<net::interfaces::ProxyScheme, net::ProxyServer::Scheme>:: | |
39 FromMojom(net::interfaces::ProxyScheme scheme, | |
40 net::ProxyServer::Scheme* out) { | |
41 using net::ProxyServer; | |
42 switch (scheme) { | |
43 case net::interfaces::ProxyScheme::INVALID: | |
44 *out = ProxyServer::SCHEME_INVALID; | |
45 return true; | |
46 case net::interfaces::ProxyScheme::DIRECT: | |
47 *out = ProxyServer::SCHEME_DIRECT; | |
48 return true; | |
49 case net::interfaces::ProxyScheme::HTTP: | |
50 *out = ProxyServer::SCHEME_HTTP; | |
51 return true; | |
52 case net::interfaces::ProxyScheme::SOCKS4: | |
53 *out = ProxyServer::SCHEME_SOCKS4; | |
54 return true; | |
55 case net::interfaces::ProxyScheme::SOCKS5: | |
56 *out = ProxyServer::SCHEME_SOCKS5; | |
57 return true; | |
58 case net::interfaces::ProxyScheme::HTTPS: | |
59 *out = ProxyServer::SCHEME_HTTPS; | |
60 return true; | |
61 case net::interfaces::ProxyScheme::QUIC: | |
62 *out = ProxyServer::SCHEME_QUIC; | |
63 return true; | |
64 } | |
65 return false; | |
66 } | |
67 | |
68 base::StringPiece | |
69 StructTraits<net::interfaces::ProxyServerDataView, net::ProxyServer>::host( | |
70 const net::ProxyServer& s) { | |
71 if (s.scheme() == net::ProxyServer::SCHEME_DIRECT || | |
72 s.scheme() == net::ProxyServer::SCHEME_INVALID) { | |
73 return base::StringPiece(); | |
dcheng
2016/10/05 05:59:33
Is it valid for these conditions to be true and ho
Sam McNally
2016/10/05 22:48:37
ProxyServer::host_port_pair() DCHECKs that it's va
| |
74 } | |
75 return s.host_port_pair().host(); | |
76 } | |
77 | |
78 uint16_t StructTraits<net::interfaces::ProxyServerDataView, | |
79 net::ProxyServer>::port(const net::ProxyServer& s) { | |
80 if (s.scheme() == net::ProxyServer::SCHEME_DIRECT || | |
81 s.scheme() == net::ProxyServer::SCHEME_INVALID) { | |
82 return 0; | |
83 } | |
84 return s.host_port_pair().port(); | |
85 } | |
86 | |
87 bool StructTraits<net::interfaces::ProxyServerDataView, net::ProxyServer>::Read( | |
88 net::interfaces::ProxyServerDataView data, | |
89 net::ProxyServer* out) { | |
90 net::ProxyServer::Scheme scheme; | |
91 if (!data.ReadScheme(&scheme)) | |
92 return false; | |
93 | |
94 base::StringPiece host; | |
95 if (!data.ReadHost(&host)) | |
96 return false; | |
97 | |
98 if ((scheme == net::ProxyServer::SCHEME_DIRECT || | |
99 scheme == net::ProxyServer::SCHEME_INVALID) && | |
100 (!host.empty() || data.port())) { | |
101 return false; | |
102 } | |
103 | |
104 *out = net::ProxyServer(scheme, | |
105 net::HostPortPair(host.as_string(), data.port())); | |
106 return true; | |
107 } | |
108 | |
109 bool StructTraits<net::interfaces::ProxyInfoDataView, net::ProxyInfo>::Read( | |
110 net::interfaces::ProxyInfoDataView data, | |
111 net::ProxyInfo* out) { | |
112 std::vector<net::ProxyServer> proxy_servers; | |
113 if (!data.ReadProxyServers(&proxy_servers)) | |
114 return false; | |
115 | |
116 net::ProxyList proxy_list; | |
117 for (const auto& server : proxy_servers) | |
118 proxy_list.AddProxyServer(server); | |
119 | |
120 out->UseProxyList(proxy_list); | |
121 return true; | |
122 } | |
123 | |
124 } // namespace mojo | |
OLD | NEW |