Fix Telemetry media seek action flakiness.
[chromium-blink-merge.git] / net / socket / socks_client_socket.h
blob3d4f9fc27712017c7b15bb2573d0cfc41877bb89
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.
5 #ifndef NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
6 #define NET_SOCKET_SOCKS_CLIENT_SOCKET_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/address_list.h"
15 #include "net/base/completion_callback.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/net_log.h"
18 #include "net/dns/host_resolver.h"
19 #include "net/dns/single_request_host_resolver.h"
20 #include "net/socket/stream_socket.h"
22 namespace net {
24 class ClientSocketHandle;
25 class BoundNetLog;
27 // The SOCKS client socket implementation
28 class NET_EXPORT_PRIVATE SOCKSClientSocket : public StreamSocket {
29 public:
30 // Takes ownership of the |transport_socket|, which should already be
31 // connected by the time Connect() is called.
33 // |req_info| contains the hostname and port to which the socket above will
34 // communicate to via the socks layer. For testing the referrer is optional.
35 SOCKSClientSocket(ClientSocketHandle* transport_socket,
36 const HostResolver::RequestInfo& req_info,
37 HostResolver* host_resolver);
39 // Deprecated constructor (http://crbug.com/37810) that takes a StreamSocket.
40 SOCKSClientSocket(StreamSocket* transport_socket,
41 const HostResolver::RequestInfo& req_info,
42 HostResolver* host_resolver);
44 // On destruction Disconnect() is called.
45 virtual ~SOCKSClientSocket();
47 // StreamSocket implementation.
49 // Does the SOCKS handshake and completes the protocol.
50 virtual int Connect(const CompletionCallback& callback) OVERRIDE;
51 virtual void Disconnect() OVERRIDE;
52 virtual bool IsConnected() const OVERRIDE;
53 virtual bool IsConnectedAndIdle() const OVERRIDE;
54 virtual const BoundNetLog& NetLog() const OVERRIDE;
55 virtual void SetSubresourceSpeculation() OVERRIDE;
56 virtual void SetOmniboxSpeculation() OVERRIDE;
57 virtual bool WasEverUsed() const OVERRIDE;
58 virtual bool UsingTCPFastOpen() const OVERRIDE;
59 virtual bool WasNpnNegotiated() const OVERRIDE;
60 virtual NextProto GetNegotiatedProtocol() const OVERRIDE;
61 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
63 // Socket implementation.
64 virtual int Read(IOBuffer* buf,
65 int buf_len,
66 const CompletionCallback& callback) OVERRIDE;
67 virtual int Write(IOBuffer* buf,
68 int buf_len,
69 const CompletionCallback& callback) OVERRIDE;
71 virtual bool SetReceiveBufferSize(int32 size) OVERRIDE;
72 virtual bool SetSendBufferSize(int32 size) OVERRIDE;
74 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE;
75 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE;
77 private:
78 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, CompleteHandshake);
79 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AFailedDNS);
80 FRIEND_TEST_ALL_PREFIXES(SOCKSClientSocketTest, SOCKS4AIfDomainInIPv6);
82 enum State {
83 STATE_RESOLVE_HOST,
84 STATE_RESOLVE_HOST_COMPLETE,
85 STATE_HANDSHAKE_WRITE,
86 STATE_HANDSHAKE_WRITE_COMPLETE,
87 STATE_HANDSHAKE_READ,
88 STATE_HANDSHAKE_READ_COMPLETE,
89 STATE_NONE,
92 void DoCallback(int result);
93 void OnIOComplete(int result);
95 int DoLoop(int last_io_result);
96 int DoResolveHost();
97 int DoResolveHostComplete(int result);
98 int DoHandshakeRead();
99 int DoHandshakeReadComplete(int result);
100 int DoHandshakeWrite();
101 int DoHandshakeWriteComplete(int result);
103 const std::string BuildHandshakeWriteBuffer() const;
105 // Stores the underlying socket.
106 scoped_ptr<ClientSocketHandle> transport_;
108 State next_state_;
110 // Stores the callback to the layer above, called on completing Connect().
111 CompletionCallback user_callback_;
113 // This IOBuffer is used by the class to read and write
114 // SOCKS handshake data. The length contains the expected size to
115 // read or write.
116 scoped_refptr<IOBuffer> handshake_buf_;
118 // While writing, this buffer stores the complete write handshake data.
119 // While reading, it stores the handshake information received so far.
120 std::string buffer_;
122 // This becomes true when the SOCKS handshake has completed and the
123 // overlying connection is free to communicate.
124 bool completed_handshake_;
126 // These contain the bytes sent / received by the SOCKS handshake.
127 size_t bytes_sent_;
128 size_t bytes_received_;
130 // Used to resolve the hostname to which the SOCKS proxy will connect.
131 SingleRequestHostResolver host_resolver_;
132 AddressList addresses_;
133 HostResolver::RequestInfo host_request_info_;
135 BoundNetLog net_log_;
137 DISALLOW_COPY_AND_ASSIGN(SOCKSClientSocket);
140 } // namespace net
142 #endif // NET_SOCKET_SOCKS_CLIENT_SOCKET_H_