File: bluetooth_utils.cc

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 (699 lines) | stat: -rw-r--r-- 26,316 bytes parent folder | download | duplicates (6)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// 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.

#include "device/bluetooth/chromeos/bluetooth_utils.h"

#include <optional>
#include <string_view>

#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "base/containers/contains.h"
#include "base/containers/fixed_flat_set.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chromeos/ash/services/nearby/public/cpp/nearby_client_uuids.h"
#include "chromeos/ash/services/secure_channel/public/cpp/shared/ble_constants.h"
#include "chromeos/constants/chromeos_features.h"
#include "device/base/features.h"

namespace device {

namespace {

// https://www.bluetooth.com/specifications/gatt/services.
const char kHIDServiceUUID[] = "1812";

// https://www.bluetooth.com/specifications/assigned-numbers/16-bit-uuids-for-sdos.
const char kSecurityKeyServiceUUID[] = "FFFD";

constexpr base::TimeDelta kMaxDeviceSelectionDuration = base::Seconds(30);
constexpr base::TimeDelta kConnectionTimeIntervalThreshold = base::Minutes(15);
constexpr base::TimeDelta kToastShownCountTimeIntervalThreshold =
    base::Hours(24);

constexpr uint8_t kLimitedDiscoveryFlag = 0x01;
constexpr uint8_t kGeneralDiscoveryFlag = 0x02;

// Get limited number of devices from |devices| and
// prioritize paired/connecting devices over other devices.
BluetoothAdapter::DeviceList GetLimitedNumDevices(
    size_t max_device_num,
    const BluetoothAdapter::DeviceList& devices) {
  // If |max_device_num| is 0, it means there's no limit.
  if (max_device_num == 0) {
    return devices;
  }

  BluetoothAdapter::DeviceList result;
  for (BluetoothDevice* device : devices) {
    if (result.size() == max_device_num) {
      break;
    }

    if (device->IsPaired() || device->IsConnecting()) {
      result.push_back(device);
    }
  }

  for (BluetoothDevice* device : devices) {
    if (result.size() == max_device_num) {
      break;
    }

    if (!device->IsPaired() && !device->IsConnecting()) {
      result.push_back(device);
    }
  }

  return result;
}

// Filter out unknown devices from the list.
BluetoothAdapter::DeviceList FilterUnknownDevices(
    const BluetoothAdapter::DeviceList& devices) {
  if (ash::switches::IsUnfilteredBluetoothDevicesEnabled()) {
    return devices;
  }

  BluetoothAdapter::DeviceList result;
  for (BluetoothDevice* device : devices) {
    if (device::IsUnsupportedDevice(device)) {
      continue;
    }

    result.push_back(device);
  }
  return result;
}

void RecordPairingDuration(const std::string& histogram_name,
                           base::TimeDelta pairing_duration) {
  base::UmaHistogramCustomTimes(histogram_name, pairing_duration,
                                base::Milliseconds(1) /* min */,
                                base::Seconds(30) /* max */, 50 /* buckets */);
}

void RecordPairingTransport(BluetoothTransport transport) {
  BluetoothTransportType type;
  switch (transport) {
    case BLUETOOTH_TRANSPORT_CLASSIC:
      type = BluetoothTransportType::kClassic;
      break;
    case BLUETOOTH_TRANSPORT_LE:
      type = BluetoothTransportType::kLE;
      break;
    case BLUETOOTH_TRANSPORT_DUAL:
      type = BluetoothTransportType::kDual;
      break;
    case BLUETOOTH_TRANSPORT_INVALID:
      type = BluetoothTransportType::kInvalid;
      break;
    default:
      type = BluetoothTransportType::kUnknown;
      break;
  }

  base::UmaHistogramEnumeration("Bluetooth.ChromeOS.Pairing.TransportType",
                                type);
}

void RecordDeviceSelectionDuration(const std::string& histogram_name,
                                   base::TimeDelta duration) {
  base::UmaHistogramCustomTimes(
      histogram_name, duration, base::Milliseconds(1) /* min */,
      kMaxDeviceSelectionDuration /* max */, 50 /* buckets */);
}

std::string GetTransportName(BluetoothTransport transport) {
  switch (transport) {
    case BluetoothTransport::BLUETOOTH_TRANSPORT_CLASSIC:
      return "Classic";
    case BluetoothTransport::BLUETOOTH_TRANSPORT_LE:
      return "BLE";
    case BluetoothTransport::BLUETOOTH_TRANSPORT_DUAL:
      return "Dual";
    case BLUETOOTH_TRANSPORT_INVALID:
      return "Invalid";
    default:
      // A transport type of other is unexpected, and no success
      // metric for it exists.
      return "";
  }
}

bool IsUserError(std::optional<ConnectionFailureReason> failure_reason) {
  if (!failure_reason.has_value()) {
    return false;
  }

  switch (failure_reason.value()) {
    case ConnectionFailureReason::kInprogress:
      [[fallthrough]];
    case ConnectionFailureReason::kNotFound:
      [[fallthrough]];
    case ConnectionFailureReason::kAuthCanceled:
      return true;
    case ConnectionFailureReason::kAuthRejected:
      [[fallthrough]];
    case ConnectionFailureReason::kUnknownError:
      [[fallthrough]];
    case ConnectionFailureReason::kAuthFailed:
      [[fallthrough]];
    case ConnectionFailureReason::kAuthTimeout:
      [[fallthrough]];
    case ConnectionFailureReason::kUnknownConnectionError:
      [[fallthrough]];
    case ConnectionFailureReason::kUnsupportedDevice:
      [[fallthrough]];
    case ConnectionFailureReason::kNotConnectable:
      [[fallthrough]];
    case ConnectionFailureReason::kSystemError:
      [[fallthrough]];
    case ConnectionFailureReason::kFailed:
      [[fallthrough]];
    case ConnectionFailureReason::kBluetoothDisabled:
      [[fallthrough]];
    case ConnectionFailureReason::kDeviceNotReady:
      [[fallthrough]];
    case ConnectionFailureReason::kAlreadyConnected:
      [[fallthrough]];
    case ConnectionFailureReason::kDeviceAlreadyExists:
      [[fallthrough]];
    case ConnectionFailureReason::kInvalidArgs:
      [[fallthrough]];
    case ConnectionFailureReason::kNonAuthTimeout:
      [[fallthrough]];
    case ConnectionFailureReason::kNoMemory:
      [[fallthrough]];
    case ConnectionFailureReason::kJniEnvironment:
      [[fallthrough]];
    case ConnectionFailureReason::kJniThreadAttach:
      [[fallthrough]];
    case ConnectionFailureReason::kWakelock:
      [[fallthrough]];
    case ConnectionFailureReason::kUnexpectedState:
      [[fallthrough]];
    case ConnectionFailureReason::kSocketError:
      return false;
  }
  NOTREACHED();
}

void EmitFilteredFailureReason(ConnectionFailureReason failure_reason,
                               const std::string& transport_name) {
  switch (failure_reason) {
    case ConnectionFailureReason::kAuthCanceled:
      [[fallthrough]];
    case ConnectionFailureReason::kAuthRejected:
      return;
    case ConnectionFailureReason::kUnknownError:
      [[fallthrough]];
    case ConnectionFailureReason::kAuthFailed:
      [[fallthrough]];
    case ConnectionFailureReason::kAuthTimeout:
      [[fallthrough]];
    case ConnectionFailureReason::kUnknownConnectionError:
      [[fallthrough]];
    case ConnectionFailureReason::kUnsupportedDevice:
      [[fallthrough]];
    case ConnectionFailureReason::kNotConnectable:
      [[fallthrough]];
    case ConnectionFailureReason::kSystemError:
      [[fallthrough]];
    case ConnectionFailureReason::kFailed:
      [[fallthrough]];
    case ConnectionFailureReason::kInprogress:
      [[fallthrough]];
    case ConnectionFailureReason::kNotFound:
      [[fallthrough]];
    case ConnectionFailureReason::kBluetoothDisabled:
      [[fallthrough]];
    case ConnectionFailureReason::kDeviceNotReady:
      [[fallthrough]];
    case ConnectionFailureReason::kAlreadyConnected:
      [[fallthrough]];
    case ConnectionFailureReason::kDeviceAlreadyExists:
      [[fallthrough]];
    case ConnectionFailureReason::kInvalidArgs:
      [[fallthrough]];
    case ConnectionFailureReason::kNonAuthTimeout:
      [[fallthrough]];
    case ConnectionFailureReason::kNoMemory:
      [[fallthrough]];
    case ConnectionFailureReason::kJniEnvironment:
      [[fallthrough]];
    case ConnectionFailureReason::kJniThreadAttach:
      [[fallthrough]];
    case ConnectionFailureReason::kWakelock:
      [[fallthrough]];
    case ConnectionFailureReason::kUnexpectedState:
      [[fallthrough]];
    case ConnectionFailureReason::kSocketError:
      const std::string result_histogram_name_prefix =
          "Bluetooth.ChromeOS.Pairing.Result";
      base::UmaHistogramEnumeration(
          result_histogram_name_prefix + ".FilteredFailureReason",
          failure_reason);
      base::UmaHistogramEnumeration(result_histogram_name_prefix +
                                        ".FilteredFailureReason." +
                                        transport_name,
                                    failure_reason);
      return;
  }
  NOTREACHED();
}

bool IsNonPhonePolyDevice(const device::BluetoothDevice* device) {
  // OUI portions of Bluetooth addresses for devices manufactured by Poly. See
  // https://standards-oui.ieee.org/. This also includes acquisitions by Poly,
  // namely ViaVideo and PictureTel.
  // Actually there are still others belonging in this category, but they are
  // truly phones, e.g. Voyant and Spectralink, so we omit those here.
  constexpr auto kNonPhonePolyOuis = base::MakeFixedFlatSet<std::string_view>(
      {"64:16:7F", "48:25:67", "00:04:F2", "00:E0:DB", "00:10:1A"});

  return base::Contains(kNonPhonePolyOuis,
                        device->GetOuiPortionOfBluetoothAddress());
}

// Provide heuristics for which transport to use for a dual device
BluetoothTransport InferDeviceTransport(const device::BluetoothDevice* device) {
  if (device->GetType() != BLUETOOTH_TRANSPORT_DUAL) {
    return device->GetType();
  }

  // Random address type indicates LE device.
  if (device->GetAddressType() ==
      BluetoothDevice::AddressType::ADDR_TYPE_RANDOM) {
    return BLUETOOTH_TRANSPORT_LE;
  }

  // Devices without type/appearance most likely signals that it is truly only
  // a LE advertisement for a peripheral which is active, but not pairable. Many
  // popular headphones behave in this exact way. Mark as invalid until they
  // provide a type/appearance; this means they've become pairable. See
  // https://crrev.com/c/1656971 for more.
  if (device->GetDeviceType() == BluetoothDeviceType::UNKNOWN) {
    return BLUETOOTH_TRANSPORT_INVALID;
  }

  return BLUETOOTH_TRANSPORT_CLASSIC;
}

}  // namespace

ConnectionFailureReason GetConnectionFailureReason(
    device::BluetoothDevice::ConnectErrorCode error_code) {
  switch (error_code) {
    case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED:
      return device::ConnectionFailureReason::kAuthCanceled;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_FAILED:
      return device::ConnectionFailureReason::kAuthFailed;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED:
      return device::ConnectionFailureReason::kAuthRejected;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_TIMEOUT:
      return device::ConnectionFailureReason::kAuthTimeout;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_FAILED:
      return device::ConnectionFailureReason::kFailed;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS:
      return device::ConnectionFailureReason::kInprogress;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN:
      return device::ConnectionFailureReason::kUnknownError;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_UNSUPPORTED_DEVICE:
      return device::ConnectionFailureReason::kUnsupportedDevice;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_DEVICE_NOT_READY:
      return device::ConnectionFailureReason::kDeviceNotReady;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_ALREADY_CONNECTED:
      return device::ConnectionFailureReason::kAlreadyConnected;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_DEVICE_ALREADY_EXISTS:
      return device::ConnectionFailureReason::kDeviceAlreadyExists;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_DEVICE_UNCONNECTED:
      return device::ConnectionFailureReason::kNotConnectable;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_DOES_NOT_EXIST:
      return device::ConnectionFailureReason::kNotFound;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_INVALID_ARGS:
      return device::ConnectionFailureReason::kInvalidArgs;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_NON_AUTH_TIMEOUT:
      return device::ConnectionFailureReason::kNonAuthTimeout;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_NO_MEMORY:
      return device::ConnectionFailureReason::kNoMemory;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_JNI_ENVIRONMENT:
      return device::ConnectionFailureReason::kJniEnvironment;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_JNI_THREAD_ATTACH:
      return device::ConnectionFailureReason::kJniThreadAttach;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_WAKELOCK:
      return device::ConnectionFailureReason::kWakelock;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_UNEXPECTED_STATE:
      return device::ConnectionFailureReason::kUnexpectedState;
    case device::BluetoothDevice::ConnectErrorCode::ERROR_SOCKET:
      return device::ConnectionFailureReason::kSocketError;
    case device::BluetoothDevice::ConnectErrorCode::NUM_CONNECT_ERROR_CODES:
      NOTREACHED();
  }
}

device::BluetoothAdapter::DeviceList FilterBluetoothDeviceList(
    const BluetoothAdapter::DeviceList& devices,
    BluetoothFilterType filter_type,
    int max_devices) {
  BluetoothAdapter::DeviceList filtered_devices =
      filter_type == BluetoothFilterType::KNOWN ? FilterUnknownDevices(devices)
                                                : devices;
  return GetLimitedNumDevices(max_devices, filtered_devices);
}

bool IsUnsupportedDevice(const device::BluetoothDevice* device) {
  if (ash::switches::IsUnfilteredBluetoothDevicesEnabled()) {
    return false;
  }

  // Never filter out Poly devices; this requires a special case since these
  // devices often identify themselves as phones, which are disallowed below.
  // See b/228118615.
  if (IsNonPhonePolyDevice(device)) {
    return false;
  }

  // Always allow bonded devices to appear in the UI.
  if (device->IsBonded()) {
    return false;
  }

  // Always filter out laptops, etc. There is no intended use case or
  // Bluetooth profile in this context.
  if (device->GetDeviceType() == BluetoothDeviceType::COMPUTER) {
    return true;
  }

  // Always filter out phones. There is no intended use case or Bluetooth
  // profile in this context.
  if (device->GetDeviceType() == BluetoothDeviceType::PHONE) {
    return true;
  }

  const BluetoothDevice::UUIDSet& uuids = device->GetUUIDs();

  // These UUIDs are specific to Nearby Share and Phone Hub and are used to
  // identify devices that should be filtered from the UI that otherwise would
  // not have been correctly identified. These devices should always be filtered
  // from the UI. For more information see b/219627324.
  for (const auto& uuid : ash::nearby::GetNearbyClientUuids()) {
    if (uuids.contains(uuid)) {
      return true;
    }
  }
  if (uuids.contains(BluetoothUUID(ash::secure_channel::kGattServerUuid))) {
    return true;
  }

  switch (InferDeviceTransport(device)) {
    // For LE devices, check the discoverable flag and UUIDs.
    case BLUETOOTH_TRANSPORT_LE:
      // Hide the LE device that mark itself as non-discoverble.
      if (device->GetAdvertisingDataFlags().has_value()) {
        if (!((kLimitedDiscoveryFlag | kGeneralDiscoveryFlag) &
              device->GetAdvertisingDataFlags().value())) {
          return true;
        }
      }
      // Check the service UUID to determine if it supports HID or second factor
      // authenticator (security key).
      if (base::Contains(device->GetUUIDs(),
                         device::BluetoothUUID(kHIDServiceUUID)) ||
          base::Contains(device->GetUUIDs(),
                         device::BluetoothUUID(kSecurityKeyServiceUUID))) {
        return false;
      }
      break;
    // For classic mode devices, only filter out if the name is empty because
    // the device could have an unknown or even known type and still also
    // provide audio/HID functionality.
    case BLUETOOTH_TRANSPORT_CLASSIC:
      if (device->GetName()) {
        return false;
      }
      break;
    // Otherwise, they are invalid, so filter them out.
    default:
      break;
  }

  return true;
}

void RecordPairingResult(std::optional<ConnectionFailureReason> failure_reason,
                         BluetoothTransport transport,
                         base::TimeDelta duration) {
  RecordPairingTransport(transport);

  std::string transport_name = GetTransportName(transport);
  if (transport_name.empty()) {
    return;
  }

  bool success = !failure_reason.has_value();
  std::string result_histogram_name_prefix =
      "Bluetooth.ChromeOS.Pairing.Result";
  std::string result_histogram_user_errors_filtered_name =
      result_histogram_name_prefix + "." + "UserErrorsFiltered2";

  base::UmaHistogramBoolean(result_histogram_name_prefix, success);
  base::UmaHistogramBoolean(result_histogram_name_prefix + "." + transport_name,
                            success);
  if (!IsUserError(failure_reason)) {
    base::UmaHistogramBoolean(result_histogram_user_errors_filtered_name,
                              success);
  }

  std::string duration_histogram_name_prefix =
      "Bluetooth.ChromeOS.Pairing.Duration";
  std::string success_histogram_name = success ? "Success" : "Failure";

  std::string base_histogram_name =
      duration_histogram_name_prefix + "." + success_histogram_name;
  RecordPairingDuration(base_histogram_name, duration);
  RecordPairingDuration(base_histogram_name + "." + transport_name, duration);

  if (!success) {
    base::UmaHistogramEnumeration(
        result_histogram_name_prefix + ".FailureReason", *failure_reason);
    base::UmaHistogramEnumeration(
        result_histogram_name_prefix + ".FailureReason." + transport_name,
        *failure_reason);
    EmitFilteredFailureReason(*failure_reason, transport_name);
  }
}

void RecordUserInitiatedReconnectionAttemptResult(
    std::optional<ConnectionFailureReason> failure_reason,
    UserInitiatedReconnectionUISurfaces surface) {
  bool success = !failure_reason.has_value();
  std::string base_histogram_name =
      "Bluetooth.ChromeOS.UserInitiatedReconnectionAttempt.Result";
  std::string result_histogram_user_errors_filtered_name =
      base_histogram_name + ".UserErrorsFiltered";

  if (!IsUserError(failure_reason)) {
    base::UmaHistogramBoolean(result_histogram_user_errors_filtered_name,
                              success);
  }

  base::UmaHistogramBoolean(base_histogram_name, success);

  std::string surface_name =
      (surface == UserInitiatedReconnectionUISurfaces::kSettings
           ? "Settings"
           : "SystemTray");
  base::UmaHistogramBoolean(base_histogram_name + "." + surface_name, success);

  if (!success) {
    base::UmaHistogramEnumeration(base_histogram_name + ".FailureReason",
                                  *failure_reason);
    base::UmaHistogramEnumeration(
        base_histogram_name + ".FailureReason." + surface_name,
        *failure_reason);
  }
}

void RecordDeviceSelectionDuration(base::TimeDelta duration,
                                   DeviceSelectionUISurfaces surface,
                                   bool was_paired,
                                   BluetoothTransport transport) {
  // Throw out longtail results of the user taking longer than
  // |kMaxDeviceSelectionDuration|. Assume that these thrown out results reflect
  // the user not being actively engaged with device connection: leaving the
  // page open for a long time, walking away from computer, etc.
  if (duration > kMaxDeviceSelectionDuration) {
    return;
  }

  std::string base_histogram_name =
      "Bluetooth.ChromeOS.DeviceSelectionDuration";
  RecordDeviceSelectionDuration(base_histogram_name, duration);

  std::string surface_name =
      (surface == DeviceSelectionUISurfaces::kSettings ? "Settings"
                                                       : "SystemTray");
  std::string surface_histogram_name = base_histogram_name + "." + surface_name;
  RecordDeviceSelectionDuration(surface_histogram_name, duration);

  std::string paired_name = (was_paired ? "Paired" : "NotPaired");
  std::string paired_histogram_name =
      surface_histogram_name + "." + paired_name;
  RecordDeviceSelectionDuration(paired_histogram_name, duration);

  if (!was_paired) {
    std::string transport_name = GetTransportName(transport);
    if (transport_name.empty()) {
      return;
    }
    std::string transport_histogram_name =
        paired_histogram_name + "." + transport_name;
    RecordDeviceSelectionDuration(transport_histogram_name, duration);
  }
}

void RecordPoweredStateOperationResult(PoweredStateOperation operation,
                                       bool success) {
  std::string operation_name =
      operation == PoweredStateOperation::kEnable ? "Enable" : "Disable";

  base::UmaHistogramBoolean(base::StrCat({"Bluetooth.ChromeOS.PoweredState.",
                                          operation_name, ".Result"}),
                            success);
}

void RecordPoweredState(bool is_powered) {
  base::UmaHistogramBoolean("Bluetooth.ChromeOS.PoweredState", is_powered);
}

void RecordForgetResult(ForgetResult forget_result) {
  base::UmaHistogramEnumeration("Bluetooth.ChromeOS.Forget.Result",
                                forget_result);
}

void RecordDeviceDisconnect(BluetoothDeviceType device_type) {
  base::UmaHistogramEnumeration("Bluetooth.ChromeOS.DeviceDisconnect",
                                device_type);
}

void RecordUserInitiatedDisconnectResult(DisconnectResult disconnect_result,
                                         BluetoothTransport transport) {
  std::string transport_name = GetTransportName(transport);

  if (transport_name.empty()) {
    return;
  }

  base::UmaHistogramEnumeration(
      "Bluetooth.ChromeOS.UserInitiatedDisconnect.Result", disconnect_result);
  base::UmaHistogramEnumeration(
      base::StrCat({"Bluetooth.ChromeOS.UserInitiatedDisconnect.Result.",
                    transport_name}),
      disconnect_result);
}

void RecordUiSurfaceDisplayed(BluetoothUiSurface ui_surface) {
  base::UmaHistogramEnumeration("Bluetooth.ChromeOS.UiSurfaceDisplayed",
                                ui_surface);
}

void RecordUserInitiatedReconnectionAttemptDuration(
    std::optional<ConnectionFailureReason> failure_reason,
    BluetoothTransport transport,
    base::TimeDelta duration) {
  bool success = !failure_reason.has_value();
  std::string transport_name = GetTransportName(transport);

  if (transport_name.empty()) {
    return;
  }
  std::string success_histogram_name = success ? "Success" : "Failure";

  std::string base_histogram_name = base::StrCat(
      {"Bluetooth.ChromeOS.UserInitiatedReconnectionAttempt.Duration.",
       success_histogram_name});
  base::UmaHistogramTimes(base_histogram_name, duration);
  base::UmaHistogramTimes(
      base::StrCat({base_histogram_name, ".", transport_name}), duration);
}

void RecordSetDeviceNickName(SetNicknameResult set_nickname_result) {
  base::UmaHistogramEnumeration("Bluetooth.ChromeOS.SetNickname.Result",
                                set_nickname_result);
}

void RecordTimeIntervalBetweenConnections(
    base::TimeDelta time_interval_since_last_connection) {
  if (time_interval_since_last_connection >= kConnectionTimeIntervalThreshold) {
    return;
  }
  base::UmaHistogramCustomTimes(
      "Bluetooth.ChromeOS.TimeIntervalBetweenConnections",
      time_interval_since_last_connection,
      /*min=*/base::Milliseconds(0),
      /*max=*/kConnectionTimeIntervalThreshold, 100);
}

void MaybeRecordConnectionToastShownCount(PrefService* local_state_pref,
                                          bool triggered_by_connect) {
  bool is_within_24_hrs =
      base::Time::Now() -
          local_state_pref->GetTime(ash::prefs::kBluetoothToastCountStartTime) <
      kToastShownCountTimeIntervalThreshold;
  int toast_shown_count = local_state_pref->GetInteger(
      ash::prefs::kBluetoothConnectionToastShownCount);

  if (is_within_24_hrs && triggered_by_connect) {
    // Increment the count if within 24 hours and it is triggered by connect.
    local_state_pref->SetInteger(
        ash::prefs::kBluetoothConnectionToastShownCount, toast_shown_count + 1);
    return;
  }

  if (is_within_24_hrs) {
    // Do nothing since we haven't exceeded the time interval.
    return;
  }

  // Emit metric and reset count and timestamp if 24 hours have passed.
  base::UmaHistogramCounts100(
      "Bluetooth.ChromeOS.ConnectionToastShownIn24Hours.Count",
      toast_shown_count);
  // Reset the count, and update the start time.
  local_state_pref->SetInteger(ash::prefs::kBluetoothConnectionToastShownCount,
                               triggered_by_connect ? 1 : 0);
  local_state_pref->SetTime(ash::prefs::kBluetoothToastCountStartTime,
                            base::Time::Now().LocalMidnight());
}

void RecordFlossManagerClientInit(bool success, base::TimeDelta duration) {
  static constexpr char kSuccessHistogramSuffix[] = "Success";
  static constexpr char kFailureHistogramSuffix[] = "Failure";
  std::string success_histogram_name =
      success ? kSuccessHistogramSuffix : kFailureHistogramSuffix;

  base::UmaHistogramTimes(
      base::StrCat({"Bluetooth.ChromeOS.FlossManagerClientInit.Duration.",
                    success_histogram_name}),
      duration);

  base::UmaHistogramBoolean("Bluetooth.ChromeOS.FlossManagerClientInit.Result",
                            success);
}

void RecordDeviceKeyMissing() {
  base::UmaHistogramBoolean("Bluetooth.ChromeOS.DeviceKeyMissing", true);
}

}  // namespace device