Home | History | Annotate | Download | only in socket
      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 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;
     77 
     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;
     82 
     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() {}
     86 
     87   // Returns true if NPN was negotiated during the connection of this socket.
     88   virtual bool WasNpnNegotiated() const = 0;
     89 
     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;
     93 
     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;
     97 
     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();
    108 
    109     // Resets the state of UseHistory and emits histograms for the
    110     // current state.
    111     void Reset();
    112 
    113     void set_was_ever_connected();
    114     void set_was_used_to_convey_data();
    115 
    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();
    122 
    123     bool was_used_to_convey_data() const;
    124 
    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_;
    132 
    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);
    138   };
    139 };
    140 
    141 }  // namespace net
    142 
    143 #endif  // NET_SOCKET_STREAM_SOCKET_H_
    144