| 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/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/location.h" |
| 8 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 11 #include "base/threading/worker_pool.h" |
| 9 #include "net/base/file_stream.h" | 12 #include "net/base/file_stream.h" |
| 10 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 11 | 14 |
| 12 namespace net { | 15 namespace net { |
| 13 | 16 |
| 14 namespace { | 17 namespace { |
| 15 | 18 |
| 16 // 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 |
| 17 // UploadFileElementReader::GetContentLength() when set to non-zero. | 20 // UploadFileElementReader::GetContentLength() when set to non-zero. |
| 18 uint64 overriding_content_length = 0; | 21 uint64 overriding_content_length = 0; |
| 19 | 22 |
| 23 // This method is used to implement Init(). |
| 24 void InitInternal(const FilePath& path, |
| 25 uint64 range_offset, |
| 26 uint64 range_length, |
| 27 const base::Time& expected_modification_time, |
| 28 scoped_ptr<FileStream>* out_file_stream, |
| 29 uint64* out_content_length, |
| 30 int* out_result) { |
| 31 scoped_ptr<FileStream> file_stream(new FileStream(NULL)); |
| 32 int64 rv = file_stream->OpenSync( |
| 33 path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
| 34 if (rv != OK) { |
| 35 // If the file can't be opened, we'll just upload an empty file. |
| 36 DLOG(WARNING) << "Failed to open \"" << path.value() |
| 37 << "\" for reading: " << rv; |
| 38 file_stream.reset(); |
| 39 } else if (range_offset) { |
| 40 rv = file_stream->SeekSync(FROM_BEGIN, range_offset); |
| 41 if (rv < 0) { |
| 42 DLOG(WARNING) << "Failed to seek \"" << path.value() |
| 43 << "\" to offset: " << range_offset << " (" << rv << ")"; |
| 44 file_stream->CloseSync(); |
| 45 file_stream.reset(); |
| 46 } |
| 47 } |
| 48 |
| 49 int64 length = 0; |
| 50 if (file_stream.get() && |
| 51 file_util::GetFileSize(path, &length) && |
| 52 range_offset < static_cast<uint64>(length)) { |
| 53 // Compensate for the offset. |
| 54 length = std::min(length - range_offset, range_length); |
| 55 } |
| 56 *out_content_length = length; |
| 57 out_file_stream->swap(file_stream); |
| 58 |
| 59 // If the underlying file has been changed and the expected file modification |
| 60 // time is set, treat it as error. Note that the expected modification time |
| 61 // from WebKit is based on time_t precision. So we have to convert both to |
| 62 // time_t to compare. This check is used for sliced files. |
| 63 if (!expected_modification_time.is_null()) { |
| 64 base::PlatformFileInfo info; |
| 65 if (file_util::GetFileInfo(path, &info) && |
| 66 expected_modification_time.ToTimeT() != info.last_modified.ToTimeT()) { |
| 67 *out_result = ERR_UPLOAD_FILE_CHANGED; |
| 68 return; |
| 69 } |
| 70 } |
| 71 |
| 72 *out_result = OK; |
| 73 } |
| 74 |
| 20 } // namespace | 75 } // namespace |
| 21 | 76 |
| 22 UploadFileElementReader::UploadFileElementReader( | 77 UploadFileElementReader::UploadFileElementReader( |
| 23 const FilePath& path, | 78 const FilePath& path, |
| 24 uint64 range_offset, | 79 uint64 range_offset, |
| 25 uint64 range_length, | 80 uint64 range_length, |
| 26 const base::Time& expected_modification_time) | 81 const base::Time& expected_modification_time) |
| 27 : path_(path), | 82 : path_(path), |
| 28 range_offset_(range_offset), | 83 range_offset_(range_offset), |
| 29 range_length_(range_length), | 84 range_length_(range_length), |
| 30 expected_modification_time_(expected_modification_time), | 85 expected_modification_time_(expected_modification_time), |
| 31 content_length_(0), | 86 content_length_(0), |
| 32 bytes_remaining_(0) { | 87 bytes_remaining_(0), |
| 88 weak_ptr_factory_(this) { |
| 33 } | 89 } |
| 34 | 90 |
| 35 UploadFileElementReader::~UploadFileElementReader() { | 91 UploadFileElementReader::~UploadFileElementReader() { |
| 36 // Temporarily allow until fix: http://crbug.com/72001. | 92 // Temporarily allow until fix: http://crbug.com/72001. |
| 37 base::ThreadRestrictions::ScopedAllowIO allow_io; | 93 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 38 if (file_stream_.get()) | 94 if (file_stream_.get()) |
| 39 file_stream_->CloseSync(); | 95 file_stream_->CloseSync(); |
| 40 } | 96 } |
| 41 | 97 |
| 98 int UploadFileElementReader::Init(const CompletionCallback& callback) { |
| 99 scoped_ptr<FileStream>* file_stream = new scoped_ptr<FileStream>; |
| 100 uint64* content_length = new uint64; |
| 101 int* result = new int; |
| 102 const bool posted = base::WorkerPool::PostTaskAndReply( |
| 103 FROM_HERE, |
| 104 base::Bind(&InitInternal, |
| 105 path_, |
| 106 range_offset_, |
| 107 range_length_, |
| 108 expected_modification_time_, |
| 109 file_stream, |
| 110 content_length, |
| 111 result), |
| 112 base::Bind(&UploadFileElementReader::OnInitCompleted, |
| 113 weak_ptr_factory_.GetWeakPtr(), |
| 114 base::Owned(file_stream), |
| 115 base::Owned(content_length), |
| 116 base::Owned(result), |
| 117 callback), |
| 118 true /* task_is_slow */); |
| 119 DCHECK(posted); |
| 120 return ERR_IO_PENDING; |
| 121 } |
| 122 |
| 42 int UploadFileElementReader::InitSync() { | 123 int UploadFileElementReader::InitSync() { |
| 43 // Temporarily allow until fix: http://crbug.com/72001. | 124 // Temporarily allow until fix: http://crbug.com/72001. |
| 44 base::ThreadRestrictions::ScopedAllowIO allow_io; | 125 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 45 | 126 |
| 46 scoped_ptr<FileStream> file_stream(new FileStream(NULL)); | 127 scoped_ptr<FileStream> file_stream; |
| 47 int64 rv = file_stream->OpenSync( | 128 uint64 content_length = 0; |
| 48 path_, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); | 129 int result = OK; |
| 49 if (rv != OK) { | 130 InitInternal(path_, range_offset_, range_length_, expected_modification_time_, |
| 50 // If the file can't be opened, we'll just upload an empty file. | 131 &file_stream, &content_length, &result); |
| 51 DLOG(WARNING) << "Failed to open \"" << path_.value() | 132 OnInitCompleted(&file_stream, &content_length, &result, CompletionCallback()); |
| 52 << "\" for reading: " << rv; | 133 return result; |
| 53 file_stream.reset(); | |
| 54 } | |
| 55 if (file_stream.get() && range_offset_) { | |
| 56 rv = file_stream->SeekSync(FROM_BEGIN, range_offset_); | |
| 57 if (rv < 0) { | |
| 58 DLOG(WARNING) << "Failed to seek \"" << path_.value() | |
| 59 << "\" to offset: " << range_offset_ << " (" << rv | |
| 60 << ")"; | |
| 61 file_stream->CloseSync(); | |
| 62 file_stream.reset(); | |
| 63 } | |
| 64 } | |
| 65 file_stream_.reset(file_stream.release()); | |
| 66 | |
| 67 int64 length = 0; | |
| 68 if (file_stream_.get() && | |
| 69 file_util::GetFileSize(path_, &length) && | |
| 70 range_offset_ < static_cast<uint64>(length)) { | |
| 71 // Compensate for the offset. | |
| 72 length = std::min(length - range_offset_, range_length_); | |
| 73 } | |
| 74 content_length_ = length; | |
| 75 bytes_remaining_ = GetContentLength(); | |
| 76 | |
| 77 // If the underlying file has been changed and the expected file | |
| 78 // modification time is set, treat it as error. Note that the expected | |
| 79 // modification time from WebKit is based on time_t precision. So we | |
| 80 // have to convert both to time_t to compare. This check is used for | |
| 81 // sliced files. | |
| 82 if (!expected_modification_time_.is_null()) { | |
| 83 base::PlatformFileInfo info; | |
| 84 if (file_util::GetFileInfo(path_, &info) && | |
| 85 expected_modification_time_.ToTimeT() != | |
| 86 info.last_modified.ToTimeT()) { | |
| 87 return ERR_UPLOAD_FILE_CHANGED; | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 return OK; | |
| 92 } | 134 } |
| 93 | 135 |
| 94 uint64 UploadFileElementReader::GetContentLength() const { | 136 uint64 UploadFileElementReader::GetContentLength() const { |
| 95 if (overriding_content_length) | 137 if (overriding_content_length) |
| 96 return overriding_content_length; | 138 return overriding_content_length; |
| 97 return content_length_; | 139 return content_length_; |
| 98 } | 140 } |
| 99 | 141 |
| 100 uint64 UploadFileElementReader::BytesRemaining() const { | 142 uint64 UploadFileElementReader::BytesRemaining() const { |
| 101 return bytes_remaining_; | 143 return bytes_remaining_; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 122 // pad with zero. Otherwise the server will hang waiting for the | 164 // pad with zero. Otherwise the server will hang waiting for the |
| 123 // rest of the data. | 165 // rest of the data. |
| 124 memset(buf, 0, num_bytes_to_read); | 166 memset(buf, 0, num_bytes_to_read); |
| 125 } | 167 } |
| 126 } | 168 } |
| 127 DCHECK_GE(bytes_remaining_, num_bytes_to_read); | 169 DCHECK_GE(bytes_remaining_, num_bytes_to_read); |
| 128 bytes_remaining_ -= num_bytes_to_read; | 170 bytes_remaining_ -= num_bytes_to_read; |
| 129 return num_bytes_to_read; | 171 return num_bytes_to_read; |
| 130 } | 172 } |
| 131 | 173 |
| 174 void UploadFileElementReader::OnInitCompleted( |
| 175 scoped_ptr<FileStream>* file_stream, |
| 176 uint64* content_length, |
| 177 int* result, |
| 178 const CompletionCallback& callback) { |
| 179 file_stream_.swap(*file_stream); |
| 180 content_length_ = *content_length; |
| 181 bytes_remaining_ = GetContentLength(); |
| 182 if (!callback.is_null()) |
| 183 callback.Run(*result); |
| 184 } |
| 185 |
| 132 UploadFileElementReader::ScopedOverridingContentLengthForTests:: | 186 UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| 133 ScopedOverridingContentLengthForTests(uint64 value) { | 187 ScopedOverridingContentLengthForTests(uint64 value) { |
| 134 overriding_content_length = value; | 188 overriding_content_length = value; |
| 135 } | 189 } |
| 136 | 190 |
| 137 UploadFileElementReader::ScopedOverridingContentLengthForTests:: | 191 UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| 138 ~ScopedOverridingContentLengthForTests() { | 192 ~ScopedOverridingContentLengthForTests() { |
| 139 overriding_content_length = 0; | 193 overriding_content_length = 0; |
| 140 } | 194 } |
| 141 | 195 |
| 142 } // namespace net | 196 } // namespace net |
| OLD | NEW |