blob: 48f4da0de4e80fa388b2b0e98035df9548d6711d [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"
16#include "net/proxy/proxy_server.h"
17#include "net/quic/quic_http_stream.h"
18#include "net/quic/quic_protocol.h"
19
20namespace net {
21
22class HostResolver;
23class ClientSocketFactory;
24class QuicClock;
25class QuicClientSession;
[email protected]e8ff26842013-03-22 21:02:0526class QuicCryptoClientStreamFactory;
[email protected]9558c5d32012-12-22 00:08:1427class QuicRandom;
[email protected]e13201d82012-12-12 05:00:3228class 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.
33class 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.
64class NET_EXPORT_PRIVATE QuicStreamFactory {
65 public:
[email protected]e8ff26842013-03-22 21:02:0566 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]e13201d82012-12-12 05:00:3272 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]56dfb902013-01-03 23:17:5597 // Closes all current sessions.
98 void CloseAllSessions(int error);
99
[email protected]c5b061b2013-01-05 00:31:34100 base::Value* QuicStreamFactoryInfoToValue() const;
101
[email protected]e13201d82012-12-12 05:00:32102 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]41d6b172013-01-29 16:10:57117 QuicClientSession* CreateSession(const std::string& host,
118 const AddressList& address_list,
119 const BoundNetLog& net_log);
[email protected]e13201d82012-12-12 05:00:32120 void ActivateSession(const HostPortProxyPair& host_port_proxy_pair,
121 QuicClientSession* session);
122
123 HostResolver* host_resolver_;
124 ClientSocketFactory* client_socket_factory_;
[email protected]e8ff26842013-03-22 21:02:05125 QuicCryptoClientStreamFactory* quic_crypto_client_stream_factory_;
[email protected]9558c5d32012-12-22 00:08:14126 QuicRandom* random_generator_;
[email protected]f1e97e92012-12-16 04:53:25127 scoped_ptr<QuicClock> clock_;
[email protected]e13201d82012-12-12 05:00:32128
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]e13201d82012-12-12 05:00:32140 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_