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/rappor_metric.h" | 5 #include "components/rappor/rappor_metric.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/rand_util.h" | |
8 | 9 |
9 namespace rappor { | 10 namespace rappor { |
10 | 11 |
11 RapporMetric::RapporMetric(const std::string& metric_name, | 12 RapporMetric::RapporMetric(const std::string& metric_name, |
12 const RapporParameters& parameters, | 13 const RapporParameters& parameters, |
13 int32_t cohort_seed) | 14 int32_t cohort_seed) |
14 : metric_name_(metric_name), | 15 : metric_name_(metric_name), |
15 parameters_(parameters), | 16 parameters_(parameters), |
Alexei Svitkine (slow)
2014/07/30 17:12:47
Init sample_count_ here.
Steven Holte
2014/08/04 23:30:26
Done.
| |
16 bloom_filter_(parameters.bloom_filter_size_bytes, | 17 bloom_filter_(parameters.bloom_filter_size_bytes, |
17 parameters.bloom_filter_hash_function_count, | 18 parameters.bloom_filter_hash_function_count, |
18 (cohort_seed % parameters.num_cohorts) * | 19 (cohort_seed % parameters.num_cohorts) * |
19 parameters.bloom_filter_hash_function_count) { | 20 parameters.bloom_filter_hash_function_count) { |
20 DCHECK_GE(cohort_seed, 0); | 21 DCHECK_GE(cohort_seed, 0); |
21 DCHECK_LT(cohort_seed, RapporParameters::kMaxCohorts); | 22 DCHECK_LT(cohort_seed, RapporParameters::kMaxCohorts); |
22 } | 23 } |
23 | 24 |
24 RapporMetric::~RapporMetric() {} | 25 RapporMetric::~RapporMetric() {} |
25 | 26 |
26 void RapporMetric::AddSample(const std::string& str) { | 27 void RapporMetric::AddSample(const std::string& str) { |
27 bloom_filter_.AddString(str); | 28 ++sample_count; |
Alexei Svitkine (slow)
2014/07/30 17:12:47
Should be sample_count_, not sample_count.
Steven Holte
2014/08/04 23:30:26
Done.
| |
29 // Replace the previous sample with a 1 in sample_count chance so that each | |
Alexei Svitkine (slow)
2014/07/30 17:12:47
Nit: sample_count_.
Steven Holte
2014/08/04 23:30:26
Done.
| |
30 // sample has equal probability of being reported. | |
31 if (base::RandGenerator(sample_count) == 0) { | |
32 bloom_filter_.Clear(); | |
33 bloom_filter_.AddString(str); | |
Alexei Svitkine (slow)
2014/07/30 17:12:47
Any reason why we can't just change AddString() to
Steven Holte
2014/08/04 23:30:26
Done.
| |
34 } | |
28 } | 35 } |
29 | 36 |
30 ByteVector RapporMetric::GetReport(const std::string& secret) const { | 37 ByteVector RapporMetric::GetReport(const std::string& secret) const { |
31 // Generate a deterministically random mask of fake data using the | 38 // Generate a deterministically random mask of fake data using the |
32 // client's secret key + real data as a seed. The inclusion of the secret | 39 // client's secret key + real data as a seed. The inclusion of the secret |
33 // in the seed avoids correlations between real and fake data. | 40 // in the seed avoids correlations between real and fake data. |
34 // The seed isn't a human-readable string. | 41 // The seed isn't a human-readable string. |
35 const std::string personalization_string = metric_name_ + | 42 const std::string personalization_string = metric_name_ + |
36 std::string(bytes().begin(), bytes().end()); | 43 std::string(bytes().begin(), bytes().end()); |
37 HmacByteVectorGenerator hmac_generator(bytes().size(), secret, | 44 HmacByteVectorGenerator hmac_generator(bytes().size(), secret, |
(...skipping 14 matching lines...) Expand all Loading... | |
52 coin_generator.GetWeightedRandomByteVector(parameters().zero_coin_prob); | 59 coin_generator.GetWeightedRandomByteVector(parameters().zero_coin_prob); |
53 ByteVector one_coins = | 60 ByteVector one_coins = |
54 coin_generator.GetWeightedRandomByteVector(parameters().one_coin_prob); | 61 coin_generator.GetWeightedRandomByteVector(parameters().one_coin_prob); |
55 | 62 |
56 // Create a randomized response report on the fake and redacted data, sending | 63 // Create a randomized response report on the fake and redacted data, sending |
57 // the outcome of flipping a zero coin for the zero bits in that data, and of | 64 // the outcome of flipping a zero coin for the zero bits in that data, and of |
58 // flipping a one coin for the one bits in that data, as the final report. | 65 // flipping a one coin for the one bits in that data, as the final report. |
59 return *ByteVectorMerge(*fake_and_redacted_bits, zero_coins, &one_coins); | 66 return *ByteVectorMerge(*fake_and_redacted_bits, zero_coins, &one_coins); |
60 } | 67 } |
61 | 68 |
69 void RapporMetric::SetBytesForTesting(const ByteVector& bytes) { | |
70 bloom_filter_.SetBytesForTesting(bytes); | |
71 } | |
72 | |
62 } // namespace rappor | 73 } // namespace rappor |
OLD | NEW |