blob: c9e287b3870a54522f3bdda1735d32b7e6e7006d [file] [log] [blame]
[email protected]e13201d82012-12-12 05:00:321// 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]41d6b172013-01-29 16:10:579#include <string>
[email protected]e13201d82012-12-12 05:00:3210
[email protected]e13201d82012-12-12 05:00:3211#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]f698a012013-05-06 20:18:5916#include "net/base/network_change_notifier.h"
[email protected]e13201d82012-12-12 05:00:3217#include "net/proxy/proxy_server.h"
[email protected]ef95114d2013-04-17 17:57:0118#include "net/quic/quic_config.h"
19#include "net/quic/quic_crypto_stream.h"
[email protected]e13201d82012-12-12 05:00:3220#include "net/quic/quic_http_stream.h"
21#include "net/quic/quic_protocol.h"
22
23namespace net {
24
25class HostResolver;
26class ClientSocketFactory;
27class QuicClock;
28class QuicClientSession;
[email protected]e8ff26842013-03-22 21:02:0529class QuicCryptoClientStreamFactory;
[email protected]9558c5d32012-12-22 00:08:1430class QuicRandom;
[email protected]e13201d82012-12-12 05:00:3231class 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.
36class 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]f698a012013-05-06 20:18:5967class NET_EXPORT_PRIVATE QuicStreamFactory
68 : public NetworkChangeNotifier::IPAddressObserver {
[email protected]e13201d82012-12-12 05:00:3269 public:
[email protected]e8ff26842013-03-22 21:02:0570 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]e13201d82012-12-12 05:00:3276 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]56dfb902013-01-03 23:17:55101 // Closes all current sessions.
102 void CloseAllSessions(int error);
103
[email protected]c5b061b2013-01-05 00:31:34104 base::Value* QuicStreamFactoryInfoToValue() const;
105
[email protected]f698a012013-05-06 20:18:59106 // 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]e13201d82012-12-12 05:00:32112 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]ef95114d2013-04-17 17:57:01119 typedef std::map<HostPortProxyPair, QuicCryptoClientConfig*> CryptoConfigMap;
[email protected]e13201d82012-12-12 05:00:32120 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]ef95114d2013-04-17 17:57:01128 QuicClientSession* CreateSession(
129 const HostPortProxyPair& host_port_proxy_pair,
130 const AddressList& address_list,
131 const BoundNetLog& net_log);
[email protected]e13201d82012-12-12 05:00:32132 void ActivateSession(const HostPortProxyPair& host_port_proxy_pair,
133 QuicClientSession* session);
134
[email protected]ef95114d2013-04-17 17:57:01135 QuicCryptoClientConfig* GetOrCreateCryptoConfig(
136 const HostPortProxyPair& host_port_proxy_pair);
137
[email protected]e13201d82012-12-12 05:00:32138 HostResolver* host_resolver_;
139 ClientSocketFactory* client_socket_factory_;
[email protected]e8ff26842013-03-22 21:02:05140 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14141 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25142 scoped_ptr<QuicClock> clock_;
[email protected]e13201d82012-12-12 05:00:32143
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]ef95114d2013-04-17 17:57:01151 // 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]e13201d82012-12-12 05:00:32157 JobMap active_jobs_;
158 JobRequestsMap job_requests_map_;
159 RequestMap active_requests_;
160
[email protected]e13201d82012-12-12 05:00:32161 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_