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..0141b177870c52ed8c10ee8f98ae25c46a4b2325 |
| --- /dev/null |
| +++ b/net/base/upload_disk_cache_entry_element_reader.cc |
| @@ -0,0 +1,84 @@ |
| +// 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, |
|
mmenke
2015/06/16 18:51:18
Should we also DCHECK both of these are >= 0?
gavinp
2015/06/16 22:28:01
Done.
|
| + disk_cache_entry_->GetDataSize(disk_cache_stream_index_)); |
|
mmenke
2015/06/16 18:51:18
nit: include base/logging.h
gavinp
2015/06/16 22:28:01
Done.
|
| +} |
| + |
| +UploadDiskCacheEntryElementReader::~UploadDiskCacheEntryElementReader() { |
| +} |
| + |
| +const UploadDiskCacheEntryElementReader* |
| +UploadDiskCacheEntryElementReader::AsDiskCacheEntryReader() const { |
| + return this; |
| +} |
| + |
| +int UploadDiskCacheEntryElementReader::Init( |
| + const CompletionCallback& callback) { |
|
mmenke
2015/06/16 18:51:18
If there's a pending read from entry, it's fine if
gavinp
2015/06/16 22:28:01
Yes.
The behaviour that we will not call the call
|
| + weak_factory_.InvalidateWeakPtrs(); |
| + offset_ = range_offset_; |
| + return OK; |
| +} |
| + |
| +uint64_t UploadDiskCacheEntryElementReader::GetContentLength() const { |
| + return range_length_; |
| +} |
| + |
| +uint64_t UploadDiskCacheEntryElementReader::BytesRemaining() const { |
| + const int amount_read = offset_ - range_offset_; |
|
mmenke
2015/06/16 18:51:18
nit: Basically no code in chrome or net consts st
michaeln
2015/06/16 21:52:54
i don't think that's true and i think its helpful
mmenke
2015/06/16 22:22:47
Per style guide, kSomething is only for compile-ti
gavinp
2015/06/16 22:28:01
In this case, it's moot, because this line doesn't
gavinp
2015/06/16 22:28:01
Done.
gavinp
2015/06/16 23:08:04
I have gone ahead and removed most const from auto
michaeln
2015/06/17 00:57:36
ok, if you say so :)
mmenke
2015/06/17 01:24:35
Note that if net/ globally follows a convention, t
|
| + return range_length_ - amount_read; |
| +} |
| + |
| +bool UploadDiskCacheEntryElementReader::IsInMemory() const { |
| + return false; |
| +} |
| + |
| +int UploadDiskCacheEntryElementReader::Read( |
| + IOBuffer* buf, |
| + int buf_length, |
| + const CompletionCallback& callback) { |
|
mmenke
2015/06/16 18:51:18
DCHECK(!callback.is_null())?
gavinp
2015/06/16 22:28:01
Done.
|
| + const int bytes_to_read = |
| + std::min(buf_length, static_cast<int>(BytesRemaining())); |
|
mmenke
2015/06/16 18:51:18
need to include the header for min (algorithm?)
gavinp
2015/06/16 22:28:01
Done. It's algorithm.
|
| + |
| + CompletionCallback new_callback = |
| + base::Bind(&UploadDiskCacheEntryElementReader::OnReadCompleted, |
| + weak_factory_.GetWeakPtr(), callback); |
|
mmenke
2015/06/16 18:51:18
include base/bind.h
mmenke
2015/06/16 18:51:18
Suggest making this callback a class member variab
gavinp
2015/06/16 22:28:01
I'm leaving this as is, absent further reviewer in
gavinp
2015/06/16 22:28:01
Done.
|
| + |
| + const int result = disk_cache_entry_->ReadData( |
| + 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 |