Home | History | Annotate | Download | only in quic
      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 // A client specific QuicSession subclass.  This class owns the underlying
      6 // QuicConnection and QuicConnectionHelper objects.  The connection stores
      7 // a non-owning pointer to the helper so this session needs to ensure that
      8 // the helper outlives the connection.
      9 
     10 #ifndef NET_QUIC_QUIC_CLIENT_SESSION_H_
     11 #define NET_QUIC_QUIC_CLIENT_SESSION_H_
     12 
     13 #include <string>
     14 
     15 #include "base/containers/hash_tables.h"
     16 #include "net/base/completion_callback.h"
     17 #include "net/quic/quic_connection_logger.h"
     18 #include "net/quic/quic_crypto_client_stream.h"
     19 #include "net/quic/quic_reliable_client_stream.h"
     20 #include "net/quic/quic_session.h"
     21 
     22 namespace net {
     23 
     24 class DatagramClientSocket;
     25 class QuicConnectionHelper;
     26 class QuicCryptoClientStreamFactory;
     27 class QuicStreamFactory;
     28 class SSLInfo;
     29 
     30 namespace test {
     31 class QuicClientSessionPeer;
     32 }  // namespace test
     33 
     34 class NET_EXPORT_PRIVATE QuicClientSession : public QuicSession {
     35  public:
     36   // A helper class used to manage a request to create a stream.
     37   class NET_EXPORT_PRIVATE StreamRequest {
     38    public:
     39     StreamRequest();
     40     ~StreamRequest();
     41 
     42     // Starts a request to create a stream.  If OK is returned, then
     43     // |stream| will be updated with the newly created stream.  If
     44     // ERR_IO_PENDING is returned, then when the request is eventuallly
     45     // complete |callback| will be called.
     46     int StartRequest(const base::WeakPtr<QuicClientSession> session,
     47                      QuicReliableClientStream** stream,
     48                      const CompletionCallback& callback);
     49 
     50     // Cancels any pending stream creation request. May be called
     51     // repeatedly.
     52     void CancelRequest();
     53 
     54    private:
     55     friend class QuicClientSession;
     56 
     57     // Called by |session_| for an asynchronous request when the stream
     58     // request has finished successfully.
     59     void OnRequestCompleteSuccess(QuicReliableClientStream* stream);
     60 
     61     // Called by |session_| for an asynchronous request when the stream
     62     // request has finished with an error. Also called with ERR_ABORTED
     63     // if |session_| is destroyed while the stream request is still pending.
     64     void OnRequestCompleteFailure(int rv);
     65 
     66     base::WeakPtr<QuicClientSession> session_;
     67     CompletionCallback callback_;
     68     QuicReliableClientStream** stream_;
     69 
     70     DISALLOW_COPY_AND_ASSIGN(StreamRequest);
     71   };
     72 
     73   // Constructs a new session which will own |connection| and |helper|, but
     74   // not |stream_factory|, which must outlive this session.
     75   // TODO(rch): decouple the factory from the session via a Delegate interface.
     76   QuicClientSession(QuicConnection* connection,
     77                     DatagramClientSocket* socket,
     78                     QuicStreamFactory* stream_factory,
     79                     QuicCryptoClientStreamFactory* crypto_client_stream_factory,
     80                     const std::string& server_hostname,
     81                     const QuicConfig& config,
     82                     QuicCryptoClientConfig* crypto_config,
     83                     NetLog* net_log);
     84 
     85   virtual ~QuicClientSession();
     86 
     87   // Attempts to create a new stream.  If the stream can be
     88   // created immediately, returns OK.  If the open stream limit
     89   // has been reached, returns ERR_IO_PENDING, and |request|
     90   // will be added to the stream requets queue and will
     91   // be completed asynchronously.
     92   // TODO(rch): remove |stream| from this and use setter on |request|
     93   // and fix in spdy too.
     94   int TryCreateStream(StreamRequest* request,
     95                       QuicReliableClientStream** stream);
     96 
     97   // Cancels the pending stream creation request.
     98   void CancelRequest(StreamRequest* request);
     99 
    100   // QuicSession methods:
    101   virtual QuicReliableClientStream* CreateOutgoingReliableStream() OVERRIDE;
    102   virtual QuicCryptoClientStream* GetCryptoStream() OVERRIDE;
    103   virtual void CloseStream(QuicStreamId stream_id) OVERRIDE;
    104   virtual void OnCryptoHandshakeEvent(CryptoHandshakeEvent event) OVERRIDE;
    105   virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE;
    106 
    107   // QuicConnectionVisitorInterface methods:
    108   virtual void ConnectionClose(QuicErrorCode error, bool from_peer) OVERRIDE;
    109 
    110   // Performs a crypto handshake with the server.
    111   int CryptoConnect(const CompletionCallback& callback);
    112 
    113   // Causes the QuicConnectionHelper to start reading from the socket
    114   // and passing the data along to the QuicConnection.
    115   void StartReading();
    116 
    117   // Close the session because of |error| and notifies the factory
    118   // that this session has been closed, which will delete the session.
    119   void CloseSessionOnError(int error);
    120 
    121   base::Value* GetInfoAsValue(const HostPortPair& pair) const;
    122 
    123   const BoundNetLog& net_log() const { return net_log_; }
    124 
    125   base::WeakPtr<QuicClientSession> GetWeakPtr();
    126 
    127  protected:
    128   // QuicSession methods:
    129   virtual ReliableQuicStream* CreateIncomingReliableStream(
    130       QuicStreamId id) OVERRIDE;
    131 
    132  private:
    133   friend class test::QuicClientSessionPeer;
    134 
    135   typedef std::list<StreamRequest*> StreamRequestQueue;
    136 
    137   QuicReliableClientStream* CreateOutgoingReliableStreamImpl();
    138   // A completion callback invoked when a read completes.
    139   void OnReadComplete(int result);
    140 
    141   void CloseSessionOnErrorInner(int error);
    142 
    143   // Posts a task to notify the factory that this session has been closed.
    144   void NotifyFactoryOfSessionCloseLater();
    145 
    146   // Notifies the factory that this session has been closed which will
    147   // delete |this|.
    148   void NotifyFactoryOfSessionClose();
    149 
    150   base::WeakPtrFactory<QuicClientSession> weak_factory_;
    151   scoped_ptr<QuicCryptoClientStream> crypto_stream_;
    152   QuicStreamFactory* stream_factory_;
    153   scoped_ptr<DatagramClientSocket> socket_;
    154   scoped_refptr<IOBufferWithSize> read_buffer_;
    155   StreamRequestQueue stream_requests_;
    156   bool read_pending_;
    157   CompletionCallback callback_;
    158   size_t num_total_streams_;
    159   BoundNetLog net_log_;
    160   QuicConnectionLogger logger_;
    161 
    162   DISALLOW_COPY_AND_ASSIGN(QuicClientSession);
    163 };
    164 
    165 }  // namespace net
    166 
    167 #endif  // NET_QUIC_QUIC_CLIENT_SESSION_H_
    168