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