File: recorder.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 (105 lines) | stat: -rw-r--r-- 3,721 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
// Copyright 2019 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_METRICS_STRUCTURED_RECORDER_H_
#define COMPONENTS_METRICS_STRUCTURED_RECORDER_H_

#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/no_destructor.h"
#include "base/task/sequenced_task_runner.h"
#include "components/metrics/structured/delegating_events_processor.h"
#include "components/metrics/structured/event.h"
#include "components/metrics/structured/events_processor_interface.h"
#include "components/metrics/structured/structured_metrics_client.h"
#include "components/metrics/structured/structured_metrics_validator.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"

namespace metrics::structured {
namespace {

using ::metrics::ChromeUserMetricsExtension;

}

// Recorder is a singleton to help communicate with the
// StructuredMetricsProvider. It serves three purposes:
// 1. Begin the initialization of the StructuredMetricsProvider (see class
// comment for more details).
// 2. Add an event for the StructuredMetricsProvider to record.
// 3. Retrieving information about project's key, specifically the day it was
// last rotated.
//
// The StructuredMetricsProvider is owned by the MetricsService, but it needs to
// be accessible to any part of the codebase, via an EventBase subclass, to
// record events. The StructuredMetricsProvider registers itself as an observer
// of this singleton when recording is enabled, and calls to Record (for
// recording) or ProfileAdded (for initialization) are then forwarded to it.
//
// Recorder is embedded within StructuredMetricsClient for Ash Chrome and should
// only be used in Ash Chrome.
//
// TODO(b/282031543): Remove this class and merge remaining logic into
// structured_metrics_recorder.h since the Record() is exposed via
// StructuredMetricsClient interface now.
//
// TODO(b/339914565): Move recording off of the UI sequence onto an IO sequence.
class Recorder {
 public:
  class RecorderImpl {
   public:
    // Called on a call to Record.
    virtual void OnEventRecord(const Event& event) = 0;
    // Called when SystemProfile has finished loading
    virtual void OnSystemProfileInitialized() {}
  };

  Recorder(const Recorder&) = delete;
  Recorder& operator=(const Recorder&) = delete;

  static Recorder* GetInstance();

  // This signals to StructuredMetricsProvider that the event should be
  // recorded.
  void RecordEvent(Event&& event);

  // Notifies observers that system profile has been loaded.
  void OnSystemProfileInitialized();

  void SetUiTaskRunner(
      const scoped_refptr<base::SequencedTaskRunner> ui_task_runner);

  base::SequencedTaskRunner* GetUiTaskRunner() { return ui_task_runner_.get(); }

  void SetRecorder(RecorderImpl* recorder);
  void UnsetRecorder(RecorderImpl* recorder);

  // Adds |events_processor| to further add metadata to recorded events or
  // listen to recorded events.
  void AddEventsProcessor(
      std::unique_ptr<EventsProcessorInterface> events_processor);

  // Modifies |uma_proto| before the log is sent.
  void OnProvideIndependentMetrics(ChromeUserMetricsExtension* uma_proto);

  // Modifies |event| once after the proto has been built.
  void OnEventRecorded(StructuredEventProto* event);

 private:
  friend class base::NoDestructor<Recorder>;

  Recorder();
  ~Recorder();

  scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;

  raw_ptr<RecorderImpl> recorder_;

  DelegatingEventsProcessor delegating_events_processor_;
};

}  // namespace metrics::structured

#endif  // COMPONENTS_METRICS_STRUCTURED_RECORDER_H_