File: bluetooth_adapter_winrt.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (270 lines) | stat: -rw-r--r-- 10,507 bytes parent folder | download | duplicates (9)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WINRT_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WINRT_H_

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

#include <memory>
#include <string>
#include <vector>

#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/threading/thread_checker.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_export.h"

namespace base {
class ScopedClosureRunner;
}

namespace device {

class BluetoothAdvertisementWinrt;
class BluetoothDeviceWinrt;

class DEVICE_BLUETOOTH_EXPORT BluetoothAdapterWinrt : public BluetoothAdapter {
 public:
  BluetoothAdapterWinrt(const BluetoothAdapterWinrt&) = delete;
  BluetoothAdapterWinrt& operator=(const BluetoothAdapterWinrt&) = delete;

  // BluetoothAdapter:
  std::string GetAddress() const override;
  std::string GetName() const override;
  void SetName(const std::string& name,
               base::OnceClosure callback,
               ErrorCallback error_callback) override;
  bool IsInitialized() const override;
  bool IsPresent() const override;
  bool CanPower() const override;
  bool IsPowered() const override;
  bool IsPeripheralRoleSupported() const override;
  bool IsDiscoverable() const override;
  void SetDiscoverable(bool discoverable,
                       base::OnceClosure callback,
                       ErrorCallback error_callback) override;
  bool IsDiscovering() const override;
  UUIDList GetUUIDs() const override;
  void CreateRfcommService(const BluetoothUUID& uuid,
                           const ServiceOptions& options,
                           CreateServiceCallback callback,
                           CreateServiceErrorCallback error_callback) override;
  void CreateL2capService(const BluetoothUUID& uuid,
                          const ServiceOptions& options,
                          CreateServiceCallback callback,
                          CreateServiceErrorCallback error_callback) override;
  void RegisterAdvertisement(
      std::unique_ptr<BluetoothAdvertisement::Data> advertisement_data,
      CreateAdvertisementCallback callback,
      AdvertisementErrorCallback error_callback) override;
  std::vector<BluetoothAdvertisement*> GetPendingAdvertisementsForTesting()
      const override;
  BluetoothLocalGattService* GetGattService(
      const std::string& identifier) const override;

  ABI::Windows::Devices::Radios::IRadio* GetRadioForTesting();
  ABI::Windows::Devices::Enumeration::IDeviceWatcher*
  GetPoweredRadioWatcherForTesting();

 protected:
  friend class BluetoothAdapterWin;
  friend class BluetoothTestWinrt;

  BluetoothAdapterWinrt();
  ~BluetoothAdapterWinrt() override;

  void Initialize(base::OnceClosure init_callback) override;
  // Allow tests to provide their own implementations of statics.
  void InitForTests(
      base::OnceClosure init_callback,
      Microsoft::WRL::ComPtr<
          ABI::Windows::Devices::Bluetooth::IBluetoothAdapterStatics>
          bluetooth_adapter_statics,
      Microsoft::WRL::ComPtr<
          ABI::Windows::Devices::Enumeration::IDeviceInformationStatics>
          device_information_statics,
      Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadioStatics>
          radio_statics);

  // BluetoothAdapter:
  base::WeakPtr<BluetoothAdapter> GetWeakPtr() override;
  bool SetPoweredImpl(bool powered) override;
  void UpdateFilter(std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
                    DiscoverySessionResultCallback callback) override;
  void StartScanWithFilter(
      std::unique_ptr<BluetoothDiscoveryFilter> discovery_filter,
      DiscoverySessionResultCallback callback) override;
  void StopScan(DiscoverySessionResultCallback callback) override;
  void RemovePairingDelegateInternal(
      BluetoothDevice::PairingDelegate* pairing_delegate) override;

  // Declared virtual so that it can be overridden by tests.
  virtual HRESULT ActivateBluetoothAdvertisementLEWatcherInstance(
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEAdvertisementWatcher** instance) const;

  virtual scoped_refptr<BluetoothAdvertisementWinrt> CreateAdvertisement()
      const;

  virtual std::unique_ptr<BluetoothDeviceWinrt> CreateDevice(
      uint64_t raw_address);

 private:
  struct StaticsInterfaces {
    StaticsInterfaces(
        Microsoft::WRL::ComPtr<IAgileReference>,   // IBluetoothStatics
        Microsoft::WRL::ComPtr<IAgileReference>,   // IDeviceInformationStatics
        Microsoft::WRL::ComPtr<IAgileReference>);  // IRadioStatics
    StaticsInterfaces();
    StaticsInterfaces(const StaticsInterfaces&);
    ~StaticsInterfaces();

    Microsoft::WRL::ComPtr<IAgileReference> adapter_statics;
    Microsoft::WRL::ComPtr<IAgileReference> device_information_statics;
    Microsoft::WRL::ComPtr<IAgileReference> radio_statics;
  };

  static StaticsInterfaces PerformSlowInitTasks();
  static StaticsInterfaces GetAgileReferencesForStatics(
      Microsoft::WRL::ComPtr<
          ABI::Windows::Devices::Bluetooth::IBluetoothAdapterStatics>
          adapter_statics,
      Microsoft::WRL::ComPtr<
          ABI::Windows::Devices::Enumeration::IDeviceInformationStatics>
          device_information_statics,
      Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadioStatics>
          radio_statics);

  // CompleteInitAgile is a proxy to CompleteInit that resolves agile
  // references.
  void CompleteInitAgile(base::OnceClosure init_callback,
                         StaticsInterfaces statics);
  void CompleteInit(
      base::OnceClosure init_callback,
      Microsoft::WRL::ComPtr<
          ABI::Windows::Devices::Bluetooth::IBluetoothAdapterStatics>
          bluetooth_adapter_statics,
      Microsoft::WRL::ComPtr<
          ABI::Windows::Devices::Enumeration::IDeviceInformationStatics>
          device_information_statics,
      Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadioStatics>
          radio_statics);

  void OnGetDefaultAdapter(
      base::ScopedClosureRunner on_init,
      Microsoft::WRL::ComPtr<
          ABI::Windows::Devices::Bluetooth::IBluetoothAdapter> adapter);

  void OnCreateFromIdAsync(
      base::ScopedClosureRunner on_init,
      Microsoft::WRL::ComPtr<
          ABI::Windows::Devices::Enumeration::IDeviceInformation>
          device_information);

  void OnRequestRadioAccess(
      base::ScopedClosureRunner on_init,
      ABI::Windows::Devices::Radios::RadioAccessStatus access_status);

  void OnGetRadio(
      base::ScopedClosureRunner on_init,
      Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadio> radio);

  void OnSetRadioState(
      ABI::Windows::Devices::Radios::RadioAccessStatus access_status);

  void OnRadioStateChanged(ABI::Windows::Devices::Radios::IRadio* radio,
                           IInspectable* object);

  void OnPoweredRadioAdded(
      ABI::Windows::Devices::Enumeration::IDeviceWatcher* watcher,
      ABI::Windows::Devices::Enumeration::IDeviceInformation* info);

  void OnPoweredRadioRemoved(
      ABI::Windows::Devices::Enumeration::IDeviceWatcher* watcher,
      ABI::Windows::Devices::Enumeration::IDeviceInformationUpdate* update);

  void OnPoweredRadiosEnumerated(
      ABI::Windows::Devices::Enumeration::IDeviceWatcher* watcher,
      IInspectable* object);

  void OnAdvertisementReceived(
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEAdvertisementWatcher* watcher,
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEAdvertisementReceivedEventArgs* args);

  void OnAdvertisementWatcherStopped(
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEAdvertisementWatcher* watcher,
      ABI::Windows::Devices::Bluetooth::Advertisement::
          IBluetoothLEAdvertisementWatcherStoppedEventArgs* args);

  void OnRegisterAdvertisement(BluetoothAdvertisement* advertisement,
                               CreateAdvertisementCallback callback);

  void OnRegisterAdvertisementError(
      BluetoothAdvertisement* advertisement,
      AdvertisementErrorCallback error_callback,
      BluetoothAdvertisement::ErrorCode error_code);

  void TryRemoveRadioStateChangedHandler();

  void TryRemovePoweredRadioEventHandlers();

  void RemoveAdvertisementWatcherEventHandlers();

  bool is_initialized_ = false;
  bool radio_access_allowed_ = false;
  std::string address_;
  std::string name_;
  std::unique_ptr<base::ScopedClosureRunner> on_init_;

  Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::IBluetoothAdapter>
      adapter_;

  Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadio> radio_;
  std::optional<EventRegistrationToken> radio_state_changed_token_;

  Microsoft::WRL::ComPtr<ABI::Windows::Devices::Enumeration::IDeviceWatcher>
      powered_radio_watcher_;
  std::optional<EventRegistrationToken> powered_radio_added_token_;
  std::optional<EventRegistrationToken> powered_radio_removed_token_;
  std::optional<EventRegistrationToken> powered_radios_enumerated_token_;
  size_t num_powered_radios_ = 0;

  bool radio_was_powered_ = false;

  std::vector<scoped_refptr<BluetoothAdvertisement>> pending_advertisements_;

  std::optional<EventRegistrationToken> advertisement_received_token_;
  std::optional<EventRegistrationToken> advertisement_watcher_stopped_token_;
  Microsoft::WRL::ComPtr<ABI::Windows::Devices::Bluetooth::Advertisement::
                             IBluetoothLEAdvertisementWatcher>
      ble_advertisement_watcher_;

  Microsoft::WRL::ComPtr<
      ABI::Windows::Devices::Bluetooth::IBluetoothAdapterStatics>
      bluetooth_adapter_statics_;
  Microsoft::WRL::ComPtr<
      ABI::Windows::Devices::Enumeration::IDeviceInformationStatics>
      device_information_statics_;
  Microsoft::WRL::ComPtr<ABI::Windows::Devices::Radios::IRadioStatics>
      radio_statics_;

  THREAD_CHECKER(thread_checker_);

  // Note: This should remain the last member so it'll be destroyed and
  // invalidate its weak pointers before any other members are destroyed.
  base::WeakPtrFactory<BluetoothAdapterWinrt> weak_ptr_factory_{this};
};

}  // namespace device

#endif  // DEVICE_BLUETOOTH_BLUETOOTH_ADAPTER_WINRT_H_