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 "chrome/browser/metrics/metrics_state_manager.h" | 5 #include "chrome/browser/metrics/metrics_state_manager.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.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 "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/time/time.h" | 15 #include "base/time/time.h" |
16 #include "chrome/browser/metrics/cloned_install_detector.h" | 16 #include "chrome/browser/metrics/cloned_install_detector.h" |
17 #include "chrome/browser/metrics/machine_id_provider.h" | 17 #include "chrome/browser/metrics/machine_id_provider.h" |
18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
20 #include "components/variations/caching_permuted_entropy_provider.h" | 20 #include "components/variations/caching_permuted_entropy_provider.h" |
21 | 21 |
22 #if defined(OS_CHROMEOS) | |
23 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
24 #endif | |
25 | |
26 namespace metrics { | 22 namespace metrics { |
27 | 23 |
28 namespace { | 24 namespace { |
29 | 25 |
30 // The argument used to generate a non-identifying entropy source. We want no | 26 // The argument used to generate a non-identifying entropy source. We want no |
31 // more than 13 bits of entropy, so use this max to return a number in the range | 27 // more than 13 bits of entropy, so use this max to return a number in the range |
32 // [0, 7999] as the entropy source (12.97 bits of entropy). | 28 // [0, 7999] as the entropy source (12.97 bits of entropy). |
33 const int kMaxLowEntropySize = 8000; | 29 const int kMaxLowEntropySize = 8000; |
34 | 30 |
35 // Default prefs value for prefs::kMetricsLowEntropySource to indicate that the | 31 // Default prefs value for prefs::kMetricsLowEntropySource to indicate that the |
36 // value has not yet been set. | 32 // value has not yet been set. |
37 const int kLowEntropySourceNotSet = -1; | 33 const int kLowEntropySourceNotSet = -1; |
38 | 34 |
39 // Generates a new non-identifying entropy source used to seed persistent | 35 // Generates a new non-identifying entropy source used to seed persistent |
40 // activities. | 36 // activities. |
41 int GenerateLowEntropySource() { | 37 int GenerateLowEntropySource() { |
42 return base::RandInt(0, kMaxLowEntropySize - 1); | 38 return base::RandInt(0, kMaxLowEntropySize - 1); |
43 } | 39 } |
44 | 40 |
45 } // namespace | 41 } // namespace |
46 | 42 |
47 // static | 43 // static |
48 bool MetricsStateManager::instance_exists_ = false; | 44 bool MetricsStateManager::instance_exists_ = false; |
49 | 45 |
50 MetricsStateManager::MetricsStateManager(PrefService* local_state) | 46 MetricsStateManager::MetricsStateManager( |
| 47 PrefService* local_state, |
| 48 const base::Callback<bool(void)>& is_reporting_enabled_callback) |
51 : local_state_(local_state), | 49 : local_state_(local_state), |
| 50 is_reporting_enabled_callback_(is_reporting_enabled_callback), |
52 low_entropy_source_(kLowEntropySourceNotSet), | 51 low_entropy_source_(kLowEntropySourceNotSet), |
53 entropy_source_returned_(ENTROPY_SOURCE_NONE) { | 52 entropy_source_returned_(ENTROPY_SOURCE_NONE) { |
54 ResetMetricsIDsIfNecessary(); | 53 ResetMetricsIDsIfNecessary(); |
55 if (IsMetricsReportingEnabled()) | 54 if (IsMetricsReportingEnabled()) |
56 ForceClientIdCreation(); | 55 ForceClientIdCreation(); |
57 | 56 |
58 DCHECK(!instance_exists_); | 57 DCHECK(!instance_exists_); |
59 instance_exists_ = true; | 58 instance_exists_ = true; |
60 } | 59 } |
61 | 60 |
62 MetricsStateManager::~MetricsStateManager() { | 61 MetricsStateManager::~MetricsStateManager() { |
63 DCHECK(instance_exists_); | 62 DCHECK(instance_exists_); |
64 instance_exists_ = false; | 63 instance_exists_ = false; |
65 } | 64 } |
66 | 65 |
67 bool MetricsStateManager::IsMetricsReportingEnabled() { | 66 bool MetricsStateManager::IsMetricsReportingEnabled() { |
68 // If the user permits metrics reporting with the checkbox in the | 67 return is_reporting_enabled_callback_.Run(); |
69 // prefs, we turn on recording. We disable metrics completely for | |
70 // non-official builds. This can be forced with a flag. | |
71 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
72 if (command_line->HasSwitch(switches::kEnableMetricsReportingForTesting)) | |
73 return true; | |
74 | |
75 // Disable metrics reporting when field trials are forced. | |
76 if (command_line->HasSwitch(switches::kForceFieldTrials)) | |
77 return false; | |
78 | |
79 bool enabled = false; | |
80 #if defined(GOOGLE_CHROME_BUILD) | |
81 #if defined(OS_CHROMEOS) | |
82 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, | |
83 &enabled); | |
84 #else | |
85 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); | |
86 #endif // #if defined(OS_CHROMEOS) | |
87 #endif // defined(GOOGLE_CHROME_BUILD) | |
88 return enabled; | |
89 } | 68 } |
90 | 69 |
91 void MetricsStateManager::ForceClientIdCreation() { | 70 void MetricsStateManager::ForceClientIdCreation() { |
92 if (!client_id_.empty()) | 71 if (!client_id_.empty()) |
93 return; | 72 return; |
94 | 73 |
95 client_id_ = local_state_->GetString(prefs::kMetricsClientID); | 74 client_id_ = local_state_->GetString(prefs::kMetricsClientID); |
96 if (!client_id_.empty()) | 75 if (!client_id_.empty()) |
97 return; | 76 return; |
98 | 77 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 kMaxLowEntropySize)); | 133 kMaxLowEntropySize)); |
155 #else | 134 #else |
156 return scoped_ptr<const base::FieldTrial::EntropyProvider>( | 135 return scoped_ptr<const base::FieldTrial::EntropyProvider>( |
157 new PermutedEntropyProvider(low_entropy_source_value, | 136 new PermutedEntropyProvider(low_entropy_source_value, |
158 kMaxLowEntropySize)); | 137 kMaxLowEntropySize)); |
159 #endif | 138 #endif |
160 } | 139 } |
161 | 140 |
162 // static | 141 // static |
163 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( | 142 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( |
164 PrefService* local_state) { | 143 PrefService* local_state, |
| 144 const base::Callback<bool(void)>& is_reporting_enabled_callback) { |
165 scoped_ptr<MetricsStateManager> result; | 145 scoped_ptr<MetricsStateManager> result; |
166 // Note: |instance_exists_| is updated in the constructor and destructor. | 146 // Note: |instance_exists_| is updated in the constructor and destructor. |
167 if (!instance_exists_) | 147 if (!instance_exists_) { |
168 result.reset(new MetricsStateManager(local_state)); | 148 result.reset( |
| 149 new MetricsStateManager(local_state, is_reporting_enabled_callback)); |
| 150 } |
169 return result.Pass(); | 151 return result.Pass(); |
170 } | 152 } |
171 | 153 |
172 // static | 154 // static |
173 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { | 155 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { |
174 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); | 156 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); |
175 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); | 157 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); |
176 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); | 158 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); |
177 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, | 159 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, |
178 kLowEntropySourceNotSet); | 160 kLowEntropySourceNotSet); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 | 207 |
226 DCHECK(client_id_.empty()); | 208 DCHECK(client_id_.empty()); |
227 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); | 209 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); |
228 | 210 |
229 local_state_->ClearPref(prefs::kMetricsClientID); | 211 local_state_->ClearPref(prefs::kMetricsClientID); |
230 local_state_->ClearPref(prefs::kMetricsLowEntropySource); | 212 local_state_->ClearPref(prefs::kMetricsLowEntropySource); |
231 local_state_->ClearPref(prefs::kMetricsResetIds); | 213 local_state_->ClearPref(prefs::kMetricsResetIds); |
232 } | 214 } |
233 | 215 |
234 } // namespace metrics | 216 } // namespace metrics |
OLD | NEW |