summaryrefslogtreecommitdiff
path: root/chromium/device/bluetooth/bluetooth_advertisement_winrt.h
blob: 6ef3dc96e299b300ad91b1fec2d16c5f4b4e2104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_WINRT_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_WINRT_H_

#include <windows.devices.bluetooth.advertisement.h>
#include <wrl/client.h>

#include <memory>

#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_advertisement.h"
#include "device/bluetooth/bluetooth_export.h"

namespace device {

class DEVICE_BLUETOOTH_EXPORT BluetoothAdvertisementWinrt
    : public BluetoothAdvertisement {
 public:
  BluetoothAdvertisementWinrt();
  bool Initialize(
      std::unique_ptr<BluetoothAdvertisement::Data> advertisement_data);
  void Register(SuccessCallback callback, ErrorCallback error_callback);

  // BluetoothAdvertisement:
  void Unregister(const SuccessCallback& success_callback,
                  const ErrorCallback& error_callback) override;

  ABI::Windows::Devices::Bluetooth::Advertisement::
      IBluetoothLEAdvertisementPublisher*
      GetPublisherForTesting();

 protected:
  ~BluetoothAdvertisementWinrt() override;

  // These are declared virtual so that they can be overridden by tests.
  virtual HRESULT GetBluetoothLEAdvertisementPublisherActivationFactory(
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEAdvertisementPublisherFactory** factory) const;

  virtual HRESULT ActivateBluetoothLEAdvertisementInstance(
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEAdvertisement** instance) const;

  virtual HRESULT GetBluetoothLEManufacturerDataFactory(
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEManufacturerDataFactory** factory) const;

 private:
  struct PendingCallbacks {
    PendingCallbacks(SuccessCallback callback, ErrorCallback error_callback);
    ~PendingCallbacks();

    SuccessCallback callback;
    ErrorCallback error_callback;
  };

  void OnStatusChanged(
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEAdvertisementPublisher* publisher,
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEAdvertisementPublisherStatusChangedEventArgs* changed);

  Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::Advertisement::
                             IBluetoothLEAdvertisementPublisher>
      publisher_;
  base::Optional<EventRegistrationToken> status_changed_token_;
  std::unique_ptr<PendingCallbacks> pending_register_callbacks_;
  std::unique_ptr<PendingCallbacks> pending_unregister_callbacks_;

  base::WeakPtrFactory<BluetoothAdvertisementWinrt> weak_ptr_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisementWinrt);
};

}  // namespace device

#endif  // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_WINRT_H_