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"
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"
17 UploadDiskCacheEntryElementReader::UploadDiskCacheEntryElementReader(
18 disk_cache::Entry
* disk_cache_entry
,
19 int disk_cache_stream_index
,
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
),
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 {
42 int UploadDiskCacheEntryElementReader::Init(
43 const CompletionCallback
& callback
) {
44 weak_factory_
.InvalidateWeakPtrs();
45 current_read_offset_
= range_begin_offset_
;
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 {
61 int UploadDiskCacheEntryElementReader::Read(
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
;
78 current_read_offset_
+= result
;
82 void UploadDiskCacheEntryElementReader::OnReadCompleted(
83 const CompletionCallback
& callback
,
86 current_read_offset_
+= result
;