bluetooth_advertisement_winrt.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_ADVERTISEMENT_WINRT_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_WINRT_H_
  6. #include <windows.devices.bluetooth.advertisement.h>
  7. #include <wrl/client.h>
  8. #include <memory>
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "device/bluetooth/bluetooth_adapter.h"
  12. #include "device/bluetooth/bluetooth_advertisement.h"
  13. #include "device/bluetooth/bluetooth_export.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace device {
  16. class DEVICE_BLUETOOTH_EXPORT BluetoothAdvertisementWinrt
  17. : public BluetoothAdvertisement {
  18. public:
  19. BluetoothAdvertisementWinrt();
  20. BluetoothAdvertisementWinrt(const BluetoothAdvertisementWinrt&) = delete;
  21. BluetoothAdvertisementWinrt& operator=(const BluetoothAdvertisementWinrt&) =
  22. delete;
  23. bool Initialize(
  24. std::unique_ptr<BluetoothAdvertisement::Data> advertisement_data);
  25. void Register(SuccessCallback callback, ErrorCallback error_callback);
  26. // BluetoothAdvertisement:
  27. void Unregister(SuccessCallback success_callback,
  28. ErrorCallback error_callback) override;
  29. ABI::Windows::Devices::Bluetooth::Advertisement::
  30. IBluetoothLEAdvertisementPublisher*
  31. GetPublisherForTesting();
  32. protected:
  33. ~BluetoothAdvertisementWinrt() override;
  34. // These are declared virtual so that they can be overridden by tests.
  35. virtual HRESULT GetBluetoothLEAdvertisementPublisherActivationFactory(
  36. ABI::Windows::Devices::Bluetooth::Advertisement::
  37. IBluetoothLEAdvertisementPublisherFactory** factory) const;
  38. virtual HRESULT ActivateBluetoothLEAdvertisementInstance(
  39. ABI::Windows::Devices::Bluetooth::Advertisement::
  40. IBluetoothLEAdvertisement** instance) const;
  41. virtual HRESULT GetBluetoothLEManufacturerDataFactory(
  42. ABI::Windows::Devices::Bluetooth::Advertisement::
  43. IBluetoothLEManufacturerDataFactory** factory) const;
  44. private:
  45. struct PendingCallbacks {
  46. PendingCallbacks(SuccessCallback callback, ErrorCallback error_callback);
  47. ~PendingCallbacks();
  48. SuccessCallback callback;
  49. ErrorCallback error_callback;
  50. };
  51. void OnStatusChanged(
  52. ABI::Windows::Devices::Bluetooth::Advertisement::
  53. IBluetoothLEAdvertisementPublisher* publisher,
  54. ABI::Windows::Devices::Bluetooth::Advertisement::
  55. IBluetoothLEAdvertisementPublisherStatusChangedEventArgs* changed);
  56. Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::Advertisement::
  57. IBluetoothLEAdvertisementPublisher>
  58. publisher_;
  59. absl::optional<EventRegistrationToken> status_changed_token_;
  60. std::unique_ptr<PendingCallbacks> pending_register_callbacks_;
  61. std::unique_ptr<PendingCallbacks> pending_unregister_callbacks_;
  62. base::WeakPtrFactory<BluetoothAdvertisementWinrt> weak_ptr_factory_{this};
  63. };
  64. } // namespace device
  65. #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_WINRT_H_