| 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/url_loader.h" | 11 #include "ppapi/cpp/url_loader.h" |
| 12 #include "ppapi/cpp/url_request_info.h" | 12 #include "ppapi/cpp/url_request_info.h" |
| 13 #include "ppapi/cpp/url_response_info.h" | 13 #include "ppapi/cpp/url_response_info.h" |
| 14 | 14 |
| 15 namespace chrome_pdf { | 15 namespace chrome_pdf { |
| 16 | 16 |
| 17 namespace { |
| 18 |
| 17 // Document below size will be downloaded in one chunk. | 19 // Document below size will be downloaded in one chunk. |
| 18 const uint32 kMinFileSize = 64*1024; | 20 const uint32_t kMinFileSize = 64 * 1024; |
| 21 |
| 22 // If the headers have a byte-range response, writes the start and end |
| 23 // positions and returns true if at least the start position was parsed. |
| 24 // The end position will be set to 0 if it was not found or parsed from the |
| 25 // response. |
| 26 // Returns false if not even a start position could be parsed. |
| 27 bool GetByteRange(const std::string& headers, uint32_t* start, uint32_t* end) { |
| 28 net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n"); |
| 29 while (it.GetNext()) { |
| 30 if (LowerCaseEqualsASCII(it.name(), "content-range")) { |
| 31 std::string range = it.values().c_str(); |
| 32 if (StartsWithASCII(range, "bytes", false)) { |
| 33 range = range.substr(strlen("bytes")); |
| 34 std::string::size_type pos = range.find('-'); |
| 35 std::string range_end; |
| 36 if (pos != std::string::npos) |
| 37 range_end = range.substr(pos + 1); |
| 38 TrimWhitespaceASCII(range, base::TRIM_LEADING, &range); |
| 39 TrimWhitespaceASCII(range_end, base::TRIM_LEADING, &range_end); |
| 40 *start = atoi(range.c_str()); |
| 41 *end = atoi(range_end.c_str()); |
| 42 return true; |
| 43 } |
| 44 } |
| 45 } |
| 46 return false; |
| 47 } |
| 48 |
| 49 // If the headers have a multi-part response, returns the boundary name. |
| 50 // Otherwise returns an empty string. |
| 51 std::string GetMultiPartBoundary(const std::string& headers) { |
| 52 net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n"); |
| 53 while (it.GetNext()) { |
| 54 if (LowerCaseEqualsASCII(it.name(), "content-type")) { |
| 55 std::string type = base::StringToLowerASCII(it.values()); |
| 56 if (StartsWithASCII(type, "multipart/", true)) { |
| 57 const char* boundary = strstr(type.c_str(), "boundary="); |
| 58 if (!boundary) { |
| 59 NOTREACHED(); |
| 60 break; |
| 61 } |
| 62 |
| 63 return std::string(boundary + 9); |
| 64 } |
| 65 } |
| 66 } |
| 67 return std::string(); |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
| 72 DocumentLoader::Client::~Client() { |
| 73 } |
| 19 | 74 |
| 20 DocumentLoader::DocumentLoader(Client* client) | 75 DocumentLoader::DocumentLoader(Client* client) |
| 21 : client_(client), partial_document_(false), request_pending_(false), | 76 : client_(client), partial_document_(false), request_pending_(false), |
| 22 current_pos_(0), current_chunk_size_(0), current_chunk_read_(0), | 77 current_pos_(0), current_chunk_size_(0), current_chunk_read_(0), |
| 23 document_size_(0), header_request_(true), is_multipart_(false) { | 78 document_size_(0), header_request_(true), is_multipart_(false) { |
| 24 loader_factory_.Initialize(this); | 79 loader_factory_.Initialize(this); |
| 25 } | 80 } |
| 26 | 81 |
| 27 DocumentLoader::~DocumentLoader() { | 82 DocumentLoader::~DocumentLoader() { |
| 28 } | 83 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 41 pp::URLResponseInfo response = loader_.GetResponseInfo(); | 96 pp::URLResponseInfo response = loader_.GetResponseInfo(); |
| 42 pp::Var headers_var = response.GetHeaders(); | 97 pp::Var headers_var = response.GetHeaders(); |
| 43 | 98 |
| 44 if (headers_var.is_string()) { | 99 if (headers_var.is_string()) { |
| 45 response_headers = headers_var.AsString(); | 100 response_headers = headers_var.AsString(); |
| 46 } | 101 } |
| 47 } | 102 } |
| 48 | 103 |
| 49 bool accept_ranges_bytes = false; | 104 bool accept_ranges_bytes = false; |
| 50 bool content_encoded = false; | 105 bool content_encoded = false; |
| 51 uint32 content_length = 0; | 106 uint32_t content_length = 0; |
| 52 std::string type; | 107 std::string type; |
| 53 std::string disposition; | 108 std::string disposition; |
| 54 if (!response_headers.empty()) { | 109 if (!response_headers.empty()) { |
| 55 net::HttpUtil::HeadersIterator it(response_headers.begin(), | 110 net::HttpUtil::HeadersIterator it(response_headers.begin(), |
| 56 response_headers.end(), "\n"); | 111 response_headers.end(), "\n"); |
| 57 while (it.GetNext()) { | 112 while (it.GetNext()) { |
| 58 if (LowerCaseEqualsASCII(it.name(), "content-length")) { | 113 if (LowerCaseEqualsASCII(it.name(), "content-length")) { |
| 59 content_length = atoi(it.values().c_str()); | 114 content_length = atoi(it.values().c_str()); |
| 60 } else if (LowerCaseEqualsASCII(it.name(), "accept-ranges")) { | 115 } else if (LowerCaseEqualsASCII(it.name(), "accept-ranges")) { |
| 61 accept_ranges_bytes = LowerCaseEqualsASCII(it.values(), "bytes"); | 116 accept_ranges_bytes = LowerCaseEqualsASCII(it.values(), "bytes"); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 chunk_buffer_.clear(); | 175 chunk_buffer_.clear(); |
| 121 ReadMore(); | 176 ReadMore(); |
| 122 } | 177 } |
| 123 | 178 |
| 124 bool DocumentLoader::IsDocumentComplete() const { | 179 bool DocumentLoader::IsDocumentComplete() const { |
| 125 if (document_size_ == 0) // Document size unknown. | 180 if (document_size_ == 0) // Document size unknown. |
| 126 return false; | 181 return false; |
| 127 return IsDataAvailable(0, document_size_); | 182 return IsDataAvailable(0, document_size_); |
| 128 } | 183 } |
| 129 | 184 |
| 130 uint32 DocumentLoader::GetAvailableData() const { | 185 uint32_t DocumentLoader::GetAvailableData() const { |
| 131 if (document_size_ == 0) { // If document size is unknown. | 186 if (document_size_ == 0) { // If document size is unknown. |
| 132 return current_pos_; | 187 return current_pos_; |
| 133 } | 188 } |
| 134 | 189 |
| 135 std::vector<std::pair<size_t, size_t> > ranges; | 190 std::vector<std::pair<size_t, size_t> > ranges; |
| 136 chunk_stream_.GetMissedRanges(0, document_size_, &ranges); | 191 chunk_stream_.GetMissedRanges(0, document_size_, &ranges); |
| 137 uint32 available = document_size_; | 192 uint32_t available = document_size_; |
| 138 std::vector<std::pair<size_t, size_t> >::iterator it; | 193 for (const auto& range : ranges) |
| 139 for (it = ranges.begin(); it != ranges.end(); ++it) { | 194 available -= range.second; |
| 140 available -= it->second; | |
| 141 } | |
| 142 return available; | 195 return available; |
| 143 } | 196 } |
| 144 | 197 |
| 145 void DocumentLoader::ClearPendingRequests() { | 198 void DocumentLoader::ClearPendingRequests() { |
| 146 // The first item in the queue is pending (need to keep it in the queue). | 199 // The first item in the queue is pending (need to keep it in the queue). |
| 147 if (pending_requests_.size() > 1) { | 200 if (pending_requests_.size() > 1) { |
| 148 // Remove all elements except the first one. | 201 // Remove all elements except the first one. |
| 149 pending_requests_.erase(++pending_requests_.begin(), | 202 pending_requests_.erase(++pending_requests_.begin(), |
| 150 pending_requests_.end()); | 203 pending_requests_.end()); |
| 151 } | 204 } |
| 152 } | 205 } |
| 153 | 206 |
| 154 bool DocumentLoader::GetBlock(uint32 position, uint32 size, void* buf) const { | 207 bool DocumentLoader::GetBlock(uint32_t position, |
| 208 uint32_t size, |
| 209 void* buf) const { |
| 155 return chunk_stream_.ReadData(position, size, buf); | 210 return chunk_stream_.ReadData(position, size, buf); |
| 156 } | 211 } |
| 157 | 212 |
| 158 bool DocumentLoader::IsDataAvailable(uint32 position, uint32 size) const { | 213 bool DocumentLoader::IsDataAvailable(uint32_t position, uint32_t size) const { |
| 159 return chunk_stream_.IsRangeAvailable(position, size); | 214 return chunk_stream_.IsRangeAvailable(position, size); |
| 160 } | 215 } |
| 161 | 216 |
| 162 void DocumentLoader::RequestData(uint32 position, uint32 size) { | 217 void DocumentLoader::RequestData(uint32_t position, uint32_t size) { |
| 163 DCHECK(partial_document_); | 218 DCHECK(partial_document_); |
| 164 | 219 |
| 165 // We have some artefact request from | 220 // We have some artefact request from |
| 166 // PDFiumEngine::OnDocumentComplete() -> FPDFAvail_IsPageAvail after | 221 // PDFiumEngine::OnDocumentComplete() -> FPDFAvail_IsPageAvail after |
| 167 // document is complete. | 222 // document is complete. |
| 168 // We need this fix in PDFIum. Adding this as a work around. | 223 // We need this fix in PDFIum. Adding this as a work around. |
| 169 // Bug: http://code.google.com/p/chromium/issues/detail?id=79996 | 224 // Bug: http://code.google.com/p/chromium/issues/detail?id=79996 |
| 170 // Test url: | 225 // Test url: |
| 171 // http://www.icann.org/en/correspondence/holtzman-to-jeffrey-02mar11-en.pdf | 226 // http://www.icann.org/en/correspondence/holtzman-to-jeffrey-02mar11-en.pdf |
| 172 if (IsDocumentComplete()) | 227 if (IsDocumentComplete()) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 185 // queue. ReadComplete() will remove the last pending comment from the queue. | 240 // queue. ReadComplete() will remove the last pending comment from the queue. |
| 186 while (pending_requests_.size() > 1) { | 241 while (pending_requests_.size() > 1) { |
| 187 if (IsDataAvailable(pending_requests_.front().first, | 242 if (IsDataAvailable(pending_requests_.front().first, |
| 188 pending_requests_.front().second)) { | 243 pending_requests_.front().second)) { |
| 189 pending_requests_.pop_front(); | 244 pending_requests_.pop_front(); |
| 190 } else { | 245 } else { |
| 191 break; | 246 break; |
| 192 } | 247 } |
| 193 } | 248 } |
| 194 | 249 |
| 195 uint32 pos = pending_requests_.front().first; | 250 uint32_t pos = pending_requests_.front().first; |
| 196 uint32 size = pending_requests_.front().second; | 251 uint32_t size = pending_requests_.front().second; |
| 197 if (IsDataAvailable(pos, size)) { | 252 if (IsDataAvailable(pos, size)) { |
| 198 ReadComplete(); | 253 ReadComplete(); |
| 199 return; | 254 return; |
| 200 } | 255 } |
| 201 | 256 |
| 202 // If current request has been partially downloaded already, split it into | 257 // If current request has been partially downloaded already, split it into |
| 203 // a few smaller requests. | 258 // a few smaller requests. |
| 204 std::vector<std::pair<size_t, size_t> > ranges; | 259 std::vector<std::pair<size_t, size_t> > ranges; |
| 205 chunk_stream_.GetMissedRanges(pos, size, &ranges); | 260 chunk_stream_.GetMissedRanges(pos, size, &ranges); |
| 206 if (ranges.size() > 0) { | 261 if (!ranges.empty()) { |
| 207 pending_requests_.pop_front(); | 262 pending_requests_.pop_front(); |
| 208 pending_requests_.insert(pending_requests_.begin(), | 263 pending_requests_.insert(pending_requests_.begin(), |
| 209 ranges.begin(), ranges.end()); | 264 ranges.begin(), ranges.end()); |
| 210 pos = pending_requests_.front().first; | 265 pos = pending_requests_.front().first; |
| 211 size = pending_requests_.front().second; | 266 size = pending_requests_.front().second; |
| 212 } | 267 } |
| 213 | 268 |
| 214 uint32 cur_request_size = GetRequestSize(); | 269 uint32_t cur_request_size = GetRequestSize(); |
| 215 // If size is less than default request, try to expand download range for | 270 // If size is less than default request, try to expand download range for |
| 216 // more optimal download. | 271 // more optimal download. |
| 217 if (size < cur_request_size && partial_document_) { | 272 if (size < cur_request_size && partial_document_) { |
| 218 // First, try to expand block towards the end of the file. | 273 // First, try to expand block towards the end of the file. |
| 219 uint32 new_pos = pos; | 274 uint32_t new_pos = pos; |
| 220 uint32 new_size = cur_request_size; | 275 uint32_t new_size = cur_request_size; |
| 221 if (pos + new_size > document_size_) | 276 if (pos + new_size > document_size_) |
| 222 new_size = document_size_ - pos; | 277 new_size = document_size_ - pos; |
| 223 | 278 |
| 224 std::vector<std::pair<size_t, size_t> > ranges; | 279 std::vector<std::pair<size_t, size_t> > ranges; |
| 225 if (chunk_stream_.GetMissedRanges(new_pos, new_size, &ranges)) { | 280 if (chunk_stream_.GetMissedRanges(new_pos, new_size, &ranges)) { |
| 226 new_pos = ranges[0].first; | 281 new_pos = ranges[0].first; |
| 227 new_size = ranges[0].second; | 282 new_size = ranges[0].second; |
| 228 } | 283 } |
| 229 | 284 |
| 230 // Second, try to expand block towards the beginning of the file. | 285 // Second, try to expand block towards the beginning of the file. |
| 231 if (new_size < cur_request_size) { | 286 if (new_size < cur_request_size) { |
| 232 uint32 block_end = new_pos + new_size; | 287 uint32_t block_end = new_pos + new_size; |
| 233 if (block_end > cur_request_size) { | 288 if (block_end > cur_request_size) { |
| 234 new_pos = block_end - cur_request_size; | 289 new_pos = block_end - cur_request_size; |
| 235 } else { | 290 } else { |
| 236 new_pos = 0; | 291 new_pos = 0; |
| 237 } | 292 } |
| 238 new_size = block_end - new_pos; | 293 new_size = block_end - new_pos; |
| 239 | 294 |
| 240 if (chunk_stream_.GetMissedRanges(new_pos, new_size, &ranges)) { | 295 if (chunk_stream_.GetMissedRanges(new_pos, new_size, &ranges)) { |
| 241 new_pos = ranges.back().first; | 296 new_pos = ranges.back().first; |
| 242 new_size = ranges.back().second; | 297 new_size = ranges.back().second; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 264 loader_ = client_->CreateURLLoader(); | 319 loader_ = client_->CreateURLLoader(); |
| 265 pp::CompletionCallback callback = | 320 pp::CompletionCallback callback = |
| 266 loader_factory_.NewCallback(&DocumentLoader::DidOpen); | 321 loader_factory_.NewCallback(&DocumentLoader::DidOpen); |
| 267 pp::URLRequestInfo request = GetRequest(pos, size); | 322 pp::URLRequestInfo request = GetRequest(pos, size); |
| 268 requests_count_++; | 323 requests_count_++; |
| 269 int rv = loader_.Open(request, callback); | 324 int rv = loader_.Open(request, callback); |
| 270 if (rv != PP_OK_COMPLETIONPENDING) | 325 if (rv != PP_OK_COMPLETIONPENDING) |
| 271 callback.Run(rv); | 326 callback.Run(rv); |
| 272 } | 327 } |
| 273 | 328 |
| 274 pp::URLRequestInfo DocumentLoader::GetRequest(uint32 position, | 329 pp::URLRequestInfo DocumentLoader::GetRequest(uint32_t position, |
| 275 uint32 size) const { | 330 uint32_t size) const { |
| 276 pp::URLRequestInfo request(client_->GetPluginInstance()); | 331 pp::URLRequestInfo request(client_->GetPluginInstance()); |
| 277 request.SetURL(url_); | 332 request.SetURL(url_); |
| 278 request.SetMethod("GET"); | 333 request.SetMethod("GET"); |
| 279 request.SetFollowRedirects(true); | 334 request.SetFollowRedirects(true); |
| 280 request.SetCustomReferrerURL(url_); | 335 request.SetCustomReferrerURL(url_); |
| 281 | 336 |
| 282 const size_t kBufSize = 100; | 337 const size_t kBufSize = 100; |
| 283 char buf[kBufSize]; | 338 char buf[kBufSize]; |
| 284 // According to rfc2616, byte range specifies position of the first and last | 339 // According to rfc2616, byte range specifies position of the first and last |
| 285 // bytes in the requested range inclusively. Therefore we should subtract 1 | 340 // bytes in the requested range inclusively. Therefore we should subtract 1 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 311 is_multipart_ = false; | 366 is_multipart_ = false; |
| 312 current_chunk_size_ = 0; | 367 current_chunk_size_ = 0; |
| 313 current_chunk_read_ = 0; | 368 current_chunk_read_ = 0; |
| 314 | 369 |
| 315 pp::Var headers_var = loader_.GetResponseInfo().GetHeaders(); | 370 pp::Var headers_var = loader_.GetResponseInfo().GetHeaders(); |
| 316 std::string headers; | 371 std::string headers; |
| 317 if (headers_var.is_string()) | 372 if (headers_var.is_string()) |
| 318 headers = headers_var.AsString(); | 373 headers = headers_var.AsString(); |
| 319 | 374 |
| 320 std::string boundary = GetMultiPartBoundary(headers); | 375 std::string boundary = GetMultiPartBoundary(headers); |
| 321 if (boundary.size()) { | 376 if (!boundary.empty()) { |
| 322 // Leave position untouched for now, when we read the data we'll get it. | 377 // Leave position untouched for now, when we read the data we'll get it. |
| 323 is_multipart_ = true; | 378 is_multipart_ = true; |
| 324 multipart_boundary_ = boundary; | 379 multipart_boundary_ = boundary; |
| 325 } else { | 380 } else { |
| 326 // Need to make sure that the server returned a byte-range, since it's | 381 // Need to make sure that the server returned a byte-range, since it's |
| 327 // possible for a server to just ignore our bye-range request and just | 382 // possible for a server to just ignore our bye-range request and just |
| 328 // return the entire document even if it supports byte-range requests. | 383 // return the entire document even if it supports byte-range requests. |
| 329 // i.e. sniff response to | 384 // i.e. sniff response to |
| 330 // http://www.act.org/compass/sample/pdf/geometry.pdf | 385 // http://www.act.org/compass/sample/pdf/geometry.pdf |
| 331 current_pos_ = 0; | 386 current_pos_ = 0; |
| 332 uint32 start_pos, end_pos; | 387 uint32_t start_pos, end_pos; |
| 333 if (GetByteRange(headers, &start_pos, &end_pos)) { | 388 if (GetByteRange(headers, &start_pos, &end_pos)) { |
| 334 current_pos_ = start_pos; | 389 current_pos_ = start_pos; |
| 335 if (end_pos && end_pos > start_pos) | 390 if (end_pos && end_pos > start_pos) |
| 336 current_chunk_size_ = end_pos - start_pos + 1; | 391 current_chunk_size_ = end_pos - start_pos + 1; |
| 337 } | 392 } |
| 338 } | 393 } |
| 339 | 394 |
| 340 ReadMore(); | 395 ReadMore(); |
| 341 } | 396 } |
| 342 | 397 |
| 343 bool DocumentLoader::GetByteRange(const std::string& headers, uint32* start, | |
| 344 uint32* end) { | |
| 345 net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n"); | |
| 346 while (it.GetNext()) { | |
| 347 if (LowerCaseEqualsASCII(it.name(), "content-range")) { | |
| 348 std::string range = it.values().c_str(); | |
| 349 if (StartsWithASCII(range, "bytes", false)) { | |
| 350 range = range.substr(strlen("bytes")); | |
| 351 std::string::size_type pos = range.find('-'); | |
| 352 std::string range_end; | |
| 353 if (pos != std::string::npos) | |
| 354 range_end = range.substr(pos + 1); | |
| 355 TrimWhitespaceASCII(range, base::TRIM_LEADING, &range); | |
| 356 TrimWhitespaceASCII(range_end, base::TRIM_LEADING, &range_end); | |
| 357 *start = atoi(range.c_str()); | |
| 358 *end = atoi(range_end.c_str()); | |
| 359 return true; | |
| 360 } | |
| 361 } | |
| 362 } | |
| 363 return false; | |
| 364 } | |
| 365 | |
| 366 std::string DocumentLoader::GetMultiPartBoundary(const std::string& headers) { | |
| 367 net::HttpUtil::HeadersIterator it(headers.begin(), headers.end(), "\n"); | |
| 368 while (it.GetNext()) { | |
| 369 if (LowerCaseEqualsASCII(it.name(), "content-type")) { | |
| 370 std::string type = base::StringToLowerASCII(it.values()); | |
| 371 if (StartsWithASCII(type, "multipart/", true)) { | |
| 372 const char* boundary = strstr(type.c_str(), "boundary="); | |
| 373 if (!boundary) { | |
| 374 NOTREACHED(); | |
| 375 break; | |
| 376 } | |
| 377 | |
| 378 return std::string(boundary + 9); | |
| 379 } | |
| 380 } | |
| 381 } | |
| 382 return std::string(); | |
| 383 } | |
| 384 | |
| 385 void DocumentLoader::ReadMore() { | 398 void DocumentLoader::ReadMore() { |
| 386 pp::CompletionCallback callback = | 399 pp::CompletionCallback callback = |
| 387 loader_factory_.NewCallback(&DocumentLoader::DidRead); | 400 loader_factory_.NewCallback(&DocumentLoader::DidRead); |
| 388 int rv = loader_.ReadResponseBody(buffer_, sizeof(buffer_), callback); | 401 int rv = loader_.ReadResponseBody(buffer_, sizeof(buffer_), callback); |
| 389 if (rv != PP_OK_COMPLETIONPENDING) | 402 if (rv != PP_OK_COMPLETIONPENDING) |
| 390 callback.Run(rv); | 403 callback.Run(rv); |
| 391 } | 404 } |
| 392 | 405 |
| 393 void DocumentLoader::DidRead(int32_t result) { | 406 void DocumentLoader::DidRead(int32_t result) { |
| 394 if (result > 0) { | 407 if (result > 0) { |
| 395 char* start = buffer_; | 408 char* start = buffer_; |
| 396 size_t length = result; | 409 size_t length = result; |
| 397 if (is_multipart_ && result > 2) { | 410 if (is_multipart_ && result > 2) { |
| 398 for (int i = 2; i < result; ++i) { | 411 for (int i = 2; i < result; ++i) { |
| 399 if ((buffer_[i - 1] == '\n' && buffer_[i - 2] == '\n') || | 412 if ((buffer_[i - 1] == '\n' && buffer_[i - 2] == '\n') || |
| 400 (i >= 4 && | 413 (i >= 4 && |
| 401 buffer_[i - 1] == '\n' && buffer_[i - 2] == '\r' && | 414 buffer_[i - 1] == '\n' && buffer_[i - 2] == '\r' && |
| 402 buffer_[i - 3] == '\n' && buffer_[i - 4] == '\r')) { | 415 buffer_[i - 3] == '\n' && buffer_[i - 4] == '\r')) { |
| 403 uint32 start_pos, end_pos; | 416 uint32_t start_pos, end_pos; |
| 404 if (GetByteRange(std::string(buffer_, i), &start_pos, &end_pos)) { | 417 if (GetByteRange(std::string(buffer_, i), &start_pos, &end_pos)) { |
| 405 current_pos_ = start_pos; | 418 current_pos_ = start_pos; |
| 406 start += i; | 419 start += i; |
| 407 length -= i; | 420 length -= i; |
| 408 if (end_pos && end_pos > start_pos) | 421 if (end_pos && end_pos > start_pos) |
| 409 current_chunk_size_ = end_pos - start_pos + 1; | 422 current_chunk_size_ = end_pos - start_pos + 1; |
| 410 } | 423 } |
| 411 break; | 424 break; |
| 412 } | 425 } |
| 413 } | 426 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 } | 461 } |
| 449 } | 462 } |
| 450 | 463 |
| 451 void DocumentLoader::ReadComplete() { | 464 void DocumentLoader::ReadComplete() { |
| 452 if (!partial_document_) { | 465 if (!partial_document_) { |
| 453 if (document_size_ == 0) { | 466 if (document_size_ == 0) { |
| 454 // For the document with no 'content-length" specified we've collected all | 467 // For the document with no 'content-length" specified we've collected all |
| 455 // the chunks already. Let's allocate final document buffer and copy them | 468 // the chunks already. Let's allocate final document buffer and copy them |
| 456 // over. | 469 // over. |
| 457 chunk_stream_.Preallocate(current_pos_); | 470 chunk_stream_.Preallocate(current_pos_); |
| 458 uint32 pos = 0; | 471 uint32_t pos = 0; |
| 459 std::list<std::vector<unsigned char> >::iterator it; | 472 for (auto& chunk : chunk_buffer_) { |
| 460 for (it = chunk_buffer_.begin(); it != chunk_buffer_.end(); ++it) { | 473 chunk_stream_.WriteData(pos, &(chunk[0]), chunk.size()); |
| 461 chunk_stream_.WriteData(pos, &((*it)[0]), it->size()); | 474 pos += chunk.size(); |
| 462 pos += it->size(); | |
| 463 } | 475 } |
| 464 chunk_buffer_.clear(); | 476 chunk_buffer_.clear(); |
| 465 } | 477 } |
| 466 document_size_ = current_pos_; | 478 document_size_ = current_pos_; |
| 467 client_->OnDocumentComplete(); | 479 client_->OnDocumentComplete(); |
| 468 return; | 480 return; |
| 469 } | 481 } |
| 470 | 482 |
| 471 request_pending_ = false; | 483 request_pending_ = false; |
| 472 pending_requests_.pop_front(); | 484 pending_requests_.pop_front(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 487 else | 499 else |
| 488 client_->OnPendingRequestComplete(); | 500 client_->OnPendingRequestComplete(); |
| 489 header_request_ = false; | 501 header_request_ = false; |
| 490 | 502 |
| 491 // The OnPendingRequestComplete could have added more requests. | 503 // The OnPendingRequestComplete could have added more requests. |
| 492 if (!pending_requests_.empty()) { | 504 if (!pending_requests_.empty()) { |
| 493 DownloadPendingRequests(); | 505 DownloadPendingRequests(); |
| 494 } else { | 506 } else { |
| 495 // Document is not complete and we have no outstanding requests. | 507 // Document is not complete and we have no outstanding requests. |
| 496 // Let's keep downloading PDF file in small chunks. | 508 // Let's keep downloading PDF file in small chunks. |
| 497 uint32 pos = chunk_stream_.GetFirstMissingByte(); | 509 uint32_t pos = chunk_stream_.GetFirstMissingByte(); |
| 498 std::vector<std::pair<size_t, size_t> > ranges; | 510 std::vector<std::pair<size_t, size_t> > ranges; |
| 499 chunk_stream_.GetMissedRanges(pos, GetRequestSize(), &ranges); | 511 chunk_stream_.GetMissedRanges(pos, GetRequestSize(), &ranges); |
| 500 DCHECK(ranges.size() > 0); | 512 DCHECK(!ranges.empty()); |
| 501 RequestData(ranges[0].first, ranges[0].second); | 513 RequestData(ranges[0].first, ranges[0].second); |
| 502 } | 514 } |
| 503 } | 515 } |
| 504 | 516 |
| 505 uint32 DocumentLoader::GetRequestSize() const { | 517 uint32_t DocumentLoader::GetRequestSize() const { |
| 506 // Document loading strategy: | 518 // Document loading strategy: |
| 507 // For first 10 requests, we use 32k chunk sizes, for the next 10 requests we | 519 // For first 10 requests, we use 32k chunk sizes, for the next 10 requests we |
| 508 // double the size (64k), and so on, until we cap max request size at 2M for | 520 // double the size (64k), and so on, until we cap max request size at 2M for |
| 509 // 71 or more requests. | 521 // 71 or more requests. |
| 510 uint32 limited_count = std::min(std::max(requests_count_, 10u), 70u); | 522 uint32_t limited_count = std::min(std::max(requests_count_, 10u), 70u); |
| 511 return 32*1024 * (1 << ((limited_count - 1) / 10u)); | 523 return 32 * 1024 * (1 << ((limited_count - 1) / 10u)); |
| 512 } | 524 } |
| 513 | 525 |
| 514 } // namespace chrome_pdf | 526 } // namespace chrome_pdf |
| OLD | NEW |