File: legacymetrics_client.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 (124 lines) | stat: -rw-r--r-- 4,935 bytes parent folder | download | duplicates (11)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_FUCHSIA_LEGACYMETRICS_LEGACYMETRICS_CLIENT_H_
#define COMPONENTS_FUCHSIA_LEGACYMETRICS_LEGACYMETRICS_CLIENT_H_

#include <fuchsia/legacymetrics/cpp/fidl.h>

#include <memory>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "components/fuchsia_legacymetrics/legacymetrics_user_event_recorder.h"

namespace fuchsia_legacymetrics {

// Used to report events & histogram data to the
// fuchsia.legacymetrics.MetricsRecorder service.
// LegacyMetricsClient must be Start()ed on an IO-capable sequence.
// Cannot be used in conjunction with other metrics reporting services.
// Must be constructed, used, and destroyed on the same sequence.
class LegacyMetricsClient {
 public:
  // Maximum number of Events to send to Record() at a time, so as to not exceed
  // the 64KB FIDL maximum message size.
  static constexpr size_t kMaxBatchSize = 50;

  // Constants for FIDL reconnection with exponential backoff.
  static constexpr base::TimeDelta kInitialReconnectDelay = base::Seconds(1);
  static constexpr base::TimeDelta kMaxReconnectDelay = base::Hours(1);
  static constexpr size_t kReconnectBackoffFactor = 2;

  using ReportAdditionalMetricsCallback = base::RepeatingCallback<void(
      base::OnceCallback<void(std::vector<fuchsia::legacymetrics::Event>)>)>;
  using NotifyFlushCallback =
      base::OnceCallback<void(base::OnceClosure completion_cb)>;

  LegacyMetricsClient();
  ~LegacyMetricsClient();

  explicit LegacyMetricsClient(const LegacyMetricsClient&) = delete;
  LegacyMetricsClient& operator=(const LegacyMetricsClient&) = delete;

  // Disables automatic MetricsRecorder connection. Caller will have to supply
  // MetricsRecorder by calling SetMetricsRecorder(). Must be called before
  // Start().
  void DisableAutoConnect();

  // Sets |metrics_recorder| to use. Should be called only after
  // DisableAutoConnect().
  void SetMetricsRecorder(
      fidl::InterfaceHandle<fuchsia::legacymetrics::MetricsRecorder>
          metrics_recorder);

  // Starts buffering data and schedules metric reporting after every
  // |report_interval|.
  void Start(base::TimeDelta report_interval);

  // Sets an asynchronous |callback| to be invoked just prior to reporting,
  // allowing users to asynchronously gather and provide additional custom
  // metrics. |callback| will receive the list of metrics when they are ready.
  // Reporting is paused until |callback| is fulfilled.
  // If used, then this method must be called before calling Start().
  void SetReportAdditionalMetricsCallback(
      ReportAdditionalMetricsCallback callback);

  // Sets a |callback| which is invoked to warn that the connection to the
  // remote MetricsRecorder will be terminated. The completion closure passed to
  // |callback| should be invoked to signal flush completion.
  void SetNotifyFlushCallback(NotifyFlushCallback callback);

  // Use when caller needs an explicit flush and then disconnect, such as before
  // termination. Caller will be notified when all events in the buffer are
  // sent.
  void FlushAndDisconnect(base::OnceClosure on_flush_complete);

 private:
  void ConnectFromComponentContext();
  void SetMetricsRecorderInternal(
      fidl::InterfaceHandle<fuchsia::legacymetrics::MetricsRecorder>
          metrics_recorder);
  void ScheduleNextReport();
  void StartReport();
  void Report(std::vector<fuchsia::legacymetrics::Event> additional_metrics);
  void OnMetricsRecorderDisconnected(zx_status_t status);
  void ReconnectMetricsRecorder();
  void OnCloseSoon();
  void CompleteFlush();
  void ResetMetricsRecorderState();

  // Incrementally sends the contents of |to_send_| to |metrics_recorder_|.
  void DrainBuffer();

  base::TimeDelta reconnect_delay_ = kInitialReconnectDelay;
  base::TimeDelta report_interval_;
  ReportAdditionalMetricsCallback report_additional_callback_;
  NotifyFlushCallback notify_flush_callback_;
  bool is_flushing_ = false;
  bool record_ack_pending_ = false;
  std::vector<fuchsia::legacymetrics::Event> to_send_;
  std::unique_ptr<LegacyMetricsUserActionRecorder> user_events_recorder_;

  bool auto_connect_ = true;
  base::RetainingOneShotTimer reconnect_timer_;

  fuchsia::legacymetrics::MetricsRecorderPtr metrics_recorder_;
  base::RetainingOneShotTimer report_timer_;
  SEQUENCE_CHECKER(sequence_checker_);

  std::vector<base::OnceClosure> on_flush_complete_closures_;

  // Prevents use-after-free if |report_additional_callback_| is invoked after
  // |this| is destroyed.
  base::WeakPtrFactory<LegacyMetricsClient> weak_factory_{this};
};

}  // namespace fuchsia_legacymetrics

#endif  // COMPONENTS_FUCHSIA_LEGACYMETRICS_LEGACYMETRICS_CLIENT_H_