Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / base / upload_disk_cache_entry_element_reader.cc
blob0635aeb1322b551d1af123f37e017b068a84e2e7
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.
5 #include "net/base/upload_disk_cache_entry_element_reader.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "net/base/io_buffer.h"
12 #include "net/base/net_errors.h"
13 #include "net/disk_cache/disk_cache.h"
15 namespace net {
17 UploadDiskCacheEntryElementReader::UploadDiskCacheEntryElementReader(
18 disk_cache::Entry* disk_cache_entry,
19 int disk_cache_stream_index,
20 int range_offset,
21 int range_length)
22 : disk_cache_entry_(disk_cache_entry),
23 disk_cache_stream_index_(disk_cache_stream_index),
24 range_begin_offset_(range_offset),
25 range_end_offset_(range_offset + range_length),
26 current_read_offset_(range_offset),
27 weak_factory_(this) {
28 DCHECK_LE(0, range_offset);
29 DCHECK_LT(0, range_length);
30 DCHECK_LE(range_offset + range_length,
31 disk_cache_entry_->GetDataSize(disk_cache_stream_index_));
34 UploadDiskCacheEntryElementReader::~UploadDiskCacheEntryElementReader() {
37 const UploadDiskCacheEntryElementReader*
38 UploadDiskCacheEntryElementReader::AsDiskCacheEntryReaderForTests() const {
39 return this;
42 int UploadDiskCacheEntryElementReader::Init(
43 const CompletionCallback& callback) {
44 weak_factory_.InvalidateWeakPtrs();
45 current_read_offset_ = range_begin_offset_;
46 return OK;
49 uint64_t UploadDiskCacheEntryElementReader::GetContentLength() const {
50 return range_end_offset_ - range_begin_offset_;
53 uint64_t UploadDiskCacheEntryElementReader::BytesRemaining() const {
54 return range_end_offset_ - current_read_offset_;
57 bool UploadDiskCacheEntryElementReader::IsInMemory() const {
58 return false;
61 int UploadDiskCacheEntryElementReader::Read(
62 IOBuffer* buf,
63 int buf_length,
64 const CompletionCallback& callback) {
65 DCHECK(!callback.is_null());
66 int bytes_to_read = std::min(buf_length, static_cast<int>(BytesRemaining()));
68 CompletionCallback new_callback =
69 base::Bind(&UploadDiskCacheEntryElementReader::OnReadCompleted,
70 weak_factory_.GetWeakPtr(), callback);
72 int result = disk_cache_entry_->ReadData(disk_cache_stream_index_,
73 current_read_offset_, buf,
74 bytes_to_read, new_callback);
75 if (result == ERR_IO_PENDING)
76 return ERR_IO_PENDING;
77 if (result > 0)
78 current_read_offset_ += result;
79 return result;
82 void UploadDiskCacheEntryElementReader::OnReadCompleted(
83 const CompletionCallback& callback,
84 int result) {
85 if (result > 0)
86 current_read_offset_ += result;
87 callback.Run(result);
90 } // namespace net