OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/common/metrics/caching_permuted_entropy_provider.h" | 5 #include "components/variations/caching_permuted_entropy_provider.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/prefs/pref_registry_simple.h" | 11 #include "base/prefs/pref_registry_simple.h" |
12 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "components/variations/pref_names.h" |
14 | 14 |
15 namespace metrics { | 15 namespace metrics { |
16 | 16 |
17 CachingPermutedEntropyProvider::CachingPermutedEntropyProvider( | 17 CachingPermutedEntropyProvider::CachingPermutedEntropyProvider( |
18 PrefService* local_state, | 18 PrefService* local_state, |
19 uint16 low_entropy_source, | 19 uint16 low_entropy_source, |
20 size_t low_entropy_source_max) | 20 size_t low_entropy_source_max) |
21 : PermutedEntropyProvider(low_entropy_source, low_entropy_source_max), | 21 : PermutedEntropyProvider(low_entropy_source, low_entropy_source_max), |
22 local_state_(local_state) { | 22 local_state_(local_state) { |
23 ReadFromLocalState(); | 23 ReadFromLocalState(); |
24 } | 24 } |
25 | 25 |
26 CachingPermutedEntropyProvider::~CachingPermutedEntropyProvider() { | 26 CachingPermutedEntropyProvider::~CachingPermutedEntropyProvider() { |
27 } | 27 } |
28 | 28 |
29 // static | 29 // static |
30 void CachingPermutedEntropyProvider::RegisterPrefs( | 30 void CachingPermutedEntropyProvider::RegisterPrefs( |
31 PrefRegistrySimple* registry) { | 31 PrefRegistrySimple* registry) { |
32 registry->RegisterStringPref(prefs::kMetricsPermutedEntropyCache, | 32 registry->RegisterStringPref(prefs::kVariationsPermutedEntropyCache, |
33 std::string()); | 33 std::string()); |
34 } | 34 } |
35 | 35 |
36 // static | 36 // static |
37 void CachingPermutedEntropyProvider::ClearCache(PrefService* local_state) { | 37 void CachingPermutedEntropyProvider::ClearCache(PrefService* local_state) { |
38 local_state->ClearPref(prefs::kMetricsPermutedEntropyCache); | 38 local_state->ClearPref(prefs::kVariationsPermutedEntropyCache); |
39 } | 39 } |
40 | 40 |
41 uint16 CachingPermutedEntropyProvider::GetPermutedValue( | 41 uint16 CachingPermutedEntropyProvider::GetPermutedValue( |
42 uint32 randomization_seed) const { | 42 uint32 randomization_seed) const { |
43 DCHECK(thread_checker_.CalledOnValidThread()); | 43 DCHECK(thread_checker_.CalledOnValidThread()); |
44 | 44 |
45 uint16 value = 0; | 45 uint16 value = 0; |
46 if (!FindValue(randomization_seed, &value)) { | 46 if (!FindValue(randomization_seed, &value)) { |
47 value = PermutedEntropyProvider::GetPermutedValue(randomization_seed); | 47 value = PermutedEntropyProvider::GetPermutedValue(randomization_seed); |
48 AddToCache(randomization_seed, value); | 48 AddToCache(randomization_seed, value); |
49 } | 49 } |
50 return value; | 50 return value; |
51 } | 51 } |
52 | 52 |
53 void CachingPermutedEntropyProvider::ReadFromLocalState() const { | 53 void CachingPermutedEntropyProvider::ReadFromLocalState() const { |
54 const std::string base64_cache_data = | 54 const std::string base64_cache_data = |
55 local_state_->GetString(prefs::kMetricsPermutedEntropyCache); | 55 local_state_->GetString(prefs::kVariationsPermutedEntropyCache); |
56 std::string cache_data; | 56 std::string cache_data; |
57 if (!base::Base64Decode(base64_cache_data, &cache_data) || | 57 if (!base::Base64Decode(base64_cache_data, &cache_data) || |
58 !cache_.ParseFromString(cache_data)) { | 58 !cache_.ParseFromString(cache_data)) { |
59 local_state_->ClearPref(prefs::kMetricsPermutedEntropyCache); | 59 local_state_->ClearPref(prefs::kVariationsPermutedEntropyCache); |
60 NOTREACHED(); | 60 NOTREACHED(); |
61 } | 61 } |
62 } | 62 } |
63 | 63 |
64 void CachingPermutedEntropyProvider::UpdateLocalState() const { | 64 void CachingPermutedEntropyProvider::UpdateLocalState() const { |
65 std::string serialized; | 65 std::string serialized; |
66 cache_.SerializeToString(&serialized); | 66 cache_.SerializeToString(&serialized); |
67 | 67 |
68 std::string base64_encoded; | 68 std::string base64_encoded; |
69 base::Base64Encode(serialized, &base64_encoded); | 69 base::Base64Encode(serialized, &base64_encoded); |
70 local_state_->SetString(prefs::kMetricsPermutedEntropyCache, base64_encoded); | 70 local_state_->SetString(prefs::kVariationsPermutedEntropyCache, |
| 71 base64_encoded); |
71 } | 72 } |
72 | 73 |
73 void CachingPermutedEntropyProvider::AddToCache(uint32 randomization_seed, | 74 void CachingPermutedEntropyProvider::AddToCache(uint32 randomization_seed, |
74 uint16 value) const { | 75 uint16 value) const { |
75 PermutedEntropyCache::Entry* entry; | 76 PermutedEntropyCache::Entry* entry; |
76 const int kMaxSize = 25; | 77 const int kMaxSize = 25; |
77 if (cache_.entry_size() >= kMaxSize) { | 78 if (cache_.entry_size() >= kMaxSize) { |
78 // If the cache is full, evict the first entry, swapping later entries in | 79 // If the cache is full, evict the first entry, swapping later entries in |
79 // to take its place. This effectively creates a FIFO cache, which is good | 80 // to take its place. This effectively creates a FIFO cache, which is good |
80 // enough here because the expectation is that there shouldn't be more than | 81 // enough here because the expectation is that there shouldn't be more than |
(...skipping 17 matching lines...) Expand all Loading... |
98 for (int i = 0; i < cache_.entry_size(); ++i) { | 99 for (int i = 0; i < cache_.entry_size(); ++i) { |
99 if (cache_.entry(i).randomization_seed() == randomization_seed) { | 100 if (cache_.entry(i).randomization_seed() == randomization_seed) { |
100 *value = cache_.entry(i).value(); | 101 *value = cache_.entry(i).value(); |
101 return true; | 102 return true; |
102 } | 103 } |
103 } | 104 } |
104 return false; | 105 return false; |
105 } | 106 } |
106 | 107 |
107 } // namespace metrics | 108 } // namespace metrics |
OLD | NEW |