diff options
author | pilgrim@chromium.org <pilgrim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-25 00:36:08 +0000 |
---|---|---|
committer | pilgrim@chromium.org <pilgrim@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-25 00:36:08 +0000 |
commit | 0664e7e32c3e128a9ac4b61e5b5cbfbcc1c30c30 (patch) | |
tree | 12856007351863f2e08d83e1cce682b1548ef389 /content/browser/fileapi | |
parent | 76e01b9e22efcc1c9f47cafa242ebba65111fc07 (diff) | |
download | chromium_src-0664e7e32c3e128a9ac4b61e5b5cbfbcc1c30c30.zip chromium_src-0664e7e32c3e128a9ac4b61e5b5cbfbcc1c30c30.tar.gz chromium_src-0664e7e32c3e128a9ac4b61e5b5cbfbcc1c30c30.tar.bz2 |
Move upload_file_system_file_element_reader.* to content/browser/fileapi/
BUG=338338
TBR=darin@chromium.org
Review URL: https://codereview.chromium.org/159523005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@253037 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/fileapi')
3 files changed, 184 insertions, 2 deletions
diff --git a/content/browser/fileapi/upload_file_system_file_element_reader.cc b/content/browser/fileapi/upload_file_system_file_element_reader.cc new file mode 100644 index 0000000..9e6a288 --- /dev/null +++ b/content/browser/fileapi/upload_file_system_file_element_reader.cc @@ -0,0 +1,117 @@ +// Copyright 2014 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 "content/browser/fileapi/upload_file_system_file_element_reader.h" + +#include <algorithm> + +#include "base/bind.h" +#include "net/base/net_errors.h" +#include "webkit/browser/blob/file_stream_reader.h" +#include "webkit/browser/fileapi/file_system_context.h" +#include "webkit/browser/fileapi/file_system_url.h" + +namespace content { + +UploadFileSystemFileElementReader::UploadFileSystemFileElementReader( + fileapi::FileSystemContext* file_system_context, + const GURL& url, + uint64 range_offset, + uint64 range_length, + const base::Time& expected_modification_time) + : file_system_context_(file_system_context), + url_(url), + range_offset_(range_offset), + range_length_(range_length), + expected_modification_time_(expected_modification_time), + stream_length_(0), + position_(0), + weak_ptr_factory_(this) { +} + +UploadFileSystemFileElementReader::~UploadFileSystemFileElementReader() { +} + +int UploadFileSystemFileElementReader::Init( + const net::CompletionCallback& callback) { + // Reset states. + weak_ptr_factory_.InvalidateWeakPtrs(); + stream_length_ = 0; + position_ = 0; + + // Initialize the stream reader and the length. + stream_reader_ = + file_system_context_->CreateFileStreamReader( + file_system_context_->CrackURL(url_), + range_offset_, + expected_modification_time_); + DCHECK(stream_reader_); + + const int64 result = stream_reader_->GetLength( + base::Bind(&UploadFileSystemFileElementReader::OnGetLength, + weak_ptr_factory_.GetWeakPtr(), + callback)); + if (result >= 0) { + stream_length_ = result; + return net::OK; + } + + // The error code can be casted to int. + return static_cast<int>(result); +} + +uint64 UploadFileSystemFileElementReader::GetContentLength() const { + return std::min(stream_length_, range_length_); +} + +uint64 UploadFileSystemFileElementReader::BytesRemaining() const { + return GetContentLength() - position_; +} + +int UploadFileSystemFileElementReader::Read( + net::IOBuffer* buf, + int buf_length, + const net::CompletionCallback& callback) { + DCHECK_LT(0, buf_length); + DCHECK(stream_reader_); + + const uint64 num_bytes_to_read = + std::min(BytesRemaining(), static_cast<uint64>(buf_length)); + + if (num_bytes_to_read == 0) + return 0; + + const int result = stream_reader_->Read( + buf, num_bytes_to_read, + base::Bind(&UploadFileSystemFileElementReader::OnRead, + weak_ptr_factory_.GetWeakPtr(), + callback)); + if (result >= 0) + OnRead(net::CompletionCallback(), result); + return result; +} + +void UploadFileSystemFileElementReader::OnGetLength( + const net::CompletionCallback& callback, + int64 result) { + if (result >= 0) { + stream_length_ = result; + callback.Run(net::OK); + return; + } + callback.Run(result); +} + +void UploadFileSystemFileElementReader::OnRead( + const net::CompletionCallback& callback, + int result) { + if (result > 0) { + position_ += result; + DCHECK_LE(position_, GetContentLength()); + } + if (!callback.is_null()) + callback.Run(result); +} + +} // namespace content diff --git a/content/browser/fileapi/upload_file_system_file_element_reader.h b/content/browser/fileapi/upload_file_system_file_element_reader.h new file mode 100644 index 0000000..1b33489 --- /dev/null +++ b/content/browser/fileapi/upload_file_system_file_element_reader.h @@ -0,0 +1,66 @@ +// Copyright 2014 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. + +#ifndef CONTENT_BROWSER_FILEAPI_UPLOAD_FILE_SYSTEM_FILE_ELEMENT_READER_H_ +#define CONTENT_BROWSER_FILEAPI_UPLOAD_FILE_SYSTEM_FILE_ELEMENT_READER_H_ + +#include "base/memory/weak_ptr.h" +#include "base/time/time.h" +#include "content/common/content_export.h" +#include "net/base/upload_element_reader.h" +#include "url/gurl.h" + +namespace webkit_blob { +class FileStreamReader; +} + +namespace fileapi { +class FileSystemContext; +} + +namespace content { + +// An UploadElementReader implementation for filesystem file. +class CONTENT_EXPORT UploadFileSystemFileElementReader : + NON_EXPORTED_BASE(public net::UploadElementReader) { + public: + UploadFileSystemFileElementReader( + fileapi::FileSystemContext* file_system_context, + const GURL& url, + uint64 range_offset, + uint64 range_length, + const base::Time& expected_modification_time); + virtual ~UploadFileSystemFileElementReader(); + + // UploadElementReader overrides: + virtual int Init(const net::CompletionCallback& callback) OVERRIDE; + virtual uint64 GetContentLength() const OVERRIDE; + virtual uint64 BytesRemaining() const OVERRIDE; + virtual int Read(net::IOBuffer* buf, + int buf_length, + const net::CompletionCallback& callback) OVERRIDE; + + private: + void OnGetLength(const net::CompletionCallback& callback, int64 result); + void OnRead(const net::CompletionCallback& callback, int result); + + scoped_refptr<fileapi::FileSystemContext> file_system_context_; + const GURL url_; + const uint64 range_offset_; + const uint64 range_length_; + const base::Time expected_modification_time_; + + scoped_ptr<webkit_blob::FileStreamReader> stream_reader_; + + uint64 stream_length_; + uint64 position_; + + base::WeakPtrFactory<UploadFileSystemFileElementReader> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(UploadFileSystemFileElementReader); +}; + +} // namespace content + +#endif // CONTENT_BROWSER_FILEAPI_UPLOAD_FILE_SYSTEM_FILE_ELEMENT_READER_H_ diff --git a/content/browser/fileapi/upload_file_system_file_element_reader_unittest.cc b/content/browser/fileapi/upload_file_system_file_element_reader_unittest.cc index bb72adc..641b968 100644 --- a/content/browser/fileapi/upload_file_system_file_element_reader_unittest.cc +++ b/content/browser/fileapi/upload_file_system_file_element_reader_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "webkit/browser/fileapi/upload_file_system_file_element_reader.h" +#include "content/browser/fileapi/upload_file_system_file_element_reader.h" #include "base/files/scoped_temp_dir.h" #include "base/message_loop/message_loop.h" @@ -21,7 +21,6 @@ using content::AsyncFileTestHelper; using fileapi::FileSystemContext; using fileapi::FileSystemType; using fileapi::FileSystemURL; -using fileapi::UploadFileSystemFileElementReader; namespace content { |