Chromium Code Reviews| 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 "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "net/http/http_util.h" | 9 #include "net/http/http_util.h" |
| 10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/cpp/dev/buffer_dev.h" | |
| 11 #include "ppapi/cpp/url_loader.h" | 12 #include "ppapi/cpp/url_loader.h" |
| 12 #include "ppapi/cpp/url_request_info.h" | 13 #include "ppapi/cpp/url_request_info.h" |
| 13 #include "ppapi/cpp/url_response_info.h" | 14 #include "ppapi/cpp/url_response_info.h" |
| 14 | 15 |
| 15 namespace chrome_pdf { | 16 namespace chrome_pdf { |
| 16 | 17 |
| 17 // Document below size will be downloaded in one chunk. | 18 // Document below size will be downloaded in one chunk. |
| 18 const uint32 kMinFileSize = 64*1024; | 19 const uint32 kMinFileSize = 64*1024; |
| 19 | 20 |
| 20 DocumentLoader::DocumentLoader(Client* client) | 21 DocumentLoader::DocumentLoader(Client* client) |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 chunk_buffer_.clear(); | 121 chunk_buffer_.clear(); |
| 121 ReadMore(); | 122 ReadMore(); |
| 122 } | 123 } |
| 123 | 124 |
| 124 bool DocumentLoader::IsDocumentComplete() const { | 125 bool DocumentLoader::IsDocumentComplete() const { |
| 125 if (document_size_ == 0) // Document size unknown. | 126 if (document_size_ == 0) // Document size unknown. |
| 126 return false; | 127 return false; |
| 127 return IsDataAvailable(0, document_size_); | 128 return IsDataAvailable(0, document_size_); |
| 128 } | 129 } |
| 129 | 130 |
| 131 void DocumentLoader::GetCompleteDocument( | |
| 132 pp::CompletionCallbackWithOutput<pp::FileRef> callback) { | |
|
Lei Zhang
2014/06/05 07:23:54
nit: indentation
raymes
2014/06/06 01:57:13
Done.
| |
| 133 if (!IsDocumentComplete() || chunk_stream_.data().size() == 0) { | |
|
Lei Zhang
2014/06/05 07:23:54
nit: size() == 0 -> empty()
raymes
2014/06/06 01:57:13
Done.
| |
| 134 callback.Run(PP_ERROR_FAILED); | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 if (!file_writer_) { | |
| 139 file_writer_.reset(new FileWriter( | |
| 140 client_->GetPluginInstance(), | |
| 141 "pdf", | |
|
Lei Zhang
2014/06/05 07:23:54
Probably ok for now, but shouldn't the caller norm
raymes
2014/06/06 01:57:13
This will just be a temporary filename. The downlo
| |
| 142 reinterpret_cast<const char*>(&chunk_stream_.data()[0]), | |
|
Lei Zhang
2014/06/05 07:23:54
Maybe just have FileWriter hold a const std::strin
| |
| 143 chunk_stream_.data().size())); | |
| 144 } | |
| 145 | |
| 146 file_writer_->GetFile(callback); | |
| 147 } | |
| 148 | |
| 130 uint32 DocumentLoader::GetAvailableData() const { | 149 uint32 DocumentLoader::GetAvailableData() const { |
| 131 if (document_size_ == 0) { // If document size is unknown. | 150 if (document_size_ == 0) { // If document size is unknown. |
| 132 return current_pos_; | 151 return current_pos_; |
| 133 } | 152 } |
| 134 | 153 |
| 135 std::vector<std::pair<size_t, size_t> > ranges; | 154 std::vector<std::pair<size_t, size_t> > ranges; |
| 136 chunk_stream_.GetMissedRanges(0, document_size_, &ranges); | 155 chunk_stream_.GetMissedRanges(0, document_size_, &ranges); |
| 137 uint32 available = document_size_; | 156 uint32 available = document_size_; |
| 138 std::vector<std::pair<size_t, size_t> >::iterator it; | 157 std::vector<std::pair<size_t, size_t> >::iterator it; |
| 139 for (it = ranges.begin(); it != ranges.end(); ++it) { | 158 for (it = ranges.begin(); it != ranges.end(); ++it) { |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 495 uint32 DocumentLoader::GetRequestSize() const { | 514 uint32 DocumentLoader::GetRequestSize() const { |
| 496 // Document loading strategy: | 515 // Document loading strategy: |
| 497 // For first 10 requests, we use 32k chunk sizes, for the next 10 requests we | 516 // For first 10 requests, we use 32k chunk sizes, for the next 10 requests we |
| 498 // double the size (64k), and so on, until we cap max request size at 2M for | 517 // double the size (64k), and so on, until we cap max request size at 2M for |
| 499 // 71 or more requests. | 518 // 71 or more requests. |
| 500 uint32 limited_count = std::min(std::max(requests_count_, 10u), 70u); | 519 uint32 limited_count = std::min(std::max(requests_count_, 10u), 70u); |
| 501 return 32*1024 * (1 << ((limited_count - 1) / 10u)); | 520 return 32*1024 * (1 << ((limited_count - 1) / 10u)); |
| 502 } | 521 } |
| 503 | 522 |
| 504 } // namespace chrome_pdf | 523 } // namespace chrome_pdf |
| OLD | NEW |