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_STREAM_SOCKET_H_
6 #define NET_SOCKET_STREAM_SOCKET_H_
8 #include "net/log/net_log.h"
9 #include "net/socket/connection_attempts.h"
10 #include "net/socket/next_proto.h"
11 #include "net/socket/socket.h"
19 class NET_EXPORT_PRIVATE StreamSocket
: public Socket
{
21 ~StreamSocket() override
{}
23 // Called to establish a connection. Returns OK if the connection could be
24 // established synchronously. Otherwise, ERR_IO_PENDING is returned and the
25 // given callback will run asynchronously when the connection is established
26 // or when an error occurs. The result is some other error code if the
27 // connection could not be established.
29 // The socket's Read and Write methods may not be called until Connect
32 // It is valid to call Connect on an already connected socket, in which case
33 // OK is simply returned.
35 // Connect may also be called again after a call to the Disconnect method.
37 virtual int Connect(const CompletionCallback
& callback
) = 0;
39 // Called to disconnect a socket. Does nothing if the socket is already
40 // disconnected. After calling Disconnect it is possible to call Connect
41 // again to establish a new connection.
43 // If IO (Connect, Read, or Write) is pending when the socket is
44 // disconnected, the pending IO is cancelled, and the completion callback
45 // will not be called.
46 virtual void Disconnect() = 0;
48 // Called to test if the connection is still alive. Returns false if a
49 // connection wasn't established or the connection is dead.
50 virtual bool IsConnected() const = 0;
52 // Called to test if the connection is still alive and idle. Returns false
53 // if a connection wasn't established, the connection is dead, or some data
54 // have been received.
55 virtual bool IsConnectedAndIdle() const = 0;
57 // Copies the peer address to |address| and returns a network error code.
58 // ERR_SOCKET_NOT_CONNECTED will be returned if the socket is not connected.
59 virtual int GetPeerAddress(IPEndPoint
* address
) const = 0;
61 // Copies the local address to |address| and returns a network error code.
62 // ERR_SOCKET_NOT_CONNECTED will be returned if the socket is not bound.
63 virtual int GetLocalAddress(IPEndPoint
* address
) const = 0;
65 // Gets the NetLog for this socket.
66 virtual const BoundNetLog
& NetLog() const = 0;
68 // Set the annotation to indicate this socket was created for speculative
69 // reasons. This call is generally forwarded to a basic TCPClientSocket*,
70 // where a UseHistory can be updated.
71 virtual void SetSubresourceSpeculation() = 0;
72 virtual void SetOmniboxSpeculation() = 0;
74 // Returns true if the socket ever had any reads or writes. StreamSockets
75 // layered on top of transport sockets should return if their own Read() or
76 // Write() methods had been called, not the underlying transport's.
77 virtual bool WasEverUsed() const = 0;
79 // TODO(jri): Clean up -- remove this method.
80 // Returns true if the underlying transport socket is using TCP FastOpen.
81 // TCP FastOpen is an experiment with sending data in the TCP SYN packet.
82 virtual bool UsingTCPFastOpen() const = 0;
84 // TODO(jri): Clean up -- rename to a more general EnableAutoConnectOnWrite.
85 // Enables use of TCP FastOpen for the underlying transport socket.
86 virtual void EnableTCPFastOpenIfSupported() {}
88 // Returns true if NPN was negotiated during the connection of this socket.
89 virtual bool WasNpnNegotiated() const = 0;
91 // Returns the protocol negotiated via NPN for this socket, or
92 // kProtoUnknown will be returned if NPN is not applicable.
93 virtual NextProto
GetNegotiatedProtocol() const = 0;
95 // Gets the SSL connection information of the socket. Returns false if
96 // SSL was not used by this socket.
97 virtual bool GetSSLInfo(SSLInfo
* ssl_info
) = 0;
99 // Overwrites |out| with the connection attempts made in the process of
100 // connecting this socket.
101 virtual void GetConnectionAttempts(ConnectionAttempts
* out
) const = 0;
103 // Clears the socket's list of connection attempts.
104 virtual void ClearConnectionAttempts() = 0;
106 // Adds |attempts| to the socket's list of connection attempts.
107 virtual void AddConnectionAttempts(const ConnectionAttempts
& attempts
) = 0;
110 // The following class is only used to gather statistics about the history of
111 // a socket. It is only instantiated and used in basic sockets, such as
112 // TCPClientSocket* instances. Other classes that are derived from
113 // StreamSocket should forward any potential settings to their underlying
114 // transport sockets.
120 // Resets the state of UseHistory and emits histograms for the
124 void set_was_ever_connected();
125 void set_was_used_to_convey_data();
127 // The next two setters only have any impact if the socket has not yet been
128 // used to transmit data. If called later, we assume that the socket was
129 // reused from the pool, and was NOT constructed to service a speculative
131 void set_subresource_speculation();
132 void set_omnibox_speculation();
134 bool was_used_to_convey_data() const;
137 // Summarize the statistics for this socket.
138 void EmitPreconnectionHistograms() const;
139 // Indicate if this was ever connected.
140 bool was_ever_connected_
;
141 // Indicate if this socket was ever used to transmit or receive data.
142 bool was_used_to_convey_data_
;
144 // Indicate if this socket was first created for speculative use, and
145 // identify the motivation.
146 bool omnibox_speculation_
;
147 bool subresource_speculation_
;
148 DISALLOW_COPY_AND_ASSIGN(UseHistory
);
154 #endif // NET_SOCKET_STREAM_SOCKET_H_