#include "device/bluetooth/adapter.h"
#include <string>
#include <utility>
#include <vector>
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram_functions.h"
#include "base/not_fatal_until.h"
#include "build/build_config.h"
#include "device/bluetooth/advertisement.h"
#include "device/bluetooth/bluetooth_local_gatt_service.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "device/bluetooth/device.h"
#include "device/bluetooth/discovery_session.h"
#include "device/bluetooth/floss/floss_features.h"
#include "device/bluetooth/gatt_service.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
#include "device/bluetooth/public/mojom/connect_result_type_converter.h"
#include "device/bluetooth/server_socket.h"
#include "device/bluetooth/socket.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
#include "device/bluetooth/bluez/metrics_recorder.h"
#endif
namespace bluetooth {
namespace {
const char kMojoReceivingPipeError[] = …;
const char kMojoSendingPipeError[] = …;
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
const char kCannotConnectToDeviceError[] = …;
#endif
}
Adapter::ConnectToServiceRequestDetails::ConnectToServiceRequestDetails(
const std::string& address,
const device::BluetoothUUID& service_uuid,
const base::Time& time_requested,
const bool should_unbond_on_error,
ConnectToServiceInsecurelyCallback callback)
: … { … }
Adapter::ConnectToServiceRequestDetails::~ConnectToServiceRequestDetails() =
default;
Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter)
: … { … }
Adapter::~Adapter() { … }
void Adapter::ConnectToDevice(const std::string& address,
ConnectToDeviceCallback callback) { … }
void Adapter::GetDevices(GetDevicesCallback callback) { … }
void Adapter::GetInfo(GetInfoCallback callback) { … }
void Adapter::AddObserver(mojo::PendingRemote<mojom::AdapterObserver> observer,
AddObserverCallback callback) { … }
void Adapter::RegisterAdvertisement(const device::BluetoothUUID& service_uuid,
const std::vector<uint8_t>& service_data,
bool use_scan_response,
bool connectable,
RegisterAdvertisementCallback callback) { … }
void Adapter::SetDiscoverable(bool discoverable,
SetDiscoverableCallback callback) { … }
void Adapter::SetName(const std::string& name, SetNameCallback callback) { … }
void Adapter::StartDiscoverySession(const std::string& client_name,
StartDiscoverySessionCallback callback) { … }
void Adapter::ConnectToServiceInsecurely(
const std::string& address,
const device::BluetoothUUID& service_uuid,
bool should_unbond_on_error,
ConnectToServiceInsecurelyCallback callback) { … }
void Adapter::CreateRfcommServiceInsecurely(
const std::string& service_name,
const device::BluetoothUUID& service_uuid,
CreateRfcommServiceInsecurelyCallback callback) { … }
void Adapter::CreateLocalGattService(
const device::BluetoothUUID& service_id,
mojo::PendingRemote<mojom::GattServiceObserver> observer,
CreateLocalGattServiceCallback callback) { … }
void Adapter::IsLeScatternetDualRoleSupported(
IsLeScatternetDualRoleSupportedCallback callback) { … }
void Adapter::AdapterPresentChanged(device::BluetoothAdapter* adapter,
bool present) { … }
void Adapter::AdapterPoweredChanged(device::BluetoothAdapter* adapter,
bool powered) { … }
void Adapter::AdapterDiscoverableChanged(device::BluetoothAdapter* adapter,
bool discoverable) { … }
void Adapter::AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
bool discovering) { … }
void Adapter::DeviceAdded(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) { … }
void Adapter::DeviceChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) { … }
void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) { … }
void Adapter::GattServicesDiscovered(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) { … }
void Adapter::AllowConnectionsForUuid(
const device::BluetoothUUID& service_uuid) { … }
void Adapter::OnGattServiceInvalidated(device::BluetoothUUID service_id) { … }
void Adapter::OnDeviceFetchedForInsecureServiceConnection(
int request_id,
device::BluetoothDevice* device) { … }
void Adapter::ProcessDeviceForInsecureServiceConnection(
int request_id,
device::BluetoothDevice* device,
bool disconnected) { … }
void Adapter::ProcessPendingInsecureServiceConnectionRequest(
device::BluetoothDevice* device,
bool disconnected) { … }
void Adapter::OnGattConnect(
ConnectToDeviceCallback callback,
std::unique_ptr<device::BluetoothGattConnection> connection,
std::optional<device::BluetoothDevice::ConnectErrorCode> error_code) { … }
void Adapter::OnRegisterAdvertisement(
RegisterAdvertisementCallback callback,
scoped_refptr<device::BluetoothAdvertisement> advertisement) { … }
void Adapter::OnRegisterAdvertisementError(
RegisterAdvertisementCallback callback,
device::BluetoothAdvertisement::ErrorCode error_code) { … }
void Adapter::OnSetDiscoverable(SetDiscoverableCallback callback) { … }
void Adapter::OnSetDiscoverableError(SetDiscoverableCallback callback) { … }
void Adapter::OnSetName(SetNameCallback callback) { … }
void Adapter::OnSetNameError(SetNameCallback callback) { … }
void Adapter::OnStartDiscoverySession(
StartDiscoverySessionCallback callback,
std::unique_ptr<device::BluetoothDiscoverySession> session) { … }
void Adapter::OnDiscoverySessionError(StartDiscoverySessionCallback callback) { … }
void Adapter::OnConnectToService(
int request_id,
scoped_refptr<device::BluetoothSocket> socket) { … }
void Adapter::OnConnectToServiceError(int request_id,
const std::string& message) { … }
void Adapter::OnConnectToServiceInsecurelyError(
int request_id,
const std::string& error_message) { … }
void Adapter::OnCreateRfcommServiceInsecurely(
CreateRfcommServiceInsecurelyCallback callback,
scoped_refptr<device::BluetoothSocket> socket) { … }
void Adapter::OnCreateRfcommServiceInsecurelyError(
CreateRfcommServiceInsecurelyCallback callback,
const std::string& message) { … }
void Adapter::ExecuteConnectToServiceCallback(
int request_id,
mojom::ConnectToServiceResultPtr result) { … }
}