summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-20 11:50:24 +0000
committerhashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-12-20 11:50:24 +0000
commit4db27d802c70279a1e54f028c35eb9ff658fdd6c (patch)
treed405d57a308ac4b11760d71421ce76c9be08bd46
parent46e404d69b13063c29a67b8f0349356afbe54796 (diff)
downloadchromium_src-4db27d802c70279a1e54f028c35eb9ff658fdd6c.zip
chromium_src-4db27d802c70279a1e54f028c35eb9ff658fdd6c.tar.gz
chromium_src-4db27d802c70279a1e54f028c35eb9ff658fdd6c.tar.bz2
net: Split sync part of UploadFileElementReader as UploadFileElementReaderSync
Add UploadFileElementReaderSync and use it from Chrome Frame. Remove UploadDataStream::InitSync/ReadSync Remove UploadElementReader::InitSync/ReadSync Allow passing null callbacks for Init/Read when callers can be confident that the operation finishes synchronously. BUG=None TEST=net_unittests, chrome_frame_net_tests TBR=ananta@chromium.org for chrome_frame/urlmon_upload_data_stream.cc Review URL: https://chromiumcodereview.appspot.com/11587007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@174133 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome_frame/urlmon_upload_data_stream.cc10
-rw-r--r--net/base/upload_bytes_element_reader.cc9
-rw-r--r--net/base/upload_bytes_element_reader.h2
-rw-r--r--net/base/upload_bytes_element_reader_unittest.cc16
-rw-r--r--net/base/upload_data_stream.cc92
-rw-r--r--net/base/upload_data_stream.h12
-rw-r--r--net/base/upload_data_stream_unittest.cc127
-rw-r--r--net/base/upload_element_reader.cc14
-rw-r--r--net/base/upload_element_reader.h9
-rw-r--r--net/base/upload_file_element_reader.cc69
-rw-r--r--net/base/upload_file_element_reader.h33
-rw-r--r--net/base/upload_file_element_reader_unittest.cc273
-rw-r--r--net/http/http_stream_parser.cc4
-rw-r--r--net/http/http_stream_parser_unittest.cc14
-rw-r--r--net/quic/quic_http_stream_test.cc2
-rw-r--r--net/spdy/spdy_http_stream_spdy2_unittest.cc4
-rw-r--r--net/spdy/spdy_http_stream_spdy3_unittest.cc6
17 files changed, 383 insertions, 313 deletions
diff --git a/chrome_frame/urlmon_upload_data_stream.cc b/chrome_frame/urlmon_upload_data_stream.cc
index ede66bf..8d0a55e 100644
--- a/chrome_frame/urlmon_upload_data_stream.cc
+++ b/chrome_frame/urlmon_upload_data_stream.cc
@@ -41,7 +41,7 @@ net::UploadDataStream* CreateUploadDataStream(net::UploadData* upload_data) {
element.bytes_length());
break;
case net::UploadElement::TYPE_FILE:
- reader = new net::UploadFileElementReader(
+ reader = new net::UploadFileElementReaderSync(
element.file_path(),
element.file_range_offset(),
element.file_range_length(),
@@ -62,7 +62,7 @@ net::UploadDataStream* CreateUploadDataStream(net::UploadData* upload_data) {
void UrlmonUploadDataStream::Initialize(net::UploadData* upload_data) {
upload_data_ = upload_data;
request_body_stream_.reset(CreateUploadDataStream(upload_data));
- const int result = request_body_stream_->InitSync();
+ const int result = request_body_stream_->Init(net::CompletionCallback());
DCHECK_EQ(net::OK, result);
}
@@ -92,7 +92,9 @@ STDMETHODIMP UrlmonUploadDataStream::Read(void* pv, ULONG cb, ULONG* read) {
scoped_refptr<net::IOBufferWithSize> buf(
new net::IOBufferWithSize(bytes_to_copy_now));
- int bytes_read = request_body_stream_->ReadSync(buf, buf->size());
+ int bytes_read = request_body_stream_->Read(buf, buf->size(),
+ net::CompletionCallback());
+ DCHECK_NE(net::ERR_IO_PENDING, bytes_read);
if (bytes_read == 0) // Reached the end of the stream.
break;
@@ -121,7 +123,7 @@ STDMETHODIMP UrlmonUploadDataStream::Seek(LARGE_INTEGER move, DWORD origin,
if (origin == STREAM_SEEK_SET && move.QuadPart == 0) {
if (request_body_stream_->position() != 0) {
request_body_stream_.reset(CreateUploadDataStream(upload_data_));
- const int result = request_body_stream_->InitSync();
+ const int result = request_body_stream_->Init(net::CompletionCallback());
DCHECK_EQ(net::OK, result);
}
if (new_pos) {
diff --git a/net/base/upload_bytes_element_reader.cc b/net/base/upload_bytes_element_reader.cc
index c01a231..6927d8e 100644
--- a/net/base/upload_bytes_element_reader.cc
+++ b/net/base/upload_bytes_element_reader.cc
@@ -27,10 +27,6 @@ UploadBytesElementReader::AsBytesReader() const {
}
int UploadBytesElementReader::Init(const CompletionCallback& callback) {
- return InitSync();
-}
-
-int UploadBytesElementReader::InitSync() {
offset_ = 0;
return OK;
}
@@ -50,11 +46,6 @@ bool UploadBytesElementReader::IsInMemory() const {
int UploadBytesElementReader::Read(IOBuffer* buf,
int buf_length,
const CompletionCallback& callback) {
- DCHECK(!callback.is_null());
- return ReadSync(buf, buf_length);
-}
-
-int UploadBytesElementReader::ReadSync(IOBuffer* buf, int buf_length) {
DCHECK_LT(0, buf_length);
const size_t num_bytes_to_read =
diff --git a/net/base/upload_bytes_element_reader.h b/net/base/upload_bytes_element_reader.h
index def8c9e..9c7daf3 100644
--- a/net/base/upload_bytes_element_reader.h
+++ b/net/base/upload_bytes_element_reader.h
@@ -28,14 +28,12 @@ class NET_EXPORT UploadBytesElementReader : public UploadElementReader {
// UploadElementReader overrides:
virtual const UploadBytesElementReader* AsBytesReader() const OVERRIDE;
virtual int Init(const CompletionCallback& callback) OVERRIDE;
- virtual int InitSync() OVERRIDE;
virtual uint64 GetContentLength() const OVERRIDE;
virtual uint64 BytesRemaining() const OVERRIDE;
virtual bool IsInMemory() const OVERRIDE;
virtual int Read(IOBuffer* buf,
int buf_length,
const CompletionCallback& callback) OVERRIDE;
- virtual int ReadSync(IOBuffer* buf, int buf_length) OVERRIDE;
private:
const char* const bytes_;
diff --git a/net/base/upload_bytes_element_reader_unittest.cc b/net/base/upload_bytes_element_reader_unittest.cc
index eca720f..b5bd588 100644
--- a/net/base/upload_bytes_element_reader_unittest.cc
+++ b/net/base/upload_bytes_element_reader_unittest.cc
@@ -19,7 +19,7 @@ class UploadBytesElementReaderTest : public PlatformTest {
const char kData[] = "123abc";
bytes_.assign(kData, kData + arraysize(kData));
reader_.reset(new UploadBytesElementReader(&bytes_[0], bytes_.size()));
- ASSERT_EQ(OK, reader_->InitSync());
+ ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
EXPECT_TRUE(reader_->IsInMemory());
@@ -34,7 +34,7 @@ TEST_F(UploadBytesElementReaderTest, ReadPartially) {
std::vector<char> buf(kHalfSize);
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
EXPECT_EQ(static_cast<int>(buf.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
EXPECT_EQ(bytes_.size() - buf.size(), reader_->BytesRemaining());
bytes_.resize(kHalfSize); // Resize to compare.
EXPECT_EQ(bytes_, buf);
@@ -44,11 +44,11 @@ TEST_F(UploadBytesElementReaderTest, ReadAll) {
std::vector<char> buf(bytes_.size());
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
EXPECT_EQ(static_cast<int>(buf.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
EXPECT_EQ(0U, reader_->BytesRemaining());
EXPECT_EQ(bytes_, buf);
// Try to read again.
- EXPECT_EQ(0, reader_->ReadSync(wrapped_buffer, buf.size()));
+ EXPECT_EQ(0, reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
}
TEST_F(UploadBytesElementReaderTest, ReadTooMuch) {
@@ -56,7 +56,7 @@ TEST_F(UploadBytesElementReaderTest, ReadTooMuch) {
std::vector<char> buf(kTooLargeSize);
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
EXPECT_EQ(static_cast<int>(bytes_.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
EXPECT_EQ(0U, reader_->BytesRemaining());
buf.resize(bytes_.size()); // Resize to compare.
EXPECT_EQ(bytes_, buf);
@@ -68,18 +68,18 @@ TEST_F(UploadBytesElementReaderTest, MultipleInit) {
// Read all.
EXPECT_EQ(static_cast<int>(buf.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
EXPECT_EQ(0U, reader_->BytesRemaining());
EXPECT_EQ(bytes_, buf);
// Call Init() again to reset the state.
- ASSERT_EQ(OK, reader_->InitSync());
+ ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
// Read again.
EXPECT_EQ(static_cast<int>(buf.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
EXPECT_EQ(0U, reader_->BytesRemaining());
EXPECT_EQ(bytes_, buf);
}
diff --git a/net/base/upload_data_stream.cc b/net/base/upload_data_stream.cc
index 0da5075..25bc9bf 100644
--- a/net/base/upload_data_stream.cc
+++ b/net/base/upload_data_stream.cc
@@ -12,12 +12,17 @@
namespace net {
-bool UploadDataStream::merge_chunks_ = true;
+namespace {
+
+const bool kMergeChunksDefault = true;
+
+} // namespace
+
+bool UploadDataStream::merge_chunks_ = kMergeChunksDefault;
// static
void UploadDataStream::ResetMergeChunks() {
- // WARNING: merge_chunks_ must match the above initializer.
- merge_chunks_ = true;
+ merge_chunks_ = kMergeChunksDefault;
}
UploadDataStream::UploadDataStream(
@@ -58,43 +63,17 @@ UploadDataStream* UploadDataStream::CreateWithReader(
int UploadDataStream::Init(const CompletionCallback& callback) {
Reset();
- // Use fast path when initialization can be done synchronously.
- if (IsInMemory())
- return InitSync();
-
return InitInternal(0, callback);
}
-int UploadDataStream::InitSync() {
- Reset();
- // Initialize all readers synchronously.
- for (size_t i = 0; i < element_readers_.size(); ++i) {
- UploadElementReader* reader = element_readers_[i];
- const int result = reader->InitSync();
- if (result != OK)
- return result;
- }
-
- FinalizeInitialization();
- return OK;
-}
-
int UploadDataStream::Read(IOBuffer* buf,
int buf_len,
const CompletionCallback& callback) {
DCHECK(initialized_successfully_);
- DCHECK(!callback.is_null());
DCHECK_GT(buf_len, 0);
return ReadInternal(new DrainableIOBuffer(buf, buf_len), callback);
}
-int UploadDataStream::ReadSync(IOBuffer* buf, int buf_len) {
- DCHECK(initialized_successfully_);
- DCHECK_GT(buf_len, 0);
- return ReadInternal(new DrainableIOBuffer(buf, buf_len),
- CompletionCallback());
-}
-
bool UploadDataStream::IsEOF() const {
DCHECK(initialized_successfully_);
// Check if all elements are consumed.
@@ -133,7 +112,7 @@ void UploadDataStream::AppendChunk(const char* bytes,
// 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();
+ const int rv = reader->Init(net::CompletionCallback());
DCHECK_EQ(OK, rv);
element_readers_.push_back(reader);
@@ -168,12 +147,22 @@ int UploadDataStream::InitInternal(int start_index,
weak_ptr_factory_.GetWeakPtr(),
i + 1,
callback));
- if (result != OK)
+ if (result != OK) {
+ DCHECK(result != ERR_IO_PENDING || !callback.is_null());
return result;
+ }
}
// Finalize initialization.
- FinalizeInitialization();
+ if (!is_chunked_) {
+ uint64 total_size = 0;
+ for (size_t i = 0; i < element_readers_.size(); ++i) {
+ UploadElementReader* reader = element_readers_[i];
+ total_size += reader->GetContentLength();
+ }
+ total_size_ = total_size;
+ }
+ initialized_successfully_ = true;
return OK;
}
@@ -195,19 +184,6 @@ void UploadDataStream::ResumePendingInit(int start_index,
callback.Run(result);
}
-void UploadDataStream::FinalizeInitialization() {
- DCHECK(!initialized_successfully_);
- if (!is_chunked_) {
- uint64 total_size = 0;
- for (size_t i = 0; i < element_readers_.size(); ++i) {
- UploadElementReader* reader = element_readers_[i];
- total_size += reader->GetContentLength();
- }
- total_size_ = total_size;
- }
- initialized_successfully_ = true;
-}
-
int UploadDataStream::ReadInternal(scoped_refptr<DrainableIOBuffer> buf,
const CompletionCallback& callback) {
DCHECK(initialized_successfully_);
@@ -227,21 +203,17 @@ int UploadDataStream::ReadInternal(scoped_refptr<DrainableIOBuffer> buf,
if (!merge_chunks_ && is_chunked_ && buf->BytesConsumed())
break;
- int result = OK;
- if (!callback.is_null()) {
- result = reader->Read(
- buf,
- buf->BytesRemaining(),
- base::Bind(base::IgnoreResult(&UploadDataStream::ResumePendingRead),
- weak_ptr_factory_.GetWeakPtr(),
- buf,
- callback));
- } else {
- result = reader->ReadSync(buf, buf->BytesRemaining());
- DCHECK_NE(ERR_IO_PENDING, result);
- }
- if (result == ERR_IO_PENDING)
+ int result = reader->Read(
+ buf,
+ buf->BytesRemaining(),
+ base::Bind(base::IgnoreResult(&UploadDataStream::ResumePendingRead),
+ weak_ptr_factory_.GetWeakPtr(),
+ buf,
+ callback));
+ if (result == ERR_IO_PENDING) {
+ DCHECK(!callback.is_null());
return ERR_IO_PENDING;
+ }
DCHECK_GE(result, 0);
buf->DidConsume(result);
}
@@ -266,12 +238,12 @@ int UploadDataStream::ReadInternal(scoped_refptr<DrainableIOBuffer> buf,
void UploadDataStream::ResumePendingRead(scoped_refptr<DrainableIOBuffer> buf,
const CompletionCallback& callback,
int previous_result) {
+ DCHECK(!callback.is_null());
DCHECK_GE(previous_result, 0);
// Add the last result.
buf->DidConsume(previous_result);
- DCHECK(!callback.is_null());
const int result = ReadInternal(buf, callback);
if (result != ERR_IO_PENDING)
callback.Run(result);
diff --git a/net/base/upload_data_stream.h b/net/base/upload_data_stream.h
index 769c2d6..d4e2316 100644
--- a/net/base/upload_data_stream.h
+++ b/net/base/upload_data_stream.h
@@ -53,10 +53,6 @@ class NET_EXPORT UploadDataStream {
// files) and the target file is changed.
int Init(const CompletionCallback& callback);
- // Initializes the stream synchronously.
- // Use this method only if the thread is IO allowed or the data is in-memory.
- int InitSync();
-
// When possible, reads up to |buf_len| bytes synchronously from the upload
// data stream to |buf| and returns the number of bytes read; otherwise,
// returns ERR_IO_PENDING and calls |callback| with the number of bytes read.
@@ -69,10 +65,6 @@ class NET_EXPORT UploadDataStream {
// size() bytes can be read, which can happen for TYPE_FILE payloads.
int Read(IOBuffer* buf, int buf_len, const CompletionCallback& callback);
- // Reads data always synchronously.
- // Use this method only if the thread is IO allowed or the data is in-memory.
- int ReadSync(IOBuffer* buf, int buf_len);
-
// 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.
@@ -121,10 +113,6 @@ class NET_EXPORT UploadDataStream {
const CompletionCallback& callback,
int previous_result);
- // Finalizes the initialization process.
- // This method is used to implement Init().
- void FinalizeInitialization();
-
// Reads data from the element readers.
// This method is used to implement Read().
int ReadInternal(scoped_refptr<DrainableIOBuffer> buf,
diff --git a/net/base/upload_data_stream_unittest.cc b/net/base/upload_data_stream_unittest.cc
index ee96e3c..4f2e8b8 100644
--- a/net/base/upload_data_stream_unittest.cc
+++ b/net/base/upload_data_stream_unittest.cc
@@ -42,7 +42,10 @@ std::string ReadFromUploadDataStream(UploadDataStream* stream) {
std::string data_read;
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream->IsEOF()) {
- const int bytes_read = stream->ReadSync(buf, kTestBufferSize);
+ TestCompletionCallback callback;
+ const int result = stream->Read(buf, kTestBufferSize, callback.callback());
+ const int bytes_read =
+ result != ERR_IO_PENDING ? result : callback.WaitForResult();
data_read.append(buf->data(), bytes_read);
}
return data_read;
@@ -114,16 +117,6 @@ class MockUploadElementReader : public UploadElementReader {
int read_result_;
};
-// A mock CompletionCallback.
-class MockCompletionCallback {
- public:
- MOCK_METHOD1(Run, void(int result));
-
- CompletionCallback CreateCallback() {
- return base::Bind(&MockCompletionCallback::Run, base::Unretained(this));
- }
-};
-
} // namespace
class UploadDataStreamTest : public PlatformTest {
@@ -142,7 +135,7 @@ class UploadDataStreamTest : public PlatformTest {
TEST_F(UploadDataStreamTest, EmptyUploadData) {
UploadDataStream stream(&element_readers_, 0);
- ASSERT_EQ(OK, stream.InitSync());
+ ASSERT_EQ(OK, stream.Init(CompletionCallback()));
EXPECT_TRUE(stream.IsInMemory());
EXPECT_EQ(0U, stream.size());
EXPECT_EQ(0U, stream.position());
@@ -153,14 +146,14 @@ TEST_F(UploadDataStreamTest, ConsumeAllBytes) {
element_readers_.push_back(new UploadBytesElementReader(
kTestData, kTestDataSize));
UploadDataStream stream(&element_readers_, 0);
- ASSERT_EQ(OK, stream.InitSync());
+ ASSERT_EQ(OK, stream.Init(CompletionCallback()));
EXPECT_TRUE(stream.IsInMemory());
EXPECT_EQ(kTestDataSize, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
- int bytes_read = stream.ReadSync(buf, kTestBufferSize);
+ int bytes_read = stream.Read(buf, kTestBufferSize, CompletionCallback());
ASSERT_LE(0, bytes_read); // Not an error.
}
EXPECT_EQ(kTestDataSize, stream.position());
@@ -177,16 +170,20 @@ TEST_F(UploadDataStreamTest, File) {
element_readers_.push_back(new UploadFileElementReader(
temp_file_path, 0, kuint64max, base::Time()));
+ TestCompletionCallback init_callback;
UploadDataStream stream(&element_readers_, 0);
- ASSERT_EQ(OK, stream.InitSync());
+ ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
+ ASSERT_EQ(OK, init_callback.WaitForResult());
EXPECT_FALSE(stream.IsInMemory());
EXPECT_EQ(kTestDataSize, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
- int bytes_read = stream.ReadSync(buf, kTestBufferSize);
- ASSERT_LE(0, bytes_read); // Not an error.
+ TestCompletionCallback read_callback;
+ ASSERT_EQ(ERR_IO_PENDING,
+ stream.Read(buf, kTestBufferSize, read_callback.callback()));
+ ASSERT_LE(0, read_callback.WaitForResult()); // Not an error.
}
EXPECT_EQ(kTestDataSize, stream.position());
ASSERT_TRUE(stream.IsEOF());
@@ -206,8 +203,10 @@ TEST_F(UploadDataStreamTest, FileSmallerThanLength) {
element_readers_.push_back(new UploadFileElementReader(
temp_file_path, 0, kuint64max, base::Time()));
+ TestCompletionCallback init_callback;
UploadDataStream stream(&element_readers_, 0);
- ASSERT_EQ(OK, stream.InitSync());
+ ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
+ ASSERT_EQ(OK, init_callback.WaitForResult());
EXPECT_FALSE(stream.IsInMemory());
EXPECT_EQ(kFakeSize, stream.size());
EXPECT_EQ(0U, stream.position());
@@ -215,7 +214,10 @@ TEST_F(UploadDataStreamTest, FileSmallerThanLength) {
uint64 read_counter = 0;
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
- int bytes_read = stream.ReadSync(buf, kTestBufferSize);
+ TestCompletionCallback read_callback;
+ ASSERT_EQ(ERR_IO_PENDING,
+ stream.Read(buf, kTestBufferSize, read_callback.callback()));
+ int bytes_read = read_callback.WaitForResult();
ASSERT_LE(0, bytes_read); // Not an error.
read_counter += bytes_read;
EXPECT_EQ(read_counter, stream.position());
@@ -242,15 +244,21 @@ TEST_F(UploadDataStreamTest, FileAndBytes) {
kTestData, kTestDataSize));
const uint64 kStreamSize = kTestDataSize + kFileRangeLength;
+ TestCompletionCallback init_callback;
UploadDataStream stream(&element_readers_, 0);
- ASSERT_EQ(OK, stream.InitSync());
+ ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
+ ASSERT_EQ(OK, init_callback.WaitForResult());
EXPECT_FALSE(stream.IsInMemory());
EXPECT_EQ(kStreamSize, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
- int bytes_read = stream.ReadSync(buf, kTestBufferSize);
+ TestCompletionCallback read_callback;
+ const int result =
+ stream.Read(buf, kTestBufferSize, read_callback.callback());
+ const int bytes_read =
+ result != ERR_IO_PENDING ? result : read_callback.WaitForResult();
ASSERT_LE(0, bytes_read); // Not an error.
}
EXPECT_EQ(kStreamSize, stream.position());
@@ -263,14 +271,14 @@ TEST_F(UploadDataStreamTest, Chunk) {
stream.AppendChunk(kTestData, kTestDataSize, false);
stream.AppendChunk(kTestData, kTestDataSize, true);
- ASSERT_EQ(OK, stream.InitSync());
+ ASSERT_EQ(OK, stream.Init(CompletionCallback()));
EXPECT_FALSE(stream.IsInMemory());
EXPECT_EQ(0U, stream.size()); // Content-Length is 0 for chunked data.
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
while (!stream.IsEOF()) {
- int bytes_read = stream.ReadSync(buf, kTestBufferSize);
+ int bytes_read = stream.Read(buf, kTestBufferSize, CompletionCallback());
ASSERT_LE(0, bytes_read); // Not an error.
}
EXPECT_EQ(kStreamSize, stream.position());
@@ -305,10 +313,9 @@ TEST_F(UploadDataStreamTest, InitAsync) {
UploadDataStream stream(&element_readers_, 0);
// Run Init().
- MockCompletionCallback mock_callback;
- EXPECT_CALL(mock_callback, Run(OK)).Times(1);
- EXPECT_EQ(stream.Init(mock_callback.CreateCallback()), ERR_IO_PENDING);
- MessageLoop::current()->RunUntilIdle();
+ TestCompletionCallback callback;
+ ASSERT_EQ(ERR_IO_PENDING, stream.Init(callback.callback()));
+ EXPECT_EQ(OK, callback.WaitForResult());
}
// Init() of a reader fails asynchronously.
@@ -323,10 +330,9 @@ TEST_F(UploadDataStreamTest, InitAsyncFailureAsync) {
UploadDataStream stream(&element_readers_, 0);
// Run Init().
- MockCompletionCallback mock_callback;
- EXPECT_CALL(mock_callback, Run(ERR_FAILED)).Times(1);
- EXPECT_EQ(stream.Init(mock_callback.CreateCallback()), ERR_IO_PENDING);
- MessageLoop::current()->RunUntilIdle();
+ TestCompletionCallback callback;
+ ASSERT_EQ(ERR_IO_PENDING, stream.Init(callback.callback()));
+ EXPECT_EQ(ERR_FAILED, callback.WaitForResult());
}
// Init() of a reader fails synchronously.
@@ -345,10 +351,9 @@ TEST_F(UploadDataStreamTest, InitAsyncFailureSync) {
UploadDataStream stream(&element_readers_, 0);
// Run Init().
- MockCompletionCallback mock_callback;
- EXPECT_CALL(mock_callback, Run(ERR_FAILED)).Times(1);
- EXPECT_EQ(stream.Init(mock_callback.CreateCallback()), ERR_IO_PENDING);
- MessageLoop::current()->RunUntilIdle();
+ TestCompletionCallback callback;
+ ASSERT_EQ(ERR_IO_PENDING, stream.Init(callback.callback()));
+ EXPECT_EQ(ERR_FAILED, callback.WaitForResult());
}
// Read with a buffer whose size is same as the data.
@@ -357,17 +362,13 @@ TEST_F(UploadDataStreamTest, ReadAsyncWithExactSizeBuffer) {
kTestData, kTestDataSize));
UploadDataStream stream(&element_readers_, 0);
- MockCompletionCallback mock_callback;
- EXPECT_CALL(mock_callback, Run(_)).Times(0);
-
- ASSERT_EQ(OK, stream.Init(mock_callback.CreateCallback()));
+ ASSERT_EQ(OK, stream.Init(CompletionCallback()));
EXPECT_TRUE(stream.IsInMemory());
EXPECT_EQ(kTestDataSize, stream.size());
EXPECT_EQ(0U, stream.position());
EXPECT_FALSE(stream.IsEOF());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestDataSize);
- int bytes_read = stream.Read(buf, kTestDataSize,
- mock_callback.CreateCallback());
+ int bytes_read = stream.Read(buf, kTestDataSize, CompletionCallback());
ASSERT_EQ(static_cast<int>(kTestDataSize), bytes_read); // Not an error.
EXPECT_EQ(kTestDataSize, stream.position());
ASSERT_TRUE(stream.IsEOF());
@@ -401,30 +402,30 @@ TEST_F(UploadDataStreamTest, ReadAsync) {
UploadDataStream stream(&element_readers_, 0);
// Run Init().
- MockCompletionCallback mock_callback;
- EXPECT_CALL(mock_callback, Run(OK)).Times(1);
- EXPECT_EQ(ERR_IO_PENDING, stream.Init(mock_callback.CreateCallback()));
- MessageLoop::current()->RunUntilIdle();
+ TestCompletionCallback init_callback;
+ EXPECT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
+ EXPECT_EQ(OK, init_callback.WaitForResult());
scoped_refptr<IOBuffer> buf = new IOBuffer(kTestBufferSize);
// Consume the first element.
- EXPECT_CALL(mock_callback, Run(kTestDataSize)).Times(0);
+ TestCompletionCallback read_callback1;
EXPECT_EQ(static_cast<int>(kTestDataSize),
- stream.Read(buf, kTestDataSize, mock_callback.CreateCallback()));
+ stream.Read(buf, kTestDataSize, read_callback1.callback()));
MessageLoop::current()->RunUntilIdle();
+ EXPECT_FALSE(read_callback1.have_result());
// Consume the second element.
- EXPECT_CALL(mock_callback, Run(kTestDataSize)).Times(1);
- EXPECT_EQ(ERR_IO_PENDING,
- stream.Read(buf, kTestDataSize, mock_callback.CreateCallback()));
- MessageLoop::current()->RunUntilIdle();
+ TestCompletionCallback read_callback2;
+ ASSERT_EQ(ERR_IO_PENDING,
+ stream.Read(buf, kTestDataSize, read_callback2.callback()));
+ EXPECT_EQ(static_cast<int>(kTestDataSize), read_callback2.WaitForResult());
// Consume the third and the fourth elements.
- EXPECT_CALL(mock_callback, Run(kTestDataSize*2)).Times(1);
- EXPECT_EQ(ERR_IO_PENDING,
- stream.Read(buf, kTestDataSize*2, mock_callback.CreateCallback()));
- MessageLoop::current()->RunUntilIdle();
+ TestCompletionCallback read_callback3;
+ ASSERT_EQ(ERR_IO_PENDING,
+ stream.Read(buf, kTestDataSize*2, read_callback3.callback()));
+ EXPECT_EQ(static_cast<int>(kTestDataSize*2), read_callback3.WaitForResult());
}
void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path,
@@ -435,8 +436,10 @@ void UploadDataStreamTest::FileChangedHelper(const FilePath& file_path,
ScopedVector<UploadElementReader> element_readers;
element_readers.push_back(new UploadFileElementReader(file_path, 1, 2, time));
+ TestCompletionCallback init_callback;
UploadDataStream stream(&element_readers, 0);
- int error_code = stream.InitSync();
+ ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback.callback()));
+ int error_code = init_callback.WaitForResult();
if (error_expected)
ASSERT_EQ(ERR_UPLOAD_FILE_CHANGED, error_code);
else
@@ -479,8 +482,10 @@ TEST_F(UploadDataStreamTest, MultipleInit) {
std::string expected_data(kTestData, kTestData + kTestDataSize);
expected_data += expected_data;
- // Call InitSync().
- ASSERT_EQ(OK, stream.InitSync());
+ // Call Init().
+ TestCompletionCallback init_callback1;
+ ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback1.callback()));
+ ASSERT_EQ(OK, init_callback1.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
@@ -488,8 +493,10 @@ TEST_F(UploadDataStreamTest, MultipleInit) {
EXPECT_EQ(expected_data, ReadFromUploadDataStream(&stream));
EXPECT_TRUE(stream.IsEOF());
- // Call InitSync() again to reset.
- ASSERT_EQ(OK, stream.InitSync());
+ // Call Init() again to reset.
+ TestCompletionCallback init_callback2;
+ ASSERT_EQ(ERR_IO_PENDING, stream.Init(init_callback2.callback()));
+ ASSERT_EQ(OK, init_callback2.WaitForResult());
EXPECT_FALSE(stream.IsEOF());
EXPECT_EQ(kTestDataSize*2, stream.size());
diff --git a/net/base/upload_element_reader.cc b/net/base/upload_element_reader.cc
index 1b29d59..f44931c 100644
--- a/net/base/upload_element_reader.cc
+++ b/net/base/upload_element_reader.cc
@@ -4,10 +4,6 @@
#include "net/base/upload_element_reader.h"
-#include "base/logging.h"
-#include "net/base/net_errors.h"
-#include "net/base/upload_element.h"
-
namespace net {
const UploadBytesElementReader* UploadElementReader::AsBytesReader() const {
@@ -18,18 +14,8 @@ const UploadFileElementReader* UploadElementReader::AsFileReader() const {
return NULL;
}
-int UploadElementReader::InitSync() {
- NOTREACHED() << "This instance does not support InitSync().";
- return ERR_NOT_IMPLEMENTED;
-}
-
bool UploadElementReader::IsInMemory() const {
return false;
}
-int UploadElementReader::ReadSync(IOBuffer* buf, int buf_length) {
- NOTREACHED() << "This instance does not support ReadSync().";
- return ERR_NOT_IMPLEMENTED;
-}
-
} // namespace net
diff --git a/net/base/upload_element_reader.h b/net/base/upload_element_reader.h
index 6c0d18a..01f91fb 100644
--- a/net/base/upload_element_reader.h
+++ b/net/base/upload_element_reader.h
@@ -13,7 +13,6 @@ namespace net {
class IOBuffer;
class UploadBytesElementReader;
-class UploadElement;
class UploadFileElementReader;
// An interface to read an upload data element.
@@ -36,10 +35,6 @@ class NET_EXPORT UploadElementReader {
// state.
virtual int Init(const CompletionCallback& callback) = 0;
- // Initializes the instance always synchronously.
- // Use this method only if the thread is IO allowed or the data is in-memory.
- virtual int InitSync();
-
// Returns the byte-length of the element. For files that do not exist, 0
// is returned. This is done for consistency with Mozilla.
virtual uint64 GetContentLength() const = 0;
@@ -60,10 +55,6 @@ class NET_EXPORT UploadElementReader {
int buf_length,
const CompletionCallback& callback) = 0;
- // Reads the data always synchronously.
- // Use this method only if the thread is IO allowed or the data is in-memory.
- virtual int ReadSync(IOBuffer* buf, int buf_length);
-
private:
DISALLOW_COPY_AND_ASSIGN(UploadElementReader);
};
diff --git a/net/base/upload_file_element_reader.cc b/net/base/upload_file_element_reader.cc
index c5abf53..8152d27 100644
--- a/net/base/upload_file_element_reader.cc
+++ b/net/base/upload_file_element_reader.cc
@@ -131,6 +131,7 @@ const UploadFileElementReader* UploadFileElementReader::AsFileReader() const {
}
int UploadFileElementReader::Init(const CompletionCallback& callback) {
+ DCHECK(!callback.is_null());
Reset();
ScopedFileStreamPtr* file_stream = new ScopedFileStreamPtr;
@@ -154,18 +155,6 @@ int UploadFileElementReader::Init(const CompletionCallback& callback) {
return ERR_IO_PENDING;
}
-int UploadFileElementReader::InitSync() {
- Reset();
-
- ScopedFileStreamPtr file_stream;
- uint64 content_length = 0;
- const int result = InitInternal(path_, range_offset_, range_length_,
- expected_modification_time_,
- &file_stream, &content_length);
- OnInitCompleted(&file_stream, &content_length, CompletionCallback(), result);
- return result;
-}
-
uint64 UploadFileElementReader::GetContentLength() const {
if (overriding_content_length)
return overriding_content_length;
@@ -204,13 +193,6 @@ int UploadFileElementReader::Read(IOBuffer* buf,
return ERR_IO_PENDING;
}
-int UploadFileElementReader::ReadSync(IOBuffer* buf, int buf_length) {
- const int result = ReadInternal(buf, buf_length, BytesRemaining(),
- file_stream_.get());
- OnReadCompleted(file_stream_.Pass(), CompletionCallback(), result);
- return result;
-}
-
void UploadFileElementReader::Reset() {
weak_ptr_factory_.InvalidateWeakPtrs();
bytes_remaining_ = 0;
@@ -251,4 +233,53 @@ UploadFileElementReader::ScopedOverridingContentLengthForTests::
overriding_content_length = 0;
}
+UploadFileElementReaderSync::UploadFileElementReaderSync(
+ const 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();
+
+ UploadFileElementReader::ScopedFileStreamPtr file_stream;
+
+ const int result = InitInternal(path_, range_offset_, range_length_,
+ expected_modification_time_,
+ &file_stream, &content_length_);
+ file_stream_.reset(file_stream.release());
+ 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());
+ DCHECK_GE(static_cast<int>(bytes_remaining_), result);
+ bytes_remaining_ -= result;
+ return result;
+}
+
} // namespace net
diff --git a/net/base/upload_file_element_reader.h b/net/base/upload_file_element_reader.h
index b5c4004..f85d45f 100644
--- a/net/base/upload_file_element_reader.h
+++ b/net/base/upload_file_element_reader.h
@@ -45,13 +45,11 @@ class NET_EXPORT UploadFileElementReader : public UploadElementReader {
// UploadElementReader overrides:
virtual const UploadFileElementReader* AsFileReader() const OVERRIDE;
virtual int Init(const CompletionCallback& callback) OVERRIDE;
- virtual int InitSync() OVERRIDE;
virtual uint64 GetContentLength() const OVERRIDE;
virtual uint64 BytesRemaining() const OVERRIDE;
virtual int Read(IOBuffer* buf,
int buf_length,
const CompletionCallback& callback) OVERRIDE;
- virtual int ReadSync(IOBuffer* buf, int buf_length) OVERRIDE;
private:
FRIEND_TEST_ALL_PREFIXES(UploadDataStreamTest, FileSmallerThanLength);
@@ -95,6 +93,37 @@ class NET_EXPORT UploadFileElementReader : public UploadElementReader {
DISALLOW_COPY_AND_ASSIGN(UploadFileElementReader);
};
+// An UploadElementReader implementation for file which performs file operation
+// synchronously.
+// Use this class only if the thread is IO allowed.
+class NET_EXPORT UploadFileElementReaderSync : public UploadElementReader {
+ public:
+ UploadFileElementReaderSync(const FilePath& path,
+ uint64 range_offset,
+ uint64 range_length,
+ const base::Time& expected_modification_time);
+ virtual ~UploadFileElementReaderSync();
+
+ // UploadElementReader overrides:
+ virtual int Init(const CompletionCallback& callback) OVERRIDE;
+ virtual uint64 GetContentLength() const OVERRIDE;
+ virtual uint64 BytesRemaining() const OVERRIDE;
+ virtual int Read(IOBuffer* buf,
+ int buf_length,
+ const CompletionCallback& callback) OVERRIDE;
+
+ private:
+ const FilePath path_;
+ const uint64 range_offset_;
+ const uint64 range_length_;
+ const base::Time expected_modification_time_;
+ scoped_ptr<FileStream> file_stream_;
+ uint64 content_length_;
+ uint64 bytes_remaining_;
+
+ DISALLOW_COPY_AND_ASSIGN(UploadFileElementReaderSync);
+};
+
} // namespace net
#endif // NET_BASE_UPLOAD_FILE_ELEMENT_READER_H_
diff --git a/net/base/upload_file_element_reader_unittest.cc b/net/base/upload_file_element_reader_unittest.cc
index f43aad1..4915635 100644
--- a/net/base/upload_file_element_reader_unittest.cc
+++ b/net/base/upload_file_element_reader_unittest.cc
@@ -31,7 +31,9 @@ class UploadFileElementReaderTest : public PlatformTest {
reader_.reset(new UploadFileElementReader(
temp_file_path_, 0, kuint64max, base::Time()));
- ASSERT_EQ(OK, reader_->InitSync());
+ TestCompletionCallback callback;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Init(callback.callback()));
+ EXPECT_EQ(OK, callback.WaitForResult());
EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
EXPECT_FALSE(reader_->IsInMemory());
@@ -48,13 +50,17 @@ TEST_F(UploadFileElementReaderTest, ReadPartially) {
ASSERT_EQ(bytes_.size(), kHalfSize * 2);
std::vector<char> buf(kHalfSize);
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
- EXPECT_EQ(static_cast<int>(buf.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
+ TestCompletionCallback read_callback1;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, buf.size(),
+ read_callback1.callback()));
+ EXPECT_EQ(static_cast<int>(buf.size()), read_callback1.WaitForResult());
EXPECT_EQ(bytes_.size() - buf.size(), reader_->BytesRemaining());
EXPECT_EQ(std::vector<char>(bytes_.begin(), bytes_.begin() + kHalfSize), buf);
- EXPECT_EQ(static_cast<int>(buf.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
+ TestCompletionCallback read_callback2;
+ EXPECT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, buf.size(),
+ read_callback2.callback()));
+ EXPECT_EQ(static_cast<int>(buf.size()), read_callback2.WaitForResult());
EXPECT_EQ(0U, reader_->BytesRemaining());
EXPECT_EQ(std::vector<char>(bytes_.begin() + kHalfSize, bytes_.end()), buf);
}
@@ -62,20 +68,25 @@ TEST_F(UploadFileElementReaderTest, ReadPartially) {
TEST_F(UploadFileElementReaderTest, ReadAll) {
std::vector<char> buf(bytes_.size());
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
- EXPECT_EQ(static_cast<int>(buf.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
+ TestCompletionCallback read_callback;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, buf.size(),
+ read_callback.callback()));
+ EXPECT_EQ(static_cast<int>(buf.size()), read_callback.WaitForResult());
EXPECT_EQ(0U, reader_->BytesRemaining());
EXPECT_EQ(bytes_, buf);
// Try to read again.
- EXPECT_EQ(0, reader_->ReadSync(wrapped_buffer, buf.size()));
+ EXPECT_EQ(0, reader_->Read(wrapped_buffer, buf.size(),
+ read_callback.callback()));
}
TEST_F(UploadFileElementReaderTest, ReadTooMuch) {
const size_t kTooLargeSize = bytes_.size() * 2;
std::vector<char> buf(kTooLargeSize);
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
- EXPECT_EQ(static_cast<int>(bytes_.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
+ TestCompletionCallback read_callback;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, buf.size(),
+ read_callback.callback()));
+ EXPECT_EQ(static_cast<int>(bytes_.size()), read_callback.WaitForResult());
EXPECT_EQ(0U, reader_->BytesRemaining());
buf.resize(bytes_.size()); // Resize to compare.
EXPECT_EQ(bytes_, buf);
@@ -86,99 +97,25 @@ TEST_F(UploadFileElementReaderTest, MultipleInit) {
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
// Read all.
- EXPECT_EQ(static_cast<int>(buf.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
- EXPECT_EQ(0U, reader_->BytesRemaining());
- EXPECT_EQ(bytes_, buf);
-
- // Call InitSync() again to reset the state.
- ASSERT_EQ(OK, reader_->InitSync());
- EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
- EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
-
- // Read again.
- EXPECT_EQ(static_cast<int>(buf.size()),
- reader_->ReadSync(wrapped_buffer, buf.size()));
- EXPECT_EQ(0U, reader_->BytesRemaining());
- EXPECT_EQ(bytes_, buf);
-}
-
-TEST_F(UploadFileElementReaderTest, ReadPartiallyAsync) {
- const size_t kHalfSize = bytes_.size() / 2;
- ASSERT_EQ(bytes_.size(), kHalfSize * 2);
- std::vector<char> buf(kHalfSize);
- scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
- TestCompletionCallback test_callback;
- EXPECT_EQ(ERR_IO_PENDING,
- reader_->Read(wrapped_buffer, buf.size(),
- test_callback.callback()));
-
- EXPECT_EQ(static_cast<int>(buf.size()), test_callback.WaitForResult());
- EXPECT_EQ(bytes_.size() - buf.size(), reader_->BytesRemaining());
- EXPECT_EQ(std::vector<char>(bytes_.begin(), bytes_.begin() + kHalfSize), buf);
-
- EXPECT_EQ(ERR_IO_PENDING,
- reader_->Read(wrapped_buffer, buf.size(),
- test_callback.callback()));
-
- EXPECT_EQ(static_cast<int>(buf.size()), test_callback.WaitForResult());
- EXPECT_EQ(0U, reader_->BytesRemaining());
- EXPECT_EQ(std::vector<char>(bytes_.begin() + kHalfSize, bytes_.end()), buf);
-}
-
-TEST_F(UploadFileElementReaderTest, ReadAllAsync) {
- std::vector<char> buf(bytes_.size());
- scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
- TestCompletionCallback test_callback;
- EXPECT_EQ(ERR_IO_PENDING,
- reader_->Read(wrapped_buffer, buf.size(),
- test_callback.callback()));
-
- EXPECT_EQ(static_cast<int>(buf.size()), test_callback.WaitForResult());
- EXPECT_EQ(0U, reader_->BytesRemaining());
- EXPECT_EQ(bytes_, buf);
- // Try to read again.
- EXPECT_EQ(0, reader_->ReadSync(wrapped_buffer, buf.size()));
-}
-
-TEST_F(UploadFileElementReaderTest, ReadTooMuchAsync) {
- const size_t kTooLargeSize = bytes_.size() * 2;
- std::vector<char> buf(kTooLargeSize);
- scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
- TestCompletionCallback test_callback;
- EXPECT_EQ(ERR_IO_PENDING,
- reader_->Read(wrapped_buffer, buf.size(),
- test_callback.callback()));
-
- EXPECT_EQ(static_cast<int>(bytes_.size()), test_callback.WaitForResult());
- EXPECT_EQ(0U, reader_->BytesRemaining());
- buf.resize(bytes_.size()); // Resize to compare.
- EXPECT_EQ(bytes_, buf);
-}
-
-TEST_F(UploadFileElementReaderTest, MultipleInitAsync) {
- std::vector<char> buf(bytes_.size());
- scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
- TestCompletionCallback test_callback;
-
- // Read all.
- EXPECT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, buf.size(),
- test_callback.callback()));
- EXPECT_EQ(static_cast<int>(buf.size()), test_callback.WaitForResult());
+ TestCompletionCallback read_callback1;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, buf.size(),
+ read_callback1.callback()));
+ EXPECT_EQ(static_cast<int>(buf.size()), read_callback1.WaitForResult());
EXPECT_EQ(0U, reader_->BytesRemaining());
EXPECT_EQ(bytes_, buf);
// Call Init() again to reset the state.
- EXPECT_EQ(ERR_IO_PENDING, reader_->Init(test_callback.callback()));
- EXPECT_EQ(OK, test_callback.WaitForResult());
+ TestCompletionCallback init_callback;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback()));
+ EXPECT_EQ(OK, init_callback.WaitForResult());
EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
// Read again.
- EXPECT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, buf.size(),
- test_callback.callback()));
-
- EXPECT_EQ(static_cast<int>(buf.size()), test_callback.WaitForResult());
+ TestCompletionCallback read_callback2;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, buf.size(),
+ read_callback2.callback()));
+ EXPECT_EQ(static_cast<int>(buf.size()), read_callback2.WaitForResult());
EXPECT_EQ(0U, reader_->BytesRemaining());
EXPECT_EQ(bytes_, buf);
}
@@ -224,13 +161,17 @@ TEST_F(UploadFileElementReaderTest, Range) {
const uint64 kLength = bytes_.size() - kOffset * 3;
reader_.reset(new UploadFileElementReader(
temp_file_path_, kOffset, kLength, base::Time()));
- ASSERT_EQ(OK, reader_->InitSync());
+ TestCompletionCallback init_callback;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback()));
+ EXPECT_EQ(OK, init_callback.WaitForResult());
EXPECT_EQ(kLength, reader_->GetContentLength());
EXPECT_EQ(kLength, reader_->BytesRemaining());
std::vector<char> buf(kLength);
scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
- EXPECT_EQ(static_cast<int>(kLength),
- reader_->ReadSync(wrapped_buffer, kLength));
+ TestCompletionCallback read_callback;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Read(wrapped_buffer, kLength,
+ read_callback.callback()));
+ EXPECT_EQ(static_cast<int>(kLength), read_callback.WaitForResult());
const std::vector<char> expected(bytes_.begin() + kOffset,
bytes_.begin() + kOffset + kLength);
EXPECT_EQ(expected, buf);
@@ -245,14 +186,146 @@ TEST_F(UploadFileElementReaderTest, FileChanged) {
info.last_modified - base::TimeDelta::FromSeconds(1);
reader_.reset(new UploadFileElementReader(
temp_file_path_, 0, kuint64max, expected_modification_time));
- EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, reader_->InitSync());
+ TestCompletionCallback init_callback;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback()));
+ EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, init_callback.WaitForResult());
}
TEST_F(UploadFileElementReaderTest, WrongPath) {
const FilePath wrong_path(FILE_PATH_LITERAL("wrong_path"));
reader_.reset(new UploadFileElementReader(
wrong_path, 0, kuint64max, base::Time()));
- ASSERT_EQ(OK, reader_->InitSync());
+ TestCompletionCallback init_callback;
+ ASSERT_EQ(ERR_IO_PENDING, reader_->Init(init_callback.callback()));
+ EXPECT_EQ(OK, init_callback.WaitForResult());
+ EXPECT_EQ(0U, reader_->GetContentLength());
+ EXPECT_EQ(0U, reader_->BytesRemaining());
+}
+
+
+class UploadFileElementReaderSyncTest : public PlatformTest {
+ protected:
+ virtual void SetUp() OVERRIDE {
+ // Some tests (*.ReadPartially) rely on bytes_.size() being even.
+ const char kData[] = "123456789abcdefghi";
+ bytes_.assign(kData, kData + arraysize(kData) - 1);
+
+ ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
+
+ ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(),
+ &temp_file_path_));
+ ASSERT_EQ(
+ static_cast<int>(bytes_.size()),
+ file_util::WriteFile(temp_file_path_, &bytes_[0], bytes_.size()));
+
+ reader_.reset(new UploadFileElementReaderSync(
+ temp_file_path_, 0, kuint64max, base::Time()));
+ ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
+ EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
+ EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
+ EXPECT_FALSE(reader_->IsInMemory());
+ }
+
+ std::vector<char> bytes_;
+ scoped_ptr<UploadElementReader> reader_;
+ base::ScopedTempDir temp_dir_;
+ FilePath temp_file_path_;
+};
+
+TEST_F(UploadFileElementReaderSyncTest, ReadPartially) {
+ const size_t kHalfSize = bytes_.size() / 2;
+ ASSERT_EQ(bytes_.size(), kHalfSize * 2);
+ std::vector<char> buf(kHalfSize);
+ scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
+ EXPECT_EQ(static_cast<int>(buf.size()),
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
+ EXPECT_EQ(bytes_.size() - buf.size(), reader_->BytesRemaining());
+ EXPECT_EQ(std::vector<char>(bytes_.begin(), bytes_.begin() + kHalfSize), buf);
+
+ EXPECT_EQ(static_cast<int>(buf.size()),
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
+ EXPECT_EQ(0U, reader_->BytesRemaining());
+ EXPECT_EQ(std::vector<char>(bytes_.begin() + kHalfSize, bytes_.end()), buf);
+}
+
+TEST_F(UploadFileElementReaderSyncTest, ReadAll) {
+ std::vector<char> buf(bytes_.size());
+ scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
+ EXPECT_EQ(static_cast<int>(buf.size()),
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
+ EXPECT_EQ(0U, reader_->BytesRemaining());
+ EXPECT_EQ(bytes_, buf);
+ // Try to read again.
+ EXPECT_EQ(0, reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
+}
+
+TEST_F(UploadFileElementReaderSyncTest, ReadTooMuch) {
+ const size_t kTooLargeSize = bytes_.size() * 2;
+ std::vector<char> buf(kTooLargeSize);
+ scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
+ EXPECT_EQ(static_cast<int>(bytes_.size()),
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
+ EXPECT_EQ(0U, reader_->BytesRemaining());
+ buf.resize(bytes_.size()); // Resize to compare.
+ EXPECT_EQ(bytes_, buf);
+}
+
+TEST_F(UploadFileElementReaderSyncTest, MultipleInit) {
+ std::vector<char> buf(bytes_.size());
+ scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
+
+ // Read all.
+ EXPECT_EQ(static_cast<int>(buf.size()),
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
+ EXPECT_EQ(0U, reader_->BytesRemaining());
+ EXPECT_EQ(bytes_, buf);
+
+ // Call Init() again to reset the state.
+ ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
+ EXPECT_EQ(bytes_.size(), reader_->GetContentLength());
+ EXPECT_EQ(bytes_.size(), reader_->BytesRemaining());
+
+ // Read again.
+ EXPECT_EQ(static_cast<int>(buf.size()),
+ reader_->Read(wrapped_buffer, buf.size(), CompletionCallback()));
+ EXPECT_EQ(0U, reader_->BytesRemaining());
+ EXPECT_EQ(bytes_, buf);
+}
+
+TEST_F(UploadFileElementReaderSyncTest, Range) {
+ const uint64 kOffset = 2;
+ const uint64 kLength = bytes_.size() - kOffset * 3;
+ reader_.reset(new UploadFileElementReaderSync(
+ temp_file_path_, kOffset, kLength, base::Time()));
+ ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
+ EXPECT_EQ(kLength, reader_->GetContentLength());
+ EXPECT_EQ(kLength, reader_->BytesRemaining());
+ std::vector<char> buf(kLength);
+ scoped_refptr<IOBuffer> wrapped_buffer = new WrappedIOBuffer(&buf[0]);
+ EXPECT_EQ(static_cast<int>(kLength),
+ reader_->Read(wrapped_buffer, kLength, CompletionCallback()));
+ const std::vector<char> expected(bytes_.begin() + kOffset,
+ bytes_.begin() + kOffset + kLength);
+ EXPECT_EQ(expected, buf);
+}
+
+TEST_F(UploadFileElementReaderSyncTest, FileChanged) {
+ base::PlatformFileInfo info;
+ ASSERT_TRUE(file_util::GetFileInfo(temp_file_path_, &info));
+
+ // Expect one second before the actual modification time to simulate change.
+ const base::Time expected_modification_time =
+ info.last_modified - base::TimeDelta::FromSeconds(1);
+ reader_.reset(new UploadFileElementReaderSync(
+ temp_file_path_, 0, kuint64max, expected_modification_time));
+ EXPECT_EQ(ERR_UPLOAD_FILE_CHANGED, reader_->Init(CompletionCallback()));
+}
+
+TEST_F(UploadFileElementReaderSyncTest, WrongPath) {
+ const FilePath wrong_path(FILE_PATH_LITERAL("wrong_path"));
+ reader_.reset(new UploadFileElementReaderSync(
+ wrong_path, 0, kuint64max, base::Time()));
+ ASSERT_EQ(OK, reader_->Init(CompletionCallback()));
EXPECT_EQ(0U, reader_->GetContentLength());
EXPECT_EQ(0U, reader_->BytesRemaining());
}
diff --git a/net/http/http_stream_parser.cc b/net/http/http_stream_parser.cc
index 95e2ba6..d08d62db 100644
--- a/net/http/http_stream_parser.cc
+++ b/net/http/http_stream_parser.cc
@@ -255,8 +255,8 @@ int HttpStreamParser::SendRequest(const std::string& request_line,
size_t todo = request_->upload_data_stream->size();
while (todo) {
- int consumed = request_->upload_data_stream->ReadSync(request_headers_,
- todo);
+ int consumed = request_->upload_data_stream->Read(request_headers_, todo,
+ CompletionCallback());
DCHECK_GT(consumed, 0); // Read() won't fail if not chunked.
request_headers_->DidConsume(consumed);
todo -= consumed;
diff --git a/net/http/http_stream_parser_unittest.cc b/net/http/http_stream_parser_unittest.cc
index b62f539..f7e69a9 100644
--- a/net/http/http_stream_parser_unittest.cc
+++ b/net/http/http_stream_parser_unittest.cc
@@ -98,7 +98,7 @@ TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_NoBody) {
TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_EmptyBody) {
ScopedVector<UploadElementReader> element_readers;
scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
- ASSERT_EQ(OK, body->InitSync());
+ ASSERT_EQ(OK, body->Init(CompletionCallback()));
// Shouldn't be merged if upload data is empty.
ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
"some header", body.get()));
@@ -109,7 +109,7 @@ TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_ChunkedBody) {
scoped_ptr<UploadDataStream> body(
new UploadDataStream(UploadDataStream::CHUNKED, 0));
body->AppendChunk(payload.data(), payload.size(), true);
- ASSERT_EQ(OK, body->InitSync());
+ ASSERT_EQ(OK, body->Init(CompletionCallback()));
// Shouldn't be merged if upload data carries chunked data.
ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
"some header", body.get()));
@@ -129,7 +129,9 @@ TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_FileBody) {
temp_file_path, 0, 0, base::Time()));
scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
- ASSERT_EQ(OK, body->InitSync());
+ TestCompletionCallback callback;
+ ASSERT_EQ(ERR_IO_PENDING, body->Init(callback.callback()));
+ ASSERT_EQ(OK, callback.WaitForResult());
// Shouldn't be merged if upload data carries a file, as it's not in-memory.
ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
"some header", body.get()));
@@ -142,7 +144,7 @@ TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_SmallBodyInMemory) {
payload.data(), payload.size()));
scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
- ASSERT_EQ(OK, body->InitSync());
+ ASSERT_EQ(OK, body->Init(CompletionCallback()));
// Yes, should be merged if the in-memory body is small here.
ASSERT_TRUE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
"some header", body.get()));
@@ -155,7 +157,7 @@ TEST(HttpStreamParser, ShouldMergeRequestHeadersAndBody_LargeBodyInMemory) {
payload.data(), payload.size()));
scoped_ptr<UploadDataStream> body(new UploadDataStream(&element_readers, 0));
- ASSERT_EQ(OK, body->InitSync());
+ ASSERT_EQ(OK, body->Init(CompletionCallback()));
// Shouldn't be merged if the in-memory body is large here.
ASSERT_FALSE(HttpStreamParser::ShouldMergeRequestHeadersAndBody(
"some header", body.get()));
@@ -198,7 +200,7 @@ TEST(HttpStreamParser, AsyncChunkAndAsyncSocket) {
UploadDataStream upload_stream(UploadDataStream::CHUNKED, 0);
upload_stream.AppendChunk(kChunk1, arraysize(kChunk1) - 1, false);
- ASSERT_EQ(OK, upload_stream.InitSync());
+ ASSERT_EQ(OK, upload_stream.Init(CompletionCallback()));
DeterministicSocketData data(reads, arraysize(reads),
writes, arraysize(writes));
diff --git a/net/quic/quic_http_stream_test.cc b/net/quic/quic_http_stream_test.cc
index 517342e..898339d 100644
--- a/net/quic/quic_http_stream_test.cc
+++ b/net/quic/quic_http_stream_test.cc
@@ -360,7 +360,7 @@ TEST_F(QuicHttpStreamTest, SendPostRequest) {
request_.method = "POST";
request_.url = GURL("http://www.google.com/");
request_.upload_data_stream = &upload_data_stream;
- ASSERT_EQ(OK, request_.upload_data_stream->InitSync());
+ ASSERT_EQ(OK, request_.upload_data_stream->Init(CompletionCallback()));
EXPECT_EQ(OK, stream_->InitializeStream(&request_, net_log_,
callback_.callback()));
diff --git a/net/spdy/spdy_http_stream_spdy2_unittest.cc b/net/spdy/spdy_http_stream_spdy2_unittest.cc
index 544f686..88e2fa9 100644
--- a/net/spdy/spdy_http_stream_spdy2_unittest.cc
+++ b/net/spdy/spdy_http_stream_spdy2_unittest.cc
@@ -150,7 +150,7 @@ TEST_F(SpdyHttpStreamSpdy2Test, SendChunkedPost) {
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &upload_stream;
- ASSERT_EQ(OK, upload_stream.InitSync());
+ ASSERT_EQ(OK, upload_stream.Init(CompletionCallback()));
TestCompletionCallback callback;
HttpResponseInfo response;
@@ -246,7 +246,7 @@ TEST_F(SpdyHttpStreamSpdy2Test, DelayedSendChunkedPost) {
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &upload_stream;
- ASSERT_EQ(OK, upload_stream.InitSync());
+ ASSERT_EQ(OK, upload_stream.Init(CompletionCallback()));
upload_stream.AppendChunk(kUploadData, kUploadDataSize, false);
BoundNetLog net_log;
diff --git a/net/spdy/spdy_http_stream_spdy3_unittest.cc b/net/spdy/spdy_http_stream_spdy3_unittest.cc
index 70c406d..f7efe26 100644
--- a/net/spdy/spdy_http_stream_spdy3_unittest.cc
+++ b/net/spdy/spdy_http_stream_spdy3_unittest.cc
@@ -160,7 +160,7 @@ TEST_F(SpdyHttpStreamSpdy3Test, SendChunkedPost) {
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &upload_stream;
- ASSERT_EQ(OK, upload_stream.InitSync());
+ ASSERT_EQ(OK, upload_stream.Init(CompletionCallback()));
TestCompletionCallback callback;
HttpResponseInfo response;
@@ -256,7 +256,7 @@ TEST_F(SpdyHttpStreamSpdy3Test, DelayedSendChunkedPost) {
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &upload_stream;
- ASSERT_EQ(OK, upload_stream.InitSync());
+ ASSERT_EQ(OK, upload_stream.Init(CompletionCallback()));
upload_stream.AppendChunk(kUploadData, kUploadDataSize, false);
BoundNetLog net_log;
@@ -386,7 +386,7 @@ TEST_F(SpdyHttpStreamSpdy3Test, DelayedSendChunkedPostWithWindowUpdate) {
request.url = GURL("http://www.google.com/");
request.upload_data_stream = &upload_stream;
- ASSERT_EQ(OK, upload_stream.InitSync());
+ ASSERT_EQ(OK, upload_stream.Init(CompletionCallback()));
upload_stream.AppendChunk(kUploadData, kUploadDataSize, true);
BoundNetLog net_log;