Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(277)

Side by Side Diff: net/http/http_stream_factory_impl_request.cc

Issue 2073293002: Revert of JobController 1: Remove cross reference between Request, Job, and Impl (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/http/http_stream_factory_impl_request.h" 5 #include "net/http/http_stream_factory_impl_request.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "net/http/bidirectional_stream_impl.h" 10 #include "net/http/bidirectional_stream_impl.h"
11 #include "net/http/http_stream_factory_impl_job.h" 11 #include "net/http/http_stream_factory_impl_job.h"
12 #include "net/spdy/spdy_http_stream.h" 12 #include "net/spdy/spdy_http_stream.h"
13 #include "net/spdy/spdy_session.h" 13 #include "net/spdy/spdy_session.h"
14 14
15 namespace net { 15 namespace net {
16 16
17 HttpStreamFactoryImpl::Request::Request( 17 HttpStreamFactoryImpl::Request::Request(
18 const GURL& url, 18 const GURL& url,
19 Helper* helper, 19 HttpStreamFactoryImpl* factory,
20 HttpStreamRequest::Delegate* delegate, 20 HttpStreamRequest::Delegate* delegate,
21 WebSocketHandshakeStreamBase::CreateHelper* 21 WebSocketHandshakeStreamBase::CreateHelper*
22 websocket_handshake_stream_create_helper, 22 websocket_handshake_stream_create_helper,
23 const BoundNetLog& net_log, 23 const BoundNetLog& net_log,
24 StreamType stream_type) 24 StreamType stream_type)
25 : url_(url), 25 : url_(url),
26 helper_(helper), 26 factory_(factory),
27 websocket_handshake_stream_create_helper_( 27 websocket_handshake_stream_create_helper_(
28 websocket_handshake_stream_create_helper), 28 websocket_handshake_stream_create_helper),
29 delegate_(delegate), 29 delegate_(delegate),
30 net_log_(net_log), 30 net_log_(net_log),
31 completed_(false), 31 completed_(false),
32 was_npn_negotiated_(false), 32 was_npn_negotiated_(false),
33 protocol_negotiated_(kProtoUnknown), 33 protocol_negotiated_(kProtoUnknown),
34 using_spdy_(false), 34 using_spdy_(false),
35 stream_type_(stream_type) { 35 stream_type_(stream_type) {
36 DCHECK(factory_);
36 DCHECK(delegate_); 37 DCHECK(delegate_);
37 38
38 net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_REQUEST); 39 net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_REQUEST);
39 } 40 }
40 41
41 HttpStreamFactoryImpl::Request::~Request() { 42 HttpStreamFactoryImpl::Request::~Request() {
43 if (bound_job_.get())
44 DCHECK(jobs_.empty());
45
42 net_log_.EndEvent(NetLog::TYPE_HTTP_STREAM_REQUEST); 46 net_log_.EndEvent(NetLog::TYPE_HTTP_STREAM_REQUEST);
43 helper_->OnRequestComplete(); 47
48 CancelJobs();
44 } 49 }
45 50
46 void HttpStreamFactoryImpl::Request::SetSpdySessionKey( 51 void HttpStreamFactoryImpl::Request::SetSpdySessionKey(
47 const SpdySessionKey& spdy_session_key) { 52 const SpdySessionKey& spdy_session_key) {
53 CHECK(!spdy_session_key_.get());
48 spdy_session_key_.reset(new SpdySessionKey(spdy_session_key)); 54 spdy_session_key_.reset(new SpdySessionKey(spdy_session_key));
55 RequestSet& request_set =
56 factory_->spdy_session_request_map_[spdy_session_key];
57 DCHECK(!ContainsKey(request_set, this));
58 request_set.insert(this);
59 }
60
61 void HttpStreamFactoryImpl::Request::AttachJob(Job* job) {
62 DCHECK(job);
63 jobs_.insert(job);
64 factory_->request_map_[job] = this;
49 } 65 }
50 66
51 void HttpStreamFactoryImpl::Request::Complete(bool was_npn_negotiated, 67 void HttpStreamFactoryImpl::Request::Complete(bool was_npn_negotiated,
52 NextProto protocol_negotiated, 68 NextProto protocol_negotiated,
53 bool using_spdy) { 69 bool using_spdy) {
54 DCHECK(!completed_); 70 DCHECK(!completed_);
55 completed_ = true; 71 completed_ = true;
56 was_npn_negotiated_ = was_npn_negotiated; 72 was_npn_negotiated_ = was_npn_negotiated;
57 protocol_negotiated_ = protocol_negotiated; 73 protocol_negotiated_ = protocol_negotiated;
58 using_spdy_ = using_spdy; 74 using_spdy_ = using_spdy;
59 } 75 }
60 76
61 void HttpStreamFactoryImpl::Request::OnStreamReady( 77 void HttpStreamFactoryImpl::Request::OnStreamReady(
78 Job* job,
62 const SSLConfig& used_ssl_config, 79 const SSLConfig& used_ssl_config,
63 const ProxyInfo& used_proxy_info, 80 const ProxyInfo& used_proxy_info,
64 HttpStream* stream) { 81 HttpStream* stream) {
82 DCHECK(!factory_->for_websockets_);
83 DCHECK_EQ(HttpStreamRequest::HTTP_STREAM, stream_type_);
84 DCHECK(stream);
65 DCHECK(completed_); 85 DCHECK(completed_);
86
87 OnJobSucceeded(job);
66 delegate_->OnStreamReady(used_ssl_config, used_proxy_info, stream); 88 delegate_->OnStreamReady(used_ssl_config, used_proxy_info, stream);
67 } 89 }
68 90
69 void HttpStreamFactoryImpl::Request::OnBidirectionalStreamImplReady( 91 void HttpStreamFactoryImpl::Request::OnBidirectionalStreamImplReady(
92 Job* job,
70 const SSLConfig& used_ssl_config, 93 const SSLConfig& used_ssl_config,
71 const ProxyInfo& used_proxy_info, 94 const ProxyInfo& used_proxy_info,
72 BidirectionalStreamImpl* stream_job) { 95 BidirectionalStreamImpl* stream_job) {
96 DCHECK(!factory_->for_websockets_);
97 DCHECK_EQ(HttpStreamRequest::BIDIRECTIONAL_STREAM, stream_type_);
98 DCHECK(stream_job);
73 DCHECK(completed_); 99 DCHECK(completed_);
100
101 OnJobSucceeded(job);
74 delegate_->OnBidirectionalStreamImplReady(used_ssl_config, used_proxy_info, 102 delegate_->OnBidirectionalStreamImplReady(used_ssl_config, used_proxy_info,
75 stream_job); 103 stream_job);
76 } 104 }
77 105
78 void HttpStreamFactoryImpl::Request::OnWebSocketHandshakeStreamReady( 106 void HttpStreamFactoryImpl::Request::OnWebSocketHandshakeStreamReady(
107 Job* job,
79 const SSLConfig& used_ssl_config, 108 const SSLConfig& used_ssl_config,
80 const ProxyInfo& used_proxy_info, 109 const ProxyInfo& used_proxy_info,
81 WebSocketHandshakeStreamBase* stream) { 110 WebSocketHandshakeStreamBase* stream) {
111 DCHECK(factory_->for_websockets_);
112 DCHECK_EQ(HttpStreamRequest::HTTP_STREAM, stream_type_);
113 DCHECK(stream);
82 DCHECK(completed_); 114 DCHECK(completed_);
115
116 OnJobSucceeded(job);
83 delegate_->OnWebSocketHandshakeStreamReady( 117 delegate_->OnWebSocketHandshakeStreamReady(
84 used_ssl_config, used_proxy_info, stream); 118 used_ssl_config, used_proxy_info, stream);
85 } 119 }
86 120
87 void HttpStreamFactoryImpl::Request::OnStreamFailed( 121 void HttpStreamFactoryImpl::Request::OnStreamFailed(
122 Job* job,
88 int status, 123 int status,
89 const SSLConfig& used_ssl_config, 124 const SSLConfig& used_ssl_config,
90 SSLFailureState ssl_failure_state) { 125 SSLFailureState ssl_failure_state) {
126 DCHECK_NE(OK, status);
127 DCHECK(job);
128 if (!bound_job_.get()) {
129 if (jobs_.size() > 1) {
130 // Hey, we've got other jobs! Maybe one of them will succeed, let's just
131 // ignore this failure.
132 jobs_.erase(job);
133 factory_->request_map_.erase(job);
134 // Notify all the other jobs that this one failed.
135 for (std::set<Job*>::iterator it = jobs_.begin(); it != jobs_.end(); ++it)
136 (*it)->MarkOtherJobComplete(*job);
137 delete job;
138 return;
139 } else {
140 BindJob(job);
141 }
142 } else {
143 DCHECK(jobs_.empty());
144 }
91 delegate_->OnStreamFailed(status, used_ssl_config, ssl_failure_state); 145 delegate_->OnStreamFailed(status, used_ssl_config, ssl_failure_state);
92 } 146 }
93 147
94 void HttpStreamFactoryImpl::Request::OnCertificateError( 148 void HttpStreamFactoryImpl::Request::OnCertificateError(
149 Job* job,
95 int status, 150 int status,
96 const SSLConfig& used_ssl_config, 151 const SSLConfig& used_ssl_config,
97 const SSLInfo& ssl_info) { 152 const SSLInfo& ssl_info) {
153 DCHECK_NE(OK, status);
154 if (!bound_job_.get())
155 BindJob(job);
156 else
157 DCHECK(jobs_.empty());
98 delegate_->OnCertificateError(status, used_ssl_config, ssl_info); 158 delegate_->OnCertificateError(status, used_ssl_config, ssl_info);
99 } 159 }
100 160
101 void HttpStreamFactoryImpl::Request::OnNeedsProxyAuth( 161 void HttpStreamFactoryImpl::Request::OnNeedsProxyAuth(
162 Job* job,
102 const HttpResponseInfo& proxy_response, 163 const HttpResponseInfo& proxy_response,
103 const SSLConfig& used_ssl_config, 164 const SSLConfig& used_ssl_config,
104 const ProxyInfo& used_proxy_info, 165 const ProxyInfo& used_proxy_info,
105 HttpAuthController* auth_controller) { 166 HttpAuthController* auth_controller) {
167 if (!bound_job_.get())
168 BindJob(job);
169 else
170 DCHECK(jobs_.empty());
106 delegate_->OnNeedsProxyAuth( 171 delegate_->OnNeedsProxyAuth(
107 proxy_response, used_ssl_config, used_proxy_info, auth_controller); 172 proxy_response, used_ssl_config, used_proxy_info, auth_controller);
108 } 173 }
109 174
110 void HttpStreamFactoryImpl::Request::OnNeedsClientAuth( 175 void HttpStreamFactoryImpl::Request::OnNeedsClientAuth(
176 Job* job,
111 const SSLConfig& used_ssl_config, 177 const SSLConfig& used_ssl_config,
112 SSLCertRequestInfo* cert_info) { 178 SSLCertRequestInfo* cert_info) {
179 if (!bound_job_.get())
180 BindJob(job);
181 else
182 DCHECK(jobs_.empty());
113 delegate_->OnNeedsClientAuth(used_ssl_config, cert_info); 183 delegate_->OnNeedsClientAuth(used_ssl_config, cert_info);
114 } 184 }
115 185
116 void HttpStreamFactoryImpl::Request::OnHttpsProxyTunnelResponse( 186 void HttpStreamFactoryImpl::Request::OnHttpsProxyTunnelResponse(
187 Job *job,
117 const HttpResponseInfo& response_info, 188 const HttpResponseInfo& response_info,
118 const SSLConfig& used_ssl_config, 189 const SSLConfig& used_ssl_config,
119 const ProxyInfo& used_proxy_info, 190 const ProxyInfo& used_proxy_info,
120 HttpStream* stream) { 191 HttpStream* stream) {
192 if (!bound_job_.get())
193 BindJob(job);
194 else
195 DCHECK(jobs_.empty());
121 delegate_->OnHttpsProxyTunnelResponse( 196 delegate_->OnHttpsProxyTunnelResponse(
122 response_info, used_ssl_config, used_proxy_info, stream); 197 response_info, used_ssl_config, used_proxy_info, stream);
123 } 198 }
124 199
125 int HttpStreamFactoryImpl::Request::RestartTunnelWithProxyAuth( 200 int HttpStreamFactoryImpl::Request::RestartTunnelWithProxyAuth(
126 const AuthCredentials& credentials) { 201 const AuthCredentials& credentials) {
127 return helper_->RestartTunnelWithProxyAuth(credentials); 202 DCHECK(bound_job_.get());
203 return bound_job_->RestartTunnelWithProxyAuth(credentials);
128 } 204 }
129 205
130 void HttpStreamFactoryImpl::Request::SetPriority(RequestPriority priority) { 206 void HttpStreamFactoryImpl::Request::SetPriority(RequestPriority priority) {
131 helper_->SetPriority(priority); 207 for (std::set<HttpStreamFactoryImpl::Job*>::const_iterator it = jobs_.begin();
208 it != jobs_.end(); ++it) {
209 (*it)->SetPriority(priority);
210 }
211 if (bound_job_)
212 bound_job_->SetPriority(priority);
132 } 213 }
133 214
134 LoadState HttpStreamFactoryImpl::Request::GetLoadState() const { 215 LoadState HttpStreamFactoryImpl::Request::GetLoadState() const {
135 return helper_->GetLoadState(); 216 if (bound_job_.get())
217 return bound_job_->GetLoadState();
218 DCHECK(!jobs_.empty());
219
220 // Just pick the first one.
221 return (*jobs_.begin())->GetLoadState();
136 } 222 }
137 223
138 bool HttpStreamFactoryImpl::Request::was_npn_negotiated() const { 224 bool HttpStreamFactoryImpl::Request::was_npn_negotiated() const {
139 DCHECK(completed_); 225 DCHECK(completed_);
140 return was_npn_negotiated_; 226 return was_npn_negotiated_;
141 } 227 }
142 228
143 NextProto HttpStreamFactoryImpl::Request::protocol_negotiated() 229 NextProto HttpStreamFactoryImpl::Request::protocol_negotiated()
144 const { 230 const {
145 DCHECK(completed_); 231 DCHECK(completed_);
146 return protocol_negotiated_; 232 return protocol_negotiated_;
147 } 233 }
148 234
149 bool HttpStreamFactoryImpl::Request::using_spdy() const { 235 bool HttpStreamFactoryImpl::Request::using_spdy() const {
150 DCHECK(completed_); 236 DCHECK(completed_);
151 return using_spdy_; 237 return using_spdy_;
152 } 238 }
153 239
154 const ConnectionAttempts& HttpStreamFactoryImpl::Request::connection_attempts() 240 const ConnectionAttempts& HttpStreamFactoryImpl::Request::connection_attempts()
155 const { 241 const {
156 return connection_attempts_; 242 return connection_attempts_;
157 } 243 }
158 244
159 void HttpStreamFactoryImpl::Request::ResetSpdySessionKey() { 245 void
246 HttpStreamFactoryImpl::Request::RemoveRequestFromSpdySessionRequestMap() {
160 if (spdy_session_key_.get()) { 247 if (spdy_session_key_.get()) {
248 SpdySessionRequestMap& spdy_session_request_map =
249 factory_->spdy_session_request_map_;
250 DCHECK(ContainsKey(spdy_session_request_map, *spdy_session_key_));
251 RequestSet& request_set =
252 spdy_session_request_map[*spdy_session_key_];
253 DCHECK(ContainsKey(request_set, this));
254 request_set.erase(this);
255 if (request_set.empty())
256 spdy_session_request_map.erase(*spdy_session_key_);
161 spdy_session_key_.reset(); 257 spdy_session_key_.reset();
162 } 258 }
163 } 259 }
164 260
165 bool HttpStreamFactoryImpl::Request::HasSpdySessionKey() const { 261 bool HttpStreamFactoryImpl::Request::HasSpdySessionKey() const {
166 return spdy_session_key_.get() != NULL; 262 return spdy_session_key_.get() != NULL;
167 } 263 }
168 264
265 // TODO(jgraettinger): Currently, HttpStreamFactoryImpl::Job notifies a
266 // Request that the session is ready, which in turn notifies it's delegate,
267 // and then it notifies HttpStreamFactoryImpl so that /other/ requests may
268 // be woken, but only if the spdy_session is still okay. This is tough to grok.
269 // Instead, see if Job can notify HttpStreamFactoryImpl only, and have one
270 // path for notifying any requests waiting for the session (including the
271 // request which spawned it).
272 void HttpStreamFactoryImpl::Request::OnNewSpdySessionReady(
273 Job* job,
274 std::unique_ptr<HttpStream> stream,
275 std::unique_ptr<BidirectionalStreamImpl> bidirectional_stream_impl,
276 const base::WeakPtr<SpdySession>& spdy_session,
277 bool direct) {
278 DCHECK(job);
279 DCHECK(job->using_spdy());
280
281 // Note: |spdy_session| may be NULL. In that case, |delegate_| should still
282 // receive |stream| so the error propagates up correctly, however there is no
283 // point in broadcasting |spdy_session| to other requests.
284
285 // The first case is the usual case.
286 if (!bound_job_.get()) {
287 BindJob(job);
288 } else { // This is the case for HTTPS proxy tunneling.
289 DCHECK_EQ(bound_job_.get(), job);
290 DCHECK(jobs_.empty());
291 }
292
293 // Cache these values in case the job gets deleted.
294 const SSLConfig used_ssl_config = job->server_ssl_config();
295 const ProxyInfo used_proxy_info = job->proxy_info();
296 const bool was_npn_negotiated = job->was_npn_negotiated();
297 const NextProto protocol_negotiated =
298 job->protocol_negotiated();
299 const bool using_spdy = job->using_spdy();
300 const BoundNetLog net_log = job->net_log();
301
302 Complete(was_npn_negotiated, protocol_negotiated, using_spdy);
303
304 // Cache this so we can still use it if the request is deleted.
305 HttpStreamFactoryImpl* factory = factory_;
306 if (factory->for_websockets_) {
307 // TODO(ricea): Re-instate this code when WebSockets over SPDY is
308 // implemented.
309 NOTREACHED();
310 } else if (stream_type_ == HttpStreamRequest::BIDIRECTIONAL_STREAM) {
311 DCHECK(bidirectional_stream_impl);
312 DCHECK(!stream);
313 delegate_->OnBidirectionalStreamImplReady(
314 job->server_ssl_config(), job->proxy_info(),
315 bidirectional_stream_impl.release());
316 } else {
317 DCHECK(!bidirectional_stream_impl);
318 DCHECK(stream);
319 delegate_->OnStreamReady(job->server_ssl_config(), job->proxy_info(),
320 stream.release());
321 }
322 // |this| may be deleted after this point.
323 if (spdy_session && spdy_session->IsAvailable()) {
324 factory->OnNewSpdySessionReady(spdy_session,
325 direct,
326 used_ssl_config,
327 used_proxy_info,
328 was_npn_negotiated,
329 protocol_negotiated,
330 using_spdy,
331 net_log);
332 }
333 }
334
169 void HttpStreamFactoryImpl::Request::AddConnectionAttempts( 335 void HttpStreamFactoryImpl::Request::AddConnectionAttempts(
170 const ConnectionAttempts& attempts) { 336 const ConnectionAttempts& attempts) {
171 for (const auto& attempt : attempts) 337 for (const auto& attempt : attempts)
172 connection_attempts_.push_back(attempt); 338 connection_attempts_.push_back(attempt);
173 } 339 }
174 340
341 void HttpStreamFactoryImpl::Request::BindJob(Job* job) {
342 DCHECK(job);
343 DCHECK(!bound_job_.get());
344 DCHECK(ContainsKey(jobs_, job));
345 bound_job_.reset(job);
346 jobs_.erase(job);
347 factory_->request_map_.erase(job);
348
349 net_log_.AddEvent(NetLog::TYPE_HTTP_STREAM_REQUEST_BOUND_TO_JOB,
350 job->net_log().source().ToEventParametersCallback());
351 job->net_log().AddEvent(NetLog::TYPE_HTTP_STREAM_JOB_BOUND_TO_REQUEST,
352 net_log_.source().ToEventParametersCallback());
353
354 OrphanJobs();
355 }
356
357 void HttpStreamFactoryImpl::Request::OrphanJobs() {
358 RemoveRequestFromSpdySessionRequestMap();
359
360 std::set<Job*> tmp;
361 tmp.swap(jobs_);
362
363 for (Job* job : tmp)
364 factory_->OrphanJob(job, this);
365 }
366
367 void HttpStreamFactoryImpl::Request::CancelJobs() {
368 RemoveRequestFromSpdySessionRequestMap();
369
370 std::set<Job*> tmp;
371 tmp.swap(jobs_);
372
373 for (Job* job : tmp) {
374 factory_->request_map_.erase(job);
375 delete job;
376 }
377 }
378
379 void HttpStreamFactoryImpl::Request::OnJobSucceeded(Job* job) {
380 // |job| should only be NULL if we're being serviced by a late bound
381 // SpdySession (one that was not created by a job in our |jobs_| set).
382 if (!job) {
383 DCHECK(!bound_job_.get());
384 DCHECK(!jobs_.empty());
385 // NOTE(willchan): We do *NOT* call OrphanJobs() here. The reason is because
386 // we *WANT* to cancel the unnecessary Jobs from other requests if another
387 // Job completes first.
388 // TODO(mbelshe): Revisit this when we implement ip connection pooling of
389 // SpdySessions. Do we want to orphan the jobs for a different hostname so
390 // they complete? Or do we want to prevent connecting a new SpdySession if
391 // we've already got one available for a different hostname where the ip
392 // address matches up?
393 CancelJobs();
394 return;
395 }
396 if (!bound_job_.get()) {
397 if (jobs_.size() > 1)
398 job->ReportJobSucceededForRequest();
399 // Notify all the other jobs that this one succeeded.
400 for (std::set<Job*>::iterator it = jobs_.begin(); it != jobs_.end(); ++it) {
401 if (*it != job) {
402 (*it)->MarkOtherJobComplete(*job);
403 }
404 }
405 // We may have other jobs in |jobs_|. For example, if we start multiple jobs
406 // for Alternate-Protocol.
407 BindJob(job);
408 return;
409 }
410 DCHECK(jobs_.empty());
411 }
412
175 } // namespace net 413 } // namespace net
OLDNEW
« no previous file with comments | « net/http/http_stream_factory_impl_request.h ('k') | net/http/http_stream_factory_impl_request_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698