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/metrics/metrics_state_manager.h" | 5 #include "components/metrics/metrics_state_manager.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/guid.h" | 10 #include "base/guid.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/metrics/sparse_histogram.h" | 12 #include "base/metrics/sparse_histogram.h" |
13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "components/metrics/cloned_install_detector.h" | 18 #include "components/metrics/cloned_install_detector.h" |
| 19 #include "components/metrics/enabled_state_provider.h" |
19 #include "components/metrics/machine_id_provider.h" | 20 #include "components/metrics/machine_id_provider.h" |
20 #include "components/metrics/metrics_pref_names.h" | 21 #include "components/metrics/metrics_pref_names.h" |
21 #include "components/metrics/metrics_switches.h" | 22 #include "components/metrics/metrics_switches.h" |
22 #include "components/prefs/pref_registry_simple.h" | 23 #include "components/prefs/pref_registry_simple.h" |
23 #include "components/prefs/pref_service.h" | 24 #include "components/prefs/pref_service.h" |
24 #include "components/variations/caching_permuted_entropy_provider.h" | 25 #include "components/variations/caching_permuted_entropy_provider.h" |
25 | 26 |
26 namespace metrics { | 27 namespace metrics { |
27 | 28 |
28 namespace { | 29 namespace { |
(...skipping 13 matching lines...) Expand all Loading... |
42 return base::RandInt(0, kMaxLowEntropySize - 1); | 43 return base::RandInt(0, kMaxLowEntropySize - 1); |
43 } | 44 } |
44 | 45 |
45 } // namespace | 46 } // namespace |
46 | 47 |
47 // static | 48 // static |
48 bool MetricsStateManager::instance_exists_ = false; | 49 bool MetricsStateManager::instance_exists_ = false; |
49 | 50 |
50 MetricsStateManager::MetricsStateManager( | 51 MetricsStateManager::MetricsStateManager( |
51 PrefService* local_state, | 52 PrefService* local_state, |
52 const base::Callback<bool(void)>& is_reporting_enabled_callback, | 53 EnabledStateProvider* enabled_state_provider, |
53 const StoreClientInfoCallback& store_client_info, | 54 const StoreClientInfoCallback& store_client_info, |
54 const LoadClientInfoCallback& retrieve_client_info) | 55 const LoadClientInfoCallback& retrieve_client_info) |
55 : local_state_(local_state), | 56 : local_state_(local_state), |
56 is_reporting_enabled_callback_(is_reporting_enabled_callback), | 57 enabled_state_provider_(enabled_state_provider), |
57 store_client_info_(store_client_info), | 58 store_client_info_(store_client_info), |
58 load_client_info_(retrieve_client_info), | 59 load_client_info_(retrieve_client_info), |
59 low_entropy_source_(kLowEntropySourceNotSet), | 60 low_entropy_source_(kLowEntropySourceNotSet), |
60 entropy_source_returned_(ENTROPY_SOURCE_NONE) { | 61 entropy_source_returned_(ENTROPY_SOURCE_NONE) { |
61 ResetMetricsIDsIfNecessary(); | 62 ResetMetricsIDsIfNecessary(); |
62 if (IsMetricsReportingEnabled()) | 63 if (enabled_state_provider_->IsConsentGiven()) |
63 ForceClientIdCreation(); | 64 ForceClientIdCreation(); |
64 | 65 |
65 DCHECK(!instance_exists_); | 66 DCHECK(!instance_exists_); |
66 instance_exists_ = true; | 67 instance_exists_ = true; |
67 } | 68 } |
68 | 69 |
69 MetricsStateManager::~MetricsStateManager() { | 70 MetricsStateManager::~MetricsStateManager() { |
70 DCHECK(instance_exists_); | 71 DCHECK(instance_exists_); |
71 instance_exists_ = false; | 72 instance_exists_ = false; |
72 } | 73 } |
73 | 74 |
74 bool MetricsStateManager::IsMetricsReportingEnabled() { | 75 bool MetricsStateManager::IsMetricsReportingEnabled() { |
75 return is_reporting_enabled_callback_.Run(); | 76 return enabled_state_provider_->IsReportingEnabled(); |
76 } | 77 } |
77 | 78 |
78 void MetricsStateManager::ForceClientIdCreation() { | 79 void MetricsStateManager::ForceClientIdCreation() { |
79 if (!client_id_.empty()) | 80 if (!client_id_.empty()) |
80 return; | 81 return; |
81 | 82 |
82 client_id_ = local_state_->GetString(prefs::kMetricsClientID); | 83 client_id_ = local_state_->GetString(prefs::kMetricsClientID); |
83 if (!client_id_.empty()) { | 84 if (!client_id_.empty()) { |
84 // It is technically sufficient to only save a backup of the client id when | 85 // It is technically sufficient to only save a backup of the client id when |
85 // it is initially generated below, but since the backup was only introduced | 86 // it is initially generated below, but since the backup was only introduced |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 // For metrics reporting-enabled users, we combine the client ID and low | 152 // For metrics reporting-enabled users, we combine the client ID and low |
152 // entropy source to get the final entropy source. Otherwise, only use the low | 153 // entropy source to get the final entropy source. Otherwise, only use the low |
153 // entropy source. | 154 // entropy source. |
154 // This has two useful properties: | 155 // This has two useful properties: |
155 // 1) It makes the entropy source less identifiable for parties that do not | 156 // 1) It makes the entropy source less identifiable for parties that do not |
156 // know the low entropy source. | 157 // know the low entropy source. |
157 // 2) It makes the final entropy source resettable. | 158 // 2) It makes the final entropy source resettable. |
158 const int low_entropy_source_value = GetLowEntropySource(); | 159 const int low_entropy_source_value = GetLowEntropySource(); |
159 UMA_HISTOGRAM_SPARSE_SLOWLY("UMA.LowEntropySourceValue", | 160 UMA_HISTOGRAM_SPARSE_SLOWLY("UMA.LowEntropySourceValue", |
160 low_entropy_source_value); | 161 low_entropy_source_value); |
161 if (IsMetricsReportingEnabled()) { | 162 if (enabled_state_provider_->IsConsentGiven()) { |
162 UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_HIGH); | 163 UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_HIGH); |
163 const std::string high_entropy_source = | 164 const std::string high_entropy_source = |
164 client_id_ + base::IntToString(low_entropy_source_value); | 165 client_id_ + base::IntToString(low_entropy_source_value); |
165 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( | 166 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( |
166 new SHA1EntropyProvider(high_entropy_source)); | 167 new SHA1EntropyProvider(high_entropy_source)); |
167 } | 168 } |
168 | 169 |
169 UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_LOW); | 170 UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_LOW); |
170 #if defined(OS_ANDROID) || defined(OS_IOS) | 171 #if defined(OS_ANDROID) || defined(OS_IOS) |
171 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( | 172 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( |
172 new CachingPermutedEntropyProvider(local_state_, low_entropy_source_value, | 173 new CachingPermutedEntropyProvider(local_state_, low_entropy_source_value, |
173 kMaxLowEntropySize)); | 174 kMaxLowEntropySize)); |
174 #else | 175 #else |
175 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( | 176 return std::unique_ptr<const base::FieldTrial::EntropyProvider>( |
176 new PermutedEntropyProvider(low_entropy_source_value, | 177 new PermutedEntropyProvider(low_entropy_source_value, |
177 kMaxLowEntropySize)); | 178 kMaxLowEntropySize)); |
178 #endif | 179 #endif |
179 } | 180 } |
180 | 181 |
181 // static | 182 // static |
182 std::unique_ptr<MetricsStateManager> MetricsStateManager::Create( | 183 std::unique_ptr<MetricsStateManager> MetricsStateManager::Create( |
183 PrefService* local_state, | 184 PrefService* local_state, |
184 const base::Callback<bool(void)>& is_reporting_enabled_callback, | 185 EnabledStateProvider* enabled_state_provider, |
185 const StoreClientInfoCallback& store_client_info, | 186 const StoreClientInfoCallback& store_client_info, |
186 const LoadClientInfoCallback& retrieve_client_info) { | 187 const LoadClientInfoCallback& retrieve_client_info) { |
187 std::unique_ptr<MetricsStateManager> result; | 188 std::unique_ptr<MetricsStateManager> result; |
188 // Note: |instance_exists_| is updated in the constructor and destructor. | 189 // Note: |instance_exists_| is updated in the constructor and destructor. |
189 if (!instance_exists_) { | 190 if (!instance_exists_) { |
190 result.reset(new MetricsStateManager(local_state, | 191 result.reset(new MetricsStateManager(local_state, enabled_state_provider, |
191 is_reporting_enabled_callback, | |
192 store_client_info, | 192 store_client_info, |
193 retrieve_client_info)); | 193 retrieve_client_info)); |
194 } | 194 } |
195 return result; | 195 return result; |
196 } | 196 } |
197 | 197 |
198 // static | 198 // static |
199 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { | 199 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { |
200 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); | 200 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); |
201 // registry->RegisterIntegerPref(prefs::kMetricsDefaultOptIn, | 201 // registry->RegisterIntegerPref(prefs::kMetricsDefaultOptIn, |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 | 305 |
306 local_state_->ClearPref(prefs::kMetricsClientID); | 306 local_state_->ClearPref(prefs::kMetricsClientID); |
307 local_state_->ClearPref(prefs::kMetricsLowEntropySource); | 307 local_state_->ClearPref(prefs::kMetricsLowEntropySource); |
308 local_state_->ClearPref(prefs::kMetricsResetIds); | 308 local_state_->ClearPref(prefs::kMetricsResetIds); |
309 | 309 |
310 // Also clear the backed up client info. | 310 // Also clear the backed up client info. |
311 store_client_info_.Run(ClientInfo()); | 311 store_client_info_.Run(ClientInfo()); |
312 } | 312 } |
313 | 313 |
314 } // namespace metrics | 314 } // namespace metrics |
OLD | NEW |