File: quick_pair_metrics_logger.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (125 lines) | stat: -rw-r--r-- 5,557 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
// 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.

#ifndef ASH_QUICK_PAIR_KEYED_SERVICE_QUICK_PAIR_METRICS_LOGGER_H_
#define ASH_QUICK_PAIR_KEYED_SERVICE_QUICK_PAIR_METRICS_LOGGER_H_

#include "ash/quick_pair/pairing/pairer_broker.h"
#include "ash/quick_pair/pairing/retroactive_pairing_detector.h"
#include "ash/quick_pair/scanning/scanner_broker.h"
#include "ash/quick_pair/ui/ui_broker.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "device/bluetooth/bluetooth_adapter.h"

namespace ash {
namespace quick_pair {

class Device;
class FastPairFeatureUsageMetricsLogger;

// Observes pairing, scanning and UI events and logs corresponding metrics.
class QuickPairMetricsLogger : public PairerBroker::Observer,
                               public ScannerBroker::Observer,
                               public UIBroker::Observer,
                               public RetroactivePairingDetector::Observer,
                               public device::BluetoothAdapter::Observer {
 public:
  QuickPairMetricsLogger(
      ScannerBroker* scanner_broker,
      PairerBroker* pairer_broker,
      UIBroker* ui_broker,
      RetroactivePairingDetector* retroactive_pairing_detector);
  QuickPairMetricsLogger(const QuickPairMetricsLogger&) = delete;
  QuickPairMetricsLogger& operator=(const QuickPairMetricsLogger&) = delete;
  ~QuickPairMetricsLogger() override;

 private:
  // PairerBroker::Observer
  void OnPairingStart(scoped_refptr<Device> device) override;
  void OnHandshakeComplete(scoped_refptr<Device> device) override;
  void OnDevicePaired(scoped_refptr<Device> device) override;
  void OnAccountKeyWrite(scoped_refptr<Device> device,
                         std::optional<AccountKeyFailure> error) override;
  void OnPairingComplete(scoped_refptr<Device> device) override;
  void OnPairFailure(scoped_refptr<Device> device,
                     PairFailure failure) override;

  // UIBroker::Observer
  void OnDiscoveryAction(scoped_refptr<Device> device,
                         DiscoveryAction action) override;
  void OnCompanionAppAction(scoped_refptr<Device> device,
                            CompanionAppAction action) override;
  void OnPairingFailureAction(scoped_refptr<Device> device,
                              PairingFailedAction action) override;
  void OnAssociateAccountAction(scoped_refptr<Device> device,
                                AssociateAccountAction action) override;

  // ScannerBroker::Observer
  void OnDeviceFound(scoped_refptr<Device> device) override;
  void OnDeviceLost(scoped_refptr<Device> device) override;

  // RetroactivePairingDetector::Observer
  void OnRetroactivePairFound(scoped_refptr<Device> device) override;

  // device::BluetoothAdapter::Observer
  void DevicePairedChanged(device::BluetoothAdapter* adapter,
                           device::BluetoothDevice* device,
                           bool new_paired_status) override;

  // Internal method called by BluetoothAdapterFactory to provide the adapter
  // object.
  void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter);

  // Helper function called to get the Bluetooth Device that corresponds with
  // the address saved in |device|. Returns nullptr if the adapter_ is not
  // initialized or the device is not found.
  const device::BluetoothDevice* GetBluetoothDevice(
      scoped_refptr<Device> device) const;

  // Map of devices to the time at which a pairing was initiated. This is used
  // to calculate the time between the user electing to pair the device and
  // the pairing entering a terminal state (success or failure).
  base::flat_map<scoped_refptr<Device>, base::TimeTicks>
      device_pairing_start_timestamps_;

  // Set of devices of which on the Associate Account UI and Discovery UI shown,
  // the Learn More button was pressed. We need this map to know which
  // |FastPairRetroactiveEngagementFlowEvent| or
  // |FastPairEngagementFlowEvent| event to log at the subsequent
  // events that will follow, since the LearnMore event is not a terminal state.
  base::flat_set<scoped_refptr<Device>> associate_account_learn_more_devices_;
  base::flat_set<scoped_refptr<Device>> discovery_learn_more_devices_;

  // The classic pairing addresses of Fast Pair devices that we have already
  // paired to.
  base::flat_set<std::string> fast_pair_addresses_;

  scoped_refptr<device::BluetoothAdapter> adapter_;
  std::unique_ptr<FastPairFeatureUsageMetricsLogger>
      feature_usage_metrics_logger_;

  base::ScopedObservation<device::BluetoothAdapter,
                          device::BluetoothAdapter::Observer>
      adapter_observation_{this};
  base::ScopedObservation<ScannerBroker, ScannerBroker::Observer>
      scanner_broker_observation_{this};
  base::ScopedObservation<PairerBroker, PairerBroker::Observer>
      pairer_broker_observation_{this};
  base::ScopedObservation<RetroactivePairingDetector,
                          RetroactivePairingDetector::Observer>
      retroactive_pairing_detector_observation_{this};
  base::ScopedObservation<UIBroker, UIBroker::Observer> ui_broker_observation_{
      this};
  base::WeakPtrFactory<QuickPairMetricsLogger> weak_ptr_factory_{this};
};

}  // namespace quick_pair
}  // namespace ash

#endif  // ASH_QUICK_PAIR_KEYED_SERVICE_QUICK_PAIR_METRICS_LOGGER_H_