| Index: device/bluetooth/test/fake_peripheral.cc
|
| diff --git a/device/bluetooth/test/fake_peripheral.cc b/device/bluetooth/test/fake_peripheral.cc
|
| index acf93c736d8f69ce17be05c4cf339d7072f23f9d..a8524e2b3abbac5ed4589b1be8cec23248b6a56a 100644
|
| --- a/device/bluetooth/test/fake_peripheral.cc
|
| +++ b/device/bluetooth/test/fake_peripheral.cc
|
| @@ -4,13 +4,17 @@
|
|
|
| #include "device/bluetooth/test/fake_peripheral.h"
|
|
|
| +#include "base/memory/weak_ptr.h"
|
| +
|
| namespace bluetooth {
|
|
|
| FakePeripheral::FakePeripheral(FakeCentral* fake_central,
|
| const std::string& address)
|
| : device::BluetoothDevice(fake_central),
|
| address_(address),
|
| - gatt_connected_(false) {}
|
| + system_connected_(false),
|
| + gatt_connected_(false),
|
| + weak_ptr_factory_(this) {}
|
|
|
| FakePeripheral::~FakePeripheral() {}
|
|
|
| @@ -18,14 +22,20 @@ void FakePeripheral::SetName(base::Optional<std::string> name) {
|
| name_ = std::move(name);
|
| }
|
|
|
| -void FakePeripheral::SetGattConnected(bool connected) {
|
| - gatt_connected_ = connected;
|
| +void FakePeripheral::SetSystemConnected(bool connected) {
|
| + system_connected_ = connected;
|
| }
|
|
|
| void FakePeripheral::SetServiceUUIDs(UUIDSet service_uuids) {
|
| service_uuids_ = std::move(service_uuids);
|
| }
|
|
|
| +void FakePeripheral::SetNextGATTConnectionResponse(uint16_t code) {
|
| + DCHECK(!next_connection_response_);
|
| + DCHECK(create_gatt_connection_error_callbacks_.empty());
|
| + next_connection_response_ = code;
|
| +}
|
| +
|
| uint32_t FakePeripheral::GetBluetoothClass() const {
|
| NOTREACHED();
|
| return 0;
|
| @@ -92,7 +102,10 @@ bool FakePeripheral::IsConnected() const {
|
| }
|
|
|
| bool FakePeripheral::IsGattConnected() const {
|
| - return gatt_connected_;
|
| + // TODO(crbug.com/728870): Return gatt_connected_ only once system connected
|
| + // peripherals are supported and Web Bluetooth uses them. See issue for more
|
| + // details.
|
| + return system_connected_ || gatt_connected_;
|
| }
|
|
|
| bool FakePeripheral::IsConnectable() const {
|
| @@ -184,11 +197,43 @@ void FakePeripheral::ConnectToServiceInsecurely(
|
| NOTREACHED();
|
| }
|
|
|
| +void FakePeripheral::CreateGattConnection(
|
| + const GattConnectionCallback& callback,
|
| + const ConnectErrorCallback& error_callback) {
|
| + create_gatt_connection_success_callbacks_.push_back(callback);
|
| + create_gatt_connection_error_callbacks_.push_back(error_callback);
|
| +
|
| + // TODO(crbug.com/728870): Stop overriding CreateGattConnection once
|
| + // IsGattConnected() is fixed. See issue for more details.
|
| + if (gatt_connected_)
|
| + return DidConnectGatt();
|
| +
|
| + CreateGattConnectionImpl();
|
| +}
|
| +
|
| void FakePeripheral::CreateGattConnectionImpl() {
|
| - NOTREACHED();
|
| + base::ThreadTaskRunnerHandle::Get()->PostTask(
|
| + FROM_HERE, base::Bind(&FakePeripheral::DispatchConnectionResponse,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| +}
|
| +
|
| +void FakePeripheral::DispatchConnectionResponse() {
|
| + DCHECK(next_connection_response_);
|
| +
|
| + uint16_t code = next_connection_response_.value();
|
| + next_connection_response_.reset();
|
| +
|
| + if (code == mojom::kHCISuccess) {
|
| + gatt_connected_ = true;
|
| + DidConnectGatt();
|
| + } else if (code == mojom::kHCIConnectionTimeout) {
|
| + DidFailToConnectGatt(ERROR_FAILED);
|
| + } else {
|
| + DidFailToConnectGatt(ERROR_UNKNOWN);
|
| + }
|
| }
|
|
|
| void FakePeripheral::DisconnectGatt() {
|
| - NOTREACHED();
|
| }
|
| +
|
| } // namespace bluetooth
|
|
|