Commit 1f2948ba authored by tzik's avatar tzik Committed by Commit Bot

Migrate base/files/file{,_util}_proxy* to OnceCallback

This replaces Callback to OnceCallback, so that users of this can pass
OnceCallback.

Change-Id: Icc4b8633fb9aa1a6c49ab06583cae985f54df597
Reviewed-on: https://chromium-review.googlesource.com/930445Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#539010}
parent a6619d74
...@@ -79,10 +79,10 @@ class GenericFileHelper : public FileHelper { ...@@ -79,10 +79,10 @@ class GenericFileHelper : public FileHelper {
error_ = File::FILE_OK; error_ = File::FILE_OK;
} }
void Reply(const FileProxy::StatusCallback& callback) { void Reply(FileProxy::StatusCallback callback) {
PassFile(); PassFile();
if (!callback.is_null()) if (!callback.is_null())
callback.Run(error_); std::move(callback).Run(error_);
} }
private: private:
...@@ -100,10 +100,10 @@ class CreateOrOpenHelper : public FileHelper { ...@@ -100,10 +100,10 @@ class CreateOrOpenHelper : public FileHelper {
error_ = file_.IsValid() ? File::FILE_OK : file_.error_details(); error_ = file_.IsValid() ? File::FILE_OK : file_.error_details();
} }
void Reply(const FileProxy::StatusCallback& callback) { void Reply(FileProxy::StatusCallback callback) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
PassFile(); PassFile();
callback.Run(error_); std::move(callback).Run(error_);
} }
private: private:
...@@ -139,10 +139,10 @@ class CreateTemporaryHelper : public FileHelper { ...@@ -139,10 +139,10 @@ class CreateTemporaryHelper : public FileHelper {
} }
} }
void Reply(const FileProxy::CreateTemporaryCallback& callback) { void Reply(FileProxy::CreateTemporaryCallback callback) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
PassFile(); PassFile();
callback.Run(error_, file_path_); std::move(callback).Run(error_, file_path_);
} }
private: private:
...@@ -161,10 +161,10 @@ class GetInfoHelper : public FileHelper { ...@@ -161,10 +161,10 @@ class GetInfoHelper : public FileHelper {
error_ = File::FILE_OK; error_ = File::FILE_OK;
} }
void Reply(const FileProxy::GetFileInfoCallback& callback) { void Reply(FileProxy::GetFileInfoCallback callback) {
PassFile(); PassFile();
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
callback.Run(error_, file_info_); std::move(callback).Run(error_, file_info_);
} }
private: private:
...@@ -186,10 +186,10 @@ class ReadHelper : public FileHelper { ...@@ -186,10 +186,10 @@ class ReadHelper : public FileHelper {
error_ = (bytes_read_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; error_ = (bytes_read_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK;
} }
void Reply(const FileProxy::ReadCallback& callback) { void Reply(FileProxy::ReadCallback callback) {
PassFile(); PassFile();
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
callback.Run(error_, buffer_.get(), bytes_read_); std::move(callback).Run(error_, buffer_.get(), bytes_read_);
} }
private: private:
...@@ -216,10 +216,10 @@ class WriteHelper : public FileHelper { ...@@ -216,10 +216,10 @@ class WriteHelper : public FileHelper {
error_ = (bytes_written_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK; error_ = (bytes_written_ < 0) ? File::FILE_ERROR_FAILED : File::FILE_OK;
} }
void Reply(const FileProxy::WriteCallback& callback) { void Reply(FileProxy::WriteCallback callback) {
PassFile(); PassFile();
if (!callback.is_null()) if (!callback.is_null())
callback.Run(error_, bytes_written_); std::move(callback).Run(error_, bytes_written_);
} }
private: private:
...@@ -241,25 +241,26 @@ FileProxy::~FileProxy() { ...@@ -241,25 +241,26 @@ FileProxy::~FileProxy() {
bool FileProxy::CreateOrOpen(const FilePath& file_path, bool FileProxy::CreateOrOpen(const FilePath& file_path,
uint32_t file_flags, uint32_t file_flags,
const StatusCallback& callback) { StatusCallback callback) {
DCHECK(!file_.IsValid()); DCHECK(!file_.IsValid());
CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File()); CreateOrOpenHelper* helper = new CreateOrOpenHelper(this, File());
return task_runner_->PostTaskAndReply( return task_runner_->PostTaskAndReply(
FROM_HERE, FROM_HERE,
BindOnce(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path, BindOnce(&CreateOrOpenHelper::RunWork, Unretained(helper), file_path,
file_flags), file_flags),
BindOnce(&CreateOrOpenHelper::Reply, Owned(helper), callback)); BindOnce(&CreateOrOpenHelper::Reply, Owned(helper), std::move(callback)));
} }
bool FileProxy::CreateTemporary(uint32_t additional_file_flags, bool FileProxy::CreateTemporary(uint32_t additional_file_flags,
const CreateTemporaryCallback& callback) { CreateTemporaryCallback callback) {
DCHECK(!file_.IsValid()); DCHECK(!file_.IsValid());
CreateTemporaryHelper* helper = new CreateTemporaryHelper(this, File()); CreateTemporaryHelper* helper = new CreateTemporaryHelper(this, File());
return task_runner_->PostTaskAndReply( return task_runner_->PostTaskAndReply(
FROM_HERE, FROM_HERE,
BindOnce(&CreateTemporaryHelper::RunWork, Unretained(helper), BindOnce(&CreateTemporaryHelper::RunWork, Unretained(helper),
additional_file_flags), additional_file_flags),
BindOnce(&CreateTemporaryHelper::Reply, Owned(helper), callback)); BindOnce(&CreateTemporaryHelper::Reply, Owned(helper),
std::move(callback)));
} }
bool FileProxy::IsValid() const { bool FileProxy::IsValid() const {
...@@ -283,25 +284,23 @@ PlatformFile FileProxy::GetPlatformFile() const { ...@@ -283,25 +284,23 @@ PlatformFile FileProxy::GetPlatformFile() const {
return file_.GetPlatformFile(); return file_.GetPlatformFile();
} }
bool FileProxy::Close(const StatusCallback& callback) { bool FileProxy::Close(StatusCallback callback) {
DCHECK(file_.IsValid()); DCHECK(file_.IsValid());
GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_));
return task_runner_->PostTaskAndReply( return task_runner_->PostTaskAndReply(
FROM_HERE, BindOnce(&GenericFileHelper::Close, Unretained(helper)), FROM_HERE, BindOnce(&GenericFileHelper::Close, Unretained(helper)),
BindOnce(&GenericFileHelper::Reply, Owned(helper), callback)); BindOnce(&GenericFileHelper::Reply, Owned(helper), std::move(callback)));
} }
bool FileProxy::GetInfo(const GetFileInfoCallback& callback) { bool FileProxy::GetInfo(GetFileInfoCallback callback) {
DCHECK(file_.IsValid()); DCHECK(file_.IsValid());
GetInfoHelper* helper = new GetInfoHelper(this, std::move(file_)); GetInfoHelper* helper = new GetInfoHelper(this, std::move(file_));
return task_runner_->PostTaskAndReply( return task_runner_->PostTaskAndReply(
FROM_HERE, BindOnce(&GetInfoHelper::RunWork, Unretained(helper)), FROM_HERE, BindOnce(&GetInfoHelper::RunWork, Unretained(helper)),
BindOnce(&GetInfoHelper::Reply, Owned(helper), callback)); BindOnce(&GetInfoHelper::Reply, Owned(helper), std::move(callback)));
} }
bool FileProxy::Read(int64_t offset, bool FileProxy::Read(int64_t offset, int bytes_to_read, ReadCallback callback) {
int bytes_to_read,
const ReadCallback& callback) {
DCHECK(file_.IsValid()); DCHECK(file_.IsValid());
if (bytes_to_read < 0) if (bytes_to_read < 0)
return false; return false;
...@@ -309,13 +308,13 @@ bool FileProxy::Read(int64_t offset, ...@@ -309,13 +308,13 @@ bool FileProxy::Read(int64_t offset,
ReadHelper* helper = new ReadHelper(this, std::move(file_), bytes_to_read); ReadHelper* helper = new ReadHelper(this, std::move(file_), bytes_to_read);
return task_runner_->PostTaskAndReply( return task_runner_->PostTaskAndReply(
FROM_HERE, BindOnce(&ReadHelper::RunWork, Unretained(helper), offset), FROM_HERE, BindOnce(&ReadHelper::RunWork, Unretained(helper), offset),
BindOnce(&ReadHelper::Reply, Owned(helper), callback)); BindOnce(&ReadHelper::Reply, Owned(helper), std::move(callback)));
} }
bool FileProxy::Write(int64_t offset, bool FileProxy::Write(int64_t offset,
const char* buffer, const char* buffer,
int bytes_to_write, int bytes_to_write,
const WriteCallback& callback) { WriteCallback callback) {
DCHECK(file_.IsValid()); DCHECK(file_.IsValid());
if (bytes_to_write <= 0 || buffer == nullptr) if (bytes_to_write <= 0 || buffer == nullptr)
return false; return false;
...@@ -324,36 +323,36 @@ bool FileProxy::Write(int64_t offset, ...@@ -324,36 +323,36 @@ bool FileProxy::Write(int64_t offset,
new WriteHelper(this, std::move(file_), buffer, bytes_to_write); new WriteHelper(this, std::move(file_), buffer, bytes_to_write);
return task_runner_->PostTaskAndReply( return task_runner_->PostTaskAndReply(
FROM_HERE, BindOnce(&WriteHelper::RunWork, Unretained(helper), offset), FROM_HERE, BindOnce(&WriteHelper::RunWork, Unretained(helper), offset),
BindOnce(&WriteHelper::Reply, Owned(helper), callback)); BindOnce(&WriteHelper::Reply, Owned(helper), std::move(callback)));
} }
bool FileProxy::SetTimes(Time last_access_time, bool FileProxy::SetTimes(Time last_access_time,
Time last_modified_time, Time last_modified_time,
const StatusCallback& callback) { StatusCallback callback) {
DCHECK(file_.IsValid()); DCHECK(file_.IsValid());
GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_));
return task_runner_->PostTaskAndReply( return task_runner_->PostTaskAndReply(
FROM_HERE, FROM_HERE,
BindOnce(&GenericFileHelper::SetTimes, Unretained(helper), BindOnce(&GenericFileHelper::SetTimes, Unretained(helper),
last_access_time, last_modified_time), last_access_time, last_modified_time),
BindOnce(&GenericFileHelper::Reply, Owned(helper), callback)); BindOnce(&GenericFileHelper::Reply, Owned(helper), std::move(callback)));
} }
bool FileProxy::SetLength(int64_t length, const StatusCallback& callback) { bool FileProxy::SetLength(int64_t length, StatusCallback callback) {
DCHECK(file_.IsValid()); DCHECK(file_.IsValid());
GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_));
return task_runner_->PostTaskAndReply( return task_runner_->PostTaskAndReply(
FROM_HERE, FROM_HERE,
BindOnce(&GenericFileHelper::SetLength, Unretained(helper), length), BindOnce(&GenericFileHelper::SetLength, Unretained(helper), length),
BindOnce(&GenericFileHelper::Reply, Owned(helper), callback)); BindOnce(&GenericFileHelper::Reply, Owned(helper), std::move(callback)));
} }
bool FileProxy::Flush(const StatusCallback& callback) { bool FileProxy::Flush(StatusCallback callback) {
DCHECK(file_.IsValid()); DCHECK(file_.IsValid());
GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_)); GenericFileHelper* helper = new GenericFileHelper(this, std::move(file_));
return task_runner_->PostTaskAndReply( return task_runner_->PostTaskAndReply(
FROM_HERE, BindOnce(&GenericFileHelper::Flush, Unretained(helper)), FROM_HERE, BindOnce(&GenericFileHelper::Flush, Unretained(helper)),
BindOnce(&GenericFileHelper::Reply, Owned(helper), callback)); BindOnce(&GenericFileHelper::Reply, Owned(helper), std::move(callback)));
} }
} // namespace base } // namespace base
...@@ -40,17 +40,14 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { ...@@ -40,17 +40,14 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
// This callback is used by methods that report only an error code. It is // This callback is used by methods that report only an error code. It is
// valid to pass a null callback to some functions that takes a // valid to pass a null callback to some functions that takes a
// StatusCallback, in which case the operation will complete silently. // StatusCallback, in which case the operation will complete silently.
typedef Callback<void(File::Error)> StatusCallback; using StatusCallback = OnceCallback<void(File::Error)>;
using CreateTemporaryCallback =
typedef Callback<void(File::Error, OnceCallback<void(File::Error, const FilePath&)>;
const FilePath&)> CreateTemporaryCallback; using GetFileInfoCallback =
typedef Callback<void(File::Error, OnceCallback<void(File::Error, const File::Info&)>;
const File::Info&)> GetFileInfoCallback; using ReadCallback =
typedef Callback<void(File::Error, OnceCallback<void(File::Error, const char* data, int bytes_read)>;
const char* data, using WriteCallback = OnceCallback<void(File::Error, int bytes_written)>;
int bytes_read)> ReadCallback;
typedef Callback<void(File::Error,
int bytes_written)> WriteCallback;
FileProxy(); FileProxy();
explicit FileProxy(TaskRunner* task_runner); explicit FileProxy(TaskRunner* task_runner);
...@@ -64,7 +61,7 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { ...@@ -64,7 +61,7 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
// This returns false if task posting to |task_runner| has failed. // This returns false if task posting to |task_runner| has failed.
bool CreateOrOpen(const FilePath& file_path, bool CreateOrOpen(const FilePath& file_path,
uint32_t file_flags, uint32_t file_flags,
const StatusCallback& callback); StatusCallback callback);
// Creates a temporary file for writing. The path and an open file are // Creates a temporary file for writing. The path and an open file are
// returned. It is invalid to pass a null callback. The additional file flags // returned. It is invalid to pass a null callback. The additional file flags
...@@ -75,7 +72,7 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { ...@@ -75,7 +72,7 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
// //
// This returns false if task posting to |task_runner| has failed. // This returns false if task posting to |task_runner| has failed.
bool CreateTemporary(uint32_t additional_file_flags, bool CreateTemporary(uint32_t additional_file_flags,
const CreateTemporaryCallback& callback); CreateTemporaryCallback callback);
// Returns true if the underlying |file_| is valid. // Returns true if the underlying |file_| is valid.
bool IsValid() const; bool IsValid() const;
...@@ -98,16 +95,16 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { ...@@ -98,16 +95,16 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
// Proxies File::Close. The callback can be null. // Proxies File::Close. The callback can be null.
// This returns false if task posting to |task_runner| has failed. // This returns false if task posting to |task_runner| has failed.
bool Close(const StatusCallback& callback); bool Close(StatusCallback callback);
// Proxies File::GetInfo. The callback can't be null. // Proxies File::GetInfo. The callback can't be null.
// This returns false if task posting to |task_runner| has failed. // This returns false if task posting to |task_runner| has failed.
bool GetInfo(const GetFileInfoCallback& callback); bool GetInfo(GetFileInfoCallback callback);
// Proxies File::Read. The callback can't be null. // Proxies File::Read. The callback can't be null.
// This returns false if |bytes_to_read| is less than zero, or // This returns false if |bytes_to_read| is less than zero, or
// if task posting to |task_runner| has failed. // if task posting to |task_runner| has failed.
bool Read(int64_t offset, int bytes_to_read, const ReadCallback& callback); bool Read(int64_t offset, int bytes_to_read, ReadCallback callback);
// Proxies File::Write. The callback can be null. // Proxies File::Write. The callback can be null.
// This returns false if |bytes_to_write| is less than or equal to zero, // This returns false if |bytes_to_write| is less than or equal to zero,
...@@ -115,21 +112,21 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> { ...@@ -115,21 +112,21 @@ class BASE_EXPORT FileProxy : public SupportsWeakPtr<FileProxy> {
bool Write(int64_t offset, bool Write(int64_t offset,
const char* buffer, const char* buffer,
int bytes_to_write, int bytes_to_write,
const WriteCallback& callback); WriteCallback callback);
// Proxies File::SetTimes. The callback can be null. // Proxies File::SetTimes. The callback can be null.
// This returns false if task posting to |task_runner| has failed. // This returns false if task posting to |task_runner| has failed.
bool SetTimes(Time last_access_time, bool SetTimes(Time last_access_time,
Time last_modified_time, Time last_modified_time,
const StatusCallback& callback); StatusCallback callback);
// Proxies File::SetLength. The callback can be null. // Proxies File::SetLength. The callback can be null.
// This returns false if task posting to |task_runner| has failed. // This returns false if task posting to |task_runner| has failed.
bool SetLength(int64_t length, const StatusCallback& callback); bool SetLength(int64_t length, StatusCallback callback);
// Proxies File::Flush. The callback can be null. // Proxies File::Flush. The callback can be null.
// This returns false if task posting to |task_runner| has failed. // This returns false if task posting to |task_runner| has failed.
bool Flush(const StatusCallback& callback); bool Flush(StatusCallback callback);
private: private:
friend class FileHelper; friend class FileHelper;
......
...@@ -78,8 +78,9 @@ class FileProxyTest : public testing::Test { ...@@ -78,8 +78,9 @@ class FileProxyTest : public testing::Test {
protected: protected:
void CreateProxy(uint32_t flags, FileProxy* proxy) { void CreateProxy(uint32_t flags, FileProxy* proxy) {
proxy->CreateOrOpen(TestPath(), flags, Bind(&FileProxyTest::DidCreateOrOpen, proxy->CreateOrOpen(
weak_factory_.GetWeakPtr())); TestPath(), flags,
BindOnce(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_TRUE(proxy->IsValid()); EXPECT_TRUE(proxy->IsValid());
} }
...@@ -106,7 +107,7 @@ TEST_F(FileProxyTest, CreateOrOpen_Create) { ...@@ -106,7 +107,7 @@ TEST_F(FileProxyTest, CreateOrOpen_Create) {
FileProxy proxy(file_task_runner()); FileProxy proxy(file_task_runner());
proxy.CreateOrOpen( proxy.CreateOrOpen(
TestPath(), File::FLAG_CREATE | File::FLAG_READ, TestPath(), File::FLAG_CREATE | File::FLAG_READ,
Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); BindOnce(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(File::FILE_OK, error_); EXPECT_EQ(File::FILE_OK, error_);
...@@ -124,7 +125,7 @@ TEST_F(FileProxyTest, CreateOrOpen_Open) { ...@@ -124,7 +125,7 @@ TEST_F(FileProxyTest, CreateOrOpen_Open) {
FileProxy proxy(file_task_runner()); FileProxy proxy(file_task_runner());
proxy.CreateOrOpen( proxy.CreateOrOpen(
TestPath(), File::FLAG_OPEN | File::FLAG_READ, TestPath(), File::FLAG_OPEN | File::FLAG_READ,
Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); BindOnce(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(File::FILE_OK, error_); EXPECT_EQ(File::FILE_OK, error_);
...@@ -136,7 +137,7 @@ TEST_F(FileProxyTest, CreateOrOpen_OpenNonExistent) { ...@@ -136,7 +137,7 @@ TEST_F(FileProxyTest, CreateOrOpen_OpenNonExistent) {
FileProxy proxy(file_task_runner()); FileProxy proxy(file_task_runner());
proxy.CreateOrOpen( proxy.CreateOrOpen(
TestPath(), File::FLAG_OPEN | File::FLAG_READ, TestPath(), File::FLAG_OPEN | File::FLAG_READ,
Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); BindOnce(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, error_); EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, error_);
EXPECT_FALSE(proxy.IsValid()); EXPECT_FALSE(proxy.IsValid());
...@@ -150,7 +151,7 @@ TEST_F(FileProxyTest, CreateOrOpen_AbandonedCreate) { ...@@ -150,7 +151,7 @@ TEST_F(FileProxyTest, CreateOrOpen_AbandonedCreate) {
FileProxy proxy(file_task_runner()); FileProxy proxy(file_task_runner());
proxy.CreateOrOpen( proxy.CreateOrOpen(
TestPath(), File::FLAG_CREATE | File::FLAG_READ, TestPath(), File::FLAG_CREATE | File::FLAG_READ,
Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr())); BindOnce(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
} }
RunLoop().Run(); RunLoop().Run();
ThreadRestrictions::SetIOAllowed(prev); ThreadRestrictions::SetIOAllowed(prev);
...@@ -168,7 +169,7 @@ TEST_F(FileProxyTest, Close) { ...@@ -168,7 +169,7 @@ TEST_F(FileProxyTest, Close) {
EXPECT_FALSE(base::Move(TestPath(), TestDirPath().AppendASCII("new"))); EXPECT_FALSE(base::Move(TestPath(), TestDirPath().AppendASCII("new")));
#endif #endif
proxy.Close(Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); proxy.Close(BindOnce(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(File::FILE_OK, error_); EXPECT_EQ(File::FILE_OK, error_);
EXPECT_FALSE(proxy.IsValid()); EXPECT_FALSE(proxy.IsValid());
...@@ -180,9 +181,9 @@ TEST_F(FileProxyTest, Close) { ...@@ -180,9 +181,9 @@ TEST_F(FileProxyTest, Close) {
TEST_F(FileProxyTest, CreateTemporary) { TEST_F(FileProxyTest, CreateTemporary) {
{ {
FileProxy proxy(file_task_runner()); FileProxy proxy(file_task_runner());
proxy.CreateTemporary( proxy.CreateTemporary(0 /* additional_file_flags */,
0 /* additional_file_flags */, BindOnce(&FileProxyTest::DidCreateTemporary,
Bind(&FileProxyTest::DidCreateTemporary, weak_factory_.GetWeakPtr())); weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_TRUE(proxy.IsValid()); EXPECT_TRUE(proxy.IsValid());
...@@ -191,7 +192,7 @@ TEST_F(FileProxyTest, CreateTemporary) { ...@@ -191,7 +192,7 @@ TEST_F(FileProxyTest, CreateTemporary) {
// The file should be writable. // The file should be writable.
proxy.Write(0, "test", 4, proxy.Write(0, "test", 4,
Bind(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr())); BindOnce(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(File::FILE_OK, error_); EXPECT_EQ(File::FILE_OK, error_);
EXPECT_EQ(4, bytes_written_); EXPECT_EQ(4, bytes_written_);
...@@ -247,7 +248,7 @@ TEST_F(FileProxyTest, GetInfo) { ...@@ -247,7 +248,7 @@ TEST_F(FileProxyTest, GetInfo) {
FileProxy proxy(file_task_runner()); FileProxy proxy(file_task_runner());
CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy); CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy);
proxy.GetInfo( proxy.GetInfo(
Bind(&FileProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr())); BindOnce(&FileProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
// Verify. // Verify.
...@@ -270,7 +271,8 @@ TEST_F(FileProxyTest, Read) { ...@@ -270,7 +271,8 @@ TEST_F(FileProxyTest, Read) {
FileProxy proxy(file_task_runner()); FileProxy proxy(file_task_runner());
CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy); CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy);
proxy.Read(0, 128, Bind(&FileProxyTest::DidRead, weak_factory_.GetWeakPtr())); proxy.Read(0, 128,
BindOnce(&FileProxyTest::DidRead, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
// Verify. // Verify.
...@@ -288,14 +290,14 @@ TEST_F(FileProxyTest, WriteAndFlush) { ...@@ -288,14 +290,14 @@ TEST_F(FileProxyTest, WriteAndFlush) {
const char data[] = "foo!"; const char data[] = "foo!";
int data_bytes = arraysize(data); int data_bytes = arraysize(data);
proxy.Write(0, data, data_bytes, proxy.Write(0, data, data_bytes,
Bind(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr())); BindOnce(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(File::FILE_OK, error_); EXPECT_EQ(File::FILE_OK, error_);
EXPECT_EQ(data_bytes, bytes_written_); EXPECT_EQ(data_bytes, bytes_written_);
// Flush the written data. (So that the following read should always // Flush the written data. (So that the following read should always
// succeed. On some platforms it may work with or without this flush.) // succeed. On some platforms it may work with or without this flush.)
proxy.Flush(Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); proxy.Flush(BindOnce(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(File::FILE_OK, error_); EXPECT_EQ(File::FILE_OK, error_);
...@@ -322,8 +324,9 @@ TEST_F(FileProxyTest, MAYBE_SetTimes) { ...@@ -322,8 +324,9 @@ TEST_F(FileProxyTest, MAYBE_SetTimes) {
Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345); Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345);
Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765); Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765);
proxy.SetTimes(last_accessed_time, last_modified_time, proxy.SetTimes(
Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); last_accessed_time, last_modified_time,
BindOnce(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(File::FILE_OK, error_); EXPECT_EQ(File::FILE_OK, error_);
...@@ -349,8 +352,8 @@ TEST_F(FileProxyTest, SetLength_Shrink) { ...@@ -349,8 +352,8 @@ TEST_F(FileProxyTest, SetLength_Shrink) {
// Run. // Run.
FileProxy proxy(file_task_runner()); FileProxy proxy(file_task_runner());
CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy); CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy);
proxy.SetLength(7, proxy.SetLength(
Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); 7, BindOnce(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
// Verify. // Verify.
...@@ -375,8 +378,8 @@ TEST_F(FileProxyTest, SetLength_Expand) { ...@@ -375,8 +378,8 @@ TEST_F(FileProxyTest, SetLength_Expand) {
// Run. // Run.
FileProxy proxy(file_task_runner()); FileProxy proxy(file_task_runner());
CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy); CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy);
proxy.SetLength(53, proxy.SetLength(
Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr())); 53, BindOnce(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
// Verify. // Verify.
......
...@@ -16,10 +16,10 @@ namespace base { ...@@ -16,10 +16,10 @@ namespace base {
namespace { namespace {
void CallWithTranslatedParameter(const FileUtilProxy::StatusCallback& callback, void CallWithTranslatedParameter(FileUtilProxy::StatusCallback callback,
bool value) { bool value) {
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
callback.Run(value ? File::FILE_OK : File::FILE_ERROR_FAILED); std::move(callback).Run(value ? File::FILE_OK : File::FILE_ERROR_FAILED);
} }
class GetFileInfoHelper { class GetFileInfoHelper {
...@@ -36,9 +36,9 @@ class GetFileInfoHelper { ...@@ -36,9 +36,9 @@ class GetFileInfoHelper {
error_ = File::FILE_ERROR_FAILED; error_ = File::FILE_ERROR_FAILED;
} }
void Reply(const FileUtilProxy::GetFileInfoCallback& callback) { void Reply(FileUtilProxy::GetFileInfoCallback callback) {
if (!callback.is_null()) { if (!callback.is_null()) {
callback.Run(error_, file_info_); std::move(callback).Run(error_, file_info_);
} }
} }
...@@ -52,30 +52,27 @@ class GetFileInfoHelper { ...@@ -52,30 +52,27 @@ class GetFileInfoHelper {
// Retrieves the information about a file. It is invalid to pass NULL for the // Retrieves the information about a file. It is invalid to pass NULL for the
// callback. // callback.
bool FileUtilProxy::GetFileInfo( bool FileUtilProxy::GetFileInfo(TaskRunner* task_runner,
TaskRunner* task_runner, const FilePath& file_path,
const FilePath& file_path, GetFileInfoCallback callback) {
const GetFileInfoCallback& callback) {
GetFileInfoHelper* helper = new GetFileInfoHelper; GetFileInfoHelper* helper = new GetFileInfoHelper;
return task_runner->PostTaskAndReply( return task_runner->PostTaskAndReply(
FROM_HERE, FROM_HERE,
BindOnce(&GetFileInfoHelper::RunWorkForFilePath, Unretained(helper), BindOnce(&GetFileInfoHelper::RunWorkForFilePath, Unretained(helper),
file_path), file_path),
BindOnce(&GetFileInfoHelper::Reply, Owned(helper), callback)); BindOnce(&GetFileInfoHelper::Reply, Owned(helper), std::move(callback)));
} }
// static // static
bool FileUtilProxy::Touch( bool FileUtilProxy::Touch(TaskRunner* task_runner,
TaskRunner* task_runner, const FilePath& file_path,
const FilePath& file_path, const Time& last_access_time,
const Time& last_access_time, const Time& last_modified_time,
const Time& last_modified_time, StatusCallback callback) {
const StatusCallback& callback) {
return base::PostTaskAndReplyWithResult( return base::PostTaskAndReplyWithResult(
task_runner, task_runner, FROM_HERE,
FROM_HERE, BindOnce(&TouchFile, file_path, last_access_time, last_modified_time),
Bind(&TouchFile, file_path, last_access_time, last_modified_time), BindOnce(&CallWithTranslatedParameter, std::move(callback)));
Bind(&CallWithTranslatedParameter, callback));
} }
} // namespace base } // namespace base
...@@ -22,27 +22,25 @@ class BASE_EXPORT FileUtilProxy { ...@@ -22,27 +22,25 @@ class BASE_EXPORT FileUtilProxy {
// This callback is used by methods that report only an error code. It is // This callback is used by methods that report only an error code. It is
// valid to pass a null callback to any function that takes a StatusCallback, // valid to pass a null callback to any function that takes a StatusCallback,
// in which case the operation will complete silently. // in which case the operation will complete silently.
typedef Callback<void(File::Error)> StatusCallback; using StatusCallback = OnceCallback<void(File::Error)>;
typedef Callback<void(File::Error, using GetFileInfoCallback =
const File::Info&)> GetFileInfoCallback; OnceCallback<void(File::Error, const File::Info&)>;
// Retrieves the information about a file. It is invalid to pass a null // Retrieves the information about a file. It is invalid to pass a null
// callback. // callback.
// This returns false if task posting to |task_runner| has failed. // This returns false if task posting to |task_runner| has failed.
static bool GetFileInfo( static bool GetFileInfo(TaskRunner* task_runner,
TaskRunner* task_runner, const FilePath& file_path,
const FilePath& file_path, GetFileInfoCallback callback);
const GetFileInfoCallback& callback);
// Touches a file. The callback can be null. // Touches a file. The callback can be null.
// This returns false if task posting to |task_runner| has failed. // This returns false if task posting to |task_runner| has failed.
static bool Touch( static bool Touch(TaskRunner* task_runner,
TaskRunner* task_runner, const FilePath& file_path,
const FilePath& file_path, const Time& last_access_time,
const Time& last_access_time, const Time& last_modified_time,
const Time& last_modified_time, StatusCallback callback);
const StatusCallback& callback);
private: private:
DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy); DISALLOW_IMPLICIT_CONSTRUCTORS(FileUtilProxy);
......
...@@ -65,7 +65,7 @@ TEST_F(FileUtilProxyTest, GetFileInfo_File) { ...@@ -65,7 +65,7 @@ TEST_F(FileUtilProxyTest, GetFileInfo_File) {
// Run. // Run.
FileUtilProxy::GetFileInfo( FileUtilProxy::GetFileInfo(
file_task_runner(), TestPath(), file_task_runner(), TestPath(),
Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr())); BindOnce(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
// Verify. // Verify.
...@@ -87,7 +87,7 @@ TEST_F(FileUtilProxyTest, GetFileInfo_Directory) { ...@@ -87,7 +87,7 @@ TEST_F(FileUtilProxyTest, GetFileInfo_Directory) {
// Run. // Run.
FileUtilProxy::GetFileInfo( FileUtilProxy::GetFileInfo(
file_task_runner(), TestPath(), file_task_runner(), TestPath(),
Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr())); BindOnce(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
// Verify. // Verify.
...@@ -107,7 +107,7 @@ TEST_F(FileUtilProxyTest, Touch) { ...@@ -107,7 +107,7 @@ TEST_F(FileUtilProxyTest, Touch) {
FileUtilProxy::Touch( FileUtilProxy::Touch(
file_task_runner(), TestPath(), last_accessed_time, last_modified_time, file_task_runner(), TestPath(), last_accessed_time, last_modified_time,
Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr())); BindOnce(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
RunLoop().Run(); RunLoop().Run();
EXPECT_EQ(File::FILE_OK, error_); EXPECT_EQ(File::FILE_OK, error_);
......
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