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
|
// Copyright 2012 The Chromium Authors
// 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 <memory>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/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 "device/bluetooth/bluetooth_adapter.h"
#if BUILDFLAG(IS_APPLE)
#include "base/mac/mac_util.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "device/bluetooth/bluetooth_adapter_win.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 BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || \
BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_APPLE)
return true;
#else
return false;
#endif
}
bool BluetoothAdapterFactory::IsLowEnergySupported() {
if (override_values_) {
return override_values_->GetLESupported();
}
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX) || \
BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_WIN)
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 BUILDFLAG(IS_WIN)
DCHECK(IsBluetoothSupported());
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 // BUILDFLAG(IS_WIN)
}
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_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 (!adapter->IsInitialized())
Get()->adapter_under_initialization_ = adapter;
#if BUILDFLAG(IS_WIN)
Get()->classic_adapter_ = adapter->GetWeakPtrForTesting();
#endif
}
// static
bool BluetoothAdapterFactory::HasSharedInstanceForTesting() {
return Get()->adapter_ != nullptr;
}
#if BUILDFLAG(IS_CHROMEOS)
// 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)
BluetoothAdapterFactory::GlobalOverrideValues::GlobalOverrideValues() = default;
BluetoothAdapterFactory::GlobalOverrideValues::~GlobalOverrideValues() =
default;
base::WeakPtr<BluetoothAdapterFactory::GlobalOverrideValues>
BluetoothAdapterFactory::GlobalOverrideValues::GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
std::unique_ptr<BluetoothAdapterFactory::GlobalOverrideValues>
BluetoothAdapterFactory::InitGlobalOverrideValues() {
auto v = std::make_unique<BluetoothAdapterFactory::GlobalOverrideValues>();
override_values_ = 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 BUILDFLAG(IS_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 // BUILDFLAG(IS_WIN)
} // namespace device
|