Chromium Code Reviews| OLD | NEW |
|---|---|
| 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" |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 public: | 184 public: |
| 185 DependentIOBuffer(ResourceBuffer* backing, char* memory) | 185 DependentIOBuffer(ResourceBuffer* backing, char* memory) |
| 186 : net::WrappedIOBuffer(memory), | 186 : net::WrappedIOBuffer(memory), |
| 187 backing_(backing) { | 187 backing_(backing) { |
| 188 } | 188 } |
| 189 private: | 189 private: |
| 190 ~DependentIOBuffer() override {} | 190 ~DependentIOBuffer() override {} |
| 191 scoped_refptr<ResourceBuffer> backing_; | 191 scoped_refptr<ResourceBuffer> backing_; |
| 192 }; | 192 }; |
| 193 | 193 |
| 194 AsyncResourceHandler::AsyncResourceHandler( | 194 AsyncResourceHandler::AsyncResourceHandler(net::URLRequest* request, |
| 195 net::URLRequest* request, | 195 ResourceDispatcherHostImpl* rdh, |
| 196 ResourceDispatcherHostImpl* rdh) | 196 bool using_mojo_data_handle) |
| 197 : ResourceHandler(request), | 197 : ResourceHandler(request), |
| 198 ResourceMessageDelegate(request), | 198 ResourceMessageDelegate(request), |
| 199 rdh_(rdh), | 199 rdh_(rdh), |
| 200 pending_data_count_(0), | 200 pending_data_count_(0), |
| 201 allocation_size_(0), | 201 allocation_size_(0), |
| 202 did_defer_(false), | 202 did_defer_(false), |
| 203 has_checked_for_sufficient_resources_(false), | 203 has_checked_for_sufficient_resources_(false), |
| 204 sent_received_response_msg_(false), | 204 sent_received_response_msg_(false), |
| 205 sent_data_buffer_msg_(false), | 205 sent_data_buffer_msg_(false), |
| 206 using_mojo_data_handle_(using_mojo_data_handle), | |
| 206 inlining_helper_(new InliningHelper), | 207 inlining_helper_(new InliningHelper), |
| 207 last_upload_position_(0), | 208 last_upload_position_(0), |
| 208 waiting_for_upload_progress_ack_(false), | 209 waiting_for_upload_progress_ack_(false), |
| 209 reported_transfer_size_(0) { | 210 reported_transfer_size_(0) { |
| 210 InitializeResourceBufferConstants(); | 211 InitializeResourceBufferConstants(); |
| 211 } | 212 } |
| 212 | 213 |
| 213 AsyncResourceHandler::~AsyncResourceHandler() { | 214 AsyncResourceHandler::~AsyncResourceHandler() { |
| 214 if (has_checked_for_sufficient_resources_) | 215 if (has_checked_for_sufficient_resources_) |
| 215 rdh_->FinishedWithResourcesForRequest(request()); | 216 rdh_->FinishedWithResourcesForRequest(request()); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 348 // If the parent handler downloaded the resource to a file, grant the child | 349 // If the parent handler downloaded the resource to a file, grant the child |
| 349 // read permissions on it. | 350 // read permissions on it. |
| 350 if (!response->head.download_file_path.empty()) { | 351 if (!response->head.download_file_path.empty()) { |
| 351 rdh_->RegisterDownloadedTempFile( | 352 rdh_->RegisterDownloadedTempFile( |
| 352 info->GetChildID(), info->GetRequestID(), | 353 info->GetChildID(), info->GetRequestID(), |
| 353 response->head.download_file_path); | 354 response->head.download_file_path); |
| 354 } | 355 } |
| 355 | 356 |
| 356 response->head.request_start = request()->creation_time(); | 357 response->head.request_start = request()->creation_time(); |
| 357 response->head.response_start = TimeTicks::Now(); | 358 response->head.response_start = TimeTicks::Now(); |
| 359 if (using_mojo_data_handle_) { | |
| 360 MojoCreateDataPipeOptions options; | |
| 361 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 362 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
| 363 options.element_num_bytes = 1; | |
| 364 options.capacity_num_bytes = kMaxAllocationSize; | |
| 365 mojo::DataPipe data_pipe(options); | |
| 366 | |
| 367 writer_ = std::move(data_pipe.producer_handle); | |
| 368 GetRequestInfo()->InstallBodyReader(std::move(data_pipe.consumer_handle)); | |
| 369 } | |
|
kinuko
2016/05/20 09:38:51
nit: having mojo-related code scattered across a c
yhirano
2016/05/20 11:32:54
Done.
| |
| 370 | |
| 358 info->filter()->Send(new ResourceMsg_ReceivedResponse(GetRequestID(), | 371 info->filter()->Send(new ResourceMsg_ReceivedResponse(GetRequestID(), |
| 359 response->head)); | 372 response->head)); |
| 360 sent_received_response_msg_ = true; | 373 sent_received_response_msg_ = true; |
| 361 | 374 |
| 362 if (request()->response_info().metadata.get()) { | 375 if (request()->response_info().metadata.get()) { |
| 363 std::vector<char> copy(request()->response_info().metadata->data(), | 376 std::vector<char> copy(request()->response_info().metadata->data(), |
| 364 request()->response_info().metadata->data() + | 377 request()->response_info().metadata->data() + |
| 365 request()->response_info().metadata->size()); | 378 request()->response_info().metadata->size()); |
| 366 info->filter()->Send(new ResourceMsg_ReceivedCachedMetadata(GetRequestID(), | 379 info->filter()->Send(new ResourceMsg_ReceivedCachedMetadata(GetRequestID(), |
| 367 copy)); | 380 copy)); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 389 } | 402 } |
| 390 | 403 |
| 391 bool AsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 404 bool AsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 392 int* buf_size, | 405 int* buf_size, |
| 393 int min_size) { | 406 int min_size) { |
| 394 DCHECK_EQ(-1, min_size); | 407 DCHECK_EQ(-1, min_size); |
| 395 | 408 |
| 396 if (!CheckForSufficientResource()) | 409 if (!CheckForSufficientResource()) |
| 397 return false; | 410 return false; |
| 398 | 411 |
| 412 if (using_mojo_data_handle_) { | |
| 413 void* buffer = nullptr; | |
| 414 uint32_t available = 0; | |
| 415 MojoResult result = mojo::BeginWriteDataRaw( | |
| 416 writer_.get(), &buffer, &available, MOJO_WRITE_DATA_FLAG_NONE); | |
| 417 // Note that we cannot handle SHOULD_WAIT here. It should be handled in | |
| 418 // OnReadCompleted. | |
| 419 if (result == MOJO_RESULT_OK) { | |
| 420 *buf = new net::WrappedIOBuffer(static_cast<const char*>(buffer)); | |
| 421 *buf_size = available; | |
| 422 return true; | |
| 423 } | |
| 424 return false; | |
| 425 } | |
| 399 // Return early if InliningHelper allocates the buffer, so that we should | 426 // Return early if InliningHelper allocates the buffer, so that we should |
| 400 // inline the data into the IPC message without allocating SharedMemory. | 427 // inline the data into the IPC message without allocating SharedMemory. |
| 401 if (inlining_helper_->PrepareInlineBufferIfApplicable(buf, buf_size)) | 428 if (inlining_helper_->PrepareInlineBufferIfApplicable(buf, buf_size)) |
| 402 return true; | 429 return true; |
| 403 | 430 |
| 404 if (!EnsureResourceBufferIsInitialized()) | 431 if (!EnsureResourceBufferIsInitialized()) |
| 405 return false; | 432 return false; |
| 406 | 433 |
| 407 DCHECK(buffer_->CanAllocate()); | 434 DCHECK(buffer_->CanAllocate()); |
| 408 char* memory = buffer_->Allocate(&allocation_size_); | 435 char* memory = buffer_->Allocate(&allocation_size_); |
| 409 CHECK(memory); | 436 CHECK(memory); |
| 410 | 437 |
| 411 *buf = new DependentIOBuffer(buffer_.get(), memory); | 438 *buf = new DependentIOBuffer(buffer_.get(), memory); |
| 412 *buf_size = allocation_size_; | 439 *buf_size = allocation_size_; |
| 413 | 440 |
| 414 return true; | 441 return true; |
| 415 } | 442 } |
| 416 | 443 |
| 417 bool AsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 444 bool AsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
| 418 DCHECK_GE(bytes_read, 0); | 445 DCHECK_GE(bytes_read, 0); |
| 419 | 446 |
| 420 if (!bytes_read) | 447 if (!bytes_read) |
| 421 return true; | 448 return true; |
| 422 | 449 |
| 450 if (using_mojo_data_handle_) { | |
| 451 MojoResult result = mojo::EndWriteDataRaw(writer_.get(), bytes_read); | |
| 452 if (result != MOJO_RESULT_OK) | |
| 453 return false; | |
| 454 void* buffer = nullptr; | |
| 455 uint32_t available = 0; | |
| 456 // To see if we can continue writing. | |
| 457 result = mojo::BeginWriteDataRaw(writer_.get(), &buffer, &available, | |
| 458 MOJO_WRITE_DATA_FLAG_NONE); | |
| 459 if (result == MOJO_RESULT_SHOULD_WAIT || | |
| 460 (result == MOJO_RESULT_OK && available == 0)) { | |
| 461 *defer = did_defer_ = true; | |
| 462 OnDefer(); | |
| 463 handle_watcher_.Start(writer_.get(), MOJO_HANDLE_SIGNAL_WRITABLE, | |
| 464 MOJO_DEADLINE_INDEFINITE, | |
| 465 base::Bind(&AsyncResourceHandler::OnWritable, | |
| 466 base::Unretained(this))); | |
| 467 } | |
| 468 if (result == MOJO_RESULT_OK) | |
| 469 mojo::EndWriteDataRaw(writer_.get(), 0); | |
| 470 return true; | |
| 471 } | |
| 472 | |
| 423 ResourceMessageFilter* filter = GetFilter(); | 473 ResourceMessageFilter* filter = GetFilter(); |
| 424 if (!filter) | 474 if (!filter) |
| 425 return false; | 475 return false; |
| 426 | 476 |
| 427 int encoded_data_length = CalculateEncodedDataLengthToReport(); | 477 int encoded_data_length = CalculateEncodedDataLengthToReport(); |
| 428 | 478 |
| 429 // Return early if InliningHelper handled the received data. | 479 // Return early if InliningHelper handled the received data. |
| 430 if (inlining_helper_->SendInlinedDataIfApplicable( | 480 if (inlining_helper_->SendInlinedDataIfApplicable( |
| 431 bytes_read, encoded_data_length, filter, GetRequestID())) | 481 bytes_read, encoded_data_length, filter, GetRequestID())) |
| 432 return true; | 482 return true; |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 586 elapsed_time, 1, 100000, 100); | 636 elapsed_time, 1, 100000, 100); |
| 587 } else { | 637 } else { |
| 588 UMA_HISTOGRAM_CUSTOM_COUNTS( | 638 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 589 "Net.ResourceLoader.ResponseStartToEnd.Over_512kB", | 639 "Net.ResourceLoader.ResponseStartToEnd.Over_512kB", |
| 590 elapsed_time, 1, 100000, 100); | 640 elapsed_time, 1, 100000, 100); |
| 591 } | 641 } |
| 592 | 642 |
| 593 inlining_helper_->RecordHistogram(elapsed_time); | 643 inlining_helper_->RecordHistogram(elapsed_time); |
| 594 } | 644 } |
| 595 | 645 |
| 646 void AsyncResourceHandler::OnWritable(MojoResult result) { | |
| 647 ResumeIfDeferred(); | |
| 648 } | |
| 649 | |
| 596 } // namespace content | 650 } // namespace content |
| OLD | NEW |