diff options
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/file_stream.h | 9 | ||||
-rw-r--r-- | net/base/file_stream_posix.cc | 86 | ||||
-rw-r--r-- | net/base/file_stream_unittest.cc | 126 | ||||
-rw-r--r-- | net/base/file_stream_win.cc | 42 | ||||
-rw-r--r-- | net/base/mock_file_stream.cc | 26 | ||||
-rw-r--r-- | net/base/mock_file_stream.h | 4 | ||||
-rw-r--r-- | net/base/upload_data_stream.cc | 5 |
7 files changed, 155 insertions, 143 deletions
diff --git a/net/base/file_stream.h b/net/base/file_stream.h index 2c8cccd..f2a8f7f 100644 --- a/net/base/file_stream.h +++ b/net/base/file_stream.h @@ -84,8 +84,8 @@ class NET_EXPORT FileStream { // // This method should not be called if the stream was opened WRITE_ONLY. // - // You can pass NULL as the callback for synchronous I/O. - virtual int Read(char* buf, int buf_len, OldCompletionCallback* callback); + // You can pass a null callback for synchronous I/O. + virtual int Read(char* buf, int buf_len, const CompletionCallback& callback); // Performs the same as Read, but ensures that exactly buf_len bytes // are copied into buf. A partial read may occur, but only as a result of @@ -112,8 +112,9 @@ class NET_EXPORT FileStream { // // This method should not be called if the stream was opened READ_ONLY. // - // You can pass NULL as the callback for synchronous I/O. - virtual int Write(const char* buf, int buf_len, OldCompletionCallback* callback); + // You can pass a null callback for synchronous I/O. + virtual int Write(const char* buf, int buf_len, + const CompletionCallback& callback); // Truncates the file to be |bytes| length. This is only valid for writable // files. After truncation the file stream is positioned at |bytes|. The new diff --git a/net/base/file_stream_posix.cc b/net/base/file_stream_posix.cc index f1c118b..45874e8 100644 --- a/net/base/file_stream_posix.cc +++ b/net/base/file_stream_posix.cc @@ -14,6 +14,7 @@ #include <errno.h> #include "base/basictypes.h" +#include "base/bind.h" #include "base/callback.h" #include "base/eintr_wrapper.h" #include "base/file_path.h" @@ -70,8 +71,8 @@ void ReadFileTask(base::PlatformFile file, char* buf, int buf_len, bool record_uma, - OldCompletionCallback* callback) { - callback->Run(ReadFile(file, buf, buf_len, record_uma)); + const CompletionCallback& callback) { + callback.Run(ReadFile(file, buf, buf_len, record_uma)); } // WriteFile() is a simple wrapper around write() that handles EINTR signals and @@ -89,8 +90,8 @@ int WriteFile(base::PlatformFile file, const char* buf, int buf_len, void WriteFileTask(base::PlatformFile file, const char* buf, int buf_len, bool record_uma, - OldCompletionCallback* callback) { - callback->Run(WriteFile(file, buf, buf_len, record_uma)); + const CompletionCallback& callback) { + callback.Run(WriteFile(file, buf, buf_len, record_uma)); } // FlushFile() is a simple wrapper around fsync() that handles EINTR signals and @@ -137,12 +138,12 @@ class FileStream::AsyncContext { // These methods post synchronous read() and write() calls to a WorkerThread. void InitiateAsyncRead( base::PlatformFile file, char* buf, int buf_len, - OldCompletionCallback* callback); + const CompletionCallback& callback); void InitiateAsyncWrite( base::PlatformFile file, const char* buf, int buf_len, - OldCompletionCallback* callback); + const CompletionCallback& callback); - OldCompletionCallback* callback() const { return callback_; } + const CompletionCallback& callback() const { return callback_; } // Called by the WorkerPool thread executing the IO after the IO completes. // This method queues RunAsynchronousCallback() on the MessageLoop and signals @@ -163,11 +164,7 @@ class FileStream::AsyncContext { // The MessageLoopForIO that this AsyncContext is running on. MessageLoopForIO* const message_loop_; - OldCompletionCallback* callback_; // The user provided callback. - - // A callback wrapper around OnBackgroundIOCompleted(). Run by the WorkerPool - // thread doing the background IO on our behalf. - OldCompletionCallbackImpl<AsyncContext> background_io_completed_callback_; + CompletionCallback callback_; // The user provided callback. // This is used to synchronize between the AsyncContext destructor (which runs // on the IO thread and OnBackgroundIOCompleted() which runs on the WorkerPool @@ -186,9 +183,6 @@ class FileStream::AsyncContext { FileStream::AsyncContext::AsyncContext() : message_loop_(MessageLoopForIO::current()), - callback_(NULL), - background_io_completed_callback_( - this, &AsyncContext::OnBackgroundIOCompleted), background_io_completed_(true, false), message_loop_task_(NULL), is_closing_(false), @@ -196,7 +190,7 @@ FileStream::AsyncContext::AsyncContext() FileStream::AsyncContext::~AsyncContext() { is_closing_ = true; - if (callback_) { + if (!callback_.is_null()) { // If |callback_| is non-NULL, that implies either the worker thread is // still running the IO task, or the completion callback is queued up on the // MessageLoopForIO, but AsyncContext() got deleted before then. @@ -213,32 +207,34 @@ FileStream::AsyncContext::~AsyncContext() { void FileStream::AsyncContext::InitiateAsyncRead( base::PlatformFile file, char* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(!callback_); + const CompletionCallback& callback) { + DCHECK(callback_.is_null()); callback_ = callback; - base::WorkerPool::PostTask(FROM_HERE, - NewRunnableFunction( - &ReadFileTask, - file, buf, buf_len, - record_uma_, - &background_io_completed_callback_), - true /* task_is_slow */); + base::WorkerPool::PostTask( + FROM_HERE, + base::Bind(&ReadFileTask, file, buf, buf_len, + record_uma_, + base::Bind(&AsyncContext::OnBackgroundIOCompleted, + base::Unretained(this))), + true /* task_is_slow */); } void FileStream::AsyncContext::InitiateAsyncWrite( base::PlatformFile file, const char* buf, int buf_len, - OldCompletionCallback* callback) { - DCHECK(!callback_); + const CompletionCallback& callback) { + DCHECK(callback_.is_null()); callback_ = callback; - base::WorkerPool::PostTask(FROM_HERE, - NewRunnableFunction( - &WriteFileTask, - file, buf, buf_len, - record_uma_, - &background_io_completed_callback_), - true /* task_is_slow */); + base::WorkerPool::PostTask( + FROM_HERE, + base::Bind( + &WriteFileTask, + file, buf, buf_len, + record_uma_, + base::Bind(&AsyncContext::OnBackgroundIOCompleted, + base::Unretained(this))), + true /* task_is_slow */); } void FileStream::AsyncContext::OnBackgroundIOCompleted(int result) { @@ -261,15 +257,15 @@ void FileStream::AsyncContext::RunAsynchronousCallback() { message_loop_task_ = NULL; if (is_closing_) { - callback_ = NULL; + callback_.Reset(); return; } - DCHECK(callback_); - OldCompletionCallback* temp = NULL; + DCHECK(!callback_.is_null()); + CompletionCallback temp; std::swap(temp, callback_); background_io_completed_.Reset(); - temp->Run(result_); + temp.Run(result_); } // FileStream ------------------------------------------------------------ @@ -339,7 +335,7 @@ int64 FileStream::Seek(Whence whence, int64 offset) { return ERR_UNEXPECTED; // If we're in async, make sure we don't have a request in flight. - DCHECK(!async_context_.get() || !async_context_->callback()); + DCHECK(!async_context_.get() || async_context_->callback().is_null()); off_t res = lseek(file_, static_cast<off_t>(offset), static_cast<int>(whence)); @@ -370,18 +366,18 @@ int64 FileStream::Available() { } int FileStream::Read( - char* buf, int buf_len, OldCompletionCallback* callback) { + char* buf, int buf_len, const CompletionCallback& callback) { if (!IsOpen()) return ERR_UNEXPECTED; // read(..., 0) will return 0, which indicates end-of-file. - DCHECK(buf_len > 0); + DCHECK_GT(buf_len, 0); DCHECK(open_flags_ & base::PLATFORM_FILE_READ); if (async_context_.get()) { DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); // If we're in async, make sure we don't have a request in flight. - DCHECK(!async_context_->callback()); + DCHECK(async_context_->callback().is_null()); if (record_uma_) async_context_->EnableErrorStatistics(); async_context_->InitiateAsyncRead(file_, buf, buf_len, callback); @@ -396,7 +392,7 @@ int FileStream::ReadUntilComplete(char *buf, int buf_len) { int bytes_total = 0; do { - int bytes_read = Read(buf, to_read, NULL); + int bytes_read = Read(buf, to_read, CompletionCallback()); if (bytes_read <= 0) { if (bytes_total == 0) return bytes_read; @@ -413,7 +409,7 @@ int FileStream::ReadUntilComplete(char *buf, int buf_len) { } int FileStream::Write( - const char* buf, int buf_len, OldCompletionCallback* callback) { + const char* buf, int buf_len, const CompletionCallback& callback) { // write(..., 0) will return 0, which indicates end-of-file. DCHECK_GT(buf_len, 0); @@ -423,7 +419,7 @@ int FileStream::Write( if (async_context_.get()) { DCHECK(open_flags_ & base::PLATFORM_FILE_ASYNC); // If we're in async, make sure we don't have a request in flight. - DCHECK(!async_context_->callback()); + DCHECK(async_context_->callback().is_null()); if (record_uma_) async_context_->EnableErrorStatistics(); async_context_->InitiateAsyncWrite(file_, buf, buf_len, callback); diff --git a/net/base/file_stream_unittest.cc b/net/base/file_stream_unittest.cc index 16c9fe3..966a829 100644 --- a/net/base/file_stream_unittest.cc +++ b/net/base/file_stream_unittest.cc @@ -2,12 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "net/base/file_stream.h" + +#include "base/bind.h" #include "base/callback.h" #include "base/file_util.h" #include "base/message_loop.h" #include "base/path_service.h" #include "base/platform_file.h" -#include "net/base/file_stream.h" #include "net/base/net_errors.h" #include "net/base/test_completion_callback.h" #include "testing/gtest/include/gtest/gtest.h" @@ -103,7 +105,8 @@ TEST_F(FileStreamTest, UseFileHandle) { ASSERT_EQ(kTestDataSize, read_stream.Available()); // Read into buffer and compare. char buffer[kTestDataSize]; - ASSERT_EQ(kTestDataSize, read_stream.Read(buffer, kTestDataSize, NULL)); + ASSERT_EQ(kTestDataSize, + read_stream.Read(buffer, kTestDataSize, CompletionCallback())); ASSERT_EQ(0, memcmp(kTestData, buffer, kTestDataSize)); read_stream.Close(); @@ -114,7 +117,8 @@ TEST_F(FileStreamTest, UseFileHandle) { FileStream write_stream(file, flags); ASSERT_EQ(0, write_stream.Seek(FROM_BEGIN, 0)); - ASSERT_EQ(kTestDataSize, write_stream.Write(kTestData, kTestDataSize, NULL)); + ASSERT_EQ(kTestDataSize, + write_stream.Write(kTestData, kTestDataSize, CompletionCallback())); write_stream.Close(); // Read into buffer and compare to make sure the handle worked fine. @@ -138,7 +142,7 @@ TEST_F(FileStreamTest, UseClosedStream) { // Try reading... char buf[10]; - int rv = stream.Read(buf, arraysize(buf), NULL); + int rv = stream.Read(buf, arraysize(buf), CompletionCallback()); EXPECT_EQ(ERR_UNEXPECTED, rv); } @@ -161,7 +165,7 @@ TEST_F(FileStreamTest, BasicRead) { std::string data_read; for (;;) { char buf[4]; - rv = stream.Read(buf, arraysize(buf), NULL); + rv = stream.Read(buf, arraysize(buf), CompletionCallback()); EXPECT_LE(0, rv); if (rv <= 0) break; @@ -187,14 +191,14 @@ TEST_F(FileStreamTest, AsyncRead) { int64 total_bytes_avail = stream.Available(); EXPECT_EQ(file_size, total_bytes_avail); - TestOldCompletionCallback callback; + TestCompletionCallback callback; int total_bytes_read = 0; std::string data_read; for (;;) { char buf[4]; - rv = stream.Read(buf, arraysize(buf), &callback); + rv = stream.Read(buf, arraysize(buf), callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_LE(0, rv); @@ -222,10 +226,10 @@ TEST_F(FileStreamTest, AsyncRead_EarlyClose) { int64 total_bytes_avail = stream.Available(); EXPECT_EQ(file_size, total_bytes_avail); - TestOldCompletionCallback callback; + TestCompletionCallback callback; char buf[4]; - rv = stream.Read(buf, arraysize(buf), &callback); + rv = stream.Read(buf, arraysize(buf), callback.callback()); stream.Close(); if (rv < 0) { EXPECT_EQ(ERR_IO_PENDING, rv); @@ -260,7 +264,7 @@ TEST_F(FileStreamTest, BasicRead_FromOffset) { std::string data_read; for (;;) { char buf[4]; - rv = stream.Read(buf, arraysize(buf), NULL); + rv = stream.Read(buf, arraysize(buf), CompletionCallback()); EXPECT_LE(0, rv); if (rv <= 0) break; @@ -291,14 +295,14 @@ TEST_F(FileStreamTest, AsyncRead_FromOffset) { int64 total_bytes_avail = stream.Available(); EXPECT_EQ(file_size - kOffset, total_bytes_avail); - TestOldCompletionCallback callback; + TestCompletionCallback callback; int total_bytes_read = 0; std::string data_read; for (;;) { char buf[4]; - rv = stream.Read(buf, arraysize(buf), &callback); + rv = stream.Read(buf, arraysize(buf), callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_LE(0, rv); @@ -346,7 +350,7 @@ TEST_F(FileStreamTest, BasicWrite) { EXPECT_TRUE(ok); EXPECT_EQ(0, file_size); - rv = stream.Write(kTestData, kTestDataSize, NULL); + rv = stream.Write(kTestData, kTestDataSize, CompletionCallback()); EXPECT_EQ(kTestDataSize, rv); stream.Close(); @@ -368,13 +372,13 @@ TEST_F(FileStreamTest, AsyncWrite) { EXPECT_TRUE(ok); EXPECT_EQ(0, file_size); - TestOldCompletionCallback callback; + TestCompletionCallback callback; int total_bytes_written = 0; while (total_bytes_written != kTestDataSize) { rv = stream.Write(kTestData + total_bytes_written, kTestDataSize - total_bytes_written, - &callback); + callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_LT(0, rv); @@ -400,12 +404,12 @@ TEST_F(FileStreamTest, AsyncWrite_EarlyClose) { EXPECT_TRUE(ok); EXPECT_EQ(0, file_size); - TestOldCompletionCallback callback; + TestCompletionCallback callback; int total_bytes_written = 0; rv = stream.Write(kTestData + total_bytes_written, kTestDataSize - total_bytes_written, - &callback); + callback.callback()); stream.Close(); if (rv < 0) { EXPECT_EQ(ERR_IO_PENDING, rv); @@ -435,7 +439,7 @@ TEST_F(FileStreamTest, BasicWrite_FromOffset) { int64 new_offset = stream.Seek(FROM_END, kOffset); EXPECT_EQ(kTestDataSize, new_offset); - rv = stream.Write(kTestData, kTestDataSize, NULL); + rv = stream.Write(kTestData, kTestDataSize, CompletionCallback()); EXPECT_EQ(kTestDataSize, rv); stream.Close(); @@ -460,13 +464,13 @@ TEST_F(FileStreamTest, AsyncWrite_FromOffset) { int64 new_offset = stream.Seek(FROM_END, kOffset); EXPECT_EQ(kTestDataSize, new_offset); - TestOldCompletionCallback callback; + TestCompletionCallback callback; int total_bytes_written = 0; while (total_bytes_written != kTestDataSize) { rv = stream.Write(kTestData + total_bytes_written, kTestDataSize - total_bytes_written, - &callback); + callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_LT(0, rv); @@ -499,7 +503,7 @@ TEST_F(FileStreamTest, BasicReadWrite) { std::string data_read; for (;;) { char buf[4]; - rv = stream.Read(buf, arraysize(buf), NULL); + rv = stream.Read(buf, arraysize(buf), CompletionCallback()); EXPECT_LE(0, rv); if (rv <= 0) break; @@ -509,7 +513,7 @@ TEST_F(FileStreamTest, BasicReadWrite) { EXPECT_EQ(file_size, total_bytes_read); EXPECT_TRUE(data_read == kTestData); - rv = stream.Write(kTestData, kTestDataSize, NULL); + rv = stream.Write(kTestData, kTestDataSize, CompletionCallback()); EXPECT_EQ(kTestDataSize, rv); stream.Close(); @@ -536,7 +540,7 @@ TEST_F(FileStreamTest, BasicWriteRead) { int64 offset = stream.Seek(FROM_END, 0); EXPECT_EQ(offset, file_size); - rv = stream.Write(kTestData, kTestDataSize, NULL); + rv = stream.Write(kTestData, kTestDataSize, CompletionCallback()); EXPECT_EQ(kTestDataSize, rv); offset = stream.Seek(FROM_BEGIN, 0); @@ -547,7 +551,7 @@ TEST_F(FileStreamTest, BasicWriteRead) { std::string data_read; for (;;) { char buf[4]; - rv = stream.Read(buf, arraysize(buf), NULL); + rv = stream.Read(buf, arraysize(buf), CompletionCallback()); EXPECT_LE(0, rv); if (rv <= 0) break; @@ -582,13 +586,13 @@ TEST_F(FileStreamTest, BasicAsyncReadWrite) { int64 total_bytes_avail = stream.Available(); EXPECT_EQ(file_size, total_bytes_avail); - TestOldCompletionCallback callback; + TestCompletionCallback callback; int64 total_bytes_read = 0; std::string data_read; for (;;) { char buf[4]; - rv = stream.Read(buf, arraysize(buf), &callback); + rv = stream.Read(buf, arraysize(buf), callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_LE(0, rv); @@ -605,7 +609,7 @@ TEST_F(FileStreamTest, BasicAsyncReadWrite) { while (total_bytes_written != kTestDataSize) { rv = stream.Write(kTestData + total_bytes_written, kTestDataSize - total_bytes_written, - &callback); + callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_LT(0, rv); @@ -640,13 +644,13 @@ TEST_F(FileStreamTest, BasicAsyncWriteRead) { int64 offset = stream.Seek(FROM_END, 0); EXPECT_EQ(offset, file_size); - TestOldCompletionCallback callback; + TestCompletionCallback callback; int total_bytes_written = 0; while (total_bytes_written != kTestDataSize) { rv = stream.Write(kTestData + total_bytes_written, kTestDataSize - total_bytes_written, - &callback); + callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_LT(0, rv); @@ -665,7 +669,7 @@ TEST_F(FileStreamTest, BasicAsyncWriteRead) { std::string data_read; for (;;) { char buf[4]; - rv = stream.Read(buf, arraysize(buf), &callback); + rv = stream.Read(buf, arraysize(buf), callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_LE(0, rv); @@ -686,9 +690,9 @@ TEST_F(FileStreamTest, BasicAsyncWriteRead) { EXPECT_EQ(kExpectedFileData, data_read); } -class TestWriteReadOldCompletionCallback : public Callback1<int>::Type { +class TestWriteReadCompletionCallback { public: - TestWriteReadOldCompletionCallback( + TestWriteReadCompletionCallback( FileStream* stream, int* total_bytes_written, int* total_bytes_read, @@ -699,7 +703,9 @@ class TestWriteReadOldCompletionCallback : public Callback1<int>::Type { stream_(stream), total_bytes_written_(total_bytes_written), total_bytes_read_(total_bytes_read), - data_read_(data_read) {} + data_read_(data_read), + callback_(base::Bind(&TestWriteReadCompletionCallback::OnComplete, + base::Unretained(this))) {} int WaitForResult() { DCHECK(!waiting_for_result_); @@ -712,10 +718,12 @@ class TestWriteReadOldCompletionCallback : public Callback1<int>::Type { return result_; } + const CompletionCallback& callback() const { return callback_; } + private: - virtual void RunWithParams(const Tuple1<int>& params) { - DCHECK_LT(0, params.a); - *total_bytes_written_ += params.a; + void OnComplete(int result) { + DCHECK_LT(0, result); + *total_bytes_written_ += result; int rv; @@ -723,11 +731,11 @@ class TestWriteReadOldCompletionCallback : public Callback1<int>::Type { // Recurse to finish writing all data. int total_bytes_written = 0, total_bytes_read = 0; std::string data_read; - TestWriteReadOldCompletionCallback callback( + TestWriteReadCompletionCallback callback( stream_, &total_bytes_written, &total_bytes_read, &data_read); rv = stream_->Write(kTestData + *total_bytes_written_, kTestDataSize - *total_bytes_written_, - &callback); + callback.callback()); DCHECK_EQ(ERR_IO_PENDING, rv); rv = callback.WaitForResult(); *total_bytes_written_ += total_bytes_written; @@ -736,10 +744,10 @@ class TestWriteReadOldCompletionCallback : public Callback1<int>::Type { } else { // We're done writing all data. Start reading the data. stream_->Seek(FROM_BEGIN, 0); - TestOldCompletionCallback callback; + TestCompletionCallback callback; for (;;) { char buf[4]; - rv = stream_->Read(buf, arraysize(buf), &callback); + rv = stream_->Read(buf, arraysize(buf), callback.callback()); if (rv == ERR_IO_PENDING) { bool old_state = MessageLoop::current()->NestableTasksAllowed(); MessageLoop::current()->SetNestableTasksAllowed(true); @@ -767,8 +775,9 @@ class TestWriteReadOldCompletionCallback : public Callback1<int>::Type { int* total_bytes_written_; int* total_bytes_read_; std::string* data_read_; + const CompletionCallback callback_; - DISALLOW_COPY_AND_ASSIGN(TestWriteReadOldCompletionCallback); + DISALLOW_COPY_AND_ASSIGN(TestWriteReadCompletionCallback); }; TEST_F(FileStreamTest, AsyncWriteRead) { @@ -793,12 +802,12 @@ TEST_F(FileStreamTest, AsyncWriteRead) { int total_bytes_written = 0; int total_bytes_read = 0; std::string data_read; - TestWriteReadOldCompletionCallback callback(&stream, &total_bytes_written, + TestWriteReadCompletionCallback callback(&stream, &total_bytes_written, &total_bytes_read, &data_read); rv = stream.Write(kTestData + total_bytes_written, kTestDataSize - static_cast<int>(total_bytes_written), - &callback); + callback.callback()); if (rv == ERR_IO_PENDING) rv = callback.WaitForResult(); EXPECT_LT(0, rv); @@ -816,14 +825,16 @@ TEST_F(FileStreamTest, AsyncWriteRead) { EXPECT_EQ(kExpectedFileData, data_read); } -class TestWriteCloseOldCompletionCallback : public Callback1<int>::Type { +class TestWriteCloseCompletionCallback { public: - TestWriteCloseOldCompletionCallback(FileStream* stream, int* total_bytes_written) + TestWriteCloseCompletionCallback(FileStream* stream, int* total_bytes_written) : result_(0), have_result_(false), waiting_for_result_(false), stream_(stream), - total_bytes_written_(total_bytes_written) {} + total_bytes_written_(total_bytes_written), + callback_(base::Bind(&TestWriteCloseCompletionCallback::OnComplete, + base::Unretained(this))) {} int WaitForResult() { DCHECK(!waiting_for_result_); @@ -836,20 +847,22 @@ class TestWriteCloseOldCompletionCallback : public Callback1<int>::Type { return result_; } + const CompletionCallback& callback() const { return callback_; } + private: - virtual void RunWithParams(const Tuple1<int>& params) { - DCHECK_LT(0, params.a); - *total_bytes_written_ += params.a; + void OnComplete(int result) { + DCHECK_LT(0, result); + *total_bytes_written_ += result; int rv; if (*total_bytes_written_ != kTestDataSize) { // Recurse to finish writing all data. int total_bytes_written = 0; - TestWriteCloseOldCompletionCallback callback(stream_, &total_bytes_written); + TestWriteCloseCompletionCallback callback(stream_, &total_bytes_written); rv = stream_->Write(kTestData + *total_bytes_written_, kTestDataSize - *total_bytes_written_, - &callback); + callback.callback()); DCHECK_EQ(ERR_IO_PENDING, rv); rv = callback.WaitForResult(); *total_bytes_written_ += total_bytes_written; @@ -868,8 +881,9 @@ class TestWriteCloseOldCompletionCallback : public Callback1<int>::Type { bool waiting_for_result_; FileStream* stream_; int* total_bytes_written_; + const CompletionCallback callback_; - DISALLOW_COPY_AND_ASSIGN(TestWriteCloseOldCompletionCallback); + DISALLOW_COPY_AND_ASSIGN(TestWriteCloseCompletionCallback); }; TEST_F(FileStreamTest, AsyncWriteClose) { @@ -892,9 +906,9 @@ TEST_F(FileStreamTest, AsyncWriteClose) { EXPECT_EQ(offset, file_size); int total_bytes_written = 0; - TestWriteCloseOldCompletionCallback callback(&stream, &total_bytes_written); + TestWriteCloseCompletionCallback callback(&stream, &total_bytes_written); - rv = stream.Write(kTestData, kTestDataSize, &callback); + rv = stream.Write(kTestData, kTestDataSize, callback.callback()); if (rv == ERR_IO_PENDING) total_bytes_written = callback.WaitForResult(); EXPECT_LT(0, total_bytes_written); @@ -914,13 +928,13 @@ TEST_F(FileStreamTest, Truncate) { // Write some data to the file. const char test_data[] = "0123456789"; - write_stream.Write(test_data, arraysize(test_data), NULL); + write_stream.Write(test_data, arraysize(test_data), CompletionCallback()); // Truncate the file. ASSERT_EQ(4, write_stream.Truncate(4)); // Write again. - write_stream.Write(test_data, 4, NULL); + write_stream.Write(test_data, 4, CompletionCallback()); // Close the stream. write_stream.Close(); diff --git a/net/base/file_stream_win.cc b/net/base/file_stream_win.cc index 86ee202..07d365f 100644 --- a/net/base/file_stream_win.cc +++ b/net/base/file_stream_win.cc @@ -48,16 +48,16 @@ int RecordAndMapError(int error, FileErrorSource source, bool record_uma) { class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { public: AsyncContext(FileStream* owner) - : owner_(owner), context_(), callback_(NULL), is_closing_(false), + : owner_(owner), context_(), is_closing_(false), record_uma_(false), error_source_(FILE_ERROR_SOURCE_COUNT) { context_.handler = this; } ~AsyncContext(); - void IOCompletionIsPending(OldCompletionCallback* callback); + void IOCompletionIsPending(const CompletionCallback& callback); OVERLAPPED* overlapped() { return &context_.overlapped; } - OldCompletionCallback* callback() const { return callback_; } + const CompletionCallback& callback() const { return callback_; } void set_error_source(FileErrorSource source) { error_source_ = source; } @@ -71,7 +71,7 @@ class FileStream::AsyncContext : public MessageLoopForIO::IOHandler { FileStream* owner_; MessageLoopForIO::IOContext context_; - OldCompletionCallback* callback_; + CompletionCallback callback_; bool is_closing_; bool record_uma_; FileErrorSource error_source_; @@ -81,7 +81,7 @@ FileStream::AsyncContext::~AsyncContext() { is_closing_ = true; bool waited = false; base::TimeTicks start = base::TimeTicks::Now(); - while (callback_) { + while (!callback_.is_null()) { waited = true; MessageLoopForIO::current()->WaitForIOCompletion(INFINITE, this); } @@ -93,18 +93,18 @@ FileStream::AsyncContext::~AsyncContext() { } void FileStream::AsyncContext::IOCompletionIsPending( - OldCompletionCallback* callback) { - DCHECK(!callback_); + const CompletionCallback& callback) { + DCHECK(callback_.is_null()); callback_ = callback; } void FileStream::AsyncContext::OnIOCompleted( MessageLoopForIO::IOContext* context, DWORD bytes_read, DWORD error) { DCHECK_EQ(&context_, context); - DCHECK(callback_); + DCHECK(!callback_.is_null()); if (is_closing_) { - callback_ = NULL; + callback_.Reset(); return; } @@ -115,9 +115,9 @@ void FileStream::AsyncContext::OnIOCompleted( if (bytes_read) IncrementOffset(&context->overlapped, bytes_read); - OldCompletionCallback* temp = NULL; + CompletionCallback temp; std::swap(temp, callback_); - temp->Run(result); + temp.Run(result); } // FileStream ------------------------------------------------------------ @@ -192,7 +192,7 @@ int64 FileStream::Seek(Whence whence, int64 offset) { if (!IsOpen()) return ERR_UNEXPECTED; - DCHECK(!async_context_.get() || !async_context_->callback()); + DCHECK(!async_context_.get() || async_context_->callback().is_null()); LARGE_INTEGER distance, result; distance.QuadPart = offset; @@ -230,7 +230,7 @@ int64 FileStream::Available() { } int FileStream::Read( - char* buf, int buf_len, OldCompletionCallback* callback) { + char* buf, int buf_len, const CompletionCallback& callback) { if (!IsOpen()) return ERR_UNEXPECTED; @@ -238,12 +238,12 @@ int FileStream::Read( OVERLAPPED* overlapped = NULL; if (async_context_.get()) { - DCHECK(callback); - DCHECK(!async_context_->callback()); + DCHECK(!callback.is_null()); + DCHECK(async_context_->callback().is_null()); overlapped = async_context_->overlapped(); async_context_->set_error_source(FILE_ERROR_SOURCE_READ); } else { - DCHECK(!callback); + DCHECK(callback.is_null()); base::ThreadRestrictions::AssertIOAllowed(); } @@ -275,7 +275,7 @@ int FileStream::ReadUntilComplete(char *buf, int buf_len) { int bytes_total = 0; do { - int bytes_read = Read(buf, to_read, NULL); + int bytes_read = Read(buf, to_read, CompletionCallback()); if (bytes_read <= 0) { if (bytes_total == 0) return bytes_read; @@ -292,7 +292,7 @@ int FileStream::ReadUntilComplete(char *buf, int buf_len) { } int FileStream::Write( - const char* buf, int buf_len, OldCompletionCallback* callback) { + const char* buf, int buf_len, const CompletionCallback& callback) { if (!IsOpen()) return ERR_UNEXPECTED; @@ -300,12 +300,12 @@ int FileStream::Write( OVERLAPPED* overlapped = NULL; if (async_context_.get()) { - DCHECK(callback); - DCHECK(!async_context_->callback()); + DCHECK(!callback.is_null()); + DCHECK(async_context_->callback().is_null()); overlapped = async_context_->overlapped(); async_context_->set_error_source(FILE_ERROR_SOURCE_WRITE); } else { - DCHECK(!callback); + DCHECK(callback.is_null()); base::ThreadRestrictions::AssertIOAllowed(); } diff --git a/net/base/mock_file_stream.cc b/net/base/mock_file_stream.cc index 19a027f..cb59cf9 100644 --- a/net/base/mock_file_stream.cc +++ b/net/base/mock_file_stream.cc @@ -10,39 +10,39 @@ namespace testing { int MockFileStream::Open(const FilePath& path, int open_flags) { path_ = path; - return ReturnError(net::FileStream::Open(path, open_flags)); + return ReturnError(FileStream::Open(path, open_flags)); } -int64 MockFileStream::Seek(net::Whence whence, int64 offset) { - return ReturnError64(net::FileStream::Seek(whence, offset)); +int64 MockFileStream::Seek(Whence whence, int64 offset) { + return ReturnError64(FileStream::Seek(whence, offset)); } int64 MockFileStream::Available() { - return ReturnError64(net::FileStream::Available()); + return ReturnError64(FileStream::Available()); } int MockFileStream::Read(char* buf, - int buf_len, - net::OldCompletionCallback* callback) { - return ReturnError(net::FileStream::Read(buf, buf_len, callback)); + int buf_len, + const CompletionCallback& callback) { + return ReturnError(FileStream::Read(buf, buf_len, callback)); } int MockFileStream::ReadUntilComplete(char *buf, int buf_len) { - return ReturnError(net::FileStream::ReadUntilComplete(buf, buf_len)); + return ReturnError(FileStream::ReadUntilComplete(buf, buf_len)); } int MockFileStream::Write(const char* buf, - int buf_len, - net::OldCompletionCallback* callback) { - return ReturnError(net::FileStream::Write(buf, buf_len, callback)); + int buf_len, + const CompletionCallback& callback) { + return ReturnError(FileStream::Write(buf, buf_len, callback)); } int64 MockFileStream::Truncate(int64 bytes) { - return ReturnError64(net::FileStream::Truncate(bytes)); + return ReturnError64(FileStream::Truncate(bytes)); } int MockFileStream::Flush() { - return ReturnError(net::FileStream::Flush()); + return ReturnError(FileStream::Flush()); } } // namespace testing diff --git a/net/base/mock_file_stream.h b/net/base/mock_file_stream.h index 569937e..be711e1 100644 --- a/net/base/mock_file_stream.h +++ b/net/base/mock_file_stream.h @@ -31,11 +31,11 @@ class MockFileStream : public net::FileStream { virtual int64 Available() OVERRIDE; virtual int Read(char* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual int ReadUntilComplete(char *buf, int buf_len) OVERRIDE; virtual int Write(const char* buf, int buf_len, - net::OldCompletionCallback* callback) OVERRIDE; + const CompletionCallback& callback) OVERRIDE; virtual int64 Truncate(int64 bytes) OVERRIDE; virtual int Flush() OVERRIDE; diff --git a/net/base/upload_data_stream.cc b/net/base/upload_data_stream.cc index 33cf120..79bfd1d 100644 --- a/net/base/upload_data_stream.cc +++ b/net/base/upload_data_stream.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -112,7 +112,8 @@ int UploadDataStream::FillBuf() { // Temporarily allow until fix: http://crbug.com/72001. base::ThreadRestrictions::ScopedAllowIO allow_io; if (next_element_stream_.get()) - rv = next_element_stream_->Read(buf_->data() + buf_len_, count, NULL); + rv = next_element_stream_->Read(buf_->data() + buf_len_, count, + CompletionCallback()); if (rv <= 0) { // If there's less data to read than we initially observed, then // pad with zero. Otherwise the server will hang waiting for the |