Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: device/bluetooth/adapter.cc

Issue 2379573006: bluetooth: Standardize Bluetooth adapter access in Adapter service. (Closed)
Patch Set: Comment updates Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 mojom::AdapterClientPtr client)
19 : adapter_(adapter), client_(std::move(client)), weak_ptr_factory_(this) {
20 adapter_->AddObserver(this);
21 }
19 22
20 Adapter::~Adapter() { 23 Adapter::~Adapter() {
21 if (adapter_) { 24 adapter_->RemoveObserver(this);
22 adapter_->RemoveObserver(this); 25 adapter_ = nullptr;
23 adapter_ = nullptr;
24 }
25 } 26 }
26 27
27 // static 28 // static
28 void Adapter::Create(mojom::AdapterRequest request) { 29 void Adapter::Create(mojom::AdapterRequest request,
29 mojo::MakeStrongBinding(base::MakeUnique<Adapter>(), std::move(request)); 30 mojom::AdapterClientPtr client,
31 scoped_refptr<device::BluetoothAdapter> adapter) {
32 mojo::MakeStrongBinding(base::MakeUnique<Adapter>(adapter, std::move(client)),
ortuno 2016/10/05 02:07:51 If you decide to use the interface I proposed you
mbrunson 2016/10/05 17:59:04 Done.
33 std::move(request));
30 } 34 }
31 35
32 void Adapter::GetDevices(const GetDevicesCallback& callback) { 36 void Adapter::GetDevices(const GetDevicesCallback& callback) {
33 if (!adapter_) { 37 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 38
38 device::BluetoothAdapterFactory::GetAdapter(base::Bind( 39 for (const device::BluetoothDevice* device : adapter_->GetDevices()) {
39 &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), c)); 40 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device);
40 return; 41 devices.push_back(std::move(device_info));
41 }
42 callback.Run(std::vector<mojom::DeviceInfoPtr>());
43 return;
44 } 42 }
45 GetDevicesImpl(callback); 43
44 callback.Run(std::move(devices));
46 } 45 }
47 46
48 void Adapter::SetClient(mojom::AdapterClientPtr client) { 47 void Adapter::SetClient(mojom::AdapterClientPtr client) {
49 client_ = std::move(client); 48 client_ = std::move(client);
50 } 49 }
51 50
52 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter, 51 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter,
53 device::BluetoothDevice* device) { 52 device::BluetoothDevice* device) {
54 if (client_) { 53 if (client_) {
55 auto device_info = ConstructDeviceInfoStruct(device); 54 auto device_info = ConstructDeviceInfoStruct(device);
56 client_->DeviceAdded(std::move(device_info)); 55 client_->DeviceAdded(std::move(device_info));
57 } 56 }
58 } 57 }
59 58
60 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, 59 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter,
61 device::BluetoothDevice* device) { 60 device::BluetoothDevice* device) {
62 if (client_) { 61 if (client_) {
63 auto device_info = ConstructDeviceInfoStruct(device); 62 auto device_info = ConstructDeviceInfoStruct(device);
64 client_->DeviceRemoved(std::move(device_info)); 63 client_->DeviceRemoved(std::move(device_info));
65 } 64 }
66 } 65 }
67 66
67 // static
68 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( 68 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct(
69 const device::BluetoothDevice* device) const { 69 const device::BluetoothDevice* const device) {
70 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); 70 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New();
71 71
72 device_info->name = device->GetName(); 72 device_info->name = device->GetName();
73 device_info->name_for_display = 73 device_info->name_for_display =
74 base::UTF16ToUTF8(device->GetNameForDisplay()); 74 base::UTF16ToUTF8(device->GetNameForDisplay());
75 device_info->id = device->GetIdentifier(); 75 device_info->id = device->GetIdentifier();
76 device_info->address = device->GetAddress(); 76 device_info->address = device->GetAddress();
77 77
78 return device_info; 78 return device_info;
79 } 79 }
80 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 81 } // namespace bluetooth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698