Commit dadd6c74 authored by Matt Menke's avatar Matt Menke Committed by Commit Bot

Introduce net::CompletionOnceCallback.

Also convert net::FileStreamContext to use it, as a sample use case with
few consumers.

This CL does not add a OnceCallback version of TestCompletionCallback,
as the repeating version works for use with methods that take
OnceCallbacks.  I think not creating a new test class will make updating
interfaces take a lot less effort.

Also, I expect that all net::CompletionCallbacks are effectively used
as OnceCallbacks, so we can make TestCompletionCallback return
OnceCallbacks when/if we ever finish converting all of net to use
CompletionOnceCallbacks.

Bug: 714018
Change-Id: Iec76deae075b32fe0c65eb18596859f5e6413647
Reviewed-on: https://chromium-review.googlesource.com/853092
Commit-Queue: Matt Menke <mmenke@chromium.org>
Reviewed-by: default avatarRandy Smith <rdsmith@chromium.org>
Cr-Commit-Position: refs/heads/master@{#533061}
parent 006fd61c
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include "content/browser/loader/test_resource_handler.h" #include "content/browser/loader/test_resource_handler.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "net/base/completion_callback.h" #include "net/base/completion_callback.h"
#include "net/base/completion_once_callback.h"
#include "net/base/file_stream.h" #include "net/base/file_stream.h"
#include "net/base/io_buffer.h" #include "net/base/io_buffer.h"
#include "net/base/mime_sniffer.h" #include "net/base/mime_sniffer.h"
...@@ -91,16 +92,16 @@ class MockFileStream : public net::FileStream { ...@@ -91,16 +92,16 @@ class MockFileStream : public net::FileStream {
int Open(const base::FilePath& path, int Open(const base::FilePath& path,
int open_flags, int open_flags,
const net::CompletionCallback& callback) override { net::CompletionOnceCallback callback) override {
return ReturnResult(open_result_, callback); return ReturnResult(open_result_, std::move(callback));
} }
int Close(const net::CompletionCallback& callback) override { int Close(net::CompletionOnceCallback callback) override {
EXPECT_FALSE(closed_); EXPECT_FALSE(closed_);
int result = ReturnResult( int result = ReturnResult(
close_result_, close_result_,
base::BindRepeating(&MockFileStream::SetClosedAndRunCallback, base::BindOnce(&MockFileStream::SetClosedAndRunCallback,
base::Unretained(this), callback)); base::Unretained(this), std::move(callback)));
if (result != net::ERR_IO_PENDING) if (result != net::ERR_IO_PENDING)
closed_ = true; closed_ = true;
return result; return result;
...@@ -111,22 +112,21 @@ class MockFileStream : public net::FileStream { ...@@ -111,22 +112,21 @@ class MockFileStream : public net::FileStream {
return false; return false;
} }
int Seek(int64_t offset, int Seek(int64_t offset, net::Int64CompletionOnceCallback callback) override {
const net::Int64CompletionCallback& callback) override {
NOTREACHED(); NOTREACHED();
return net::ERR_UNEXPECTED; return net::ERR_UNEXPECTED;
} }
int Read(net::IOBuffer* buf, int Read(net::IOBuffer* buf,
int buf_len, int buf_len,
const net::CompletionCallback& callback) override { net::CompletionOnceCallback callback) override {
NOTREACHED(); NOTREACHED();
return net::ERR_UNEXPECTED; return net::ERR_UNEXPECTED;
} }
int Write(net::IOBuffer* buf, int Write(net::IOBuffer* buf,
int buf_len, int buf_len,
const net::CompletionCallback& callback) override { net::CompletionOnceCallback callback) override {
// 0-byte writes aren't allowed. // 0-byte writes aren't allowed.
EXPECT_GT(buf_len, 0); EXPECT_GT(buf_len, 0);
...@@ -137,10 +137,10 @@ class MockFileStream : public net::FileStream { ...@@ -137,10 +137,10 @@ class MockFileStream : public net::FileStream {
if (write_result.result > 0) if (write_result.result > 0)
written_data_ += std::string(buf->data(), write_result.result); written_data_ += std::string(buf->data(), write_result.result);
return ReturnResult(write_result, callback); return ReturnResult(write_result, std::move(callback));
} }
int Flush(const net::CompletionCallback& callback) override { int Flush(net::CompletionOnceCallback callback) override {
NOTREACHED(); NOTREACHED();
return net::ERR_UNEXPECTED; return net::ERR_UNEXPECTED;
} }
...@@ -172,19 +172,19 @@ class MockFileStream : public net::FileStream { ...@@ -172,19 +172,19 @@ class MockFileStream : public net::FileStream {
void set_expect_closed(bool expect_closed) { expect_closed_ = expect_closed; } void set_expect_closed(bool expect_closed) { expect_closed_ = expect_closed; }
private: private:
void SetClosedAndRunCallback(const net::CompletionCallback& callback, void SetClosedAndRunCallback(net::CompletionOnceCallback callback,
int result) { int result) {
EXPECT_FALSE(closed_); EXPECT_FALSE(closed_);
closed_ = true; closed_ = true;
callback.Run(result); std::move(callback).Run(result);
} }
int ReturnResult(OperationResult result, int ReturnResult(OperationResult result,
const net::CompletionCallback& callback) { net::CompletionOnceCallback callback) {
if (result.completion_mode == CompletionMode::SYNC) if (result.completion_mode == CompletionMode::SYNC)
return result.result; return result.result;
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(callback, result.result)); FROM_HERE, base::BindOnce(std::move(callback), result.result));
return net::ERR_IO_PENDING; return net::ERR_IO_PENDING;
} }
......
...@@ -108,6 +108,7 @@ component("net") { ...@@ -108,6 +108,7 @@ component("net") {
"base/auth.cc", "base/auth.cc",
"base/auth.h", "base/auth.h",
"base/completion_callback.h", "base/completion_callback.h",
"base/completion_once_callback.h",
"base/escape.cc", "base/escape.cc",
"base/escape.h", "base/escape.h",
"base/hash_value.cc", "base/hash_value.cc",
......
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#ifndef NET_BASE_COMPLETION_CALLBACK_H__ #ifndef NET_BASE_COMPLETION_CALLBACK_H_
#define NET_BASE_COMPLETION_CALLBACK_H__ #define NET_BASE_COMPLETION_CALLBACK_H_
#include <stdint.h> #include <stdint.h>
...@@ -25,4 +25,4 @@ typedef base::CancelableCallback<void(int)> CancelableCompletionCallback; ...@@ -25,4 +25,4 @@ typedef base::CancelableCallback<void(int)> CancelableCompletionCallback;
} // namespace net } // namespace net
#endif // NET_BASE_COMPLETION_CALLBACK_H__ #endif // NET_BASE_COMPLETION_CALLBACK_H_
// Copyright 2018 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.
#ifndef NET_BASE_COMPLETION_ONCE_CALLBACK_H_
#define NET_BASE_COMPLETION_ONCE_CALLBACK_H_
#include <stdint.h>
#include "base/callback.h"
#include "base/cancelable_callback.h"
namespace net {
// A OnceCallback specialization that takes a single int parameter. Usually this
// is used to report a byte count or network error code.
typedef base::OnceCallback<void(int)> CompletionOnceCallback;
// 64bit version of the OnceCallback specialization that takes a single int64_t
// parameter. Usually this is used to report a file offset, size or network
// error code.
typedef base::OnceCallback<void(int64_t)> Int64CompletionOnceCallback;
typedef base::CancelableOnceCallback<void(int)>
CancelableCompletionOnceCallback;
} // namespace net
#endif // NET_BASE_COMPLETION_ONCE_CALLBACK_H_
...@@ -22,20 +22,21 @@ FileStream::~FileStream() { ...@@ -22,20 +22,21 @@ FileStream::~FileStream() {
context_.release()->Orphan(); context_.release()->Orphan();
} }
int FileStream::Open(const base::FilePath& path, int open_flags, int FileStream::Open(const base::FilePath& path,
const CompletionCallback& callback) { int open_flags,
CompletionOnceCallback callback) {
if (IsOpen()) { if (IsOpen()) {
DLOG(FATAL) << "File is already open!"; DLOG(FATAL) << "File is already open!";
return ERR_UNEXPECTED; return ERR_UNEXPECTED;
} }
DCHECK(open_flags & base::File::FLAG_ASYNC); DCHECK(open_flags & base::File::FLAG_ASYNC);
context_->Open(path, open_flags, callback); context_->Open(path, open_flags, std::move(callback));
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
int FileStream::Close(const CompletionCallback& callback) { int FileStream::Close(CompletionOnceCallback callback) {
context_->Close(callback); context_->Close(std::move(callback));
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
...@@ -43,50 +44,50 @@ bool FileStream::IsOpen() const { ...@@ -43,50 +44,50 @@ bool FileStream::IsOpen() const {
return context_->IsOpen(); return context_->IsOpen();
} }
int FileStream::Seek(int64_t offset, const Int64CompletionCallback& callback) { int FileStream::Seek(int64_t offset, Int64CompletionOnceCallback callback) {
if (!IsOpen()) if (!IsOpen())
return ERR_UNEXPECTED; return ERR_UNEXPECTED;
context_->Seek(offset, callback); context_->Seek(offset, std::move(callback));
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
int FileStream::Read(IOBuffer* buf, int FileStream::Read(IOBuffer* buf,
int buf_len, int buf_len,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
if (!IsOpen()) if (!IsOpen())
return ERR_UNEXPECTED; return ERR_UNEXPECTED;
// read(..., 0) will return 0, which indicates end-of-file. // read(..., 0) will return 0, which indicates end-of-file.
DCHECK_GT(buf_len, 0); DCHECK_GT(buf_len, 0);
return context_->Read(buf, buf_len, callback); return context_->Read(buf, buf_len, std::move(callback));
} }
int FileStream::Write(IOBuffer* buf, int FileStream::Write(IOBuffer* buf,
int buf_len, int buf_len,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
if (!IsOpen()) if (!IsOpen())
return ERR_UNEXPECTED; return ERR_UNEXPECTED;
DCHECK_GE(buf_len, 0); DCHECK_GE(buf_len, 0);
return context_->Write(buf, buf_len, callback); return context_->Write(buf, buf_len, std::move(callback));
} }
int FileStream::GetFileInfo(base::File::Info* file_info, int FileStream::GetFileInfo(base::File::Info* file_info,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
if (!IsOpen()) if (!IsOpen())
return ERR_UNEXPECTED; return ERR_UNEXPECTED;
context_->GetFileInfo(file_info, callback); context_->GetFileInfo(file_info, std::move(callback));
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
int FileStream::Flush(const CompletionCallback& callback) { int FileStream::Flush(CompletionOnceCallback callback) {
if (!IsOpen()) if (!IsOpen())
return ERR_UNEXPECTED; return ERR_UNEXPECTED;
context_->Flush(callback); context_->Flush(std::move(callback));
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include "base/files/file.h" #include "base/files/file.h"
#include "base/macros.h" #include "base/macros.h"
#include "net/base/completion_callback.h" #include "net/base/completion_once_callback.h"
#include "net/base/net_export.h" #include "net/base/net_export.h"
namespace base { namespace base {
...@@ -56,14 +56,15 @@ class NET_EXPORT FileStream { ...@@ -56,14 +56,15 @@ class NET_EXPORT FileStream {
// automatically closed when FileStream is destructed in an asynchronous // automatically closed when FileStream is destructed in an asynchronous
// manner (i.e. the file stream is closed in the background but you don't // manner (i.e. the file stream is closed in the background but you don't
// know when). // know when).
virtual int Open(const base::FilePath& path, int open_flags, virtual int Open(const base::FilePath& path,
const CompletionCallback& callback); int open_flags,
CompletionOnceCallback callback);
// Returns ERR_IO_PENDING and closes the file asynchronously, calling // Returns ERR_IO_PENDING and closes the file asynchronously, calling
// |callback| when done. // |callback| when done.
// It is invalid to request any asynchronous operations while there is an // It is invalid to request any asynchronous operations while there is an
// in-flight asynchronous operation. // in-flight asynchronous operation.
virtual int Close(const CompletionCallback& callback); virtual int Close(CompletionOnceCallback callback);
// Returns true if Open succeeded and Close has not been called. // Returns true if Open succeeded and Close has not been called.
virtual bool IsOpen() const; virtual bool IsOpen() const;
...@@ -74,7 +75,7 @@ class NET_EXPORT FileStream { ...@@ -74,7 +75,7 @@ class NET_EXPORT FileStream {
// position relative to the start of the file. Otherwise, an error code is // position relative to the start of the file. Otherwise, an error code is
// returned. It is invalid to request any asynchronous operations while there // returned. It is invalid to request any asynchronous operations while there
// is an in-flight asynchronous operation. // is an in-flight asynchronous operation.
virtual int Seek(int64_t offset, const Int64CompletionCallback& callback); virtual int Seek(int64_t offset, Int64CompletionOnceCallback callback);
// Call this method to read data from the current stream position // Call this method to read data from the current stream position
// asynchronously. Up to buf_len bytes will be copied into buf. (In // asynchronously. Up to buf_len bytes will be copied into buf. (In
...@@ -96,8 +97,7 @@ class NET_EXPORT FileStream { ...@@ -96,8 +97,7 @@ class NET_EXPORT FileStream {
// in-flight asynchronous operation. // in-flight asynchronous operation.
// //
// This method must not be called if the stream was opened WRITE_ONLY. // This method must not be called if the stream was opened WRITE_ONLY.
virtual int Read(IOBuffer* buf, int buf_len, virtual int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
const CompletionCallback& callback);
// Call this method to write data at the current stream position // Call this method to write data at the current stream position
// asynchronously. Up to buf_len bytes will be written from buf. (In // asynchronously. Up to buf_len bytes will be written from buf. (In
...@@ -121,8 +121,9 @@ class NET_EXPORT FileStream { ...@@ -121,8 +121,9 @@ class NET_EXPORT FileStream {
// This method must not be called if the stream was opened READ_ONLY. // This method must not be called if the stream was opened READ_ONLY.
// //
// Zero byte writes are not allowed. // Zero byte writes are not allowed.
virtual int Write(IOBuffer* buf, int buf_len, virtual int Write(IOBuffer* buf,
const CompletionCallback& callback); int buf_len,
CompletionOnceCallback callback);
// Gets status information about File. May fail synchronously, but never // Gets status information about File. May fail synchronously, but never
// succeeds synchronously. // succeeds synchronously.
...@@ -132,7 +133,7 @@ class NET_EXPORT FileStream { ...@@ -132,7 +133,7 @@ class NET_EXPORT FileStream {
// //
// |file_info| must remain valid until |callback| is invoked. // |file_info| must remain valid until |callback| is invoked.
virtual int GetFileInfo(base::File::Info* file_info, virtual int GetFileInfo(base::File::Info* file_info,
const CompletionCallback& callback); CompletionOnceCallback callback);
// Forces out a filesystem sync on this file to make sure that the file was // Forces out a filesystem sync on this file to make sure that the file was
// written out to disk and is not currently sitting in the buffer. This does // written out to disk and is not currently sitting in the buffer. This does
...@@ -153,7 +154,7 @@ class NET_EXPORT FileStream { ...@@ -153,7 +154,7 @@ class NET_EXPORT FileStream {
// in-flight asynchronous operation. // in-flight asynchronous operation.
// //
// This method should not be called if the stream was opened READ_ONLY. // This method should not be called if the stream was opened READ_ONLY.
virtual int Flush(const CompletionCallback& callback); virtual int Flush(CompletionOnceCallback callback);
private: private:
class Context; class Context;
......
...@@ -23,8 +23,8 @@ namespace net { ...@@ -23,8 +23,8 @@ namespace net {
namespace { namespace {
void CallInt64ToInt(const CompletionCallback& callback, int64_t result) { void CallInt64ToInt(CompletionOnceCallback callback, int64_t result) {
callback.Run(static_cast<int>(result)); std::move(callback).Run(static_cast<int>(result));
} }
} // namespace } // namespace
...@@ -81,30 +81,28 @@ void FileStream::Context::Orphan() { ...@@ -81,30 +81,28 @@ void FileStream::Context::Orphan() {
void FileStream::Context::Open(const base::FilePath& path, void FileStream::Context::Open(const base::FilePath& path,
int open_flags, int open_flags,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
CheckNoAsyncInProgress(); CheckNoAsyncInProgress();
bool posted = base::PostTaskAndReplyWithResult( bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(), task_runner_.get(), FROM_HERE,
FROM_HERE, base::BindOnce(&Context::OpenFileImpl, base::Unretained(this), path,
base::Bind( open_flags),
&Context::OpenFileImpl, base::Unretained(this), path, open_flags), base::BindOnce(&Context::OnOpenCompleted, base::Unretained(this),
base::Bind(&Context::OnOpenCompleted, base::Unretained(this), callback)); std::move(callback)));
DCHECK(posted); DCHECK(posted);
last_operation_ = OPEN; last_operation_ = OPEN;
async_in_progress_ = true; async_in_progress_ = true;
} }
void FileStream::Context::Close(const CompletionCallback& callback) { void FileStream::Context::Close(CompletionOnceCallback callback) {
CheckNoAsyncInProgress(); CheckNoAsyncInProgress();
bool posted = base::PostTaskAndReplyWithResult( bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(), task_runner_.get(), FROM_HERE,
FROM_HERE, base::BindOnce(&Context::CloseFileImpl, base::Unretained(this)),
base::Bind(&Context::CloseFileImpl, base::Unretained(this)), base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
base::Bind(&Context::OnAsyncCompleted, IntToInt64(std::move(callback))));
base::Unretained(this),
IntToInt64(callback)));
DCHECK(posted); DCHECK(posted);
last_operation_ = CLOSE; last_operation_ = CLOSE;
...@@ -112,13 +110,14 @@ void FileStream::Context::Close(const CompletionCallback& callback) { ...@@ -112,13 +110,14 @@ void FileStream::Context::Close(const CompletionCallback& callback) {
} }
void FileStream::Context::Seek(int64_t offset, void FileStream::Context::Seek(int64_t offset,
const Int64CompletionCallback& callback) { Int64CompletionOnceCallback callback) {
CheckNoAsyncInProgress(); CheckNoAsyncInProgress();
bool posted = base::PostTaskAndReplyWithResult( bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(), FROM_HERE, task_runner_.get(), FROM_HERE,
base::Bind(&Context::SeekFileImpl, base::Unretained(this), offset), base::BindOnce(&Context::SeekFileImpl, base::Unretained(this), offset),
base::Bind(&Context::OnAsyncCompleted, base::Unretained(this), callback)); base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
std::move(callback)));
DCHECK(posted); DCHECK(posted);
last_operation_ = SEEK; last_operation_ = SEEK;
...@@ -126,29 +125,27 @@ void FileStream::Context::Seek(int64_t offset, ...@@ -126,29 +125,27 @@ void FileStream::Context::Seek(int64_t offset,
} }
void FileStream::Context::GetFileInfo(base::File::Info* file_info, void FileStream::Context::GetFileInfo(base::File::Info* file_info,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
CheckNoAsyncInProgress(); CheckNoAsyncInProgress();
base::PostTaskAndReplyWithResult( base::PostTaskAndReplyWithResult(
task_runner_.get(), FROM_HERE, task_runner_.get(), FROM_HERE,
base::Bind(&Context::GetFileInfoImpl, base::Unretained(this), base::BindOnce(&Context::GetFileInfoImpl, base::Unretained(this),
base::Unretained(file_info)), base::Unretained(file_info)),
base::Bind(&Context::OnAsyncCompleted, base::Unretained(this), base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
IntToInt64(callback))); IntToInt64(std::move(callback))));
async_in_progress_ = true; async_in_progress_ = true;
} }
void FileStream::Context::Flush(const CompletionCallback& callback) { void FileStream::Context::Flush(CompletionOnceCallback callback) {
CheckNoAsyncInProgress(); CheckNoAsyncInProgress();
bool posted = base::PostTaskAndReplyWithResult( bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(), task_runner_.get(), FROM_HERE,
FROM_HERE, base::BindOnce(&Context::FlushFileImpl, base::Unretained(this)),
base::Bind(&Context::FlushFileImpl, base::Unretained(this)), base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
base::Bind(&Context::OnAsyncCompleted, IntToInt64(std::move(callback))));
base::Unretained(this),
IntToInt64(callback)));
DCHECK(posted); DCHECK(posted);
last_operation_ = FLUSH; last_operation_ = FLUSH;
...@@ -223,13 +220,13 @@ FileStream::Context::IOResult FileStream::Context::FlushFileImpl() { ...@@ -223,13 +220,13 @@ FileStream::Context::IOResult FileStream::Context::FlushFileImpl() {
return IOResult::FromOSError(logging::GetLastSystemErrorCode()); return IOResult::FromOSError(logging::GetLastSystemErrorCode());
} }
void FileStream::Context::OnOpenCompleted(const CompletionCallback& callback, void FileStream::Context::OnOpenCompleted(CompletionOnceCallback callback,
OpenResult open_result) { OpenResult open_result) {
file_ = std::move(open_result.file); file_ = std::move(open_result.file);
if (file_.IsValid() && !orphaned_) if (file_.IsValid() && !orphaned_)
OnFileOpened(); OnFileOpened();
OnAsyncCompleted(IntToInt64(callback), open_result.error_code); OnAsyncCompleted(IntToInt64(std::move(callback)), open_result.error_code);
} }
void FileStream::Context::CloseAndDelete() { void FileStream::Context::CloseAndDelete() {
...@@ -240,7 +237,7 @@ void FileStream::Context::CloseAndDelete() { ...@@ -240,7 +237,7 @@ void FileStream::Context::CloseAndDelete() {
if (file_.IsValid()) { if (file_.IsValid()) {
bool posted = task_runner_.get()->PostTask( bool posted = task_runner_.get()->PostTask(
FROM_HERE, base::Bind(base::IgnoreResult(&Context::CloseFileImpl), FROM_HERE, base::BindOnce(base::IgnoreResult(&Context::CloseFileImpl),
base::Owned(this))); base::Owned(this)));
DCHECK(posted); DCHECK(posted);
} else { } else {
...@@ -248,23 +245,23 @@ void FileStream::Context::CloseAndDelete() { ...@@ -248,23 +245,23 @@ void FileStream::Context::CloseAndDelete() {
} }
} }
Int64CompletionCallback FileStream::Context::IntToInt64( Int64CompletionOnceCallback FileStream::Context::IntToInt64(
const CompletionCallback& callback) { CompletionOnceCallback callback) {
return base::Bind(&CallInt64ToInt, callback); return base::BindOnce(&CallInt64ToInt, std::move(callback));
} }
void FileStream::Context::OnAsyncCompleted( void FileStream::Context::OnAsyncCompleted(Int64CompletionOnceCallback callback,
const Int64CompletionCallback& callback,
const IOResult& result) { const IOResult& result) {
// Reset this before Run() as Run() may issue a new async operation. Also it // Reset this before Run() as Run() may issue a new async operation. Also it
// should be reset before Close() because it shouldn't run if any async // should be reset before Close() because it shouldn't run if any async
// operation is in progress. // operation is in progress.
async_in_progress_ = false; async_in_progress_ = false;
last_operation_ = NONE; last_operation_ = NONE;
if (orphaned_) if (orphaned_) {
CloseAndDelete(); CloseAndDelete();
else } else {
callback.Run(result.result); std::move(callback).Run(result.result);
}
} }
} // namespace net } // namespace net
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/task_runner.h" #include "base/task_runner.h"
#include "net/base/completion_callback.h" #include "net/base/completion_once_callback.h"
#include "net/base/file_stream.h" #include "net/base/file_stream.h"
#if defined(OS_POSIX) #if defined(OS_POSIX)
...@@ -69,13 +69,9 @@ class FileStream::Context { ...@@ -69,13 +69,9 @@ class FileStream::Context {
~Context(); ~Context();
#endif #endif
int Read(IOBuffer* buf, int Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
int buf_len,
const CompletionCallback& callback);
int Write(IOBuffer* buf, int Write(IOBuffer* buf, int buf_len, CompletionOnceCallback callback);
int buf_len,
const CompletionCallback& callback);
bool async_in_progress() const { return async_in_progress_; } bool async_in_progress() const { return async_in_progress_; }
...@@ -90,17 +86,17 @@ class FileStream::Context { ...@@ -90,17 +86,17 @@ class FileStream::Context {
void Open(const base::FilePath& path, void Open(const base::FilePath& path,
int open_flags, int open_flags,
const CompletionCallback& callback); CompletionOnceCallback callback);
void Close(const CompletionCallback& callback); void Close(CompletionOnceCallback callback);
// Seeks |offset| bytes from the start of the file. // Seeks |offset| bytes from the start of the file.
void Seek(int64_t offset, const Int64CompletionCallback& callback); void Seek(int64_t offset, Int64CompletionOnceCallback callback);
void GetFileInfo(base::File::Info* file_info, void GetFileInfo(base::File::Info* file_info,
const CompletionCallback& callback); CompletionOnceCallback callback);
void Flush(const CompletionCallback& callback); void Flush(CompletionOnceCallback callback);
bool IsOpen() const; bool IsOpen() const;
...@@ -161,16 +157,15 @@ class FileStream::Context { ...@@ -161,16 +157,15 @@ class FileStream::Context {
IOResult FlushFileImpl(); IOResult FlushFileImpl();
void OnOpenCompleted(const CompletionCallback& callback, void OnOpenCompleted(CompletionOnceCallback callback, OpenResult open_result);
OpenResult open_result);
void CloseAndDelete(); void CloseAndDelete();
Int64CompletionCallback IntToInt64(const CompletionCallback& callback); Int64CompletionOnceCallback IntToInt64(CompletionOnceCallback callback);
// Called when Open() or Seek() completes. |result| contains the result or a // Called when Open() or Seek() completes. |result| contains the result or a
// network error code. // network error code.
void OnAsyncCompleted(const Int64CompletionCallback& callback, void OnAsyncCompleted(Int64CompletionOnceCallback callback,
const IOResult& result); const IOResult& result);
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
...@@ -184,7 +179,7 @@ class FileStream::Context { ...@@ -184,7 +179,7 @@ class FileStream::Context {
void OnFileOpened(); void OnFileOpened();
#if defined(OS_WIN) #if defined(OS_WIN)
void IOCompletionIsPending(const CompletionCallback& callback, IOBuffer* buf); void IOCompletionIsPending(CompletionOnceCallback callback, IOBuffer* buf);
// Implementation of MessageLoopForIO::IOHandler. // Implementation of MessageLoopForIO::IOHandler.
void OnIOCompleted(base::MessageLoopForIO::IOContext* context, void OnIOCompleted(base::MessageLoopForIO::IOContext* context,
...@@ -252,7 +247,7 @@ class FileStream::Context { ...@@ -252,7 +247,7 @@ class FileStream::Context {
#if defined(OS_WIN) #if defined(OS_WIN)
base::MessageLoopForIO::IOContext io_context_; base::MessageLoopForIO::IOContext io_context_;
CompletionCallback callback_; CompletionOnceCallback callback_;
scoped_refptr<IOBuffer> in_flight_buf_; scoped_refptr<IOBuffer> in_flight_buf_;
// This flag is set to true when we receive a Read request which is queued to // This flag is set to true when we receive a Read request which is queued to
// the thread pool. // the thread pool.
......
...@@ -39,17 +39,16 @@ FileStream::Context::~Context() = default; ...@@ -39,17 +39,16 @@ FileStream::Context::~Context() = default;
int FileStream::Context::Read(IOBuffer* in_buf, int FileStream::Context::Read(IOBuffer* in_buf,
int buf_len, int buf_len,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
CheckNoAsyncInProgress(); CheckNoAsyncInProgress();
scoped_refptr<IOBuffer> buf = in_buf; scoped_refptr<IOBuffer> buf = in_buf;
const bool posted = base::PostTaskAndReplyWithResult( const bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(), task_runner_.get(), FROM_HERE,
FROM_HERE, base::BindOnce(&Context::ReadFileImpl, base::Unretained(this), buf,
base::Bind(&Context::ReadFileImpl, base::Unretained(this), buf, buf_len), buf_len),
base::Bind(&Context::OnAsyncCompleted, base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
base::Unretained(this), IntToInt64(std::move(callback))));
IntToInt64(callback)));
DCHECK(posted); DCHECK(posted);
async_in_progress_ = true; async_in_progress_ = true;
...@@ -59,17 +58,16 @@ int FileStream::Context::Read(IOBuffer* in_buf, ...@@ -59,17 +58,16 @@ int FileStream::Context::Read(IOBuffer* in_buf,
int FileStream::Context::Write(IOBuffer* in_buf, int FileStream::Context::Write(IOBuffer* in_buf,
int buf_len, int buf_len,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
CheckNoAsyncInProgress(); CheckNoAsyncInProgress();
scoped_refptr<IOBuffer> buf = in_buf; scoped_refptr<IOBuffer> buf = in_buf;
const bool posted = base::PostTaskAndReplyWithResult( const bool posted = base::PostTaskAndReplyWithResult(
task_runner_.get(), task_runner_.get(), FROM_HERE,
FROM_HERE, base::BindOnce(&Context::WriteFileImpl, base::Unretained(this), buf,
base::Bind(&Context::WriteFileImpl, base::Unretained(this), buf, buf_len), buf_len),
base::Bind(&Context::OnAsyncCompleted, base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this),
base::Unretained(this), IntToInt64(std::move(callback))));
IntToInt64(callback)));
DCHECK(posted); DCHECK(posted);
async_in_progress_ = true; async_in_progress_ = true;
......
...@@ -68,7 +68,7 @@ FileStream::Context::~Context() { ...@@ -68,7 +68,7 @@ FileStream::Context::~Context() {
int FileStream::Context::Read(IOBuffer* buf, int FileStream::Context::Read(IOBuffer* buf,
int buf_len, int buf_len,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
CheckNoAsyncInProgress(); CheckNoAsyncInProgress();
DCHECK(!async_read_initiated_); DCHECK(!async_read_initiated_);
...@@ -76,7 +76,7 @@ int FileStream::Context::Read(IOBuffer* buf, ...@@ -76,7 +76,7 @@ int FileStream::Context::Read(IOBuffer* buf,
DCHECK(!io_complete_for_read_received_); DCHECK(!io_complete_for_read_received_);
last_operation_ = READ; last_operation_ = READ;
IOCompletionIsPending(callback, buf); IOCompletionIsPending(std::move(callback), buf);
async_read_initiated_ = true; async_read_initiated_ = true;
result_ = 0; result_ = 0;
...@@ -91,7 +91,7 @@ int FileStream::Context::Read(IOBuffer* buf, ...@@ -91,7 +91,7 @@ int FileStream::Context::Read(IOBuffer* buf,
int FileStream::Context::Write(IOBuffer* buf, int FileStream::Context::Write(IOBuffer* buf,
int buf_len, int buf_len,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
CheckNoAsyncInProgress(); CheckNoAsyncInProgress();
last_operation_ = WRITE; last_operation_ = WRITE;
...@@ -101,14 +101,15 @@ int FileStream::Context::Write(IOBuffer* buf, ...@@ -101,14 +101,15 @@ int FileStream::Context::Write(IOBuffer* buf,
if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len, if (!WriteFile(file_.GetPlatformFile(), buf->data(), buf_len,
&bytes_written, &io_context_.overlapped)) { &bytes_written, &io_context_.overlapped)) {
IOResult error = IOResult::FromOSError(GetLastError()); IOResult error = IOResult::FromOSError(GetLastError());
if (error.os_error == ERROR_IO_PENDING) if (error.os_error == ERROR_IO_PENDING) {
IOCompletionIsPending(callback, buf); IOCompletionIsPending(std::move(callback), buf);
else } else {
LOG(WARNING) << "WriteFile failed: " << error.os_error; LOG(WARNING) << "WriteFile failed: " << error.os_error;
}
return static_cast<int>(error.result); return static_cast<int>(error.result);
} }
IOCompletionIsPending(callback, buf); IOCompletionIsPending(std::move(callback), buf);
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
...@@ -125,11 +126,10 @@ void FileStream::Context::OnFileOpened() { ...@@ -125,11 +126,10 @@ void FileStream::Context::OnFileOpened() {
this); this);
} }
void FileStream::Context::IOCompletionIsPending( void FileStream::Context::IOCompletionIsPending(CompletionOnceCallback callback,
const CompletionCallback& callback,
IOBuffer* buf) { IOBuffer* buf) {
DCHECK(callback_.is_null()); DCHECK(callback_.is_null());
callback_ = callback; callback_ = std::move(callback);
in_flight_buf_ = buf; // Hold until the async operation ends. in_flight_buf_ = buf; // Hold until the async operation ends.
async_in_progress_ = true; async_in_progress_ = true;
} }
...@@ -188,11 +188,9 @@ void FileStream::Context::InvokeUserCallback() { ...@@ -188,11 +188,9 @@ void FileStream::Context::InvokeUserCallback() {
last_operation_ = NONE; last_operation_ = NONE;
async_in_progress_ = false; async_in_progress_ = false;
} }
CompletionCallback temp_callback = callback_;
callback_.Reset();
scoped_refptr<IOBuffer> temp_buf = in_flight_buf_; scoped_refptr<IOBuffer> temp_buf = in_flight_buf_;
in_flight_buf_ = NULL; in_flight_buf_ = NULL;
temp_callback.Run(result_); std::move(callback_).Run(result_);
} }
void FileStream::Context::DeleteOrphanedContext() { void FileStream::Context::DeleteOrphanedContext() {
......
...@@ -35,45 +35,44 @@ MockFileStream::MockFileStream( ...@@ -35,45 +35,44 @@ MockFileStream::MockFileStream(
MockFileStream::~MockFileStream() = default; MockFileStream::~MockFileStream() = default;
int MockFileStream::Seek(int64_t offset, int MockFileStream::Seek(int64_t offset, Int64CompletionOnceCallback callback) {
const Int64CompletionCallback& callback) { Int64CompletionOnceCallback wrapped_callback =
Int64CompletionCallback wrapped_callback = base::BindOnce(&MockFileStream::DoCallback64, weak_factory_.GetWeakPtr(),
base::Bind(&MockFileStream::DoCallback64, std::move(callback));
weak_factory_.GetWeakPtr(), callback);
if (forced_error_ == OK) if (forced_error_ == OK)
return FileStream::Seek(offset, wrapped_callback); return FileStream::Seek(offset, std::move(wrapped_callback));
return ErrorCallback64(wrapped_callback); return ErrorCallback64(std::move(wrapped_callback));
} }
int MockFileStream::Read(IOBuffer* buf, int MockFileStream::Read(IOBuffer* buf,
int buf_len, int buf_len,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, CompletionOnceCallback wrapped_callback =
weak_factory_.GetWeakPtr(), base::BindOnce(&MockFileStream::DoCallback, weak_factory_.GetWeakPtr(),
callback); std::move(callback));
if (forced_error_ == OK) if (forced_error_ == OK)
return FileStream::Read(buf, buf_len, wrapped_callback); return FileStream::Read(buf, buf_len, std::move(wrapped_callback));
return ErrorCallback(wrapped_callback); return ErrorCallback(std::move(wrapped_callback));
} }
int MockFileStream::Write(IOBuffer* buf, int MockFileStream::Write(IOBuffer* buf,
int buf_len, int buf_len,
const CompletionCallback& callback) { CompletionOnceCallback callback) {
CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, CompletionOnceCallback wrapped_callback =
weak_factory_.GetWeakPtr(), base::BindOnce(&MockFileStream::DoCallback, weak_factory_.GetWeakPtr(),
callback); std::move(callback));
if (forced_error_ == OK) if (forced_error_ == OK)
return FileStream::Write(buf, buf_len, wrapped_callback); return FileStream::Write(buf, buf_len, std::move(wrapped_callback));
return ErrorCallback(wrapped_callback); return ErrorCallback(std::move(wrapped_callback));
} }
int MockFileStream::Flush(const CompletionCallback& callback) { int MockFileStream::Flush(CompletionOnceCallback callback) {
CompletionCallback wrapped_callback = base::Bind(&MockFileStream::DoCallback, CompletionOnceCallback wrapped_callback =
weak_factory_.GetWeakPtr(), base::BindOnce(&MockFileStream::DoCallback, weak_factory_.GetWeakPtr(),
callback); std::move(callback));
if (forced_error_ == OK) if (forced_error_ == OK)
return FileStream::Flush(wrapped_callback); return FileStream::Flush(std::move(wrapped_callback));
return ErrorCallback(wrapped_callback); return ErrorCallback(std::move(wrapped_callback));
} }
void MockFileStream::ThrottleCallbacks() { void MockFileStream::ThrottleCallbacks() {
...@@ -86,37 +85,35 @@ void MockFileStream::ReleaseCallbacks() { ...@@ -86,37 +85,35 @@ void MockFileStream::ReleaseCallbacks() {
throttled_ = false; throttled_ = false;
if (!throttled_task_.is_null()) { if (!throttled_task_.is_null()) {
base::Closure throttled_task = throttled_task_; base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
throttled_task_.Reset(); std::move(throttled_task_));
base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, throttled_task);
} }
} }
void MockFileStream::DoCallback(const CompletionCallback& callback, void MockFileStream::DoCallback(CompletionOnceCallback callback, int result) {
int result) {
if (!throttled_) { if (!throttled_) {
callback.Run(result); std::move(callback).Run(result);
return; return;
} }
CHECK(throttled_task_.is_null()); CHECK(throttled_task_.is_null());
throttled_task_ = base::Bind(callback, result); throttled_task_ = base::BindOnce(std::move(callback), result);
} }
void MockFileStream::DoCallback64(const Int64CompletionCallback& callback, void MockFileStream::DoCallback64(Int64CompletionOnceCallback callback,
int64_t result) { int64_t result) {
if (!throttled_) { if (!throttled_) {
callback.Run(result); std::move(callback).Run(result);
return; return;
} }
CHECK(throttled_task_.is_null()); CHECK(throttled_task_.is_null());
throttled_task_ = base::Bind(callback, result); throttled_task_ = base::BindOnce(std::move(callback), result);
} }
int MockFileStream::ErrorCallback(const CompletionCallback& callback) { int MockFileStream::ErrorCallback(CompletionOnceCallback callback) {
CHECK_NE(OK, forced_error_); CHECK_NE(OK, forced_error_);
if (async_error_) { if (async_error_) {
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(callback, forced_error_)); FROM_HERE, base::BindOnce(std::move(callback), forced_error_));
clear_forced_error(); clear_forced_error();
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
...@@ -125,12 +122,11 @@ int MockFileStream::ErrorCallback(const CompletionCallback& callback) { ...@@ -125,12 +122,11 @@ int MockFileStream::ErrorCallback(const CompletionCallback& callback) {
return ret; return ret;
} }
int64_t MockFileStream::ErrorCallback64( int64_t MockFileStream::ErrorCallback64(Int64CompletionOnceCallback callback) {
const Int64CompletionCallback& callback) {
CHECK_NE(OK, forced_error_); CHECK_NE(OK, forced_error_);
if (async_error_) { if (async_error_) {
base::ThreadTaskRunnerHandle::Get()->PostTask( base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(callback, forced_error_)); FROM_HERE, base::BindOnce(std::move(callback), forced_error_));
clear_forced_error(); clear_forced_error();
return ERR_IO_PENDING; return ERR_IO_PENDING;
} }
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "net/base/completion_once_callback.h"
#include "net/base/file_stream.h" #include "net/base/file_stream.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
...@@ -29,14 +30,14 @@ class MockFileStream : public FileStream { ...@@ -29,14 +30,14 @@ class MockFileStream : public FileStream {
~MockFileStream() override; ~MockFileStream() override;
// FileStream methods. // FileStream methods.
int Seek(int64_t offset, const Int64CompletionCallback& callback) override; int Seek(int64_t offset, Int64CompletionOnceCallback callback) override;
int Read(IOBuffer* buf, int Read(IOBuffer* buf,
int buf_len, int buf_len,
const CompletionCallback& callback) override; CompletionOnceCallback callback) override;
int Write(IOBuffer* buf, int Write(IOBuffer* buf,
int buf_len, int buf_len,
const CompletionCallback& callback) override; CompletionOnceCallback callback) override;
int Flush(const CompletionCallback& callback) override; int Flush(CompletionOnceCallback callback) override;
void set_forced_error_async(int error) { void set_forced_error_async(int error) {
forced_error_ = error; forced_error_ = error;
...@@ -83,18 +84,18 @@ class MockFileStream : public FileStream { ...@@ -83,18 +84,18 @@ class MockFileStream : public FileStream {
// Wrappers for callbacks to make them honor ThrottleCallbacks and // Wrappers for callbacks to make them honor ThrottleCallbacks and
// ReleaseCallbacks. // ReleaseCallbacks.
void DoCallback(const CompletionCallback& callback, int result); void DoCallback(CompletionOnceCallback callback, int result);
void DoCallback64(const Int64CompletionCallback& callback, int64_t result); void DoCallback64(Int64CompletionOnceCallback callback, int64_t result);
// Depending on |async_error_|, either synchronously returns |forced_error_| // Depending on |async_error_|, either synchronously returns |forced_error_|
// asynchronously calls |callback| with |async_error_|. // asynchronously calls |callback| with |async_error_|.
int ErrorCallback(const CompletionCallback& callback); int ErrorCallback(CompletionOnceCallback callback);
int64_t ErrorCallback64(const Int64CompletionCallback& callback); int64_t ErrorCallback64(Int64CompletionOnceCallback callback);
int forced_error_; int forced_error_;
bool async_error_; bool async_error_;
bool throttled_; bool throttled_;
base::Closure throttled_task_; base::OnceClosure throttled_task_;
base::FilePath path_; base::FilePath path_;
base::WeakPtrFactory<MockFileStream> weak_factory_; base::WeakPtrFactory<MockFileStream> weak_factory_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment