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

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

Issue 2481093003: Introduce ResourceRequesterInfo to abstract the requester of resource request (Closed)
Patch Set: incorporated yhirano & kinuko's comment Created 4 years, 1 month 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/async_resource_handler.h" 5 #include "content/browser/loader/async_resource_handler.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
11 #include "base/containers/hash_tables.h" 11 #include "base/containers/hash_tables.h"
12 #include "base/debug/alias.h" 12 #include "base/debug/alias.h"
13 #include "base/feature_list.h" 13 #include "base/feature_list.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/shared_memory.h" 16 #include "base/memory/shared_memory.h"
17 #include "base/metrics/histogram_macros.h" 17 #include "base/metrics/histogram_macros.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/time/time.h" 19 #include "base/time/time.h"
20 #include "content/browser/loader/netlog_observer.h" 20 #include "content/browser/loader/netlog_observer.h"
21 #include "content/browser/loader/resource_buffer.h" 21 #include "content/browser/loader/resource_buffer.h"
22 #include "content/browser/loader/resource_dispatcher_host_impl.h" 22 #include "content/browser/loader/resource_dispatcher_host_impl.h"
23 #include "content/browser/loader/resource_message_filter.h"
24 #include "content/browser/loader/resource_request_info_impl.h" 23 #include "content/browser/loader/resource_request_info_impl.h"
25 #include "content/common/resource_messages.h" 24 #include "content/common/resource_messages.h"
26 #include "content/common/resource_request_completion_status.h" 25 #include "content/common/resource_request_completion_status.h"
27 #include "content/common/view_messages.h" 26 #include "content/common/view_messages.h"
27 #include "content/public/browser/browser_message_filter.h"
28 #include "content/public/browser/resource_dispatcher_host_delegate.h" 28 #include "content/public/browser/resource_dispatcher_host_delegate.h"
29 #include "content/public/common/content_features.h" 29 #include "content/public/common/content_features.h"
30 #include "content/public/common/resource_response.h" 30 #include "content/public/common/resource_response.h"
31 #include "ipc/ipc_message_macros.h" 31 #include "ipc/ipc_message_macros.h"
32 #include "net/base/io_buffer.h" 32 #include "net/base/io_buffer.h"
33 #include "net/base/load_flags.h" 33 #include "net/base/load_flags.h"
34 #include "net/url_request/redirect_info.h" 34 #include "net/url_request/redirect_info.h"
35 35
36 using base::TimeDelta; 36 using base::TimeDelta;
37 using base::TimeTicks; 37 using base::TimeTicks;
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 allocation_size_(0), 209 allocation_size_(0),
210 did_defer_(false), 210 did_defer_(false),
211 has_checked_for_sufficient_resources_(false), 211 has_checked_for_sufficient_resources_(false),
212 sent_received_response_msg_(false), 212 sent_received_response_msg_(false),
213 sent_data_buffer_msg_(false), 213 sent_data_buffer_msg_(false),
214 inlining_helper_(new InliningHelper), 214 inlining_helper_(new InliningHelper),
215 last_upload_position_(0), 215 last_upload_position_(0),
216 waiting_for_upload_progress_ack_(false), 216 waiting_for_upload_progress_ack_(false),
217 reported_transfer_size_(0), 217 reported_transfer_size_(0),
218 reported_encoded_body_length_(0) { 218 reported_encoded_body_length_(0) {
219 DCHECK(GetRequestInfo()->requester_info().IsRenderer());
219 InitializeResourceBufferConstants(); 220 InitializeResourceBufferConstants();
220 } 221 }
221 222
222 AsyncResourceHandler::~AsyncResourceHandler() { 223 AsyncResourceHandler::~AsyncResourceHandler() {
223 if (has_checked_for_sufficient_resources_) 224 if (has_checked_for_sufficient_resources_)
224 rdh_->FinishedWithResourcesForRequest(request()); 225 rdh_->FinishedWithResourcesForRequest(request());
225 } 226 }
226 227
227 bool AsyncResourceHandler::OnMessageReceived(const IPC::Message& message) { 228 bool AsyncResourceHandler::OnMessageReceived(const IPC::Message& message) {
228 bool handled = true; 229 bool handled = true;
(...skipping 24 matching lines...) Expand all
253 ResumeIfDeferred(); 254 ResumeIfDeferred();
254 } 255 }
255 } 256 }
256 257
257 void AsyncResourceHandler::OnUploadProgressACK(int request_id) { 258 void AsyncResourceHandler::OnUploadProgressACK(int request_id) {
258 waiting_for_upload_progress_ack_ = false; 259 waiting_for_upload_progress_ack_ = false;
259 } 260 }
260 261
261 void AsyncResourceHandler::ReportUploadProgress() { 262 void AsyncResourceHandler::ReportUploadProgress() {
262 DCHECK(GetRequestInfo()->is_upload_progress_enabled()); 263 DCHECK(GetRequestInfo()->is_upload_progress_enabled());
264 DCHECK(GetRequestInfo()->requester_info().IsRenderer());
263 if (waiting_for_upload_progress_ack_) 265 if (waiting_for_upload_progress_ack_)
264 return; // Send one progress event at a time. 266 return; // Send one progress event at a time.
265 267
266 net::UploadProgress progress = request()->GetUploadProgress(); 268 net::UploadProgress progress = request()->GetUploadProgress();
267 if (!progress.size()) 269 if (!progress.size())
268 return; // Nothing to upload. 270 return; // Nothing to upload.
269 271
270 if (progress.position() == last_upload_position_) 272 if (progress.position() == last_upload_position_)
271 return; // No progress made since last time. 273 return; // No progress made since last time.
272 274
273 const uint64_t kHalfPercentIncrements = 200; 275 const uint64_t kHalfPercentIncrements = 200;
274 const TimeDelta kOneSecond = TimeDelta::FromMilliseconds(1000); 276 const TimeDelta kOneSecond = TimeDelta::FromMilliseconds(1000);
275 277
276 uint64_t amt_since_last = progress.position() - last_upload_position_; 278 uint64_t amt_since_last = progress.position() - last_upload_position_;
277 TimeDelta time_since_last = TimeTicks::Now() - last_upload_ticks_; 279 TimeDelta time_since_last = TimeTicks::Now() - last_upload_ticks_;
278 280
279 bool is_finished = (progress.size() == progress.position()); 281 bool is_finished = (progress.size() == progress.position());
280 bool enough_new_progress = 282 bool enough_new_progress =
281 (amt_since_last > (progress.size() / kHalfPercentIncrements)); 283 (amt_since_last > (progress.size() / kHalfPercentIncrements));
282 bool too_much_time_passed = time_since_last > kOneSecond; 284 bool too_much_time_passed = time_since_last > kOneSecond;
283 285
284 if (is_finished || enough_new_progress || too_much_time_passed) { 286 if (is_finished || enough_new_progress || too_much_time_passed) {
285 ResourceMessageFilter* filter = GetFilter(); 287 BrowserMessageFilter* filter = GetFilter();
286 if (filter) { 288 if (filter) {
287 filter->Send( 289 filter->Send(
288 new ResourceMsg_UploadProgress(GetRequestID(), 290 new ResourceMsg_UploadProgress(GetRequestID(),
289 progress.position(), 291 progress.position(),
290 progress.size())); 292 progress.size()));
291 } 293 }
292 waiting_for_upload_progress_ack_ = true; 294 waiting_for_upload_progress_ack_ = true;
293 last_upload_ticks_ = TimeTicks::Now(); 295 last_upload_ticks_ = TimeTicks::Now();
294 last_upload_position_ = progress.position(); 296 last_upload_position_ = progress.position();
295 } 297 }
296 } 298 }
297 299
298 bool AsyncResourceHandler::OnRequestRedirected( 300 bool AsyncResourceHandler::OnRequestRedirected(
299 const net::RedirectInfo& redirect_info, 301 const net::RedirectInfo& redirect_info,
300 ResourceResponse* response, 302 ResourceResponse* response,
301 bool* defer) { 303 bool* defer) {
302 const ResourceRequestInfoImpl* info = GetRequestInfo(); 304 DCHECK(GetRequestInfo()->requester_info().IsRenderer());
303 if (!info->filter()) 305 BrowserMessageFilter* filter = GetFilter();
306 if (!filter)
304 return false; 307 return false;
305 308
306 *defer = did_defer_ = true; 309 *defer = did_defer_ = true;
307 OnDefer(); 310 OnDefer();
308 311
309 NetLogObserver::PopulateResponseInfo(request(), response); 312 NetLogObserver::PopulateResponseInfo(request(), response);
310 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); 313 response->head.encoded_data_length = request()->GetTotalReceivedBytes();
311 reported_transfer_size_ = 0; 314 reported_transfer_size_ = 0;
312 response->head.request_start = request()->creation_time(); 315 response->head.request_start = request()->creation_time();
313 response->head.response_start = TimeTicks::Now(); 316 response->head.response_start = TimeTicks::Now();
314 // TODO(davidben): Is it necessary to pass the new first party URL for 317 // TODO(davidben): Is it necessary to pass the new first party URL for
315 // cookies? The only case where it can change is top-level navigation requests 318 // cookies? The only case where it can change is top-level navigation requests
316 // and hopefully those will eventually all be owned by the browser. It's 319 // and hopefully those will eventually all be owned by the browser. It's
317 // possible this is still needed while renderer-owned ones exist. 320 // possible this is still needed while renderer-owned ones exist.
318 return info->filter()->Send(new ResourceMsg_ReceivedRedirect( 321 return filter->Send(new ResourceMsg_ReceivedRedirect(
319 GetRequestID(), redirect_info, response->head)); 322 GetRequestID(), redirect_info, response->head));
320 } 323 }
321 324
322 bool AsyncResourceHandler::OnResponseStarted(ResourceResponse* response, 325 bool AsyncResourceHandler::OnResponseStarted(ResourceResponse* response,
323 bool* defer) { 326 bool* defer) {
327 DCHECK(GetRequestInfo()->requester_info().IsRenderer());
324 // For changes to the main frame, inform the renderer of the new URL's 328 // For changes to the main frame, inform the renderer of the new URL's
325 // per-host settings before the request actually commits. This way the 329 // per-host settings before the request actually commits. This way the
326 // renderer will be able to set these precisely at the time the 330 // renderer will be able to set these precisely at the time the
327 // request commits, avoiding the possibility of e.g. zooming the old content 331 // request commits, avoiding the possibility of e.g. zooming the old content
328 // or of having to layout the new content twice. 332 // or of having to layout the new content twice.
329 333
330 response_started_ticks_ = base::TimeTicks::Now(); 334 response_started_ticks_ = base::TimeTicks::Now();
331 335
332 progress_timer_.Stop(); 336 progress_timer_.Stop();
333 const ResourceRequestInfoImpl* info = GetRequestInfo(); 337 const ResourceRequestInfoImpl* info = GetRequestInfo();
334 if (!info->filter()) 338 BrowserMessageFilter* filter = GetFilter();
339 if (!filter)
335 return false; 340 return false;
336 341
337 // We want to send a final upload progress message prior to sending the 342 // We want to send a final upload progress message prior to sending the
338 // response complete message even if we're waiting for an ack to to a 343 // response complete message even if we're waiting for an ack to to a
339 // previous upload progress message. 344 // previous upload progress message.
340 if (info->is_upload_progress_enabled()) { 345 if (info->is_upload_progress_enabled()) {
341 waiting_for_upload_progress_ack_ = false; 346 waiting_for_upload_progress_ack_ = false;
342 ReportUploadProgress(); 347 ReportUploadProgress();
343 } 348 }
344 349
345 if (rdh_->delegate()) { 350 if (rdh_->delegate()) {
346 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), 351 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(),
347 response); 352 response);
348 } 353 }
349 354
350 NetLogObserver::PopulateResponseInfo(request(), response); 355 NetLogObserver::PopulateResponseInfo(request(), response);
351 response->head.encoded_data_length = request()->raw_header_size(); 356 response->head.encoded_data_length = request()->raw_header_size();
352 357
353 // If the parent handler downloaded the resource to a file, grant the child 358 // If the parent handler downloaded the resource to a file, grant the child
354 // read permissions on it. 359 // read permissions on it.
355 if (!response->head.download_file_path.empty()) { 360 if (!response->head.download_file_path.empty()) {
356 rdh_->RegisterDownloadedTempFile( 361 rdh_->RegisterDownloadedTempFile(
357 info->GetChildID(), info->GetRequestID(), 362 info->GetChildID(), info->GetRequestID(),
358 response->head.download_file_path); 363 response->head.download_file_path);
359 } 364 }
360 365
361 response->head.request_start = request()->creation_time(); 366 response->head.request_start = request()->creation_time();
362 response->head.response_start = TimeTicks::Now(); 367 response->head.response_start = TimeTicks::Now();
363 info->filter()->Send(new ResourceMsg_ReceivedResponse(GetRequestID(), 368 filter->Send(
364 response->head)); 369 new ResourceMsg_ReceivedResponse(GetRequestID(), response->head));
365 sent_received_response_msg_ = true; 370 sent_received_response_msg_ = true;
366 371
367 if (request()->response_info().metadata.get()) { 372 if (request()->response_info().metadata.get()) {
368 std::vector<char> copy(request()->response_info().metadata->data(), 373 std::vector<char> copy(request()->response_info().metadata->data(),
369 request()->response_info().metadata->data() + 374 request()->response_info().metadata->data() +
370 request()->response_info().metadata->size()); 375 request()->response_info().metadata->size());
371 info->filter()->Send(new ResourceMsg_ReceivedCachedMetadata(GetRequestID(), 376 filter->Send(new ResourceMsg_ReceivedCachedMetadata(GetRequestID(), copy));
372 copy));
373 } 377 }
374 378
375 inlining_helper_->OnResponseReceived(*response); 379 inlining_helper_->OnResponseReceived(*response);
376 return true; 380 return true;
377 } 381 }
378 382
379 bool AsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { 383 bool AsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) {
384 DCHECK(GetRequestInfo()->requester_info().IsRenderer());
380 if (GetRequestInfo()->is_upload_progress_enabled() && 385 if (GetRequestInfo()->is_upload_progress_enabled() &&
381 request()->has_upload()) { 386 request()->has_upload()) {
382 ReportUploadProgress(); 387 ReportUploadProgress();
383 progress_timer_.Start( 388 progress_timer_.Start(
384 FROM_HERE, 389 FROM_HERE,
385 base::TimeDelta::FromMilliseconds(kUploadProgressIntervalMsec), 390 base::TimeDelta::FromMilliseconds(kUploadProgressIntervalMsec),
386 this, 391 this,
387 &AsyncResourceHandler::ReportUploadProgress); 392 &AsyncResourceHandler::ReportUploadProgress);
388 } 393 }
389 return true; 394 return true;
390 } 395 }
391 396
392 bool AsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, 397 bool AsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
393 int* buf_size, 398 int* buf_size,
394 int min_size) { 399 int min_size) {
400 DCHECK(GetRequestInfo()->requester_info().IsRenderer());
395 DCHECK_EQ(-1, min_size); 401 DCHECK_EQ(-1, min_size);
396 402
397 if (!CheckForSufficientResource()) 403 if (!CheckForSufficientResource())
398 return false; 404 return false;
399 405
400 // Return early if InliningHelper allocates the buffer, so that we should 406 // Return early if InliningHelper allocates the buffer, so that we should
401 // inline the data into the IPC message without allocating SharedMemory. 407 // inline the data into the IPC message without allocating SharedMemory.
402 if (inlining_helper_->PrepareInlineBufferIfApplicable(buf, buf_size)) 408 if (inlining_helper_->PrepareInlineBufferIfApplicable(buf, buf_size))
403 return true; 409 return true;
404 410
405 if (!EnsureResourceBufferIsInitialized()) 411 if (!EnsureResourceBufferIsInitialized())
406 return false; 412 return false;
407 413
408 DCHECK(buffer_->CanAllocate()); 414 DCHECK(buffer_->CanAllocate());
409 char* memory = buffer_->Allocate(&allocation_size_); 415 char* memory = buffer_->Allocate(&allocation_size_);
410 CHECK(memory); 416 CHECK(memory);
411 417
412 *buf = new DependentIOBuffer(buffer_.get(), memory); 418 *buf = new DependentIOBuffer(buffer_.get(), memory);
413 *buf_size = allocation_size_; 419 *buf_size = allocation_size_;
414 420
415 return true; 421 return true;
416 } 422 }
417 423
418 bool AsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { 424 bool AsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) {
425 DCHECK(GetRequestInfo()->requester_info().IsRenderer());
419 DCHECK_GE(bytes_read, 0); 426 DCHECK_GE(bytes_read, 0);
420 427
421 if (!bytes_read) 428 if (!bytes_read)
422 return true; 429 return true;
423 430
424 ResourceMessageFilter* filter = GetFilter(); 431 BrowserMessageFilter* filter = GetFilter();
425 if (!filter) 432 if (!filter)
426 return false; 433 return false;
427 434
428 int encoded_data_length = CalculateEncodedDataLengthToReport(); 435 int encoded_data_length = CalculateEncodedDataLengthToReport();
429 if (!first_chunk_read_) 436 if (!first_chunk_read_)
430 encoded_data_length -= request()->raw_header_size(); 437 encoded_data_length -= request()->raw_header_size();
431 438
432 int encoded_body_length = CalculateEncodedBodyLengthToReport(); 439 int encoded_body_length = CalculateEncodedBodyLengthToReport();
433 first_chunk_read_ = true; 440 first_chunk_read_ = true;
434 441
(...skipping 25 matching lines...) Expand all
460 467
461 if (!buffer_->CanAllocate()) { 468 if (!buffer_->CanAllocate()) {
462 *defer = did_defer_ = true; 469 *defer = did_defer_ = true;
463 OnDefer(); 470 OnDefer();
464 } 471 }
465 472
466 return true; 473 return true;
467 } 474 }
468 475
469 void AsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { 476 void AsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) {
477 DCHECK(GetRequestInfo()->requester_info().IsRenderer());
470 int encoded_data_length = CalculateEncodedDataLengthToReport(); 478 int encoded_data_length = CalculateEncodedDataLengthToReport();
471 479
472 ResourceMessageFilter* filter = GetFilter(); 480 BrowserMessageFilter* filter = GetFilter();
473 if (filter) { 481 if (filter) {
474 filter->Send(new ResourceMsg_DataDownloaded( 482 filter->Send(new ResourceMsg_DataDownloaded(
475 GetRequestID(), bytes_downloaded, encoded_data_length)); 483 GetRequestID(), bytes_downloaded, encoded_data_length));
476 } 484 }
477 } 485 }
478 486
479 void AsyncResourceHandler::OnResponseCompleted( 487 void AsyncResourceHandler::OnResponseCompleted(
480 const net::URLRequestStatus& status, 488 const net::URLRequestStatus& status,
481 bool* defer) { 489 bool* defer) {
490 DCHECK(GetRequestInfo()->requester_info().IsRenderer());
482 const ResourceRequestInfoImpl* info = GetRequestInfo(); 491 const ResourceRequestInfoImpl* info = GetRequestInfo();
483 if (!info->filter()) 492 BrowserMessageFilter* filter = GetFilter();
493 if (!filter)
484 return; 494 return;
485 495
486 // If we crash here, figure out what URL the renderer was requesting. 496 // If we crash here, figure out what URL the renderer was requesting.
487 // http://crbug.com/107692 497 // http://crbug.com/107692
488 char url_buf[128]; 498 char url_buf[128];
489 base::strlcpy(url_buf, request()->url().spec().c_str(), arraysize(url_buf)); 499 base::strlcpy(url_buf, request()->url().spec().c_str(), arraysize(url_buf));
490 base::debug::Alias(url_buf); 500 base::debug::Alias(url_buf);
491 501
492 // TODO(gavinp): Remove this CHECK when we figure out the cause of 502 // TODO(gavinp): Remove this CHECK when we figure out the cause of
493 // http://crbug.com/124680 . This check mirrors closely check in 503 // http://crbug.com/124680 . This check mirrors closely check in
(...skipping 12 matching lines...) Expand all
506 // the ERR_ABORTED error code). 516 // the ERR_ABORTED error code).
507 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED); 517 DCHECK(!was_ignored_by_handler || error_code == net::ERR_ABORTED);
508 518
509 ResourceRequestCompletionStatus request_complete_data; 519 ResourceRequestCompletionStatus request_complete_data;
510 request_complete_data.error_code = error_code; 520 request_complete_data.error_code = error_code;
511 request_complete_data.was_ignored_by_handler = was_ignored_by_handler; 521 request_complete_data.was_ignored_by_handler = was_ignored_by_handler;
512 request_complete_data.exists_in_cache = request()->response_info().was_cached; 522 request_complete_data.exists_in_cache = request()->response_info().was_cached;
513 request_complete_data.completion_time = TimeTicks::Now(); 523 request_complete_data.completion_time = TimeTicks::Now();
514 request_complete_data.encoded_data_length = 524 request_complete_data.encoded_data_length =
515 request()->GetTotalReceivedBytes(); 525 request()->GetTotalReceivedBytes();
516 info->filter()->Send( 526 filter->Send(
517 new ResourceMsg_RequestComplete(GetRequestID(), request_complete_data)); 527 new ResourceMsg_RequestComplete(GetRequestID(), request_complete_data));
518 528
519 if (status.is_success()) 529 if (status.is_success())
520 RecordHistogram(); 530 RecordHistogram();
521 } 531 }
522 532
523 bool AsyncResourceHandler::EnsureResourceBufferIsInitialized() { 533 bool AsyncResourceHandler::EnsureResourceBufferIsInitialized() {
524 DCHECK(has_checked_for_sufficient_resources_); 534 DCHECK(has_checked_for_sufficient_resources_);
525 535
526 if (buffer_.get() && buffer_->IsInitialized()) 536 if (buffer_.get() && buffer_->IsInitialized())
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 } else { 596 } else {
587 UMA_HISTOGRAM_CUSTOM_COUNTS( 597 UMA_HISTOGRAM_CUSTOM_COUNTS(
588 "Net.ResourceLoader.ResponseStartToEnd.Over_512kB", 598 "Net.ResourceLoader.ResponseStartToEnd.Over_512kB",
589 elapsed_time, 1, 100000, 100); 599 elapsed_time, 1, 100000, 100);
590 } 600 }
591 601
592 inlining_helper_->RecordHistogram(elapsed_time); 602 inlining_helper_->RecordHistogram(elapsed_time);
593 } 603 }
594 604
595 } // namespace content 605 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698