1 // Copyright 2020 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef THIRD_PARTY_BLINK_PUBLIC_COMMON_PRIVACY_BUDGET_SCOPED_IDENTIFIABILITY_TEST_SAMPLE_COLLECTOR_H_ 6 #define THIRD_PARTY_BLINK_PUBLIC_COMMON_PRIVACY_BUDGET_SCOPED_IDENTIFIABILITY_TEST_SAMPLE_COLLECTOR_H_ 7 8 #include <memory> 9 #include <vector> 10 11 #include "base/component_export.h" 12 #include "third_party/blink/public/common/privacy_budget/identifiability_sample_collector.h" 13 #include "third_party/blink/public/common/privacy_budget/scoped_switch_sample_collector.h" 14 15 namespace blink { 16 namespace test { 17 18 // An `IdentifiabilitySampleCollector` implementation for testing. Allows 19 // inspecting recorded metrics. 20 // 21 // Instantiating this class automatically sets the per-process 22 // `IdentifiabilitySampleCollector` to point to the new instance. 23 // 24 // Note: Unlike the real collector nothing in this class is thread safe. COMPONENT_EXPORT(PRIVACY_BUDGET_TEST_SUPPORT)25class COMPONENT_EXPORT(PRIVACY_BUDGET_TEST_SUPPORT) 26 ScopedIdentifiabilityTestSampleCollector 27 : public IdentifiabilitySampleCollector { 28 public: 29 ScopedIdentifiabilityTestSampleCollector(); 30 ~ScopedIdentifiabilityTestSampleCollector() override; 31 32 // IdentifiabilitySampleCollector 33 void Record(ukm::UkmRecorder* recorder, 34 ukm::SourceId source, 35 std::vector<IdentifiableSample> metrics) override; 36 void Flush(ukm::UkmRecorder* recorder) override; 37 void FlushSource(ukm::UkmRecorder* recorder, ukm::SourceId source) override; 38 39 // Each call to `Record()` results in one of these being added to `entries()` 40 // in order of occurrence that faithfully records the arguments to `Record()`. 41 struct Entry { 42 Entry(ukm::SourceId source_in, std::vector<IdentifiableSample> metrics_in) 43 : source(source_in), metrics(std::move(metrics_in)) {} 44 45 const ukm::SourceId source; 46 const std::vector<IdentifiableSample> metrics; 47 }; 48 49 // Returns a reference to the list of `Entry` objects representing the 50 // `Record()` calls received so far. 51 const std::vector<Entry>& entries() const { return entries_; } 52 53 // Reset all recorded entries. `entries()` returns an empty list after this. 54 void ClearEntries(); 55 56 private: 57 std::vector<Entry> entries_; 58 ScopedSwitchSampleCollector scoped_default_; 59 }; 60 61 } // namespace test 62 } // namespace blink 63 64 #endif // THIRD_PARTY_BLINK_PUBLIC_COMMON_PRIVACY_BUDGET_SCOPED_IDENTIFIABILITY_TEST_SAMPLE_COLLECTOR_H_ 65