#include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_api.h"
#include <stdint.h>
#include <algorithm>
#include <iterator>
#include <unordered_set>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/types/cxx23_to_underlying.h"
#include "base/types/expected_macros.h"
#include "base/values.h"
#include "build/build_config.h"
#include "content/public/browser/browser_thread.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_local_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_local_gatt_descriptor.h"
#include "device/bluetooth/bluetooth_local_gatt_service.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
#include "extensions/browser/api/bluetooth_low_energy/utils.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/kiosk/kiosk_delegate.h"
#include "extensions/common/api/bluetooth/bluetooth_manifest_data.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/switches.h"
BrowserContext;
BrowserThread;
apibtle;
namespace extensions {
namespace {
const char kErrorAdapterNotInitialized[] = …;
const char kErrorAlreadyConnected[] = …;
const char kErrorAlreadyNotifying[] = …;
const char kErrorAuthenticationFailed[] = …;
const char kErrorCanceled[] = …;
const char kErrorGattNotSupported[] = …;
const char kErrorHigherSecurity[] = …;
const char kErrorInProgress[] = …;
const char kErrorInsufficientAuthorization[] = …;
const char kErrorInvalidAdvertisementLength[] = …;
const char kErrorInvalidLength[] = …;
const char kErrorInvalidServiceId[] = …;
const char kErrorJniThreadAttach[] = …;
const char kErrorJniEnvironment[] = …;
const char kErrorNoMemory[] = …;
const char kErrorNotConnected[] = …;
const char kErrorNotFound[] = …;
const char kErrorNotNotifying[] = …;
const char kErrorOperationFailed[] = …;
const char kErrorPermissionDenied[] = …;
const char kErrorPlatformNotSupported[] = …;
const char kErrorTimeout[] = …;
const char kErrorUnsupportedDevice[] = …;
const char kErrorWakelock[] = …;
const char kErrorInvalidCharacteristicId[] = …;
const char kErrorNotifyPropertyNotSet[] = …;
const char kErrorIndicatePropertyNotSet[] = …;
const char kErrorServiceNotRegistered[] = …;
const char kErrorUnknownNotificationError[] = …;
const char kStatusAdvertisementAlreadyExists[] = …;
const char kStatusAdvertisementDoesNotExist[] = …;
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
const char kStatusInvalidAdvertisingInterval[] = …;
#endif
std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) { … }
extensions::BluetoothLowEnergyEventRouter* GetEventRouter(
BrowserContext* context) { … }
template <typename T>
void DoWorkCallback(base::OnceCallback<T()> callback) { … }
device::BluetoothAdvertisement::ManufacturerData CreateManufacturerData(
const std::vector<apibtle::ManufacturerData>& manufacturer_data) { … }
device::BluetoothAdvertisement::ServiceData CreateServiceData(
const std::vector<apibtle::ServiceData>& service_data) { … }
bool HasProperty(
const std::vector<apibtle::CharacteristicProperty>& api_properties,
apibtle::CharacteristicProperty property) { … }
bool HasPermission(
const std::vector<apibtle::DescriptorPermission>& api_permissions,
apibtle::DescriptorPermission permission) { … }
device::BluetoothGattCharacteristic::Properties GetBluetoothProperties(
const std::vector<apibtle::CharacteristicProperty>& api_properties) { … }
device::BluetoothGattCharacteristic::Permissions GetBluetoothPermissions(
const std::vector<apibtle::DescriptorPermission>& api_permissions) { … }
bool IsAutoLaunchedKioskApp(const ExtensionId& id) { … }
bool IsPeripheralFlagEnabled() { … }
}
static base::LazyInstance<
BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>>::DestructorAtExit
g_factory = …;
BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>*
BluetoothLowEnergyAPI::GetFactoryInstance() { … }
BluetoothLowEnergyAPI* BluetoothLowEnergyAPI::Get(BrowserContext* context) { … }
BluetoothLowEnergyAPI::BluetoothLowEnergyAPI(BrowserContext* context)
: … { … }
BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() = default;
void BluetoothLowEnergyAPI::Shutdown() { … }
namespace api {
BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction()
: … { … }
BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() =
default;
ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { … }
void BluetoothLowEnergyExtensionFunction::EmptyResponse() { … }
void BluetoothLowEnergyExtensionFunction::RespondWithErrorStatus(
BluetoothLowEnergyEventRouter::Status status) { … }
void BluetoothLowEnergyExtensionFunction::PreDoWork() { … }
BLEPeripheralExtensionFunction::BLEPeripheralExtensionFunction() = default;
BLEPeripheralExtensionFunction::~BLEPeripheralExtensionFunction() = default;
ExtensionFunction::ResponseAction BLEPeripheralExtensionFunction::Run() { … }
BluetoothLowEnergyConnectFunction::BluetoothLowEnergyConnectFunction() =
default;
BluetoothLowEnergyConnectFunction::~BluetoothLowEnergyConnectFunction() =
default;
bool BluetoothLowEnergyConnectFunction::ParseParams() { … }
void BluetoothLowEnergyConnectFunction::DoWork() { … }
void BluetoothLowEnergyConnectFunction::ConnectCallback(
BluetoothLowEnergyEventRouter::Status status) { … }
BluetoothLowEnergyDisconnectFunction::BluetoothLowEnergyDisconnectFunction() =
default;
BluetoothLowEnergyDisconnectFunction::~BluetoothLowEnergyDisconnectFunction() =
default;
bool BluetoothLowEnergyDisconnectFunction::ParseParams() { … }
void BluetoothLowEnergyDisconnectFunction::DoWork() { … }
BluetoothLowEnergyGetServiceFunction::BluetoothLowEnergyGetServiceFunction() =
default;
BluetoothLowEnergyGetServiceFunction::~BluetoothLowEnergyGetServiceFunction() =
default;
bool BluetoothLowEnergyGetServiceFunction::ParseParams() { … }
void BluetoothLowEnergyGetServiceFunction::DoWork() { … }
BluetoothLowEnergyGetServicesFunction::BluetoothLowEnergyGetServicesFunction() { … }
BluetoothLowEnergyGetServicesFunction::
~BluetoothLowEnergyGetServicesFunction() { … }
bool BluetoothLowEnergyGetServicesFunction::ParseParams() { … }
void BluetoothLowEnergyGetServicesFunction::DoWork() { … }
BluetoothLowEnergyGetCharacteristicFunction::
BluetoothLowEnergyGetCharacteristicFunction() { … }
BluetoothLowEnergyGetCharacteristicFunction::
~BluetoothLowEnergyGetCharacteristicFunction() { … }
bool BluetoothLowEnergyGetCharacteristicFunction::ParseParams() { … }
void BluetoothLowEnergyGetCharacteristicFunction::DoWork() { … }
BluetoothLowEnergyGetCharacteristicsFunction::
BluetoothLowEnergyGetCharacteristicsFunction() { … }
BluetoothLowEnergyGetCharacteristicsFunction::
~BluetoothLowEnergyGetCharacteristicsFunction() { … }
bool BluetoothLowEnergyGetCharacteristicsFunction::ParseParams() { … }
void BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { … }
BluetoothLowEnergyGetIncludedServicesFunction::
BluetoothLowEnergyGetIncludedServicesFunction() { … }
BluetoothLowEnergyGetIncludedServicesFunction::
~BluetoothLowEnergyGetIncludedServicesFunction() { … }
bool BluetoothLowEnergyGetIncludedServicesFunction::ParseParams() { … }
void BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { … }
BluetoothLowEnergyGetDescriptorFunction::
BluetoothLowEnergyGetDescriptorFunction() { … }
BluetoothLowEnergyGetDescriptorFunction::
~BluetoothLowEnergyGetDescriptorFunction() { … }
bool BluetoothLowEnergyGetDescriptorFunction::ParseParams() { … }
void BluetoothLowEnergyGetDescriptorFunction::DoWork() { … }
BluetoothLowEnergyGetDescriptorsFunction::
BluetoothLowEnergyGetDescriptorsFunction() { … }
BluetoothLowEnergyGetDescriptorsFunction::
~BluetoothLowEnergyGetDescriptorsFunction() { … }
bool BluetoothLowEnergyGetDescriptorsFunction::ParseParams() { … }
void BluetoothLowEnergyGetDescriptorsFunction::DoWork() { … }
BluetoothLowEnergyReadCharacteristicValueFunction::
BluetoothLowEnergyReadCharacteristicValueFunction() { … }
BluetoothLowEnergyReadCharacteristicValueFunction::
~BluetoothLowEnergyReadCharacteristicValueFunction() { … }
bool BluetoothLowEnergyReadCharacteristicValueFunction::ParseParams() { … }
void BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { … }
void BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback() { … }
BluetoothLowEnergyWriteCharacteristicValueFunction::
BluetoothLowEnergyWriteCharacteristicValueFunction() { … }
BluetoothLowEnergyWriteCharacteristicValueFunction::
~BluetoothLowEnergyWriteCharacteristicValueFunction() { … }
bool BluetoothLowEnergyWriteCharacteristicValueFunction::ParseParams() { … }
void BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { … }
void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() { … }
BluetoothLowEnergyStartCharacteristicNotificationsFunction::
BluetoothLowEnergyStartCharacteristicNotificationsFunction() { … }
BluetoothLowEnergyStartCharacteristicNotificationsFunction::
~BluetoothLowEnergyStartCharacteristicNotificationsFunction() { … }
bool BluetoothLowEnergyStartCharacteristicNotificationsFunction::ParseParams() { … }
void BluetoothLowEnergyStartCharacteristicNotificationsFunction::DoWork() { … }
BluetoothLowEnergyStopCharacteristicNotificationsFunction::
BluetoothLowEnergyStopCharacteristicNotificationsFunction() { … }
BluetoothLowEnergyStopCharacteristicNotificationsFunction::
~BluetoothLowEnergyStopCharacteristicNotificationsFunction() { … }
bool BluetoothLowEnergyStopCharacteristicNotificationsFunction::ParseParams() { … }
void BluetoothLowEnergyStopCharacteristicNotificationsFunction::DoWork() { … }
BluetoothLowEnergyReadDescriptorValueFunction::
BluetoothLowEnergyReadDescriptorValueFunction() { … }
BluetoothLowEnergyReadDescriptorValueFunction::
~BluetoothLowEnergyReadDescriptorValueFunction() { … }
bool BluetoothLowEnergyReadDescriptorValueFunction::ParseParams() { … }
void BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { … }
void BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback() { … }
BluetoothLowEnergyWriteDescriptorValueFunction::
BluetoothLowEnergyWriteDescriptorValueFunction() { … }
BluetoothLowEnergyWriteDescriptorValueFunction::
~BluetoothLowEnergyWriteDescriptorValueFunction() { … }
bool BluetoothLowEnergyWriteDescriptorValueFunction::ParseParams() { … }
void BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { … }
void BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback() { … }
BluetoothLowEnergyAdvertisementFunction::
BluetoothLowEnergyAdvertisementFunction()
: … { … }
BluetoothLowEnergyAdvertisementFunction::
~BluetoothLowEnergyAdvertisementFunction() { … }
int BluetoothLowEnergyAdvertisementFunction::AddAdvertisement(
BluetoothApiAdvertisement* advertisement) { … }
BluetoothApiAdvertisement*
BluetoothLowEnergyAdvertisementFunction::GetAdvertisement(
int advertisement_id) { … }
void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement(
int advertisement_id) { … }
const std::unordered_set<int>*
BluetoothLowEnergyAdvertisementFunction::GetAdvertisementIds() { … }
ExtensionFunction::ResponseAction
BluetoothLowEnergyAdvertisementFunction::Run() { … }
void BluetoothLowEnergyAdvertisementFunction::Initialize() { … }
BluetoothLowEnergyRegisterAdvertisementFunction::
BluetoothLowEnergyRegisterAdvertisementFunction() { … }
BluetoothLowEnergyRegisterAdvertisementFunction::
~BluetoothLowEnergyRegisterAdvertisementFunction() { … }
bool BluetoothLowEnergyRegisterAdvertisementFunction::ParseParams() { … }
void BluetoothLowEnergyRegisterAdvertisementFunction::DoWork() { … }
void BluetoothLowEnergyRegisterAdvertisementFunction::SuccessCallback(
scoped_refptr<device::BluetoothAdvertisement> advertisement) { … }
void BluetoothLowEnergyRegisterAdvertisementFunction::ErrorCallback(
device::BluetoothAdvertisement::ErrorCode status) { … }
BluetoothLowEnergyUnregisterAdvertisementFunction::
BluetoothLowEnergyUnregisterAdvertisementFunction() { … }
BluetoothLowEnergyUnregisterAdvertisementFunction::
~BluetoothLowEnergyUnregisterAdvertisementFunction() { … }
bool BluetoothLowEnergyUnregisterAdvertisementFunction::ParseParams() { … }
void BluetoothLowEnergyUnregisterAdvertisementFunction::DoWork() { … }
void BluetoothLowEnergyUnregisterAdvertisementFunction::SuccessCallback(
int advertisement_id) { … }
void BluetoothLowEnergyUnregisterAdvertisementFunction::ErrorCallback(
int advertisement_id,
device::BluetoothAdvertisement::ErrorCode status) { … }
BluetoothLowEnergyResetAdvertisingFunction::
BluetoothLowEnergyResetAdvertisingFunction() { … }
BluetoothLowEnergyResetAdvertisingFunction::
~BluetoothLowEnergyResetAdvertisingFunction() { … }
bool BluetoothLowEnergyResetAdvertisingFunction::ParseParams() { … }
void BluetoothLowEnergyResetAdvertisingFunction::DoWork() { … }
void BluetoothLowEnergyResetAdvertisingFunction::ErrorCallback(
device::BluetoothAdvertisement::ErrorCode status) { … }
BluetoothLowEnergySetAdvertisingIntervalFunction::
BluetoothLowEnergySetAdvertisingIntervalFunction() { … }
BluetoothLowEnergySetAdvertisingIntervalFunction::
~BluetoothLowEnergySetAdvertisingIntervalFunction() { … }
bool BluetoothLowEnergySetAdvertisingIntervalFunction::ParseParams() { … }
void BluetoothLowEnergySetAdvertisingIntervalFunction::DoWork() { … }
void BluetoothLowEnergySetAdvertisingIntervalFunction::ErrorCallback(
device::BluetoothAdvertisement::ErrorCode status) { … }
BluetoothLowEnergyCreateServiceFunction::
BluetoothLowEnergyCreateServiceFunction() { … }
BluetoothLowEnergyCreateServiceFunction::
~BluetoothLowEnergyCreateServiceFunction() { … }
bool BluetoothLowEnergyCreateServiceFunction::ParseParams() { … }
void BluetoothLowEnergyCreateServiceFunction::DoWork() { … }
BluetoothLowEnergyCreateCharacteristicFunction::
BluetoothLowEnergyCreateCharacteristicFunction() { … }
BluetoothLowEnergyCreateCharacteristicFunction::
~BluetoothLowEnergyCreateCharacteristicFunction() { … }
bool BluetoothLowEnergyCreateCharacteristicFunction::ParseParams() { … }
void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() { … }
BluetoothLowEnergyCreateDescriptorFunction::
BluetoothLowEnergyCreateDescriptorFunction() { … }
BluetoothLowEnergyCreateDescriptorFunction::
~BluetoothLowEnergyCreateDescriptorFunction() { … }
bool BluetoothLowEnergyCreateDescriptorFunction::ParseParams() { … }
void BluetoothLowEnergyCreateDescriptorFunction::DoWork() { … }
BluetoothLowEnergyRegisterServiceFunction::
BluetoothLowEnergyRegisterServiceFunction() { … }
BluetoothLowEnergyRegisterServiceFunction::
~BluetoothLowEnergyRegisterServiceFunction() { … }
bool BluetoothLowEnergyRegisterServiceFunction::ParseParams() { … }
void BluetoothLowEnergyRegisterServiceFunction::DoWork() { … }
BluetoothLowEnergyUnregisterServiceFunction::
BluetoothLowEnergyUnregisterServiceFunction() { … }
BluetoothLowEnergyUnregisterServiceFunction::
~BluetoothLowEnergyUnregisterServiceFunction() { … }
bool BluetoothLowEnergyUnregisterServiceFunction::ParseParams() { … }
void BluetoothLowEnergyUnregisterServiceFunction::DoWork() { … }
BluetoothLowEnergyNotifyCharacteristicValueChangedFunction::
BluetoothLowEnergyNotifyCharacteristicValueChangedFunction() { … }
BluetoothLowEnergyNotifyCharacteristicValueChangedFunction::
~BluetoothLowEnergyNotifyCharacteristicValueChangedFunction() { … }
bool BluetoothLowEnergyNotifyCharacteristicValueChangedFunction::ParseParams() { … }
void BluetoothLowEnergyNotifyCharacteristicValueChangedFunction::DoWork() { … }
BluetoothLowEnergyRemoveServiceFunction::
BluetoothLowEnergyRemoveServiceFunction() { … }
BluetoothLowEnergyRemoveServiceFunction::
~BluetoothLowEnergyRemoveServiceFunction() { … }
bool BluetoothLowEnergyRemoveServiceFunction::ParseParams() { … }
void BluetoothLowEnergyRemoveServiceFunction::DoWork() { … }
BluetoothLowEnergySendRequestResponseFunction::
BluetoothLowEnergySendRequestResponseFunction() { … }
BluetoothLowEnergySendRequestResponseFunction::
~BluetoothLowEnergySendRequestResponseFunction() { … }
bool BluetoothLowEnergySendRequestResponseFunction::ParseParams() { … }
void BluetoothLowEnergySendRequestResponseFunction::DoWork() { … }
}
}