#include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_event_router.h"
#include <algorithm>
#include <iterator>
#include <memory>
#include <unordered_set>
#include <utility>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_gatt_service.h"
#include "device/bluetooth/bluetooth_local_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_local_gatt_descriptor.h"
#include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_remote_gatt_descriptor.h"
#include "extensions/browser/api/api_resource_manager.h"
#include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_connection.h"
#include "extensions/browser/api/bluetooth_low_energy/bluetooth_low_energy_notify_session.h"
#include "extensions/browser/api/bluetooth_low_energy/utils.h"
#include "extensions/browser/event_listener_map.h"
#include "extensions/browser/event_router.h"
#include "extensions/common/api/bluetooth/bluetooth_manifest_data.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
BrowserThread;
BluetoothAdapter;
BluetoothAdapterFactory;
BluetoothDevice;
BluetoothGattConnection;
BluetoothGattService;
BluetoothRemoteGattCharacteristic;
BluetoothRemoteGattDescriptor;
BluetoothRemoteGattService;
apibtle;
namespace {
apibtle::Service PopulateService(const BluetoothRemoteGattService* service) { … }
std::vector<apibtle::CharacteristicProperty> PopulateCharacteristicProperties(
BluetoothRemoteGattCharacteristic::Properties properties) { … }
apibtle::Characteristic PopulateCharacteristic(
const BluetoothRemoteGattCharacteristic* characteristic) { … }
apibtle::Descriptor PopulateDescriptor(
const BluetoothRemoteGattDescriptor* descriptor) { … }
apibtle::Request PopulateDevice(const device::BluetoothDevice* device) { … }
ConnectionResourceManager;
ConnectionResourceManager* GetConnectionResourceManager(
content::BrowserContext* context) { … }
NotifySessionResourceManager;
NotifySessionResourceManager* GetNotifySessionResourceManager(
content::BrowserContext* context) { … }
extensions::BluetoothLowEnergyEventRouter::Status GattErrorToRouterError(
BluetoothGattService::GattErrorCode error_code) { … }
extensions::BluetoothLowEnergyEventRouter::Status
DeviceConnectErrorCodeToStatus(BluetoothDevice::ConnectErrorCode error_code) { … }
}
namespace extensions {
BluetoothLowEnergyEventRouter::AttributeValueRequest::AttributeValueRequest(
Delegate::ValueCallback value_callback,
Delegate::ErrorCallback error_callback) { … }
BluetoothLowEnergyEventRouter::AttributeValueRequest::AttributeValueRequest(
base::OnceClosure success_callback,
Delegate::ErrorCallback error_callback) { … }
BluetoothLowEnergyEventRouter::AttributeValueRequest::~AttributeValueRequest() =
default;
BluetoothLowEnergyEventRouter::BluetoothLowEnergyEventRouter(
content::BrowserContext* context)
: … { … }
BluetoothLowEnergyEventRouter::~BluetoothLowEnergyEventRouter() { … }
bool BluetoothLowEnergyEventRouter::IsBluetoothSupported() const { … }
bool BluetoothLowEnergyEventRouter::InitializeAdapterAndInvokeCallback(
base::OnceClosure callback) { … }
bool BluetoothLowEnergyEventRouter::HasAdapter() const { … }
void BluetoothLowEnergyEventRouter::Connect(bool persistent,
const Extension* extension,
const std::string& device_address,
ErrorCallback callback) { … }
void BluetoothLowEnergyEventRouter::Disconnect(
const Extension* extension,
const std::string& device_address,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
std::optional<BluetoothLowEnergyEventRouter::ServiceList>
BluetoothLowEnergyEventRouter::GetServices(
const std::string& device_address) const { … }
base::expected<api::bluetooth_low_energy::Service,
BluetoothLowEnergyEventRouter::Status>
BluetoothLowEnergyEventRouter::GetService(
const std::string& instance_id) const { … }
base::expected<BluetoothLowEnergyEventRouter::ServiceList,
BluetoothLowEnergyEventRouter::Status>
BluetoothLowEnergyEventRouter::GetIncludedServices(
const std::string& instance_id) const { … }
base::expected<BluetoothLowEnergyEventRouter::CharacteristicList,
BluetoothLowEnergyEventRouter::Status>
BluetoothLowEnergyEventRouter::GetCharacteristics(
const Extension* extension,
const std::string& instance_id) const { … }
base::expected<api::bluetooth_low_energy::Characteristic,
BluetoothLowEnergyEventRouter::Status>
BluetoothLowEnergyEventRouter::GetCharacteristic(
const Extension* extension,
const std::string& instance_id) const { … }
base::expected<BluetoothLowEnergyEventRouter::DescriptorList,
BluetoothLowEnergyEventRouter::Status>
BluetoothLowEnergyEventRouter::GetDescriptors(
const Extension* extension,
const std::string& instance_id) const { … }
base::expected<api::bluetooth_low_energy::Descriptor,
BluetoothLowEnergyEventRouter::Status>
BluetoothLowEnergyEventRouter::GetDescriptor(
const Extension* extension,
const std::string& instance_id) const { … }
void BluetoothLowEnergyEventRouter::ReadCharacteristicValue(
const Extension* extension,
const std::string& instance_id,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::WriteCharacteristicValue(
const Extension* extension,
const std::string& instance_id,
const std::vector<uint8_t>& value,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::StartCharacteristicNotifications(
bool persistent,
const Extension* extension,
const std::string& instance_id,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::StopCharacteristicNotifications(
const Extension* extension,
const std::string& instance_id,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::ReadDescriptorValue(
const Extension* extension,
const std::string& instance_id,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::WriteDescriptorValue(
const Extension* extension,
const std::string& instance_id,
const std::vector<uint8_t>& value,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::SetAdapterForTesting(
device::BluetoothAdapter* adapter) { … }
void BluetoothLowEnergyEventRouter::GattServiceAdded(
BluetoothAdapter* adapter,
BluetoothDevice* device,
BluetoothRemoteGattService* service) { … }
void BluetoothLowEnergyEventRouter::GattServiceRemoved(
BluetoothAdapter* adapter,
BluetoothDevice* device,
BluetoothRemoteGattService* service) { … }
void BluetoothLowEnergyEventRouter::GattDiscoveryCompleteForService(
BluetoothAdapter* adapter,
BluetoothRemoteGattService* service) { … }
void BluetoothLowEnergyEventRouter::DeviceAddressChanged(
BluetoothAdapter* adapter,
BluetoothDevice* device,
const std::string& old_address) { … }
void BluetoothLowEnergyEventRouter::GattServiceChanged(
BluetoothAdapter* adapter,
BluetoothRemoteGattService* service) { … }
void BluetoothLowEnergyEventRouter::GattCharacteristicAdded(
BluetoothAdapter* adapter,
BluetoothRemoteGattCharacteristic* characteristic) { … }
void BluetoothLowEnergyEventRouter::GattCharacteristicRemoved(
BluetoothAdapter* adapter,
BluetoothRemoteGattCharacteristic* characteristic) { … }
void BluetoothLowEnergyEventRouter::GattDescriptorAdded(
BluetoothAdapter* adapter,
BluetoothRemoteGattDescriptor* descriptor) { … }
void BluetoothLowEnergyEventRouter::GattDescriptorRemoved(
BluetoothAdapter* adapter,
BluetoothRemoteGattDescriptor* descriptor) { … }
void BluetoothLowEnergyEventRouter::GattCharacteristicValueChanged(
BluetoothAdapter* adapter,
BluetoothRemoteGattCharacteristic* characteristic,
const std::vector<uint8_t>& value) { … }
void BluetoothLowEnergyEventRouter::GattDescriptorValueChanged(
BluetoothAdapter* adapter,
BluetoothRemoteGattDescriptor* descriptor,
const std::vector<uint8_t>& value) { … }
void BluetoothLowEnergyEventRouter::OnCharacteristicReadRequest(
const device::BluetoothDevice* device,
const device::BluetoothLocalGattCharacteristic* characteristic,
int offset,
Delegate::ValueCallback value_callback) { … }
void BluetoothLowEnergyEventRouter::OnCharacteristicWriteRequest(
const device::BluetoothDevice* device,
const device::BluetoothLocalGattCharacteristic* characteristic,
const std::vector<uint8_t>& value,
int offset,
base::OnceClosure callback,
Delegate::ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::OnCharacteristicPrepareWriteRequest(
const device::BluetoothDevice* device,
const device::BluetoothLocalGattCharacteristic* characteristic,
const std::vector<uint8_t>& value,
int offset,
bool has_subsequent_request,
base::OnceClosure callback,
Delegate::ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::OnDescriptorReadRequest(
const device::BluetoothDevice* device,
const device::BluetoothLocalGattDescriptor* descriptor,
int offset,
Delegate::ValueCallback value_callback) { … }
void BluetoothLowEnergyEventRouter::OnDescriptorWriteRequest(
const device::BluetoothDevice* device,
const device::BluetoothLocalGattDescriptor* descriptor,
const std::vector<uint8_t>& value,
int offset,
base::OnceClosure callback,
Delegate::ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::OnNotificationsStart(
const device::BluetoothDevice* device,
device::BluetoothGattCharacteristic::NotificationType notification_type,
const device::BluetoothLocalGattCharacteristic* characteristic) { … }
void BluetoothLowEnergyEventRouter::OnNotificationsStop(
const device::BluetoothDevice* device,
const device::BluetoothLocalGattCharacteristic* characteristic) { … }
void BluetoothLowEnergyEventRouter::OnExtensionUnloaded(
content::BrowserContext* browser_context,
const extensions::Extension* extension,
extensions::UnloadedExtensionReason reason) { … }
void BluetoothLowEnergyEventRouter::AddLocalCharacteristic(
const std::string& id,
const std::string& service_id) { … }
device::BluetoothLocalGattCharacteristic*
BluetoothLowEnergyEventRouter::GetLocalCharacteristic(
const std::string& id) const { … }
void BluetoothLowEnergyEventRouter::AddServiceToApp(
const std::string& app_id,
const std::string& service_id) { … }
void BluetoothLowEnergyEventRouter::RemoveServiceFromApp(
const std::string& app_id,
const std::string& service_id) { … }
void BluetoothLowEnergyEventRouter::RegisterGattService(
const Extension* extension,
const std::string& service_id,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::UnregisterGattService(
const Extension* extension,
const std::string& service_id,
base::OnceClosure callback,
ErrorCallback error_callback) { … }
void BluetoothLowEnergyEventRouter::HandleRequestResponse(
const Extension* extension,
size_t request_id,
bool is_error,
const std::vector<uint8_t>& value) { … }
void BluetoothLowEnergyEventRouter::OnGetAdapter(
base::OnceClosure callback,
scoped_refptr<device::BluetoothAdapter> adapter) { … }
void BluetoothLowEnergyEventRouter::InitializeIdentifierMappings() { … }
void BluetoothLowEnergyEventRouter::DispatchEventToExtensionsWithPermission(
events::HistogramValue histogram_value,
const std::string& event_name,
const device::BluetoothUUID& uuid,
const std::string& characteristic_id,
base::Value::List args) { … }
void BluetoothLowEnergyEventRouter::DispatchEventToExtension(
const ExtensionId& extension_id,
events::HistogramValue histogram_value,
const std::string& event_name,
base::Value::List args) { … }
BluetoothRemoteGattService* BluetoothLowEnergyEventRouter::FindServiceById(
const std::string& instance_id) const { … }
BluetoothRemoteGattCharacteristic*
BluetoothLowEnergyEventRouter::FindCharacteristicById(
const std::string& instance_id) const { … }
BluetoothRemoteGattDescriptor*
BluetoothLowEnergyEventRouter::FindDescriptorById(
const std::string& instance_id) const { … }
void BluetoothLowEnergyEventRouter::OnReadRemoteCharacteristic(
const std::string& characteristic_instance_id,
base::OnceClosure callback,
ErrorCallback error_callback,
std::optional<BluetoothGattService::GattErrorCode> error_code,
const std::vector<uint8_t>& value) { … }
void BluetoothLowEnergyEventRouter::OnReadRemoteDescriptor(
base::OnceClosure callback,
ErrorCallback error_callback,
std::optional<device::BluetoothGattService::GattErrorCode> error_code,
const std::vector<uint8_t>& value) { … }
void BluetoothLowEnergyEventRouter::OnRegisterGattServiceSuccess(
const std::string& service_id,
const ExtensionId& extension_id,
base::OnceClosure callback) { … }
void BluetoothLowEnergyEventRouter::OnUnregisterGattServiceSuccess(
const std::string& service_id,
const ExtensionId& extension_id,
base::OnceClosure callback) { … }
void BluetoothLowEnergyEventRouter::OnCreateGattConnection(
bool persistent,
const ExtensionId& extension_id,
const std::string& device_address,
ErrorCallback callback,
std::unique_ptr<BluetoothGattConnection> connection,
std::optional<BluetoothDevice::ConnectErrorCode> error_code) { … }
void BluetoothLowEnergyEventRouter::OnError(
ErrorCallback error_callback,
BluetoothGattService::GattErrorCode error_code) { … }
void BluetoothLowEnergyEventRouter::OnStartNotifySession(
bool persistent,
const ExtensionId& extension_id,
const std::string& characteristic_id,
base::OnceClosure callback,
std::unique_ptr<device::BluetoothGattNotifySession> session) { … }
void BluetoothLowEnergyEventRouter::OnStartNotifySessionError(
const ExtensionId& extension_id,
const std::string& characteristic_id,
ErrorCallback error_callback,
BluetoothGattService::GattErrorCode error_code) { … }
void BluetoothLowEnergyEventRouter::OnStopNotifySession(
const ExtensionId& extension_id,
const std::string& characteristic_id,
base::OnceClosure callback) { … }
BluetoothLowEnergyConnection* BluetoothLowEnergyEventRouter::FindConnection(
const ExtensionId& extension_id,
const std::string& device_address) { … }
bool BluetoothLowEnergyEventRouter::RemoveConnection(
const ExtensionId& extension_id,
const std::string& device_address) { … }
BluetoothLowEnergyNotifySession*
BluetoothLowEnergyEventRouter::FindNotifySession(
const ExtensionId& extension_id,
const std::string& characteristic_id) { … }
bool BluetoothLowEnergyEventRouter::RemoveNotifySession(
const ExtensionId& extension_id,
const std::string& characteristic_id) { … }
size_t BluetoothLowEnergyEventRouter::StoreSentRequest(
const ExtensionId& extension_id,
std::unique_ptr<AttributeValueRequest> request) { … }
}