James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | #include <vector> |
| 11 | |
| 12 | #include "base/gtest_prod_util.h" |
| 13 | #include "base/memory/ref_counted.h" |
| 14 | #include "net/base/host_port_pair.h" |
| 15 | #include "net/base/net_log.h" |
| 16 | #include "net/http/http_stream_factory.h" |
| 17 | #include "net/proxy/proxy_server.h" |
| 18 | #include "net/socket/ssl_client_socket.h" |
| 19 | #include "net/spdy/spdy_session_key.h" |
| 20 | |
| 21 | namespace net { |
| 22 | |
| 23 | class HttpNetworkSession; |
| 24 | class SpdySession; |
| 25 | |
| 26 | class NET_EXPORT_PRIVATE HttpStreamFactoryImpl : public HttpStreamFactory { |
| 27 | public: |
| 28 | // RequestStream may only be called if |for_websockets| is false. |
| 29 | // RequestWebSocketHandshakeStream may only be called if |for_websockets| |
| 30 | // is true. |
| 31 | HttpStreamFactoryImpl(HttpNetworkSession* session, bool for_websockets); |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 32 | ~HttpStreamFactoryImpl() override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 33 | |
| 34 | // HttpStreamFactory interface |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [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; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 41 | |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [diff] [blame] | 42 | HttpStreamRequest* RequestWebSocketHandshakeStream( |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [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, |
| 48 | WebSocketHandshakeStreamBase::CreateHelper* create_helper, |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 49 | const BoundNetLog& net_log) override; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 50 | |
James Robinson | e1b30cf | 2014-10-21 12:25:40 -0700 | [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; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 57 | |
| 58 | size_t num_orphaned_jobs() const { return orphaned_job_set_.size(); } |
| 59 | |
| 60 | private: |
| 61 | FRIEND_TEST_ALL_PREFIXES(HttpStreamFactoryImplRequestTest, SetPriority); |
| 62 | |
| 63 | class NET_EXPORT_PRIVATE Request; |
| 64 | class NET_EXPORT_PRIVATE Job; |
| 65 | |
| 66 | typedef std::set<Request*> RequestSet; |
| 67 | typedef std::map<SpdySessionKey, RequestSet> SpdySessionRequestMap; |
| 68 | |
| 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, |
| 75 | WebSocketHandshakeStreamBase::CreateHelper* create_helper, |
| 76 | const BoundNetLog& net_log); |
| 77 | |
| 78 | AlternateProtocolInfo GetAlternateProtocolRequestFor( |
| 79 | const GURL& original_url, |
| 80 | GURL* alternate_url); |
| 81 | |
| 82 | // Detaches |job| from |request|. |
| 83 | void OrphanJob(Job* job, const Request* request); |
| 84 | |
| 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. |
| 88 | void OnNewSpdySessionReady(const base::WeakPtr<SpdySession>& spdy_session, |
| 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); |
| 96 | |
| 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 | |
| 102 | // Invoked when an orphaned Job finishes. |
| 103 | void OnOrphanedJobComplete(const Job* job); |
| 104 | |
| 105 | // Invoked when the Job finishes preconnecting sockets. |
| 106 | void OnPreconnectsComplete(const Job* job); |
| 107 | |
| 108 | // Called when the Preconnect completes. Used for testing. |
| 109 | virtual void OnPreconnectsCompleteInternal() {} |
| 110 | |
| 111 | HttpNetworkSession* const session_; |
| 112 | |
| 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 |
| 115 | // |request_map_|. The Requests will delete the corresponding job. |
| 116 | std::map<const Job*, Request*> request_map_; |
| 117 | |
| 118 | SpdySessionRequestMap spdy_session_request_map_; |
| 119 | |
| 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 | |
| 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 | |
| 132 | const bool for_websockets_; |
| 133 | DISALLOW_COPY_AND_ASSIGN(HttpStreamFactoryImpl); |
| 134 | }; |
| 135 | |
| 136 | } // namespace net |
| 137 | |
| 138 | #endif // NET_HTTP_HTTP_STREAM_FACTORY_IMPL_H_ |