Chromecast: adds class to help record complex histograms.
[chromium-blink-merge.git] / net / socket / stream_socket.h
blob7631e126fc50d724996e31cc38b5f550a447e1e6
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/base/net_log.h"
9 #include "net/socket/next_proto.h"
10 #include "net/socket/socket.h"
12 namespace net {
14 class AddressList;
15 class IPEndPoint;
16 class SSLInfo;
18 class NET_EXPORT_PRIVATE StreamSocket : public Socket {
19 public:
20 virtual ~StreamSocket() {}
22 // Called to establish a connection. Returns OK if the connection could be
23 // established synchronously. Otherwise, ERR_IO_PENDING is returned and the
24 // given callback will run asynchronously when the connection is established
25 // or when an error occurs. The result is some other error code if the
26 // connection could not be established.
28 // The socket's Read and Write methods may not be called until Connect
29 // succeeds.
31 // It is valid to call Connect on an already connected socket, in which case
32 // OK is simply returned.
34 // Connect may also be called again after a call to the Disconnect method.
36 virtual int Connect(const CompletionCallback& callback) = 0;
38 // Called to disconnect a socket. Does nothing if the socket is already
39 // disconnected. After calling Disconnect it is possible to call Connect
40 // again to establish a new connection.
42 // If IO (Connect, Read, or Write) is pending when the socket is
43 // disconnected, the pending IO is cancelled, and the completion callback
44 // will not be called.
45 virtual void Disconnect() = 0;
47 // Called to test if the connection is still alive. Returns false if a
48 // connection wasn't established or the connection is dead.
49 virtual bool IsConnected() const = 0;
51 // Called to test if the connection is still alive and idle. Returns false
52 // if a connection wasn't established, the connection is dead, or some data
53 // have been received.
54 virtual bool IsConnectedAndIdle() const = 0;
56 // Copies the peer address to |address| and returns a network error code.
57 // ERR_SOCKET_NOT_CONNECTED will be returned if the socket is not connected.
58 virtual int GetPeerAddress(IPEndPoint* address) const = 0;
60 // Copies the local address to |address| and returns a network error code.
61 // ERR_SOCKET_NOT_CONNECTED will be returned if the socket is not bound.
62 virtual int GetLocalAddress(IPEndPoint* address) const = 0;
64 // Gets the NetLog for this socket.
65 virtual const BoundNetLog& NetLog() const = 0;
67 // Set the annotation to indicate this socket was created for speculative
68 // reasons. This call is generally forwarded to a basic TCPClientSocket*,
69 // where a UseHistory can be updated.
70 virtual void SetSubresourceSpeculation() = 0;
71 virtual void SetOmniboxSpeculation() = 0;
73 // Returns true if the socket ever had any reads or writes. StreamSockets
74 // layered on top of transport sockets should return if their own Read() or
75 // Write() methods had been called, not the underlying transport's.
76 virtual bool WasEverUsed() const = 0;
78 // TODO(jri): Clean up -- remove this method.
79 // Returns true if the underlying transport socket is using TCP FastOpen.
80 // TCP FastOpen is an experiment with sending data in the TCP SYN packet.
81 virtual bool UsingTCPFastOpen() const = 0;
83 // TODO(jri): Clean up -- rename to a more general EnableAutoConnectOnWrite.
84 // Enables use of TCP FastOpen for the underlying transport socket.
85 virtual void EnableTCPFastOpenIfSupported() {}
87 // Returns true if NPN was negotiated during the connection of this socket.
88 virtual bool WasNpnNegotiated() const = 0;
90 // Returns the protocol negotiated via NPN for this socket, or
91 // kProtoUnknown will be returned if NPN is not applicable.
92 virtual NextProto GetNegotiatedProtocol() const = 0;
94 // Gets the SSL connection information of the socket. Returns false if
95 // SSL was not used by this socket.
96 virtual bool GetSSLInfo(SSLInfo* ssl_info) = 0;
98 protected:
99 // The following class is only used to gather statistics about the history of
100 // a socket. It is only instantiated and used in basic sockets, such as
101 // TCPClientSocket* instances. Other classes that are derived from
102 // StreamSocket should forward any potential settings to their underlying
103 // transport sockets.
104 class UseHistory {
105 public:
106 UseHistory();
107 ~UseHistory();
109 // Resets the state of UseHistory and emits histograms for the
110 // current state.
111 void Reset();
113 void set_was_ever_connected();
114 void set_was_used_to_convey_data();
116 // The next two setters only have any impact if the socket has not yet been
117 // used to transmit data. If called later, we assume that the socket was
118 // reused from the pool, and was NOT constructed to service a speculative
119 // request.
120 void set_subresource_speculation();
121 void set_omnibox_speculation();
123 bool was_used_to_convey_data() const;
125 private:
126 // Summarize the statistics for this socket.
127 void EmitPreconnectionHistograms() const;
128 // Indicate if this was ever connected.
129 bool was_ever_connected_;
130 // Indicate if this socket was ever used to transmit or receive data.
131 bool was_used_to_convey_data_;
133 // Indicate if this socket was first created for speculative use, and
134 // identify the motivation.
135 bool omnibox_speculation_;
136 bool subresource_speculation_;
137 DISALLOW_COPY_AND_ASSIGN(UseHistory);
141 } // namespace net
143 #endif // NET_SOCKET_STREAM_SOCKET_H_