forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_adapter_factory.cc
217 lines (181 loc) · 6.39 KB
/
bluetooth_adapter_factory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2012 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 "device/bluetooth/bluetooth_adapter_factory.h"
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "device/bluetooth/bluetooth_adapter.h"
#if defined(OS_MAC)
#include "base/mac/mac_util.h"
#endif
#if defined(OS_WIN)
#include "base/win/windows_version.h"
#include "device/bluetooth/bluetooth_adapter_win.h"
#endif
#if defined(ANDROID)
#include "base/android/build_info.h"
#endif
namespace device {
BluetoothAdapterFactory::BluetoothAdapterFactory() = default;
BluetoothAdapterFactory::~BluetoothAdapterFactory() = default;
// static
BluetoothAdapterFactory* BluetoothAdapterFactory::Get() {
static base::NoDestructor<BluetoothAdapterFactory> factory;
return factory.get();
}
// static
bool BluetoothAdapterFactory::IsBluetoothSupported() {
// SetAdapterForTesting() may be used to provide a test or mock adapter
// instance even on platforms that would otherwise not support it.
if (Get()->adapter_)
return true;
#if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_LINUX) || \
defined(OS_CHROMEOS) || defined(OS_MAC)
return true;
#else
return false;
#endif
}
bool BluetoothAdapterFactory::IsLowEnergySupported() {
if (values_for_testing_) {
return values_for_testing_->GetLESupported();
}
#if defined(OS_ANDROID)
return base::android::BuildInfo::GetInstance()->sdk_int() >=
base::android::SDK_VERSION_MARSHMALLOW;
#elif defined(OS_WIN)
// Windows 8 supports Low Energy GATT operations but it does not support
// scanning, initiating connections and GATT Server. To keep the API
// consistent we consider Windows 8 as lacking Low Energy support.
return base::win::GetVersion() >= base::win::Version::WIN10;
#elif defined(OS_MAC)
return true;
#elif (defined(OS_LINUX) || defined(OS_CHROMEOS))
return true;
#else
return false;
#endif
}
void BluetoothAdapterFactory::GetAdapter(AdapterCallback callback) {
DCHECK(IsBluetoothSupported());
if (!adapter_) {
adapter_callbacks_.push_back(std::move(callback));
adapter_under_initialization_ = BluetoothAdapter::CreateAdapter();
adapter_ = adapter_under_initialization_->GetWeakPtr();
adapter_->Initialize(base::BindOnce(
&BluetoothAdapterFactory::AdapterInitialized, base::Unretained(this)));
return;
}
if (!adapter_->IsInitialized()) {
adapter_callbacks_.push_back(std::move(callback));
return;
}
std::move(callback).Run(scoped_refptr<BluetoothAdapter>(adapter_.get()));
}
void BluetoothAdapterFactory::GetClassicAdapter(AdapterCallback callback) {
#if defined(OS_WIN)
DCHECK(IsBluetoothSupported());
if (base::win::GetVersion() < base::win::Version::WIN10) {
// Prior to Win10, the default adapter will support Bluetooth classic.
GetAdapter(std::move(callback));
return;
}
if (!classic_adapter_) {
classic_adapter_callbacks_.push_back(std::move(callback));
classic_adapter_under_initialization_ =
BluetoothAdapterWin::CreateClassicAdapter();
classic_adapter_ = classic_adapter_under_initialization_->GetWeakPtr();
classic_adapter_->Initialize(
base::BindOnce(&BluetoothAdapterFactory::ClassicAdapterInitialized,
base::Unretained(this)));
return;
}
if (!classic_adapter_->IsInitialized()) {
classic_adapter_callbacks_.push_back(std::move(callback));
return;
}
std::move(callback).Run(
scoped_refptr<BluetoothAdapter>(classic_adapter_.get()));
#else
GetAdapter(std::move(callback));
#endif // defined(OS_WIN)
}
#if defined(OS_LINUX) || defined(OS_CHROMEOS)
// static
void BluetoothAdapterFactory::Shutdown() {
if (Get()->adapter_)
Get()->adapter_->Shutdown();
}
#endif
// static
void BluetoothAdapterFactory::SetAdapterForTesting(
scoped_refptr<BluetoothAdapter> adapter) {
Get()->adapter_ = adapter->GetWeakPtrForTesting();
#if defined(OS_WIN)
Get()->classic_adapter_ = adapter->GetWeakPtrForTesting();
#endif
}
// static
bool BluetoothAdapterFactory::HasSharedInstanceForTesting() {
return Get()->adapter_ != nullptr;
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
// static
void BluetoothAdapterFactory::SetBleScanParserCallback(
BleScanParserCallback callback) {
Get()->ble_scan_parser_ = callback;
}
// static
BluetoothAdapterFactory::BleScanParserCallback
BluetoothAdapterFactory::GetBleScanParserCallback() {
return Get()->ble_scan_parser_;
}
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
BluetoothAdapterFactory::GlobalValuesForTesting::GlobalValuesForTesting() =
default;
BluetoothAdapterFactory::GlobalValuesForTesting::~GlobalValuesForTesting() =
default;
base::WeakPtr<BluetoothAdapterFactory::GlobalValuesForTesting>
BluetoothAdapterFactory::GlobalValuesForTesting::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
std::unique_ptr<BluetoothAdapterFactory::GlobalValuesForTesting>
BluetoothAdapterFactory::InitGlobalValuesForTesting() {
auto v = std::make_unique<BluetoothAdapterFactory::GlobalValuesForTesting>();
values_for_testing_ = v->GetWeakPtr();
return v;
}
void BluetoothAdapterFactory::AdapterInitialized() {
DCHECK(adapter_);
DCHECK(adapter_under_initialization_);
// Move |adapter_under_initialization_| and |adapter_callbacks_| to avoid
// potential re-entrancy issues while looping over the callbacks.
scoped_refptr<BluetoothAdapter> adapter =
std::move(adapter_under_initialization_);
std::vector<AdapterCallback> callbacks = std::move(adapter_callbacks_);
for (auto& callback : callbacks)
std::move(callback).Run(adapter);
}
#if defined(OS_WIN)
void BluetoothAdapterFactory::ClassicAdapterInitialized() {
DCHECK(classic_adapter_);
DCHECK(classic_adapter_under_initialization_);
// Move |adapter_under_initialization_| and |adapter_callbacks_| to avoid
// potential re-entrancy issues while looping over the callbacks.
scoped_refptr<BluetoothAdapter> adapter =
std::move(classic_adapter_under_initialization_);
std::vector<AdapterCallback> callbacks =
std::move(classic_adapter_callbacks_);
for (auto& callback : callbacks)
std::move(callback).Run(adapter);
}
#endif // defined(OS_WIN)
} // namespace device