#include "extensions/browser/api/bluetooth/bluetooth_private_api.h"
#include <stdint.h>
#include <string_view>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/lazy_instance.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "build/chromeos_buildflags.h"
#include "components/device_event_log/device_event_log.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_common.h"
#include "device/bluetooth/bluetooth_discovery_session.h"
#include "extensions/browser/api/bluetooth/bluetooth_api.h"
#include "extensions/browser/api/bluetooth/bluetooth_api_pairing_delegate.h"
#include "extensions/browser/api/bluetooth/bluetooth_event_router.h"
#include "extensions/common/api/bluetooth.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "device/bluetooth/chromeos/bluetooth_utils.h"
#endif
bt;
bt_private;
SetDiscoveryFilter;
namespace extensions {
static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothPrivateAPI>>::
DestructorAtExit g_factory = …;
namespace {
#if BUILDFLAG(IS_CHROMEOS_ASH)
device::BluetoothTransport GetBluetoothTransport(bt::Transport transport) {
switch (transport) {
case bt::Transport::kClassic:
return device::BLUETOOTH_TRANSPORT_CLASSIC;
case bt::Transport::kLe:
return device::BLUETOOTH_TRANSPORT_LE;
case bt::Transport::kDual:
return device::BLUETOOTH_TRANSPORT_DUAL;
default:
return device::BLUETOOTH_TRANSPORT_INVALID;
}
}
bool IsActualConnectionFailure(bt_private::ConnectResultType result) {
DCHECK(result != bt_private::ConnectResultType::kSuccess);
switch (result) {
case bt_private::ConnectResultType::kInProgress:
case bt_private::ConnectResultType::kAuthCanceled:
case bt_private::ConnectResultType::kAuthRejected:
return false;
default:
return true;
}
}
std::optional<device::ConnectionFailureReason> GetConnectionFailureReason(
bt_private::ConnectResultType result) {
DCHECK(IsActualConnectionFailure(result));
switch (result) {
case bt_private::ConnectResultType::kNone:
return device::ConnectionFailureReason::kSystemError;
case bt_private::ConnectResultType::kAuthFailed:
return device::ConnectionFailureReason::kAuthFailed;
case bt_private::ConnectResultType::kAuthTimeout:
return device::ConnectionFailureReason::kAuthTimeout;
case bt_private::ConnectResultType::kFailed:
return device::ConnectionFailureReason::kFailed;
case bt_private::ConnectResultType::kUnknownError:
return device::ConnectionFailureReason::kUnknownConnectionError;
case bt_private::ConnectResultType::kUnsupportedDevice:
return device::ConnectionFailureReason::kUnsupportedDevice;
case bt_private::ConnectResultType::kNotReady:
return device::ConnectionFailureReason::kDeviceNotReady;
case bt_private::ConnectResultType::kAlreadyExists:
return device::ConnectionFailureReason::kDeviceAlreadyExists;
case bt_private::ConnectResultType::kNotConnected:
return device::ConnectionFailureReason::kNotConnectable;
case bt_private::ConnectResultType::kDoesNotExist:
return device::ConnectionFailureReason::kNotFound;
case bt_private::ConnectResultType::kInvalidArgs:
return device::ConnectionFailureReason::kInvalidArgs;
case bt_private::ConnectResultType::kNonAuthTimeout:
return device::ConnectionFailureReason::kNonAuthTimeout;
case bt_private::ConnectResultType::kNoMemory:
return device::ConnectionFailureReason::kNoMemory;
case bt_private::ConnectResultType::kJniEnvironment:
return device::ConnectionFailureReason::kJniEnvironment;
case bt_private::ConnectResultType::kJniThreadAttach:
return device::ConnectionFailureReason::kJniThreadAttach;
case bt_private::ConnectResultType::kWakelock:
return device::ConnectionFailureReason::kWakelock;
case bt_private::ConnectResultType::kAlreadyConnected:
return device::ConnectionFailureReason::kAlreadyConnected;
case bt_private::ConnectResultType::kUnexpectedState:
return device::ConnectionFailureReason::kUnexpectedState;
case bt_private::ConnectResultType::kSocketError:
return device::ConnectionFailureReason::kSocketError;
case bt_private::ConnectResultType::kInProgress:
[[fallthrough]];
case bt_private::ConnectResultType::kAuthRejected:
[[fallthrough]];
case bt_private::ConnectResultType::kAuthCanceled:
[[fallthrough]];
case bt_private::ConnectResultType::kSuccess:
NOTREACHED();
}
}
#endif
std::string GetListenerId(const EventListenerInfo& details) { … }
bt_private::ConnectResultType DeviceConnectErrorToConnectResult(
device::BluetoothDevice::ConnectErrorCode error_code) { … }
}
BrowserContextKeyedAPIFactory<BluetoothPrivateAPI>*
BluetoothPrivateAPI::GetFactoryInstance() { … }
BluetoothPrivateAPI::BluetoothPrivateAPI(content::BrowserContext* context)
: … { … }
BluetoothPrivateAPI::~BluetoothPrivateAPI() = default;
void BluetoothPrivateAPI::Shutdown() { … }
void BluetoothPrivateAPI::OnListenerAdded(const EventListenerInfo& details) { … }
void BluetoothPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) { … }
namespace api {
namespace {
const char kNameProperty[] = …;
const char kPoweredProperty[] = …;
const char kDiscoverableProperty[] = …;
const char kSetAdapterPropertyError[] = …;
const char kDeviceNotFoundError[] = …;
const char kDeviceNotConnectedError[] = …;
const char kPairingNotEnabled[] = …;
const char kInvalidPairingResponseOptions[] = …;
const char kAdapterNotPresent[] = …;
const char kDisconnectError[] = …;
const char kForgetDeviceError[] = …;
const char kSetDiscoveryFilterFailed[] = …;
const char kPairingFailed[] = …;
bool ValidatePairingResponseOptions(
const device::BluetoothDevice* device,
const bt_private::SetPairingResponseOptions& options) { … }
}
BluetoothPrivateSetAdapterStateFunction::
BluetoothPrivateSetAdapterStateFunction() { … }
BluetoothPrivateSetAdapterStateFunction::
~BluetoothPrivateSetAdapterStateFunction() { … }
bool BluetoothPrivateSetAdapterStateFunction::CreateParams() { … }
void BluetoothPrivateSetAdapterStateFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
base::OnceClosure
BluetoothPrivateSetAdapterStateFunction::CreatePropertySetCallback(
const std::string& property_name) { … }
base::OnceClosure
BluetoothPrivateSetAdapterStateFunction::CreatePropertyErrorCallback(
const std::string& property_name) { … }
void BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet(
const std::string& property) { … }
void BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertyError(
const std::string& property) { … }
void BluetoothPrivateSetAdapterStateFunction::SendError() { … }
BluetoothPrivateSetPairingResponseFunction::
BluetoothPrivateSetPairingResponseFunction() { … }
BluetoothPrivateSetPairingResponseFunction::
~BluetoothPrivateSetPairingResponseFunction() { … }
bool BluetoothPrivateSetPairingResponseFunction::CreateParams() { … }
void BluetoothPrivateSetPairingResponseFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
BluetoothPrivateDisconnectAllFunction::BluetoothPrivateDisconnectAllFunction() { … }
BluetoothPrivateDisconnectAllFunction::
~BluetoothPrivateDisconnectAllFunction() { … }
bool BluetoothPrivateDisconnectAllFunction::CreateParams() { … }
void BluetoothPrivateDisconnectAllFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
void BluetoothPrivateDisconnectAllFunction::OnSuccessCallback() { … }
void BluetoothPrivateDisconnectAllFunction::OnErrorCallback(
scoped_refptr<device::BluetoothAdapter> adapter,
const std::string& device_address) { … }
BluetoothPrivateForgetDeviceFunction::BluetoothPrivateForgetDeviceFunction() =
default;
BluetoothPrivateForgetDeviceFunction::~BluetoothPrivateForgetDeviceFunction() =
default;
bool BluetoothPrivateForgetDeviceFunction::CreateParams() { … }
void BluetoothPrivateForgetDeviceFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
void BluetoothPrivateForgetDeviceFunction::OnSuccessCallback() { … }
void BluetoothPrivateForgetDeviceFunction::OnErrorCallback(
scoped_refptr<device::BluetoothAdapter> adapter,
const std::string& device_address) { … }
BluetoothPrivateSetDiscoveryFilterFunction::
BluetoothPrivateSetDiscoveryFilterFunction() = default;
BluetoothPrivateSetDiscoveryFilterFunction::
~BluetoothPrivateSetDiscoveryFilterFunction() = default;
bool BluetoothPrivateSetDiscoveryFilterFunction::CreateParams() { … }
void BluetoothPrivateSetDiscoveryFilterFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
void BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback() { … }
void BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback() { … }
BluetoothPrivateConnectFunction::BluetoothPrivateConnectFunction() = default;
BluetoothPrivateConnectFunction::~BluetoothPrivateConnectFunction() = default;
bool BluetoothPrivateConnectFunction::CreateParams() { … }
void BluetoothPrivateConnectFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
void BluetoothPrivateConnectFunction::OnConnect(
std::optional<device::BluetoothDevice::ConnectErrorCode> error_code) { … }
BluetoothPrivatePairFunction::BluetoothPrivatePairFunction() = default;
BluetoothPrivatePairFunction::~BluetoothPrivatePairFunction() = default;
bool BluetoothPrivatePairFunction::CreateParams() { … }
void BluetoothPrivatePairFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
void BluetoothPrivatePairFunction::OnPair(
std::optional<device::BluetoothDevice::ConnectErrorCode> error_code) { … }
BluetoothPrivateRecordPairingFunction::BluetoothPrivateRecordPairingFunction() =
default;
BluetoothPrivateRecordPairingFunction::
~BluetoothPrivateRecordPairingFunction() = default;
bool BluetoothPrivateRecordPairingFunction::CreateParams() { … }
void BluetoothPrivateRecordPairingFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
BluetoothPrivateRecordReconnectionFunction::
BluetoothPrivateRecordReconnectionFunction() = default;
BluetoothPrivateRecordReconnectionFunction::
~BluetoothPrivateRecordReconnectionFunction() = default;
bool BluetoothPrivateRecordReconnectionFunction::CreateParams() { … }
void BluetoothPrivateRecordReconnectionFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
BluetoothPrivateRecordDeviceSelectionFunction::
BluetoothPrivateRecordDeviceSelectionFunction() = default;
BluetoothPrivateRecordDeviceSelectionFunction::
~BluetoothPrivateRecordDeviceSelectionFunction() = default;
bool BluetoothPrivateRecordDeviceSelectionFunction::CreateParams() { … }
void BluetoothPrivateRecordDeviceSelectionFunction::DoWork(
scoped_refptr<device::BluetoothAdapter> adapter) { … }
}
}