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

Convert BluetoothApiSocket ErrorCompletionCallback to OnceCallback

This callback is only ever invoked synchronously or put into a BindOnce
and invoked from there. Hence, it cannot be called multiple times.

Bug: 1152268
Change-Id: Ib7012cf922ab8de546f712bbda36e448c63005d0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2593876
Commit-Queue: David Bokan <bokan@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#837652}
parent 6a350237
...@@ -107,25 +107,25 @@ bool BluetoothApiSocket::IsPersistent() const { ...@@ -107,25 +107,25 @@ bool BluetoothApiSocket::IsPersistent() const {
return persistent_; return persistent_;
} }
void BluetoothApiSocket::Receive( void BluetoothApiSocket::Receive(int count,
int count, ReceiveCompletionCallback success_callback,
ReceiveCompletionCallback success_callback, ErrorCompletionCallback error_callback) {
const ErrorCompletionCallback& 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_->Receive(count, std::move(success_callback), socket_->Receive(
base::BindOnce(&OnSocketReceiveError, error_callback)); count, std::move(success_callback),
base::BindOnce(&OnSocketReceiveError, std::move(error_callback)));
} }
// static // static
void BluetoothApiSocket::OnSocketReceiveError( void BluetoothApiSocket::OnSocketReceiveError(
const ErrorCompletionCallback& error_callback, ErrorCompletionCallback error_callback,
device::BluetoothSocket::ErrorReason reason, device::BluetoothSocket::ErrorReason reason,
const std::string& message) { const std::string& message) {
DCHECK_CURRENTLY_ON(kThreadId); DCHECK_CURRENTLY_ON(kThreadId);
...@@ -142,13 +142,13 @@ void BluetoothApiSocket::OnSocketReceiveError( ...@@ -142,13 +142,13 @@ void BluetoothApiSocket::OnSocketReceiveError(
error_reason = BluetoothApiSocket::kSystemError; error_reason = BluetoothApiSocket::kSystemError;
break; break;
} }
error_callback.Run(error_reason, message); std::move(error_callback).Run(error_reason, message);
} }
void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer, void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer,
int buffer_size, int buffer_size,
SendCompletionCallback success_callback, SendCompletionCallback success_callback,
ErrorCompletionOnceCallback error_callback) { ErrorCompletionCallback error_callback) {
DCHECK_CURRENTLY_ON(kThreadId); DCHECK_CURRENTLY_ON(kThreadId);
if (!socket_.get() || !IsConnected()) { if (!socket_.get() || !IsConnected()) {
...@@ -163,32 +163,33 @@ void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer, ...@@ -163,32 +163,33 @@ void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer,
// static // static
void BluetoothApiSocket::OnSocketSendError( void BluetoothApiSocket::OnSocketSendError(
ErrorCompletionOnceCallback error_callback, ErrorCompletionCallback error_callback,
const std::string& message) { const std::string& message) {
DCHECK_CURRENTLY_ON(kThreadId); DCHECK_CURRENTLY_ON(kThreadId);
std::move(error_callback).Run(BluetoothApiSocket::kSystemError, message); std::move(error_callback).Run(BluetoothApiSocket::kSystemError, message);
} }
void BluetoothApiSocket::Accept(AcceptCompletionCallback success_callback, void BluetoothApiSocket::Accept(AcceptCompletionCallback success_callback,
const ErrorCompletionCallback& error_callback) { ErrorCompletionCallback error_callback) {
DCHECK_CURRENTLY_ON(kThreadId); DCHECK_CURRENTLY_ON(kThreadId);
if (!socket_.get() || IsConnected()) { if (!socket_.get() || IsConnected()) {
error_callback.Run(BluetoothApiSocket::kNotListening, std::move(error_callback)
kSocketNotListeningError); .Run(BluetoothApiSocket::kNotListening, kSocketNotListeningError);
return; return;
} }
socket_->Accept(std::move(success_callback), socket_->Accept(
base::BindOnce(&OnSocketAcceptError, error_callback)); std::move(success_callback),
base::BindOnce(&OnSocketAcceptError, std::move(error_callback)));
} }
// static // static
void BluetoothApiSocket::OnSocketAcceptError( void BluetoothApiSocket::OnSocketAcceptError(
const ErrorCompletionCallback& error_callback, ErrorCompletionCallback 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);
} }
} // namespace extensions } // namespace extensions
...@@ -29,17 +29,14 @@ class BluetoothApiSocket : public ApiResource { ...@@ -29,17 +29,14 @@ class BluetoothApiSocket : public ApiResource {
enum ErrorReason { kSystemError, kNotConnected, kNotListening, kIOPending, enum ErrorReason { kSystemError, kNotConnected, kNotListening, kIOPending,
kDisconnected }; kDisconnected };
typedef base::OnceCallback<void(int)> SendCompletionCallback; using SendCompletionCallback = base::OnceCallback<void(int)>;
typedef base::OnceCallback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> using ReceiveCompletionCallback =
ReceiveCompletionCallback; base::OnceCallback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>;
typedef base::OnceCallback<void(const device::BluetoothDevice* device, using AcceptCompletionCallback =
scoped_refptr<device::BluetoothSocket>)> base::OnceCallback<void(const device::BluetoothDevice* device,
AcceptCompletionCallback; scoped_refptr<device::BluetoothSocket>)>;
typedef base::Callback<void(ErrorReason, const std::string& error_message)> using ErrorCompletionCallback =
ErrorCompletionCallback; base::OnceCallback<void(ErrorReason, const std::string& error_message)>;
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,
...@@ -71,7 +68,7 @@ class BluetoothApiSocket : public ApiResource { ...@@ -71,7 +68,7 @@ class BluetoothApiSocket : public ApiResource {
// |kIOPending| error. // |kIOPending| error.
virtual void Receive(int count, virtual void Receive(int count,
ReceiveCompletionCallback success_callback, ReceiveCompletionCallback success_callback,
const ErrorCompletionCallback& error_callback); ErrorCompletionCallback error_callback);
// Sends |buffer| to the socket and calls |success_callback| when data has // Sends |buffer| to the socket and calls |success_callback| when data has
// been successfully sent. |buffer_size| is the numberof bytes contained in // been successfully sent. |buffer_size| is the numberof bytes contained in
...@@ -82,13 +79,13 @@ class BluetoothApiSocket : public ApiResource { ...@@ -82,13 +79,13 @@ class BluetoothApiSocket : public ApiResource {
virtual void Send(scoped_refptr<net::IOBuffer> buffer, virtual void Send(scoped_refptr<net::IOBuffer> buffer,
int buffer_size, int buffer_size,
SendCompletionCallback success_callback, SendCompletionCallback success_callback,
ErrorCompletionOnceCallback error_callback); ErrorCompletionCallback 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
// reason and a message. // reason and a message.
virtual void Accept(AcceptCompletionCallback success_callback, virtual void Accept(AcceptCompletionCallback success_callback,
const ErrorCompletionCallback& error_callback); ErrorCompletionCallback error_callback);
const std::string& device_address() const { return device_address_; } const std::string& device_address() const { return device_address_; }
const device::BluetoothUUID& uuid() const { return uuid_; } const device::BluetoothUUID& uuid() const { return uuid_; }
...@@ -119,17 +116,15 @@ class BluetoothApiSocket : public ApiResource { ...@@ -119,17 +116,15 @@ class BluetoothApiSocket : public ApiResource {
friend class ApiResourceManager<BluetoothApiSocket>; friend class ApiResourceManager<BluetoothApiSocket>;
static const char* service_name() { return "BluetoothApiSocketManager"; } static const char* service_name() { return "BluetoothApiSocketManager"; }
static void OnSocketReceiveError( static void OnSocketReceiveError(ErrorCompletionCallback error_callback,
const ErrorCompletionCallback& error_callback, device::BluetoothSocket::ErrorReason reason,
device::BluetoothSocket::ErrorReason reason, const std::string& message);
const std::string& message);
static void OnSocketSendError(ErrorCompletionOnceCallback error_callback, static void OnSocketSendError(ErrorCompletionCallback error_callback,
const std::string& message); const std::string& message);
static void OnSocketAcceptError( static void OnSocketAcceptError(ErrorCompletionCallback error_callback,
const ErrorCompletionCallback& error_callback, const std::string& message);
const std::string& message);
// The underlying device socket instance. // The underlying device socket instance.
scoped_refptr<device::BluetoothSocket> socket_; scoped_refptr<device::BluetoothSocket> socket_;
......
...@@ -184,8 +184,8 @@ void BluetoothSocketEventDispatcher::StartReceive(const SocketParams& params) { ...@@ -184,8 +184,8 @@ void BluetoothSocketEventDispatcher::StartReceive(const SocketParams& params) {
socket->Receive( socket->Receive(
buffer_size, buffer_size,
base::BindOnce(&BluetoothSocketEventDispatcher::ReceiveCallback, params), base::BindOnce(&BluetoothSocketEventDispatcher::ReceiveCallback, params),
base::Bind(&BluetoothSocketEventDispatcher::ReceiveErrorCallback, base::BindOnce(&BluetoothSocketEventDispatcher::ReceiveErrorCallback,
params)); params));
} }
// static // static
...@@ -268,7 +268,8 @@ void BluetoothSocketEventDispatcher::StartAccept(const SocketParams& params) { ...@@ -268,7 +268,8 @@ void BluetoothSocketEventDispatcher::StartAccept(const SocketParams& params) {
socket->Accept( socket->Accept(
base::BindOnce(&BluetoothSocketEventDispatcher::AcceptCallback, params), base::BindOnce(&BluetoothSocketEventDispatcher::AcceptCallback, params),
base::Bind(&BluetoothSocketEventDispatcher::AcceptErrorCallback, params)); base::BindOnce(&BluetoothSocketEventDispatcher::AcceptErrorCallback,
params));
} }
// static // static
......
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