bluetooth_adapter_win.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <set>
  9. #include <string>
  10. #include <unordered_set>
  11. #include <utility>
  12. #include <vector>
  13. #include "base/memory/ref_counted.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/threading/thread_checker.h"
  16. #include "device/bluetooth/bluetooth_adapter.h"
  17. #include "device/bluetooth/bluetooth_discovery_session.h"
  18. #include "device/bluetooth/bluetooth_export.h"
  19. #include "device/bluetooth/bluetooth_task_manager_win.h"
  20. namespace device {
  21. class BluetoothAdapterWinTest;
  22. class BluetoothDevice;
  23. class BluetoothSocketThread;
  24. class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWin
  25. : public BluetoothAdapter,
  26. public BluetoothTaskManagerWin::Observer {
  27. public:
  28. static scoped_refptr<BluetoothAdapter> CreateAdapter();
  29. static scoped_refptr<BluetoothAdapter> CreateClassicAdapter();
  30. BluetoothAdapterWin(const BluetoothAdapterWin&) = delete;
  31. BluetoothAdapterWin& operator=(const BluetoothAdapterWin&) = delete;
  32. static bool UseNewBLEWinImplementation();
  33. // BluetoothAdapter:
  34. std::string GetAddress() const override;
  35. std::string GetName() const override;
  36. void SetName(const std::string& name,
  37. base::OnceClosure callback,
  38. ErrorCallback error_callback) override;
  39. bool IsInitialized() const override;
  40. bool IsPresent() const override;
  41. bool IsPowered() const override;
  42. void SetPowered(bool discoverable,
  43. base::OnceClosure callback,
  44. ErrorCallback error_callback) override;
  45. bool IsDiscoverable() const override;
  46. void SetDiscoverable(bool discoverable,
  47. base::OnceClosure callback,
  48. ErrorCallback error_callback) override;
  49. bool IsDiscovering() const override;
  50. UUIDList GetUUIDs() const override;
  51. void CreateRfcommService(const BluetoothUUID& uuid,
  52. const ServiceOptions& options,
  53. CreateServiceCallback callback,
  54. CreateServiceErrorCallback error_callback) override;
  55. void CreateL2capService(const BluetoothUUID& uuid,
  56. const ServiceOptions& options,
  57. CreateServiceCallback callback,
  58. CreateServiceErrorCallback error_callback) override;
  59. void RegisterAdvertisement(
  60. std::unique_ptr<BluetoothAdvertisement::Data> advertisement_data,
  61. CreateAdvertisementCallback callback,
  62. AdvertisementErrorCallback error_callback) override;
  63. BluetoothLocalGattService* GetGattService(
  64. const std::string& identifier) const override;
  65. // BluetoothTaskManagerWin::Observer override
  66. void AdapterStateChanged(
  67. const BluetoothTaskManagerWin::AdapterState& state) override;
  68. void DiscoveryStarted(bool success) override;
  69. void DiscoveryStopped() override;
  70. void DevicesPolled(
  71. const std::vector<std::unique_ptr<BluetoothTaskManagerWin::DeviceState>>&
  72. devices) override;
  73. const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner() const {
  74. return ui_task_runner_;
  75. }
  76. const scoped_refptr<BluetoothSocketThread>& socket_thread() const {
  77. return socket_thread_;
  78. }
  79. scoped_refptr<BluetoothTaskManagerWin> GetWinBluetoothTaskManager() {
  80. return task_manager_;
  81. }
  82. protected:
  83. // BluetoothAdapter:
  84. void RemovePairingDelegateInternal(
  85. device::BluetoothDevice::PairingDelegate* pairing_delegate) override;
  86. private:
  87. friend class BluetoothAdapterWinTest;
  88. friend class BluetoothTestWin;
  89. enum DiscoveryStatus {
  90. NOT_DISCOVERING,
  91. DISCOVERY_STARTING,
  92. DISCOVERING,
  93. DISCOVERY_STOPPING
  94. };
  95. BluetoothAdapterWin();
  96. ~BluetoothAdapterWin() override;
  97. // BluetoothAdapter:
  98. base::WeakPtr<BluetoothAdapter> GetWeakPtr() override;
  99. bool SetPoweredImpl(bool powered) override;
  100. void UpdateFilter(std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
  101. DiscoverySessionResultCallback callback) override;
  102. void StartScanWithFilter(
  103. std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
  104. DiscoverySessionResultCallback callback) override;
  105. void StopScan(DiscoverySessionResultCallback callback) override;
  106. void Initialize(base::OnceClosure callback) override;
  107. void InitForTest(
  108. base::OnceClosure init_callback,
  109. std::unique_ptr<win::BluetoothClassicWrapper> classic_wrapper,
  110. std::unique_ptr<win::BluetoothLowEnergyWrapper> le_wrapper,
  111. scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner,
  112. scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
  113. void MaybePostStartDiscoveryTask();
  114. void MaybePostStopDiscoveryTask();
  115. base::OnceClosure init_callback_;
  116. std::string address_;
  117. std::string name_;
  118. bool initialized_;
  119. bool powered_;
  120. DiscoveryStatus discovery_status_;
  121. std::unordered_set<std::string> discovered_devices_;
  122. DiscoverySessionResultCallback discovery_changed_callback_;
  123. scoped_refptr<BluetoothSocketThread> socket_thread_;
  124. scoped_refptr<BluetoothTaskManagerWin> task_manager_;
  125. base::ThreadChecker thread_checker_;
  126. // Flag indicating a device update must be forced in DevicesPolled.
  127. bool force_update_device_for_test_;
  128. // NOTE: This should remain the last member so it'll be destroyed and
  129. // invalidate its weak pointers before any other members are destroyed.
  130. base::WeakPtrFactory<BluetoothAdapterWin> weak_ptr_factory_{this};
  131. };
  132. } // namespace device
  133. #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WIN_H_