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 "storage/browser/blob/blob_url_request_job.h" | 5 #include "storage/browser/blob/blob_url_request_job.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/files/file_util_proxy.h" | 15 #include "base/files/file_util_proxy.h" |
16 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
18 #include "base/numerics/safe_conversions.h" | 18 #include "base/numerics/safe_conversions.h" |
19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
20 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
21 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
22 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
23 #include "net/base/io_buffer.h" | 23 #include "net/base/io_buffer.h" |
24 #include "net/base/net_errors.h" | 24 #include "net/base/net_errors.h" |
| 25 #include "net/disk_cache/disk_cache.h" |
25 #include "net/http/http_request_headers.h" | 26 #include "net/http/http_request_headers.h" |
26 #include "net/http/http_response_headers.h" | 27 #include "net/http/http_response_headers.h" |
27 #include "net/http/http_response_info.h" | 28 #include "net/http/http_response_info.h" |
28 #include "net/http/http_util.h" | 29 #include "net/http/http_util.h" |
29 #include "net/url_request/url_request.h" | 30 #include "net/url_request/url_request.h" |
30 #include "net/url_request/url_request_context.h" | 31 #include "net/url_request/url_request_context.h" |
31 #include "net/url_request/url_request_error_job.h" | 32 #include "net/url_request/url_request_error_job.h" |
32 #include "net/url_request/url_request_status.h" | 33 #include "net/url_request/url_request_status.h" |
33 #include "storage/browser/fileapi/file_stream_reader.h" | 34 #include "storage/browser/fileapi/file_stream_reader.h" |
34 #include "storage/browser/fileapi/file_system_context.h" | 35 #include "storage/browser/fileapi/file_system_context.h" |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 // If nothing to read for current item, advance to next item. | 350 // If nothing to read for current item, advance to next item. |
350 if (bytes_to_read == 0) { | 351 if (bytes_to_read == 0) { |
351 AdvanceItem(); | 352 AdvanceItem(); |
352 return true; | 353 return true; |
353 } | 354 } |
354 | 355 |
355 // Do the reading. | 356 // Do the reading. |
356 const BlobDataItem& item = *items.at(current_item_index_); | 357 const BlobDataItem& item = *items.at(current_item_index_); |
357 if (item.type() == DataElement::TYPE_BYTES) | 358 if (item.type() == DataElement::TYPE_BYTES) |
358 return ReadBytesItem(item, bytes_to_read); | 359 return ReadBytesItem(item, bytes_to_read); |
| 360 if (item.type() == DataElement::TYPE_DISK_CACHE_ENTRY) |
| 361 return ReadDiskCacheEntryItem(item, bytes_to_read); |
359 if (!IsFileType(item.type())) { | 362 if (!IsFileType(item.type())) { |
360 NOTREACHED(); | 363 NOTREACHED(); |
361 return false; | 364 return false; |
362 } | 365 } |
363 storage::FileStreamReader* const reader = | 366 storage::FileStreamReader* const reader = |
364 GetFileStreamReader(current_item_index_); | 367 GetFileStreamReader(current_item_index_); |
365 if (!reader) { | 368 if (!reader) { |
366 NotifyFailure(net::ERR_FAILED); | 369 NotifyFailure(net::ERR_FAILED); |
367 return false; | 370 return false; |
368 } | 371 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 memcpy(read_buf_->data(), | 408 memcpy(read_buf_->data(), |
406 item.bytes() + item.offset() + current_item_offset_, | 409 item.bytes() + item.offset() + current_item_offset_, |
407 bytes_to_read); | 410 bytes_to_read); |
408 | 411 |
409 AdvanceBytesRead(bytes_to_read); | 412 AdvanceBytesRead(bytes_to_read); |
410 return true; | 413 return true; |
411 } | 414 } |
412 | 415 |
413 bool BlobURLRequestJob::ReadFileItem(FileStreamReader* reader, | 416 bool BlobURLRequestJob::ReadFileItem(FileStreamReader* reader, |
414 int bytes_to_read) { | 417 int bytes_to_read) { |
| 418 DCHECK(!GetStatus().is_io_pending()) |
| 419 << "Can't begin IO while another IO operation is pending."; |
415 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); | 420 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); |
416 DCHECK(reader); | 421 DCHECK(reader); |
417 int chunk_number = current_file_chunk_number_++; | 422 int chunk_number = current_file_chunk_number_++; |
418 TRACE_EVENT_ASYNC_BEGIN1("Blob", "BlobRequest::ReadFileItem", this, "uuid", | 423 TRACE_EVENT_ASYNC_BEGIN1("Blob", "BlobRequest::ReadFileItem", this, "uuid", |
419 blob_data_->uuid()); | 424 blob_data_->uuid()); |
420 const int result = | 425 const int result = |
421 reader->Read(read_buf_.get(), bytes_to_read, | 426 reader->Read(read_buf_.get(), bytes_to_read, |
422 base::Bind(&BlobURLRequestJob::DidReadFile, | 427 base::Bind(&BlobURLRequestJob::DidReadFile, |
423 base::Unretained(this), chunk_number)); | 428 weak_factory_.GetWeakPtr(), chunk_number)); |
424 if (result >= 0) { | 429 if (result >= 0) { |
425 // Data is immediately available. | 430 AdvanceBytesRead(result); |
426 if (GetStatus().is_io_pending()) | |
427 DidReadFile(chunk_number, result); | |
428 else | |
429 AdvanceBytesRead(result); | |
430 return true; | 431 return true; |
431 } | 432 } |
432 if (result == net::ERR_IO_PENDING) | 433 if (result == net::ERR_IO_PENDING) |
433 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | 434 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
434 else | 435 else |
435 NotifyFailure(result); | 436 NotifyFailure(result); |
436 return false; | 437 return false; |
437 } | 438 } |
438 | 439 |
439 void BlobURLRequestJob::DidReadFile(int chunk_number, int result) { | 440 void BlobURLRequestJob::DidReadFile(int chunk_number, int result) { |
| 441 DCHECK(GetStatus().is_io_pending()) |
| 442 << "Asynchronous IO completed while IO wasn't pending?"; |
440 TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest::ReadFileItem", this, "uuid", | 443 TRACE_EVENT_ASYNC_END1("Blob", "BlobRequest::ReadFileItem", this, "uuid", |
441 blob_data_->uuid()); | 444 blob_data_->uuid()); |
442 if (result <= 0) { | 445 if (result <= 0) { |
443 NotifyFailure(net::ERR_FAILED); | 446 NotifyFailure(result); |
444 return; | 447 return; |
445 } | 448 } |
446 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status | 449 SetStatus(net::URLRequestStatus()); // Clear the IO_PENDING status |
447 | 450 |
448 AdvanceBytesRead(result); | 451 AdvanceBytesRead(result); |
449 | 452 |
450 // If the read buffer is completely filled, we're done. | |
451 if (!read_buf_->BytesRemaining()) { | |
452 int bytes_read = BytesReadCompleted(); | |
453 NotifyReadComplete(bytes_read); | |
454 return; | |
455 } | |
456 | |
457 // Otherwise, continue the reading. | 453 // Otherwise, continue the reading. |
458 int bytes_read = 0; | 454 int bytes_read = 0; |
459 if (ReadLoop(&bytes_read)) | 455 if (ReadLoop(&bytes_read)) |
460 NotifyReadComplete(bytes_read); | 456 NotifyReadComplete(bytes_read); |
461 } | 457 } |
462 | 458 |
463 void BlobURLRequestJob::DeleteCurrentFileReader() { | 459 void BlobURLRequestJob::DeleteCurrentFileReader() { |
464 IndexToReaderMap::iterator found = index_to_reader_.find(current_item_index_); | 460 IndexToReaderMap::iterator found = index_to_reader_.find(current_item_index_); |
465 if (found != index_to_reader_.end() && found->second) { | 461 if (found != index_to_reader_.end() && found->second) { |
466 delete found->second; | 462 delete found->second; |
467 index_to_reader_.erase(found); | 463 index_to_reader_.erase(found); |
468 } | 464 } |
469 } | 465 } |
470 | 466 |
| 467 bool BlobURLRequestJob::ReadDiskCacheEntryItem(const BlobDataItem& item, |
| 468 int bytes_to_read) { |
| 469 DCHECK(!GetStatus().is_io_pending()) |
| 470 << "Can't begin IO while another IO operation is pending."; |
| 471 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); |
| 472 |
| 473 const int result = item.disk_cache_entry()->ReadData( |
| 474 item.disk_cache_stream_index(), current_item_offset_, read_buf_.get(), |
| 475 bytes_to_read, base::Bind(&BlobURLRequestJob::DidReadDiskCacheEntry, |
| 476 weak_factory_.GetWeakPtr())); |
| 477 if (result >= 0) { |
| 478 AdvanceBytesRead(result); |
| 479 return true; |
| 480 } |
| 481 if (result == net::ERR_IO_PENDING) |
| 482 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 483 else |
| 484 NotifyFailure(result); |
| 485 return false; |
| 486 } |
| 487 |
| 488 void BlobURLRequestJob::DidReadDiskCacheEntry(int result) { |
| 489 DCHECK(GetStatus().is_io_pending()) |
| 490 << "Asynchronous IO completed while IO wasn't pending?"; |
| 491 if (result <= 0) { |
| 492 NotifyFailure(result); |
| 493 return; |
| 494 } |
| 495 SetStatus(net::URLRequestStatus()); |
| 496 |
| 497 AdvanceBytesRead(result); |
| 498 |
| 499 int bytes_read = 0; |
| 500 if (ReadLoop(&bytes_read)) |
| 501 NotifyReadComplete(bytes_read); |
| 502 } |
| 503 |
471 int BlobURLRequestJob::BytesReadCompleted() { | 504 int BlobURLRequestJob::BytesReadCompleted() { |
472 int bytes_read = read_buf_->BytesConsumed(); | 505 int bytes_read = read_buf_->BytesConsumed(); |
473 read_buf_ = NULL; | 506 read_buf_ = NULL; |
474 return bytes_read; | 507 return bytes_read; |
475 } | 508 } |
476 | 509 |
477 int BlobURLRequestJob::ComputeBytesToRead() const { | 510 int BlobURLRequestJob::ComputeBytesToRead() const { |
478 int64 current_item_length = item_length_list_[current_item_index_]; | 511 int64 current_item_length = item_length_list_[current_item_index_]; |
479 | 512 |
480 int64 item_remaining = current_item_length - current_item_offset_; | 513 int64 item_remaining = current_item_length - current_item_offset_; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 | 674 |
642 default: | 675 default: |
643 break; | 676 break; |
644 } | 677 } |
645 | 678 |
646 NOTREACHED(); | 679 NOTREACHED(); |
647 return false; | 680 return false; |
648 } | 681 } |
649 | 682 |
650 } // namespace storage | 683 } // namespace storage |
OLD | NEW |