#ifndef EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_API_SOCKET_H_
#define EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_API_SOCKET_H_
#include <string>
#include "content/public/browser/browser_thread.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
#include "extensions/browser/api/api_resource.h"
#include "extensions/browser/api/api_resource_manager.h"
namespace net {
class IOBuffer;
}
namespace extensions {
class BluetoothApiSocket : public ApiResource {
public:
enum ErrorReason { kSystemError, kNotConnected, kNotListening, kIOPending,
kDisconnected };
using SendCompletionCallback = base::OnceCallback<void(int)>;
using ReceiveCompletionCallback =
base::OnceCallback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>;
using AcceptCompletionCallback =
base::OnceCallback<void(const device::BluetoothDevice* device,
scoped_refptr<device::BluetoothSocket>)>;
using ErrorCompletionCallback =
base::OnceCallback<void(ErrorReason, const std::string& error_message)>;
explicit BluetoothApiSocket(const std::string& owner_extension_id);
BluetoothApiSocket(const std::string& owner_extension_id,
scoped_refptr<device::BluetoothSocket> socket,
const std::string& device_address,
const device::BluetoothUUID& uuid);
BluetoothApiSocket(const BluetoothApiSocket&) = delete;
BluetoothApiSocket& operator=(const BluetoothApiSocket&) = delete;
~BluetoothApiSocket() override;
virtual void AdoptConnectedSocket(
scoped_refptr<device::BluetoothSocket> socket,
const std::string& device_address,
const device::BluetoothUUID& uuid);
virtual void AdoptListeningSocket(
scoped_refptr<device::BluetoothSocket> socket,
const device::BluetoothUUID& uuid);
virtual void Disconnect(base::OnceClosure callback);
virtual void Receive(int count,
ReceiveCompletionCallback success_callback,
ErrorCompletionCallback error_callback);
virtual void Send(scoped_refptr<net::IOBuffer> buffer,
int buffer_size,
SendCompletionCallback success_callback,
ErrorCompletionCallback error_callback);
virtual void Accept(AcceptCompletionCallback success_callback,
ErrorCompletionCallback error_callback);
const std::string& device_address() const { return device_address_; }
const device::BluetoothUUID& uuid() const { return uuid_; }
bool IsPersistent() const override;
const std::string* name() const { return name_.get(); }
void set_name(const std::string& name) { name_.reset(new std::string(name)); }
bool persistent() const { return persistent_; }
void set_persistent(bool persistent) { persistent_ = persistent; }
int buffer_size() const { return buffer_size_; }
void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; }
bool paused() const { return paused_; }
void set_paused(bool paused) { paused_ = paused; }
bool IsConnected() const { return connected_; }
static const content::BrowserThread::ID kThreadId =
content::BrowserThread::UI;
private:
friend class ApiResourceManager<BluetoothApiSocket>;
static const char* service_name() { return "BluetoothApiSocketManager"; }
static void OnSocketReceiveError(ErrorCompletionCallback error_callback,
device::BluetoothSocket::ErrorReason reason,
const std::string& message);
static void OnSocketSendError(ErrorCompletionCallback error_callback,
const std::string& message);
static void OnSocketAcceptError(ErrorCompletionCallback error_callback,
const std::string& message);
scoped_refptr<device::BluetoothSocket> socket_;
std::string device_address_;
device::BluetoothUUID uuid_;
std::unique_ptr<std::string> name_;
bool persistent_;
int buffer_size_;
bool paused_;
bool connected_;
};
}
#endif