From 09c08283f3bb20f4441d518f6ba84fe937c19fa9 Mon Sep 17 00:00:00 2001 From: "hashimoto@chromium.org" Date: Wed, 20 Feb 2013 09:24:33 +0000 Subject: net: Use DrainableIOBuffer in URLFetcherFileWriter With DrainableIOBuffer, there is no need to maintain |pending_bytes_| and |buffer_offset_| manually. BUG=126753 TEST=net_unittests Review URL: https://chromiumcodereview.appspot.com/12297023 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@183440 0039d316-1c4b-4281-b951-d872f2087c98 --- net/url_request/url_fetcher_file_writer.cc | 17 ++++++----------- net/url_request/url_fetcher_file_writer.h | 12 ++---------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/net/url_request/url_fetcher_file_writer.cc b/net/url_request/url_fetcher_file_writer.cc index bd70b57..07da472 100644 --- a/net/url_request/url_fetcher_file_writer.cc +++ b/net/url_request/url_fetcher_file_writer.cc @@ -49,16 +49,12 @@ void URLFetcherFileWriter::CreateTempFile(const CompletionCallback& callback) { void URLFetcherFileWriter::Write(scoped_refptr buffer, int num_bytes, const CompletionCallback& callback) { - // Start writing to the file by setting the initial state - // of |pending_bytes_| and |buffer_offset_| to indicate that the - // entire buffer has not yet been written. - pending_bytes_ = num_bytes; - buffer_offset_ = 0; - ContinueWrite(buffer, callback, base::PLATFORM_FILE_OK, 0); + ContinueWrite(new DrainableIOBuffer(buffer, num_bytes), callback, + base::PLATFORM_FILE_OK, 0); } void URLFetcherFileWriter::ContinueWrite( - scoped_refptr buffer, + scoped_refptr buffer, const CompletionCallback& callback, base::PlatformFileError error_code, int bytes_written) { @@ -78,14 +74,13 @@ void URLFetcherFileWriter::ContinueWrite( } total_bytes_written_ += bytes_written; - buffer_offset_ += bytes_written; - pending_bytes_ -= bytes_written; + buffer->DidConsume(bytes_written); - if (pending_bytes_ > 0) { + if (buffer->BytesRemaining() > 0) { base::FileUtilProxy::Write( file_task_runner_, file_handle_, total_bytes_written_, // Append to the end - (buffer->data() + buffer_offset_), pending_bytes_, + buffer->data(), buffer->BytesRemaining(), base::Bind(&URLFetcherFileWriter::ContinueWrite, weak_factory_.GetWeakPtr(), buffer, diff --git a/net/url_request/url_fetcher_file_writer.h b/net/url_request/url_fetcher_file_writer.h index 6a14409..3596f2b 100644 --- a/net/url_request/url_fetcher_file_writer.h +++ b/net/url_request/url_fetcher_file_writer.h @@ -18,6 +18,7 @@ class TaskRunner; namespace net { +class DrainableIOBuffer; class IOBuffer; // This class encapsulates all state involved in writing URLFetcher response @@ -38,7 +39,7 @@ class URLFetcherFileWriter { // Called when a write has been done. Continues writing if there are more // bytes to write. Otherwise, runs |callback|. - void ContinueWrite(scoped_refptr buffer, + void ContinueWrite(scoped_refptr buffer, const CompletionCallback& callback, base::PlatformFileError error_code, int bytes_written); @@ -103,15 +104,6 @@ class URLFetcherFileWriter { // written, so that writes know the offset to give. int64 total_bytes_written_; - // How many bytes did the last Write() try to write? Needed so - // that if not all the bytes get written on a Write(), we can - // call Write() again with the rest. - int pending_bytes_; - - // When writing, how many bytes from the buffer have been successfully - // written so far? - int buffer_offset_; - DISALLOW_COPY_AND_ASSIGN(URLFetcherFileWriter); }; -- cgit v1.1