File: test_structured_metrics_provider.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (111 lines) | stat: -rw-r--r-- 3,739 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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/structured/test/test_structured_metrics_provider.h"

#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "components/metrics/structured/test/test_event_storage.h"
#include "components/metrics/structured/test/test_key_data_provider.h"

namespace metrics::structured {

TestStructuredMetricsProvider::TestStructuredMetricsProvider() {
  if (temp_dir_.CreateUniqueTempDir()) {
    structured_metrics_recorder_ =
        base::MakeRefCounted<StructuredMetricsRecorder>(
            std::make_unique<TestKeyDataProvider>(
                temp_dir_.GetPath()
                    .Append(FILE_PATH_LITERAL("structured_metrics"))
                    .Append(FILE_PATH_LITERAL("device_keys"))),
            std::make_unique<TestEventStorage>());
    Recorder::GetInstance()->SetRecorder(this);
  }
}

TestStructuredMetricsProvider::TestStructuredMetricsProvider(
    scoped_refptr<StructuredMetricsRecorder> recorder)
    : structured_metrics_recorder_(std::move(recorder)) {
  Recorder::GetInstance()->SetRecorder(this);
}

TestStructuredMetricsProvider::~TestStructuredMetricsProvider() {
  Recorder::GetInstance()->UnsetRecorder(this);
}

const EventsProto& TestStructuredMetricsProvider::ReadEvents() const {
  return *static_cast<const TestEventStorage*>(
              structured_metrics_recorder_->event_storage())
              ->events();
}

std::optional<const StructuredEventProto*>
TestStructuredMetricsProvider::FindEvent(uint64_t project_name_hash,
                                         uint64_t event_name_hash) {
  if (!structured_metrics_recorder_->CanProvideMetrics()) {
    return std::nullopt;
  }

  const EventsProto& events = TestStructuredMetricsProvider::ReadEvents();

  for (const auto& event : events.events()) {
    if (event.project_name_hash() == project_name_hash &&
        event.event_name_hash() == event_name_hash) {
      return &event;
    }
  }
  return std::nullopt;
}

std::vector<const StructuredEventProto*>
TestStructuredMetricsProvider::FindEvents(uint64_t project_name_hash,
                                          uint64_t event_name_hash) {
  std::vector<const StructuredEventProto*> events_vector;
  if (!structured_metrics_recorder_->CanProvideMetrics()) {
    return events_vector;
  }

  const EventsProto& events = TestStructuredMetricsProvider::ReadEvents();
  for (const auto& event : events.events()) {
    if (event.project_name_hash() == project_name_hash &&
        event.event_name_hash() == event_name_hash) {
      events_vector.push_back(&event);
    }
  }
  return events_vector;
}

void TestStructuredMetricsProvider::EnableRecording() {
  structured_metrics_recorder_->EnableRecording();
}

void TestStructuredMetricsProvider::DisableRecording() {
  structured_metrics_recorder_->DisableRecording();
}

void TestStructuredMetricsProvider::WaitUntilReady() {
  base::RunLoop run_loop;
  structured_metrics_recorder_->SetOnReadyToRecord(
      base::BindLambdaForTesting([&run_loop]() { run_loop.Quit(); }));
  run_loop.Run();
}

void TestStructuredMetricsProvider::SetOnEventsRecordClosure(
    base::RepeatingCallback<void(const Event& event)> event_record_callback) {
  event_record_callback_ = std::move(event_record_callback);
}

void TestStructuredMetricsProvider::OnEventRecord(const Event& event) {
  structured_metrics_recorder_->OnEventRecord(event);
  if (!event_record_callback_) {
    return;
  }

  event_record_callback_.Run(event);
}

}  // namespace metrics::structured