OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 CHROME_COMMON_METRICS_ENTROPY_PROVIDER_H_ |
| 6 #define CHROME_COMMON_METRICS_ENTROPY_PROVIDER_H_ |
| 7 |
| 8 #include <functional> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/base_export.h" |
| 13 #include "base/basictypes.h" |
| 14 #include "base/compiler_specific.h" |
| 15 #include "base/metrics/field_trial.h" |
| 16 #include "third_party/mt19937ar/mt19937ar.h" |
| 17 |
| 18 namespace metrics { |
| 19 |
| 20 // Internals of entropy_provider.cc exposed for testing. |
| 21 namespace internal { |
| 22 |
| 23 // A functor that generates random numbers based on a seed, using the Mersenne |
| 24 // Twister algorithm. Suitable for use with std::random_shuffle(). |
| 25 struct SeededRandGenerator : std::unary_function<uint32, uint32> { |
| 26 explicit SeededRandGenerator(uint32 seed); |
| 27 ~SeededRandGenerator(); |
| 28 |
| 29 // Returns a random number in range [0, range). |
| 30 uint32 operator()(uint32 range); |
| 31 |
| 32 MersenneTwister mersenne_twister_; |
| 33 }; |
| 34 |
| 35 // Creates unique identifier for the trial by hashing a name string, whether |
| 36 // it's for the field trial or the group name. |
| 37 // TODO(asvitkine): Share the implementation with variations_util.cc. |
| 38 uint32 HashName(const std::string& name); |
| 39 |
| 40 // Fills |mapping| to create a bijection of values in the range of |
| 41 // [0, |mapping.size()|), permuted based on |trial_name|. |
| 42 void PermuteMappingUsingTrialName(const std::string& trial_name, |
| 43 std::vector<uint16>* mapping); |
| 44 |
| 45 } // namespace internal |
| 46 |
| 47 // SHA1EntropyProvider is an entropy provider suitable for high entropy |
| 48 // sources. It works by taking the first 64 bits of the SHA1 hash of the |
| 49 // entropy source concatenated with the trial name and using that for the |
| 50 // final entropy value. |
| 51 class SHA1EntropyProvider : public base::FieldTrial::EntropyProvider { |
| 52 public: |
| 53 // Creates a SHA1EntropyProvider with the given |entropy_source|, which |
| 54 // should contain a large amount of entropy - for example, a textual |
| 55 // representation of a persistent randomly-generated 128-bit value. |
| 56 explicit SHA1EntropyProvider(const std::string& entropy_source); |
| 57 virtual ~SHA1EntropyProvider(); |
| 58 |
| 59 // base::FieldTrial::EntropyProvider implementation: |
| 60 virtual double GetEntropyForTrial(const std::string& trial_name) const |
| 61 OVERRIDE; |
| 62 |
| 63 private: |
| 64 std::string entropy_source_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(SHA1EntropyProvider); |
| 67 }; |
| 68 |
| 69 // PermutedEntropyProvider is an entropy provider suitable for low entropy |
| 70 // sources (below 16 bits). It uses the field trial name to generate a |
| 71 // permutation of a mapping array from an initial entropy value to a new value. |
| 72 // Note: This provider's performance is O(2^n), where n is the number of bits |
| 73 // in the entropy source. |
| 74 class PermutedEntropyProvider : public base::FieldTrial::EntropyProvider { |
| 75 public: |
| 76 // Creates a PermutedEntropyProvider with the given |low_entropy_source|, |
| 77 // which should have a value in the range of [0, low_entropy_source_max). |
| 78 PermutedEntropyProvider(uint16 low_entropy_source, |
| 79 size_t low_entropy_source_max); |
| 80 virtual ~PermutedEntropyProvider(); |
| 81 |
| 82 // base::FieldTrial::EntropyProvider implementation: |
| 83 virtual double GetEntropyForTrial(const std::string& trial_name) const |
| 84 OVERRIDE; |
| 85 |
| 86 private: |
| 87 uint16 low_entropy_source_; |
| 88 size_t low_entropy_source_max_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(PermutedEntropyProvider); |
| 91 }; |
| 92 |
| 93 } // namespace metrics |
| 94 |
| 95 #endif // CHROME_COMMON_METRICS_ENTROPY_PROVIDER_H_ |
OLD | NEW |