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