[email protected] | e13201d8 | 2012-12-12 05:00:32 | [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_QUIC_QUIC_STREAM_FACTORY_H_ |
| 6 | #define NET_QUIC_QUIC_STREAM_FACTORY_H_ |
| 7 | |
| 8 | #include <map> |
[email protected] | 41d6b17 | 2013-01-29 16:10:57 | [diff] [blame] | 9 | #include <string> |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 10 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
| 12 | #include "net/base/address_list.h" |
| 13 | #include "net/base/completion_callback.h" |
| 14 | #include "net/base/host_port_pair.h" |
| 15 | #include "net/base/net_log.h" |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame^] | 16 | #include "net/base/network_change_notifier.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 17 | #include "net/proxy/proxy_server.h" |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 18 | #include "net/quic/quic_config.h" |
| 19 | #include "net/quic/quic_crypto_stream.h" |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 20 | #include "net/quic/quic_http_stream.h" |
| 21 | #include "net/quic/quic_protocol.h" |
| 22 | |
| 23 | namespace net { |
| 24 | |
| 25 | class HostResolver; |
| 26 | class ClientSocketFactory; |
| 27 | class QuicClock; |
| 28 | class QuicClientSession; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 29 | class QuicCryptoClientStreamFactory; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 30 | class QuicRandom; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 31 | class QuicStreamFactory; |
| 32 | |
| 33 | // Encapsulates a pending request for a QuicHttpStream. |
| 34 | // If the request is still pending when it is destroyed, it will |
| 35 | // cancel the request with the factory. |
| 36 | class NET_EXPORT_PRIVATE QuicStreamRequest { |
| 37 | public: |
| 38 | explicit QuicStreamRequest(QuicStreamFactory* factory); |
| 39 | ~QuicStreamRequest(); |
| 40 | |
| 41 | int Request(const HostPortProxyPair& host_port_proxy_pair, |
| 42 | const BoundNetLog& net_log, |
| 43 | const CompletionCallback& callback); |
| 44 | |
| 45 | void OnRequestComplete(int rv); |
| 46 | |
| 47 | scoped_ptr<QuicHttpStream> ReleaseStream(); |
| 48 | |
| 49 | void set_stream(scoped_ptr<QuicHttpStream> stream); |
| 50 | |
| 51 | const BoundNetLog& net_log() const{ |
| 52 | return net_log_; |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | QuicStreamFactory* factory_; |
| 57 | HostPortProxyPair host_port_proxy_pair_; |
| 58 | BoundNetLog net_log_; |
| 59 | CompletionCallback callback_; |
| 60 | scoped_ptr<QuicHttpStream> stream_; |
| 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(QuicStreamRequest); |
| 63 | }; |
| 64 | |
| 65 | // A factory for creating new QuicHttpStreams on top of a pool of |
| 66 | // QuicClientSessions. |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame^] | 67 | class NET_EXPORT_PRIVATE QuicStreamFactory |
| 68 | : public NetworkChangeNotifier::IPAddressObserver { |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 69 | public: |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 70 | QuicStreamFactory( |
| 71 | HostResolver* host_resolver, |
| 72 | ClientSocketFactory* client_socket_factory, |
| 73 | QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory, |
| 74 | QuicRandom* random_generator, |
| 75 | QuicClock* clock); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 76 | virtual ~QuicStreamFactory(); |
| 77 | |
| 78 | // Creates a new QuicHttpStream to |host_port_proxy_pair| which will be |
| 79 | // owned by |request|. If a matching session already exists, this |
| 80 | // method will return OK. If no matching session exists, this will |
| 81 | // return ERR_IO_PENDING and will invoke OnRequestComplete asynchronously. |
| 82 | int Create(const HostPortProxyPair& host_port_proxy_pair, |
| 83 | const BoundNetLog& net_log, |
| 84 | QuicStreamRequest* request); |
| 85 | |
| 86 | // Returns a newly created QuicHttpStream owned by the caller, if a |
| 87 | // matching session already exists. Returns NULL otherwise. |
| 88 | scoped_ptr<QuicHttpStream> CreateIfSessionExists( |
| 89 | const HostPortProxyPair& host_port_proxy_pair, |
| 90 | const BoundNetLog& net_log); |
| 91 | |
| 92 | // Called by a session when it becomes idle. |
| 93 | void OnIdleSession(QuicClientSession* session); |
| 94 | |
| 95 | // Called by a session after it shuts down. |
| 96 | void OnSessionClose(QuicClientSession* session); |
| 97 | |
| 98 | // Cancels a pending request. |
| 99 | void CancelRequest(QuicStreamRequest* request); |
| 100 | |
[email protected] | 56dfb90 | 2013-01-03 23:17:55 | [diff] [blame] | 101 | // Closes all current sessions. |
| 102 | void CloseAllSessions(int error); |
| 103 | |
[email protected] | c5b061b | 2013-01-05 00:31:34 | [diff] [blame] | 104 | base::Value* QuicStreamFactoryInfoToValue() const; |
| 105 | |
[email protected] | f698a01 | 2013-05-06 20:18:59 | [diff] [blame^] | 106 | // NetworkChangeNotifier::IPAddressObserver methods: |
| 107 | |
| 108 | // Until the servers support roaming, close all connections when the local |
| 109 | // IP address changes. |
| 110 | virtual void OnIPAddressChanged() OVERRIDE; |
| 111 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 112 | private: |
| 113 | class Job; |
| 114 | |
| 115 | typedef std::map<HostPortProxyPair, QuicClientSession*> SessionMap; |
| 116 | typedef std::set<HostPortProxyPair> AliasSet; |
| 117 | typedef std::map<QuicClientSession*, AliasSet> SessionAliasMap; |
| 118 | typedef std::set<QuicClientSession*> SessionSet; |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 119 | typedef std::map<HostPortProxyPair, QuicCryptoClientConfig*> CryptoConfigMap; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 120 | typedef std::map<HostPortProxyPair, Job*> JobMap; |
| 121 | typedef std::map<QuicStreamRequest*, Job*> RequestMap; |
| 122 | typedef std::set<QuicStreamRequest*> RequestSet; |
| 123 | typedef std::map<Job*, RequestSet> JobRequestsMap; |
| 124 | |
| 125 | void OnJobComplete(Job* job, int rv); |
| 126 | bool HasActiveSession(const HostPortProxyPair& host_port_proxy_pair); |
| 127 | bool HasActiveJob(const HostPortProxyPair& host_port_proxy_pair); |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 128 | QuicClientSession* CreateSession( |
| 129 | const HostPortProxyPair& host_port_proxy_pair, |
| 130 | const AddressList& address_list, |
| 131 | const BoundNetLog& net_log); |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 132 | void ActivateSession(const HostPortProxyPair& host_port_proxy_pair, |
| 133 | QuicClientSession* session); |
| 134 | |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 135 | QuicCryptoClientConfig* GetOrCreateCryptoConfig( |
| 136 | const HostPortProxyPair& host_port_proxy_pair); |
| 137 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 138 | HostResolver* host_resolver_; |
| 139 | ClientSocketFactory* client_socket_factory_; |
[email protected] | e8ff2684 | 2013-03-22 21:02:05 | [diff] [blame] | 140 | QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_; |
[email protected] | 9558c5d3 | 2012-12-22 00:08:14 | [diff] [blame] | 141 | QuicRandom* random_generator_; |
[email protected] | f1e97e9 | 2012-12-16 04:53:25 | [diff] [blame] | 142 | scoped_ptr<QuicClock> clock_; |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 143 | |
| 144 | // Contains owning pointers to all sessions that currently exist. |
| 145 | SessionSet all_sessions_; |
| 146 | // Contains non-owning pointers to currently active session |
| 147 | // (not going away session, once they're implemented). |
| 148 | SessionMap active_sessions_; |
| 149 | SessionAliasMap session_aliases_; |
| 150 | |
[email protected] | ef95114d | 2013-04-17 17:57:01 | [diff] [blame] | 151 | // Contains owning pointers to QuicCryptoClientConfig. QuicCryptoClientConfig |
| 152 | // contains configuration and cached state about servers. |
| 153 | // TODO(rtenneti): Persist all_crypto_configs_ to disk and decide when to |
| 154 | // clear the data in the map. |
| 155 | CryptoConfigMap all_crypto_configs_; |
| 156 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 157 | JobMap active_jobs_; |
| 158 | JobRequestsMap job_requests_map_; |
| 159 | RequestMap active_requests_; |
| 160 | |
[email protected] | e13201d8 | 2012-12-12 05:00:32 | [diff] [blame] | 161 | base::WeakPtrFactory<QuicStreamFactory> weak_factory_; |
| 162 | |
| 163 | DISALLOW_COPY_AND_ASSIGN(QuicStreamFactory); |
| 164 | }; |
| 165 | |
| 166 | } // namespace net |
| 167 | |
| 168 | #endif // NET_QUIC_QUIC_STREAM_FACTORY_H_ |