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