Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/base/upload_disk_cache_entry_element_reader.h" | |
| 6 | |
| 7 #include "net/base/io_buffer.h" | |
| 8 #include "net/base/net_errors.h" | |
| 9 #include "net/disk_cache/disk_cache.h" | |
| 10 | |
| 11 namespace net { | |
| 12 | |
| 13 UploadDiskCacheEntryElementReader::UploadDiskCacheEntryElementReader( | |
| 14 disk_cache::Entry* disk_cache_entry, | |
| 15 int disk_cache_stream_index, | |
| 16 int range_offset, | |
| 17 int range_length) | |
| 18 : disk_cache_entry_(disk_cache_entry), | |
| 19 disk_cache_stream_index_(disk_cache_stream_index), | |
| 20 range_offset_(range_offset), | |
| 21 range_length_(range_length), | |
| 22 offset_(range_offset_), | |
| 23 weak_factory_(this) { | |
| 24 DCHECK_LE(range_offset_ + range_length, | |
| 25 disk_cache_entry_->GetDataSize(disk_cache_stream_index_)); | |
| 26 } | |
| 27 | |
| 28 UploadDiskCacheEntryElementReader::~UploadDiskCacheEntryElementReader() { | |
| 29 } | |
| 30 | |
| 31 const UploadDiskCacheEntryElementReader* | |
| 32 UploadDiskCacheEntryElementReader::AsDiskCacheEntryReader() const { | |
| 33 return this; | |
| 34 } | |
| 35 | |
| 36 int UploadDiskCacheEntryElementReader::Init( | |
| 37 const CompletionCallback& callback) { | |
| 38 offset_ = range_offset_; | |
| 39 return OK; | |
| 40 } | |
| 41 | |
| 42 uint64_t UploadDiskCacheEntryElementReader::GetContentLength() const { | |
| 43 return range_length_; | |
| 44 } | |
| 45 | |
| 46 uint64_t UploadDiskCacheEntryElementReader::BytesRemaining() const { | |
| 47 return range_length_ + range_offset_ - offset_; | |
|
michaeln
2015/06/15 22:17:24
nit: the math in here might be more readable in te
gavinp
2015/06/16 16:07:48
Done.
| |
| 48 } | |
| 49 | |
| 50 bool UploadDiskCacheEntryElementReader::IsInMemory() const { | |
| 51 return false; | |
| 52 } | |
| 53 | |
| 54 int UploadDiskCacheEntryElementReader::Read( | |
| 55 IOBuffer* buf, | |
| 56 int buf_length, | |
| 57 const CompletionCallback& callback) { | |
| 58 const int bytes_to_read = | |
| 59 std::min(buf_length, static_cast<int>(BytesRemaining())); | |
| 60 | |
| 61 CompletionCallback new_callback = | |
| 62 base::Bind(&UploadDiskCacheEntryElementReader::OnReadCompleted, | |
| 63 weak_factory_.GetWeakPtr(), callback); | |
| 64 | |
| 65 const int result = disk_cache_entry_->ReadData( | |
|
michaeln
2015/06/15 22:17:24
Calling ReadData() with zero bytes_to_read is ok,
gavinp
2015/06/16 16:07:48
I thought so, and I just double checked all three
| |
| 66 disk_cache_stream_index_, offset_, buf, bytes_to_read, new_callback); | |
| 67 if (result == ERR_IO_PENDING) | |
| 68 return ERR_IO_PENDING; | |
| 69 if (result > 0) | |
| 70 offset_ += result; | |
| 71 return result; | |
| 72 } | |
| 73 | |
| 74 void UploadDiskCacheEntryElementReader::OnReadCompleted( | |
| 75 const CompletionCallback& callback, | |
| 76 int result) { | |
| 77 if (result > 0) | |
| 78 offset_ += result; | |
| 79 callback.Run(result); | |
| 80 } | |
| 81 | |
| 82 } // namespace net | |
| OLD | NEW |