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 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 } | 464 } |
462 | 465 |
463 void BlobURLRequestJob::DeleteCurrentFileReader() { | 466 void BlobURLRequestJob::DeleteCurrentFileReader() { |
464 IndexToReaderMap::iterator found = index_to_reader_.find(current_item_index_); | 467 IndexToReaderMap::iterator found = index_to_reader_.find(current_item_index_); |
465 if (found != index_to_reader_.end() && found->second) { | 468 if (found != index_to_reader_.end() && found->second) { |
466 delete found->second; | 469 delete found->second; |
467 index_to_reader_.erase(found); | 470 index_to_reader_.erase(found); |
468 } | 471 } |
469 } | 472 } |
470 | 473 |
474 bool BlobURLRequestJob::ReadDiskCacheEntryItem(const BlobDataItem& item, | |
475 int bytes_to_read) { | |
476 DCHECK_GE(read_buf_->BytesRemaining(), bytes_to_read); | |
477 | |
478 const int result = item.disk_cache_entry()->ReadData( | |
479 item.disk_cache_stream_index(), current_item_offset_, read_buf_.get(), | |
480 bytes_to_read, base::Bind(&BlobURLRequestJob::DidReadDiskCacheEntry, | |
481 base::Unretained(this))); | |
michaeln
2015/06/12 23:43:06
2 questions:
1) How is base::Unretained here safe
| |
482 if (result >= 0) { | |
483 // Data is immediately available. | |
484 if (GetStatus().is_io_pending()) | |
485 DidReadDiskCacheEntry(result); | |
486 else | |
487 AdvanceBytesRead(result); | |
488 return true; | |
489 } | |
490 if (result == net::ERR_IO_PENDING) | |
491 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); | |
492 else | |
493 NotifyFailure(result); | |
494 return false; | |
495 } | |
496 | |
497 void BlobURLRequestJob::DidReadDiskCacheEntry(int result) { | |
498 if (result <= 0) { | |
499 NotifyFailure(net::ERR_FAILED); | |
500 return; | |
501 } | |
502 SetStatus(net::URLRequestStatus()); | |
503 | |
504 AdvanceBytesRead(result); | |
505 | |
506 if (!read_buf_->BytesRemaining()) { | |
507 int bytes_read = BytesReadCompleted(); | |
508 NotifyReadComplete(bytes_read); | |
509 return; | |
510 } | |
511 | |
512 int bytes_read = 0; | |
513 if (ReadLoop(&bytes_read)) | |
514 NotifyReadComplete(bytes_read); | |
515 } | |
516 | |
471 int BlobURLRequestJob::BytesReadCompleted() { | 517 int BlobURLRequestJob::BytesReadCompleted() { |
472 int bytes_read = read_buf_->BytesConsumed(); | 518 int bytes_read = read_buf_->BytesConsumed(); |
473 read_buf_ = NULL; | 519 read_buf_ = NULL; |
474 return bytes_read; | 520 return bytes_read; |
475 } | 521 } |
476 | 522 |
477 int BlobURLRequestJob::ComputeBytesToRead() const { | 523 int BlobURLRequestJob::ComputeBytesToRead() const { |
478 int64 current_item_length = item_length_list_[current_item_index_]; | 524 int64 current_item_length = item_length_list_[current_item_index_]; |
479 | 525 |
480 int64 item_remaining = current_item_length - current_item_offset_; | 526 int64 item_remaining = current_item_length - current_item_offset_; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
641 | 687 |
642 default: | 688 default: |
643 break; | 689 break; |
644 } | 690 } |
645 | 691 |
646 NOTREACHED(); | 692 NOTREACHED(); |
647 return false; | 693 return false; |
648 } | 694 } |
649 | 695 |
650 } // namespace storage | 696 } // namespace storage |
OLD | NEW |