[email protected] | 7acf9829 | 2012-01-28 00:51:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 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> |
[email protected] | 8005968 | 2012-03-03 00:42:31 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 11 | |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 13 | #include "net/base/host_port_pair.h" |
[email protected] | 1b32317 | 2011-03-01 17:50:17 | [diff] [blame] | 14 | #include "net/base/net_log.h" |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 15 | #include "net/http/http_pipelined_host_pool.h" |
| 16 | #include "net/http/http_stream_factory.h" |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 17 | #include "net/proxy/proxy_server.h" |
[email protected] | c30bcce | 2011-12-20 17:50:51 | [diff] [blame] | 18 | #include "net/socket/ssl_client_socket.h" |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 19 | #include "net/spdy/spdy_session_key.h" |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 20 | |
| 21 | namespace net { |
| 22 | |
| 23 | class HttpNetworkSession; |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 24 | class HttpPipelinedHost; |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 25 | class SpdySession; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 26 | |
[email protected] | 4b9f729 | 2011-12-01 22:20:06 | [diff] [blame] | 27 | class NET_EXPORT_PRIVATE HttpStreamFactoryImpl : |
| 28 | public HttpStreamFactory, |
[email protected] | cb4bf8d | 2013-02-06 15:32:07 | [diff] [blame] | 29 | public HttpPipelinedHostPool::Delegate { |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 30 | public: |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame^] | 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); |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 34 | virtual ~HttpStreamFactoryImpl(); |
| 35 | |
[email protected] | 4b9f729 | 2011-12-01 22:20:06 | [diff] [blame] | 36 | // HttpStreamFactory interface |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 37 | virtual HttpStreamRequest* RequestStream( |
| 38 | const HttpRequestInfo& info, |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 39 | RequestPriority priority, |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 40 | const SSLConfig& server_ssl_config, |
| 41 | const SSLConfig& proxy_ssl_config, |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 42 | HttpStreamRequest::Delegate* delegate, |
[email protected] | f2cbbc8 | 2011-11-16 01:10:29 | [diff] [blame] | 43 | const BoundNetLog& net_log) OVERRIDE; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 44 | |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame^] | 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 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 54 | virtual void PreconnectStreams(int num_streams, |
| 55 | const HttpRequestInfo& info, |
[email protected] | 262eec8 | 2013-03-19 21:01:36 | [diff] [blame] | 56 | RequestPriority priority, |
[email protected] | 102957f | 2011-09-02 17:10:14 | [diff] [blame] | 57 | const SSLConfig& server_ssl_config, |
[email protected] | 7acf9829 | 2012-01-28 00:51:55 | [diff] [blame] | 58 | const SSLConfig& proxy_ssl_config) OVERRIDE; |
[email protected] | 837c4f5 | 2011-12-20 22:17:03 | [diff] [blame] | 59 | virtual base::Value* PipelineInfoToValue() const OVERRIDE; |
[email protected] | c2dad29 | 2012-09-07 21:27:35 | [diff] [blame] | 60 | virtual const HostMappingRules* GetHostMappingRules() const OVERRIDE; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 61 | |
[email protected] | 4b9f729 | 2011-12-01 22:20:06 | [diff] [blame] | 62 | // HttpPipelinedHostPool::Delegate interface |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 63 | virtual void OnHttpPipelinedHostHasAdditionalCapacity( |
[email protected] | 5477d89 | 2012-03-01 21:31:31 | [diff] [blame] | 64 | HttpPipelinedHost* host) OVERRIDE; |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 65 | |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame^] | 66 | size_t num_orphaned_jobs() const { return orphaned_job_set_.size(); } |
| 67 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 68 | private: |
| 69 | class Request; |
| 70 | class Job; |
| 71 | |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 72 | typedef std::set<Request*> RequestSet; |
[email protected] | 8005968 | 2012-03-03 00:42:31 | [diff] [blame] | 73 | typedef std::vector<Request*> RequestVector; |
[email protected] | e6d01765 | 2013-05-17 18:01:40 | [diff] [blame] | 74 | typedef std::map<SpdySessionKey, RequestSet> SpdySessionRequestMap; |
[email protected] | 8005968 | 2012-03-03 00:42:31 | [diff] [blame] | 75 | typedef std::map<HttpPipelinedHost::Key, |
| 76 | RequestVector> HttpPipeliningRequestMap; |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 77 | |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame^] | 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 | |
[email protected] | 61a52778 | 2013-02-21 03:58:00 | [diff] [blame] | 87 | PortAlternateProtocolPair GetAlternateProtocolRequestFor( |
| 88 | const GURL& original_url, |
| 89 | GURL* alternate_url) const; |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 90 | |
[email protected] | 1b32317 | 2011-03-01 17:50:17 | [diff] [blame] | 91 | // Detaches |job| from |request|. |
| 92 | void OrphanJob(Job* job, const Request* request); |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 93 | |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 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. |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame^] | 97 | void OnNewSpdySessionReady(scoped_refptr<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); |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 105 | |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 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 | |
[email protected] | 1b32317 | 2011-03-01 17:50:17 | [diff] [blame] | 111 | // Invoked when an orphaned Job finishes. |
| 112 | void OnOrphanedJobComplete(const Job* job); |
| 113 | |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 114 | // Invoked when the Job finishes preconnecting sockets. |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 115 | void OnPreconnectsComplete(const Job* job); |
| 116 | |
| 117 | // Called when the Preconnect completes. Used for testing. |
| 118 | virtual void OnPreconnectsCompleteInternal() {} |
| 119 | |
[email protected] | 5477d89 | 2012-03-01 21:31:31 | [diff] [blame] | 120 | void AbortPipelinedRequestsWithKey(const Job* job, |
| 121 | const HttpPipelinedHost::Key& key, |
| 122 | int status, |
| 123 | const SSLConfig& used_ssl_config); |
| 124 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 125 | HttpNetworkSession* const session_; |
| 126 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 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 |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 129 | // |request_map_|. The Requests will delete the corresponding job. |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 130 | std::map<const Job*, Request*> request_map_; |
| 131 | |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 132 | SpdySessionRequestMap spdy_session_request_map_; |
[email protected] | 5a60c8b | 2011-10-19 20:14:29 | [diff] [blame] | 133 | HttpPipeliningRequestMap http_pipelining_request_map_; |
| 134 | |
| 135 | HttpPipelinedHostPool http_pipelined_host_pool_; |
[email protected] | 7f28a8df | 2011-02-25 22:26:03 | [diff] [blame] | 136 | |
[email protected] | 1b32317 | 2011-03-01 17:50:17 | [diff] [blame] | 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 | |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 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 | |
[email protected] | 3732cea | 2013-06-21 06:50:50 | [diff] [blame^] | 149 | const bool for_websockets_; |
[email protected] | 102e27c | 2011-02-23 01:01:31 | [diff] [blame] | 150 | DISALLOW_COPY_AND_ASSIGN(HttpStreamFactoryImpl); |
| 151 | }; |
| 152 | |
| 153 | } // namespace net |
| 154 | |
| 155 | #endif // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_H_ |