• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/metrics/structured/recorder.h"
6 
7 #include <utility>
8 
9 #include "base/no_destructor.h"
10 #include "base/task/current_thread.h"
11 #include "base/task/sequenced_task_runner.h"
12 #include "components/metrics/structured/histogram_util.h"
13 #include "components/metrics/structured/lib/histogram_util.h"
14 #include "components/metrics/structured/structured_metrics_features.h"
15 
16 namespace metrics::structured {
17 
18 Recorder::Recorder() = default;
19 
20 Recorder::~Recorder() = default;
21 
GetInstance()22 Recorder* Recorder::GetInstance() {
23   static base::NoDestructor<Recorder> recorder;
24   return recorder.get();
25 }
26 
RecordEvent(Event && event)27 void Recorder::RecordEvent(Event&& event) {
28   // If the recorder is null, this doesn't need to be run on the same sequence.
29   if (recorder_ == nullptr) {
30     // Other values of EventRecordingState are recorded in
31     // StructuredMetricsProvider::OnRecord.
32     LogEventRecordingState(EventRecordingState::kProviderMissing);
33     return;
34   }
35 
36   // All calls to StructuredMetricsProvider (the observer) must be on the UI
37   // sequence, so re-call Record if needed. If a UI task runner hasn't been set
38   // yet, ignore this Record.
39   if (!ui_task_runner_) {
40     LogInternalError(StructuredMetricsError::kUninitializedClient);
41     return;
42   }
43 
44   if (!ui_task_runner_->RunsTasksInCurrentSequence()) {
45     ui_task_runner_->PostTask(
46         FROM_HERE, base::BindOnce(&Recorder::RecordEvent,
47                                   base::Unretained(this), std::move(event)));
48     return;
49   }
50 
51   DCHECK(base::CurrentUIThread::IsSet());
52 
53   delegating_events_processor_.OnEventsRecord(&event);
54   recorder_->OnEventRecord(event);
55 }
56 
OnSystemProfileInitialized()57 void Recorder::OnSystemProfileInitialized() {
58   if (recorder_) {
59     recorder_->OnSystemProfileInitialized();
60   }
61 }
62 
SetUiTaskRunner(const scoped_refptr<base::SequencedTaskRunner> ui_task_runner)63 void Recorder::SetUiTaskRunner(
64     const scoped_refptr<base::SequencedTaskRunner> ui_task_runner) {
65   ui_task_runner_ = ui_task_runner;
66 }
67 
SetRecorder(RecorderImpl * recorder)68 void Recorder::SetRecorder(RecorderImpl* recorder) {
69   recorder_ = recorder;
70 }
71 
UnsetRecorder(RecorderImpl * recorder)72 void Recorder::UnsetRecorder(RecorderImpl* recorder) {
73   // Only reset if this is the same recorder. Otherwise, changing the recorder
74   // isn't needed.
75   if (recorder_ == recorder) {
76     recorder_ = nullptr;
77   }
78 }
79 
AddEventsProcessor(std::unique_ptr<EventsProcessorInterface> events_processor)80 void Recorder::AddEventsProcessor(
81     std::unique_ptr<EventsProcessorInterface> events_processor) {
82   delegating_events_processor_.AddEventsProcessor(std::move(events_processor));
83 }
84 
OnProvideIndependentMetrics(ChromeUserMetricsExtension * uma_proto)85 void Recorder::OnProvideIndependentMetrics(
86     ChromeUserMetricsExtension* uma_proto) {
87   delegating_events_processor_.OnProvideIndependentMetrics(uma_proto);
88 }
89 
OnEventRecorded(StructuredEventProto * event)90 void Recorder::OnEventRecorded(StructuredEventProto* event) {
91   delegating_events_processor_.OnEventRecorded(event);
92 }
93 
94 }  // namespace metrics::structured
95