Returning scoped_ptr instead of raw pointer in QuicInfoToValue in net/
[chromium-blink-merge.git] / net / http / http_stream_factory_impl_request.cc
blob53548b1254fbf0706ca1c177bac9a57264f6bd17
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.
5 #include "net/http/http_stream_factory_impl_request.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/stl_util.h"
10 #include "net/http/http_stream_factory_impl_job.h"
11 #include "net/spdy/spdy_http_stream.h"
12 #include "net/spdy/spdy_session.h"
14 namespace net {
16 HttpStreamFactoryImpl::Request::Request(
17 const GURL& url,
18 HttpStreamFactoryImpl* factory,
19 HttpStreamRequest::Delegate* delegate,
20 WebSocketHandshakeStreamBase::CreateHelper*
21 websocket_handshake_stream_create_helper,
22 const BoundNetLog& net_log)
23 : url_(url),
24 factory_(factory),
25 websocket_handshake_stream_create_helper_(
26 websocket_handshake_stream_create_helper),
27 delegate_(delegate),
28 net_log_(net_log),
29 completed_(false),
30 was_npn_negotiated_(false),
31 protocol_negotiated_(kProtoUnknown),
32 using_spdy_(false) {
33 DCHECK(factory_);
34 DCHECK(delegate_);
36 net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_REQUEST);
39 HttpStreamFactoryImpl::Request::~Request() {
40 if (bound_job_.get())
41 DCHECK(jobs_.empty());
42 else
43 DCHECK(!jobs_.empty());
45 net_log_.EndEvent(NetLog::TYPE_HTTP_STREAM_REQUEST);
47 for (std::set<Job*>::iterator it = jobs_.begin(); it != jobs_.end(); ++it)
48 factory_->request_map_.erase(*it);
50 RemoveRequestFromSpdySessionRequestMap();
52 STLDeleteElements(&jobs_);
55 void HttpStreamFactoryImpl::Request::SetSpdySessionKey(
56 const SpdySessionKey& spdy_session_key) {
57 CHECK(!spdy_session_key_.get());
58 spdy_session_key_.reset(new SpdySessionKey(spdy_session_key));
59 RequestSet& request_set =
60 factory_->spdy_session_request_map_[spdy_session_key];
61 DCHECK(!ContainsKey(request_set, this));
62 request_set.insert(this);
65 void HttpStreamFactoryImpl::Request::AttachJob(Job* job) {
66 DCHECK(job);
67 jobs_.insert(job);
68 factory_->request_map_[job] = this;
71 void HttpStreamFactoryImpl::Request::Complete(
72 bool was_npn_negotiated,
73 NextProto protocol_negotiated,
74 bool using_spdy,
75 const BoundNetLog& job_net_log) {
76 DCHECK(!completed_);
77 completed_ = true;
78 was_npn_negotiated_ = was_npn_negotiated;
79 protocol_negotiated_ = protocol_negotiated;
80 using_spdy_ = using_spdy;
81 net_log_.AddEvent(
82 NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_JOB,
83 job_net_log.source().ToEventParametersCallback());
84 job_net_log.AddEvent(
85 NetLog::TYPE_HTTP_STREAM_JOB_BOUND_TO_REQUEST,
86 net_log_.source().ToEventParametersCallback());
89 void HttpStreamFactoryImpl::Request::OnStreamReady(
90 Job* job,
91 const SSLConfig& used_ssl_config,
92 const ProxyInfo& used_proxy_info,
93 HttpStream* stream) {
94 DCHECK(!factory_->for_websockets_);
95 DCHECK(stream);
96 DCHECK(completed_);
98 OnJobSucceeded(job);
99 delegate_->OnStreamReady(used_ssl_config, used_proxy_info, stream);
102 void HttpStreamFactoryImpl::Request::OnWebSocketHandshakeStreamReady(
103 Job* job,
104 const SSLConfig& used_ssl_config,
105 const ProxyInfo& used_proxy_info,
106 WebSocketHandshakeStreamBase* stream) {
107 DCHECK(factory_->for_websockets_);
108 DCHECK(stream);
109 DCHECK(completed_);
111 OnJobSucceeded(job);
112 delegate_->OnWebSocketHandshakeStreamReady(
113 used_ssl_config, used_proxy_info, stream);
116 void HttpStreamFactoryImpl::Request::OnStreamFailed(
117 Job* job,
118 int status,
119 const SSLConfig& used_ssl_config,
120 SSLFailureState ssl_failure_state) {
121 DCHECK_NE(OK, status);
122 DCHECK(job);
123 if (!bound_job_.get()) {
124 // Hey, we've got other jobs! Maybe one of them will succeed, let's just
125 // ignore this failure.
126 if (jobs_.size() > 1) {
127 jobs_.erase(job);
128 factory_->request_map_.erase(job);
129 // Notify all the other jobs that this one failed.
130 for (std::set<Job*>::iterator it = jobs_.begin(); it != jobs_.end(); ++it)
131 (*it)->MarkOtherJobComplete(*job);
132 delete job;
133 return;
134 } else {
135 bound_job_.reset(job);
136 jobs_.erase(job);
137 DCHECK(jobs_.empty());
138 factory_->request_map_.erase(job);
140 } else {
141 DCHECK(jobs_.empty());
143 delegate_->OnStreamFailed(status, used_ssl_config, ssl_failure_state);
146 void HttpStreamFactoryImpl::Request::OnCertificateError(
147 Job* job,
148 int status,
149 const SSLConfig& used_ssl_config,
150 const SSLInfo& ssl_info) {
151 DCHECK_NE(OK, status);
152 if (!bound_job_.get())
153 OrphanJobsExcept(job);
154 else
155 DCHECK(jobs_.empty());
156 delegate_->OnCertificateError(status, used_ssl_config, ssl_info);
159 void HttpStreamFactoryImpl::Request::OnNeedsProxyAuth(
160 Job* job,
161 const HttpResponseInfo& proxy_response,
162 const SSLConfig& used_ssl_config,
163 const ProxyInfo& used_proxy_info,
164 HttpAuthController* auth_controller) {
165 if (!bound_job_.get())
166 OrphanJobsExcept(job);
167 else
168 DCHECK(jobs_.empty());
169 delegate_->OnNeedsProxyAuth(
170 proxy_response, used_ssl_config, used_proxy_info, auth_controller);
173 void HttpStreamFactoryImpl::Request::OnNeedsClientAuth(
174 Job* job,
175 const SSLConfig& used_ssl_config,
176 SSLCertRequestInfo* cert_info) {
177 if (!bound_job_.get())
178 OrphanJobsExcept(job);
179 else
180 DCHECK(jobs_.empty());
181 delegate_->OnNeedsClientAuth(used_ssl_config, cert_info);
184 void HttpStreamFactoryImpl::Request::OnHttpsProxyTunnelResponse(
185 Job *job,
186 const HttpResponseInfo& response_info,
187 const SSLConfig& used_ssl_config,
188 const ProxyInfo& used_proxy_info,
189 HttpStream* stream) {
190 if (!bound_job_.get())
191 OrphanJobsExcept(job);
192 else
193 DCHECK(jobs_.empty());
194 delegate_->OnHttpsProxyTunnelResponse(
195 response_info, used_ssl_config, used_proxy_info, stream);
198 int HttpStreamFactoryImpl::Request::RestartTunnelWithProxyAuth(
199 const AuthCredentials& credentials) {
200 DCHECK(bound_job_.get());
201 return bound_job_->RestartTunnelWithProxyAuth(credentials);
204 void HttpStreamFactoryImpl::Request::SetPriority(RequestPriority priority) {
205 for (std::set<HttpStreamFactoryImpl::Job*>::const_iterator it = jobs_.begin();
206 it != jobs_.end(); ++it) {
207 (*it)->SetPriority(priority);
209 if (bound_job_)
210 bound_job_->SetPriority(priority);
213 LoadState HttpStreamFactoryImpl::Request::GetLoadState() const {
214 if (bound_job_.get())
215 return bound_job_->GetLoadState();
216 DCHECK(!jobs_.empty());
218 // Just pick the first one.
219 return (*jobs_.begin())->GetLoadState();
222 bool HttpStreamFactoryImpl::Request::was_npn_negotiated() const {
223 DCHECK(completed_);
224 return was_npn_negotiated_;
227 NextProto HttpStreamFactoryImpl::Request::protocol_negotiated()
228 const {
229 DCHECK(completed_);
230 return protocol_negotiated_;
233 bool HttpStreamFactoryImpl::Request::using_spdy() const {
234 DCHECK(completed_);
235 return using_spdy_;
238 const ConnectionAttempts& HttpStreamFactoryImpl::Request::connection_attempts()
239 const {
240 return connection_attempts_;
243 void
244 HttpStreamFactoryImpl::Request::RemoveRequestFromSpdySessionRequestMap() {
245 if (spdy_session_key_.get()) {
246 SpdySessionRequestMap& spdy_session_request_map =
247 factory_->spdy_session_request_map_;
248 DCHECK(ContainsKey(spdy_session_request_map, *spdy_session_key_));
249 RequestSet& request_set =
250 spdy_session_request_map[*spdy_session_key_];
251 DCHECK(ContainsKey(request_set, this));
252 request_set.erase(this);
253 if (request_set.empty())
254 spdy_session_request_map.erase(*spdy_session_key_);
255 spdy_session_key_.reset();
259 bool HttpStreamFactoryImpl::Request::HasSpdySessionKey() const {
260 return spdy_session_key_.get() != NULL;
263 // TODO(jgraettinger): Currently, HttpStreamFactoryImpl::Job notifies a
264 // Request that the session is ready, which in turn notifies it's delegate,
265 // and then it notifies HttpStreamFactoryImpl so that /other/ requests may
266 // be woken, but only if the spdy_session is still okay. This is tough to grok.
267 // Instead, see if Job can notify HttpStreamFactoryImpl only, and have one
268 // path for notifying any requests waiting for the session (including the
269 // request which spawned it).
270 void HttpStreamFactoryImpl::Request::OnNewSpdySessionReady(
271 Job* job,
272 scoped_ptr<HttpStream> stream,
273 const base::WeakPtr<SpdySession>& spdy_session,
274 bool direct) {
275 DCHECK(job);
276 DCHECK(job->using_spdy());
278 // Note: |spdy_session| may be NULL. In that case, |delegate_| should still
279 // receive |stream| so the error propogates up correctly, however there is no
280 // point in broadcasting |spdy_session| to other requests.
282 // The first case is the usual case.
283 if (!bound_job_.get()) {
284 OrphanJobsExcept(job);
285 } else { // This is the case for HTTPS proxy tunneling.
286 DCHECK_EQ(bound_job_.get(), job);
287 DCHECK(jobs_.empty());
290 // Cache these values in case the job gets deleted.
291 const SSLConfig used_ssl_config = job->server_ssl_config();
292 const ProxyInfo used_proxy_info = job->proxy_info();
293 const bool was_npn_negotiated = job->was_npn_negotiated();
294 const NextProto protocol_negotiated =
295 job->protocol_negotiated();
296 const bool using_spdy = job->using_spdy();
297 const BoundNetLog net_log = job->net_log();
299 Complete(was_npn_negotiated, protocol_negotiated, using_spdy, net_log);
301 // Cache this so we can still use it if the request is deleted.
302 HttpStreamFactoryImpl* factory = factory_;
303 if (factory->for_websockets_) {
304 // TODO(ricea): Re-instate this code when WebSockets over SPDY is
305 // implemented.
306 NOTREACHED();
307 } else {
308 delegate_->OnStreamReady(job->server_ssl_config(), job->proxy_info(),
309 stream.release());
311 // |this| may be deleted after this point.
312 if (spdy_session && spdy_session->IsAvailable()) {
313 factory->OnNewSpdySessionReady(spdy_session,
314 direct,
315 used_ssl_config,
316 used_proxy_info,
317 was_npn_negotiated,
318 protocol_negotiated,
319 using_spdy,
320 net_log);
324 void HttpStreamFactoryImpl::Request::AddConnectionAttempts(
325 const ConnectionAttempts& attempts) {
326 for (const auto& attempt : attempts)
327 connection_attempts_.push_back(attempt);
330 void HttpStreamFactoryImpl::Request::OrphanJobsExcept(Job* job) {
331 DCHECK(job);
332 DCHECK(!bound_job_.get());
333 DCHECK(ContainsKey(jobs_, job));
334 bound_job_.reset(job);
335 jobs_.erase(job);
336 factory_->request_map_.erase(job);
338 OrphanJobs();
341 void HttpStreamFactoryImpl::Request::OrphanJobs() {
342 RemoveRequestFromSpdySessionRequestMap();
344 std::set<Job*> tmp;
345 tmp.swap(jobs_);
347 for (std::set<Job*>::iterator it = tmp.begin(); it != tmp.end(); ++it)
348 factory_->OrphanJob(*it, this);
351 void HttpStreamFactoryImpl::Request::OnJobSucceeded(Job* job) {
352 // |job| should only be NULL if we're being serviced by a late bound
353 // SpdySession (one that was not created by a job in our |jobs_| set).
354 if (!job) {
355 DCHECK(!bound_job_.get());
356 DCHECK(!jobs_.empty());
357 // NOTE(willchan): We do *NOT* call OrphanJobs() here. The reason is because
358 // we *WANT* to cancel the unnecessary Jobs from other requests if another
359 // Job completes first.
360 // TODO(mbelshe): Revisit this when we implement ip connection pooling of
361 // SpdySessions. Do we want to orphan the jobs for a different hostname so
362 // they complete? Or do we want to prevent connecting a new SpdySession if
363 // we've already got one available for a different hostname where the ip
364 // address matches up?
365 return;
367 if (!bound_job_.get()) {
368 if (jobs_.size() > 1)
369 job->ReportJobSucceededForRequest();
370 // Notify all the other jobs that this one succeeded.
371 for (std::set<Job*>::iterator it = jobs_.begin(); it != jobs_.end(); ++it) {
372 if (*it != job) {
373 (*it)->MarkOtherJobComplete(*job);
376 // We may have other jobs in |jobs_|. For example, if we start multiple jobs
377 // for Alternate-Protocol.
378 OrphanJobsExcept(job);
379 return;
381 DCHECK(jobs_.empty());
384 } // namespace net