diff options
author | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-11 10:36:06 +0000 |
---|---|---|
committer | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-11 10:36:06 +0000 |
commit | b2d26cfdcc1201f5e459ca3f9e0278e3fe45a45f (patch) | |
tree | 839657d1fc942a5976fe8a540c11bdf236f6fb22 /net/base | |
parent | e2c00ede28ae5323b806a8fe1b108d020300e392 (diff) | |
download | chromium_src-b2d26cfdcc1201f5e459ca3f9e0278e3fe45a45f.zip chromium_src-b2d26cfdcc1201f5e459ca3f9e0278e3fe45a45f.tar.gz chromium_src-b2d26cfdcc1201f5e459ca3f9e0278e3fe45a45f.tar.bz2 |
net: Make UploadDataStream independent from UploadData
Change argument of UploadDataStream's ctor from UploadData to ScopedVector<UploadElementReader>
Move chunk related logic from UploadData to UploadDataStream.
Move UploadElementReader creation code from UploadElementReader::Create to URLRequest::set_upload. (this code will soon be moved again to src/webkit/glue/resource_request_body.cc)
BUG=156574
TEST=net_unittests
TBR=jam@chromium.org for chrome/browser/automation (already reviewed by ananta@)
Review URL: https://chromiumcodereview.appspot.com/11419220
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@172295 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/upload_data.cc | 19 | ||||
-rw-r--r-- | net/base/upload_data.h | 13 | ||||
-rw-r--r-- | net/base/upload_data_stream.cc | 118 | ||||
-rw-r--r-- | net/base/upload_data_stream.h | 39 | ||||
-rw-r--r-- | net/base/upload_data_stream_unittest.cc | 178 | ||||
-rw-r--r-- | net/base/upload_element_reader.cc | 22 | ||||
-rw-r--r-- | net/base/upload_element_reader.h | 3 |
7 files changed, 167 insertions, 225 deletions
diff --git a/net/base/upload_data.cc b/net/base/upload_data.cc index 91923a5..5c53e08 100644 --- a/net/base/upload_data.cc +++ b/net/base/upload_data.cc @@ -4,10 +4,7 @@ #include "net/base/upload_data.h" -#include "base/bind.h" -#include "base/location.h" #include "base/logging.h" -#include "base/threading/worker_pool.h" namespace net { @@ -34,22 +31,6 @@ void UploadData::AppendFileRange(const FilePath& file_path, expected_modification_time); } -void UploadData::AppendChunk(const char* bytes, - int bytes_len, - bool is_last_chunk) { - DCHECK(is_chunked_); - DCHECK(!last_chunk_appended_); - elements_.push_back(new UploadElement()); - elements_.back()->SetToBytes(bytes, bytes_len); - last_chunk_appended_ = is_last_chunk; - if (!chunk_callback_.is_null()) - chunk_callback_.Run(); -} - -void UploadData::set_chunk_callback(const base::Closure& callback) { - chunk_callback_ = callback; -} - UploadData::~UploadData() { } diff --git a/net/base/upload_data.h b/net/base/upload_data.h index 2f0494c..4e6c6ca 100644 --- a/net/base/upload_data.h +++ b/net/base/upload_data.h @@ -6,7 +6,6 @@ #define NET_BASE_UPLOAD_DATA_H_ #include "base/basictypes.h" -#include "base/callback.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_vector.h" #include "base/supports_user_data.h" @@ -28,10 +27,6 @@ namespace net { // Until there is a more abstract class for this, this one derives from // SupportsUserData to allow users to stash random data by // key and ensure its destruction when UploadData is finally deleted. -// -// Chunked uploads are handled by repeatedly calling AppendChunk() as data -// becomes available, which adds to |elements_|. Whenever this happens, -// |chunk_callback_| is called, if non-NULL. class NET_EXPORT UploadData : public base::RefCounted<UploadData>, public base::SupportsUserData { @@ -44,13 +39,6 @@ class NET_EXPORT UploadData uint64 offset, uint64 length, const base::Time& expected_modification_time); - // Adds the given chunk of bytes to be sent immediately with chunked transfer - // encoding. - void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); - - // Sets the callback to be invoked when a new chunk is available to upload. - void set_chunk_callback(const base::Closure& callback); - // Initializes the object to send chunks of upload data over time rather // than all at once. Chunked data may only contain bytes, not files. void set_is_chunked(bool set) { is_chunked_ = set; } @@ -85,7 +73,6 @@ class NET_EXPORT UploadData ScopedVector<UploadElement> elements_; int64 identifier_; - base::Closure chunk_callback_; bool is_chunked_; bool last_chunk_appended_; diff --git a/net/base/upload_data_stream.cc b/net/base/upload_data_stream.cc index 750302e..c3a2f0b 100644 --- a/net/base/upload_data_stream.cc +++ b/net/base/upload_data_stream.cc @@ -7,11 +7,29 @@ #include "base/logging.h" #include "net/base/io_buffer.h" #include "net/base/net_errors.h" -#include "net/base/upload_data.h" +#include "net/base/upload_bytes_element_reader.h" #include "net/base/upload_element_reader.h" namespace net { +namespace { + +// A subclass of UplodBytesElementReader which owns the data given as a vector. +class UploadOwnedBytesElementReader : public UploadBytesElementReader { + public: + UploadOwnedBytesElementReader(std::vector<char>* data) + : UploadBytesElementReader(&(*data)[0], data->size()) { + data_.swap(*data); + } + + virtual ~UploadOwnedBytesElementReader() {} + + private: + std::vector<char> data_; +}; + +} // namespace + bool UploadDataStream::merge_chunks_ = true; // static @@ -20,21 +38,29 @@ void UploadDataStream::ResetMergeChunks() { merge_chunks_ = true; } -UploadDataStream::UploadDataStream(UploadData* upload_data) - : upload_data_(upload_data), - element_index_(0), +UploadDataStream::UploadDataStream( + ScopedVector<UploadElementReader>* element_readers, + int64 identifier) + : element_index_(0), + total_size_(0), + current_position_(0), + identifier_(identifier), + is_chunked_(false), + last_chunk_appended_(false), + initialized_successfully_(false), + weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { + element_readers_.swap(*element_readers); +} + +UploadDataStream::UploadDataStream(Chunked /*chunked*/, int64 identifier) + : element_index_(0), total_size_(0), current_position_(0), + identifier_(identifier), + is_chunked_(true), + last_chunk_appended_(false), initialized_successfully_(false), - weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), - weak_ptr_factory_for_chunks_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { - const ScopedVector<UploadElement>& elements = upload_data_->elements(); - for (size_t i = 0; i < elements.size(); ++i) - element_readers_.push_back(UploadElementReader::Create(*elements[i])); - - upload_data_->set_chunk_callback( - base::Bind(&UploadDataStream::OnChunkAvailable, - weak_ptr_factory_for_chunks_.GetWeakPtr())); + weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { } UploadDataStream::~UploadDataStream() { @@ -79,20 +105,12 @@ int UploadDataStream::ReadSync(IOBuffer* buf, int buf_len) { CompletionCallback()); } -int64 UploadDataStream::identifier() const { - return upload_data_->identifier(); -} - -bool UploadDataStream::is_chunked() const { - return upload_data_->is_chunked(); -} - bool UploadDataStream::IsEOF() const { DCHECK(initialized_successfully_); // Check if all elements are consumed. if (element_index_ == element_readers_.size()) { // If the upload data is chunked, check if the last chunk is appended. - if (!is_chunked() || upload_data_->last_chunk_appended()) + if (!is_chunked_ || last_chunk_appended_) return true; } return false; @@ -104,7 +122,7 @@ bool UploadDataStream::IsInMemory() const { // are ready. Check is_chunked_ here, rather than relying on the loop // below, as there is a case that is_chunked_ is set to true, but the // first chunk is not yet delivered. - if (is_chunked()) + if (is_chunked_) return false; for (size_t i = 0; i < element_readers_.size(); ++i) { @@ -114,6 +132,29 @@ bool UploadDataStream::IsInMemory() const { return true; } +void UploadDataStream::AppendChunk(const char* bytes, + int bytes_len, + bool is_last_chunk) { + DCHECK(is_chunked_); + DCHECK(!last_chunk_appended_); + last_chunk_appended_ = is_last_chunk; + + // Initialize a reader for the newly appended chunk. We leave |total_size_| at + // zero, since for chunked uploads, we may not know the total size. + std::vector<char> data(bytes, bytes + bytes_len); + UploadElementReader* reader = new UploadOwnedBytesElementReader(&data); + const int rv = reader->InitSync(); + DCHECK_EQ(OK, rv); + element_readers_.push_back(reader); + + // Resume pending read. + if (!pending_chunked_read_callback_.is_null()) { + base::Closure callback = pending_chunked_read_callback_; + pending_chunked_read_callback_.Reset(); + callback.Run(); + } +} + void UploadDataStream::Reset() { weak_ptr_factory_.InvalidateWeakPtrs(); pending_chunked_read_callback_.Reset(); @@ -166,7 +207,7 @@ void UploadDataStream::ResumePendingInit(int start_index, void UploadDataStream::FinalizeInitialization() { DCHECK(!initialized_successfully_); - if (!is_chunked()) { + if (!is_chunked_) { uint64 total_size = 0; for (size_t i = 0; i < element_readers_.size(); ++i) { UploadElementReader* reader = element_readers_[i]; @@ -193,7 +234,7 @@ int UploadDataStream::ReadInternal(scoped_refptr<DrainableIOBuffer> buf, break; // Some tests need chunks to be kept unmerged. - if (!merge_chunks_ && is_chunked() && buf->BytesConsumed()) + if (!merge_chunks_ && is_chunked_ && buf->BytesConsumed()) break; int result = OK; @@ -218,7 +259,7 @@ int UploadDataStream::ReadInternal(scoped_refptr<DrainableIOBuffer> buf, const int bytes_copied = buf->BytesConsumed(); current_position_ += bytes_copied; - if (is_chunked() && !IsEOF() && bytes_copied == 0) { + if (is_chunked_ && !IsEOF() && bytes_copied == 0) { DCHECK(!callback.is_null()); DCHECK(pending_chunked_read_callback_.is_null()); pending_chunked_read_callback_ = @@ -246,29 +287,4 @@ void UploadDataStream::ResumePendingRead(scoped_refptr<DrainableIOBuffer> buf, callback.Run(result); } -void UploadDataStream::OnChunkAvailable() { - DCHECK(is_chunked()); - - // Initialize a reader for the newly appended chunk. - const ScopedVector<UploadElement>& elements = upload_data_->elements(); - DCHECK_EQ(elements.size(), element_readers_.size() + 1); - - // We can initialize the reader synchronously here because only bytes can be - // appended for chunked data. We leave |total_size_| at zero, since for - // chunked uploads, we may not know the total size. - const UploadElement& element = *elements.back(); - DCHECK_EQ(UploadElement::TYPE_BYTES, element.type()); - UploadElementReader* reader = UploadElementReader::Create(element); - const int rv = reader->InitSync(); - DCHECK_EQ(OK, rv); - element_readers_.push_back(reader); - - // Resume pending read. - if (!pending_chunked_read_callback_.is_null()) { - base::Closure callback = pending_chunked_read_callback_; - pending_chunked_read_callback_.Reset(); - callback.Run(); - } -} - } // namespace net diff --git a/net/base/upload_data_stream.h b/net/base/upload_data_stream.h index 021b6ca..2465302 100644 --- a/net/base/upload_data_stream.h +++ b/net/base/upload_data_stream.h @@ -16,13 +16,22 @@ namespace net { class DrainableIOBuffer; class IOBuffer; -class UploadData; class UploadElementReader; // A class to read all elements from an UploadData object. class NET_EXPORT UploadDataStream { public: - explicit UploadDataStream(UploadData* upload_data); + // An enum used to construct chunked data stream. + enum Chunked { CHUNKED }; + + // Constructs a non-chunked data stream. + // |element_readers| is cleared by this ctor. + UploadDataStream(ScopedVector<UploadElementReader>* element_readers, + int64 identifier); + + // Constructs a chunked data stream. + UploadDataStream(Chunked chunked, int64 identifier); + ~UploadDataStream(); // Initializes the stream. This function must be called before calling any @@ -62,7 +71,7 @@ class NET_EXPORT UploadDataStream { // Identifies a particular upload instance, which is used by the cache to // formulate a cache key. This value should be unique across browser // sessions. A value of 0 is used to indicate an unspecified identifier. - int64 identifier() const; + int64 identifier() const { return identifier_; } // Returns the total size of the data stream and the current position. // size() is not to be used to determine whether the stream has ended @@ -72,7 +81,8 @@ class NET_EXPORT UploadDataStream { uint64 size() const { return total_size_; } uint64 position() const { return current_position_; } - bool is_chunked() const; + bool is_chunked() const { return is_chunked_; } + bool last_chunk_appended() const { return last_chunk_appended_; } const ScopedVector<UploadElementReader>& element_readers() const { return element_readers_; @@ -85,19 +95,15 @@ class NET_EXPORT UploadDataStream { // Returns true if the upload data in the stream is entirely in memory. bool IsInMemory() const; + // Adds the given chunk of bytes to be sent with chunked transfer encoding. + void AppendChunk(const char* bytes, int bytes_len, bool is_last_chunk); + private: friend class SpdyHttpStreamSpdy2Test; friend class SpdyHttpStreamSpdy3Test; friend class SpdyNetworkTransactionSpdy2Test; friend class SpdyNetworkTransactionSpdy3Test; - // TODO(hashimoto): Stop directly accsssing element_readers_ from tests and - // remove these friend declarations. - FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsync); - FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureAsync); - FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, InitAsyncFailureSync); - FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, ReadAsync); - // Resets this instance to the uninitialized state. void Reset(); @@ -124,14 +130,10 @@ class NET_EXPORT UploadDataStream { const CompletionCallback& callback, int previous_result); - // This method is called when a new chunk is available. - void OnChunkAvailable(); - // These methods are provided only to be used by unit tests. static void ResetMergeChunks(); static void set_merge_chunks(bool merge) { merge_chunks_ = merge; } - scoped_refptr<UploadData> upload_data_; ScopedVector<UploadElementReader> element_readers_; // Index of the current upload element (i.e. the element currently being @@ -144,6 +146,11 @@ class NET_EXPORT UploadDataStream { uint64 total_size_; uint64 current_position_; + const int64 identifier_; + + const bool is_chunked_; + bool last_chunk_appended_; + // True if the initialization was successful. bool initialized_successfully_; @@ -152,8 +159,6 @@ class NET_EXPORT UploadDataStream { base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_; - base::WeakPtrFactory<UploadDataStream> weak_ptr_factory_for_chunks_; - // TODO(satish): Remove this once we have a better way to unit test POST // requests with chunked uploads. static bool merge_chunks_; diff --git a/net/base/upload_data_stream_unittest.cc b/net/base/upload_data_stream_unittest.cc index 0960c30..ee96e3c 100644 --- a/net/base/upload_data_stream_unittest.cc +++ b/net/base/upload_data_stream_unittest.cc @@ -18,7 +18,7 @@ #include "net/base/io_buffer.h" #include "net/base/net_errors.h" #include "net/base/test_completion_callback.h" -#include "net/base/upload_data.h" +#include "net/base/upload_bytes_element_reader.h" #include "net/base/upload_file_element_reader.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" @@ -128,8 +128,6 @@ class MockCompletionCallback { class UploadDataStreamTest : public PlatformTest { public: - UploadDataStreamTest() : upload_data_(new UploadData) { } - virtual void SetUp() OVERRIDE { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } @@ -139,13 +137,11 @@ class UploadDataStreamTest : public PlatformTest { bool error_expected); base::ScopedTempDir temp_dir_; - - scoped_refptr<UploadData> upload_data_; + ScopedVector<UploadElementReader> element_readers_; }; TEST_F(UploadDataStreamTest, EmptyUploadData) { - upload_data_->AppendBytes("", 0); - UploadDataStream stream(upload_data_); + UploadDataStream stream(&element_readers_, 0); ASSERT_EQ(OK, stream.InitSync()); EXPECT_TRUE(stream.IsInMemory()); EXPECT_EQ(0U, stream.size()); @@ -154,8 +150,9 @@ TEST_F(UploadDataStreamTest, EmptyUploadData) { } TEST_F(UploadDataStreamTest, ConsumeAllBytes) { - upload_data_->AppendBytes(kTestData, kTestDataSize); - UploadDataStream stream(upload_data_); + element_readers_.push_back(new UploadBytesElementReader( + kTestData, kTestDataSize)); + UploadDataStream stream(&element_readers_, 0); ASSERT_EQ(OK, stream.InitSync()); EXPECT_TRUE(stream.IsInMemory()); EXPECT_EQ(kTestDataSize, stream.size()); @@ -177,9 +174,10 @@ TEST_F(UploadDataStreamTest, File) { ASSERT_EQ(static_cast<int>(kTestDataSize), file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); - upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); + element_readers_.push_back(new UploadFileElementReader( + temp_file_path, 0, kuint64max, base::Time())); - UploadDataStream stream(upload_data_); + UploadDataStream stream(&element_readers_, 0); ASSERT_EQ(OK, stream.InitSync()); EXPECT_FALSE(stream.IsInMemory()); EXPECT_EQ(kTestDataSize, stream.size()); @@ -205,9 +203,10 @@ TEST_F(UploadDataStreamTest, FileSmallerThanLength) { UploadFileElementReader::ScopedOverridingContentLengthForTests overriding_content_length(kFakeSize); - upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); + element_readers_.push_back(new UploadFileElementReader( + temp_file_path, 0, kuint64max, base::Time())); - UploadDataStream stream(upload_data_); + UploadDataStream stream(&element_readers_, 0); ASSERT_EQ(OK, stream.InitSync()); EXPECT_FALSE(stream.IsInMemory()); EXPECT_EQ(kFakeSize, stream.size()); @@ -236,13 +235,14 @@ TEST_F(UploadDataStreamTest, FileAndBytes) { const uint64 kFileRangeOffset = 1; const uint64 kFileRangeLength = 4; - upload_data_->AppendFileRange( - temp_file_path, kFileRangeOffset, kFileRangeLength, base::Time()); + element_readers_.push_back(new UploadFileElementReader( + temp_file_path, kFileRangeOffset, kFileRangeLength, base::Time())); - upload_data_->AppendBytes(kTestData, kTestDataSize); + element_readers_.push_back(new UploadBytesElementReader( + kTestData, kTestDataSize)); const uint64 kStreamSize = kTestDataSize + kFileRangeLength; - UploadDataStream stream(upload_data_); + UploadDataStream stream(&element_readers_, 0); ASSERT_EQ(OK, stream.InitSync()); EXPECT_FALSE(stream.IsInMemory()); EXPECT_EQ(kStreamSize, stream.size()); @@ -258,12 +258,11 @@ TEST_F(UploadDataStreamTest, FileAndBytes) { } TEST_F(UploadDataStreamTest, Chunk) { - upload_data_->set_is_chunked(true); - upload_data_->AppendChunk(kTestData, kTestDataSize, false); - upload_data_->AppendChunk(kTestData, kTestDataSize, true); - const uint64 kStreamSize = kTestDataSize*2; - UploadDataStream stream(upload_data_); + UploadDataStream stream(UploadDataStream::CHUNKED, 0); + stream.AppendChunk(kTestData, kTestDataSize, false); + stream.AppendChunk(kTestData, kTestDataSize, true); + ASSERT_EQ(OK, stream.InitSync()); EXPECT_FALSE(stream.IsInMemory()); EXPECT_EQ(0U, stream.size()); // Content-Length is 0 for chunked data. @@ -280,31 +279,30 @@ TEST_F(UploadDataStreamTest, Chunk) { // Init() with on-memory and not-on-memory readers. TEST_F(UploadDataStreamTest, InitAsync) { - // Create stream without element readers. - UploadDataStream stream(upload_data_); - - // Set mock readers to the stream. + // Create UploadDataStream with mock readers. MockUploadElementReader* reader = NULL; reader = new MockUploadElementReader(kTestDataSize, true); EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK)); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); reader = new MockUploadElementReader(kTestDataSize, true); EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK)); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); reader = new MockUploadElementReader(kTestDataSize, false); reader->SetAsyncInitExpectation(OK); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); reader = new MockUploadElementReader(kTestDataSize, false); reader->SetAsyncInitExpectation(OK); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); reader = new MockUploadElementReader(kTestDataSize, true); EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK)); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); + + UploadDataStream stream(&element_readers_, 0); // Run Init(). MockCompletionCallback mock_callback; @@ -315,15 +313,14 @@ TEST_F(UploadDataStreamTest, InitAsync) { // Init() of a reader fails asynchronously. TEST_F(UploadDataStreamTest, InitAsyncFailureAsync) { - // Create stream without element readers. - UploadDataStream stream(upload_data_); - - // Set a mock reader to the stream. + // Create UploadDataStream with a mock reader. MockUploadElementReader* reader = NULL; reader = new MockUploadElementReader(kTestDataSize, false); reader->SetAsyncInitExpectation(ERR_FAILED); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); + + UploadDataStream stream(&element_readers_, 0); // Run Init(). MockCompletionCallback mock_callback; @@ -334,19 +331,18 @@ TEST_F(UploadDataStreamTest, InitAsyncFailureAsync) { // Init() of a reader fails synchronously. TEST_F(UploadDataStreamTest, InitAsyncFailureSync) { - // Create stream without element readers. - UploadDataStream stream(upload_data_); - - // Set mock readers to the stream. + // Create UploadDataStream with mock readers. MockUploadElementReader* reader = NULL; reader = new MockUploadElementReader(kTestDataSize, false); reader->SetAsyncInitExpectation(OK); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); reader = new MockUploadElementReader(kTestDataSize, true); EXPECT_CALL(*reader, Init(_)).WillOnce(Return(ERR_FAILED)); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); + + UploadDataStream stream(&element_readers_, 0); // Run Init(). MockCompletionCallback mock_callback; @@ -357,8 +353,9 @@ TEST_F(UploadDataStreamTest, InitAsyncFailureSync) { // Read with a buffer whose size is same as the data. TEST_F(UploadDataStreamTest, ReadAsyncWithExactSizeBuffer) { - upload_data_->AppendBytes(kTestData, kTestDataSize); - UploadDataStream stream(upload_data_); + element_readers_.push_back(new UploadBytesElementReader( + kTestData, kTestDataSize)); + UploadDataStream stream(&element_readers_, 0); MockCompletionCallback mock_callback; EXPECT_CALL(mock_callback, Run(_)).Times(0); @@ -378,31 +375,30 @@ TEST_F(UploadDataStreamTest, ReadAsyncWithExactSizeBuffer) { // Async Read() with on-memory and not-on-memory readers. TEST_F(UploadDataStreamTest, ReadAsync) { - // Create stream without element readers. - UploadDataStream stream(upload_data_); - - // Set mock readers to the stream. + // Create UploadDataStream with mock readers. MockUploadElementReader* reader = NULL; reader = new MockUploadElementReader(kTestDataSize, true); EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK)); reader->SetReadExpectation(kTestDataSize); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); reader = new MockUploadElementReader(kTestDataSize, false); reader->SetAsyncInitExpectation(OK); reader->SetReadExpectation(kTestDataSize); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); reader = new MockUploadElementReader(kTestDataSize, true); EXPECT_CALL(*reader, Init(_)).WillOnce(Return(OK)); reader->SetReadExpectation(kTestDataSize); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); reader = new MockUploadElementReader(kTestDataSize, false); reader->SetAsyncInitExpectation(OK); reader->SetReadExpectation(kTestDataSize); - stream.element_readers_.push_back(reader); + element_readers_.push_back(reader); + + UploadDataStream stream(&element_readers_, 0); // Run Init(). MockCompletionCallback mock_callback; @@ -434,12 +430,12 @@ TEST_F(UploadDataStreamTest, ReadAsync) { void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path, const base::Time& time, bool error_expected) { - // Don't use upload_data_ here, as this function is called twice, and - // reusing upload_data_ is wrong. - scoped_refptr<UploadData> upload_data(new UploadData); - upload_data->AppendFileRange(file_path, 1, 2, time); + // Don't use element_readers_ here, as this function is called twice, and + // reusing element_readers_ is wrong. + ScopedVector<UploadElementReader> element_readers; + element_readers.push_back(new UploadFileElementReader(file_path, 1, 2, time)); - UploadDataStream stream(upload_data); + UploadDataStream stream(&element_readers, 0); int error_code = stream.InitSync(); if (error_expected) ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED, error_code); @@ -466,34 +462,6 @@ TEST_F(UploadDataStreamTest, FileChanged) { true); } -TEST_F(UploadDataStreamTest, UploadDataReused) { - FilePath temp_file_path; - ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), - &temp_file_path)); - ASSERT_EQ(static_cast<int>(kTestDataSize), - file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); - - // Prepare |upload_data_| that contains a file. - upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); - - // Confirm that the file is read properly. - { - UploadDataStream stream(upload_data_); - ASSERT_EQ(OK, stream.InitSync()); - EXPECT_EQ(kTestDataSize, stream.size()); - EXPECT_EQ(kTestData, ReadFromUploadDataStream(&stream)); - } - - // Reuse |upload_data_| for another UploadDataStream, and confirm that the - // file is read properly. - { - UploadDataStream stream(upload_data_); - ASSERT_EQ(OK, stream.InitSync()); - EXPECT_EQ(kTestDataSize, stream.size()); - EXPECT_EQ(kTestData, ReadFromUploadDataStream(&stream)); - } -} - TEST_F(UploadDataStreamTest, MultipleInit) { FilePath temp_file_path; ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), @@ -502,9 +470,11 @@ TEST_F(UploadDataStreamTest, MultipleInit) { file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); // Prepare data. - upload_data_->AppendBytes(kTestData, kTestDataSize); - upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); - UploadDataStream stream(upload_data_); + element_readers_.push_back(new UploadBytesElementReader( + kTestData, kTestDataSize)); + element_readers_.push_back(new UploadFileElementReader( + temp_file_path, 0, kuint64max, base::Time())); + UploadDataStream stream(&element_readers_, 0); std::string expected_data(kTestData, kTestData + kTestDataSize); expected_data += expected_data; @@ -537,9 +507,11 @@ TEST_F(UploadDataStreamTest, MultipleInitAsync) { TestCompletionCallback test_callback; // Prepare data. - upload_data_->AppendBytes(kTestData, kTestDataSize); - upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); - UploadDataStream stream(upload_data_); + element_readers_.push_back(new UploadBytesElementReader( + kTestData, kTestDataSize)); + element_readers_.push_back(new UploadFileElementReader( + temp_file_path, 0, kuint64max, base::Time())); + UploadDataStream stream(&element_readers_, 0); std::string expected_data(kTestData, kTestData + kTestDataSize); expected_data += expected_data; @@ -573,9 +545,11 @@ TEST_F(UploadDataStreamTest, InitToReset) { file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); // Prepare data. - upload_data_->AppendBytes(kTestData, kTestDataSize); - upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); - UploadDataStream stream(upload_data_); + element_readers_.push_back(new UploadBytesElementReader( + kTestData, kTestDataSize)); + element_readers_.push_back(new UploadFileElementReader( + temp_file_path, 0, kuint64max, base::Time())); + UploadDataStream stream(&element_readers_, 0); std::vector<char> expected_data(kTestData, kTestData + kTestDataSize); expected_data.insert(expected_data.end(), expected_data.begin(), @@ -622,9 +596,11 @@ TEST_F(UploadDataStreamTest, InitDuringAsyncInit) { file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); // Prepare data. - upload_data_->AppendBytes(kTestData, kTestDataSize); - upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); - UploadDataStream stream(upload_data_); + element_readers_.push_back(new UploadBytesElementReader( + kTestData, kTestDataSize)); + element_readers_.push_back(new UploadFileElementReader( + temp_file_path, 0, kuint64max, base::Time())); + UploadDataStream stream(&element_readers_, 0); std::vector<char> expected_data(kTestData, kTestData + kTestDataSize); expected_data.insert(expected_data.end(), expected_data.begin(), @@ -663,9 +639,11 @@ TEST_F(UploadDataStreamTest, InitDuringAsyncRead) { file_util::WriteFile(temp_file_path, kTestData, kTestDataSize)); // Prepare data. - upload_data_->AppendBytes(kTestData, kTestDataSize); - upload_data_->AppendFileRange(temp_file_path, 0, kuint64max, base::Time()); - UploadDataStream stream(upload_data_); + element_readers_.push_back(new UploadBytesElementReader( + kTestData, kTestDataSize)); + element_readers_.push_back(new UploadFileElementReader( + temp_file_path, 0, kuint64max, base::Time())); + UploadDataStream stream(&element_readers_, 0); std::vector<char> expected_data(kTestData, kTestData + kTestDataSize); expected_data.insert(expected_data.end(), expected_data.begin(), diff --git a/net/base/upload_element_reader.cc b/net/base/upload_element_reader.cc index ad7ed99..1b29d59 100644 --- a/net/base/upload_element_reader.cc +++ b/net/base/upload_element_reader.cc @@ -6,32 +6,10 @@ #include "base/logging.h" #include "net/base/net_errors.h" -#include "net/base/upload_bytes_element_reader.h" #include "net/base/upload_element.h" -#include "net/base/upload_file_element_reader.h" namespace net { -// static -UploadElementReader* UploadElementReader::Create(const UploadElement& element) { - UploadElementReader* reader = NULL; - switch (element.type()) { - case UploadElement::TYPE_BYTES: - reader = new UploadBytesElementReader(element.bytes(), - element.bytes_length()); - break; - case UploadElement::TYPE_FILE: - reader = new UploadFileElementReader( - element.file_path(), - element.file_range_offset(), - element.file_range_length(), - element.expected_file_modification_time()); - break; - } - DCHECK(reader); - return reader; -} - const UploadBytesElementReader* UploadElementReader::AsBytesReader() const { return NULL; } diff --git a/net/base/upload_element_reader.h b/net/base/upload_element_reader.h index c717833..6c0d18a 100644 --- a/net/base/upload_element_reader.h +++ b/net/base/upload_element_reader.h @@ -22,9 +22,6 @@ class NET_EXPORT UploadElementReader { UploadElementReader() {} virtual ~UploadElementReader() {} - // Creates an appropriate UploadElementReader instance for the given element. - static UploadElementReader* Create(const UploadElement& element); - // Returns this instance's pointer as UploadBytesElementReader when possible, // otherwise returns NULL. virtual const UploadBytesElementReader* AsBytesReader() const; |