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