Chromium Code Reviews| Index: device/bluetooth/adapter.cc |
| diff --git a/device/bluetooth/adapter.cc b/device/bluetooth/adapter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5b43b819e1b158ef41e395b134a11d35ba807262 |
| --- /dev/null |
| +++ b/device/bluetooth/adapter.cc |
| @@ -0,0 +1,105 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "device/bluetooth/adapter.h" |
| +#include "mojo/public/cpp/bindings/string.h" |
| +#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.
|
| + |
| +namespace bluetooth { |
| + |
| +Adapter::Adapter() : client_(nullptr), weak_ptr_factory_(this) {} |
| + |
| +Adapter::~Adapter() { |
| + if (adapter_.get()) { |
| + adapter_->RemoveObserver(this); |
| + adapter_ = nullptr; |
| + } |
| +} |
| + |
| +// static |
| +void Adapter::Create(mojom::AdapterRequest request) { |
| + mojo::MakeStrongBinding(base::MakeUnique<Adapter>(), std::move(request)); |
| +} |
| + |
| +void Adapter::GetDevices(const GetDevicesCallback& callback) { |
| + if (!GetAdapter().get()) { |
| + if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| + 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
|
| + &Adapter::GetDevicesImpl, weak_ptr_factory_.GetWeakPtr(), callback)); |
| + return; |
| + } |
| + callback.Run(std::vector<mojom::DeviceInfoPtr>()); |
| + return; |
| + } |
| + GetDevicesImpl(callback, GetAdapter()); |
| +} |
| + |
| +void Adapter::SetClient(mojom::AdapterClientPtr client) { |
| + client_ = std::move(client); |
| +} |
| + |
| +void Adapter::DeviceAdded(device::BluetoothAdapter* adapter, |
| + device::BluetoothDevice* device) { |
| + std::string device_address = device->GetAddress(); |
| + |
| + // If unknown address was added, alert client |
|
scheib
2016/09/28 03:12:21
Remove comment
mbrunson
2016/09/28 21:20:35
Done.
|
| + if (client_) { |
| + auto device_info = ConstructDeviceInfoStruct(device); |
| + client_->DeviceAdded(std::move(device_info)); |
| + } |
| +} |
| + |
| +void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, |
| + device::BluetoothDevice* device) { |
| + 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.
|
| + |
| + // If known address was removed, alert client |
|
scheib
2016/09/28 03:12:21
Remove comment
mbrunson
2016/09/28 21:20:35
Done.
|
| + if (client_) { |
| + auto device_info = ConstructDeviceInfoStruct(device); |
| + client_->DeviceRemoved(std::move(device_info)); |
| + } |
| +} |
| + |
| +mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( |
| + const device::BluetoothDevice* device) const { |
| + mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); |
| + |
| + device_info->name = device->GetName(); |
| + device_info->name_for_display = |
| + base::UTF16ToUTF8(device->GetNameForDisplay()); |
| + device_info->id = device->GetIdentifier(); |
| + device_info->address = device->GetAddress(); |
| + |
| + return device_info; |
| +} |
| + |
| +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.
|
| + return adapter_; |
| +} |
| + |
| +void Adapter::GetDevicesImpl(const GetDevicesCallback& callback, |
| + scoped_refptr<device::BluetoothAdapter> adapter) { |
| + OnGetAdapter(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)); |
| +} |
| + |
| +void Adapter::OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter) { |
| + if (!adapter_.get()) { |
| + VLOG(1) << "Adapter acquired"; |
| + adapter_ = adapter; |
| + adapter_->AddObserver(this); |
| + } |
| +} |
| + |
| +} // namespace bluetooth |