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

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

Issue 1941083002: JobController 1: Adding a new class HttpStreamFactoryImpl::JobController (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add JobFactory interface in JobController, remove JobControllerPeer 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 HttpStreamFactoryImpl* factory, 19 Helper* helper,
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 factory_(factory), 26 helper_(helper),
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_);
37 DCHECK(delegate_); 36 DCHECK(delegate_);
38 37
39 net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_REQUEST); 38 net_log_.BeginEvent(NetLog::TYPE_HTTP_STREAM_REQUEST);
40 } 39 }
41 40
42 HttpStreamFactoryImpl::Request::~Request() { 41 HttpStreamFactoryImpl::Request::~Request() {
43 if (bound_job_.get())
44 DCHECK(jobs_.empty());
45
46 net_log_.EndEvent(NetLog::TYPE_HTTP_STREAM_REQUEST); 42 net_log_.EndEvent(NetLog::TYPE_HTTP_STREAM_REQUEST);
47 43 helper_->OnRequestComplete();
48 CancelJobs();
49 } 44 }
50 45
51 void HttpStreamFactoryImpl::Request::SetSpdySessionKey( 46 void HttpStreamFactoryImpl::Request::SetSpdySessionKey(
52 const SpdySessionKey& spdy_session_key) { 47 const SpdySessionKey& spdy_session_key) {
53 CHECK(!spdy_session_key_.get());
54 spdy_session_key_.reset(new SpdySessionKey(spdy_session_key)); 48 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;
65 } 49 }
66 50
67 void HttpStreamFactoryImpl::Request::Complete(bool was_npn_negotiated, 51 void HttpStreamFactoryImpl::Request::Complete(bool was_npn_negotiated,
68 NextProto protocol_negotiated, 52 NextProto protocol_negotiated,
69 bool using_spdy) { 53 bool using_spdy) {
70 DCHECK(!completed_); 54 DCHECK(!completed_);
71 completed_ = true; 55 completed_ = true;
72 was_npn_negotiated_ = was_npn_negotiated; 56 was_npn_negotiated_ = was_npn_negotiated;
73 protocol_negotiated_ = protocol_negotiated; 57 protocol_negotiated_ = protocol_negotiated;
74 using_spdy_ = using_spdy; 58 using_spdy_ = using_spdy;
75 } 59 }
76 60
77 void HttpStreamFactoryImpl::Request::OnStreamReady( 61 void HttpStreamFactoryImpl::Request::OnStreamReady(
78 Job* job,
79 const SSLConfig& used_ssl_config, 62 const SSLConfig& used_ssl_config,
80 const ProxyInfo& used_proxy_info, 63 const ProxyInfo& used_proxy_info,
81 HttpStream* stream) { 64 HttpStream* stream) {
82 DCHECK(!factory_->for_websockets_);
83 DCHECK_EQ(HttpStreamRequest::HTTP_STREAM, stream_type_);
84 DCHECK(stream);
85 DCHECK(completed_); 65 DCHECK(completed_);
86
87 OnJobSucceeded(job);
88 delegate_->OnStreamReady(used_ssl_config, used_proxy_info, stream); 66 delegate_->OnStreamReady(used_ssl_config, used_proxy_info, stream);
89 } 67 }
90 68
91 void HttpStreamFactoryImpl::Request::OnBidirectionalStreamImplReady( 69 void HttpStreamFactoryImpl::Request::OnBidirectionalStreamImplReady(
92 Job* job,
93 const SSLConfig& used_ssl_config, 70 const SSLConfig& used_ssl_config,
94 const ProxyInfo& used_proxy_info, 71 const ProxyInfo& used_proxy_info,
95 BidirectionalStreamImpl* stream_job) { 72 BidirectionalStreamImpl* stream_job) {
96 DCHECK(!factory_->for_websockets_);
97 DCHECK_EQ(HttpStreamRequest::BIDIRECTIONAL_STREAM, stream_type_);
98 DCHECK(stream_job);
99 DCHECK(completed_); 73 DCHECK(completed_);
100
101 OnJobSucceeded(job);
102 delegate_->OnBidirectionalStreamImplReady(used_ssl_config, used_proxy_info, 74 delegate_->OnBidirectionalStreamImplReady(used_ssl_config, used_proxy_info,
103 stream_job); 75 stream_job);
104 } 76 }
105 77
106 void HttpStreamFactoryImpl::Request::OnWebSocketHandshakeStreamReady( 78 void HttpStreamFactoryImpl::Request::OnWebSocketHandshakeStreamReady(
107 Job* job,
108 const SSLConfig& used_ssl_config, 79 const SSLConfig& used_ssl_config,
109 const ProxyInfo& used_proxy_info, 80 const ProxyInfo& used_proxy_info,
110 WebSocketHandshakeStreamBase* stream) { 81 WebSocketHandshakeStreamBase* stream) {
111 DCHECK(factory_->for_websockets_);
112 DCHECK_EQ(HttpStreamRequest::HTTP_STREAM, stream_type_);
113 DCHECK(stream);
114 DCHECK(completed_); 82 DCHECK(completed_);
115
116 OnJobSucceeded(job);
117 delegate_->OnWebSocketHandshakeStreamReady( 83 delegate_->OnWebSocketHandshakeStreamReady(
118 used_ssl_config, used_proxy_info, stream); 84 used_ssl_config, used_proxy_info, stream);
119 } 85 }
120 86
121 void HttpStreamFactoryImpl::Request::OnStreamFailed( 87 void HttpStreamFactoryImpl::Request::OnStreamFailed(
122 Job* job,
123 int status, 88 int status,
124 const SSLConfig& used_ssl_config, 89 const SSLConfig& used_ssl_config,
125 SSLFailureState ssl_failure_state) { 90 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 }
145 delegate_->OnStreamFailed(status, used_ssl_config, ssl_failure_state); 91 delegate_->OnStreamFailed(status, used_ssl_config, ssl_failure_state);
146 } 92 }
147 93
148 void HttpStreamFactoryImpl::Request::OnCertificateError( 94 void HttpStreamFactoryImpl::Request::OnCertificateError(
149 Job* job,
150 int status, 95 int status,
151 const SSLConfig& used_ssl_config, 96 const SSLConfig& used_ssl_config,
152 const SSLInfo& ssl_info) { 97 const SSLInfo& ssl_info) {
153 DCHECK_NE(OK, status);
154 if (!bound_job_.get())
155 BindJob(job);
156 else
157 DCHECK(jobs_.empty());
158 delegate_->OnCertificateError(status, used_ssl_config, ssl_info); 98 delegate_->OnCertificateError(status, used_ssl_config, ssl_info);
159 } 99 }
160 100
161 void HttpStreamFactoryImpl::Request::OnNeedsProxyAuth( 101 void HttpStreamFactoryImpl::Request::OnNeedsProxyAuth(
162 Job* job,
163 const HttpResponseInfo& proxy_response, 102 const HttpResponseInfo& proxy_response,
164 const SSLConfig& used_ssl_config, 103 const SSLConfig& used_ssl_config,
165 const ProxyInfo& used_proxy_info, 104 const ProxyInfo& used_proxy_info,
166 HttpAuthController* auth_controller) { 105 HttpAuthController* auth_controller) {
167 if (!bound_job_.get())
168 BindJob(job);
169 else
170 DCHECK(jobs_.empty());
171 delegate_->OnNeedsProxyAuth( 106 delegate_->OnNeedsProxyAuth(
172 proxy_response, used_ssl_config, used_proxy_info, auth_controller); 107 proxy_response, used_ssl_config, used_proxy_info, auth_controller);
173 } 108 }
174 109
175 void HttpStreamFactoryImpl::Request::OnNeedsClientAuth( 110 void HttpStreamFactoryImpl::Request::OnNeedsClientAuth(
176 Job* job,
177 const SSLConfig& used_ssl_config, 111 const SSLConfig& used_ssl_config,
178 SSLCertRequestInfo* cert_info) { 112 SSLCertRequestInfo* cert_info) {
179 if (!bound_job_.get())
180 BindJob(job);
181 else
182 DCHECK(jobs_.empty());
183 delegate_->OnNeedsClientAuth(used_ssl_config, cert_info); 113 delegate_->OnNeedsClientAuth(used_ssl_config, cert_info);
184 } 114 }
185 115
186 void HttpStreamFactoryImpl::Request::OnHttpsProxyTunnelResponse( 116 void HttpStreamFactoryImpl::Request::OnHttpsProxyTunnelResponse(
187 Job *job,
188 const HttpResponseInfo& response_info, 117 const HttpResponseInfo& response_info,
189 const SSLConfig& used_ssl_config, 118 const SSLConfig& used_ssl_config,
190 const ProxyInfo& used_proxy_info, 119 const ProxyInfo& used_proxy_info,
191 HttpStream* stream) { 120 HttpStream* stream) {
192 if (!bound_job_.get())
193 BindJob(job);
194 else
195 DCHECK(jobs_.empty());
196 delegate_->OnHttpsProxyTunnelResponse( 121 delegate_->OnHttpsProxyTunnelResponse(
197 response_info, used_ssl_config, used_proxy_info, stream); 122 response_info, used_ssl_config, used_proxy_info, stream);
198 } 123 }
199 124
200 int HttpStreamFactoryImpl::Request::RestartTunnelWithProxyAuth( 125 int HttpStreamFactoryImpl::Request::RestartTunnelWithProxyAuth(
201 const AuthCredentials& credentials) { 126 const AuthCredentials& credentials) {
202 DCHECK(bound_job_.get()); 127 return helper_->RestartTunnelWithProxyAuth(credentials);
203 return bound_job_->RestartTunnelWithProxyAuth(credentials);
204 } 128 }
205 129
206 void HttpStreamFactoryImpl::Request::SetPriority(RequestPriority priority) { 130 void HttpStreamFactoryImpl::Request::SetPriority(RequestPriority priority) {
207 for (std::set<HttpStreamFactoryImpl::Job*>::const_iterator it = jobs_.begin(); 131 helper_->SetPriority(priority);
208 it != jobs_.end(); ++it) {
209 (*it)->SetPriority(priority);
210 }
211 if (bound_job_)
212 bound_job_->SetPriority(priority);
213 } 132 }
214 133
215 LoadState HttpStreamFactoryImpl::Request::GetLoadState() const { 134 LoadState HttpStreamFactoryImpl::Request::GetLoadState() const {
216 if (bound_job_.get()) 135 return helper_->GetLoadState();
217 return bound_job_->GetLoadState();
218 DCHECK(!jobs_.empty());
219
220 // Just pick the first one.
221 return (*jobs_.begin())->GetLoadState();
222 } 136 }
223 137
224 bool HttpStreamFactoryImpl::Request::was_npn_negotiated() const { 138 bool HttpStreamFactoryImpl::Request::was_npn_negotiated() const {
225 DCHECK(completed_); 139 DCHECK(completed_);
226 return was_npn_negotiated_; 140 return was_npn_negotiated_;
227 } 141 }
228 142
229 NextProto HttpStreamFactoryImpl::Request::protocol_negotiated() 143 NextProto HttpStreamFactoryImpl::Request::protocol_negotiated()
230 const { 144 const {
231 DCHECK(completed_); 145 DCHECK(completed_);
232 return protocol_negotiated_; 146 return protocol_negotiated_;
233 } 147 }
234 148
235 bool HttpStreamFactoryImpl::Request::using_spdy() const { 149 bool HttpStreamFactoryImpl::Request::using_spdy() const {
236 DCHECK(completed_); 150 DCHECK(completed_);
237 return using_spdy_; 151 return using_spdy_;
238 } 152 }
239 153
240 const ConnectionAttempts& HttpStreamFactoryImpl::Request::connection_attempts() 154 const ConnectionAttempts& HttpStreamFactoryImpl::Request::connection_attempts()
241 const { 155 const {
242 return connection_attempts_; 156 return connection_attempts_;
243 } 157 }
244 158
245 void 159 void HttpStreamFactoryImpl::Request::ResetSpdySessionKey() {
246 HttpStreamFactoryImpl::Request::RemoveRequestFromSpdySessionRequestMap() {
247 if (spdy_session_key_.get()) { 160 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_);
257 spdy_session_key_.reset(); 161 spdy_session_key_.reset();
258 } 162 }
259 } 163 }
260 164
261 bool HttpStreamFactoryImpl::Request::HasSpdySessionKey() const { 165 bool HttpStreamFactoryImpl::Request::HasSpdySessionKey() const {
262 return spdy_session_key_.get() != NULL; 166 return spdy_session_key_.get() != NULL;
263 } 167 }
264 168
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
335 void HttpStreamFactoryImpl::Request::AddConnectionAttempts( 169 void HttpStreamFactoryImpl::Request::AddConnectionAttempts(
336 const ConnectionAttempts& attempts) { 170 const ConnectionAttempts& attempts) {
337 for (const auto& attempt : attempts) 171 for (const auto& attempt : attempts)
338 connection_attempts_.push_back(attempt); 172 connection_attempts_.push_back(attempt);
339 } 173 }
340 174
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
413 } // namespace net 175 } // namespace net
Ryan Hamilton 2016/06/06 17:57:31 Wow, this file is SO much cleaner.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698