Chromium Code Reviews| Index: device/bluetooth/adapter.cc |
| diff --git a/device/bluetooth/adapter.cc b/device/bluetooth/adapter.cc |
| index 639e7a2fd501b984dcbe1fc63486200e7523ef1c..140999708b0b82de5461ebebe4549d5998e16546 100644 |
| --- a/device/bluetooth/adapter.cc |
| +++ b/device/bluetooth/adapter.cc |
| @@ -8,9 +8,52 @@ |
| #include "device/bluetooth/adapter.h" |
| #include "device/bluetooth/device.h" |
| -#include "mojo/public/cpp/bindings/strong_binding.h" |
| namespace bluetooth { |
| +namespace { |
| +mojom::ConnectResult BluetoothErrorCodeToMojomResult( |
| + device::BluetoothDevice::ConnectErrorCode error_code) { |
|
dcheng
2016/11/11 01:25:32
Let's use EnumTraits for this: https://www.chromiu
mbrunson
2016/11/11 21:26:15
Is it possible to use EnumTraits when the C++ clas
dcheng
2016/11/15 07:24:02
Yes, that's not a problem.
(If the conversion is
mbrunson
2016/11/16 03:32:03
I talked to yzshen about this. It's not possible.
dcheng
2016/11/17 01:31:47
Why would it need to do that? We only need to hand
ortuno
2016/11/17 03:54:44
I think what mbrunson meant is that the mojo enum
mbrunson
2016/11/17 21:28:01
Following our chat discussion: Has a simpler alter
dcheng
2016/11/17 21:35:36
Sorry for the slow responses. I really don't think
mbrunson
2016/11/17 23:00:43
Hmm. It seems really awkward to start an action in
mbrunson
2016/11/18 01:53:06
TypeConverter added. Done.
|
| + switch (error_code) { |
| + case device::BluetoothDevice::ConnectErrorCode:: |
| + ERROR_ATTRIBUTE_LENGTH_INVALID: |
| + return mojom::ConnectResult::ATTRIBUTE_LENGTH_INVALID; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED: |
| + return mojom::ConnectResult::AUTH_CANCELED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_FAILED: |
| + return mojom::ConnectResult::AUTH_FAILED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED: |
| + return mojom::ConnectResult::AUTH_REJECTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_TIMEOUT: |
| + return mojom::ConnectResult::AUTH_TIMEOUT; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_CONNECTION_CONGESTED: |
| + return mojom::ConnectResult::CONNECTION_CONGESTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_FAILED: |
| + return mojom::ConnectResult::FAILED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS: |
| + return mojom::ConnectResult::INPROGRESS; |
| + case device::BluetoothDevice::ConnectErrorCode:: |
| + ERROR_INSUFFICIENT_ENCRYPTION: |
| + return mojom::ConnectResult::INSUFFICIENT_ENCRYPTION; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_OFFSET_INVALID: |
| + return mojom::ConnectResult::OFFSET_INVALID; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_READ_NOT_PERMITTED: |
| + return mojom::ConnectResult::READ_NOT_PERMITTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_REQUEST_NOT_SUPPORTED: |
| + return mojom::ConnectResult::REQUEST_NOT_SUPPORTED; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN: |
| + return mojom::ConnectResult::UNKNOWN; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_UNSUPPORTED_DEVICE: |
| + return mojom::ConnectResult::UNSUPPORTED_DEVICE; |
| + case device::BluetoothDevice::ConnectErrorCode::ERROR_WRITE_NOT_PERMITTED: |
| + return mojom::ConnectResult::WRITE_NOT_PERMITTED; |
| + case device::BluetoothDevice::ConnectErrorCode::NUM_CONNECT_ERROR_CODES: |
| + NOTREACHED(); |
| + return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; |
| + } |
| + NOTREACHED(); |
| + return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; |
| +} |
| +} // namespace |
| Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) |
| : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) { |
| @@ -34,12 +77,21 @@ void Adapter::GetInfo(const GetInfoCallback& callback) { |
| callback.Run(std::move(adapter_info)); |
| } |
| -void Adapter::GetDevice(const std::string& address, |
| - const GetDeviceCallback& callback) { |
| - mojom::DevicePtr device_ptr; |
| - mojo::MakeStrongBinding(base::MakeUnique<Device>(address, adapter_), |
| - mojo::GetProxy(&device_ptr)); |
| - callback.Run(std::move(device_ptr)); |
| +void Adapter::ConnectToDevice(const std::string& address, |
| + const ConnectToDeviceCallback& callback) { |
| + device::BluetoothDevice* device = adapter_->GetDevice(address); |
| + |
| + if (!device) { |
| + callback.Run(mojom::ConnectResult::DEVICE_NO_LONGER_IN_RANGE, |
| + nullptr /* device */); |
| + return; |
| + } |
| + |
| + device->CreateGattConnection( |
| + base::Bind(&Adapter::OnGattConnected, weak_ptr_factory_.GetWeakPtr(), |
| + callback), |
| + base::Bind(&Adapter::OnConnectError, weak_ptr_factory_.GetWeakPtr(), |
| + callback)); |
| } |
| void Adapter::GetDevices(const GetDevicesCallback& callback) { |
| @@ -82,4 +134,21 @@ void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, |
| } |
| } |
| +void Adapter::OnGattConnected( |
| + const ConnectToDeviceCallback& callback, |
| + std::unique_ptr<device::BluetoothGattConnection> connection) { |
| + mojom::DevicePtr device_ptr; |
| + |
| + // Owns itself. |
| + new Device(adapter_, std::move(connection), mojo::GetProxy(&device_ptr)); |
| + |
| + callback.Run(mojom::ConnectResult::SUCCESS, std::move(device_ptr)); |
| +} |
| + |
| +void Adapter::OnConnectError( |
| + const ConnectToDeviceCallback& callback, |
| + device::BluetoothDevice::ConnectErrorCode error_code) { |
| + mojom::ConnectResult code = BluetoothErrorCodeToMojomResult(error_code); |
| + callback.Run(code, nullptr /* Device */); |
| +} |
| } // namespace bluetooth |