Home | History | Annotate | Download | only in http
      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_HTTP_HTTP_STREAM_FACTORY_IMPL_H_
      6 #define NET_HTTP_HTTP_STREAM_FACTORY_IMPL_H_
      7 
      8 #include <map>
      9 #include <set>
     10 #include <vector>
     11 
     12 #include "base/gtest_prod_util.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "net/base/host_port_pair.h"
     15 #include "net/base/net_log.h"
     16 #include "net/http/http_stream_factory.h"
     17 #include "net/proxy/proxy_server.h"
     18 #include "net/socket/ssl_client_socket.h"
     19 #include "net/spdy/spdy_session_key.h"
     20 
     21 namespace net {
     22 
     23 class HttpNetworkSession;
     24 class SpdySession;
     25 
     26 class NET_EXPORT_PRIVATE HttpStreamFactoryImpl : public HttpStreamFactory {
     27  public:
     28   // RequestStream may only be called if |for_websockets| is false.
     29   // RequestWebSocketHandshakeStream may only be called if |for_websockets|
     30   // is true.
     31   HttpStreamFactoryImpl(HttpNetworkSession* session, bool for_websockets);
     32   virtual ~HttpStreamFactoryImpl();
     33 
     34   // HttpStreamFactory interface
     35   virtual HttpStreamRequest* RequestStream(
     36       const HttpRequestInfo& info,
     37       RequestPriority priority,
     38       const SSLConfig& server_ssl_config,
     39       const SSLConfig& proxy_ssl_config,
     40       HttpStreamRequest::Delegate* delegate,
     41       const BoundNetLog& net_log) OVERRIDE;
     42 
     43   virtual HttpStreamRequest* RequestWebSocketHandshakeStream(
     44       const HttpRequestInfo& info,
     45       RequestPriority priority,
     46       const SSLConfig& server_ssl_config,
     47       const SSLConfig& proxy_ssl_config,
     48       HttpStreamRequest::Delegate* delegate,
     49       WebSocketHandshakeStreamBase::CreateHelper* create_helper,
     50       const BoundNetLog& net_log) OVERRIDE;
     51 
     52   virtual void PreconnectStreams(int num_streams,
     53                                  const HttpRequestInfo& info,
     54                                  RequestPriority priority,
     55                                  const SSLConfig& server_ssl_config,
     56                                  const SSLConfig& proxy_ssl_config) OVERRIDE;
     57   virtual const HostMappingRules* GetHostMappingRules() const OVERRIDE;
     58 
     59   size_t num_orphaned_jobs() const { return orphaned_job_set_.size(); }
     60 
     61  private:
     62   FRIEND_TEST_ALL_PREFIXES(HttpStreamFactoryImplRequestTest, SetPriority);
     63 
     64   class NET_EXPORT_PRIVATE Request;
     65   class NET_EXPORT_PRIVATE Job;
     66 
     67   typedef std::set<Request*> RequestSet;
     68   typedef std::map<SpdySessionKey, RequestSet> SpdySessionRequestMap;
     69 
     70   HttpStreamRequest* RequestStreamInternal(
     71       const HttpRequestInfo& info,
     72       RequestPriority priority,
     73       const SSLConfig& server_ssl_config,
     74       const SSLConfig& proxy_ssl_config,
     75       HttpStreamRequest::Delegate* delegate,
     76       WebSocketHandshakeStreamBase::CreateHelper* create_helper,
     77       const BoundNetLog& net_log);
     78 
     79   AlternateProtocolInfo GetAlternateProtocolRequestFor(
     80       const GURL& original_url,
     81       GURL* alternate_url);
     82 
     83   // Detaches |job| from |request|.
     84   void OrphanJob(Job* job, const Request* request);
     85 
     86   // Called when a SpdySession is ready. It will find appropriate Requests and
     87   // fulfill them. |direct| indicates whether or not |spdy_session| uses a
     88   // proxy.
     89   void OnNewSpdySessionReady(const base::WeakPtr<SpdySession>& spdy_session,
     90                              bool direct,
     91                              const SSLConfig& used_ssl_config,
     92                              const ProxyInfo& used_proxy_info,
     93                              bool was_npn_negotiated,
     94                              NextProto protocol_negotiated,
     95                              bool using_spdy,
     96                              const BoundNetLog& net_log);
     97 
     98   // Called when the Job detects that the endpoint indicated by the
     99   // Alternate-Protocol does not work. Lets the factory update
    100   // HttpAlternateProtocols with the failure and resets the SPDY session key.
    101   void OnBrokenAlternateProtocol(const Job*, const HostPortPair& origin);
    102 
    103   // Invoked when an orphaned Job finishes.
    104   void OnOrphanedJobComplete(const Job* job);
    105 
    106   // Invoked when the Job finishes preconnecting sockets.
    107   void OnPreconnectsComplete(const Job* job);
    108 
    109   // Called when the Preconnect completes. Used for testing.
    110   virtual void OnPreconnectsCompleteInternal() {}
    111 
    112   HttpNetworkSession* const session_;
    113 
    114   // All Requests are handed out to clients. By the time HttpStreamFactoryImpl
    115   // is destroyed, all Requests should be deleted (which should remove them from
    116   // |request_map_|. The Requests will delete the corresponding job.
    117   std::map<const Job*, Request*> request_map_;
    118 
    119   SpdySessionRequestMap spdy_session_request_map_;
    120 
    121   // These jobs correspond to jobs orphaned by Requests and now owned by
    122   // HttpStreamFactoryImpl. Since they are no longer tied to Requests, they will
    123   // not be canceled when Requests are canceled. Therefore, in
    124   // ~HttpStreamFactoryImpl, it is possible for some jobs to still exist in this
    125   // set. Leftover jobs will be deleted when the factory is destroyed.
    126   std::set<const Job*> orphaned_job_set_;
    127 
    128   // These jobs correspond to preconnect requests and have no associated Request
    129   // object. They're owned by HttpStreamFactoryImpl. Leftover jobs will be
    130   // deleted when the factory is destroyed.
    131   std::set<const Job*> preconnect_job_set_;
    132 
    133   const bool for_websockets_;
    134   DISALLOW_COPY_AND_ASSIGN(HttpStreamFactoryImpl);
    135 };
    136 
    137 }  // namespace net
    138 
    139 #endif  // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_H_
    140