James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "net/base/upload_file_element_reader.h" |
| 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/files/file_util.h" |
| 9 | #include "base/location.h" |
James Robinson | 7f48021 | 2014-10-31 10:28:08 -0700 | [diff] [blame] | 10 | #include "base/profiler/scoped_tracker.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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" |
| 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 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; |
| 23 | |
| 24 | } // namespace |
| 25 | |
| 26 | UploadFileElementReader::UploadFileElementReader( |
| 27 | base::TaskRunner* task_runner, |
| 28 | const base::FilePath& path, |
| 29 | uint64 range_offset, |
| 30 | uint64 range_length, |
| 31 | const base::Time& expected_modification_time) |
| 32 | : task_runner_(task_runner), |
| 33 | path_(path), |
| 34 | range_offset_(range_offset), |
| 35 | range_length_(range_length), |
| 36 | expected_modification_time_(expected_modification_time), |
| 37 | content_length_(0), |
| 38 | bytes_remaining_(0), |
| 39 | weak_ptr_factory_(this) { |
| 40 | DCHECK(task_runner_.get()); |
| 41 | } |
| 42 | |
| 43 | UploadFileElementReader::~UploadFileElementReader() { |
| 44 | } |
| 45 | |
| 46 | const UploadFileElementReader* UploadFileElementReader::AsFileReader() const { |
| 47 | return this; |
| 48 | } |
| 49 | |
| 50 | int UploadFileElementReader::Init(const CompletionCallback& callback) { |
| 51 | DCHECK(!callback.is_null()); |
| 52 | Reset(); |
| 53 | |
| 54 | file_stream_.reset(new FileStream(task_runner_.get())); |
| 55 | int result = file_stream_->Open( |
| 56 | path_, |
| 57 | base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 58 | base::File::FLAG_ASYNC, |
| 59 | base::Bind(&UploadFileElementReader::OnOpenCompleted, |
| 60 | weak_ptr_factory_.GetWeakPtr(), |
| 61 | callback)); |
| 62 | DCHECK_GT(0, result); |
| 63 | return result; |
| 64 | } |
| 65 | |
| 66 | uint64 UploadFileElementReader::GetContentLength() const { |
| 67 | if (overriding_content_length) |
| 68 | return overriding_content_length; |
| 69 | return content_length_; |
| 70 | } |
| 71 | |
| 72 | uint64 UploadFileElementReader::BytesRemaining() const { |
| 73 | return bytes_remaining_; |
| 74 | } |
| 75 | |
| 76 | int UploadFileElementReader::Read(IOBuffer* buf, |
| 77 | int buf_length, |
| 78 | const CompletionCallback& callback) { |
| 79 | DCHECK(!callback.is_null()); |
| 80 | |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 81 | int num_bytes_to_read = static_cast<int>( |
| 82 | std::min(BytesRemaining(), static_cast<uint64>(buf_length))); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 83 | if (num_bytes_to_read == 0) |
| 84 | return 0; |
| 85 | |
| 86 | int result = file_stream_->Read( |
| 87 | buf, num_bytes_to_read, |
| 88 | base::Bind(base::IgnoreResult(&UploadFileElementReader::OnReadCompleted), |
| 89 | weak_ptr_factory_.GetWeakPtr(), |
| 90 | callback)); |
| 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; |
| 95 | } |
| 96 | |
| 97 | void UploadFileElementReader::Reset() { |
| 98 | weak_ptr_factory_.InvalidateWeakPtrs(); |
| 99 | bytes_remaining_ = 0; |
| 100 | content_length_ = 0; |
| 101 | file_stream_.reset(); |
| 102 | } |
| 103 | |
| 104 | void UploadFileElementReader::OnOpenCompleted( |
| 105 | const CompletionCallback& callback, |
| 106 | int result) { |
James Robinson | 7f48021 | 2014-10-31 10:28:08 -0700 | [diff] [blame] | 107 | // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. |
| 108 | tracked_objects::ScopedTracker tracking_profile( |
James Robinson | 80d418c | 2014-10-16 16:00:02 -0700 | [diff] [blame] | 109 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 110 | "423948 UploadFileElementReader::OnOpenCompleted")); |
| 111 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 112 | DCHECK(!callback.is_null()); |
| 113 | |
| 114 | if (result < 0) { |
| 115 | DLOG(WARNING) << "Failed to open \"" << path_.value() |
| 116 | << "\" for reading: " << result; |
| 117 | callback.Run(result); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | if (range_offset_) { |
| 122 | int result = file_stream_->Seek( |
| 123 | base::File::FROM_BEGIN, range_offset_, |
| 124 | base::Bind(&UploadFileElementReader::OnSeekCompleted, |
| 125 | weak_ptr_factory_.GetWeakPtr(), |
| 126 | callback)); |
| 127 | DCHECK_GT(0, result); |
| 128 | if (result != ERR_IO_PENDING) |
| 129 | callback.Run(result); |
| 130 | } else { |
| 131 | OnSeekCompleted(callback, OK); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void UploadFileElementReader::OnSeekCompleted( |
| 136 | const CompletionCallback& callback, |
| 137 | int64 result) { |
| 138 | DCHECK(!callback.is_null()); |
| 139 | |
| 140 | if (result < 0) { |
| 141 | DLOG(WARNING) << "Failed to seek \"" << path_.value() |
| 142 | << "\" to offset: " << range_offset_ << " (" << result << ")"; |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 143 | callback.Run(static_cast<int>(result)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
| 147 | base::File::Info* file_info = new base::File::Info; |
| 148 | bool posted = base::PostTaskAndReplyWithResult( |
| 149 | task_runner_.get(), |
| 150 | FROM_HERE, |
| 151 | base::Bind(&base::GetFileInfo, path_, file_info), |
| 152 | base::Bind(&UploadFileElementReader::OnGetFileInfoCompleted, |
| 153 | weak_ptr_factory_.GetWeakPtr(), |
| 154 | callback, |
| 155 | base::Owned(file_info))); |
| 156 | DCHECK(posted); |
| 157 | } |
| 158 | |
| 159 | void UploadFileElementReader::OnGetFileInfoCompleted( |
| 160 | const CompletionCallback& callback, |
| 161 | base::File::Info* file_info, |
| 162 | bool result) { |
| 163 | DCHECK(!callback.is_null()); |
| 164 | if (!result) { |
| 165 | DLOG(WARNING) << "Failed to get file info of \"" << path_.value() << "\""; |
| 166 | callback.Run(ERR_FILE_NOT_FOUND); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | int64 length = file_info->size; |
| 171 | if (range_offset_ < static_cast<uint64>(length)) { |
| 172 | // Compensate for the offset. |
| 173 | length = std::min(length - range_offset_, range_length_); |
| 174 | } |
| 175 | |
| 176 | // If the underlying file has been changed and the expected file modification |
| 177 | // time is set, treat it as error. Note that the expected modification time |
| 178 | // from WebKit is based on time_t precision. So we have to convert both to |
| 179 | // time_t to compare. This check is used for sliced files. |
| 180 | if (!expected_modification_time_.is_null() && |
| 181 | expected_modification_time_.ToTimeT() != |
| 182 | file_info->last_modified.ToTimeT()) { |
| 183 | callback.Run(ERR_UPLOAD_FILE_CHANGED); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | content_length_ = length; |
| 188 | bytes_remaining_ = GetContentLength(); |
| 189 | callback.Run(OK); |
| 190 | } |
| 191 | |
| 192 | int UploadFileElementReader::OnReadCompleted( |
| 193 | const CompletionCallback& callback, |
| 194 | int result) { |
| 195 | if (result == 0) // Reached end-of-file earlier than expected. |
| 196 | result = ERR_UPLOAD_FILE_CHANGED; |
| 197 | |
| 198 | if (result > 0) { |
| 199 | DCHECK_GE(bytes_remaining_, static_cast<uint64>(result)); |
| 200 | bytes_remaining_ -= result; |
| 201 | } |
| 202 | |
| 203 | if (!callback.is_null()) |
| 204 | callback.Run(result); |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| 209 | ScopedOverridingContentLengthForTests(uint64 value) { |
| 210 | overriding_content_length = value; |
| 211 | } |
| 212 | |
| 213 | UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| 214 | ~ScopedOverridingContentLengthForTests() { |
| 215 | overriding_content_length = 0; |
| 216 | } |
| 217 | |
| 218 | } // namespace net |