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. 4 5 #ifndef NET_SOCKET_STREAM_SOCKET_H_ 6 #define NET_SOCKET_STREAM_SOCKET_H_ 7 8 #include "net/base/net_log.h" 9 #include "net/socket/next_proto.h" 10 #include "net/socket/socket.h" 11 12 namespace net { 13 14 class AddressList; 15 class IPEndPoint; 16 class SSLInfo; 17 18 class NET_EXPORT_PRIVATE StreamSocket : public Socket { 19 public: 20 virtual ~StreamSocket() {} 21 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. 27 // 28 // The socket's Read and Write methods may not be called until Connect 29 // succeeds. 30 // 31 // It is valid to call Connect on an already connected socket, in which case 32 // OK is simply returned. 33 // 34 // Connect may also be called again after a call to the Disconnect method. 35 // 36 virtual int Connect(const CompletionCallback& callback) = 0; 37 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. 41 // 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; 46 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; 50 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; 55 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; 59 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; 63 64 // Gets the NetLog for this socket. 65 virtual const BoundNetLog& NetLog() const = 0; 66 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; 72 73 // Returns true if the underlying transport socket ever had any reads or 74 // writes. StreamSockets layered on top of transport sockets should forward 75 // this call to the transport socket. 76 virtual bool WasEverUsed() const = 0; 77 78 // Returns true if the underlying transport socket is using TCP FastOpen. 79 // TCP FastOpen is an experiment with sending data in the TCP SYN packet. 80 virtual bool UsingTCPFastOpen() const = 0; 81 82 // Returns true if NPN was negotiated during the connection of this socket. 83 virtual bool WasNpnNegotiated() const = 0; 84 85 // Returns the protocol negotiated via NPN for this socket, or 86 // kProtoUnknown will be returned if NPN is not applicable. 87 virtual NextProto GetNegotiatedProtocol() const = 0; 88 89 // Gets the SSL connection information of the socket. Returns false if 90 // SSL was not used by this socket. 91 virtual bool GetSSLInfo(SSLInfo* ssl_info) = 0; 92 93 protected: 94 // The following class is only used to gather statistics about the history of 95 // a socket. It is only instantiated and used in basic sockets, such as 96 // TCPClientSocket* instances. Other classes that are derived from 97 // StreamSocket should forward any potential settings to their underlying 98 // transport sockets. 99 class UseHistory { 100 public: 101 UseHistory(); 102 ~UseHistory(); 103 104 // Resets the state of UseHistory and emits histograms for the 105 // current state. 106 void Reset(); 107 108 void set_was_ever_connected(); 109 void set_was_used_to_convey_data(); 110 111 // The next two setters only have any impact if the socket has not yet been 112 // used to transmit data. If called later, we assume that the socket was 113 // reused from the pool, and was NOT constructed to service a speculative 114 // request. 115 void set_subresource_speculation(); 116 void set_omnibox_speculation(); 117 118 bool was_used_to_convey_data() const; 119 120 private: 121 // Summarize the statistics for this socket. 122 void EmitPreconnectionHistograms() const; 123 // Indicate if this was ever connected. 124 bool was_ever_connected_; 125 // Indicate if this socket was ever used to transmit or receive data. 126 bool was_used_to_convey_data_; 127 128 // Indicate if this socket was first created for speculative use, and 129 // identify the motivation. 130 bool omnibox_speculation_; 131 bool subresource_speculation_; 132 DISALLOW_COPY_AND_ASSIGN(UseHistory); 133 }; 134 }; 135 136 } // namespace net 137 138 #endif // NET_SOCKET_STREAM_SOCKET_H_ 139