1 // Copyright (c) 2012 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_file_element_reader.h"
8 #include "base/files/file_util.h"
9 #include "base/location.h"
10 #include "base/profiler/scoped_tracker.h"
11 #include "base/task_runner_util.h"
12 #include "net/base/file_stream.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
20 // In tests, this value is used to override the return value of
21 // UploadFileElementReader::GetContentLength() when set to non-zero.
22 uint64 overriding_content_length
= 0;
26 UploadFileElementReader::UploadFileElementReader(
27 base::TaskRunner
* task_runner
,
28 const base::FilePath
& path
,
31 const base::Time
& expected_modification_time
)
32 : task_runner_(task_runner
),
34 range_offset_(range_offset
),
35 range_length_(range_length
),
36 expected_modification_time_(expected_modification_time
),
39 weak_ptr_factory_(this) {
40 DCHECK(task_runner_
.get());
43 UploadFileElementReader::~UploadFileElementReader() {
46 const UploadFileElementReader
* UploadFileElementReader::AsFileReader() const {
50 int UploadFileElementReader::Init(const CompletionCallback
& callback
) {
51 DCHECK(!callback
.is_null());
54 file_stream_
.reset(new FileStream(task_runner_
.get()));
55 int result
= file_stream_
->Open(
57 base::File::FLAG_OPEN
| base::File::FLAG_READ
|
58 base::File::FLAG_ASYNC
,
59 base::Bind(&UploadFileElementReader::OnOpenCompleted
,
60 weak_ptr_factory_
.GetWeakPtr(),
66 uint64
UploadFileElementReader::GetContentLength() const {
67 if (overriding_content_length
)
68 return overriding_content_length
;
69 return content_length_
;
72 uint64
UploadFileElementReader::BytesRemaining() const {
73 return bytes_remaining_
;
76 int UploadFileElementReader::Read(IOBuffer
* buf
,
78 const CompletionCallback
& callback
) {
79 DCHECK(!callback
.is_null());
81 int num_bytes_to_read
= static_cast<int>(
82 std::min(BytesRemaining(), static_cast<uint64
>(buf_length
)));
83 if (num_bytes_to_read
== 0)
86 int result
= file_stream_
->Read(
87 buf
, num_bytes_to_read
,
88 base::Bind(base::IgnoreResult(&UploadFileElementReader::OnReadCompleted
),
89 weak_ptr_factory_
.GetWeakPtr(),
91 // Even in async mode, FileStream::Read() may return the result synchronously.
92 if (result
!= ERR_IO_PENDING
)
93 return OnReadCompleted(CompletionCallback(), result
);
94 return ERR_IO_PENDING
;
97 void UploadFileElementReader::Reset() {
98 weak_ptr_factory_
.InvalidateWeakPtrs();
101 file_stream_
.reset();
104 void UploadFileElementReader::OnOpenCompleted(
105 const CompletionCallback
& callback
,
107 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed.
108 tracked_objects::ScopedTracker
tracking_profile(
109 FROM_HERE_WITH_EXPLICIT_FUNCTION(
110 "423948 UploadFileElementReader::OnOpenCompleted"));
112 DCHECK(!callback
.is_null());
115 DLOG(WARNING
) << "Failed to open \"" << path_
.value()
116 << "\" for reading: " << result
;
117 callback
.Run(result
);
122 int seek_result
= file_stream_
->Seek(
123 base::File::FROM_BEGIN
, range_offset_
,
124 base::Bind(&UploadFileElementReader::OnSeekCompleted
,
125 weak_ptr_factory_
.GetWeakPtr(), callback
));
126 DCHECK_GT(0, seek_result
);
127 if (seek_result
!= ERR_IO_PENDING
)
128 callback
.Run(seek_result
);
130 OnSeekCompleted(callback
, OK
);
134 void UploadFileElementReader::OnSeekCompleted(
135 const CompletionCallback
& callback
,
137 DCHECK(!callback
.is_null());
140 DLOG(WARNING
) << "Failed to seek \"" << path_
.value()
141 << "\" to offset: " << range_offset_
<< " (" << result
<< ")";
142 callback
.Run(static_cast<int>(result
));
146 base::File::Info
* file_info
= new base::File::Info
;
147 bool posted
= base::PostTaskAndReplyWithResult(
150 base::Bind(&base::GetFileInfo
, path_
, file_info
),
151 base::Bind(&UploadFileElementReader::OnGetFileInfoCompleted
,
152 weak_ptr_factory_
.GetWeakPtr(),
154 base::Owned(file_info
)));
158 void UploadFileElementReader::OnGetFileInfoCompleted(
159 const CompletionCallback
& callback
,
160 base::File::Info
* file_info
,
162 DCHECK(!callback
.is_null());
164 DLOG(WARNING
) << "Failed to get file info of \"" << path_
.value() << "\"";
165 callback
.Run(ERR_FILE_NOT_FOUND
);
169 int64 length
= file_info
->size
;
170 if (range_offset_
< static_cast<uint64
>(length
)) {
171 // Compensate for the offset.
172 length
= std::min(length
- range_offset_
, range_length_
);
175 // If the underlying file has been changed and the expected file modification
176 // time is set, treat it as error. Note that |expected_modification_time_| may
177 // have gone through multiple conversion steps involving loss of precision
178 // (including conversion to time_t). Therefore the check below only verifies
179 // that the timestamps are within one second of each other. This check is used
181 if (!expected_modification_time_
.is_null() &&
182 (expected_modification_time_
- file_info
->last_modified
)
185 callback
.Run(ERR_UPLOAD_FILE_CHANGED
);
189 content_length_
= length
;
190 bytes_remaining_
= GetContentLength();
194 int UploadFileElementReader::OnReadCompleted(
195 const CompletionCallback
& callback
,
197 if (result
== 0) // Reached end-of-file earlier than expected.
198 result
= ERR_UPLOAD_FILE_CHANGED
;
201 DCHECK_GE(bytes_remaining_
, static_cast<uint64
>(result
));
202 bytes_remaining_
-= result
;
205 if (!callback
.is_null())
206 callback
.Run(result
);
210 UploadFileElementReader::ScopedOverridingContentLengthForTests::
211 ScopedOverridingContentLengthForTests(uint64 value
) {
212 overriding_content_length
= value
;
215 UploadFileElementReader::ScopedOverridingContentLengthForTests::
216 ~ScopedOverridingContentLengthForTests() {
217 overriding_content_length
= 0;