bluetooth_adapter_factory.cc 6.3 KB

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