summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--content/browser/byte_stream.cc7
-rw-r--r--content/browser/byte_stream.h6
-rw-r--r--content/browser/byte_stream_unittest.cc39
3 files changed, 52 insertions, 0 deletions
diff --git a/content/browser/byte_stream.cc b/content/browser/byte_stream.cc
index f83e8c3..8031815 100644
--- a/content/browser/byte_stream.cc
+++ b/content/browser/byte_stream.cc
@@ -55,6 +55,7 @@ class ByteStreamWriterImpl : public ByteStreamWriter {
// Overridden from ByteStreamWriter.
virtual bool Write(scoped_refptr<net::IOBuffer> buffer,
size_t byte_count) OVERRIDE;
+ virtual void Flush() OVERRIDE;
virtual void Close(DownloadInterruptReason status) OVERRIDE;
virtual void RegisterCallback(const base::Closure& source_callback) OVERRIDE;
@@ -215,6 +216,12 @@ bool ByteStreamWriterImpl::Write(
return (input_contents_size_ + output_size_used_ <= total_buffer_size_);
}
+void ByteStreamWriterImpl::Flush() {
+ DCHECK(my_task_runner_->RunsTasksOnCurrentThread());
+ if (input_contents_size_ > 0)
+ PostToPeer(false, DOWNLOAD_INTERRUPT_REASON_NONE);
+}
+
void ByteStreamWriterImpl::Close(
DownloadInterruptReason status) {
DCHECK(my_task_runner_->RunsTasksOnCurrentThread());
diff --git a/content/browser/byte_stream.h b/content/browser/byte_stream.h
index 49b7674..973e80573 100644
--- a/content/browser/byte_stream.h
+++ b/content/browser/byte_stream.h
@@ -134,6 +134,12 @@ public:
virtual bool Write(scoped_refptr<net::IOBuffer> buffer,
size_t byte_count) = 0;
+ // Flushes contents buffered in this writer to the corresponding reader
+ // regardless if buffer filling rate is greater than
+ // kFractionBufferBeforeSending or not. Does nothing if there's no contents
+ // buffered.
+ virtual void Flush() = 0;
+
// Signal that all data that is going to be sent, has been sent,
// and provide a status. |DOWNLOAD_INTERRUPT_REASON_NONE| should be
// passed for successful completion.
diff --git a/content/browser/byte_stream_unittest.cc b/content/browser/byte_stream_unittest.cc
index ed413da..a585fb7 100644
--- a/content/browser/byte_stream_unittest.cc
+++ b/content/browser/byte_stream_unittest.cc
@@ -146,6 +146,45 @@ TEST_F(ByteStreamTest, ByteStream_PushBack) {
byte_stream_output->Read(&output_io_buffer, &output_length));
}
+// Confirm that Flush() method makes the writer to send written contents to
+// the reader.
+TEST_F(ByteStreamTest, ByteStream_Flush) {
+ scoped_ptr<ByteStreamWriter> byte_stream_input;
+ scoped_ptr<ByteStreamReader> byte_stream_output;
+ CreateByteStream(
+ message_loop_.message_loop_proxy(), message_loop_.message_loop_proxy(),
+ 1024, &byte_stream_input, &byte_stream_output);
+
+ EXPECT_TRUE(Write(byte_stream_input.get(), 1));
+ message_loop_.RunUntilIdle();
+
+ scoped_refptr<net::IOBuffer> output_io_buffer;
+ size_t output_length = 0;
+ // Check that data is not sent to the reader yet.
+ EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
+ byte_stream_output->Read(&output_io_buffer, &output_length));
+
+ byte_stream_input->Flush();
+ message_loop_.RunUntilIdle();
+
+ EXPECT_EQ(ByteStreamReader::STREAM_HAS_DATA,
+ byte_stream_output->Read(&output_io_buffer, &output_length));
+ EXPECT_TRUE(ValidateIOBuffer(output_io_buffer, output_length));
+
+ // Check that it's ok to Flush() an empty writer.
+ byte_stream_input->Flush();
+ message_loop_.RunUntilIdle();
+
+ EXPECT_EQ(ByteStreamReader::STREAM_EMPTY,
+ byte_stream_output->Read(&output_io_buffer, &output_length));
+
+ byte_stream_input->Close(DOWNLOAD_INTERRUPT_REASON_NONE);
+ message_loop_.RunUntilIdle();
+
+ EXPECT_EQ(ByteStreamReader::STREAM_COMPLETE,
+ byte_stream_output->Read(&output_io_buffer, &output_length));
+}
+
// Same as above, only use knowledge of the internals to confirm
// that we're getting pushback even when data's split across the two
// objects