[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / base / upload_file_element_reader.cc
blob3c8aef9265432de235c2388c285713aaceae39ae
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"
7 #include "base/bind.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"
15 namespace net {
17 namespace {
19 // In tests, this value is used to override the return value of
20 // UploadFileElementReader::GetContentLength() when set to non-zero.
21 uint64_t overriding_content_length = 0;
23 } // namespace
25 UploadFileElementReader::UploadFileElementReader(
26 base::TaskRunner* task_runner,
27 const base::FilePath& path,
28 uint64_t range_offset,
29 uint64_t range_length,
30 const base::Time& expected_modification_time)
31 : task_runner_(task_runner),
32 path_(path),
33 range_offset_(range_offset),
34 range_length_(range_length),
35 expected_modification_time_(expected_modification_time),
36 content_length_(0),
37 bytes_remaining_(0),
38 weak_ptr_factory_(this) {
39 DCHECK(task_runner_.get());
42 UploadFileElementReader::~UploadFileElementReader() {
45 const UploadFileElementReader* UploadFileElementReader::AsFileReader() const {
46 return this;
49 int UploadFileElementReader::Init(const CompletionCallback& callback) {
50 DCHECK(!callback.is_null());
51 Reset();
53 file_stream_.reset(new FileStream(task_runner_.get()));
54 int result = file_stream_->Open(
55 path_,
56 base::File::FLAG_OPEN | base::File::FLAG_READ |
57 base::File::FLAG_ASYNC,
58 base::Bind(&UploadFileElementReader::OnOpenCompleted,
59 weak_ptr_factory_.GetWeakPtr(),
60 callback));
61 DCHECK_GT(0, result);
62 return result;
65 uint64_t UploadFileElementReader::GetContentLength() const {
66 if (overriding_content_length)
67 return overriding_content_length;
68 return content_length_;
71 uint64_t UploadFileElementReader::BytesRemaining() const {
72 return bytes_remaining_;
75 int UploadFileElementReader::Read(IOBuffer* buf,
76 int buf_length,
77 const CompletionCallback& callback) {
78 DCHECK(!callback.is_null());
80 int num_bytes_to_read = static_cast<int>(
81 std::min(BytesRemaining(), static_cast<uint64_t>(buf_length)));
82 if (num_bytes_to_read == 0)
83 return 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(),
89 callback));
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();
98 bytes_remaining_ = 0;
99 content_length_ = 0;
100 file_stream_.reset();
103 void UploadFileElementReader::OnOpenCompleted(
104 const CompletionCallback& callback,
105 int result) {
106 DCHECK(!callback.is_null());
108 if (result < 0) {
109 DLOG(WARNING) << "Failed to open \"" << path_.value()
110 << "\" for reading: " << result;
111 callback.Run(result);
112 return;
115 if (range_offset_) {
116 int seek_result = file_stream_->Seek(
117 range_offset_, base::Bind(&UploadFileElementReader::OnSeekCompleted,
118 weak_ptr_factory_.GetWeakPtr(), callback));
119 DCHECK_GT(0, seek_result);
120 if (seek_result != ERR_IO_PENDING)
121 callback.Run(seek_result);
122 } else {
123 OnSeekCompleted(callback, OK);
127 void UploadFileElementReader::OnSeekCompleted(
128 const CompletionCallback& callback,
129 int64_t result) {
130 DCHECK(!callback.is_null());
132 if (result < 0) {
133 DLOG(WARNING) << "Failed to seek \"" << path_.value()
134 << "\" to offset: " << range_offset_ << " (" << result << ")";
135 callback.Run(static_cast<int>(result));
136 return;
139 base::File::Info* file_info = new base::File::Info;
140 bool posted = base::PostTaskAndReplyWithResult(
141 task_runner_.get(),
142 FROM_HERE,
143 base::Bind(&base::GetFileInfo, path_, file_info),
144 base::Bind(&UploadFileElementReader::OnGetFileInfoCompleted,
145 weak_ptr_factory_.GetWeakPtr(),
146 callback,
147 base::Owned(file_info)));
148 DCHECK(posted);
151 void UploadFileElementReader::OnGetFileInfoCompleted(
152 const CompletionCallback& callback,
153 base::File::Info* file_info,
154 bool result) {
155 DCHECK(!callback.is_null());
156 if (!result) {
157 DLOG(WARNING) << "Failed to get file info of \"" << path_.value() << "\"";
158 callback.Run(ERR_FILE_NOT_FOUND);
159 return;
162 int64_t length = file_info->size;
163 if (range_offset_ < static_cast<uint64_t>(length)) {
164 // Compensate for the offset.
165 length = std::min(length - range_offset_, range_length_);
168 // If the underlying file has been changed and the expected file modification
169 // time is set, treat it as error. Note that |expected_modification_time_| may
170 // have gone through multiple conversion steps involving loss of precision
171 // (including conversion to time_t). Therefore the check below only verifies
172 // that the timestamps are within one second of each other. This check is used
173 // for sliced files.
174 if (!expected_modification_time_.is_null() &&
175 (expected_modification_time_ - file_info->last_modified)
176 .magnitude()
177 .InSeconds() != 0) {
178 callback.Run(ERR_UPLOAD_FILE_CHANGED);
179 return;
182 content_length_ = length;
183 bytes_remaining_ = GetContentLength();
184 callback.Run(OK);
187 int UploadFileElementReader::OnReadCompleted(
188 const CompletionCallback& callback,
189 int result) {
190 if (result == 0) // Reached end-of-file earlier than expected.
191 result = ERR_UPLOAD_FILE_CHANGED;
193 if (result > 0) {
194 DCHECK_GE(bytes_remaining_, static_cast<uint64_t>(result));
195 bytes_remaining_ -= result;
198 if (!callback.is_null())
199 callback.Run(result);
200 return result;
203 UploadFileElementReader::ScopedOverridingContentLengthForTests::
204 ScopedOverridingContentLengthForTests(uint64_t value) {
205 overriding_content_length = value;
208 UploadFileElementReader::ScopedOverridingContentLengthForTests::
209 ~ScopedOverridingContentLengthForTests() {
210 overriding_content_length = 0;
213 } // namespace net