OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <utility> | |
6 #include <vector> | |
7 | |
8 #include "device/bluetooth/adapter.h" | |
9 #include "mojo/public/cpp/bindings/string.h" | |
10 #include "mojo/public/cpp/bindings/strong_binding.h" | |
ortuno
2016/09/28 09:44:55
Also include:
base/memory/ptr_util.h
mbrunson
2016/09/28 21:20:35
Done.
| |
11 | |
12 namespace bluetooth { | |
13 | |
14 Adapter::Adapter() : client_(nullptr), weak_ptr_factory_(this) {} | |
15 | |
16 Adapter::~Adapter() { | |
17 if (adapter_.get()) { | |
18 adapter_->RemoveObserver(this); | |
19 adapter_ = nullptr; | |
20 } | |
21 } | |
22 | |
23 // static | |
24 void Adapter::Create(mojom::AdapterRequest request) { | |
25 mojo::MakeStrongBinding(base::MakeUnique<Adapter>(), std::move(request)); | |
26 } | |
27 | |
28 void Adapter::GetDevices(const GetDevicesCallback& callback) { | |
29 if (!GetAdapter().get()) { | |
30 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { | |
31 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | |
ortuno
2016/09/28 09:44:55
optional: I think it might make more sense to call
mbrunson
2016/09/28 21:20:35
This is the exact feature I was looking for when I
| |
32 &Adapter::GetDevicesImpl, weak_ptr_factory_.GetWeakPtr(), callback)); | |
33 return; | |
34 } | |
35 callback.Run(std::vector<mojom::DeviceInfoPtr>()); | |
36 return; | |
37 } | |
38 GetDevicesImpl(callback, GetAdapter()); | |
39 } | |
40 | |
41 void Adapter::SetClient(mojom::AdapterClientPtr client) { | |
42 client_ = std::move(client); | |
43 } | |
44 | |
45 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter, | |
46 device::BluetoothDevice* device) { | |
47 std::string device_address = device->GetAddress(); | |
48 | |
49 // If unknown address was added, alert client | |
scheib
2016/09/28 03:12:21
Remove comment
mbrunson
2016/09/28 21:20:35
Done.
| |
50 if (client_) { | |
51 auto device_info = ConstructDeviceInfoStruct(device); | |
52 client_->DeviceAdded(std::move(device_info)); | |
53 } | |
54 } | |
55 | |
56 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, | |
57 device::BluetoothDevice* device) { | |
58 std::string device_address = device->GetAddress(); | |
ortuno
2016/09/28 09:44:55
I think this is unused? I'm surprised the compiler
mbrunson
2016/09/28 21:20:35
Yeah. That's unusual. Done.
| |
59 | |
60 // If known address was removed, alert client | |
scheib
2016/09/28 03:12:21
Remove comment
mbrunson
2016/09/28 21:20:35
Done.
| |
61 if (client_) { | |
62 auto device_info = ConstructDeviceInfoStruct(device); | |
63 client_->DeviceRemoved(std::move(device_info)); | |
64 } | |
65 } | |
66 | |
67 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( | |
68 const device::BluetoothDevice* device) const { | |
69 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); | |
70 | |
71 device_info->name = device->GetName(); | |
72 device_info->name_for_display = | |
73 base::UTF16ToUTF8(device->GetNameForDisplay()); | |
74 device_info->id = device->GetIdentifier(); | |
75 device_info->address = device->GetAddress(); | |
76 | |
77 return device_info; | |
78 } | |
79 | |
80 scoped_refptr<device::BluetoothAdapter> Adapter::GetAdapter() { | |
ortuno
2016/09/28 09:44:55
nit: Seems like accessing adapter_ directly might
mbrunson
2016/09/28 21:20:35
Done.
| |
81 return adapter_; | |
82 } | |
83 | |
84 void Adapter::GetDevicesImpl(const GetDevicesCallback& callback, | |
85 scoped_refptr<device::BluetoothAdapter> adapter) { | |
86 OnGetAdapter(adapter); | |
87 std::vector<mojom::DeviceInfoPtr> devices; | |
88 | |
89 for (const device::BluetoothDevice* device : adapter->GetDevices()) { | |
90 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device); | |
91 devices.push_back(std::move(device_info)); | |
92 } | |
93 | |
94 callback.Run(std::move(devices)); | |
95 } | |
96 | |
97 void Adapter::OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter) { | |
98 if (!adapter_.get()) { | |
99 VLOG(1) << "Adapter acquired"; | |
100 adapter_ = adapter; | |
101 adapter_->AddObserver(this); | |
102 } | |
103 } | |
104 | |
105 } // namespace bluetooth | |
OLD | NEW |