Skip to content

Commit

Permalink
Use base::BindOnce for PostTask callbacks.
Browse files Browse the repository at this point in the history
TaskRunner::PostTask() takes a OnceCallback. Replace usage of
base::Bind(), which produces a RepeatingCallback, with base::BindOnce()
when the callback is created as a temporary inside of PostTask(). This
CL replaces instances of Bind() without the namespace specifier for code
in src/base/.

Bug: 714018
Change-Id: Idea07d7a7717a1b69f5f1b01973863957767baee
Reviewed-on: https://chromium-review.googlesource.com/c/1476519
Reviewed-by: danakj <danakj@chromium.org>
Commit-Queue: kylechar <kylechar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#633329}
  • Loading branch information
kylechar authored and Commit Bot committed Feb 19, 2019
1 parent 66d1271 commit 60486b0
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion base/bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ static inline internal::PassedWrapper<T> Passed(T* scoper) {
// cb->Run(1); // Prints "1".
//
// // Prints "1" on |ml|.
// ml->PostTask(FROM_HERE, Bind(IgnoreResult(&DoSomething), 1);
// ml->PostTask(FROM_HERE, BindOnce(IgnoreResult(&DoSomething), 1);
template <typename T>
static inline internal::IgnoreResultHelper<T> IgnoreResult(T data) {
return internal::IgnoreResultHelper<T>(std::move(data));
Expand Down
14 changes: 7 additions & 7 deletions base/files/file_path_watcher_fsevents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void FilePathWatcherFSEvents::FSEventsCallback(
// task_runner() and calls dispatch_sync, it is guaranteed that |watcher|
// still exists when UpdateEventStream() runs.
watcher->task_runner()->PostTask(
FROM_HERE, Bind(
FROM_HERE, BindOnce(
[](WeakPtr<FilePathWatcherFSEvents> weak_watcher,
FSEventStreamEventId root_change_at) {
if (!weak_watcher)
Expand All @@ -184,8 +184,8 @@ void FilePathWatcherFSEvents::OnFilePathsChanged(
DCHECK(!resolved_target_.empty());
task_runner()->PostTask(
FROM_HERE,
Bind(&FilePathWatcherFSEvents::DispatchEvents, weak_factory_.GetWeakPtr(),
paths, target_, resolved_target_));
BindOnce(&FilePathWatcherFSEvents::DispatchEvents,
weak_factory_.GetWeakPtr(), paths, target_, resolved_target_));
}

void FilePathWatcherFSEvents::DispatchEvents(const std::vector<FilePath>& paths,
Expand Down Expand Up @@ -242,8 +242,8 @@ void FilePathWatcherFSEvents::UpdateEventStream(

if (!FSEventStreamStart(fsevent_stream_)) {
task_runner()->PostTask(FROM_HERE,
Bind(&FilePathWatcherFSEvents::ReportError,
weak_factory_.GetWeakPtr(), target_));
BindOnce(&FilePathWatcherFSEvents::ReportError,
weak_factory_.GetWeakPtr(), target_));
}
}

Expand All @@ -253,8 +253,8 @@ bool FilePathWatcherFSEvents::ResolveTargetPath() {
resolved_target_ = resolved;
if (resolved_target_.empty()) {
task_runner()->PostTask(FROM_HERE,
Bind(&FilePathWatcherFSEvents::ReportError,
weak_factory_.GetWeakPtr(), target_));
BindOnce(&FilePathWatcherFSEvents::ReportError,
weak_factory_.GetWeakPtr(), target_));
}
return changed;
}
Expand Down
6 changes: 3 additions & 3 deletions base/ios/weak_nsobject_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object,
scoped_refptr<SingleThreadTaskRunner> runner) {
// Copy using constructor.
WeakNSObject<NSMutableData> weak_copy1(weak_object);
runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy1));
runner->PostTask(FROM_HERE, BindOnce(&TouchWeakData, weak_copy1));
// Copy using assignment operator.
WeakNSObject<NSMutableData> weak_copy2 = weak_object;
runner->PostTask(FROM_HERE, Bind(&TouchWeakData, weak_copy2));
runner->PostTask(FROM_HERE, BindOnce(&TouchWeakData, weak_copy2));
}

// Tests that the weak object can be copied on a different thread.
Expand All @@ -129,7 +129,7 @@ void CopyWeakNSObjectAndPost(const WeakNSObject<NSMutableData>& weak_object,

scoped_refptr<SingleThreadTaskRunner> runner = ThreadTaskRunnerHandle::Get();
other_thread.task_runner()->PostTask(
FROM_HERE, Bind(&CopyWeakNSObjectAndPost, weak, runner));
FROM_HERE, BindOnce(&CopyWeakNSObjectAndPost, weak, runner));
other_thread.Stop();
RunLoop().RunUntilIdle();

Expand Down
4 changes: 2 additions & 2 deletions base/memory/memory_pressure_monitor_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ void MemoryPressureMonitor::CheckMemoryPressureSoon() {
DCHECK(thread_checker_.CalledOnValidThread());

ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, Bind(&MemoryPressureMonitor::CheckMemoryPressure,
weak_ptr_factory_.GetWeakPtr()));
FROM_HERE, BindOnce(&MemoryPressureMonitor::CheckMemoryPressure,
weak_ptr_factory_.GetWeakPtr()));
}

MemoryPressureListener::MemoryPressureLevel
Expand Down
10 changes: 5 additions & 5 deletions base/process/process_util_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ MULTIPROCESS_TEST_MAIN(CheckCwdProcess) {
BindRepeating(&WaitableEvent::Signal, Unretained(&event));

// Test that a non-main thread has the right cwd.
task_runner->PostTask(FROM_HERE, Bind(&CheckCwdIsExpected, temp_dir));
task_runner->PostTask(FROM_HERE, BindOnce(&CheckCwdIsExpected, temp_dir));
task_runner->PostTask(FROM_HERE, signal_event);

event.Wait();
Expand All @@ -525,8 +525,8 @@ MULTIPROCESS_TEST_MAIN(CheckCwdProcess) {
// Change the cwd on the secondary thread. IgnoreResult is used when setting
// because it is checked immediately after.
task_runner->PostTask(FROM_HERE,
Bind(IgnoreResult(&SetCurrentDirectory), home_dir));
task_runner->PostTask(FROM_HERE, Bind(&CheckCwdIsExpected, home_dir));
BindOnce(IgnoreResult(&SetCurrentDirectory), home_dir));
task_runner->PostTask(FROM_HERE, BindOnce(&CheckCwdIsExpected, home_dir));
task_runner->PostTask(FROM_HERE, signal_event);

event.Wait();
Expand All @@ -539,14 +539,14 @@ MULTIPROCESS_TEST_MAIN(CheckCwdProcess) {
CheckCwdIsExpected(temp_dir);

// Ensure that the secondary thread sees the new cwd too.
task_runner->PostTask(FROM_HERE, Bind(&CheckCwdIsExpected, temp_dir));
task_runner->PostTask(FROM_HERE, BindOnce(&CheckCwdIsExpected, temp_dir));
task_runner->PostTask(FROM_HERE, signal_event);

event.Wait();

// Change the cwd on the secondary thread one more time and join the thread.
task_runner->PostTask(FROM_HERE,
Bind(IgnoreResult(&SetCurrentDirectory), home_dir));
BindOnce(IgnoreResult(&SetCurrentDirectory), home_dir));
thread.Stop();

// Make sure that the main thread picked up the new cwd.
Expand Down
12 changes: 6 additions & 6 deletions base/task/post_task.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ namespace base {
// This is the interface to post tasks.
//
// To post a simple one-off task with default traits:
// PostTask(FROM_HERE, Bind(...));
// PostTask(FROM_HERE, BindOnce(...));
//
// To post a high priority one-off task to respond to a user interaction:
// PostTaskWithTraits(
// FROM_HERE,
// {TaskPriority::USER_BLOCKING},
// Bind(...));
// BindOnce(...));
//
// To post tasks that must run in sequence with default traits:
// scoped_refptr<SequencedTaskRunner> task_runner =
// CreateSequencedTaskRunnerWithTraits(TaskTraits());
// task_runner.PostTask(FROM_HERE, Bind(...));
// task_runner.PostTask(FROM_HERE, Bind(...));
// task_runner->PostTask(FROM_HERE, BindOnce(...));
// task_runner->PostTask(FROM_HERE, BindOnce(...));
//
// To post tasks that may block, must run in sequence and can be skipped on
// shutdown:
// scoped_refptr<SequencedTaskRunner> task_runner =
// CreateSequencedTaskRunnerWithTraits(
// {MayBlock(), TaskShutdownBehavior::SKIP_ON_SHUTDOWN});
// task_runner.PostTask(FROM_HERE, Bind(...));
// task_runner.PostTask(FROM_HERE, Bind(...));
// task_runner->PostTask(FROM_HERE, BindOnce(...));
// task_runner->PostTask(FROM_HERE, BindOnce(...));
//
// The default traits apply to tasks that:
// (1) don't block (ref. MayBlock() and WithBaseSyncPrimitives()),
Expand Down
6 changes: 3 additions & 3 deletions base/task/sequence_manager/time_domain_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -414,9 +414,9 @@ TEST_F(TimeDomainTest, SetNextWakeUpForQueueInThePast) {
low_prio_runner->PostDelayedTask(FROM_HERE, task_2.Get(), kDelay);
high_prio_runner->PostDelayedTask(FROM_HERE, task_1.Get(), kDelay * 2);
high_prio_runner->PostTask(
FROM_HERE, Bind([](SimpleTestTickClock* clock,
TimeDelta delay) { clock->Advance(delay); },
base::Unretained(&clock), kDelay * 2));
FROM_HERE, BindOnce([](SimpleTestTickClock* clock,
TimeDelta delay) { clock->Advance(delay); },
base::Unretained(&clock), kDelay * 2));
RunLoop().RunUntilIdle();
}

Expand Down
2 changes: 1 addition & 1 deletion base/task/task_scheduler/task_scheduler_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ TEST_F(TaskSchedulerImplTest, COMSTATaskRunnersRunWithCOMSTA) {

WaitableEvent task_ran;
com_sta_task_runner->PostTask(
FROM_HERE, Bind(
FROM_HERE, BindOnce(
[](WaitableEvent* task_ran) {
win::AssertComApartmentType(win::ComApartmentType::STA);
task_ran->Signal();
Expand Down
3 changes: 2 additions & 1 deletion base/test/test_mock_time_task_runner_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "base/test/test_mock_time_task_runner.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/cancelable_callback.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
Expand Down Expand Up @@ -234,7 +235,7 @@ TEST(TestMockTimeTaskRunnerTest, RunLoopQuitFromIdle) {

TEST(TestMockTimeTaskRunnerTest, TakePendingTasks) {
auto task_runner = MakeRefCounted<TestMockTimeTaskRunner>();
task_runner->PostTask(FROM_HERE, Bind([]() {}));
task_runner->PostTask(FROM_HERE, DoNothing());
EXPECT_TRUE(task_runner->HasPendingTask());
EXPECT_EQ(1u, task_runner->TakePendingTasks().size());
EXPECT_FALSE(task_runner->HasPendingTask());
Expand Down

0 comments on commit 60486b0

Please sign in to comment.