| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "pdf/document_loader.h" | 5 #include "pdf/document_loader.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 | 442 |
| 443 if (length) { | 443 if (length) { |
| 444 if (document_size_ > 0) { | 444 if (document_size_ > 0) { |
| 445 chunk_stream_.WriteData(current_pos_, start, length); | 445 chunk_stream_.WriteData(current_pos_, start, length); |
| 446 } else { | 446 } else { |
| 447 // If we did not get content-length in the response, we can't | 447 // If we did not get content-length in the response, we can't |
| 448 // preallocate buffer for the entire document. Resizing array causing | 448 // preallocate buffer for the entire document. Resizing array causing |
| 449 // memory fragmentation issues on the large files and OOM exceptions. | 449 // memory fragmentation issues on the large files and OOM exceptions. |
| 450 // To fix this, we collect all chunks of the file to the list and | 450 // To fix this, we collect all chunks of the file to the list and |
| 451 // concatenate them together after request is complete. | 451 // concatenate them together after request is complete. |
| 452 chunk_buffer_.push_back(std::vector<unsigned char>()); | 452 std::vector<unsigned char> buf(length); |
| 453 chunk_buffer_.back().resize(length); | 453 memcpy(buf.data(), start, length); |
| 454 memcpy(&(chunk_buffer_.back()[0]), start, length); | 454 chunk_buffer_.push_back(std::move(buf)); |
| 455 } | 455 } |
| 456 current_pos_ += length; | 456 current_pos_ += length; |
| 457 current_chunk_read_ += length; | 457 current_chunk_read_ += length; |
| 458 client_->OnNewDataAvailable(); | 458 client_->OnNewDataAvailable(); |
| 459 } | 459 } |
| 460 | 460 |
| 461 // Only call the renderer if we allow partial loading. | 461 // Only call the renderer if we allow partial loading. |
| 462 if (!partial_document_) { | 462 if (!partial_document_) { |
| 463 ReadMore(); | 463 ReadMore(); |
| 464 return; | 464 return; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 | 504 |
| 505 void DocumentLoader::ReadComplete() { | 505 void DocumentLoader::ReadComplete() { |
| 506 if (!partial_document_) { | 506 if (!partial_document_) { |
| 507 if (document_size_ == 0) { | 507 if (document_size_ == 0) { |
| 508 // For the document with no 'content-length" specified we've collected all | 508 // For the document with no 'content-length" specified we've collected all |
| 509 // the chunks already. Let's allocate final document buffer and copy them | 509 // the chunks already. Let's allocate final document buffer and copy them |
| 510 // over. | 510 // over. |
| 511 chunk_stream_.Preallocate(current_pos_); | 511 chunk_stream_.Preallocate(current_pos_); |
| 512 uint32_t pos = 0; | 512 uint32_t pos = 0; |
| 513 for (auto& chunk : chunk_buffer_) { | 513 for (auto& chunk : chunk_buffer_) { |
| 514 chunk_stream_.WriteData(pos, &(chunk[0]), chunk.size()); | 514 chunk_stream_.WriteData(pos, chunk.data(), chunk.size()); |
| 515 pos += chunk.size(); | 515 pos += chunk.size(); |
| 516 } | 516 } |
| 517 chunk_buffer_.clear(); | 517 chunk_buffer_.clear(); |
| 518 } | 518 } |
| 519 document_size_ = current_pos_; | 519 document_size_ = current_pos_; |
| 520 client_->OnDocumentComplete(); | 520 client_->OnDocumentComplete(); |
| 521 return; | 521 return; |
| 522 } | 522 } |
| 523 | 523 |
| 524 request_pending_ = false; | 524 request_pending_ = false; |
| 525 | 525 |
| 526 if (IsDocumentComplete()) { | 526 if (IsDocumentComplete()) { |
| 527 client_->OnDocumentComplete(); | 527 client_->OnDocumentComplete(); |
| 528 return; | 528 return; |
| 529 } | 529 } |
| 530 | 530 |
| 531 UpdateRendering(); | 531 UpdateRendering(); |
| 532 DownloadPendingRequests(); | 532 DownloadPendingRequests(); |
| 533 } | 533 } |
| 534 | 534 |
| 535 void DocumentLoader::UpdateRendering() { | 535 void DocumentLoader::UpdateRendering() { |
| 536 if (header_request_) | 536 if (header_request_) |
| 537 client_->OnPartialDocumentLoaded(); | 537 client_->OnPartialDocumentLoaded(); |
| 538 else | 538 else |
| 539 client_->OnPendingRequestComplete(); | 539 client_->OnPendingRequestComplete(); |
| 540 header_request_ = false; | 540 header_request_ = false; |
| 541 } | 541 } |
| 542 | 542 |
| 543 } // namespace chrome_pdf | 543 } // namespace chrome_pdf |
| OLD | NEW |