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/task_runner_util.h"
11 #include "net/base/file_stream.h"
12 #include "net/base/io_buffer.h"
13 #include "net/base/net_errors.h"
19 // In tests, this value is used to override the return value of
20 // UploadFileElementReader::GetContentLength() when set to non-zero.
21 uint64 overriding_content_length
= 0;
25 UploadFileElementReader::UploadFileElementReader(
26 base::TaskRunner
* task_runner
,
27 const base::FilePath
& path
,
30 const base::Time
& expected_modification_time
)
31 : task_runner_(task_runner
),
33 range_offset_(range_offset
),
34 range_length_(range_length
),
35 expected_modification_time_(expected_modification_time
),
38 weak_ptr_factory_(this) {
39 DCHECK(task_runner_
.get());
42 UploadFileElementReader::~UploadFileElementReader() {
45 const UploadFileElementReader
* UploadFileElementReader::AsFileReader() const {
49 int UploadFileElementReader::Init(const CompletionCallback
& callback
) {
50 DCHECK(!callback
.is_null());
53 file_stream_
.reset(new FileStream(task_runner_
.get()));
54 int result
= file_stream_
->Open(
56 base::File::FLAG_OPEN
| base::File::FLAG_READ
|
57 base::File::FLAG_ASYNC
,
58 base::Bind(&UploadFileElementReader::OnOpenCompleted
,
59 weak_ptr_factory_
.GetWeakPtr(),
65 uint64
UploadFileElementReader::GetContentLength() const {
66 if (overriding_content_length
)
67 return overriding_content_length
;
68 return content_length_
;
71 uint64
UploadFileElementReader::BytesRemaining() const {
72 return bytes_remaining_
;
75 int UploadFileElementReader::Read(IOBuffer
* buf
,
77 const CompletionCallback
& callback
) {
78 DCHECK(!callback
.is_null());
80 uint64 num_bytes_to_read
=
81 std::min(BytesRemaining(), static_cast<uint64
>(buf_length
));
82 if (num_bytes_to_read
== 0)
85 int result
= file_stream_
->Read(
86 buf
, num_bytes_to_read
,
87 base::Bind(base::IgnoreResult(&UploadFileElementReader::OnReadCompleted
),
88 weak_ptr_factory_
.GetWeakPtr(),
90 // Even in async mode, FileStream::Read() may return the result synchronously.
91 if (result
!= ERR_IO_PENDING
)
92 return OnReadCompleted(CompletionCallback(), result
);
93 return ERR_IO_PENDING
;
96 void UploadFileElementReader::Reset() {
97 weak_ptr_factory_
.InvalidateWeakPtrs();
100 file_stream_
.reset();
103 void UploadFileElementReader::OnOpenCompleted(
104 const CompletionCallback
& callback
,
106 DCHECK(!callback
.is_null());
109 DLOG(WARNING
) << "Failed to open \"" << path_
.value()
110 << "\" for reading: " << result
;
111 callback
.Run(result
);
116 int result
= file_stream_
->Seek(
117 base::File::FROM_BEGIN
, range_offset_
,
118 base::Bind(&UploadFileElementReader::OnSeekCompleted
,
119 weak_ptr_factory_
.GetWeakPtr(),
121 DCHECK_GT(0, result
);
122 if (result
!= ERR_IO_PENDING
)
123 callback
.Run(result
);
125 OnSeekCompleted(callback
, OK
);
129 void UploadFileElementReader::OnSeekCompleted(
130 const CompletionCallback
& callback
,
132 DCHECK(!callback
.is_null());
135 DLOG(WARNING
) << "Failed to seek \"" << path_
.value()
136 << "\" to offset: " << range_offset_
<< " (" << result
<< ")";
137 callback
.Run(result
);
141 base::File::Info
* file_info
= new base::File::Info
;
142 bool posted
= base::PostTaskAndReplyWithResult(
145 base::Bind(&base::GetFileInfo
, path_
, file_info
),
146 base::Bind(&UploadFileElementReader::OnGetFileInfoCompleted
,
147 weak_ptr_factory_
.GetWeakPtr(),
149 base::Owned(file_info
)));
153 void UploadFileElementReader::OnGetFileInfoCompleted(
154 const CompletionCallback
& callback
,
155 base::File::Info
* file_info
,
157 DCHECK(!callback
.is_null());
159 DLOG(WARNING
) << "Failed to get file info of \"" << path_
.value() << "\"";
160 callback
.Run(ERR_FILE_NOT_FOUND
);
164 int64 length
= file_info
->size
;
165 if (range_offset_
< static_cast<uint64
>(length
)) {
166 // Compensate for the offset.
167 length
= std::min(length
- range_offset_
, range_length_
);
170 // If the underlying file has been changed and the expected file modification
171 // time is set, treat it as error. Note that the expected modification time
172 // from WebKit is based on time_t precision. So we have to convert both to
173 // time_t to compare. This check is used for sliced files.
174 if (!expected_modification_time_
.is_null() &&
175 expected_modification_time_
.ToTimeT() !=
176 file_info
->last_modified
.ToTimeT()) {
177 callback
.Run(ERR_UPLOAD_FILE_CHANGED
);
181 content_length_
= length
;
182 bytes_remaining_
= GetContentLength();
186 int UploadFileElementReader::OnReadCompleted(
187 const CompletionCallback
& callback
,
189 if (result
== 0) // Reached end-of-file earlier than expected.
190 result
= ERR_UPLOAD_FILE_CHANGED
;
193 DCHECK_GE(bytes_remaining_
, static_cast<uint64
>(result
));
194 bytes_remaining_
-= result
;
197 if (!callback
.is_null())
198 callback
.Run(result
);
202 UploadFileElementReader::ScopedOverridingContentLengthForTests::
203 ScopedOverridingContentLengthForTests(uint64 value
) {
204 overriding_content_length
= value
;
207 UploadFileElementReader::ScopedOverridingContentLengthForTests::
208 ~ScopedOverridingContentLengthForTests() {
209 overriding_content_length
= 0;