bluetooth_pairing_winrt.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_PAIRING_WINRT_H_
  5. #define DEVICE_BLUETOOTH_BLUETOOTH_PAIRING_WINRT_H_
  6. #include <windows.devices.enumeration.h>
  7. #include <windows.foundation.h>
  8. #include <wrl/client.h>
  9. #include "base/memory/raw_ptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/strings/string_piece_forward.h"
  13. #include "device/bluetooth/bluetooth_device.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace device {
  16. class BluetoothDeviceWinrt;
  17. // This class encapsulates logic required to perform a custom pairing on WinRT.
  18. // Currently only pairing with a pin code is supported.
  19. class BluetoothPairingWinrt {
  20. public:
  21. // On error |error_code| will have a value, otherwise successful.
  22. using ConnectCallback = base::OnceCallback<void(
  23. absl::optional<BluetoothDevice::ConnectErrorCode> error_code)>;
  24. BluetoothPairingWinrt(
  25. BluetoothDeviceWinrt* device,
  26. BluetoothDevice::PairingDelegate* pairing_delegate,
  27. Microsoft::WRL::ComPtr<
  28. ABI::Windows::Devices::Enumeration::IDeviceInformationCustomPairing>
  29. custom_pairing,
  30. ConnectCallback callback);
  31. BluetoothPairingWinrt(const BluetoothPairingWinrt&) = delete;
  32. BluetoothPairingWinrt& operator=(const BluetoothPairingWinrt&) = delete;
  33. ~BluetoothPairingWinrt();
  34. // Initiates the pairing procedure.
  35. void StartPairing();
  36. // Indicates whether the device is currently pairing and expecting a
  37. // PIN Code to be returned.
  38. bool ExpectingPinCode() const;
  39. // Sends the PIN code |pin_code| to the remote device during pairing.
  40. void SetPinCode(base::StringPiece pin_code);
  41. // User consented to continue pairing the remote device.
  42. void ConfirmPairing();
  43. // Rejects a pairing or connection request from a remote device.
  44. void RejectPairing();
  45. // Cancels a pairing or connection attempt to a remote device.
  46. void CancelPairing();
  47. private:
  48. void OnPairingRequested(
  49. ABI::Windows::Devices::Enumeration::IDeviceInformationCustomPairing*
  50. custom_pairing,
  51. ABI::Windows::Devices::Enumeration::IDevicePairingRequestedEventArgs*
  52. pairing_requested);
  53. void OnPair(Microsoft::WRL::ComPtr<
  54. ABI::Windows::Devices::Enumeration::IDevicePairingResult>
  55. pairing_result);
  56. void OnSetPinCodeDeferralCompletion(HRESULT hr);
  57. void OnConfirmPairingDeferralCompletion(HRESULT hr);
  58. void OnRejectPairing(HRESULT hr);
  59. void OnCancelPairing(HRESULT hr);
  60. // Weak. This is the device object that owns this pairing instance.
  61. raw_ptr<BluetoothDeviceWinrt> device_;
  62. // Weak. This is the pairing delegate provided to BluetoothDevice::Pair.
  63. // Clients need to ensure the delegate stays alive during the pairing
  64. // procedure.
  65. raw_ptr<BluetoothDevice::PairingDelegate> pairing_delegate_;
  66. // Boolean indicating whether the device is currently pairing and expecting a
  67. // PIN Code to be returned.
  68. bool expecting_pin_code_ = false;
  69. Microsoft::WRL::ComPtr<
  70. ABI::Windows::Devices::Enumeration::IDeviceInformationCustomPairing>
  71. custom_pairing_;
  72. ConnectCallback callback_;
  73. absl::optional<EventRegistrationToken> pairing_requested_token_;
  74. Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IDeferral> pairing_deferral_;
  75. Microsoft::WRL::ComPtr<
  76. ABI::Windows::Devices::Enumeration::IDevicePairingRequestedEventArgs>
  77. pairing_requested_;
  78. bool was_cancelled_ = false;
  79. SEQUENCE_CHECKER(sequence_checker_);
  80. base::WeakPtrFactory<BluetoothPairingWinrt> weak_ptr_factory_{this};
  81. };
  82. } // namespace device
  83. #endif // DEVICE_BLUETOOTH_BLUETOOTH_PAIRING_WINRT_H_