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

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

Issue 2574063003: Move ResourceHandler deferred actions ahead of external protocol handling. (Closed)
Patch Set: Reverts URLRequest changes; redirect bugfix; improved unit tests. Created 4 years 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"
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 if (login_delegate_.get()) 150 if (login_delegate_.get())
151 login_delegate_->OnRequestCancelled(); 151 login_delegate_->OnRequestCancelled();
152 ssl_client_auth_handler_.reset(); 152 ssl_client_auth_handler_.reset();
153 153
154 // Run ResourceHandler destructor before we tear-down the rest of our state 154 // 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. 155 // as the ResourceHandler may want to inspect the URLRequest and other state.
156 handler_.reset(); 156 handler_.reset();
157 } 157 }
158 158
159 void ResourceLoader::StartRequest() { 159 void ResourceLoader::StartRequest() {
160 if (delegate_->HandleExternalProtocol(this, request_->url())) {
161 CancelAndIgnore();
162 return;
163 }
164
165 // Give the handler a chance to delay the URLRequest from being started. 160 // Give the handler a chance to delay the URLRequest from being started.
166 bool defer_start = false; 161 bool defer_start = false;
167 if (!handler_->OnWillStart(request_->url(), &defer_start)) { 162 if (!handler_->OnWillStart(request_->url(), &defer_start)) {
168 Cancel(); 163 Cancel();
169 return; 164 return;
170 } 165 }
171 166
172 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::StartRequest", this, 167 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::StartRequest", this,
173 TRACE_EVENT_FLAG_FLOW_OUT); 168 TRACE_EVENT_FLAG_FLOW_OUT);
174 if (defer_start) { 169 if (defer_start) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 info->GetChildID(), redirect_info.new_url)) { 261 info->GetChildID(), redirect_info.new_url)) {
267 DVLOG(1) << "Denied unauthorized request for " 262 DVLOG(1) << "Denied unauthorized request for "
268 << redirect_info.new_url.possibly_invalid_spec(); 263 << redirect_info.new_url.possibly_invalid_spec();
269 264
270 // Tell the renderer that this request was disallowed. 265 // Tell the renderer that this request was disallowed.
271 Cancel(); 266 Cancel();
272 return; 267 return;
273 } 268 }
274 } 269 }
275 270
276 if (delegate_->HandleExternalProtocol(this, redirect_info.new_url)) {
277 // The request is complete so we can remove it.
278 CancelAndIgnore();
279 return;
280 }
281
282 scoped_refptr<ResourceResponse> response = new ResourceResponse(); 271 scoped_refptr<ResourceResponse> response = new ResourceResponse();
283 PopulateResourceResponse(info, request_.get(), response.get()); 272 PopulateResourceResponse(info, request_.get(), response.get());
284 delegate_->DidReceiveRedirect(this, redirect_info.new_url, response.get()); 273 delegate_->DidReceiveRedirect(this, redirect_info.new_url, response.get());
285 if (!handler_->OnRequestRedirected(redirect_info, response.get(), defer)) { 274 if (!handler_->OnRequestRedirected(redirect_info, response.get(), defer)) {
286 Cancel(); 275 Cancel();
287 } else if (*defer) { 276 } else if (*defer) {
288 deferred_stage_ = DEFERRED_REDIRECT; // Follow redirect when resumed. 277 deferred_stage_ = DEFERRED_REDIRECT; // Follow redirect when resumed.
278 DCHECK(deferred_redirect_url_.is_empty());
279 deferred_redirect_url_ = redirect_info.new_url;
280 } else if (delegate_->HandleExternalProtocol(this, redirect_info.new_url)) {
281 // The request is complete so we can remove it.
282 CancelAndIgnore();
283 return;
289 } 284 }
290 } 285 }
291 286
292 void ResourceLoader::OnAuthRequired(net::URLRequest* unused, 287 void ResourceLoader::OnAuthRequired(net::URLRequest* unused,
293 net::AuthChallengeInfo* auth_info) { 288 net::AuthChallengeInfo* auth_info) {
294 DCHECK_EQ(request_.get(), unused); 289 DCHECK_EQ(request_.get(), unused);
295 290
296 ResourceRequestInfoImpl* info = GetRequestInfo(); 291 ResourceRequestInfoImpl* info = GetRequestInfo();
297 if (info->do_not_prompt_for_login()) { 292 if (info->do_not_prompt_for_login()) {
298 request_->CancelAuth(); 293 request_->CancelAuth();
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 DeferredStage stage = deferred_stage_; 438 DeferredStage stage = deferred_stage_;
444 deferred_stage_ = DEFERRED_NONE; 439 deferred_stage_ = DEFERRED_NONE;
445 switch (stage) { 440 switch (stage) {
446 case DEFERRED_NONE: 441 case DEFERRED_NONE:
447 NOTREACHED(); 442 NOTREACHED();
448 break; 443 break;
449 case DEFERRED_START: 444 case DEFERRED_START:
450 StartRequestInternal(); 445 StartRequestInternal();
451 break; 446 break;
452 case DEFERRED_REDIRECT: 447 case DEFERRED_REDIRECT:
453 request_->FollowDeferredRedirect(); 448 FollowDeferredRedirectInternal();
454 break; 449 break;
455 case DEFERRED_READ: 450 case DEFERRED_READ:
456 base::ThreadTaskRunnerHandle::Get()->PostTask( 451 base::ThreadTaskRunnerHandle::Get()->PostTask(
457 FROM_HERE, base::Bind(&ResourceLoader::ResumeReading, 452 FROM_HERE, base::Bind(&ResourceLoader::ResumeReading,
458 weak_ptr_factory_.GetWeakPtr())); 453 weak_ptr_factory_.GetWeakPtr()));
459 break; 454 break;
460 case DEFERRED_RESPONSE_COMPLETE: 455 case DEFERRED_RESPONSE_COMPLETE:
461 base::ThreadTaskRunnerHandle::Get()->PostTask( 456 base::ThreadTaskRunnerHandle::Get()->PostTask(
462 FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted, 457 FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted,
463 weak_ptr_factory_.GetWeakPtr())); 458 weak_ptr_factory_.GetWeakPtr()));
464 break; 459 break;
465 case DEFERRED_FINISH: 460 case DEFERRED_FINISH:
466 // Delay self-destruction since we don't know how we were reached. 461 // Delay self-destruction since we don't know how we were reached.
467 base::ThreadTaskRunnerHandle::Get()->PostTask( 462 base::ThreadTaskRunnerHandle::Get()->PostTask(
468 FROM_HERE, base::Bind(&ResourceLoader::CallDidFinishLoading, 463 FROM_HERE, base::Bind(&ResourceLoader::CallDidFinishLoading,
469 weak_ptr_factory_.GetWeakPtr())); 464 weak_ptr_factory_.GetWeakPtr()));
470 break; 465 break;
471 } 466 }
472 } 467 }
473 468
474 void ResourceLoader::Cancel() { 469 void ResourceLoader::Cancel() {
475 CancelRequest(false); 470 CancelRequest(false);
476 } 471 }
477 472
478 void ResourceLoader::StartRequestInternal() { 473 void ResourceLoader::StartRequestInternal() {
474 // At this point any possible deferred start is already over.
mmenke 2016/12/16 16:19:36 I don't understand this comment, or what it has to
carlosk 2016/12/20 19:23:31 It was just meant to clarify that once this method
479 DCHECK(!request_->is_pending()); 475 DCHECK(!request_->is_pending());
480 476
477 if (delegate_->HandleExternalProtocol(this, request_->url())) {
478 CancelAndIgnore();
479 return;
480 }
481
481 if (!request_->status().is_success()) { 482 if (!request_->status().is_success()) {
mmenke 2016/12/16 16:19:36 Not quite sure what this is checking (Hrm...), but
carlosk 2016/12/20 19:23:31 Done. It was not the case before my change, but e
482 return; 483 return;
483 } 484 }
484 485
485 started_request_ = true; 486 started_request_ = true;
486 request_->Start(); 487 request_->Start();
487 488
488 delegate_->DidStartRequest(this); 489 delegate_->DidStartRequest(this);
489 } 490 }
490 491
491 void ResourceLoader::CancelRequestInternal(int error, bool from_renderer) { 492 void ResourceLoader::CancelRequestInternal(int error, bool from_renderer) {
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
527 if (!was_pending) { 528 if (!was_pending) {
528 // If the request isn't in flight, then we won't get an asynchronous 529 // If the request isn't in flight, then we won't get an asynchronous
529 // notification from the request, so we have to signal ourselves to finish 530 // notification from the request, so we have to signal ourselves to finish
530 // this request. 531 // this request.
531 base::ThreadTaskRunnerHandle::Get()->PostTask( 532 base::ThreadTaskRunnerHandle::Get()->PostTask(
532 FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted, 533 FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted,
533 weak_ptr_factory_.GetWeakPtr())); 534 weak_ptr_factory_.GetWeakPtr()));
534 } 535 }
535 } 536 }
536 537
538 void ResourceLoader::FollowDeferredRedirectInternal() {
539 DCHECK(!deferred_redirect_url_.is_empty());
540 if (delegate_->HandleExternalProtocol(this, deferred_redirect_url_)) {
541 CancelAndIgnore();
542 } else {
543 request_->FollowDeferredRedirect();
544 }
545 deferred_redirect_url_ = GURL();
mmenke 2016/12/16 16:19:36 Should clear this before calling FollowDeferredRed
carlosk 2016/12/20 19:23:31 Done.
546 }
547
537 void ResourceLoader::CompleteResponseStarted() { 548 void ResourceLoader::CompleteResponseStarted() {
538 ResourceRequestInfoImpl* info = GetRequestInfo(); 549 ResourceRequestInfoImpl* info = GetRequestInfo();
539 scoped_refptr<ResourceResponse> response = new ResourceResponse(); 550 scoped_refptr<ResourceResponse> response = new ResourceResponse();
540 PopulateResourceResponse(info, request_.get(), response.get()); 551 PopulateResourceResponse(info, request_.get(), response.get());
541 552
542 delegate_->DidReceiveResponse(this); 553 delegate_->DidReceiveResponse(this);
543 554
544 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. 555 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed.
545 tracked_objects::ScopedTracker tracking_profile( 556 tracked_objects::ScopedTracker tracking_profile(
546 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseStarted()")); 557 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseStarted()"));
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
727 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", prefetch_status, 738 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", prefetch_status,
728 STATUS_MAX); 739 STATUS_MAX);
729 } 740 }
730 } else if (request_->response_info().unused_since_prefetch) { 741 } else if (request_->response_info().unused_since_prefetch) {
731 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time(); 742 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time();
732 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time); 743 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time);
733 } 744 }
734 } 745 }
735 746
736 } // namespace content 747 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698