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" |
11 #include "device/bluetooth/adapter.h" | 11 #include "device/bluetooth/adapter.h" |
12 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
13 #include "mojo/public/cpp/bindings/string.h" | 12 #include "mojo/public/cpp/bindings/string.h" |
14 #include "mojo/public/cpp/bindings/strong_binding.h" | 13 #include "mojo/public/cpp/bindings/strong_binding.h" |
15 | 14 |
16 namespace bluetooth { | 15 namespace bluetooth { |
17 | 16 |
18 Adapter::Adapter() : client_(nullptr), weak_ptr_factory_(this) {} | 17 Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) |
18 : adapter_(adapter), client_(nullptr), weak_ptr_factory_(this) { | |
dcheng
2016/10/06 04:01:39
Nit: std::move(adapter)
mbrunson
2016/10/06 20:25:57
Done.
| |
19 adapter_->AddObserver(this); | |
20 } | |
19 | 21 |
20 Adapter::~Adapter() { | 22 Adapter::~Adapter() { |
21 if (adapter_) { | 23 adapter_->RemoveObserver(this); |
22 adapter_->RemoveObserver(this); | 24 adapter_ = nullptr; |
23 adapter_ = nullptr; | |
24 } | |
25 } | |
26 | |
27 // static | |
28 void Adapter::Create(mojom::AdapterRequest request) { | |
29 mojo::MakeStrongBinding(base::MakeUnique<Adapter>(), std::move(request)); | |
30 } | 25 } |
31 | 26 |
32 void Adapter::GetDevices(const GetDevicesCallback& callback) { | 27 void Adapter::GetDevices(const GetDevicesCallback& callback) { |
33 if (!adapter_) { | 28 std::vector<mojom::DeviceInfoPtr> devices; |
34 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | |
35 base::Closure c = base::Bind(&Adapter::GetDevicesImpl, | |
36 weak_ptr_factory_.GetWeakPtr(), callback); | |
37 | 29 |
38 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | 30 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
39 &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), c)); | 31 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); |
40 return; | 32 devices.push_back(std::move(device_info)); |
41 } | |
42 callback.Run(std::vector<mojom::DeviceInfoPtr>()); | |
43 return; | |
44 } | 33 } |
45 GetDevicesImpl(callback); | 34 |
35 callback.Run(std::move(devices)); | |
46 } | 36 } |
47 | 37 |
48 void Adapter::SetClient(mojom::AdapterClientPtr client) { | 38 void Adapter::SetClient(mojom::AdapterClientPtr client) { |
49 client_ = std::move(client); | 39 client_ = std::move(client); |
50 } | 40 } |
51 | 41 |
52 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter, | 42 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter, |
53 device::BluetoothDevice* device) { | 43 device::BluetoothDevice* device) { |
54 if (client_) { | 44 if (client_) { |
55 auto device_info = ConstructDeviceInfoStruct(device); | 45 auto device_info = ConstructDeviceInfoStruct(device); |
56 client_->DeviceAdded(std::move(device_info)); | 46 client_->DeviceAdded(std::move(device_info)); |
57 } | 47 } |
58 } | 48 } |
59 | 49 |
60 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, | 50 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, |
61 device::BluetoothDevice* device) { | 51 device::BluetoothDevice* device) { |
62 if (client_) { | 52 if (client_) { |
63 auto device_info = ConstructDeviceInfoStruct(device); | 53 auto device_info = ConstructDeviceInfoStruct(device); |
64 client_->DeviceRemoved(std::move(device_info)); | 54 client_->DeviceRemoved(std::move(device_info)); |
65 } | 55 } |
66 } | 56 } |
67 | 57 |
58 // static | |
68 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( | 59 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( |
69 const device::BluetoothDevice* device) const { | 60 const device::BluetoothDevice* const device) { |
70 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); | 61 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); |
71 | 62 |
72 device_info->name = device->GetName(); | 63 device_info->name = device->GetName(); |
73 device_info->name_for_display = | 64 device_info->name_for_display = |
74 base::UTF16ToUTF8(device->GetNameForDisplay()); | 65 base::UTF16ToUTF8(device->GetNameForDisplay()); |
dcheng
2016/10/06 18:28:15
(Not related to this CL, but it seems a bit silly
mbrunson
2016/10/06 20:25:57
Yes. It is silly. If there was a straightforward w
| |
75 device_info->id = device->GetIdentifier(); | 66 device_info->id = device->GetIdentifier(); |
76 device_info->address = device->GetAddress(); | 67 device_info->address = device->GetAddress(); |
77 | 68 |
78 return device_info; | 69 return device_info; |
79 } | 70 } |
80 | 71 |
81 void Adapter::GetDevicesImpl(const GetDevicesCallback& callback) { | |
82 std::vector<mojom::DeviceInfoPtr> devices; | |
83 | |
84 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | |
85 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); | |
86 devices.push_back(std::move(device_info)); | |
87 } | |
88 | |
89 callback.Run(std::move(devices)); | |
90 } | |
91 | |
92 void Adapter::OnGetAdapter(const base::Closure& continuation, | |
93 scoped_refptr<device::BluetoothAdapter> adapter) { | |
94 if (!adapter_) { | |
95 VLOG(1) << "Adapter acquired"; | |
96 adapter_ = adapter; | |
97 adapter_->AddObserver(this); | |
98 } | |
99 continuation.Run(); | |
100 } | |
101 | |
102 } // namespace bluetooth | 72 } // namespace bluetooth |
OLD | NEW |