| 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/file_util.h" | 8 #include "base/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 "base/threading/worker_pool.h" | |
| 12 #include "net/base/file_stream.h" | 11 #include "net/base/file_stream.h" |
| 13 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 15 | 14 |
| 16 namespace net { | 15 namespace net { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 // 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 |
| 21 // UploadFileElementReader::GetContentLength() when set to non-zero. | 20 // UploadFileElementReader::GetContentLength() when set to non-zero. |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 DCHECK(file_stream); // file_stream is non-null if content_length_ > 0. | 84 DCHECK(file_stream); // file_stream is non-null if content_length_ > 0. |
| 86 result = file_stream->ReadSync(buf->data(), num_bytes_to_read); | 85 result = file_stream->ReadSync(buf->data(), num_bytes_to_read); |
| 87 if (result == 0) // Reached end-of-file earlier than expected. | 86 if (result == 0) // Reached end-of-file earlier than expected. |
| 88 result = ERR_UPLOAD_FILE_CHANGED; | 87 result = ERR_UPLOAD_FILE_CHANGED; |
| 89 } | 88 } |
| 90 return result; | 89 return result; |
| 91 } | 90 } |
| 92 | 91 |
| 93 } // namespace | 92 } // namespace |
| 94 | 93 |
| 94 UploadFileElementReader::FileStreamDeleter::FileStreamDeleter( |
| 95 base::TaskRunner* task_runner) : task_runner_(task_runner) {} |
| 96 |
| 97 UploadFileElementReader::FileStreamDeleter::~FileStreamDeleter() {} |
| 98 |
| 95 void UploadFileElementReader::FileStreamDeleter::operator() ( | 99 void UploadFileElementReader::FileStreamDeleter::operator() ( |
| 96 FileStream* file_stream) const { | 100 FileStream* file_stream) const { |
| 97 if (file_stream) { | 101 if (file_stream) { |
| 98 base::WorkerPool::PostTask(FROM_HERE, | 102 DCHECK(task_runner_); |
| 99 base::Bind(&base::DeletePointer<FileStream>, | 103 task_runner_->PostTask(FROM_HERE, |
| 100 file_stream), | 104 base::Bind(&base::DeletePointer<FileStream>, |
| 101 true /* task_is_slow */); | 105 file_stream)); |
| 102 } | 106 } |
| 103 } | 107 } |
| 104 | 108 |
| 105 UploadFileElementReader::UploadFileElementReader( | 109 UploadFileElementReader::UploadFileElementReader( |
| 110 base::TaskRunner* task_runner, |
| 106 const FilePath& path, | 111 const FilePath& path, |
| 107 uint64 range_offset, | 112 uint64 range_offset, |
| 108 uint64 range_length, | 113 uint64 range_length, |
| 109 const base::Time& expected_modification_time) | 114 const base::Time& expected_modification_time) |
| 110 : path_(path), | 115 : task_runner_(task_runner), |
| 116 path_(path), |
| 111 range_offset_(range_offset), | 117 range_offset_(range_offset), |
| 112 range_length_(range_length), | 118 range_length_(range_length), |
| 113 expected_modification_time_(expected_modification_time), | 119 expected_modification_time_(expected_modification_time), |
| 120 file_stream_(NULL, FileStreamDeleter(task_runner_)), |
| 114 content_length_(0), | 121 content_length_(0), |
| 115 bytes_remaining_(0), | 122 bytes_remaining_(0), |
| 116 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 123 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 117 } | 124 } |
| 118 | 125 |
| 119 UploadFileElementReader::~UploadFileElementReader() { | 126 UploadFileElementReader::~UploadFileElementReader() { |
| 120 } | 127 } |
| 121 | 128 |
| 122 const UploadFileElementReader* UploadFileElementReader::AsFileReader() const { | 129 const UploadFileElementReader* UploadFileElementReader::AsFileReader() const { |
| 123 return this; | 130 return this; |
| 124 } | 131 } |
| 125 | 132 |
| 126 int UploadFileElementReader::Init(const CompletionCallback& callback) { | 133 int UploadFileElementReader::Init(const CompletionCallback& callback) { |
| 127 DCHECK(!callback.is_null()); | 134 DCHECK(!callback.is_null()); |
| 128 Reset(); | 135 Reset(); |
| 129 | 136 |
| 130 ScopedFileStreamPtr* file_stream = new ScopedFileStreamPtr; | 137 ScopedFileStreamPtr* file_stream = |
| 138 new ScopedFileStreamPtr(NULL, FileStreamDeleter(task_runner_)); |
| 131 uint64* content_length = new uint64; | 139 uint64* content_length = new uint64; |
| 132 const bool posted = base::PostTaskAndReplyWithResult( | 140 const bool posted = base::PostTaskAndReplyWithResult( |
| 133 base::WorkerPool::GetTaskRunner(true /* task_is_slow */), | 141 task_runner_, |
| 134 FROM_HERE, | 142 FROM_HERE, |
| 135 base::Bind(&InitInternal, | 143 base::Bind(&InitInternal, |
| 136 path_, | 144 path_, |
| 137 range_offset_, | 145 range_offset_, |
| 138 range_length_, | 146 range_length_, |
| 139 expected_modification_time_, | 147 expected_modification_time_, |
| 140 file_stream, | 148 file_stream, |
| 141 content_length), | 149 content_length), |
| 142 base::Bind(&UploadFileElementReader::OnInitCompleted, | 150 base::Bind(&UploadFileElementReader::OnInitCompleted, |
| 143 weak_ptr_factory_.GetWeakPtr(), | 151 weak_ptr_factory_.GetWeakPtr(), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 164 DCHECK(!callback.is_null()); | 172 DCHECK(!callback.is_null()); |
| 165 | 173 |
| 166 if (BytesRemaining() == 0) | 174 if (BytesRemaining() == 0) |
| 167 return 0; | 175 return 0; |
| 168 | 176 |
| 169 // Save the value of file_stream_.get() before base::Passed() invalidates it. | 177 // Save the value of file_stream_.get() before base::Passed() invalidates it. |
| 170 FileStream* file_stream_ptr = file_stream_.get(); | 178 FileStream* file_stream_ptr = file_stream_.get(); |
| 171 // Pass the ownership of file_stream_ to the worker pool to safely perform | 179 // Pass the ownership of file_stream_ to the worker pool to safely perform |
| 172 // operation even when |this| is destructed before the read completes. | 180 // operation even when |this| is destructed before the read completes. |
| 173 const bool posted = base::PostTaskAndReplyWithResult( | 181 const bool posted = base::PostTaskAndReplyWithResult( |
| 174 base::WorkerPool::GetTaskRunner(true /* task_is_slow */), | 182 task_runner_, |
| 175 FROM_HERE, | 183 FROM_HERE, |
| 176 base::Bind(&ReadInternal, | 184 base::Bind(&ReadInternal, |
| 177 scoped_refptr<IOBuffer>(buf), | 185 scoped_refptr<IOBuffer>(buf), |
| 178 buf_length, | 186 buf_length, |
| 179 BytesRemaining(), | 187 BytesRemaining(), |
| 180 file_stream_ptr), | 188 file_stream_ptr), |
| 181 base::Bind(&UploadFileElementReader::OnReadCompleted, | 189 base::Bind(&UploadFileElementReader::OnReadCompleted, |
| 182 weak_ptr_factory_.GetWeakPtr(), | 190 weak_ptr_factory_.GetWeakPtr(), |
| 183 base::Passed(&file_stream_), | 191 base::Passed(&file_stream_), |
| 184 callback)); | 192 callback)); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 } | 250 } |
| 243 | 251 |
| 244 UploadFileElementReaderSync::~UploadFileElementReaderSync() { | 252 UploadFileElementReaderSync::~UploadFileElementReaderSync() { |
| 245 } | 253 } |
| 246 | 254 |
| 247 int UploadFileElementReaderSync::Init(const CompletionCallback& callback) { | 255 int UploadFileElementReaderSync::Init(const CompletionCallback& callback) { |
| 248 bytes_remaining_ = 0; | 256 bytes_remaining_ = 0; |
| 249 content_length_ = 0; | 257 content_length_ = 0; |
| 250 file_stream_.reset(); | 258 file_stream_.reset(); |
| 251 | 259 |
| 252 UploadFileElementReader::ScopedFileStreamPtr file_stream; | 260 // It's safe to pass NULL for |deleter| here because it will not be used. |
| 261 UploadFileElementReader::FileStreamDeleter deleter(NULL); |
| 262 UploadFileElementReader::ScopedFileStreamPtr file_stream(NULL, deleter); |
| 253 | 263 |
| 254 const int result = InitInternal(path_, range_offset_, range_length_, | 264 const int result = InitInternal(path_, range_offset_, range_length_, |
| 255 expected_modification_time_, | 265 expected_modification_time_, |
| 256 &file_stream, &content_length_); | 266 &file_stream, &content_length_); |
| 257 file_stream_.reset(file_stream.release()); | 267 file_stream_.reset(file_stream.release()); |
| 258 bytes_remaining_ = GetContentLength(); | 268 bytes_remaining_ = GetContentLength(); |
| 259 return result; | 269 return result; |
| 260 } | 270 } |
| 261 | 271 |
| 262 uint64 UploadFileElementReaderSync::GetContentLength() const { | 272 uint64 UploadFileElementReaderSync::GetContentLength() const { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 273 const int result = ReadInternal(buf, buf_length, BytesRemaining(), | 283 const int result = ReadInternal(buf, buf_length, BytesRemaining(), |
| 274 file_stream_.get()); | 284 file_stream_.get()); |
| 275 if (result > 0) { | 285 if (result > 0) { |
| 276 DCHECK_GE(static_cast<int>(bytes_remaining_), result); | 286 DCHECK_GE(static_cast<int>(bytes_remaining_), result); |
| 277 bytes_remaining_ -= result; | 287 bytes_remaining_ -= result; |
| 278 } | 288 } |
| 279 return result; | 289 return result; |
| 280 } | 290 } |
| 281 | 291 |
| 282 } // namespace net | 292 } // namespace net |
| OLD | NEW |