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

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

Issue 2379573006: bluetooth: Standardize Bluetooth adapter access in Adapter service. (Closed)
Patch Set: Simplify WithAdapter logic 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
« no previous file with comments | « device/bluetooth/adapter.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
(...skipping 12 matching lines...) Expand all
23 adapter_ = nullptr; 23 adapter_ = nullptr;
24 } 24 }
25 } 25 }
26 26
27 // static 27 // static
28 void Adapter::Create(mojom::AdapterRequest request) { 28 void Adapter::Create(mojom::AdapterRequest request) {
29 mojo::MakeStrongBinding(base::MakeUnique<Adapter>(), std::move(request)); 29 mojo::MakeStrongBinding(base::MakeUnique<Adapter>(), std::move(request));
30 } 30 }
31 31
32 void Adapter::GetDevices(const GetDevicesCallback& callback) { 32 void Adapter::GetDevices(const GetDevicesCallback& callback) {
33 if (!adapter_) { 33 WithAdapter(base::Bind(
34 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) { 34 [](const GetDevicesCallback& callback,
35 base::Closure c = base::Bind(&Adapter::GetDevicesImpl, 35 scoped_refptr<device::BluetoothAdapter> adapter) {
36 weak_ptr_factory_.GetWeakPtr(), callback); 36 if (adapter) {
37 std::vector<mojom::DeviceInfoPtr> devices;
38 for (const device::BluetoothDevice* device : adapter->GetDevices()) {
39 mojom::DeviceInfoPtr device_info =
40 ConstructDeviceInfoStruct(device);
41 devices.push_back(std::move(device_info));
42 }
37 43
38 device::BluetoothAdapterFactory::GetAdapter(base::Bind( 44 callback.Run(std::move(devices));
39 &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), c)); 45 } else {
40 return; 46 callback.Run(std::vector<mojom::DeviceInfoPtr>());
41 } 47 }
42 callback.Run(std::vector<mojom::DeviceInfoPtr>()); 48 },
43 return; 49 callback));
44 }
45 GetDevicesImpl(callback);
46 } 50 }
47 51
48 void Adapter::SetClient(mojom::AdapterClientPtr client) { 52 void Adapter::SetClient(mojom::AdapterClientPtr client) {
49 client_ = std::move(client); 53 client_ = std::move(client);
50 } 54 }
51 55
52 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter, 56 void Adapter::DeviceAdded(device::BluetoothAdapter* adapter,
53 device::BluetoothDevice* device) { 57 device::BluetoothDevice* device) {
54 if (client_) { 58 if (client_) {
55 auto device_info = ConstructDeviceInfoStruct(device); 59 auto device_info = ConstructDeviceInfoStruct(device);
56 client_->DeviceAdded(std::move(device_info)); 60 client_->DeviceAdded(std::move(device_info));
57 } 61 }
58 } 62 }
59 63
60 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter, 64 void Adapter::DeviceRemoved(device::BluetoothAdapter* adapter,
61 device::BluetoothDevice* device) { 65 device::BluetoothDevice* device) {
62 if (client_) { 66 if (client_) {
63 auto device_info = ConstructDeviceInfoStruct(device); 67 auto device_info = ConstructDeviceInfoStruct(device);
64 client_->DeviceRemoved(std::move(device_info)); 68 client_->DeviceRemoved(std::move(device_info));
65 } 69 }
66 } 70 }
67 71
72 // static
68 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct( 73 mojom::DeviceInfoPtr Adapter::ConstructDeviceInfoStruct(
69 const device::BluetoothDevice* device) const { 74 const device::BluetoothDevice* const device) {
70 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New(); 75 mojom::DeviceInfoPtr device_info = mojom::DeviceInfo::New();
71 76
72 device_info->name = device->GetName(); 77 device_info->name = device->GetName();
73 device_info->name_for_display = 78 device_info->name_for_display =
74 base::UTF16ToUTF8(device->GetNameForDisplay()); 79 base::UTF16ToUTF8(device->GetNameForDisplay());
75 device_info->id = device->GetIdentifier(); 80 device_info->id = device->GetIdentifier();
76 device_info->address = device->GetAddress(); 81 device_info->address = device->GetAddress();
77 82
78 return device_info; 83 return device_info;
79 } 84 }
80 85
81 void Adapter::GetDevicesImpl(const GetDevicesCallback& callback) { 86 void Adapter::WithAdapter(
82 std::vector<mojom::DeviceInfoPtr> devices; 87 const device::BluetoothAdapterFactory::AdapterCallback& action) {
83 88 if (adapter_) {
84 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { 89 action.Run(adapter_);
scheib 2016/09/30 21:52:01 add return here, or remove the return below and ch
mbrunson 2016/09/30 22:13:02 Done.
85 mojom::DeviceInfoPtr device_info = ConstructDeviceInfoStruct(device);
86 devices.push_back(std::move(device_info));
87 } 90 }
88 91
89 callback.Run(std::move(devices)); 92 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
93 device::BluetoothAdapterFactory::GetAdapter(base::Bind(
94 &Adapter::OnGetAdapter, weak_ptr_factory_.GetWeakPtr(), action));
95 return;
96 }
97
98 action.Run(nullptr);
90 } 99 }
91 100
92 void Adapter::OnGetAdapter(const base::Closure& continuation, 101 void Adapter::OnGetAdapter(
93 scoped_refptr<device::BluetoothAdapter> adapter) { 102 const device::BluetoothAdapterFactory::AdapterCallback& continuation,
103 scoped_refptr<device::BluetoothAdapter> adapter) {
94 if (!adapter_) { 104 if (!adapter_) {
95 VLOG(1) << "Adapter acquired"; 105 VLOG(1) << "Adapter acquired";
96 adapter_ = adapter; 106 adapter_ = adapter;
97 adapter_->AddObserver(this); 107 adapter_->AddObserver(this);
98 } 108 }
99 continuation.Run(); 109 continuation.Run(adapter);
100 } 110 }
101 111
102 } // namespace bluetooth 112 } // namespace bluetooth
OLDNEW
« no previous file with comments | « device/bluetooth/adapter.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698