holte | ca6486b | 2014-11-06 02:30:07 | [diff] [blame] | 1 | // Copyright 2014 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 | #include "components/rappor/test_rappor_service.h" |
| 6 | |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 9 | #include "base/logging.h" |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
holte | ca6486b | 2014-11-06 02:30:07 | [diff] [blame] | 11 | #include "components/rappor/byte_vector_utils.h" |
| 12 | #include "components/rappor/proto/rappor_metric.pb.h" |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 13 | #include "components/rappor/public/rappor_parameters.h" |
holte | 2a1a547 | 2015-01-15 00:42:38 | [diff] [blame] | 14 | #include "components/rappor/rappor_prefs.h" |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 15 | #include "components/rappor/test_log_uploader.h" |
holte | ca6486b | 2014-11-06 02:30:07 | [diff] [blame] | 16 | |
| 17 | namespace rappor { |
| 18 | |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 19 | namespace { |
| 20 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 21 | bool MockIsIncognito(bool* is_incognito) { |
| 22 | return *is_incognito; |
holte | 79705c8 | 2014-11-14 23:41:04 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | } // namespace |
| 26 | |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 27 | TestSample::TestSample(RapporType type) |
| 28 | : Sample(0, internal::kRapporParametersForType[type]), shadow_(type) {} |
| 29 | |
| 30 | TestSample::~TestSample() {} |
| 31 | |
| 32 | void TestSample::SetStringField(const std::string& field_name, |
| 33 | const std::string& value) { |
| 34 | shadow_.string_fields[field_name] = value; |
| 35 | Sample::SetStringField(field_name, value); |
| 36 | } |
| 37 | |
| 38 | void TestSample::SetFlagsField(const std::string& field_name, |
| 39 | uint64_t flags, |
| 40 | size_t num_flags) { |
| 41 | shadow_.flag_fields[field_name] = flags; |
| 42 | Sample::SetFlagsField(field_name, flags, num_flags); |
| 43 | } |
| 44 | |
nzolghadr | 3fddfc5 | 2016-12-13 21:52:12 | [diff] [blame] | 45 | void TestSample::SetUInt64Field(const std::string& field_name, |
| 46 | uint64_t value, |
| 47 | NoiseLevel noise_level) { |
| 48 | shadow_.uint64_fields[field_name] = |
| 49 | std::pair<std::uint64_t, NoiseLevel>(value, noise_level); |
| 50 | Sample::SetUInt64Field(field_name, value, noise_level); |
| 51 | } |
| 52 | |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 53 | TestSample::Shadow::Shadow(RapporType type) : type(type) {} |
| 54 | |
| 55 | TestSample::Shadow::Shadow(const TestSample::Shadow& other) { |
| 56 | type = other.type; |
| 57 | flag_fields = other.flag_fields; |
| 58 | string_fields = other.string_fields; |
nzolghadr | 3fddfc5 | 2016-12-13 21:52:12 | [diff] [blame] | 59 | uint64_fields = other.uint64_fields; |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | TestSample::Shadow::~Shadow() {} |
| 63 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 64 | TestRapporServiceImpl::TestRapporServiceImpl() |
| 65 | : RapporServiceImpl(&test_prefs_, |
| 66 | base::Bind(&MockIsIncognito, &is_incognito_)), |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 67 | next_rotation_(base::TimeDelta()), |
| 68 | is_incognito_(false) { |
| 69 | RegisterPrefs(test_prefs_.registry()); |
| 70 | test_uploader_ = new TestLogUploader(); |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 71 | InitializeInternal(base::WrapUnique(test_uploader_), 0, |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 72 | HmacByteVectorGenerator::GenerateEntropyInput()); |
holte | 0036d9b | 2017-03-15 21:49:16 | [diff] [blame] | 73 | Update(true, true); |
holte | ca6486b | 2014-11-06 02:30:07 | [diff] [blame] | 74 | } |
| 75 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 76 | TestRapporServiceImpl::~TestRapporServiceImpl() {} |
holte | ca6486b | 2014-11-06 02:30:07 | [diff] [blame] | 77 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 78 | std::unique_ptr<Sample> TestRapporServiceImpl::CreateSample(RapporType type) { |
dcheng | 82beb4f | 2016-04-26 00:35:02 | [diff] [blame] | 79 | std::unique_ptr<TestSample> test_sample(new TestSample(type)); |
dcheng | 51ace48a | 2015-12-26 22:45:17 | [diff] [blame] | 80 | return std::move(test_sample); |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // Intercepts the sample being recorded and saves it in a test structure. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 84 | void TestRapporServiceImpl::RecordSample(const std::string& metric_name, |
| 85 | std::unique_ptr<Sample> sample) { |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 86 | TestSample* test_sample = static_cast<TestSample*>(sample.get()); |
| 87 | // Erase the previous sample if we logged one. |
| 88 | shadows_.erase(metric_name); |
| 89 | shadows_.insert(std::pair<std::string, TestSample::Shadow>( |
| 90 | metric_name, test_sample->GetShadow())); |
| 91 | // Original version is still called. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 92 | RapporServiceImpl::RecordSample(metric_name, std::move(sample)); |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 93 | } |
| 94 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 95 | void TestRapporServiceImpl::RecordSampleString(const std::string& metric_name, |
| 96 | RapporType type, |
| 97 | const std::string& sample) { |
mathp | 7deb9df | 2015-03-13 14:44:16 | [diff] [blame] | 98 | // Save the recorded sample to the local structure. |
| 99 | RapporSample rappor_sample; |
| 100 | rappor_sample.type = type; |
| 101 | rappor_sample.value = sample; |
| 102 | samples_[metric_name] = rappor_sample; |
| 103 | // Original version is still called. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 104 | RapporServiceImpl::RecordSampleString(metric_name, type, sample); |
mathp | 7deb9df | 2015-03-13 14:44:16 | [diff] [blame] | 105 | } |
| 106 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 107 | int TestRapporServiceImpl::GetReportsCount() { |
holte | ca6486b | 2014-11-06 02:30:07 | [diff] [blame] | 108 | RapporReports reports; |
| 109 | ExportMetrics(&reports); |
| 110 | return reports.report_size(); |
| 111 | } |
| 112 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 113 | void TestRapporServiceImpl::GetReports(RapporReports* reports) { |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 114 | ExportMetrics(reports); |
| 115 | } |
| 116 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 117 | TestSample::Shadow* TestRapporServiceImpl::GetRecordedSampleForMetric( |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 118 | const std::string& metric_name) { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 119 | auto it = shadows_.find(metric_name); |
csharrison | b50320c3 | 2015-11-02 15:59:57 | [diff] [blame] | 120 | if (it == shadows_.end()) |
| 121 | return nullptr; |
| 122 | return &it->second; |
| 123 | } |
| 124 | |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 125 | bool TestRapporServiceImpl::GetRecordedSampleForMetric( |
mathp | 7deb9df | 2015-03-13 14:44:16 | [diff] [blame] | 126 | const std::string& metric_name, |
| 127 | std::string* sample, |
| 128 | RapporType* type) { |
jdoerrie | 3feb185 | 2018-10-05 12:16:44 | [diff] [blame] | 129 | auto it = samples_.find(metric_name); |
mathp | 7deb9df | 2015-03-13 14:44:16 | [diff] [blame] | 130 | if (it == samples_.end()) |
| 131 | return false; |
| 132 | *sample = it->second.value; |
| 133 | *type = it->second.type; |
| 134 | return true; |
| 135 | } |
| 136 | |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 137 | // Cancel the next call to OnLogInterval. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 138 | void TestRapporServiceImpl::CancelNextLogRotation() { |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 139 | next_rotation_ = base::TimeDelta(); |
| 140 | } |
| 141 | |
| 142 | // Schedule the next call to OnLogInterval. |
nzolghadr | d87a308d | 2016-12-07 15:45:56 | [diff] [blame] | 143 | void TestRapporServiceImpl::ScheduleNextLogRotation(base::TimeDelta interval) { |
holte | 5a7ed7c | 2015-01-09 23:52:46 | [diff] [blame] | 144 | next_rotation_ = interval; |
| 145 | } |
| 146 | |
holte | ca6486b | 2014-11-06 02:30:07 | [diff] [blame] | 147 | } // namespace rappor |