1 // Copyright 2023 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/test/test_structured_metrics_provider.h" 6 7 #include "base/files/file_path.h" 8 #include "base/memory/ptr_util.h" 9 #include "base/memory/scoped_refptr.h" 10 #include "base/run_loop.h" 11 #include "base/test/bind.h" 12 #include "components/metrics/structured/test/test_event_storage.h" 13 #include "components/metrics/structured/test/test_key_data_provider.h" 14 15 namespace metrics::structured { 16 TestStructuredMetricsProvider()17TestStructuredMetricsProvider::TestStructuredMetricsProvider() { 18 if (temp_dir_.CreateUniqueTempDir()) { 19 structured_metrics_recorder_ = 20 base::MakeRefCounted<StructuredMetricsRecorder>( 21 std::make_unique<TestKeyDataProvider>( 22 temp_dir_.GetPath() 23 .Append(FILE_PATH_LITERAL("structured_metrics")) 24 .Append(FILE_PATH_LITERAL("device_keys"))), 25 std::make_unique<TestEventStorage>()); 26 Recorder::GetInstance()->SetRecorder(this); 27 } 28 } 29 TestStructuredMetricsProvider(scoped_refptr<StructuredMetricsRecorder> recorder)30TestStructuredMetricsProvider::TestStructuredMetricsProvider( 31 scoped_refptr<StructuredMetricsRecorder> recorder) 32 : structured_metrics_recorder_(std::move(recorder)) { 33 Recorder::GetInstance()->SetRecorder(this); 34 } 35 ~TestStructuredMetricsProvider()36TestStructuredMetricsProvider::~TestStructuredMetricsProvider() { 37 Recorder::GetInstance()->UnsetRecorder(this); 38 } 39 ReadEvents() const40const EventsProto& TestStructuredMetricsProvider::ReadEvents() const { 41 return *static_cast<const TestEventStorage*>( 42 structured_metrics_recorder_->event_storage()) 43 ->events(); 44 } 45 46 std::optional<const StructuredEventProto*> FindEvent(uint64_t project_name_hash,uint64_t event_name_hash)47TestStructuredMetricsProvider::FindEvent(uint64_t project_name_hash, 48 uint64_t event_name_hash) { 49 if (!structured_metrics_recorder_->CanProvideMetrics()) { 50 return std::nullopt; 51 } 52 53 const EventsProto& events = TestStructuredMetricsProvider::ReadEvents(); 54 55 for (const auto& event : events.events()) { 56 if (event.project_name_hash() == project_name_hash && 57 event.event_name_hash() == event_name_hash) { 58 return &event; 59 } 60 } 61 return std::nullopt; 62 } 63 64 std::vector<const StructuredEventProto*> FindEvents(uint64_t project_name_hash,uint64_t event_name_hash)65TestStructuredMetricsProvider::FindEvents(uint64_t project_name_hash, 66 uint64_t event_name_hash) { 67 std::vector<const StructuredEventProto*> events_vector; 68 if (!structured_metrics_recorder_->CanProvideMetrics()) { 69 return events_vector; 70 } 71 72 const EventsProto& events = TestStructuredMetricsProvider::ReadEvents(); 73 for (const auto& event : events.events()) { 74 if (event.project_name_hash() == project_name_hash && 75 event.event_name_hash() == event_name_hash) { 76 events_vector.push_back(&event); 77 } 78 } 79 return events_vector; 80 } 81 EnableRecording()82void TestStructuredMetricsProvider::EnableRecording() { 83 structured_metrics_recorder_->EnableRecording(); 84 } 85 DisableRecording()86void TestStructuredMetricsProvider::DisableRecording() { 87 structured_metrics_recorder_->DisableRecording(); 88 } 89 WaitUntilReady()90void TestStructuredMetricsProvider::WaitUntilReady() { 91 base::RunLoop run_loop; 92 structured_metrics_recorder_->SetOnReadyToRecord( 93 base::BindLambdaForTesting([&run_loop]() { run_loop.Quit(); })); 94 run_loop.Run(); 95 } 96 SetOnEventsRecordClosure(base::RepeatingCallback<void (const Event & event)> event_record_callback)97void TestStructuredMetricsProvider::SetOnEventsRecordClosure( 98 base::RepeatingCallback<void(const Event& event)> event_record_callback) { 99 event_record_callback_ = std::move(event_record_callback); 100 } 101 OnEventRecord(const Event & event)102void TestStructuredMetricsProvider::OnEventRecord(const Event& event) { 103 structured_metrics_recorder_->OnEventRecord(event); 104 if (!event_record_callback_) { 105 return; 106 } 107 108 event_record_callback_.Run(event); 109 } 110 111 } // namespace metrics::structured 112