Chromium Code Reviews| Index: net/base/upload_disk_cache_entry_element_reader.cc |
| diff --git a/net/base/upload_disk_cache_entry_element_reader.cc b/net/base/upload_disk_cache_entry_element_reader.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..48fca6ee8837684a3b8232762f776efd1c410068 |
| --- /dev/null |
| +++ b/net/base/upload_disk_cache_entry_element_reader.cc |
| @@ -0,0 +1,82 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/base/upload_disk_cache_entry_element_reader.h" |
| + |
| +#include "net/base/io_buffer.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/disk_cache/disk_cache.h" |
| + |
| +namespace net { |
| + |
| +UploadDiskCacheEntryElementReader::UploadDiskCacheEntryElementReader( |
| + disk_cache::Entry* disk_cache_entry, |
| + int disk_cache_stream_index, |
| + int range_offset, |
| + int range_length) |
| + : disk_cache_entry_(disk_cache_entry), |
| + disk_cache_stream_index_(disk_cache_stream_index), |
| + range_offset_(range_offset), |
| + range_length_(range_length), |
| + offset_(range_offset_), |
| + weak_factory_(this) { |
| + DCHECK_LE(range_offset_ + range_length, |
| + disk_cache_entry_->GetDataSize(disk_cache_stream_index_)); |
| +} |
| + |
| +UploadDiskCacheEntryElementReader::~UploadDiskCacheEntryElementReader() { |
| +} |
| + |
| +const UploadDiskCacheEntryElementReader* |
| +UploadDiskCacheEntryElementReader::AsDiskCacheEntryReader() const { |
| + return this; |
| +} |
| + |
| +int UploadDiskCacheEntryElementReader::Init( |
| + const CompletionCallback& callback) { |
| + offset_ = range_offset_; |
| + return OK; |
| +} |
| + |
| +uint64_t UploadDiskCacheEntryElementReader::GetContentLength() const { |
| + return range_length_; |
| +} |
| + |
| +uint64_t UploadDiskCacheEntryElementReader::BytesRemaining() const { |
| + 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.
|
| +} |
| + |
| +bool UploadDiskCacheEntryElementReader::IsInMemory() const { |
| + return false; |
| +} |
| + |
| +int UploadDiskCacheEntryElementReader::Read( |
| + IOBuffer* buf, |
| + int buf_length, |
| + const CompletionCallback& callback) { |
| + const int bytes_to_read = |
| + std::min(buf_length, static_cast<int>(BytesRemaining())); |
| + |
| + CompletionCallback new_callback = |
| + base::Bind(&UploadDiskCacheEntryElementReader::OnReadCompleted, |
| + weak_factory_.GetWeakPtr(), callback); |
| + |
| + 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
|
| + disk_cache_stream_index_, offset_, buf, bytes_to_read, new_callback); |
| + if (result == ERR_IO_PENDING) |
| + return ERR_IO_PENDING; |
| + if (result > 0) |
| + offset_ += result; |
| + return result; |
| +} |
| + |
| +void UploadDiskCacheEntryElementReader::OnReadCompleted( |
| + const CompletionCallback& callback, |
| + int result) { |
| + if (result > 0) |
| + offset_ += result; |
| + callback.Run(result); |
| +} |
| + |
| +} // namespace net |