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 "chrome/browser/ui/webui/bluetooth_internals/services/bluetooth_adapter _service.h" | |
9 #include "mojo/public/cpp/bindings/string.h" | |
10 | |
11 BluetoothAdapterService::BluetoothAdapterService( | |
12 bluetooth::AdapterClientPtr client) | |
13 : client_(std::move(client)), weak_ptr_factory_(this) { | |
14 if (!GetAdapter()) { | |
15 device::BluetoothAdapterFactoryWrapper::Get().AcquireAdapter( | |
ortuno
2016/09/22 08:32:53
Don't use the Wrapper. That class was added becaus
mbrunson
2016/09/24 01:05:47
Done.
| |
16 this, base::Bind(&BluetoothAdapterService::OnAdapterAcquired, | |
17 weak_ptr_factory_.GetWeakPtr())); | |
18 } | |
19 } | |
20 | |
21 BluetoothAdapterService::~BluetoothAdapterService() {} | |
22 | |
23 void BluetoothAdapterService::OnAdapterAcquired( | |
24 device::BluetoothAdapter* adapter) { | |
25 VLOG(1) << "Adapter acquired in BluetoothAdapterService"; | |
26 | |
27 for (const device::BluetoothDevice* device : adapter->GetDevices()) { | |
ortuno
2016/09/22 08:32:54
q: Why do you do this after adapter construction r
mbrunson
2016/09/24 01:05:47
Done.
| |
28 addresses_.insert(device->GetAddress()); | |
29 } | |
30 } | |
31 | |
32 void BluetoothAdapterService::GetDevices(int8_t index, | |
33 int8_t count, | |
34 const GetDevicesCallback& callback) { | |
35 std::vector<bluetooth::DeviceInfoPtr> result; | |
ortuno
2016/09/22 08:32:53
optional nit: "devices" is more specific.
mbrunson
2016/09/24 01:05:47
Done.
| |
36 | |
37 for (const std::string& address : addresses_) { | |
38 if (result.size() >= (size_t)(count)) { | |
39 break; | |
40 } | |
41 | |
42 device::BluetoothDevice* device = GetAdapter()->GetDevice(address); | |
43 bluetooth::DeviceInfoPtr device_info = GetDeviceInfo(device); | |
44 result.push_back(std::move(device_info)); | |
45 } | |
46 | |
47 callback.Run(std::move(result)); | |
48 } | |
49 | |
50 device::BluetoothAdapter* BluetoothAdapterService::GetAdapter() { | |
51 return device::BluetoothAdapterFactoryWrapper::Get().GetAdapter(this); | |
52 } | |
53 | |
54 void BluetoothAdapterService::DeviceAdded(device::BluetoothAdapter* adapter, | |
55 device::BluetoothDevice* device) { | |
56 std::string device_address = device->GetAddress(); | |
57 | |
58 // If known address was added, alert client | |
59 if (addresses_.insert(device_address).second && client_) { | |
ortuno
2016/09/22 08:32:53
The comment says that if a *known* address was add
mbrunson
2016/09/24 01:05:47
That's a typo.
I don't expect to see known addres
ortuno
2016/09/26 01:57:55
We should be able to trust the API to not mess up.
| |
60 device::BluetoothDevice* device = GetAdapter()->GetDevice(device_address); | |
61 auto device_info = GetDeviceInfo(device); | |
62 client_->DeviceAdded(std::move(device_info)); | |
63 } | |
64 } | |
65 | |
66 void BluetoothAdapterService::DeviceRemoved(device::BluetoothAdapter* adapter, | |
67 device::BluetoothDevice* device) { | |
68 std::string device_address = device->GetAddress(); | |
69 | |
70 // If known address was removed, alert client | |
71 if (addresses_.erase(device_address) && client_) { | |
72 device::BluetoothDevice* device = GetAdapter()->GetDevice(device_address); | |
73 auto device_info = GetDeviceInfo(device); | |
74 client_->DeviceRemoved(std::move(device_info)); | |
75 } | |
76 } | |
77 | |
78 bluetooth::DeviceInfoPtr BluetoothAdapterService::GetDeviceInfo( | |
ortuno
2016/09/22 08:32:54
optional nit: ConstructDeviceInfoStruct would be m
mbrunson
2016/09/24 01:05:47
Done.
| |
79 device::BluetoothDevice* device) { | |
80 bluetooth::DeviceInfoPtr device_info = bluetooth::DeviceInfo::New(); | |
81 device_info->name = device->GetName().value_or(""); | |
ortuno
2016/09/22 08:32:53
I think you need to surround this by an if stateme
mbrunson
2016/09/24 01:05:47
Removing the value_or should be enough. Name is an
| |
82 device_info->name_for_display = | |
83 base::UTF16ToUTF8(device->GetNameForDisplay()); | |
84 device_info->id = device->GetIdentifier(); | |
85 device_info->address = device->GetAddress(); | |
86 device_info->device_id = device->GetDeviceID(); | |
87 device_info->product_id = device->GetProductID(); | |
88 device_info->vendor_id = device->GetVendorID(); | |
89 return device_info; | |
90 } | |
OLD | NEW |