bluetooth_adapter_winrt.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // Copyright 2018 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_WINRT_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WINRT_H_
  6. #include <windows.devices.bluetooth.h>
  7. #include <windows.devices.enumeration.h>
  8. #include <windows.devices.radios.h>
  9. #include <wrl/client.h>
  10. #include <memory>
  11. #include <string>
  12. #include <vector>
  13. #include "base/callback_forward.h"
  14. #include "base/callback_helpers.h"
  15. #include "base/memory/ref_counted.h"
  16. #include "base/threading/thread_checker.h"
  17. #include "device/bluetooth/bluetooth_adapter.h"
  18. #include "device/bluetooth/bluetooth_export.h"
  19. namespace base {
  20. class ScopedClosureRunner;
  21. }
  22. namespace device {
  23. class BluetoothAdvertisementWinrt;
  24. class BluetoothDeviceWinrt;
  25. class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWinrt : public BluetoothAdapter {
  26. public:
  27. BluetoothAdapterWinrt(const BluetoothAdapterWinrt&) = delete;
  28. BluetoothAdapterWinrt& operator=(const BluetoothAdapterWinrt&) = delete;
  29. // BluetoothAdapter:
  30. std::string GetAddress() const override;
  31. std::string GetName() const override;
  32. void SetName(const std::string& name,
  33. base::OnceClosure callback,
  34. ErrorCallback error_callback) override;
  35. bool IsInitialized() const override;
  36. bool IsPresent() const override;
  37. bool CanPower() const override;
  38. bool IsPowered() const override;
  39. bool IsPeripheralRoleSupported() const override;
  40. bool IsDiscoverable() const override;
  41. void SetDiscoverable(bool discoverable,
  42. base::OnceClosure callback,
  43. ErrorCallback error_callback) override;
  44. bool IsDiscovering() const override;
  45. UUIDList GetUUIDs() const override;
  46. void CreateRfcommService(const BluetoothUUID& uuid,
  47. const ServiceOptions& options,
  48. CreateServiceCallback callback,
  49. CreateServiceErrorCallback error_callback) override;
  50. void CreateL2capService(const BluetoothUUID& uuid,
  51. const ServiceOptions& options,
  52. CreateServiceCallback callback,
  53. CreateServiceErrorCallback error_callback) override;
  54. void RegisterAdvertisement(
  55. std::unique_ptr<BluetoothAdvertisement::Data> advertisement_data,
  56. CreateAdvertisementCallback callback,
  57. AdvertisementErrorCallback error_callback) override;
  58. std::vector<BluetoothAdvertisement*> GetPendingAdvertisementsForTesting()
  59. const override;
  60. BluetoothLocalGattService* GetGattService(
  61. const std::string& identifier) const override;
  62. ABI::Windows::Devices::Radios::IRadio* GetRadioForTesting();
  63. ABI::Windows::Devices::Enumeration::IDeviceWatcher*
  64. GetPoweredRadioWatcherForTesting();
  65. protected:
  66. friend class BluetoothAdapterWin;
  67. friend class BluetoothTestWinrt;
  68. BluetoothAdapterWinrt();
  69. ~BluetoothAdapterWinrt() override;
  70. void Initialize(base::OnceClosure init_callback) override;
  71. // Allow tests to provide their own implementations of statics.
  72. void InitForTests(
  73. base::OnceClosure init_callback,
  74. Microsoft::WRL::ComPtr<
  75. ABI::Windows::Devices::Bluetooth::IBluetoothAdapterStatics>
  76. bluetooth_adapter_statics,
  77. Microsoft::WRL::ComPtr<
  78. ABI::Windows::Devices::Enumeration::IDeviceInformationStatics>
  79. device_information_statics,
  80. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadioStatics>
  81. radio_statics);
  82. // BluetoothAdapter:
  83. base::WeakPtr<BluetoothAdapter> GetWeakPtr() override;
  84. bool SetPoweredImpl(bool powered) override;
  85. void UpdateFilter(std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
  86. DiscoverySessionResultCallback callback) override;
  87. void StartScanWithFilter(
  88. std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
  89. DiscoverySessionResultCallback callback) override;
  90. void StopScan(DiscoverySessionResultCallback callback) override;
  91. void RemovePairingDelegateInternal(
  92. BluetoothDevice::PairingDelegate* pairing_delegate) override;
  93. // Declared virtual so that it can be overridden by tests.
  94. virtual HRESULT ActivateBluetoothAdvertisementLEWatcherInstance(
  95. ABI::Windows::Devices::Bluetooth::Advertisement::
  96. IBluetoothLEAdvertisementWatcher** instance) const;
  97. virtual scoped_refptr<BluetoothAdvertisementWinrt> CreateAdvertisement()
  98. const;
  99. virtual std::unique_ptr<BluetoothDeviceWinrt> CreateDevice(
  100. uint64_t raw_address);
  101. private:
  102. struct StaticsInterfaces {
  103. StaticsInterfaces(
  104. Microsoft::WRL::ComPtr<IAgileReference>, // IBluetoothStatics
  105. Microsoft::WRL::ComPtr<IAgileReference>, // IDeviceInformationStatics
  106. Microsoft::WRL::ComPtr<IAgileReference>); // IRadioStatics
  107. StaticsInterfaces();
  108. StaticsInterfaces(const StaticsInterfaces&);
  109. ~StaticsInterfaces();
  110. Microsoft::WRL::ComPtr<IAgileReference> adapter_statics;
  111. Microsoft::WRL::ComPtr<IAgileReference> device_information_statics;
  112. Microsoft::WRL::ComPtr<IAgileReference> radio_statics;
  113. };
  114. static StaticsInterfaces PerformSlowInitTasks();
  115. static StaticsInterfaces GetAgileReferencesForStatics(
  116. Microsoft::WRL::ComPtr<
  117. ABI::Windows::Devices::Bluetooth::IBluetoothAdapterStatics>
  118. adapter_statics,
  119. Microsoft::WRL::ComPtr<
  120. ABI::Windows::Devices::Enumeration::IDeviceInformationStatics>
  121. device_information_statics,
  122. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadioStatics>
  123. radio_statics);
  124. // CompleteInitAgile is a proxy to CompleteInit that resolves agile
  125. // references.
  126. void CompleteInitAgile(base::OnceClosure init_callback,
  127. StaticsInterfaces statics);
  128. void CompleteInit(
  129. base::OnceClosure init_callback,
  130. Microsoft::WRL::ComPtr<
  131. ABI::Windows::Devices::Bluetooth::IBluetoothAdapterStatics>
  132. bluetooth_adapter_statics,
  133. Microsoft::WRL::ComPtr<
  134. ABI::Windows::Devices::Enumeration::IDeviceInformationStatics>
  135. device_information_statics,
  136. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadioStatics>
  137. radio_statics);
  138. void OnGetDefaultAdapter(
  139. base::ScopedClosureRunner on_init,
  140. Microsoft::WRL::ComPtr<
  141. ABI::Windows::Devices::Bluetooth::IBluetoothAdapter> adapter);
  142. void OnCreateFromIdAsync(
  143. base::ScopedClosureRunner on_init,
  144. Microsoft::WRL::ComPtr<
  145. ABI::Windows::Devices::Enumeration::IDeviceInformation>
  146. device_information);
  147. void OnRequestRadioAccess(
  148. base::ScopedClosureRunner on_init,
  149. ABI::Windows::Devices::Radios::RadioAccessStatus access_status);
  150. void OnGetRadio(
  151. base::ScopedClosureRunner on_init,
  152. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadio> radio);
  153. void OnSetRadioState(
  154. ABI::Windows::Devices::Radios::RadioAccessStatus access_status);
  155. void OnRadioStateChanged(ABI::Windows::Devices::Radios::IRadio* radio,
  156. IInspectable* object);
  157. void OnPoweredRadioAdded(
  158. ABI::Windows::Devices::Enumeration::IDeviceWatcher* watcher,
  159. ABI::Windows::Devices::Enumeration::IDeviceInformation* info);
  160. void OnPoweredRadioRemoved(
  161. ABI::Windows::Devices::Enumeration::IDeviceWatcher* watcher,
  162. ABI::Windows::Devices::Enumeration::IDeviceInformationUpdate* update);
  163. void OnPoweredRadiosEnumerated(
  164. ABI::Windows::Devices::Enumeration::IDeviceWatcher* watcher,
  165. IInspectable* object);
  166. void OnAdvertisementReceived(
  167. ABI::Windows::Devices::Bluetooth::Advertisement::
  168. IBluetoothLEAdvertisementWatcher* watcher,
  169. ABI::Windows::Devices::Bluetooth::Advertisement::
  170. IBluetoothLEAdvertisementReceivedEventArgs* args);
  171. void OnAdvertisementWatcherStopped(
  172. ABI::Windows::Devices::Bluetooth::Advertisement::
  173. IBluetoothLEAdvertisementWatcher* watcher,
  174. ABI::Windows::Devices::Bluetooth::Advertisement::
  175. IBluetoothLEAdvertisementWatcherStoppedEventArgs* args);
  176. void OnRegisterAdvertisement(BluetoothAdvertisement* advertisement,
  177. CreateAdvertisementCallback callback);
  178. void OnRegisterAdvertisementError(
  179. BluetoothAdvertisement* advertisement,
  180. AdvertisementErrorCallback error_callback,
  181. BluetoothAdvertisement::ErrorCode error_code);
  182. void TryRemoveRadioStateChangedHandler();
  183. void TryRemovePoweredRadioEventHandlers();
  184. void RemoveAdvertisementWatcherEventHandlers();
  185. bool is_initialized_ = false;
  186. bool radio_access_allowed_ = false;
  187. std::string address_;
  188. std::string name_;
  189. std::unique_ptr<base::ScopedClosureRunner> on_init_;
  190. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothAdapter>
  191. adapter_;
  192. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadio> radio_;
  193. absl::optional<EventRegistrationToken> radio_state_changed_token_;
  194. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Enumeration::IDeviceWatcher>
  195. powered_radio_watcher_;
  196. absl::optional<EventRegistrationToken> powered_radio_added_token_;
  197. absl::optional<EventRegistrationToken> powered_radio_removed_token_;
  198. absl::optional<EventRegistrationToken> powered_radios_enumerated_token_;
  199. size_t num_powered_radios_ = 0;
  200. bool radio_was_powered_ = false;
  201. std::vector<scoped_refptr<BluetoothAdvertisement>> pending_advertisements_;
  202. absl::optional<EventRegistrationToken> advertisement_received_token_;
  203. absl::optional<EventRegistrationToken> advertisement_watcher_stopped_token_;
  204. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::Advertisement::
  205. IBluetoothLEAdvertisementWatcher>
  206. ble_advertisement_watcher_;
  207. Microsoft::WRL::ComPtr<
  208. ABI::Windows::Devices::Bluetooth::IBluetoothAdapterStatics>
  209. bluetooth_adapter_statics_;
  210. Microsoft::WRL::ComPtr<
  211. ABI::Windows::Devices::Enumeration::IDeviceInformationStatics>
  212. device_information_statics_;
  213. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadioStatics>
  214. radio_statics_;
  215. THREAD_CHECKER(thread_checker_);
  216. // Note: This should remain the last member so it'll be destroyed and
  217. // invalidate its weak pointers before any other members are destroyed.
  218. base::WeakPtrFactory<BluetoothAdapterWinrt> weak_ptr_factory_{this};
  219. };
  220. } // namespace device
  221. #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WINRT_H_