OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 #include <utility> | 6 #include <utility> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 12 matching lines...) Loading... | |
23 adapter_ = nullptr; | 23 adapter_ = nullptr; |
24 } | 24 } |
25 } | 25 } |
26 | 26 |
27 // static | 27 // static |
28 void Adapter::Create(mojom::AdapterRequest request) { | 28 void Adapter::Create(mojom::AdapterRequest request) { |
29 mojo::MakeStrongBinding(base::MakeUnique<Adapter>(), std::move(request)); | 29 mojo::MakeStrongBinding(base::MakeUnique<Adapter>(), std::move(request)); |
30 } | 30 } |
31 | 31 |
32 void Adapter::GetDevices(const GetDevicesCallback& callback) { | 32 void Adapter::GetDevices(const GetDevicesCallback& callback) { |
33 if (!adapter_.get()) { | 33 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
| |
34 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | 34 [](const GetDevicesCallback& callback, |
35 base::Closure c = base::Bind(&Adapter::GetDevicesImpl, | 35 scoped_refptr<device::BluetoothAdapter> adapter) { |
36 weak_ptr_factory_.GetWeakPtr(), callback); | 36 if (adapter) { |
37 std::vector<mojom::DeviceInfoPtr> devices; | |
37 | 38 |
38 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | 39 for (const device::BluetoothDevice* device : adapter->GetDevices()) { |
39 &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), c)); | 40 mojom::DeviceInfoPtr device_info = |
40 return; | 41 ConstructDeviceInfoStruct(device); |
41 } | 42 devices.push_back(std::move(device_info)); |
42 callback.Run(std::vector<mojom::DeviceInfoPtr>()); | 43 } |
43 return; | 44 |
44 } | 45 callback.Run(std::move(devices)); |
45 GetDevicesImpl(callback); | 46 } else { |
47 callback.Run(std::vector<mojom::DeviceInfoPtr>()); | |
48 } | |
49 }, | |
50 callback)); | |
46 } | 51 } |
47 | 52 |
48 void Adapter::SetClient(mojom::AdapterClientPtr client) { | 53 void Adapter::SetClient(mojom::AdapterClientPtr client) { |
49 client_ = std::move(client); | 54 client_ = std::move(client); |
50 } | 55 } |
51 | 56 |
52 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter, | 57 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter, |
53 device::BluetoothDevice* device) { | 58 device::BluetoothDevice* device) { |
54 if (client_) { | 59 if (client_) { |
55 auto device_info = ConstructDeviceInfoStruct(device); | 60 auto device_info = ConstructDeviceInfoStruct(device); |
56 client_->DeviceAdded(std::move(device_info)); | 61 client_->DeviceAdded(std::move(device_info)); |
57 } | 62 } |
58 } | 63 } |
59 | 64 |
60 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, | 65 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, |
61 device::BluetoothDevice* device) { | 66 device::BluetoothDevice* device) { |
62 if (client_) { | 67 if (client_) { |
63 auto device_info = ConstructDeviceInfoStruct(device); | 68 auto device_info = ConstructDeviceInfoStruct(device); |
64 client_->DeviceRemoved(std::move(device_info)); | 69 client_->DeviceRemoved(std::move(device_info)); |
65 } | 70 } |
66 } | 71 } |
67 | 72 |
73 // static | |
68 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( | 74 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( |
69 const device::BluetoothDevice* device) const { | 75 const device::BluetoothDevice* const device) { |
70 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); | 76 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); |
71 | 77 |
72 device_info->name = device->GetName(); | 78 device_info->name = device->GetName(); |
73 device_info->name_for_display = | 79 device_info->name_for_display = |
74 base::UTF16ToUTF8(device->GetNameForDisplay()); | 80 base::UTF16ToUTF8(device->GetNameForDisplay()); |
75 device_info->id = device->GetIdentifier(); | 81 device_info->id = device->GetIdentifier(); |
76 device_info->address = device->GetAddress(); | 82 device_info->address = device->GetAddress(); |
77 | 83 |
78 return device_info; | 84 return device_info; |
79 } | 85 } |
80 | 86 |
81 void Adapter::GetDevicesImpl(const GetDevicesCallback& callback) { | 87 void Adapter::WithAdapter( |
82 std::vector<mojom::DeviceInfoPtr> devices; | 88 const device::BluetoothAdapterFactory::AdapterCallback& action) { |
83 | 89 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.
| |
84 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | 90 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.
| |
85 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); | 91 device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
86 devices.push_back(std::move(device_info)); | 92 &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), action)); |
93 return; | |
94 } | |
87 } | 95 } |
88 | 96 action.Run(adapter_); |
89 callback.Run(std::move(devices)); | |
90 } | 97 } |
91 | 98 |
92 void Adapter::OnGetAdapter(const base::Closure& continuation, | 99 void Adapter::OnGetAdapter( |
93 scoped_refptr<device::BluetoothAdapter> adapter) { | 100 const device::BluetoothAdapterFactory::AdapterCallback& continuation, |
101 scoped_refptr<device::BluetoothAdapter> adapter) { | |
94 if (!adapter_.get()) { | 102 if (!adapter_.get()) { |
95 VLOG(1) << "Adapter acquired"; | 103 VLOG(1) << "Adapter acquired"; |
96 adapter_ = adapter; | 104 adapter_ = adapter; |
97 adapter_->AddObserver(this); | 105 adapter_->AddObserver(this); |
98 } | 106 } |
99 continuation.Run(); | 107 continuation.Run(adapter); |
100 } | 108 } |
101 | 109 |
102 } // namespace bluetooth | 110 } // namespace bluetooth |
OLD | NEW |