Commit d275ea02 authored by David Bokan's avatar David Bokan Committed by Chromium LUCI CQ

Convert bluetooth_socket_api to BindOnce

This CL converts two functions to use BindOnce:

 - BluetoothSocketSendFunction
 - BluetoothSocketDisconnectFunction

In both cases, the callback is passed to a method on |socket_| which
already takes a OnceCallback so this is a safe change. The Send function
requires a bit of extra work due to the error callback but this is also
obviously a OnceCallback.

Bug: 1152268
Change-Id: Ieb457c3d86f63732f33f851ac1f659e35af9a44c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2592249
Commit-Queue: David Bokan <bokan@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#837638}
parent 14714196
...@@ -90,16 +90,16 @@ void BluetoothApiSocket::AdoptListeningSocket( ...@@ -90,16 +90,16 @@ void BluetoothApiSocket::AdoptListeningSocket(
connected_ = false; connected_ = false;
} }
void BluetoothApiSocket::Disconnect(const base::Closure& callback) { void BluetoothApiSocket::Disconnect(base::OnceClosure callback) {
DCHECK_CURRENTLY_ON(kThreadId); DCHECK_CURRENTLY_ON(kThreadId);
if (!socket_.get()) { if (!socket_.get()) {
callback.Run(); std::move(callback).Run();
return; return;
} }
connected_ = false; connected_ = false;
socket_->Disconnect(callback); socket_->Disconnect(std::move(callback));
} }
bool BluetoothApiSocket::IsPersistent() const { bool BluetoothApiSocket::IsPersistent() const {
...@@ -147,26 +147,26 @@ void BluetoothApiSocket::OnSocketReceiveError( ...@@ -147,26 +147,26 @@ void BluetoothApiSocket::OnSocketReceiveError(
void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer, void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer,
int buffer_size, int buffer_size,
const SendCompletionCallback& success_callback, SendCompletionCallback success_callback,
const ErrorCompletionCallback& error_callback) { ErrorCompletionOnceCallback error_callback) {
DCHECK_CURRENTLY_ON(kThreadId); DCHECK_CURRENTLY_ON(kThreadId);
if (!socket_.get() || !IsConnected()) { if (!socket_.get() || !IsConnected()) {
error_callback.Run(BluetoothApiSocket::kNotConnected, std::move(error_callback)
kSocketNotConnectedError); .Run(BluetoothApiSocket::kNotConnected, kSocketNotConnectedError);
return; return;
} }
socket_->Send(buffer, buffer_size, success_callback, socket_->Send(buffer, buffer_size, std::move(success_callback),
base::BindOnce(&OnSocketSendError, error_callback)); base::BindOnce(&OnSocketSendError, std::move(error_callback)));
} }
// static // static
void BluetoothApiSocket::OnSocketSendError( void BluetoothApiSocket::OnSocketSendError(
const ErrorCompletionCallback& error_callback, ErrorCompletionOnceCallback error_callback,
const std::string& message) { const std::string& message) {
DCHECK_CURRENTLY_ON(kThreadId); DCHECK_CURRENTLY_ON(kThreadId);
error_callback.Run(BluetoothApiSocket::kSystemError, message); std::move(error_callback).Run(BluetoothApiSocket::kSystemError, message);
} }
void BluetoothApiSocket::Accept( void BluetoothApiSocket::Accept(
......
...@@ -29,7 +29,7 @@ class BluetoothApiSocket : public ApiResource { ...@@ -29,7 +29,7 @@ class BluetoothApiSocket : public ApiResource {
enum ErrorReason { kSystemError, kNotConnected, kNotListening, kIOPending, enum ErrorReason { kSystemError, kNotConnected, kNotListening, kIOPending,
kDisconnected }; kDisconnected };
typedef base::Callback<void(int)> SendCompletionCallback; typedef base::OnceCallback<void(int)> SendCompletionCallback;
typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>
ReceiveCompletionCallback; ReceiveCompletionCallback;
typedef base::Callback<void(const device::BluetoothDevice* device, typedef base::Callback<void(const device::BluetoothDevice* device,
...@@ -37,6 +37,9 @@ class BluetoothApiSocket : public ApiResource { ...@@ -37,6 +37,9 @@ class BluetoothApiSocket : public ApiResource {
AcceptCompletionCallback; AcceptCompletionCallback;
typedef base::Callback<void(ErrorReason, const std::string& error_message)> typedef base::Callback<void(ErrorReason, const std::string& error_message)>
ErrorCompletionCallback; ErrorCompletionCallback;
typedef base::OnceCallback<void(ErrorReason,
const std::string& error_message)>
ErrorCompletionOnceCallback;
explicit BluetoothApiSocket(const std::string& owner_extension_id); explicit BluetoothApiSocket(const std::string& owner_extension_id);
BluetoothApiSocket(const std::string& owner_extension_id, BluetoothApiSocket(const std::string& owner_extension_id,
...@@ -59,7 +62,7 @@ class BluetoothApiSocket : public ApiResource { ...@@ -59,7 +62,7 @@ class BluetoothApiSocket : public ApiResource {
const device::BluetoothUUID& uuid); const device::BluetoothUUID& uuid);
// Closes the underlying connection. This is a best effort, and never fails. // Closes the underlying connection. This is a best effort, and never fails.
virtual void Disconnect(const base::Closure& callback); virtual void Disconnect(base::OnceClosure callback);
// Receives data from the socket and calls |success_callback| when data is // Receives data from the socket and calls |success_callback| when data is
// available. |count| is maximum amount of bytes received. If an error occurs, // available. |count| is maximum amount of bytes received. If an error occurs,
...@@ -78,8 +81,8 @@ class BluetoothApiSocket : public ApiResource { ...@@ -78,8 +81,8 @@ class BluetoothApiSocket : public ApiResource {
// underlying communication channel is available for sending data. // underlying communication channel is available for sending data.
virtual void Send(scoped_refptr<net::IOBuffer> buffer, virtual void Send(scoped_refptr<net::IOBuffer> buffer,
int buffer_size, int buffer_size,
const SendCompletionCallback& success_callback, SendCompletionCallback success_callback,
const ErrorCompletionCallback& error_callback); ErrorCompletionOnceCallback error_callback);
// Accepts a client connection from the socket and calls |success_callback| // Accepts a client connection from the socket and calls |success_callback|
// when one has connected. If an error occurs, calls |error_callback| with a // when one has connected. If an error occurs, calls |error_callback| with a
...@@ -121,9 +124,8 @@ class BluetoothApiSocket : public ApiResource { ...@@ -121,9 +124,8 @@ class BluetoothApiSocket : public ApiResource {
device::BluetoothSocket::ErrorReason reason, device::BluetoothSocket::ErrorReason reason,
const std::string& message); const std::string& message);
static void OnSocketSendError( static void OnSocketSendError(ErrorCompletionOnceCallback error_callback,
const ErrorCompletionCallback& error_callback, const std::string& message);
const std::string& message);
static void OnSocketAcceptError( static void OnSocketAcceptError(
const ErrorCompletionCallback& error_callback, const ErrorCompletionCallback& error_callback,
......
...@@ -512,8 +512,8 @@ ExtensionFunction::ResponseAction BluetoothSocketDisconnectFunction::Run() { ...@@ -512,8 +512,8 @@ ExtensionFunction::ResponseAction BluetoothSocketDisconnectFunction::Run() {
if (!socket) if (!socket)
return RespondNow(Error(kSocketNotFoundError)); return RespondNow(Error(kSocketNotFoundError));
socket->Disconnect(base::Bind(&BluetoothSocketDisconnectFunction::OnSuccess, socket->Disconnect(
this)); base::BindOnce(&BluetoothSocketDisconnectFunction::OnSuccess, this));
return did_respond() ? AlreadyResponded() : RespondLater(); return did_respond() ? AlreadyResponded() : RespondLater();
} }
...@@ -556,10 +556,9 @@ ExtensionFunction::ResponseAction BluetoothSocketSendFunction::Run() { ...@@ -556,10 +556,9 @@ ExtensionFunction::ResponseAction BluetoothSocketSendFunction::Run() {
if (!socket) if (!socket)
return RespondNow(Error(kSocketNotFoundError)); return RespondNow(Error(kSocketNotFoundError));
socket->Send(io_buffer_, socket->Send(io_buffer_, io_buffer_size_,
io_buffer_size_, base::BindOnce(&BluetoothSocketSendFunction::OnSuccess, this),
base::Bind(&BluetoothSocketSendFunction::OnSuccess, this), base::BindOnce(&BluetoothSocketSendFunction::OnError, this));
base::Bind(&BluetoothSocketSendFunction::OnError, this));
return did_respond() ? AlreadyResponded() : RespondLater(); return did_respond() ? AlreadyResponded() : RespondLater();
} }
......
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