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.
5 #include "components/rappor/rappor_metric.h"
7 #include "base/logging.h"
8 #include "base/rand_util.h"
9 #include "components/rappor/reports.h"
13 RapporMetric::RapporMetric(const std::string
& metric_name
,
14 const RapporParameters
& parameters
,
16 : metric_name_(metric_name
),
17 parameters_(parameters
),
19 bloom_filter_(parameters
.bloom_filter_size_bytes
,
20 parameters
.bloom_filter_hash_function_count
,
21 (cohort_seed
% parameters
.num_cohorts
) *
22 parameters
.bloom_filter_hash_function_count
) {
23 DCHECK_GE(cohort_seed
, 0);
24 DCHECK_LT(cohort_seed
, RapporParameters::kMaxCohorts
);
25 // Since cohort_seed is in the range [0, kMaxCohorts), num_cohorts should
26 // divide kMaxCohorts for each cohort to have equal weight.
27 DCHECK_EQ(0, RapporParameters::kMaxCohorts
% parameters
.num_cohorts
);
30 RapporMetric::~RapporMetric() {}
32 void RapporMetric::AddSample(const std::string
& str
) {
34 // Replace the previous sample with a 1 in sample_count_ chance so that each
35 // sample has equal probability of being reported.
36 if (base::RandGenerator(sample_count_
) == 0) {
37 bloom_filter_
.SetString(str
);
41 ByteVector
RapporMetric::GetReport(const std::string
& secret
) const {
42 return internal::GenerateReport(secret
, parameters(), bytes());
45 void RapporMetric::SetBytesForTesting(const ByteVector
& bytes
) {
46 bloom_filter_
.SetBytesForTesting(bytes
);