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 <string> |
| 6 #include <utility> |
| 7 #include <vector> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "device/bluetooth/adapter.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 13 #include "mojo/public/cpp/bindings/string.h" |
| 14 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 15 |
| 16 namespace bluetooth { |
| 17 |
| 18 Adapter::Adapter() : client_(nullptr), weak_ptr_factory_(this) {} |
| 19 |
| 20 Adapter::~Adapter() { |
| 21 if (adapter_) { |
| 22 adapter_->RemoveObserver(this); |
| 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 } |
| 31 |
| 32 void Adapter::GetDevices(const GetDevicesCallback& callback) { |
| 33 if (!adapter_) { |
| 34 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { |
| 35 base::Closure c = base::Bind(&Adapter::GetDevicesImpl, |
| 36 weak_ptr_factory_.GetWeakPtr(), callback); |
| 37 |
| 38 device::BluetoothAdapterFactory::GetAdapter(base::Bind( |
| 39 &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), c)); |
| 40 return; |
| 41 } |
| 42 callback.Run(std::vector<mojom::DeviceInfoPtr>()); |
| 43 return; |
| 44 } |
| 45 GetDevicesImpl(callback); |
| 46 } |
| 47 |
| 48 void Adapter::SetClient(mojom::AdapterClientPtr client) { |
| 49 client_ = std::move(client); |
| 50 } |
| 51 |
| 52 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter, |
| 53 device::BluetoothDevice* device) { |
| 54 if (client_) { |
| 55 auto device_info = ConstructDeviceInfoStruct(device); |
| 56 client_->DeviceAdded(std::move(device_info)); |
| 57 } |
| 58 } |
| 59 |
| 60 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, |
| 61 device::BluetoothDevice* device) { |
| 62 if (client_) { |
| 63 auto device_info = ConstructDeviceInfoStruct(device); |
| 64 client_->DeviceRemoved(std::move(device_info)); |
| 65 } |
| 66 } |
| 67 |
| 68 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( |
| 69 const device::BluetoothDevice* device) const { |
| 70 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); |
| 71 |
| 72 device_info->name = device->GetName(); |
| 73 device_info->name_for_display = |
| 74 base::UTF16ToUTF8(device->GetNameForDisplay()); |
| 75 device_info->id = device->GetIdentifier(); |
| 76 device_info->address = device->GetAddress(); |
| 77 |
| 78 return device_info; |
| 79 } |
| 80 |
| 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 |
OLD | NEW |