Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / net / http / http_stream_factory_impl_request.cc
blob9e2f9dc3d94c49a617ee2cd23066175424a470a1
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());
43 net_log_.EndEvent(NetLog::TYPE_HTTP_STREAM_REQUEST);
45 CancelJobs();
48 void HttpStreamFactoryImpl::Request::SetSpdySessionKey(
49 const SpdySessionKey& spdy_session_key) {
50 CHECK(!spdy_session_key_.get());
51 spdy_session_key_.reset(new SpdySessionKey(spdy_session_key));
52 RequestSet& request_set =
53 factory_->spdy_session_request_map_[spdy_session_key];
54 DCHECK(!ContainsKey(request_set, this));
55 request_set.insert(this);
58 void HttpStreamFactoryImpl::Request::AttachJob(Job* job) {
59 DCHECK(job);
60 jobs_.insert(job);
61 factory_->request_map_[job] = this;
64 void HttpStreamFactoryImpl::Request::Complete(bool was_npn_negotiated,
65 NextProto protocol_negotiated,
66 bool using_spdy) {
67 DCHECK(!completed_);
68 completed_ = true;
69 was_npn_negotiated_ = was_npn_negotiated;
70 protocol_negotiated_ = protocol_negotiated;
71 using_spdy_ = using_spdy;
74 void HttpStreamFactoryImpl::Request::OnStreamReady(
75 Job* job,
76 const SSLConfig& used_ssl_config,
77 const ProxyInfo& used_proxy_info,
78 HttpStream* stream) {
79 DCHECK(!factory_->for_websockets_);
80 DCHECK(stream);
81 DCHECK(completed_);
83 OnJobSucceeded(job);
84 delegate_->OnStreamReady(used_ssl_config, used_proxy_info, stream);
87 void HttpStreamFactoryImpl::Request::OnWebSocketHandshakeStreamReady(
88 Job* job,
89 const SSLConfig& used_ssl_config,
90 const ProxyInfo& used_proxy_info,
91 WebSocketHandshakeStreamBase* stream) {
92 DCHECK(factory_->for_websockets_);
93 DCHECK(stream);
94 DCHECK(completed_);
96 OnJobSucceeded(job);
97 delegate_->OnWebSocketHandshakeStreamReady(
98 used_ssl_config, used_proxy_info, stream);
101 void HttpStreamFactoryImpl::Request::OnStreamFailed(
102 Job* job,
103 int status,
104 const SSLConfig& used_ssl_config,
105 SSLFailureState ssl_failure_state) {
106 DCHECK_NE(OK, status);
107 DCHECK(job);
108 if (!bound_job_.get()) {
109 if (jobs_.size() > 1) {
110 // Hey, we've got other jobs! Maybe one of them will succeed, let's just
111 // ignore this failure.
112 jobs_.erase(job);
113 factory_->request_map_.erase(job);
114 // Notify all the other jobs that this one failed.
115 for (std::set<Job*>::iterator it = jobs_.begin(); it != jobs_.end(); ++it)
116 (*it)->MarkOtherJobComplete(*job);
117 delete job;
118 return;
119 } else {
120 BindJob(job);
122 } else {
123 DCHECK(jobs_.empty());
125 delegate_->OnStreamFailed(status, used_ssl_config, ssl_failure_state);
128 void HttpStreamFactoryImpl::Request::OnCertificateError(
129 Job* job,
130 int status,
131 const SSLConfig& used_ssl_config,
132 const SSLInfo& ssl_info) {
133 DCHECK_NE(OK, status);
134 if (!bound_job_.get())
135 BindJob(job);
136 else
137 DCHECK(jobs_.empty());
138 delegate_->OnCertificateError(status, used_ssl_config, ssl_info);
141 void HttpStreamFactoryImpl::Request::OnNeedsProxyAuth(
142 Job* job,
143 const HttpResponseInfo& proxy_response,
144 const SSLConfig& used_ssl_config,
145 const ProxyInfo& used_proxy_info,
146 HttpAuthController* auth_controller) {
147 if (!bound_job_.get())
148 BindJob(job);
149 else
150 DCHECK(jobs_.empty());
151 delegate_->OnNeedsProxyAuth(
152 proxy_response, used_ssl_config, used_proxy_info, auth_controller);
155 void HttpStreamFactoryImpl::Request::OnNeedsClientAuth(
156 Job* job,
157 const SSLConfig& used_ssl_config,
158 SSLCertRequestInfo* cert_info) {
159 if (!bound_job_.get())
160 BindJob(job);
161 else
162 DCHECK(jobs_.empty());
163 delegate_->OnNeedsClientAuth(used_ssl_config, cert_info);
166 void HttpStreamFactoryImpl::Request::OnHttpsProxyTunnelResponse(
167 Job *job,
168 const HttpResponseInfo& response_info,
169 const SSLConfig& used_ssl_config,
170 const ProxyInfo& used_proxy_info,
171 HttpStream* stream) {
172 if (!bound_job_.get())
173 BindJob(job);
174 else
175 DCHECK(jobs_.empty());
176 delegate_->OnHttpsProxyTunnelResponse(
177 response_info, used_ssl_config, used_proxy_info, stream);
180 int HttpStreamFactoryImpl::Request::RestartTunnelWithProxyAuth(
181 const AuthCredentials& credentials) {
182 DCHECK(bound_job_.get());
183 return bound_job_->RestartTunnelWithProxyAuth(credentials);
186 void HttpStreamFactoryImpl::Request::SetPriority(RequestPriority priority) {
187 for (std::set<HttpStreamFactoryImpl::Job*>::const_iterator it = jobs_.begin();
188 it != jobs_.end(); ++it) {
189 (*it)->SetPriority(priority);
191 if (bound_job_)
192 bound_job_->SetPriority(priority);
195 LoadState HttpStreamFactoryImpl::Request::GetLoadState() const {
196 if (bound_job_.get())
197 return bound_job_->GetLoadState();
198 DCHECK(!jobs_.empty());
200 // Just pick the first one.
201 return (*jobs_.begin())->GetLoadState();
204 bool HttpStreamFactoryImpl::Request::was_npn_negotiated() const {
205 DCHECK(completed_);
206 return was_npn_negotiated_;
209 NextProto HttpStreamFactoryImpl::Request::protocol_negotiated()
210 const {
211 DCHECK(completed_);
212 return protocol_negotiated_;
215 bool HttpStreamFactoryImpl::Request::using_spdy() const {
216 DCHECK(completed_);
217 return using_spdy_;
220 const ConnectionAttempts& HttpStreamFactoryImpl::Request::connection_attempts()
221 const {
222 return connection_attempts_;
225 void
226 HttpStreamFactoryImpl::Request::RemoveRequestFromSpdySessionRequestMap() {
227 if (spdy_session_key_.get()) {
228 SpdySessionRequestMap& spdy_session_request_map =
229 factory_->spdy_session_request_map_;
230 DCHECK(ContainsKey(spdy_session_request_map, *spdy_session_key_));
231 RequestSet& request_set =
232 spdy_session_request_map[*spdy_session_key_];
233 DCHECK(ContainsKey(request_set, this));
234 request_set.erase(this);
235 if (request_set.empty())
236 spdy_session_request_map.erase(*spdy_session_key_);
237 spdy_session_key_.reset();
241 bool HttpStreamFactoryImpl::Request::HasSpdySessionKey() const {
242 return spdy_session_key_.get() != NULL;
245 // TODO(jgraettinger): Currently, HttpStreamFactoryImpl::Job notifies a
246 // Request that the session is ready, which in turn notifies it's delegate,
247 // and then it notifies HttpStreamFactoryImpl so that /other/ requests may
248 // be woken, but only if the spdy_session is still okay. This is tough to grok.
249 // Instead, see if Job can notify HttpStreamFactoryImpl only, and have one
250 // path for notifying any requests waiting for the session (including the
251 // request which spawned it).
252 void HttpStreamFactoryImpl::Request::OnNewSpdySessionReady(
253 Job* job,
254 scoped_ptr<HttpStream> stream,
255 const base::WeakPtr<SpdySession>& spdy_session,
256 bool direct) {
257 DCHECK(job);
258 DCHECK(job->using_spdy());
260 // Note: |spdy_session| may be NULL. In that case, |delegate_| should still
261 // receive |stream| so the error propogates up correctly, however there is no
262 // point in broadcasting |spdy_session| to other requests.
264 // The first case is the usual case.
265 if (!bound_job_.get()) {
266 BindJob(job);
267 } else { // This is the case for HTTPS proxy tunneling.
268 DCHECK_EQ(bound_job_.get(), job);
269 DCHECK(jobs_.empty());
272 // Cache these values in case the job gets deleted.
273 const SSLConfig used_ssl_config = job->server_ssl_config();
274 const ProxyInfo used_proxy_info = job->proxy_info();
275 const bool was_npn_negotiated = job->was_npn_negotiated();
276 const NextProto protocol_negotiated =
277 job->protocol_negotiated();
278 const bool using_spdy = job->using_spdy();
279 const BoundNetLog net_log = job->net_log();
281 Complete(was_npn_negotiated, protocol_negotiated, using_spdy);
283 // Cache this so we can still use it if the request is deleted.
284 HttpStreamFactoryImpl* factory = factory_;
285 if (factory->for_websockets_) {
286 // TODO(ricea): Re-instate this code when WebSockets over SPDY is
287 // implemented.
288 NOTREACHED();
289 } else {
290 delegate_->OnStreamReady(job->server_ssl_config(), job->proxy_info(),
291 stream.release());
293 // |this| may be deleted after this point.
294 if (spdy_session && spdy_session->IsAvailable()) {
295 factory->OnNewSpdySessionReady(spdy_session,
296 direct,
297 used_ssl_config,
298 used_proxy_info,
299 was_npn_negotiated,
300 protocol_negotiated,
301 using_spdy,
302 net_log);
306 void HttpStreamFactoryImpl::Request::AddConnectionAttempts(
307 const ConnectionAttempts& attempts) {
308 for (const auto& attempt : attempts)
309 connection_attempts_.push_back(attempt);
312 void HttpStreamFactoryImpl::Request::BindJob(Job* job) {
313 DCHECK(job);
314 DCHECK(!bound_job_.get());
315 DCHECK(ContainsKey(jobs_, job));
316 bound_job_.reset(job);
317 jobs_.erase(job);
318 factory_->request_map_.erase(job);
320 net_log_.AddEvent(NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_JOB,
321 job->net_log().source().ToEventParametersCallback());
322 job->net_log().AddEvent(NetLog::TYPE_HTTP_STREAM_JOB_BOUND_TO_REQUEST,
323 net_log_.source().ToEventParametersCallback());
325 OrphanJobs();
328 void HttpStreamFactoryImpl::Request::OrphanJobs() {
329 RemoveRequestFromSpdySessionRequestMap();
331 std::set<Job*> tmp;
332 tmp.swap(jobs_);
334 for (Job* job : tmp)
335 factory_->OrphanJob(job, this);
338 void HttpStreamFactoryImpl::Request::CancelJobs() {
339 RemoveRequestFromSpdySessionRequestMap();
341 std::set<Job*> tmp;
342 tmp.swap(jobs_);
344 for (Job* job : tmp) {
345 factory_->request_map_.erase(job);
346 delete job;
350 void HttpStreamFactoryImpl::Request::OnJobSucceeded(Job* job) {
351 // |job| should only be NULL if we're being serviced by a late bound
352 // SpdySession (one that was not created by a job in our |jobs_| set).
353 if (!job) {
354 DCHECK(!bound_job_.get());
355 DCHECK(!jobs_.empty());
356 // NOTE(willchan): We do *NOT* call OrphanJobs() here. The reason is because
357 // we *WANT* to cancel the unnecessary Jobs from other requests if another
358 // Job completes first.
359 // TODO(mbelshe): Revisit this when we implement ip connection pooling of
360 // SpdySessions. Do we want to orphan the jobs for a different hostname so
361 // they complete? Or do we want to prevent connecting a new SpdySession if
362 // we've already got one available for a different hostname where the ip
363 // address matches up?
364 CancelJobs();
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 BindJob(job);
379 return;
381 DCHECK(jobs_.empty());
384 } // namespace net