From e4923e8e5ce3727a0a78db75458348d2d85ce705 Mon Sep 17 00:00:00 2001
From: "tyoshino@chromium.org"
 <tyoshino@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Wed, 14 Aug 2013 04:44:38 +0000
Subject: StreamURLRequestJob::OnDataAvailable refactoring

- Use return value of reader_->Read() call rather than depending on bytes_read
- More comments
- More DCHECKs

BUG=

Review URL: https://chromiumcodereview.appspot.com/23025002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@217481 0039d316-1c4b-4281-b951-d872f2087c98
---
 content/browser/streams/stream.cc                 |  3 ++
 content/browser/streams/stream_url_request_job.cc | 43 ++++++++++++++++-------
 2 files changed, 34 insertions(+), 12 deletions(-)

(limited to 'content/browser/streams')

diff --git a/content/browser/streams/stream.cc b/content/browser/streams/stream.cc
index f5abe02..6026df9 100644
--- a/content/browser/streams/stream.cc
+++ b/content/browser/streams/stream.cc
@@ -90,6 +90,9 @@ void Stream::Finalize() {
 Stream::StreamState Stream::ReadRawData(net::IOBuffer* buf,
                                         int buf_size,
                                         int* bytes_read) {
+  DCHECK(buf);
+  DCHECK(bytes_read);
+
   *bytes_read = 0;
   if (!data_.get()) {
     data_length_ = 0;
diff --git a/content/browser/streams/stream_url_request_job.cc b/content/browser/streams/stream_url_request_job.cc
index d2719a0..0965178 100644
--- a/content/browser/streams/stream_url_request_job.cc
+++ b/content/browser/streams/stream_url_request_job.cc
@@ -39,19 +39,37 @@ StreamURLRequestJob::~StreamURLRequestJob() {
 void StreamURLRequestJob::OnDataAvailable(Stream* stream) {
   // Clear the IO_PENDING status.
   SetStatus(net::URLRequestStatus());
-  if (pending_buffer_.get()) {
-    int bytes_read;
-    stream_->ReadRawData(
-        pending_buffer_.get(), pending_buffer_size_, &bytes_read);
-
-    // Clear the buffers before notifying the read is complete, so that it is
-    // safe for the observer to read.
-    pending_buffer_ = NULL;
-    pending_buffer_size_ = 0;
-
-    total_bytes_read_ += bytes_read;
-    NotifyReadComplete(bytes_read);
+  // Do nothing if pending_buffer_ is empty, i.e. there's no ReadRawData()
+  // operation waiting for IO completion.
+  if (!pending_buffer_.get())
+    return;
+
+  // pending_buffer_ is set to the IOBuffer instance provided to ReadRawData()
+  // by URLRequestJob.
+
+  int bytes_read;
+  switch (stream_->ReadRawData(
+      pending_buffer_.get(), pending_buffer_size_, &bytes_read)) {
+    case Stream::STREAM_HAS_DATA:
+      DCHECK_GT(bytes_read, 0);
+      break;
+    case Stream::STREAM_COMPLETE:
+      // Ensure this. Calling NotifyReadComplete call with 0 signals
+      // completion.
+      bytes_read = 0;
+      break;
+    case Stream::STREAM_EMPTY:
+      NOTREACHED();
+      break;
   }
+
+  // Clear the buffers before notifying the read is complete, so that it is
+  // safe for the observer to read.
+  pending_buffer_ = NULL;
+  pending_buffer_size_ = 0;
+
+  total_bytes_read_ += bytes_read;
+  NotifyReadComplete(bytes_read);
 }
 
 // net::URLRequestJob methods.
@@ -74,6 +92,7 @@ bool StreamURLRequestJob::ReadRawData(net::IOBuffer* buf,
   if (request_failed_)
     return true;
 
+  DCHECK(buf);
   DCHECK(bytes_read);
   int to_read = buf_size;
   if (max_range_ && to_read) {
-- 
cgit v1.1