Make per-tab zoom mode reset on new-page navigation, and send event.
[chromium-blink-merge.git] / net / socket / tcp_socket_libevent.h
blob0958b6d25d0f3e522524452bcb8e87281e3368fb
1 // Copyright 2013 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_TCP_SOCKET_LIBEVENT_H_
6 #define NET_SOCKET_TCP_SOCKET_LIBEVENT_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "net/base/address_family.h"
13 #include "net/base/completion_callback.h"
14 #include "net/base/net_export.h"
15 #include "net/base/net_log.h"
17 namespace net {
19 class AddressList;
20 class IOBuffer;
21 class IPEndPoint;
22 class SocketLibevent;
24 class NET_EXPORT TCPSocketLibevent {
25 public:
26 TCPSocketLibevent(NetLog* net_log, const NetLog::Source& source);
27 virtual ~TCPSocketLibevent();
29 int Open(AddressFamily family);
30 // Takes ownership of |socket_fd|.
31 int AdoptConnectedSocket(int socket_fd, const IPEndPoint& peer_address);
33 int Bind(const IPEndPoint& address);
35 int Listen(int backlog);
36 int Accept(scoped_ptr<TCPSocketLibevent>* socket,
37 IPEndPoint* address,
38 const CompletionCallback& callback);
40 int Connect(const IPEndPoint& address, const CompletionCallback& callback);
41 bool IsConnected() const;
42 bool IsConnectedAndIdle() const;
44 // Multiple outstanding requests are not supported.
45 // Full duplex mode (reading and writing at the same time) is supported.
46 int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
47 int Write(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
49 int GetLocalAddress(IPEndPoint* address) const;
50 int GetPeerAddress(IPEndPoint* address) const;
52 // Sets various socket options.
53 // The commonly used options for server listening sockets:
54 // - SetAddressReuse(true).
55 int SetDefaultOptionsForServer();
56 // The commonly used options for client sockets and accepted sockets:
57 // - SetNoDelay(true);
58 // - SetKeepAlive(true, 45).
59 void SetDefaultOptionsForClient();
60 int SetAddressReuse(bool allow);
61 int SetReceiveBufferSize(int32 size);
62 int SetSendBufferSize(int32 size);
63 bool SetKeepAlive(bool enable, int delay);
64 bool SetNoDelay(bool no_delay);
66 void Close();
68 // Setter/Getter methods for TCP FastOpen socket option.
69 bool UsingTCPFastOpen() const;
70 void EnableTCPFastOpenIfSupported();
72 bool IsValid() const;
74 // Marks the start/end of a series of connect attempts for logging purpose.
76 // TCPClientSocket may attempt to connect to multiple addresses until it
77 // succeeds in establishing a connection. The corresponding log will have
78 // multiple NetLog::TYPE_TCP_CONNECT_ATTEMPT entries nested within a
79 // NetLog::TYPE_TCP_CONNECT. These methods set the start/end of
80 // NetLog::TYPE_TCP_CONNECT.
82 // TODO(yzshen): Change logging format and let TCPClientSocket log the
83 // start/end of a series of connect attempts itself.
84 void StartLoggingMultipleConnectAttempts(const AddressList& addresses);
85 void EndLoggingMultipleConnectAttempts(int net_error);
87 const BoundNetLog& net_log() const { return net_log_; }
89 private:
90 // States that using a socket with TCP FastOpen can lead to.
91 enum TCPFastOpenStatus {
92 TCP_FASTOPEN_STATUS_UNKNOWN,
94 // The initial FastOpen connect attempted returned synchronously,
95 // indicating that we had and sent a cookie along with the initial data.
96 TCP_FASTOPEN_FAST_CONNECT_RETURN,
98 // The initial FastOpen connect attempted returned asynchronously,
99 // indicating that we did not have a cookie for the server.
100 TCP_FASTOPEN_SLOW_CONNECT_RETURN,
102 // Some other error occurred on connection, so we couldn't tell if
103 // FastOpen would have worked.
104 TCP_FASTOPEN_ERROR,
106 // An attempt to do a FastOpen succeeded immediately
107 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
108 // had acked the data we sent.
109 TCP_FASTOPEN_SYN_DATA_ACK,
111 // An attempt to do a FastOpen succeeded immediately
112 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and we later confirmed that the server
113 // had nacked the data we sent.
114 TCP_FASTOPEN_SYN_DATA_NACK,
116 // An attempt to do a FastOpen succeeded immediately
117 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and our probe to determine if the
118 // socket was using FastOpen failed.
119 TCP_FASTOPEN_SYN_DATA_GETSOCKOPT_FAILED,
121 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
122 // and we later confirmed that the server had acked initial data. This
123 // should never happen (we didn't send data, so it shouldn't have
124 // been acked).
125 TCP_FASTOPEN_NO_SYN_DATA_ACK,
127 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
128 // and we later discovered that the server had nacked initial data. This
129 // is the expected case results for TCP_FASTOPEN_SLOW_CONNECT_RETURN.
130 TCP_FASTOPEN_NO_SYN_DATA_NACK,
132 // An attempt to do a FastOpen failed (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
133 // and our later probe for ack/nack state failed.
134 TCP_FASTOPEN_NO_SYN_DATA_GETSOCKOPT_FAILED,
136 // The initial FastOpen connect+write succeeded immediately
137 // (TCP_FASTOPEN_FAST_CONNECT_RETURN) and a subsequent attempt to read from
138 // the connection failed.
139 TCP_FASTOPEN_FAST_CONNECT_READ_FAILED,
141 // The initial FastOpen connect+write failed
142 // (TCP_FASTOPEN_SLOW_CONNECT_RETURN)
143 // and a subsequent attempt to read from the connection failed.
144 TCP_FASTOPEN_SLOW_CONNECT_READ_FAILED,
146 // We didn't try FastOpen because it had failed in the past
147 // (g_tcp_fastopen_has_failed was true.)
148 // NOTE: This status is currently registered before a connect/write call
149 // is attempted, and may capture some cases where the status is registered
150 // but no connect is subsequently attempted.
151 // TODO(jri): The expectation is that such cases are not the common case
152 // with TCP FastOpen for SSL sockets however. Change code to be more
153 // accurate when TCP FastOpen is used for more than just SSL sockets.
154 TCP_FASTOPEN_PREVIOUSLY_FAILED,
156 TCP_FASTOPEN_MAX_VALUE
159 void AcceptCompleted(scoped_ptr<TCPSocketLibevent>* tcp_socket,
160 IPEndPoint* address,
161 const CompletionCallback& callback,
162 int rv);
163 int HandleAcceptCompleted(scoped_ptr<TCPSocketLibevent>* tcp_socket,
164 IPEndPoint* address,
165 int rv);
166 int BuildTcpSocketLibevent(scoped_ptr<TCPSocketLibevent>* tcp_socket,
167 IPEndPoint* address);
169 void ConnectCompleted(const CompletionCallback& callback, int rv) const;
170 int HandleConnectCompleted(int rv) const;
171 void LogConnectBegin(const AddressList& addresses) const;
172 void LogConnectEnd(int net_error) const;
174 void ReadCompleted(const scoped_refptr<IOBuffer>& buf,
175 const CompletionCallback& callback,
176 int rv);
177 int HandleReadCompleted(IOBuffer* buf, int rv);
179 void WriteCompleted(const scoped_refptr<IOBuffer>& buf,
180 const CompletionCallback& callback,
181 int rv);
182 int HandleWriteCompleted(IOBuffer* buf, int rv);
183 int TcpFastOpenWrite(IOBuffer* buf,
184 int buf_len,
185 const CompletionCallback& callback);
187 // Called after the first read completes on a TCP FastOpen socket.
188 void UpdateTCPFastOpenStatusAfterRead();
190 scoped_ptr<SocketLibevent> socket_;
191 scoped_ptr<SocketLibevent> accept_socket_;
193 // Enables experimental TCP FastOpen option.
194 bool use_tcp_fastopen_;
196 // True when TCP FastOpen is in use and we have attempted the
197 // connect with write.
198 bool tcp_fastopen_write_attempted_;
200 // True when TCP FastOpen is in use and we have done the connect.
201 bool tcp_fastopen_connected_;
203 TCPFastOpenStatus tcp_fastopen_status_;
205 bool logging_multiple_connect_attempts_;
207 BoundNetLog net_log_;
209 DISALLOW_COPY_AND_ASSIGN(TCPSocketLibevent);
212 } // namespace net
214 #endif // NET_SOCKET_TCP_SOCKET_LIBEVENT_H_