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 <algorithm> | |
8 | |
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" | |
14 | |
15 namespace net { | |
16 | |
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_)); | |
32 } | |
33 | |
34 UploadDiskCacheEntryElementReader::~UploadDiskCacheEntryElementReader() { | |
35 } | |
36 | |
37 const UploadDiskCacheEntryElementReader* | |
38 UploadDiskCacheEntryElementReader::AsDiskCacheEntryReaderForTests() const { | |
39 return this; | |
40 } | |
41 | |
42 int UploadDiskCacheEntryElementReader::Init( | |
43 const CompletionCallback& callback) { | |
44 weak_factory_.InvalidateWeakPtrs(); | |
45 current_read_offset_ = range_begin_offset_; | |
46 return OK; | |
47 } | |
48 | |
49 uint64_t UploadDiskCacheEntryElementReader::GetContentLength() const { | |
50 return range_end_offset_ - range_begin_offset_; | |
51 } | |
52 | |
53 uint64_t UploadDiskCacheEntryElementReader::BytesRemaining() const { | |
54 return range_end_offset_ - current_read_offset_; | |
55 } | |
56 | |
57 bool UploadDiskCacheEntryElementReader::IsInMemory() const { | |
58 return false; | |
59 } | |
60 | |
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())); | |
67 | |
68 CompletionCallback new_callback = | |
69 base::Bind(&UploadDiskCacheEntryElementReader::OnReadCompleted, | |
70 weak_factory_.GetWeakPtr(), callback); | |
71 | |
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; | |
80 } | |
81 | |
82 void UploadDiskCacheEntryElementReader::OnReadCompleted( | |
83 const CompletionCallback& callback, | |
84 int result) { | |
85 if (result > 0) | |
86 current_read_offset_ += result; | |
87 callback.Run(result); | |
88 } | |
89 | |
90 } // namespace net | |
OLD | NEW |