Chromium Code Reviews| Index: net/base/upload_content_url_element_reader_android.cc |
| diff --git a/net/base/upload_file_element_reader.cc b/net/base/upload_content_url_element_reader_android.cc |
| similarity index 55% |
| copy from net/base/upload_file_element_reader.cc |
| copy to net/base/upload_content_url_element_reader_android.cc |
| index d1f2a12ac8cd13c6d5037032f13cdd98baf2ea03..0145bde633f51cef4defb0ad64160d2544758339 100644 |
| --- a/net/base/upload_file_element_reader.cc |
| +++ b/net/base/upload_content_url_element_reader_android.cc |
| @@ -1,13 +1,14 @@ |
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "net/base/upload_file_element_reader.h" |
| +#include "net/base/upload_content_url_element_reader_android.h" |
| #include "base/bind.h" |
| #include "base/file_util.h" |
| #include "base/location.h" |
| #include "base/task_runner_util.h" |
| +#include "net/android/content_uri_utils.h" |
| #include "net/base/file_stream.h" |
| #include "net/base/io_buffer.h" |
| #include "net/base/net_errors.h" |
| @@ -16,57 +17,42 @@ namespace net { |
| namespace { |
| -// In tests, this value is used to override the return value of |
| -// UploadFileElementReader::GetContentLength() when set to non-zero. |
| -uint64 overriding_content_length = 0; |
| - |
| // This function is used to implement Init(). |
|
joth
2013/10/29 01:30:25
the duplication with UploadFileElementReader does
qinmin
2013/10/29 02:45:57
ok, will do.
On 2013/10/29 01:30:25, joth wrote:
|
| template<typename FileStreamDeleter> |
| -int InitInternal(const base::FilePath& path, |
| +int InitInternal(const GURL& content_url, |
| uint64 range_offset, |
| uint64 range_length, |
| const base::Time& expected_modification_time, |
| scoped_ptr<FileStream, FileStreamDeleter>* out_file_stream, |
| uint64* out_content_length) { |
| scoped_ptr<FileStream> file_stream(new FileStream(NULL)); |
| - int64 rv = file_stream->OpenSync( |
| - path, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
| + int64 rv = file_stream->OpenContentUrlSync( |
| + content_url, base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ); |
| if (rv != OK) { |
| // If the file can't be opened, we'll just upload an empty file. |
| - DLOG(WARNING) << "Failed to open \"" << path.value() |
| + DLOG(WARNING) << "Failed to open \"" << content_url.spec() |
| << "\" for reading: " << rv; |
| file_stream.reset(); |
| } else if (range_offset) { |
| rv = file_stream->SeekSync(FROM_BEGIN, range_offset); |
| if (rv < 0) { |
| - DLOG(WARNING) << "Failed to seek \"" << path.value() |
| + DLOG(WARNING) << "Failed to seek \"" << content_url.spec() |
| << "\" to offset: " << range_offset << " (" << rv << ")"; |
| file_stream.reset(); |
| } |
| } |
| - int64 length = 0; |
| - if (file_stream.get() && |
| - file_util::GetFileSize(path, &length) && |
| + int64 length = GetContentUrlLengthSync(content_url); |
| + if (file_stream.get() && length >= 0 && |
| range_offset < static_cast<uint64>(length)) { |
| // Compensate for the offset. |
| length = std::min(length - range_offset, range_length); |
| + } else { |
| + length = 0; |
| } |
| *out_content_length = length; |
| out_file_stream->reset(file_stream.release()); |
| - // If the underlying file has been changed and the expected file modification |
| - // time is set, treat it as error. Note that the expected modification time |
| - // from WebKit is based on time_t precision. So we have to convert both to |
| - // time_t to compare. This check is used for sliced files. |
| - if (!expected_modification_time.is_null()) { |
| - base::PlatformFileInfo info; |
| - if (file_util::GetFileInfo(path, &info) && |
| - expected_modification_time.ToTimeT() != info.last_modified.ToTimeT()) { |
| - return ERR_UPLOAD_FILE_CHANGED; |
| - } |
| - } |
| - |
| return OK; |
| } |
| @@ -92,14 +78,14 @@ int ReadInternal(scoped_refptr<IOBuffer> buf, |
| } // namespace |
| -UploadFileElementReader::FileStreamDeleter::FileStreamDeleter( |
| +UploadContentUrlElementReader::FileStreamDeleter::FileStreamDeleter( |
| base::TaskRunner* task_runner) : task_runner_(task_runner) { |
| DCHECK(task_runner_.get()); |
| } |
| -UploadFileElementReader::FileStreamDeleter::~FileStreamDeleter() {} |
| +UploadContentUrlElementReader::FileStreamDeleter::~FileStreamDeleter() {} |
| -void UploadFileElementReader::FileStreamDeleter::operator() ( |
| +void UploadContentUrlElementReader::FileStreamDeleter::operator() ( |
| FileStream* file_stream) const { |
| if (file_stream) { |
| task_runner_->PostTask(FROM_HERE, |
| @@ -108,14 +94,14 @@ void UploadFileElementReader::FileStreamDeleter::operator() ( |
| } |
| } |
| -UploadFileElementReader::UploadFileElementReader( |
| +UploadContentUrlElementReader::UploadContentUrlElementReader( |
| base::TaskRunner* task_runner, |
| - const base::FilePath& path, |
| + const GURL& content_url, |
| uint64 range_offset, |
| uint64 range_length, |
| const base::Time& expected_modification_time) |
| : task_runner_(task_runner), |
| - path_(path), |
| + content_url_(content_url), |
| range_offset_(range_offset), |
| range_length_(range_length), |
| expected_modification_time_(expected_modification_time), |
| @@ -126,17 +112,11 @@ UploadFileElementReader::UploadFileElementReader( |
| DCHECK(task_runner_.get()); |
| } |
| -UploadFileElementReader::~UploadFileElementReader() { |
| -} |
| +UploadContentUrlElementReader::~UploadContentUrlElementReader() {} |
| -const UploadFileElementReader* UploadFileElementReader::AsFileReader() const { |
| - return this; |
| -} |
| - |
| -int UploadFileElementReader::Init(const CompletionCallback& callback) { |
| +int UploadContentUrlElementReader::Init(const CompletionCallback& callback) { |
| DCHECK(!callback.is_null()); |
| Reset(); |
| - |
| ScopedFileStreamPtr* file_stream = |
| new ScopedFileStreamPtr(NULL, FileStreamDeleter(task_runner_.get())); |
| uint64* content_length = new uint64; |
| @@ -144,13 +124,13 @@ int UploadFileElementReader::Init(const CompletionCallback& callback) { |
| task_runner_.get(), |
| FROM_HERE, |
| base::Bind(&InitInternal<FileStreamDeleter>, |
| - path_, |
| + content_url_, |
| range_offset_, |
| range_length_, |
| expected_modification_time_, |
| file_stream, |
| content_length), |
| - base::Bind(&UploadFileElementReader::OnInitCompleted, |
| + base::Bind(&UploadContentUrlElementReader::OnInitCompleted, |
| weak_ptr_factory_.GetWeakPtr(), |
| base::Owned(file_stream), |
| base::Owned(content_length), |
| @@ -159,19 +139,17 @@ int UploadFileElementReader::Init(const CompletionCallback& callback) { |
| return ERR_IO_PENDING; |
| } |
| -uint64 UploadFileElementReader::GetContentLength() const { |
| - if (overriding_content_length) |
| - return overriding_content_length; |
| +uint64 UploadContentUrlElementReader::GetContentLength() const { |
| return content_length_; |
| } |
| -uint64 UploadFileElementReader::BytesRemaining() const { |
| +uint64 UploadContentUrlElementReader::BytesRemaining() const { |
| return bytes_remaining_; |
| } |
| -int UploadFileElementReader::Read(IOBuffer* buf, |
| - int buf_length, |
| - const CompletionCallback& callback) { |
| +int UploadContentUrlElementReader::Read(IOBuffer* buf, |
| + int buf_length, |
| + const CompletionCallback& callback) { |
| DCHECK(!callback.is_null()); |
| if (BytesRemaining() == 0) |
| @@ -189,7 +167,7 @@ int UploadFileElementReader::Read(IOBuffer* buf, |
| buf_length, |
| BytesRemaining(), |
| file_stream_ptr), |
| - base::Bind(&UploadFileElementReader::OnReadCompleted, |
| + base::Bind(&UploadContentUrlElementReader::OnReadCompleted, |
| weak_ptr_factory_.GetWeakPtr(), |
| base::Passed(&file_stream_), |
| callback)); |
| @@ -197,14 +175,14 @@ int UploadFileElementReader::Read(IOBuffer* buf, |
| return ERR_IO_PENDING; |
| } |
| -void UploadFileElementReader::Reset() { |
| +void UploadContentUrlElementReader::Reset() { |
| weak_ptr_factory_.InvalidateWeakPtrs(); |
| bytes_remaining_ = 0; |
| content_length_ = 0; |
| file_stream_.reset(); |
| } |
| -void UploadFileElementReader::OnInitCompleted( |
| +void UploadContentUrlElementReader::OnInitCompleted( |
| ScopedFileStreamPtr* file_stream, |
| uint64* content_length, |
| const CompletionCallback& callback, |
| @@ -216,7 +194,7 @@ void UploadFileElementReader::OnInitCompleted( |
| callback.Run(result); |
| } |
| -void UploadFileElementReader::OnReadCompleted( |
| +void UploadContentUrlElementReader::OnReadCompleted( |
| ScopedFileStreamPtr file_stream, |
| const CompletionCallback& callback, |
| int result) { |
| @@ -229,62 +207,4 @@ void UploadFileElementReader::OnReadCompleted( |
| callback.Run(result); |
| } |
| -UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| -ScopedOverridingContentLengthForTests(uint64 value) { |
| - overriding_content_length = value; |
| -} |
| - |
| -UploadFileElementReader::ScopedOverridingContentLengthForTests:: |
| -~ScopedOverridingContentLengthForTests() { |
| - overriding_content_length = 0; |
| -} |
| - |
| -UploadFileElementReaderSync::UploadFileElementReaderSync( |
| - const base::FilePath& path, |
| - uint64 range_offset, |
| - uint64 range_length, |
| - const base::Time& expected_modification_time) |
| - : path_(path), |
| - range_offset_(range_offset), |
| - range_length_(range_length), |
| - expected_modification_time_(expected_modification_time), |
| - content_length_(0), |
| - bytes_remaining_(0) { |
| -} |
| - |
| -UploadFileElementReaderSync::~UploadFileElementReaderSync() { |
| -} |
| - |
| -int UploadFileElementReaderSync::Init(const CompletionCallback& callback) { |
| - bytes_remaining_ = 0; |
| - content_length_ = 0; |
| - file_stream_.reset(); |
| - |
| - const int result = InitInternal(path_, range_offset_, range_length_, |
| - expected_modification_time_, |
| - &file_stream_, &content_length_); |
| - bytes_remaining_ = GetContentLength(); |
| - return result; |
| -} |
| - |
| -uint64 UploadFileElementReaderSync::GetContentLength() const { |
| - return content_length_; |
| -} |
| - |
| -uint64 UploadFileElementReaderSync::BytesRemaining() const { |
| - return bytes_remaining_; |
| -} |
| - |
| -int UploadFileElementReaderSync::Read(IOBuffer* buf, |
| - int buf_length, |
| - const CompletionCallback& callback) { |
| - const int result = ReadInternal(buf, buf_length, BytesRemaining(), |
| - file_stream_.get()); |
| - if (result > 0) { |
| - DCHECK_GE(bytes_remaining_, static_cast<uint64>(result)); |
| - bytes_remaining_ -= result; |
| - } |
| - return result; |
| -} |
| - |
| } // namespace net |