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