OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/rappor/test_rappor_service.h" | 5 #include "components/rappor/test_rappor_service.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "components/rappor/byte_vector_utils.h" | 11 #include "components/rappor/byte_vector_utils.h" |
12 #include "components/rappor/proto/rappor_metric.pb.h" | 12 #include "components/rappor/proto/rappor_metric.pb.h" |
13 #include "components/rappor/rappor_parameters.h" | 13 #include "components/rappor/public/rappor_parameters.h" |
14 #include "components/rappor/rappor_prefs.h" | 14 #include "components/rappor/rappor_prefs.h" |
15 #include "components/rappor/test_log_uploader.h" | 15 #include "components/rappor/test_log_uploader.h" |
16 | 16 |
17 namespace rappor { | 17 namespace rappor { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 bool MockIsIncognito(bool* is_incognito) { | 21 bool MockIsIncognito(bool* is_incognito) { |
22 return *is_incognito; | 22 return *is_incognito; |
23 } | 23 } |
(...skipping 21 matching lines...) Expand all Loading... |
45 TestSample::Shadow::Shadow(RapporType type) : type(type) {} | 45 TestSample::Shadow::Shadow(RapporType type) : type(type) {} |
46 | 46 |
47 TestSample::Shadow::Shadow(const TestSample::Shadow& other) { | 47 TestSample::Shadow::Shadow(const TestSample::Shadow& other) { |
48 type = other.type; | 48 type = other.type; |
49 flag_fields = other.flag_fields; | 49 flag_fields = other.flag_fields; |
50 string_fields = other.string_fields; | 50 string_fields = other.string_fields; |
51 } | 51 } |
52 | 52 |
53 TestSample::Shadow::~Shadow() {} | 53 TestSample::Shadow::~Shadow() {} |
54 | 54 |
55 TestRapporService::TestRapporService() | 55 TestRapporServiceImpl::TestRapporServiceImpl() |
56 : RapporService(&test_prefs_, base::Bind(&MockIsIncognito, &is_incognito_)), | 56 : RapporServiceImpl(&test_prefs_, |
| 57 base::Bind(&MockIsIncognito, &is_incognito_)), |
57 next_rotation_(base::TimeDelta()), | 58 next_rotation_(base::TimeDelta()), |
58 is_incognito_(false) { | 59 is_incognito_(false) { |
59 RegisterPrefs(test_prefs_.registry()); | 60 RegisterPrefs(test_prefs_.registry()); |
60 test_uploader_ = new TestLogUploader(); | 61 test_uploader_ = new TestLogUploader(); |
61 InitializeInternal(base::WrapUnique(test_uploader_), 0, | 62 InitializeInternal(base::WrapUnique(test_uploader_), 0, |
62 HmacByteVectorGenerator::GenerateEntropyInput()); | 63 HmacByteVectorGenerator::GenerateEntropyInput()); |
63 Update(UMA_RAPPOR_GROUP | SAFEBROWSING_RAPPOR_GROUP, true); | 64 Update(UMA_RAPPOR_GROUP | SAFEBROWSING_RAPPOR_GROUP, true); |
64 } | 65 } |
65 | 66 |
66 TestRapporService::~TestRapporService() {} | 67 TestRapporServiceImpl::~TestRapporServiceImpl() {} |
67 | 68 |
68 std::unique_ptr<Sample> TestRapporService::CreateSample(RapporType type) { | 69 std::unique_ptr<Sample> TestRapporServiceImpl::CreateSample(RapporType type) { |
69 std::unique_ptr<TestSample> test_sample(new TestSample(type)); | 70 std::unique_ptr<TestSample> test_sample(new TestSample(type)); |
70 return std::move(test_sample); | 71 return std::move(test_sample); |
71 } | 72 } |
72 | 73 |
73 // Intercepts the sample being recorded and saves it in a test structure. | 74 // Intercepts the sample being recorded and saves it in a test structure. |
74 void TestRapporService::RecordSampleObj(const std::string& metric_name, | 75 void TestRapporServiceImpl::RecordSample(const std::string& metric_name, |
75 std::unique_ptr<Sample> sample) { | 76 std::unique_ptr<Sample> sample) { |
76 TestSample* test_sample = static_cast<TestSample*>(sample.get()); | 77 TestSample* test_sample = static_cast<TestSample*>(sample.get()); |
77 // Erase the previous sample if we logged one. | 78 // Erase the previous sample if we logged one. |
78 shadows_.erase(metric_name); | 79 shadows_.erase(metric_name); |
79 shadows_.insert(std::pair<std::string, TestSample::Shadow>( | 80 shadows_.insert(std::pair<std::string, TestSample::Shadow>( |
80 metric_name, test_sample->GetShadow())); | 81 metric_name, test_sample->GetShadow())); |
81 // Original version is still called. | 82 // Original version is still called. |
82 RapporService::RecordSampleObj(metric_name, std::move(sample)); | 83 RapporServiceImpl::RecordSample(metric_name, std::move(sample)); |
83 } | 84 } |
84 | 85 |
85 void TestRapporService::RecordSample(const std::string& metric_name, | 86 void TestRapporServiceImpl::RecordSampleString(const std::string& metric_name, |
86 RapporType type, | 87 RapporType type, |
87 const std::string& sample) { | 88 const std::string& sample) { |
88 // Save the recorded sample to the local structure. | 89 // Save the recorded sample to the local structure. |
89 RapporSample rappor_sample; | 90 RapporSample rappor_sample; |
90 rappor_sample.type = type; | 91 rappor_sample.type = type; |
91 rappor_sample.value = sample; | 92 rappor_sample.value = sample; |
92 samples_[metric_name] = rappor_sample; | 93 samples_[metric_name] = rappor_sample; |
93 // Original version is still called. | 94 // Original version is still called. |
94 RapporService::RecordSample(metric_name, type, sample); | 95 RapporServiceImpl::RecordSampleString(metric_name, type, sample); |
95 } | 96 } |
96 | 97 |
97 int TestRapporService::GetReportsCount() { | 98 int TestRapporServiceImpl::GetReportsCount() { |
98 RapporReports reports; | 99 RapporReports reports; |
99 ExportMetrics(&reports); | 100 ExportMetrics(&reports); |
100 return reports.report_size(); | 101 return reports.report_size(); |
101 } | 102 } |
102 | 103 |
103 void TestRapporService::GetReports(RapporReports* reports) { | 104 void TestRapporServiceImpl::GetReports(RapporReports* reports) { |
104 ExportMetrics(reports); | 105 ExportMetrics(reports); |
105 } | 106 } |
106 | 107 |
107 TestSample::Shadow* TestRapporService::GetRecordedSampleForMetric( | 108 TestSample::Shadow* TestRapporServiceImpl::GetRecordedSampleForMetric( |
108 const std::string& metric_name) { | 109 const std::string& metric_name) { |
109 ShadowMap::iterator it = shadows_.find(metric_name); | 110 ShadowMap::iterator it = shadows_.find(metric_name); |
110 if (it == shadows_.end()) | 111 if (it == shadows_.end()) |
111 return nullptr; | 112 return nullptr; |
112 return &it->second; | 113 return &it->second; |
113 } | 114 } |
114 | 115 |
115 bool TestRapporService::GetRecordedSampleForMetric( | 116 bool TestRapporServiceImpl::GetRecordedSampleForMetric( |
116 const std::string& metric_name, | 117 const std::string& metric_name, |
117 std::string* sample, | 118 std::string* sample, |
118 RapporType* type) { | 119 RapporType* type) { |
119 SamplesMap::iterator it = samples_.find(metric_name); | 120 SamplesMap::iterator it = samples_.find(metric_name); |
120 if (it == samples_.end()) | 121 if (it == samples_.end()) |
121 return false; | 122 return false; |
122 *sample = it->second.value; | 123 *sample = it->second.value; |
123 *type = it->second.type; | 124 *type = it->second.type; |
124 return true; | 125 return true; |
125 } | 126 } |
126 | 127 |
127 // Cancel the next call to OnLogInterval. | 128 // Cancel the next call to OnLogInterval. |
128 void TestRapporService::CancelNextLogRotation() { | 129 void TestRapporServiceImpl::CancelNextLogRotation() { |
129 next_rotation_ = base::TimeDelta(); | 130 next_rotation_ = base::TimeDelta(); |
130 } | 131 } |
131 | 132 |
132 // Schedule the next call to OnLogInterval. | 133 // Schedule the next call to OnLogInterval. |
133 void TestRapporService::ScheduleNextLogRotation(base::TimeDelta interval) { | 134 void TestRapporServiceImpl::ScheduleNextLogRotation(base::TimeDelta interval) { |
134 next_rotation_ = interval; | 135 next_rotation_ = interval; |
135 } | 136 } |
136 | 137 |
137 } // namespace rappor | 138 } // namespace rappor |
OLD | NEW |