cc: Remove unused disable_hi_res_timer_tasks_on_battery flag.
[chromium-blink-merge.git] / components / variations / entropy_provider.h
blob0fff0b032c3c7c75359d6578423d31939f39a8d6
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.
5 #ifndef COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_
6 #define COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_
8 #include <functional>
9 #include <string>
10 #include <vector>
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"
17 namespace metrics {
19 // Internals of entropy_provider.cc exposed for testing.
20 namespace internal {
22 // A functor that generates random numbers based on a seed, using the Mersenne
23 // Twister algorithm. Suitable for use with std::random_shuffle().
24 struct SeededRandGenerator : std::unary_function<uint32, uint32> {
25 explicit SeededRandGenerator(uint32 seed);
26 ~SeededRandGenerator();
28 // Returns a random number in range [0, range).
29 uint32 operator()(uint32 range);
31 MersenneTwister mersenne_twister_;
34 // Fills |mapping| to create a bijection of values in the range of
35 // [0, |mapping.size()|), permuted based on |randomization_seed|.
36 void PermuteMappingUsingRandomizationSeed(uint32 randomization_seed,
37 std::vector<uint16>* mapping);
39 } // namespace internal
41 // SHA1EntropyProvider is an entropy provider suitable for high entropy
42 // sources. It works by taking the first 64 bits of the SHA1 hash of the
43 // entropy source concatenated with the trial name and using that for the
44 // final entropy value.
45 class SHA1EntropyProvider : public base::FieldTrial::EntropyProvider {
46 public:
47 // Creates a SHA1EntropyProvider with the given |entropy_source|, which
48 // should contain a large amount of entropy - for example, a textual
49 // representation of a persistent randomly-generated 128-bit value.
50 explicit SHA1EntropyProvider(const std::string& entropy_source);
51 ~SHA1EntropyProvider() override;
53 // base::FieldTrial::EntropyProvider implementation:
54 double GetEntropyForTrial(const std::string& trial_name,
55 uint32 randomization_seed) const override;
57 private:
58 std::string entropy_source_;
60 DISALLOW_COPY_AND_ASSIGN(SHA1EntropyProvider);
63 // PermutedEntropyProvider is an entropy provider suitable for low entropy
64 // sources (below 16 bits). It uses the field trial name to generate a
65 // permutation of a mapping array from an initial entropy value to a new value.
66 // Note: This provider's performance is O(2^n), where n is the number of bits
67 // in the entropy source.
68 class PermutedEntropyProvider : public base::FieldTrial::EntropyProvider {
69 public:
70 // Creates a PermutedEntropyProvider with the given |low_entropy_source|,
71 // which should have a value in the range of [0, low_entropy_source_max).
72 PermutedEntropyProvider(uint16 low_entropy_source,
73 size_t low_entropy_source_max);
74 ~PermutedEntropyProvider() override;
76 // base::FieldTrial::EntropyProvider implementation:
77 double GetEntropyForTrial(const std::string& trial_name,
78 uint32 randomization_seed) const override;
80 protected:
81 // Performs the permutation algorithm and returns the permuted value that
82 // corresponds to |low_entropy_source_|.
83 virtual uint16 GetPermutedValue(uint32 randomization_seed) const;
85 private:
86 uint16 low_entropy_source_;
87 size_t low_entropy_source_max_;
89 DISALLOW_COPY_AND_ASSIGN(PermutedEntropyProvider);
92 } // namespace metrics
94 #endif // COMPONENTS_VARIATIONS_ENTROPY_PROVIDER_H_