Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1527)

Unified Diff: device/bluetooth/adapter.cc

Issue 2379573006: bluetooth: Standardize Bluetooth adapter access in Adapter service. (Closed)
Patch Set: Simplify WithAdapter logic Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « device/bluetooth/adapter.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/bluetooth/adapter.cc
diff --git a/device/bluetooth/adapter.cc b/device/bluetooth/adapter.cc
index 2f7aff0be4c195e18a1570e55852ea00b95a5268..06c2232972b20b3974a1184cb5908e893691efdf 100644
--- a/device/bluetooth/adapter.cc
+++ b/device/bluetooth/adapter.cc
@@ -30,19 +30,23 @@ void Adapter::Create(mojom::AdapterRequest request) {
}
void Adapter::GetDevices(const GetDevicesCallback& callback) {
- if (!adapter_) {
- 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(
+ [](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 +69,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 +83,30 @@ mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct(
return device_info;
}
-void Adapter::GetDevicesImpl(const GetDevicesCallback& callback) {
- std::vector<mojom::DeviceInfoPtr> devices;
+void Adapter::WithAdapter(
+ const device::BluetoothAdapterFactory::AdapterCallback& action) {
+ if (adapter_) {
+ action.Run(adapter_);
scheib 2016/09/30 21:52:01 add return here, or remove the return below and ch
mbrunson 2016/09/30 22:13:02 Done.
+ }
- for (const device::BluetoothDevice* device : adapter_->GetDevices()) {
- mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device);
- devices.push_back(std::move(device_info));
+ if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
+ device::BluetoothAdapterFactory::GetAdapter(base::Bind(
+ &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), action));
+ return;
}
- callback.Run(std::move(devices));
+ action.Run(nullptr);
}
-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_) {
VLOG(1) << "Adapter acquired";
adapter_ = adapter;
adapter_->AddObserver(this);
}
- continuation.Run();
+ continuation.Run(adapter);
}
} // namespace bluetooth
« no previous file with comments | « device/bluetooth/adapter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698