| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/upload_file_element_reader.h" | 5 #include "net/base/upload_file_element_reader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
| 11 #include "net/base/file_stream.h" | 11 #include "net/base/file_stream.h" |
| 12 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 13 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 14 | 14 |
| 15 namespace net { | 15 namespace net { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // In tests, this value is used to override the return value of | 19 // In tests, this value is used to override the return value of |
| 20 // UploadFileElementReader::GetContentLength() when set to non-zero. | 20 // UploadFileElementReader::GetContentLength() when set to non-zero. |
| 21 uint64 overriding_content_length = 0; | 21 uint64_t overriding_content_length = 0; |
| 22 | 22 |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 UploadFileElementReader::UploadFileElementReader( | 25 UploadFileElementReader::UploadFileElementReader( |
| 26 base::TaskRunner* task_runner, | 26 base::TaskRunner* task_runner, |
| 27 const base::FilePath& path, | 27 const base::FilePath& path, |
| 28 uint64 range_offset, | 28 uint64_t range_offset, |
| 29 uint64 range_length, | 29 uint64_t range_length, |
| 30 const base::Time& expected_modification_time) | 30 const base::Time& expected_modification_time) |
| 31 : task_runner_(task_runner), | 31 : task_runner_(task_runner), |
| 32 path_(path), | 32 path_(path), |
| 33 range_offset_(range_offset), | 33 range_offset_(range_offset), |
| 34 range_length_(range_length), | 34 range_length_(range_length), |
| 35 expected_modification_time_(expected_modification_time), | 35 expected_modification_time_(expected_modification_time), |
| 36 content_length_(0), | 36 content_length_(0), |
| 37 bytes_remaining_(0), | 37 bytes_remaining_(0), |
| 38 weak_ptr_factory_(this) { | 38 weak_ptr_factory_(this) { |
| 39 DCHECK(task_runner_.get()); | 39 DCHECK(task_runner_.get()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 55 path_, | 55 path_, |
| 56 base::File::FLAG_OPEN | base::File::FLAG_READ | | 56 base::File::FLAG_OPEN | base::File::FLAG_READ | |
| 57 base::File::FLAG_ASYNC, | 57 base::File::FLAG_ASYNC, |
| 58 base::Bind(&UploadFileElementReader::OnOpenCompleted, | 58 base::Bind(&UploadFileElementReader::OnOpenCompleted, |
| 59 weak_ptr_factory_.GetWeakPtr(), | 59 weak_ptr_factory_.GetWeakPtr(), |
| 60 callback)); | 60 callback)); |
| 61 DCHECK_GT(0, result); | 61 DCHECK_GT(0, result); |
| 62 return result; | 62 return result; |
| 63 } | 63 } |
| 64 | 64 |
| 65 uint64 UploadFileElementReader::GetContentLength() const { | 65 uint64_t UploadFileElementReader::GetContentLength() const { |
| 66 if (overriding_content_length) | 66 if (overriding_content_length) |
| 67 return overriding_content_length; | 67 return overriding_content_length; |
| 68 return content_length_; | 68 return content_length_; |
| 69 } | 69 } |
| 70 | 70 |
| 71 uint64 UploadFileElementReader::BytesRemaining() const { | 71 uint64_t UploadFileElementReader::BytesRemaining() const { |
| 72 return bytes_remaining_; | 72 return bytes_remaining_; |
| 73 } | 73 } |
| 74 | 74 |
| 75 int UploadFileElementReader::Read(IOBuffer* buf, | 75 int UploadFileElementReader::Read(IOBuffer* buf, |
| 76 int buf_length, | 76 int buf_length, |
| 77 const CompletionCallback& callback) { | 77 const CompletionCallback& callback) { |
| 78 DCHECK(!callback.is_null()); | 78 DCHECK(!callback.is_null()); |
| 79 | 79 |
| 80 int num_bytes_to_read = static_cast<int>( | 80 int num_bytes_to_read = static_cast<int>( |
| 81 std::min(BytesRemaining(), static_cast<uint64>(buf_length))); | 81 std::min(BytesRemaining(), static_cast<uint64_t>(buf_length))); |
| 82 if (num_bytes_to_read == 0) | 82 if (num_bytes_to_read == 0) |
| 83 return 0; | 83 return 0; |
| 84 | 84 |
| 85 int result = file_stream_->Read( | 85 int result = file_stream_->Read( |
| 86 buf, num_bytes_to_read, | 86 buf, num_bytes_to_read, |
| 87 base::Bind(base::IgnoreResult(&UploadFileElementReader::OnReadCompleted), | 87 base::Bind(base::IgnoreResult(&UploadFileElementReader::OnReadCompleted), |
| 88 weak_ptr_factory_.GetWeakPtr(), | 88 weak_ptr_factory_.GetWeakPtr(), |
| 89 callback)); | 89 callback)); |
| 90 // Even in async mode, FileStream::Read() may return the result synchronously. | 90 // Even in async mode, FileStream::Read() may return the result synchronously. |
| 91 if (result != ERR_IO_PENDING) | 91 if (result != ERR_IO_PENDING) |
| (...skipping 28 matching lines...) Expand all Loading... |
| 120 DCHECK_GT(0, seek_result); | 120 DCHECK_GT(0, seek_result); |
| 121 if (seek_result != ERR_IO_PENDING) | 121 if (seek_result != ERR_IO_PENDING) |
| 122 callback.Run(seek_result); | 122 callback.Run(seek_result); |
| 123 } else { | 123 } else { |
| 124 OnSeekCompleted(callback, OK); | 124 OnSeekCompleted(callback, OK); |
| 125 } | 125 } |
| 126 } | 126 } |
| 127 | 127 |
| 128 void UploadFileElementReader::OnSeekCompleted( | 128 void UploadFileElementReader::OnSeekCompleted( |
| 129 const CompletionCallback& callback, | 129 const CompletionCallback& callback, |
| 130 int64 result) { | 130 int64_t result) { |
| 131 DCHECK(!callback.is_null()); | 131 DCHECK(!callback.is_null()); |
| 132 | 132 |
| 133 if (result < 0) { | 133 if (result < 0) { |
| 134 DLOG(WARNING) << "Failed to seek \"" << path_.value() | 134 DLOG(WARNING) << "Failed to seek \"" << path_.value() |
| 135 << "\" to offset: " << range_offset_ << " (" << result << ")"; | 135 << "\" to offset: " << range_offset_ << " (" << result << ")"; |
| 136 callback.Run(static_cast<int>(result)); | 136 callback.Run(static_cast<int>(result)); |
| 137 return; | 137 return; |
| 138 } | 138 } |
| 139 | 139 |
| 140 base::File::Info* file_info = new base::File::Info; | 140 base::File::Info* file_info = new base::File::Info; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 153 const CompletionCallback& callback, | 153 const CompletionCallback& callback, |
| 154 base::File::Info* file_info, | 154 base::File::Info* file_info, |
| 155 bool result) { | 155 bool result) { |
| 156 DCHECK(!callback.is_null()); | 156 DCHECK(!callback.is_null()); |
| 157 if (!result) { | 157 if (!result) { |
| 158 DLOG(WARNING) << "Failed to get file info of \"" << path_.value() << "\""; | 158 DLOG(WARNING) << "Failed to get file info of \"" << path_.value() << "\""; |
| 159 callback.Run(ERR_FILE_NOT_FOUND); | 159 callback.Run(ERR_FILE_NOT_FOUND); |
| 160 return; | 160 return; |
| 161 } | 161 } |
| 162 | 162 |
| 163 int64 length = file_info->size; | 163 int64_t length = file_info->size; |
| 164 if (range_offset_ < static_cast<uint64>(length)) { | 164 if (range_offset_ < static_cast<uint64_t>(length)) { |
| 165 // Compensate for the offset. | 165 // Compensate for the offset. |
| 166 length = std::min(length - range_offset_, range_length_); | 166 length = std::min(length - range_offset_, range_length_); |
| 167 } | 167 } |
| 168 | 168 |
| 169 // If the underlying file has been changed and the expected file modification | 169 // If the underlying file has been changed and the expected file modification |
| 170 // time is set, treat it as error. Note that |expected_modification_time_| may | 170 // time is set, treat it as error. Note that |expected_modification_time_| may |
| 171 // have gone through multiple conversion steps involving loss of precision | 171 // have gone through multiple conversion steps involving loss of precision |
| 172 // (including conversion to time_t). Therefore the check below only verifies | 172 // (including conversion to time_t). Therefore the check below only verifies |
| 173 // that the timestamps are within one second of each other. This check is used | 173 // that the timestamps are within one second of each other. This check is used |
| 174 // for sliced files. | 174 // for sliced files. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 185 callback.Run(OK); | 185 callback.Run(OK); |
| 186 } | 186 } |
| 187 | 187 |
| 188 int UploadFileElementReader::OnReadCompleted( | 188 int UploadFileElementReader::OnReadCompleted( |
| 189 const CompletionCallback& callback, | 189 const CompletionCallback& callback, |
| 190 int result) { | 190 int result) { |
| 191 if (result == 0) // Reached end-of-file earlier than expected. | 191 if (result == 0) // Reached end-of-file earlier than expected. |
| 192 result = ERR_UPLOAD_FILE_CHANGED; | 192 result = ERR_UPLOAD_FILE_CHANGED; |
| 193 | 193 |
| 194 if (result > 0) { | 194 if (result > 0) { |
| 195 DCHECK_GE(bytes_remaining_, static_cast<uint64>(result)); | 195 DCHECK_GE(bytes_remaining_, static_cast<uint64_t>(result)); |
| 196 bytes_remaining_ -= result; | 196 bytes_remaining_ -= result; |
| 197 } | 197 } |
| 198 | 198 |
| 199 if (!callback.is_null()) | 199 if (!callback.is_null()) |
| 200 callback.Run(result); | 200 callback.Run(result); |
| 201 return result; | 201 return result; |
| 202 } | 202 } |
| 203 | 203 |
| 204 UploadFileElementReader::ScopedOverridingContentLengthForTests:: | 204 UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| 205 ScopedOverridingContentLengthForTests(uint64 value) { | 205 ScopedOverridingContentLengthForTests(uint64_t value) { |
| 206 overriding_content_length = value; | 206 overriding_content_length = value; |
| 207 } | 207 } |
| 208 | 208 |
| 209 UploadFileElementReader::ScopedOverridingContentLengthForTests:: | 209 UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| 210 ~ScopedOverridingContentLengthForTests() { | 210 ~ScopedOverridingContentLengthForTests() { |
| 211 overriding_content_length = 0; | 211 overriding_content_length = 0; |
| 212 } | 212 } |
| 213 | 213 |
| 214 } // namespace net | 214 } // namespace net |
| OLD | NEW |