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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
content / browser / bluetooth / web_bluetooth_pairing_manager_impl.cc [blame]
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/bluetooth/web_bluetooth_pairing_manager_impl.h"
#include <utility>
#include "base/functional/callback_helpers.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "content/browser/bluetooth/web_bluetooth_pairing_manager_delegate.h"
#include "content/browser/bluetooth/web_bluetooth_service_impl.h"
#include "content/public/browser/bluetooth_delegate.h"
namespace content {
namespace {
using ::blink::mojom::WebBluetoothService;
using ::device::BluetoothDevice;
void OnPairForReadCharacteristicCallback(
std::string characteristic_instance_id,
WebBluetoothPairingManagerDelegate* pairing_manager_delegate,
WebBluetoothService::RemoteCharacteristicReadValueCallback callback,
std::optional<BluetoothDevice::ConnectErrorCode> error_code) {
if (error_code) {
std::move(callback).Run(
WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(*error_code),
/*value=*/std::nullopt);
return;
}
pairing_manager_delegate->RemoteCharacteristicReadValue(
characteristic_instance_id, std::move(callback));
}
void OnPairForWriteCharacteristicCallback(
std::string characteristic_instance_id,
WebBluetoothPairingManagerDelegate* pairing_manager_delegate,
std::vector<uint8_t> value,
blink::mojom::WebBluetoothWriteType write_type,
WebBluetoothService::RemoteCharacteristicWriteValueCallback callback,
std::optional<BluetoothDevice::ConnectErrorCode> error_code) {
if (error_code) {
std::move(callback).Run(
WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(*error_code));
return;
}
pairing_manager_delegate->RemoteCharacteristicWriteValue(
characteristic_instance_id, value, write_type, std::move(callback));
}
void OnPairForReadDescriptorCallback(
const std::string& descriptor_instance_id,
WebBluetoothPairingManagerDelegate* pairing_manager_delegate,
WebBluetoothService::RemoteDescriptorReadValueCallback callback,
std::optional<BluetoothDevice::ConnectErrorCode> error_code) {
if (error_code) {
std::move(callback).Run(
WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(*error_code),
/*value=*/std::nullopt);
return;
}
pairing_manager_delegate->RemoteDescriptorReadValue(descriptor_instance_id,
std::move(callback));
}
void OnPairForWriteDescriptorCallback(
const std::string& descriptor_instance_id,
WebBluetoothPairingManagerDelegate* pairing_manager_delegate,
std::vector<uint8_t> value,
WebBluetoothService::RemoteDescriptorWriteValueCallback callback,
std::optional<BluetoothDevice::ConnectErrorCode> error_code) {
if (error_code) {
std::move(callback).Run(
WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(*error_code));
return;
}
pairing_manager_delegate->RemoteDescriptorWriteValue(
descriptor_instance_id, value, std::move(callback));
}
void OnPairCharacteristicStartNotifications(
const std::string& characteristic_instance_id,
mojo::AssociatedRemote<blink::mojom::WebBluetoothCharacteristicClient>
client,
WebBluetoothPairingManagerDelegate* pairing_manager_delegate,
WebBluetoothService::RemoteCharacteristicStartNotificationsCallback
callback,
std::optional<BluetoothDevice::ConnectErrorCode> error_code) {
if (error_code) {
std::move(callback).Run(
WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(*error_code));
return;
}
pairing_manager_delegate->RemoteCharacteristicStartNotificationsInternal(
characteristic_instance_id, std::move(client), std::move(callback));
}
} // namespace
constexpr int WebBluetoothPairingManagerImpl::kMaxPairAttempts;
// TODO(crbug.com/40626253): Ensure this delegate outlives any in-progress
// pairing operation for which it is used. Additionally review use of
// WebBluetoothDeviceId vs. BluetoothDevice as well as how to deal with
// simultaneous pairing requests for the same device.
WebBluetoothPairingManagerImpl::WebBluetoothPairingManagerImpl(
WebBluetoothPairingManagerDelegate* pairing_manager_delegate)
: pairing_manager_delegate_(pairing_manager_delegate) {
DCHECK(pairing_manager_delegate_);
}
WebBluetoothPairingManagerImpl::~WebBluetoothPairingManagerImpl() {
auto pending_pair_device_ids = std::move(pending_pair_device_ids_);
for (const auto& device_id : pending_pair_device_ids) {
pairing_manager_delegate_->CancelPairing(device_id);
}
}
void WebBluetoothPairingManagerImpl::PairForCharacteristicReadValue(
const std::string& characteristic_instance_id,
WebBluetoothService::RemoteCharacteristicReadValueCallback read_callback) {
blink::WebBluetoothDeviceId device_id =
pairing_manager_delegate_->GetCharacteristicDeviceID(
characteristic_instance_id);
if (!device_id.IsValid()) {
std::move(read_callback)
.Run(WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(
BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN),
/*value=*/std::nullopt);
return;
}
PairDevice(
device_id, /*num_pair_attempts=*/0,
base::BindOnce(&OnPairForReadCharacteristicCallback,
characteristic_instance_id, pairing_manager_delegate_,
std::move(read_callback)));
}
void WebBluetoothPairingManagerImpl::PairForCharacteristicWriteValue(
const std::string& characteristic_instance_id,
const std::vector<uint8_t>& value,
blink::mojom::WebBluetoothWriteType write_type,
WebBluetoothService::RemoteCharacteristicWriteValueCallback callback) {
blink::WebBluetoothDeviceId device_id =
pairing_manager_delegate_->GetCharacteristicDeviceID(
characteristic_instance_id);
if (!device_id.IsValid()) {
std::move(callback).Run(
WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(
BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN));
return;
}
PairDevice(
device_id, /*num_pair_attempts=*/0,
base::BindOnce(&OnPairForWriteCharacteristicCallback,
characteristic_instance_id, pairing_manager_delegate_,
value, write_type, std::move(callback)));
}
void WebBluetoothPairingManagerImpl::PairForDescriptorReadValue(
const std::string& descriptor_instance_id,
WebBluetoothService::RemoteDescriptorReadValueCallback read_callback) {
blink::WebBluetoothDeviceId device_id =
pairing_manager_delegate_->GetDescriptorDeviceId(descriptor_instance_id);
if (!device_id.IsValid()) {
std::move(read_callback)
.Run(WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(
BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN),
/*value=*/std::nullopt);
return;
}
PairDevice(
device_id, /*num_pair_attempts=*/0,
base::BindOnce(&OnPairForReadDescriptorCallback, descriptor_instance_id,
pairing_manager_delegate_, std::move(read_callback)));
}
void WebBluetoothPairingManagerImpl::PairForDescriptorWriteValue(
const std::string& descriptor_instance_id,
const std::vector<uint8_t>& value,
WebBluetoothService::RemoteDescriptorWriteValueCallback callback) {
blink::WebBluetoothDeviceId device_id =
pairing_manager_delegate_->GetDescriptorDeviceId(descriptor_instance_id);
if (!device_id.IsValid()) {
std::move(callback).Run(
WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(
BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN));
return;
}
PairDevice(device_id, /*num_pair_attempts=*/0,
base::BindOnce(&OnPairForWriteDescriptorCallback,
descriptor_instance_id, pairing_manager_delegate_,
std::move(value), std::move(callback)));
}
void WebBluetoothPairingManagerImpl::PairForCharacteristicStartNotifications(
const std::string& characteristic_instance_id,
mojo::AssociatedRemote<blink::mojom::WebBluetoothCharacteristicClient>
client,
blink::mojom::WebBluetoothService::
RemoteCharacteristicStartNotificationsCallback callback) {
blink::WebBluetoothDeviceId device_id =
pairing_manager_delegate_->GetCharacteristicDeviceID(
characteristic_instance_id);
if (!device_id.IsValid()) {
std::move(callback).Run(
WebBluetoothServiceImpl::TranslateConnectErrorAndRecord(
BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN));
return;
}
PairDevice(device_id, /*num_pair_attempts=*/0,
base::BindOnce(&OnPairCharacteristicStartNotifications,
characteristic_instance_id, std::move(client),
pairing_manager_delegate_, std::move(callback)));
}
void WebBluetoothPairingManagerImpl::PairDevice(
blink::WebBluetoothDeviceId device_id,
int num_pair_attempts,
device::BluetoothDevice::ConnectCallback callback) {
DCHECK(device_id.IsValid());
if (pending_pair_device_ids_.contains(device_id)) {
std::move(callback).Run(
BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED);
return;
}
pending_pair_device_ids_.insert(device_id);
pairing_manager_delegate_->PairDevice(
device_id, /*pairing_delegate=*/this,
base::BindOnce(&WebBluetoothPairingManagerImpl::OnPairDevice,
weak_ptr_factory_.GetWeakPtr(), device_id,
num_pair_attempts + 1, std::move(callback)));
}
void WebBluetoothPairingManagerImpl::OnPairDevice(
blink::WebBluetoothDeviceId device_id,
int num_pair_attempts,
BluetoothDevice::ConnectCallback callback,
std::optional<BluetoothDevice::ConnectErrorCode> error_code) {
pending_pair_device_ids_.erase(device_id);
if (!error_code) {
std::move(callback).Run(/*error_code=*/std::nullopt);
return;
}
if (*error_code == BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED &&
num_pair_attempts < kMaxPairAttempts) {
PairDevice(device_id, num_pair_attempts, std::move(callback));
return;
}
std::move(callback).Run(error_code);
}
void WebBluetoothPairingManagerImpl::RequestPinCode(BluetoothDevice* device) {
blink::WebBluetoothDeviceId device_id =
pairing_manager_delegate_->GetWebBluetoothDeviceId(device->GetAddress());
pairing_manager_delegate_->PromptForBluetoothPairing(
device->GetNameForDisplay(),
base::BindOnce(&WebBluetoothPairingManagerImpl::OnPinCodeResult,
weak_ptr_factory_.GetWeakPtr(), device_id),
BluetoothDelegate::PairingKind::kProvidePin, std::nullopt);
}
void WebBluetoothPairingManagerImpl::OnPinCodeResult(
blink::WebBluetoothDeviceId device_id,
const BluetoothDelegate::PairPromptResult& result) {
switch (result.result_code) {
case BluetoothDelegate::PairPromptStatus::kCancelled:
pairing_manager_delegate_->CancelPairing(device_id);
break;
case BluetoothDelegate::PairPromptStatus::kSuccess:
pairing_manager_delegate_->SetPinCode(device_id, result.pin);
break;
}
}
void WebBluetoothPairingManagerImpl::OnPairConfirmResult(
blink::WebBluetoothDeviceId device_id,
const BluetoothDelegate::PairPromptResult& result) {
switch (result.result_code) {
case BluetoothDelegate::PairPromptStatus::kCancelled:
pairing_manager_delegate_->CancelPairing(device_id);
break;
case BluetoothDelegate::PairPromptStatus::kSuccess:
pairing_manager_delegate_->PairConfirmed(device_id);
break;
}
}
void WebBluetoothPairingManagerImpl::RequestPasskey(BluetoothDevice* device) {
device->CancelPairing();
NOTIMPLEMENTED() << "Passkey pairing not supported.";
}
void WebBluetoothPairingManagerImpl::DisplayPinCode(
BluetoothDevice* device,
const std::string& pincode) {
device->CancelPairing();
NOTIMPLEMENTED();
}
void WebBluetoothPairingManagerImpl::DisplayPasskey(BluetoothDevice* device,
uint32_t passkey) {
device->CancelPairing();
NOTIMPLEMENTED();
}
void WebBluetoothPairingManagerImpl::KeysEntered(BluetoothDevice* device,
uint32_t entered) {
device->CancelPairing();
NOTIMPLEMENTED();
}
void WebBluetoothPairingManagerImpl::ConfirmPasskey(BluetoothDevice* device,
uint32_t passkey) {
blink::WebBluetoothDeviceId device_id =
pairing_manager_delegate_->GetWebBluetoothDeviceId(device->GetAddress());
// In HstringToUint32() we have validated original uint32_t passkey range from
// 0 to 999999 before conversion. So here we can safely always assume
// pin.size() == 6 after conversion
std::u16string pin = base::ASCIIToUTF16(base::StringPrintf("%06u", passkey));
pairing_manager_delegate_->PromptForBluetoothPairing(
device->GetNameForDisplay(),
base::BindOnce(&WebBluetoothPairingManagerImpl::OnPairConfirmResult,
weak_ptr_factory_.GetWeakPtr(), device_id),
BluetoothDelegate::PairingKind::kConfirmPinMatch, pin);
}
void WebBluetoothPairingManagerImpl::AuthorizePairing(BluetoothDevice* device) {
blink::WebBluetoothDeviceId device_id =
pairing_manager_delegate_->GetWebBluetoothDeviceId(device->GetAddress());
pairing_manager_delegate_->PromptForBluetoothPairing(
device->GetNameForDisplay(),
base::BindOnce(&WebBluetoothPairingManagerImpl::OnPairConfirmResult,
weak_ptr_factory_.GetWeakPtr(), device_id),
BluetoothDelegate::PairingKind::kConfirmOnly, std::nullopt);
}
} // namespace content