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

Side by Side Diff: content/browser/loader/resource_loader.cc

Issue 2526983002: Refactor ResourceHandler API. (Closed)
Patch Set: Silly merge Created 3 years, 11 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 "content/browser/loader/resource_loader.h" 5 #include "content/browser/loader/resource_loader.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/callback_helpers.h" 9 #include "base/callback_helpers.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/memory/ptr_util.h"
12 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/histogram_macros.h"
13 #include "base/profiler/scoped_tracker.h" 14 #include "base/profiler/scoped_tracker.h"
14 #include "base/single_thread_task_runner.h" 15 #include "base/single_thread_task_runner.h"
15 #include "base/threading/thread_task_runner_handle.h" 16 #include "base/threading/thread_task_runner_handle.h"
16 #include "base/trace_event/trace_event.h" 17 #include "base/trace_event/trace_event.h"
17 #include "content/browser/appcache/appcache_interceptor.h" 18 #include "content/browser/appcache/appcache_interceptor.h"
18 #include "content/browser/child_process_security_policy_impl.h" 19 #include "content/browser/child_process_security_policy_impl.h"
19 #include "content/browser/loader/detachable_resource_handler.h" 20 #include "content/browser/loader/detachable_resource_handler.h"
21 #include "content/browser/loader/resource_controller.h"
20 #include "content/browser/loader/resource_handler.h" 22 #include "content/browser/loader/resource_handler.h"
21 #include "content/browser/loader/resource_loader_delegate.h" 23 #include "content/browser/loader/resource_loader_delegate.h"
22 #include "content/browser/loader/resource_request_info_impl.h" 24 #include "content/browser/loader/resource_request_info_impl.h"
23 #include "content/browser/service_worker/service_worker_request_handler.h" 25 #include "content/browser/service_worker/service_worker_request_handler.h"
24 #include "content/browser/service_worker/service_worker_response_info.h" 26 #include "content/browser/service_worker/service_worker_response_info.h"
25 #include "content/browser/ssl/ssl_client_auth_handler.h" 27 #include "content/browser/ssl/ssl_client_auth_handler.h"
26 #include "content/browser/ssl/ssl_manager.h" 28 #include "content/browser/ssl/ssl_manager.h"
27 #include "content/public/browser/resource_dispatcher_host_login_delegate.h" 29 #include "content/public/browser/resource_dispatcher_host_login_delegate.h"
28 #include "content/public/common/browser_side_navigation_policy.h" 30 #include "content/public/common/browser_side_navigation_policy.h"
29 #include "content/public/common/content_client.h" 31 #include "content/public/common/content_client.h"
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 // We should not have any SSL state. 125 // We should not have any SSL state.
124 DCHECK(!request->ssl_info().cert_status); 126 DCHECK(!request->ssl_info().cert_status);
125 DCHECK_EQ(request->ssl_info().security_bits, -1); 127 DCHECK_EQ(request->ssl_info().security_bits, -1);
126 DCHECK_EQ(request->ssl_info().key_exchange_group, 0); 128 DCHECK_EQ(request->ssl_info().key_exchange_group, 0);
127 DCHECK(!request->ssl_info().connection_status); 129 DCHECK(!request->ssl_info().connection_status);
128 } 130 }
129 } 131 }
130 132
131 } // namespace 133 } // namespace
132 134
135 class ResourceLoader::Controller : public ResourceController {
136 public:
137 explicit Controller(ResourceLoader* resource_loader)
138 : resource_loader_(resource_loader){};
139
140 ~Controller() override {}
Charlie Harrison 2017/01/25 20:23:00 Can we DCHECK(done_) here? I'm nervous about peopl
mmenke 2017/01/25 22:07:59 Unfortunately not, due to out-of-band cancels. :(
141
142 // ResourceController implementation:
143 void Resume() override {
144 DCHECK(!done_);
145 done_ = true;
146 resource_loader_->Resume(true);
147 }
148
149 void Cancel() override {
150 DCHECK(!done_);
151 done_ = true;
152 resource_loader_->Cancel();
153 }
154
155 void CancelAndIgnore() override {
156 DCHECK(!done_);
157 done_ = true;
158 resource_loader_->CancelAndIgnore();
159 }
160
161 void CancelWithError(int error_code) override {
162 DCHECK(!done_);
163 done_ = true;
164 resource_loader_->CancelWithError(error_code);
165 }
166
167 private:
168 ResourceLoader* const resource_loader_;
169
170 // Set to true once one of the ResourceContoller methods has been invoked.
171 bool done_ = false;
172
173 DISALLOW_COPY_AND_ASSIGN(Controller);
174 };
175
176 // Helper class. Sets the stage of a ResourceLoader to DEFERRED_SYNC on
177 // construction, and on destruction does one of the following:
178 // 1) If the ResourceLoader has a deferred stage of DEFERRED_NONE, sets the
Charlie Harrison 2017/01/25 20:23:00 Can the ResourceLoader have deferred stage apart f
mmenke 2017/01/25 22:07:59 Good idea, done. Wonder if it would be cleaner to
179 // ResourceLoader's stage to the stage specified on construction and resumes it.
180 // 2) If the ResourceLoader still has a deferred stage of DEFERRED_SYNC, sets
181 // the ResourceLoader's stage to the stage specified on construction. The
182 // ResourceLoader will be resumed at some point in the future.
183 class ResourceLoader::ScopedDeferral {
184 public:
185 ScopedDeferral(ResourceLoader* resource_loader,
186 ResourceLoader::DeferredStage deferred_stage)
187 : resource_loader_(resource_loader), deferred_stage_(deferred_stage) {
188 resource_loader_->deferred_stage_ = DEFERRED_SYNC;
189 }
190
191 ~ScopedDeferral() {
192 DeferredStage old_deferred_stage = resource_loader_->deferred_stage_;
193 resource_loader_->deferred_stage_ = deferred_stage_;
194 if (old_deferred_stage == DEFERRED_NONE)
195 resource_loader_->Resume(false);
196 }
197
198 private:
199 ResourceLoader* const resource_loader_;
200 const DeferredStage deferred_stage_;
201
202 DISALLOW_COPY_AND_ASSIGN(ScopedDeferral);
203 };
204
133 ResourceLoader::ResourceLoader(std::unique_ptr<net::URLRequest> request, 205 ResourceLoader::ResourceLoader(std::unique_ptr<net::URLRequest> request,
134 std::unique_ptr<ResourceHandler> handler, 206 std::unique_ptr<ResourceHandler> handler,
135 ResourceLoaderDelegate* delegate) 207 ResourceLoaderDelegate* delegate)
136 : deferred_stage_(DEFERRED_NONE), 208 : deferred_stage_(DEFERRED_NONE),
137 request_(std::move(request)), 209 request_(std::move(request)),
138 handler_(std::move(handler)), 210 handler_(std::move(handler)),
139 delegate_(delegate), 211 delegate_(delegate),
140 is_transferring_(false), 212 is_transferring_(false),
141 times_cancelled_before_request_start_(0), 213 times_cancelled_before_request_start_(0),
142 started_request_(false), 214 started_request_(false),
143 times_cancelled_after_request_start_(0), 215 times_cancelled_after_request_start_(0),
144 weak_ptr_factory_(this) { 216 weak_ptr_factory_(this) {
145 request_->set_delegate(this); 217 request_->set_delegate(this);
146 handler_->SetController(this); 218 handler_->SetDelegate(this);
147 } 219 }
148 220
149 ResourceLoader::~ResourceLoader() { 221 ResourceLoader::~ResourceLoader() {
150 if (login_delegate_.get()) 222 if (login_delegate_.get())
151 login_delegate_->OnRequestCancelled(); 223 login_delegate_->OnRequestCancelled();
152 ssl_client_auth_handler_.reset(); 224 ssl_client_auth_handler_.reset();
153 225
154 // Run ResourceHandler destructor before we tear-down the rest of our state 226 // Run ResourceHandler destructor before we tear-down the rest of our state
155 // as the ResourceHandler may want to inspect the URLRequest and other state. 227 // as the ResourceHandler may want to inspect the URLRequest and other state.
156 handler_.reset(); 228 handler_.reset();
157 } 229 }
158 230
159 void ResourceLoader::StartRequest() { 231 void ResourceLoader::StartRequest() {
160 // Give the handler a chance to delay the URLRequest from being started.
161 bool defer_start = false;
162 if (!handler_->OnWillStart(request_->url(), &defer_start)) {
163 Cancel();
164 return;
165 }
166
167 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::StartRequest", this, 232 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::StartRequest", this,
168 TRACE_EVENT_FLAG_FLOW_OUT); 233 TRACE_EVENT_FLAG_FLOW_OUT);
169 if (defer_start) { 234
170 deferred_stage_ = DEFERRED_START; 235 ScopedDeferral scoped_deferral(this, DEFERRED_START);
171 } else { 236 handler_->OnWillStart(request_->url(), base::MakeUnique<Controller>(this));
172 StartRequestInternal();
173 }
174 } 237 }
175 238
176 void ResourceLoader::CancelRequest(bool from_renderer) { 239 void ResourceLoader::CancelRequest(bool from_renderer) {
177 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CancelRequest", this, 240 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CancelRequest", this,
178 TRACE_EVENT_FLAG_FLOW_IN); 241 TRACE_EVENT_FLAG_FLOW_IN);
179 CancelRequestInternal(net::ERR_ABORTED, from_renderer); 242 CancelRequestInternal(net::ERR_ABORTED, from_renderer);
180 } 243 }
181 244
182 void ResourceLoader::CancelAndIgnore() { 245 void ResourceLoader::CancelAndIgnore() {
183 ResourceRequestInfoImpl* info = GetRequestInfo(); 246 ResourceRequestInfoImpl* info = GetRequestInfo();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 } 295 }
233 296
234 ResourceRequestInfoImpl* ResourceLoader::GetRequestInfo() { 297 ResourceRequestInfoImpl* ResourceLoader::GetRequestInfo() {
235 return ResourceRequestInfoImpl::ForRequest(request_.get()); 298 return ResourceRequestInfoImpl::ForRequest(request_.get());
236 } 299 }
237 300
238 void ResourceLoader::ClearLoginDelegate() { 301 void ResourceLoader::ClearLoginDelegate() {
239 login_delegate_ = NULL; 302 login_delegate_ = NULL;
240 } 303 }
241 304
305 void ResourceLoader::OutOfBandCancel(int error_code, bool tell_renderer) {
306 CancelRequestInternal(error_code, !tell_renderer);
307 }
308
242 void ResourceLoader::OnReceivedRedirect(net::URLRequest* unused, 309 void ResourceLoader::OnReceivedRedirect(net::URLRequest* unused,
243 const net::RedirectInfo& redirect_info, 310 const net::RedirectInfo& redirect_info,
244 bool* defer) { 311 bool* defer) {
245 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"), 312 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"),
246 "ResourceLoader::OnReceivedRedirect"); 313 "ResourceLoader::OnReceivedRedirect");
247 DCHECK_EQ(request_.get(), unused); 314 DCHECK_EQ(request_.get(), unused);
248 315
249 DVLOG(1) << "OnReceivedRedirect: " << request_->url().spec(); 316 DVLOG(1) << "OnReceivedRedirect: " << request_->url().spec();
250 DCHECK(request_->status().is_success()); 317 DCHECK(request_->status().is_success());
251 318
(...skipping 12 matching lines...) Expand all
264 331
265 // Tell the renderer that this request was disallowed. 332 // Tell the renderer that this request was disallowed.
266 Cancel(); 333 Cancel();
267 return; 334 return;
268 } 335 }
269 } 336 }
270 337
271 scoped_refptr<ResourceResponse> response = new ResourceResponse(); 338 scoped_refptr<ResourceResponse> response = new ResourceResponse();
272 PopulateResourceResponse(info, request_.get(), response.get()); 339 PopulateResourceResponse(info, request_.get(), response.get());
273 delegate_->DidReceiveRedirect(this, redirect_info.new_url, response.get()); 340 delegate_->DidReceiveRedirect(this, redirect_info.new_url, response.get());
274 if (!handler_->OnRequestRedirected(redirect_info, response.get(), defer)) { 341
275 Cancel(); 342 // Can't used ScopedDeferral here, because on sync completion, need to set
276 } else if (*defer) { 343 // |defer| to false instead of calling back into the URLRequest.
277 deferred_stage_ = DEFERRED_REDIRECT; // Follow redirect when resumed. 344 deferred_stage_ = DEFERRED_SYNC;
278 DCHECK(deferred_redirect_url_.is_empty()); 345 handler_->OnRequestRedirected(redirect_info, response.get(),
279 deferred_redirect_url_ = redirect_info.new_url; 346 base::MakeUnique<Controller>(this));
280 } else if (delegate_->HandleExternalProtocol(this, redirect_info.new_url)) { 347 if (!is_deferred()) {
Charlie Harrison 2017/01/25 20:23:00 optional: I don't think early-return is a more rea
mmenke 2017/01/25 22:08:00 Done.
281 // The request is complete so we can remove it. 348 *defer = false;
282 CancelAndIgnore(); 349 if (delegate_->HandleExternalProtocol(this, redirect_info.new_url))
350 CancelAndIgnore();
283 return; 351 return;
284 } 352 }
353
354 *defer = true;
355 deferred_redirect_url_ = redirect_info.new_url;
356 deferred_stage_ = DEFERRED_REDIRECT;
285 } 357 }
286 358
287 void ResourceLoader::OnAuthRequired(net::URLRequest* unused, 359 void ResourceLoader::OnAuthRequired(net::URLRequest* unused,
288 net::AuthChallengeInfo* auth_info) { 360 net::AuthChallengeInfo* auth_info) {
289 DCHECK_EQ(request_.get(), unused); 361 DCHECK_EQ(request_.get(), unused);
290 362
291 ResourceRequestInfoImpl* info = GetRequestInfo(); 363 ResourceRequestInfoImpl* info = GetRequestInfo();
292 if (info->do_not_prompt_for_login()) { 364 if (info->do_not_prompt_for_login()) {
293 request_->CancelAuth(); 365 request_->CancelAuth();
294 return; 366 return;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 DCHECK_EQ(request_.get(), unused); 409 DCHECK_EQ(request_.get(), unused);
338 410
339 DVLOG(1) << "OnResponseStarted: " << request_->url().spec(); 411 DVLOG(1) << "OnResponseStarted: " << request_->url().spec();
340 412
341 if (!request_->status().is_success()) { 413 if (!request_->status().is_success()) {
342 ResponseCompleted(); 414 ResponseCompleted();
343 return; 415 return;
344 } 416 }
345 417
346 CompleteResponseStarted(); 418 CompleteResponseStarted();
347
348 // If the handler deferred the request, it will resume the request later. If
349 // the request was cancelled, the request will call back into |this| with a
350 // bogus read completed error.
351 if (is_deferred() || !request_->status().is_success())
352 return;
353
354 ReadMore(false); // Read the first chunk.
355 } 419 }
356 420
357 void ResourceLoader::OnReadCompleted(net::URLRequest* unused, int bytes_read) { 421 void ResourceLoader::OnReadCompleted(net::URLRequest* unused, int bytes_read) {
358 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"), 422 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("loading"),
359 "ResourceLoader::OnReadCompleted"); 423 "ResourceLoader::OnReadCompleted");
360 DCHECK_EQ(request_.get(), unused); 424 DCHECK_EQ(request_.get(), unused);
361 DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\"" 425 DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\""
362 << " bytes_read = " << bytes_read; 426 << " bytes_read = " << bytes_read;
363 427
364 // bytes_read == -1 always implies an error. 428 // bytes_read == -1 always implies an error.
365 if (bytes_read == -1 || !request_->status().is_success()) { 429 if (bytes_read == -1 || !request_->status().is_success()) {
366 ResponseCompleted(); 430 ResponseCompleted();
367 return; 431 return;
368 } 432 }
369 433
370 CompleteRead(bytes_read); 434 CompleteRead(bytes_read);
371
372 // If the handler cancelled or deferred the request, do not continue
373 // processing the read. If canceled, either the request will call into |this|
374 // with a bogus read error, or, if the request was completed, a task posted
375 // from ResourceLoader::CancelREquestInternal will run OnResponseCompleted.
376 if (is_deferred() || !request_->status().is_success())
377 return;
378
379 if (bytes_read > 0) {
380 ReadMore(true); // Read the next chunk.
381 } else {
382 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed.
383 tracked_objects::ScopedTracker tracking_profile(
384 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 ResponseCompleted()"));
385
386 // URLRequest reported an EOF. Call ResponseCompleted.
387 DCHECK_EQ(0, bytes_read);
388 ResponseCompleted();
389 }
390 } 435 }
391 436
392 void ResourceLoader::CancelSSLRequest(int error, 437 void ResourceLoader::CancelSSLRequest(int error,
393 const net::SSLInfo* ssl_info) { 438 const net::SSLInfo* ssl_info) {
394 DCHECK_CURRENTLY_ON(BrowserThread::IO); 439 DCHECK_CURRENTLY_ON(BrowserThread::IO);
395 440
396 // The request can be NULL if it was cancelled by the renderer (as the 441 // The request can be NULL if it was cancelled by the renderer (as the
397 // request of the user navigating to a new page from the location bar). 442 // request of the user navigating to a new page from the location bar).
398 if (!request_->is_pending()) 443 if (!request_->is_pending())
399 return; 444 return;
(...skipping 25 matching lines...) Expand all
425 net::FetchClientCertPrivateKey(cert); 470 net::FetchClientCertPrivateKey(cert);
426 request_->ContinueWithCertificate(cert, private_key.get()); 471 request_->ContinueWithCertificate(cert, private_key.get());
427 } 472 }
428 473
429 void ResourceLoader::CancelCertificateSelection() { 474 void ResourceLoader::CancelCertificateSelection() {
430 DCHECK(ssl_client_auth_handler_); 475 DCHECK(ssl_client_auth_handler_);
431 ssl_client_auth_handler_.reset(); 476 ssl_client_auth_handler_.reset();
432 request_->CancelWithError(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); 477 request_->CancelWithError(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED);
433 } 478 }
434 479
435 void ResourceLoader::Resume() { 480 void ResourceLoader::Resume(bool called_from_resource_controller) {
436 DCHECK(!is_transferring_); 481 DCHECK(!is_transferring_);
437 482
438 DeferredStage stage = deferred_stage_; 483 DeferredStage stage = deferred_stage_;
439 deferred_stage_ = DEFERRED_NONE; 484 deferred_stage_ = DEFERRED_NONE;
440 switch (stage) { 485 switch (stage) {
441 case DEFERRED_NONE: 486 case DEFERRED_NONE:
442 NOTREACHED(); 487 NOTREACHED();
443 break; 488 break;
489 case DEFERRED_SYNC:
490 DCHECK(called_from_resource_controller);
491 // Request will be resumed when the stack unwinds.
492 break;
444 case DEFERRED_START: 493 case DEFERRED_START:
494 // Starting requests completes asynchronously, so starting the request now
Charlie Harrison 2017/01/25 20:23:00 nit: "so start the request now"? The sentence conf
mmenke 2017/01/25 22:08:00 Done.
445 StartRequestInternal(); 495 StartRequestInternal();
446 break; 496 break;
447 case DEFERRED_REDIRECT: 497 case DEFERRED_REDIRECT:
448 FollowDeferredRedirectInternal(); 498 FollowDeferredRedirectInternal();
449 break; 499 break;
450 case DEFERRED_READ: 500 case DEFERRED_READ:
451 base::ThreadTaskRunnerHandle::Get()->PostTask( 501 if (!called_from_resource_controller) {
Charlie Harrison 2017/01/25 20:23:00 nit: I would prefer the conditional to be reversed
mmenke 2017/01/25 22:07:59 Done.
452 FROM_HERE, base::Bind(&ResourceLoader::ResumeReading, 502 ReadMore(called_from_resource_controller);
453 weak_ptr_factory_.GetWeakPtr())); 503 } else {
504 base::ThreadTaskRunnerHandle::Get()->PostTask(
505 FROM_HERE, base::Bind(&ResourceLoader::ResumeReading,
506 weak_ptr_factory_.GetWeakPtr()));
507 }
454 break; 508 break;
455 case DEFERRED_RESPONSE_COMPLETE: 509 case DEFERRED_RESPONSE_COMPLETE:
456 base::ThreadTaskRunnerHandle::Get()->PostTask( 510 if (!called_from_resource_controller) {
457 FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted, 511 ResponseCompleted();
458 weak_ptr_factory_.GetWeakPtr())); 512 } else {
513 base::ThreadTaskRunnerHandle::Get()->PostTask(
514 FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted,
515 weak_ptr_factory_.GetWeakPtr()));
516 }
459 break; 517 break;
460 case DEFERRED_FINISH: 518 case DEFERRED_FINISH:
461 // Delay self-destruction since we don't know how we were reached. 519 if (!called_from_resource_controller) {
462 base::ThreadTaskRunnerHandle::Get()->PostTask( 520 CallDidFinishLoading();
463 FROM_HERE, base::Bind(&ResourceLoader::CallDidFinishLoading, 521 } else {
464 weak_ptr_factory_.GetWeakPtr())); 522 // Delay self-destruction since we don't know how we were reached.
523 base::ThreadTaskRunnerHandle::Get()->PostTask(
524 FROM_HERE, base::Bind(&ResourceLoader::CallDidFinishLoading,
525 weak_ptr_factory_.GetWeakPtr()));
526 }
465 break; 527 break;
466 } 528 }
467 } 529 }
468 530
469 void ResourceLoader::Cancel() { 531 void ResourceLoader::Cancel() {
470 CancelRequest(false); 532 CancelRequest(false);
471 } 533 }
472 534
473 void ResourceLoader::StartRequestInternal() { 535 void ResourceLoader::StartRequestInternal() {
474 DCHECK(!request_->is_pending()); 536 DCHECK(!request_->is_pending());
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 ResourceRequestInfoImpl* info = GetRequestInfo(); 613 ResourceRequestInfoImpl* info = GetRequestInfo();
552 scoped_refptr<ResourceResponse> response = new ResourceResponse(); 614 scoped_refptr<ResourceResponse> response = new ResourceResponse();
553 PopulateResourceResponse(info, request_.get(), response.get()); 615 PopulateResourceResponse(info, request_.get(), response.get());
554 616
555 delegate_->DidReceiveResponse(this); 617 delegate_->DidReceiveResponse(this);
556 618
557 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. 619 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed.
558 tracked_objects::ScopedTracker tracking_profile( 620 tracked_objects::ScopedTracker tracking_profile(
559 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseStarted()")); 621 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseStarted()"));
560 622
561 bool defer = false; 623 read_deferral_start_time_ = base::TimeTicks::Now();
562 if (!handler_->OnResponseStarted(response.get(), &defer)) { 624 ScopedDeferral scoped_deferral(this, DEFERRED_READ);
563 Cancel(); 625 handler_->OnResponseStarted(response.get(),
564 } else if (defer) { 626 base::MakeUnique<Controller>(this));
565 read_deferral_start_time_ = base::TimeTicks::Now();
566 deferred_stage_ = DEFERRED_READ; // Read first chunk when resumed.
567 }
568 } 627 }
569 628
570 void ResourceLoader::ReadMore(bool is_continuation) { 629 void ResourceLoader::ReadMore(bool called_from_resource_controller) {
571 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::ReadMore", this, 630 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::ReadMore", this,
572 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT); 631 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT);
573 DCHECK(!is_deferred()); 632 DCHECK(!is_deferred());
574 633
575 // Make sure we track the buffer in at least one place. This ensures it gets 634 // Make sure we track the buffer in at least one place. This ensures it gets
576 // deleted even in the case the request has already finished its job and 635 // deleted even in the case the request has already finished its job and
577 // doesn't use the buffer. 636 // doesn't use the buffer.
578 scoped_refptr<net::IOBuffer> buf; 637 scoped_refptr<net::IOBuffer> buf;
579 int buf_size; 638 int buf_size;
580 { 639 {
(...skipping 10 matching lines...) Expand all
591 } 650 }
592 651
593 DCHECK(buf.get()); 652 DCHECK(buf.get());
594 DCHECK(buf_size > 0); 653 DCHECK(buf_size > 0);
595 654
596 int result = request_->Read(buf.get(), buf_size); 655 int result = request_->Read(buf.get(), buf_size);
597 656
598 if (result == net::ERR_IO_PENDING) 657 if (result == net::ERR_IO_PENDING)
599 return; 658 return;
600 659
601 if (!is_continuation || result <= 0) { 660 if (!called_from_resource_controller || result <= 0) {
602 OnReadCompleted(request_.get(), result); 661 OnReadCompleted(request_.get(), result);
Charlie Harrison 2017/01/25 20:23:00 Isn't it breaking the contract to call OnReadCompl
mmenke 2017/01/25 22:07:59 Great catch! I was keeping old behavior here, but
mmenke 2017/01/25 22:38:04 One other thing I noticed - the old code mentions
Charlie Harrison 2017/01/26 00:25:50 Acknowledged
mmenke 2017/01/27 19:14:29 Ok, I looked into this: The reason why I couldn't
603 } else { 662 } else {
604 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO 663 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO
605 // thread in case the URLRequest can provide data synchronously. 664 // thread in case the URLRequest can provide data synchronously.
606 base::ThreadTaskRunnerHandle::Get()->PostTask( 665 base::ThreadTaskRunnerHandle::Get()->PostTask(
607 FROM_HERE, 666 FROM_HERE,
608 base::Bind(&ResourceLoader::OnReadCompleted, 667 base::Bind(&ResourceLoader::OnReadCompleted,
609 weak_ptr_factory_.GetWeakPtr(), request_.get(), result)); 668 weak_ptr_factory_.GetWeakPtr(), request_.get(), result));
610 } 669 }
611 } 670 }
612 671
(...skipping 16 matching lines...) Expand all
629 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CompleteRead", this, 688 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CompleteRead", this,
630 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT); 689 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT);
631 690
632 DCHECK(bytes_read >= 0); 691 DCHECK(bytes_read >= 0);
633 DCHECK(request_->status().is_success()); 692 DCHECK(request_->status().is_success());
634 693
635 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. 694 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed.
636 tracked_objects::ScopedTracker tracking_profile( 695 tracked_objects::ScopedTracker tracking_profile(
637 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnReadCompleted()")); 696 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnReadCompleted()"));
638 697
639 bool defer = false; 698 ScopedDeferral scoped_deferral(
640 if (!handler_->OnReadCompleted(bytes_read, &defer)) { 699 this, bytes_read > 0 ? DEFERRED_READ : DEFERRED_RESPONSE_COMPLETE);
641 Cancel(); 700 handler_->OnReadCompleted(bytes_read, base::MakeUnique<Controller>(this));
642 } else if (defer) {
643 deferred_stage_ =
644 bytes_read > 0 ? DEFERRED_READ : DEFERRED_RESPONSE_COMPLETE;
645 }
646
647 // Note: the request may still have been cancelled while OnReadCompleted
648 // returns true if OnReadCompleted caused request to get cancelled
649 // out-of-band. (In AwResourceDispatcherHostDelegate::DownloadStarting, for
650 // instance.)
651 } 701 }
652 702
653 void ResourceLoader::ResponseCompleted() { 703 void ResourceLoader::ResponseCompleted() {
654 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::ResponseCompleted", this, 704 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::ResponseCompleted", this,
655 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT); 705 TRACE_EVENT_FLAG_FLOW_IN | TRACE_EVENT_FLAG_FLOW_OUT);
656 706
657 DVLOG(1) << "ResponseCompleted: " << request_->url().spec(); 707 DVLOG(1) << "ResponseCompleted: " << request_->url().spec();
658 RecordHistograms(); 708 RecordHistograms();
659 709
660 bool defer = false; 710 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed.
661 { 711 tracked_objects::ScopedTracker tracking_profile(
662 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. 712 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseCompleted()"));
663 tracked_objects::ScopedTracker tracking_profile(
664 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseCompleted()"));
665 713
666 handler_->OnResponseCompleted(request_->status(), &defer); 714 ScopedDeferral scoped_deferral(this, DEFERRED_FINISH);
667 } 715 handler_->OnResponseCompleted(request_->status(),
668 if (defer) { 716 base::MakeUnique<Controller>(this));
669 // The handler is not ready to die yet. We will call DidFinishLoading when
670 // we resume.
671 deferred_stage_ = DEFERRED_FINISH;
672 } else {
673 // This will result in our destruction.
674 CallDidFinishLoading();
675 }
676 } 717 }
677 718
678 void ResourceLoader::CallDidFinishLoading() { 719 void ResourceLoader::CallDidFinishLoading() {
679 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CallDidFinishLoading", 720 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CallDidFinishLoading",
680 this, TRACE_EVENT_FLAG_FLOW_IN); 721 this, TRACE_EVENT_FLAG_FLOW_IN);
681 delegate_->DidFinishLoading(this); 722 delegate_->DidFinishLoading(this);
682 } 723 }
683 724
684 void ResourceLoader::RecordHistograms() { 725 void ResourceLoader::RecordHistograms() {
685 ResourceRequestInfoImpl* info = GetRequestInfo(); 726 ResourceRequestInfoImpl* info = GetRequestInfo();
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", prefetch_status, 781 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", prefetch_status,
741 STATUS_MAX); 782 STATUS_MAX);
742 } 783 }
743 } else if (request_->response_info().unused_since_prefetch) { 784 } else if (request_->response_info().unused_since_prefetch) {
744 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time(); 785 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time();
745 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time); 786 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time);
746 } 787 }
747 } 788 }
748 789
749 } // namespace content 790 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698