1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CONTENT_BROWSER_DEVTOOLS_PROTOCOL_BLUETOOTH_EMULATION_HANDLER_H_
#define CONTENT_BROWSER_DEVTOOLS_PROTOCOL_BLUETOOTH_EMULATION_HANDLER_H_
#include "content/browser/devtools/protocol/bluetooth_emulation.h"
#include "content/browser/devtools/protocol/devtools_domain_handler.h"
#include "content/common/content_export.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/public/mojom/emulation/fake_bluetooth.mojom.h"
namespace content::protocol {
// Class that implements the BluetoothEmulation domain, to allow
// configuring virtual Bluetooth devices to test the web-bluetooth API.
// See
// [device/bluetooth/test/README.md](https://chromium.googlesource.com/chromium/src/+/main/device/bluetooth/test/README.md)
class CONTENT_EXPORT BluetoothEmulationHandler
: public DevToolsDomainHandler,
public BluetoothEmulation::Backend,
public bluetooth::mojom::FakeCentralClient {
public:
BluetoothEmulationHandler();
~BluetoothEmulationHandler() override;
static std::vector<BluetoothEmulationHandler*> ForAgentHost(
DevToolsAgentHostImpl* host);
// DevToolsDomainHandler:
void Wire(UberDispatcher* dispatcher) override;
private:
// BluetoothEmulation::Backend
Response Enable(const std::string& in_state, bool in_le_supported) override;
Response Disable() override;
void SetSimulatedCentralState(
const std::string& in_state,
std::unique_ptr<SetSimulatedCentralStateCallback> callback) override;
void SimulatePreconnectedPeripheral(
const std::string& in_address,
const std::string& in_name,
std::unique_ptr<
protocol::Array<protocol::BluetoothEmulation::ManufacturerData>>
in_manufacturer_data,
std::unique_ptr<protocol::Array<std::string>> in_known_service_uuids,
std::unique_ptr<SimulatePreconnectedPeripheralCallback> callback)
override;
// Simulates an advertisement packet described by |in_entry| being received
// from a device. If central is currently scanning, the device will appear on
// the list of discovered devices.
void SimulateAdvertisement(
std::unique_ptr<protocol::BluetoothEmulation::ScanEntry> in_entry,
std::unique_ptr<SimulateAdvertisementCallback> callback) override;
void SimulateGATTOperationResponse(
const std::string& in_address,
const std::string& in_type,
int in_code,
std::unique_ptr<SimulateGATTOperationResponseCallback> callback) override;
void SimulateCharacteristicOperationResponse(
const std::string& characteristic_id,
const std::string& in_type,
int in_code,
std::optional<Binary> in_data,
std::unique_ptr<SimulateCharacteristicOperationResponseCallback> callback)
override;
void SimulateDescriptorOperationResponse(
const std::string& descriptor_id,
const std::string& in_type,
int in_code,
std::optional<Binary> in_data,
std::unique_ptr<SimulateDescriptorOperationResponseCallback> callback)
override;
void AddService(const std::string& in_address,
const std::string& service_uuid,
std::unique_ptr<AddServiceCallback> callback) override;
void RemoveService(const std::string& service_id,
std::unique_ptr<RemoveServiceCallback> callback) override;
void AddCharacteristic(
const std::string& service_id,
const std::string& in_characteristicUuid,
std::unique_ptr<protocol::BluetoothEmulation::CharacteristicProperties>
in_properties,
std::unique_ptr<AddCharacteristicCallback> callback) override;
void RemoveCharacteristic(
const std::string& characteristic_id,
std::unique_ptr<RemoveCharacteristicCallback> callback) override;
void AddDescriptor(const std::string& characteristic_id,
const std::string& descriptor_uuid,
std::unique_ptr<AddDescriptorCallback> callback) override;
void RemoveDescriptor(
const std::string& in_descriptorId,
std::unique_ptr<RemoveDescriptorCallback> callback) override;
void SimulateGATTDisconnection(
const std::string& address,
std::unique_ptr<SimulateGATTDisconnectionCallback> callback) override;
// bluetooth::mojom::FakeCentralClient
void DispatchGATTOperationEvent(
bluetooth::mojom::GATTOperationType type,
const std::string& peripheral_address) override;
void DispatchCharacteristicOperationEvent(
bluetooth::mojom::CharacteristicOperationType type,
const std::optional<std::vector<uint8_t>>& data,
const std::optional<bluetooth::mojom::WriteType> write_type,
const std::string& characteristic_id) override;
void DispatchDescriptorOperationEvent(
bluetooth::mojom::DescriptorOperationType type,
const std::optional<std::vector<uint8_t>>& data,
const std::string& descriptor_id) override;
bool is_enabled() { return fake_central_.is_bound(); }
std::optional<BluetoothEmulation::Frontend> frontend_;
// Tracks emulation usage across all instances, since the backend can only
// support one fake adapter at a time.
inline static bool emulation_enabled_ = false;
mojo::Remote<bluetooth::mojom::FakeCentral> fake_central_;
mojo::AssociatedReceiver<bluetooth::mojom::FakeCentralClient>
client_receiver_{this};
std::unique_ptr<device::BluetoothAdapterFactory::GlobalOverrideValues>
global_factory_values_;
};
} // namespace content::protocol
#endif // CONTENT_BROWSER_DEVTOOLS_PROTOCOL_BLUETOOTH_EMULATION_HANDLER_H_
|