blob: 8de588d85a9314a52d81ee5ec1b1a4e660880485 [file] [log] [blame]
Matt Menke7b5051072019-01-27 21:22:491// 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#ifndef NET_SOCKET_SSL_CONNECT_JOB_H_
6#define NET_SOCKET_SSL_CONNECT_JOB_H_
7
8#include <memory>
9#include <string>
10
11#include "base/macros.h"
12#include "base/memory/ref_counted.h"
13#include "base/time/time.h"
14#include "net/base/completion_once_callback.h"
15#include "net/base/completion_repeating_callback.h"
16#include "net/base/net_export.h"
17#include "net/base/privacy_mode.h"
18#include "net/http/http_response_info.h"
19#include "net/socket/connect_job.h"
20#include "net/socket/connection_attempts.h"
21#include "net/socket/ssl_client_socket.h"
22#include "net/ssl/ssl_config_service.h"
23
24namespace net {
25
Matt Menke7b5051072019-01-27 21:22:4926class HostPortPair;
Matt Menke7b5051072019-01-27 21:22:4927class HttpProxySocketParams;
Matt Menke7b5051072019-01-27 21:22:4928class SOCKSSocketParams;
Matt Menke0754b5d02019-02-10 21:46:4329class TransportClientSocketPool;
Matt Menke7b5051072019-01-27 21:22:4930class TransportSocketParams;
31
32class NET_EXPORT_PRIVATE SSLSocketParams
33 : public base::RefCounted<SSLSocketParams> {
34 public:
35 enum ConnectionType { DIRECT, SOCKS_PROXY, HTTP_PROXY };
36
37 // Exactly one of |direct_params|, |socks_proxy_params|, and
38 // |http_proxy_params| must be non-NULL.
39 SSLSocketParams(const scoped_refptr<TransportSocketParams>& direct_params,
40 const scoped_refptr<SOCKSSocketParams>& socks_proxy_params,
41 const scoped_refptr<HttpProxySocketParams>& http_proxy_params,
42 const HostPortPair& host_and_port,
43 const SSLConfig& ssl_config,
44 PrivacyMode privacy_mode);
45
46 // Returns the type of the underlying connection.
47 ConnectionType GetConnectionType() const;
48
49 // Must be called only when GetConnectionType() returns DIRECT.
50 const scoped_refptr<TransportSocketParams>& GetDirectConnectionParams() const;
51
52 // Must be called only when GetConnectionType() returns SOCKS_PROXY.
53 const scoped_refptr<SOCKSSocketParams>& GetSocksProxyConnectionParams() const;
54
55 // Must be called only when GetConnectionType() returns HTTP_PROXY.
56 const scoped_refptr<HttpProxySocketParams>& GetHttpProxyConnectionParams()
57 const;
58
59 const HostPortPair& host_and_port() const { return host_and_port_; }
60 const SSLConfig& ssl_config() const { return ssl_config_; }
61 PrivacyMode privacy_mode() const { return privacy_mode_; }
62
63 private:
64 friend class base::RefCounted<SSLSocketParams>;
65 ~SSLSocketParams();
66
67 const scoped_refptr<TransportSocketParams> direct_params_;
68 const scoped_refptr<SOCKSSocketParams> socks_proxy_params_;
69 const scoped_refptr<HttpProxySocketParams> http_proxy_params_;
70 const HostPortPair host_and_port_;
71 const SSLConfig ssl_config_;
72 const PrivacyMode privacy_mode_;
73
74 DISALLOW_COPY_AND_ASSIGN(SSLSocketParams);
75};
76
77// SSLConnectJob establishes a connection, through a proxy if needed, and then
78// handles the SSL handshake. It returns an SSLClientSocket on success.
Matt Menke9d5e2c92019-02-05 01:42:2379class NET_EXPORT_PRIVATE SSLConnectJob : public ConnectJob,
80 public ConnectJob::Delegate {
Matt Menke7b5051072019-01-27 21:22:4981 public:
82 // Note: the SSLConnectJob does not own |messenger| so it must outlive the
83 // job.
Matt Menkecb77b5402019-01-28 17:11:2384 SSLConnectJob(RequestPriority priority,
85 const CommonConnectJobParams& common_connect_job_params,
Matt Menke7b5051072019-01-27 21:22:4986 const scoped_refptr<SSLSocketParams>& params,
Matt Menke0754b5d02019-02-10 21:46:4387 TransportClientSocketPool* http_proxy_pool,
Matt Menke1a6c92d2019-02-23 00:25:3888 ConnectJob::Delegate* delegate,
89 const NetLogWithSource* net_log);
Matt Menke7b5051072019-01-27 21:22:4990 ~SSLConnectJob() override;
91
92 // ConnectJob methods.
93 LoadState GetLoadState() const override;
Matt Menke141b87f22019-01-30 02:43:0394 bool HasEstablishedConnection() const override;
Matt Menke7b5051072019-01-27 21:22:4995
Matt Menke9d5e2c92019-02-05 01:42:2396 // ConnectJob::Delegate methods.
97 void OnConnectJobComplete(int result, ConnectJob* job) override;
98
Matt Menke7b5051072019-01-27 21:22:4999 void GetAdditionalErrorState(ClientSocketHandle* handle) override;
100
101 // Returns the connection timeout that will be used by a HttpProxyConnectJob
102 // created with the specified parameters, given current network conditions.
103 static base::TimeDelta ConnectionTimeout(
104 const SSLSocketParams& params,
105 const NetworkQualityEstimator* network_quality_estimator);
106
107 private:
108 enum State {
109 STATE_TRANSPORT_CONNECT,
110 STATE_TRANSPORT_CONNECT_COMPLETE,
111 STATE_SOCKS_CONNECT,
112 STATE_SOCKS_CONNECT_COMPLETE,
113 STATE_TUNNEL_CONNECT,
114 STATE_TUNNEL_CONNECT_COMPLETE,
115 STATE_SSL_CONNECT,
116 STATE_SSL_CONNECT_COMPLETE,
117 STATE_NONE,
118 };
119
120 void OnIOComplete(int result);
121
122 // Runs the state transition loop.
123 int DoLoop(int result);
124
125 int DoTransportConnect();
126 int DoTransportConnectComplete(int result);
127 int DoSOCKSConnect();
128 int DoSOCKSConnectComplete(int result);
129 int DoTunnelConnect();
130 int DoTunnelConnectComplete(int result);
131 int DoSSLConnect();
132 int DoSSLConnectComplete(int result);
133
134 // Returns the initial state for the state machine based on the
135 // |connection_type|.
136 static State GetInitialState(SSLSocketParams::ConnectionType connection_type);
137
138 // Starts the SSL connection process. Returns OK on success and
139 // ERR_IO_PENDING if it cannot immediately service the request.
140 // Otherwise, it returns a net error code.
141 int ConnectInternal() override;
142
143 void ChangePriorityInternal(RequestPriority priority) override;
144
145 scoped_refptr<SSLSocketParams> params_;
Matt Menke0754b5d02019-02-10 21:46:43146 TransportClientSocketPool* const http_proxy_pool_;
Matt Menke7b5051072019-01-27 21:22:49147
148 State next_state_;
149 CompletionRepeatingCallback callback_;
Matt Menke9d5e2c92019-02-05 01:42:23150 std::unique_ptr<ConnectJob> nested_connect_job_;
151 std::unique_ptr<StreamSocket> nested_socket_;
Matt Menke7b5051072019-01-27 21:22:49152 std::unique_ptr<ClientSocketHandle> transport_socket_handle_;
153 std::unique_ptr<SSLClientSocket> ssl_socket_;
154
155 HttpResponseInfo error_response_info_;
156
157 ConnectionAttempts connection_attempts_;
158 // The address of the server the connect job is connected to. Populated if
159 // and only if the connect job is connected *directly* to the server (not
160 // through an HTTPS CONNECT request or a SOCKS proxy).
161 IPEndPoint server_address_;
162
163 DISALLOW_COPY_AND_ASSIGN(SSLConnectJob);
164};
165
166} // namespace net
167
168#endif // NET_SOCKET_SSL_CONNECT_JOB_H_