1// Copyright (c) 2011 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
11#include "base/memory/ref_counted.h"
12#include "net/base/host_port_pair.h"
13#include "net/http/http_stream_factory.h"
14#include "net/base/net_log.h"
15#include "net/proxy/proxy_server.h"
16
17namespace net {
18
19class HttpNetworkSession;
20class SpdySession;
21
22class HttpStreamFactoryImpl : public HttpStreamFactory {
23 public:
24  explicit HttpStreamFactoryImpl(HttpNetworkSession* session);
25  virtual ~HttpStreamFactoryImpl();
26
27  // HttpStreamFactory Interface
28  virtual HttpStreamRequest* RequestStream(
29      const HttpRequestInfo& info,
30      const SSLConfig& ssl_config,
31      HttpStreamRequest::Delegate* delegate,
32      const BoundNetLog& net_log);
33
34  virtual void PreconnectStreams(int num_streams,
35                                 const HttpRequestInfo& info,
36                                 const SSLConfig& ssl_config,
37                                 const BoundNetLog& net_log);
38  virtual void AddTLSIntolerantServer(const HostPortPair& server);
39  virtual bool IsTLSIntolerantServer(const HostPortPair& server) const;
40
41 private:
42  class Request;
43  class Job;
44
45  typedef std::set<Request*> RequestSet;
46  typedef std::map<HostPortProxyPair, RequestSet> SpdySessionRequestMap;
47
48  bool GetAlternateProtocolRequestFor(const GURL& original_url,
49                                      GURL* alternate_url) const;
50
51  // Detaches |job| from |request|.
52  void OrphanJob(Job* job, const Request* request);
53
54  // Called when a SpdySession is ready. It will find appropriate Requests and
55  // fulfill them. |direct| indicates whether or not |spdy_session| uses a
56  // proxy.
57  void OnSpdySessionReady(scoped_refptr<SpdySession> spdy_session,
58                          bool direct,
59                          const SSLConfig& used_ssl_config,
60                          const ProxyInfo& used_proxy_info,
61                          bool was_npn_negotiated,
62                          bool using_spdy,
63                          const NetLog::Source& source);
64
65  // Called when the Job detects that the endpoint indicated by the
66  // Alternate-Protocol does not work. Lets the factory update
67  // HttpAlternateProtocols with the failure and resets the SPDY session key.
68  void OnBrokenAlternateProtocol(const Job*, const HostPortPair& origin);
69
70  // Invoked when an orphaned Job finishes.
71  void OnOrphanedJobComplete(const Job* job);
72
73  // Invoked when the Job finishes preconnecting sockets.
74  void OnPreconnectsComplete(const Job* job);
75
76  // Called when the Preconnect completes. Used for testing.
77  virtual void OnPreconnectsCompleteInternal() {}
78
79  HttpNetworkSession* const session_;
80
81  std::set<HostPortPair> tls_intolerant_servers_;
82
83  // All Requests are handed out to clients. By the time HttpStreamFactoryImpl
84  // is destroyed, all Requests should be deleted (which should remove them from
85  // |request_map_|. The Requests will delete the corresponding job.
86  std::map<const Job*, Request*> request_map_;
87
88  SpdySessionRequestMap spdy_session_request_map_;
89
90  // These jobs correspond to jobs orphaned by Requests and now owned by
91  // HttpStreamFactoryImpl. Since they are no longer tied to Requests, they will
92  // not be canceled when Requests are canceled. Therefore, in
93  // ~HttpStreamFactoryImpl, it is possible for some jobs to still exist in this
94  // set. Leftover jobs will be deleted when the factory is destroyed.
95  std::set<const Job*> orphaned_job_set_;
96
97  // These jobs correspond to preconnect requests and have no associated Request
98  // object. They're owned by HttpStreamFactoryImpl. Leftover jobs will be
99  // deleted when the factory is destroyed.
100  std::set<const Job*> preconnect_job_set_;
101
102  DISALLOW_COPY_AND_ASSIGN(HttpStreamFactoryImpl);
103};
104
105}  // namespace net
106
107#endif  // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_H_
108