Skip to content

Commit

Permalink
Convert Callback -> {Once,Repeating}Callback in mojo/public/cpp/bindings
Browse files Browse the repository at this point in the history
Also convert usages of Bind -> Bind{Once,Repeating}.

Bug: 1007814
Change-Id: I205f9a8ff20900eb42e8b2225a1ca3fb49a4a989
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1902178
Commit-Queue: Anand Mistry <amistry@chromium.org>
Reviewed-by: Ken Rockot <rockot@google.com>
Cr-Commit-Position: refs/heads/master@{#713691}
  • Loading branch information
akmistry authored and Commit Bot committed Nov 8, 2019
1 parent 2bdbfa9 commit ff664b7
Show file tree
Hide file tree
Showing 38 changed files with 221 additions and 219 deletions.
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ Pipe disconnection may be caused by:
Regardless of the underlying cause, when a connection error is encountered on
a receiver endpoint, that endpoint's **disconnect handler** (if set) is
invoked. This handler is a simple `base::Closure` and may only be invoked
invoked. This handler is a simple `base::OnceClosure` and may only be invoked
*once* as long as the endpoint is bound to the same pipe. Typically clients and
implementations use this handler to do some kind of cleanup or -- particuarly if
the error was unexpected -- create a new pipe and attempt to establish a new
Expand Down Expand Up @@ -1733,7 +1733,7 @@ versioned Mojom types.
version:
```cpp
void QueryVersion(const base::Callback<void(uint32_t)>& callback);
void QueryVersion(base::OnceCallback<void(uint32_t)> callback);
```

This queries the remote endpoint for the version number of its binding. When a
Expand Down
2 changes: 1 addition & 1 deletion mojo/public/cpp/bindings/associated_group.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE) AssociatedGroup {
AssociatedGroupController* GetController();

private:
base::Callback<AssociatedGroupController*()> controller_getter_;
base::RepeatingCallback<AssociatedGroupController*()> controller_getter_;
scoped_refptr<AssociatedGroupController> controller_;
};

Expand Down
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/associated_interface_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ class AssociatedInterfacePtr {
// Queries the max version that the remote side supports. On completion, the
// result will be returned as the input of |callback|. The version number of
// this object will also be updated.
void QueryVersion(const base::Callback<void(uint32_t)>& callback) {
internal_state_.QueryVersion(callback);
void QueryVersion(base::OnceCallback<void(uint32_t)> callback) {
internal_state_.QueryVersion(std::move(callback));
}

// If the remote side doesn't support the specified version, it will close the
Expand Down
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/interface_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ class InterfacePtr {
// Queries the max version that the remote side supports. On completion, the
// result will be returned as the input of |callback|. The version number of
// this interface pointer will also be updated.
void QueryVersion(const base::Callback<void(uint32_t)>& callback) {
internal_state_.QueryVersionDeprecated(callback);
void QueryVersion(base::OnceCallback<void(uint32_t)> callback) {
internal_state_.QueryVersion(std::move(callback));
}

// If the remote side doesn't support the specified version, it will close its
Expand Down
2 changes: 1 addition & 1 deletion mojo/public/cpp/bindings/interface_ptr_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class PtrSet {
class Element {
public:
explicit Element(Ptr<Interface> ptr) : ptr_(std::move(ptr)) {
ptr_.set_connection_error_handler(base::Bind(&DeleteElement, this));
ptr_.set_connection_error_handler(base::BindOnce(&DeleteElement, this));
}

~Element() {}
Expand Down
10 changes: 5 additions & 5 deletions mojo/public/cpp/bindings/lib/associated_interface_ptr_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ AssociatedInterfacePtrStateBase::AssociatedInterfacePtrStateBase() = default;
AssociatedInterfacePtrStateBase::~AssociatedInterfacePtrStateBase() = default;

void AssociatedInterfacePtrStateBase::QueryVersion(
const base::Callback<void(uint32_t)>& callback) {
base::OnceCallback<void(uint32_t)> callback) {
// It is safe to capture |this| because the callback won't be run after this
// object goes away.
endpoint_client_->QueryVersion(
base::Bind(&AssociatedInterfacePtrStateBase::OnQueryVersion,
base::Unretained(this), callback));
base::BindOnce(&AssociatedInterfacePtrStateBase::OnQueryVersion,
base::Unretained(this), std::move(callback)));
}

void AssociatedInterfacePtrStateBase::RequireVersion(uint32_t version) {
Expand All @@ -31,10 +31,10 @@ void AssociatedInterfacePtrStateBase::RequireVersion(uint32_t version) {
}

void AssociatedInterfacePtrStateBase::OnQueryVersion(
const base::Callback<void(uint32_t)>& callback,
base::OnceCallback<void(uint32_t)> callback,
uint32_t version) {
version_ = version;
callback.Run(version);
std::move(callback).Run(version);
}

void AssociatedInterfacePtrStateBase::FlushForTesting() {
Expand Down
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/lib/associated_interface_ptr_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) AssociatedInterfacePtrStateBase {

uint32_t version() const { return version_; }

void QueryVersion(const base::Callback<void(uint32_t)>& callback);
void QueryVersion(base::OnceCallback<void(uint32_t)> callback);
void RequireVersion(uint32_t version);
void FlushForTesting();
void CloseWithReason(uint32_t custom_reason, const std::string& description);
Expand Down Expand Up @@ -93,7 +93,7 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) AssociatedInterfacePtrStateBase {
InterfaceEndpointClient* endpoint_client() { return endpoint_client_.get(); }

private:
void OnQueryVersion(const base::Callback<void(uint32_t)>& callback,
void OnQueryVersion(base::OnceCallback<void(uint32_t)> callback,
uint32_t version);

std::unique_ptr<InterfaceEndpointClient> endpoint_client_;
Expand Down
3 changes: 2 additions & 1 deletion mojo/public/cpp/bindings/lib/connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ void Connector::WaitToReadMore() {
handle_watcher_->set_heap_profiler_tag(heap_profiler_tag_);
MojoResult rv = handle_watcher_->Watch(
message_pipe_.get(), MOJO_HANDLE_SIGNAL_READABLE,
base::Bind(&Connector::OnWatcherHandleReady, base::Unretained(this)));
base::BindRepeating(&Connector::OnWatcherHandleReady,
base::Unretained(this)));

if (message_pipe_.is_valid()) {
peer_remoteness_tracker_.emplace(
Expand Down
2 changes: 1 addition & 1 deletion mojo/public/cpp/bindings/lib/interface_endpoint_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ InterfaceEndpointClient::InterfaceEndpointClient(
dispatcher_.SetValidator(std::move(payload_validator));

if (handle_.pending_association()) {
handle_.SetAssociationEventHandler(base::Bind(
handle_.SetAssociationEventHandler(base::BindOnce(
&InterfaceEndpointClient::OnAssociationEvent, base::Unretained(this)));
} else {
InitControllerIfNecessary();
Expand Down
4 changes: 0 additions & 4 deletions mojo/public/cpp/bindings/lib/interface_ptr_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,6 @@ class InterfacePtrState : public InterfacePtrStateBase {
#endif
}

void QueryVersionDeprecated(const base::Callback<void(uint32_t)>& callback) {
QueryVersion(base::BindOnce(callback));
}

void QueryVersion(base::OnceCallback<void(uint32_t)> callback) {
ConfigureProxyIfNecessary();
InterfacePtrStateBase::QueryVersion(std::move(callback));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,12 +372,12 @@ void ScopedInterfaceEndpointHandle::ResetInternal(
state_.swap(new_state);
}

base::Callback<AssociatedGroupController*()>
base::RepeatingCallback<AssociatedGroupController*()>
ScopedInterfaceEndpointHandle::CreateGroupControllerGetter() const {
// We allow this callback to be run on any sequence. If this handle is created
// in non-pending state, we don't have a lock but it should still be safe
// because the group controller never changes.
return base::Bind(&State::group_controller, state_);
return base::BindRepeating(&State::group_controller, state_);
}

} // namespace mojo
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/lib/sync_event_watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
namespace mojo {

SyncEventWatcher::SyncEventWatcher(base::WaitableEvent* event,
const base::Closure& callback)
base::RepeatingClosure callback)
: event_(event),
callback_(callback),
callback_(std::move(callback)),
registry_(SyncHandleRegistry::current()),
destroyed_(new base::RefCountedData<bool>(false)) {}

Expand Down
19 changes: 10 additions & 9 deletions mojo/public/cpp/bindings/lib/sync_handle_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ scoped_refptr<SyncHandleRegistry> SyncHandleRegistry::current() {

bool SyncHandleRegistry::RegisterHandle(const Handle& handle,
MojoHandleSignals handle_signals,
const HandleCallback& callback) {
HandleCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

if (base::Contains(handles_, handle))
Expand All @@ -43,7 +43,7 @@ bool SyncHandleRegistry::RegisterHandle(const Handle& handle,
if (result != MOJO_RESULT_OK)
return false;

handles_[handle] = callback;
handles_[handle] = std::move(callback);
return true;
}

Expand All @@ -58,7 +58,7 @@ void SyncHandleRegistry::UnregisterHandle(const Handle& handle) {
}

void SyncHandleRegistry::RegisterEvent(base::WaitableEvent* event,
const base::Closure& callback) {
base::RepeatingClosure callback) {
auto it = events_.find(event);
if (it == events_.end()) {
auto result = events_.emplace(event, EventCallbackList{});
Expand All @@ -70,11 +70,11 @@ void SyncHandleRegistry::RegisterEvent(base::WaitableEvent* event,
// callbacks to see if any are valid.
wait_set_.AddEvent(event);

it->second.container().push_back(callback);
it->second.container().push_back(std::move(callback));
}

void SyncHandleRegistry::UnregisterEvent(base::WaitableEvent* event,
const base::Closure& callback) {
base::RepeatingClosure callback) {
auto it = events_.find(event);
if (it == events_.end())
return;
Expand Down Expand Up @@ -174,10 +174,11 @@ SyncHandleRegistry::~SyncHandleRegistry() = default;
void SyncHandleRegistry::RemoveInvalidEventCallbacks() {
for (auto it = events_.begin(); it != events_.end();) {
auto& callbacks = it->second.container();
callbacks.erase(
std::remove_if(callbacks.begin(), callbacks.end(),
[](const base::Closure& callback) { return !callback; }),
callbacks.end());
callbacks.erase(std::remove_if(callbacks.begin(), callbacks.end(),
[](const base::RepeatingClosure& callback) {
return !callback;
}),
callbacks.end());
if (callbacks.empty())
events_.erase(it++);
else
Expand Down
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/lib/validation_errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ ScopedSuppressValidationErrorLoggingForTests
}

ValidationErrorObserverForTesting::ValidationErrorObserverForTesting(
const base::Closure& callback)
: last_error_(VALIDATION_ERROR_NONE), callback_(callback) {
base::RepeatingClosure callback)
: last_error_(VALIDATION_ERROR_NONE), callback_(std::move(callback)) {
DCHECK(!g_validation_error_observer);
g_validation_error_observer = this;
}
Expand Down
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/lib/validation_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE)
class COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE)
ValidationErrorObserverForTesting {
public:
explicit ValidationErrorObserverForTesting(const base::Closure& callback);
explicit ValidationErrorObserverForTesting(base::RepeatingClosure callback);
~ValidationErrorObserverForTesting();

ValidationError last_error() const { return last_error_; }
Expand All @@ -122,7 +122,7 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE)

private:
ValidationError last_error_;
base::Closure callback_;
base::RepeatingClosure callback_;

DISALLOW_COPY_AND_ASSIGN(ValidationErrorObserverForTesting);
};
Expand Down
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS_BASE) ScopedInterfaceEndpointHandle {
// asssociation, the return value of the getter will initially be null,
// change to non-null when the handle is associated, and remain unchanged
// ever since.
base::Callback<AssociatedGroupController*()> CreateGroupControllerGetter()
const;
base::RepeatingCallback<AssociatedGroupController*()>
CreateGroupControllerGetter() const;

scoped_refptr<State> state_;

Expand Down
2 changes: 1 addition & 1 deletion mojo/public/cpp/bindings/strong_associated_binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class StrongAssociatedBinding {
scoped_refptr<base::SequencedTaskRunner> task_runner)
: impl_(std::move(impl)),
binding_(impl_.get(), std::move(request), std::move(task_runner)) {
binding_.set_connection_error_with_reason_handler(base::Bind(
binding_.set_connection_error_with_reason_handler(base::BindOnce(
&StrongAssociatedBinding::OnConnectionError, base::Unretained(this)));
}

Expand Down
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/strong_binding.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ class StrongBinding {
scoped_refptr<base::SequencedTaskRunner> task_runner)
: impl_(std::move(impl)),
binding_(impl_.get(), std::move(request), std::move(task_runner)) {
binding_.set_connection_error_with_reason_handler(
base::Bind(&StrongBinding::OnConnectionError, base::Unretained(this)));
binding_.set_connection_error_with_reason_handler(base::BindOnce(
&StrongBinding::OnConnectionError, base::Unretained(this)));
}

~StrongBinding() {}
Expand Down
4 changes: 2 additions & 2 deletions mojo/public/cpp/bindings/sync_event_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace mojo {
// This class is not thread safe.
class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) SyncEventWatcher {
public:
SyncEventWatcher(base::WaitableEvent* event, const base::Closure& callback);
SyncEventWatcher(base::WaitableEvent* event, base::RepeatingClosure callback);

~SyncEventWatcher();

Expand All @@ -51,7 +51,7 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) SyncEventWatcher {
void DecrementRegisterCount();

base::WaitableEvent* const event_;
const base::Closure callback_;
const base::RepeatingClosure callback_;

// Whether |event_| has been registered with SyncHandleRegistry.
bool registered_ = false;
Expand Down
11 changes: 6 additions & 5 deletions mojo/public/cpp/bindings/sync_handle_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,27 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) SyncHandleRegistry
// Returns a sequence-local object.
static scoped_refptr<SyncHandleRegistry> current();

using HandleCallback = base::Callback<void(MojoResult)>;
using HandleCallback = base::RepeatingCallback<void(MojoResult)>;

// Registers a |Handle| to be watched for |handle_signals|. If any such
// signals are satisfied during a Wait(), the Wait() is woken up and
// |callback| is run.
bool RegisterHandle(const Handle& handle,
MojoHandleSignals handle_signals,
const HandleCallback& callback);
HandleCallback callback);

void UnregisterHandle(const Handle& handle);

// Registers a |base::WaitableEvent| which can be used to wake up
// Wait() before any handle signals. |event| is not owned, and if it signals
// during Wait(), |callback| is invoked. Note that |event| may be registered
// multiple times with different callbacks.
void RegisterEvent(base::WaitableEvent* event, const base::Closure& callback);
void RegisterEvent(base::WaitableEvent* event,
base::RepeatingClosure callback);

// Unregisters a specific |event|+|callback| pair.
void UnregisterEvent(base::WaitableEvent* event,
const base::Closure& callback);
base::RepeatingClosure callback);

// Waits on all the registered handles and events and runs callbacks
// synchronously for any that become ready.
Expand All @@ -60,7 +61,7 @@ class COMPONENT_EXPORT(MOJO_CPP_BINDINGS) SyncHandleRegistry
private:
friend class base::RefCounted<SyncHandleRegistry>;

using EventCallbackList = base::StackVector<base::Closure, 1>;
using EventCallbackList = base::StackVector<base::RepeatingClosure, 1>;
using EventMap = std::map<base::WaitableEvent*, EventCallbackList>;

SyncHandleRegistry();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,11 @@ class TestReceiver {
CHECK(task_runner()->RunsTasksInCurrentSequence());

impl0_.reset(new IntegerSenderImpl(std::move(receiver0)));
impl0_->set_notify_send_method_called(
base::Bind(&TestReceiver::SendMethodCalled, base::Unretained(this)));
impl0_->set_notify_send_method_called(base::BindRepeating(
&TestReceiver::SendMethodCalled, base::Unretained(this)));
impl1_.reset(new IntegerSenderImpl(std::move(receiver1)));
impl1_->set_notify_send_method_called(
base::Bind(&TestReceiver::SendMethodCalled, base::Unretained(this)));
impl1_->set_notify_send_method_called(base::BindRepeating(
&TestReceiver::SendMethodCalled, base::Unretained(this)));

expected_calls_ = expected_calls;
notify_finish_ = std::move(notify_finish);
Expand Down
6 changes: 3 additions & 3 deletions mojo/public/cpp/bindings/tests/bindings_perftest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class PingPongTest {
void OnPingDone() {
current_iterations_++;
if (current_iterations_ >= iterations_to_run_) {
quit_closure_.Run();
std::move(quit_closure_).Run();
return;
}

Expand All @@ -75,7 +75,7 @@ class PingPongTest {
unsigned int iterations_to_run_;
unsigned int current_iterations_;

base::Closure quit_closure_;
base::OnceClosure quit_closure_;

DISALLOW_COPY_AND_ASSIGN(PingPongTest);
};
Expand Down Expand Up @@ -183,7 +183,7 @@ class PingPongPaddle : public MessageReceiverWithResponderStatus {
base::TimeTicks end_time_;
uint32_t expected_count_ = 0;
MessageReceiver* sender_;
base::Closure quit_closure_;
base::RepeatingClosure quit_closure_;
};

TEST_F(MojoBindingsPerftest, MultiplexRouterPingPong) {
Expand Down
Loading

0 comments on commit ff664b7

Please sign in to comment.