Chromium Code Reviews| Index: device/bluetooth/adapter.cc |
| diff --git a/device/bluetooth/adapter.cc b/device/bluetooth/adapter.cc |
| index 8a73a71ac10e0e210eefc28bf520b1422e99cef1..6e5a0613cc398efa7f7552cf03766bf89f827ad4 100644 |
| --- a/device/bluetooth/adapter.cc |
| +++ b/device/bluetooth/adapter.cc |
| @@ -30,19 +30,24 @@ void Adapter::Create(mojom::AdapterRequest request) { |
| } |
| void Adapter::GetDevices(const GetDevicesCallback& callback) { |
| - if (!adapter_.get()) { |
| - if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| - base::Closure c = base::Bind(&Adapter::GetDevicesImpl, |
| - weak_ptr_factory_.GetWeakPtr(), callback); |
| - |
| - device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| - &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), c)); |
| - return; |
| - } |
| - callback.Run(std::vector<mojom::DeviceInfoPtr>()); |
| - return; |
| - } |
| - GetDevicesImpl(callback); |
| + WithAdapter(base::Bind( |
|
ortuno
2016/09/29 22:55:54
We can't use Lambda Expressions if the lambda expr
mbrunson
2016/09/30 00:15:07
Doesn't this only apply to capturing lambdas? This
scheib
2016/09/30 06:35:42
I /think/ this is OK, as mbrunson states because i
scheib
2016/09/30 06:50:27
I've asked the lambda thread for thoughts. https:/
scheib
2016/09/30 21:52:01
Thread approved, citing previous thread, and style
|
| + [](const GetDevicesCallback& callback, |
| + scoped_refptr<device::BluetoothAdapter> adapter) { |
| + if (adapter) { |
| + std::vector<mojom::DeviceInfoPtr> devices; |
| + |
| + for (const device::BluetoothDevice* device : adapter->GetDevices()) { |
| + mojom::DeviceInfoPtr device_info = |
| + ConstructDeviceInfoStruct(device); |
| + devices.push_back(std::move(device_info)); |
| + } |
| + |
| + callback.Run(std::move(devices)); |
| + } else { |
| + callback.Run(std::vector<mojom::DeviceInfoPtr>()); |
| + } |
| + }, |
| + callback)); |
| } |
| void Adapter::SetClient(mojom::AdapterClientPtr client) { |
| @@ -65,8 +70,9 @@ void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, |
| } |
| } |
| +// static |
| mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( |
| - const device::BluetoothDevice* device) const { |
| + const device::BluetoothDevice* const device) { |
| mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); |
| device_info->name = device->GetName(); |
| @@ -78,25 +84,27 @@ mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( |
| return device_info; |
| } |
| -void Adapter::GetDevicesImpl(const GetDevicesCallback& callback) { |
| - std::vector<mojom::DeviceInfoPtr> devices; |
| - |
| - for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
| - mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); |
| - devices.push_back(std::move(device_info)); |
| +void Adapter::WithAdapter( |
| + const device::BluetoothAdapterFactory::AdapterCallback& action) { |
| + if (!adapter_.get()) { |
|
scheib
2016/09/30 06:35:42
Keep logic simple if both if and else are run:
if
mbrunson
2016/09/30 21:14:38
Done.
|
| + if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
|
scheib
2016/09/30 06:35:42
If the adapter isn't available run the action with
mbrunson
2016/09/30 21:14:38
Done.
|
| + device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| + &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), action)); |
| + return; |
| + } |
| } |
| - |
| - callback.Run(std::move(devices)); |
| + action.Run(adapter_); |
| } |
| -void Adapter::OnGetAdapter(const base::Closure& continuation, |
| - scoped_refptr<device::BluetoothAdapter> adapter) { |
| +void Adapter::OnGetAdapter( |
| + const device::BluetoothAdapterFactory::AdapterCallback& continuation, |
| + scoped_refptr<device::BluetoothAdapter> adapter) { |
| if (!adapter_.get()) { |
| VLOG(1) << "Adapter acquired"; |
| adapter_ = adapter; |
| adapter_->AddObserver(this); |
| } |
| - continuation.Run(); |
| + continuation.Run(adapter); |
| } |
| } // namespace bluetooth |