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