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" |
(...skipping 29 matching lines...) Expand all Loading... |
40 // activities. | 40 // activities. |
41 int GenerateLowEntropySource() { | 41 int GenerateLowEntropySource() { |
42 return base::RandInt(0, kMaxLowEntropySize - 1); | 42 return base::RandInt(0, kMaxLowEntropySize - 1); |
43 } | 43 } |
44 | 44 |
45 } // namespace | 45 } // namespace |
46 | 46 |
47 // static | 47 // static |
48 bool MetricsStateManager::instance_exists_ = false; | 48 bool MetricsStateManager::instance_exists_ = false; |
49 | 49 |
50 MetricsStateManager::MetricsStateManager(PrefService* local_state) | 50 MetricsStateManager::MetricsStateManager( |
| 51 PrefService* local_state, |
| 52 const base::Callback<bool(void)>& is_reporting_enabled_callback) |
51 : local_state_(local_state), | 53 : local_state_(local_state), |
| 54 is_reporting_enabled_callback_(is_reporting_enabled_callback), |
52 low_entropy_source_(kLowEntropySourceNotSet), | 55 low_entropy_source_(kLowEntropySourceNotSet), |
53 entropy_source_returned_(ENTROPY_SOURCE_NONE) { | 56 entropy_source_returned_(ENTROPY_SOURCE_NONE) { |
54 ResetMetricsIDsIfNecessary(); | 57 ResetMetricsIDsIfNecessary(); |
55 if (IsMetricsReportingEnabled()) | 58 if (IsMetricsReportingEnabled()) |
56 ForceClientIdCreation(); | 59 ForceClientIdCreation(); |
57 | 60 |
58 DCHECK(!instance_exists_); | 61 DCHECK(!instance_exists_); |
59 instance_exists_ = true; | 62 instance_exists_ = true; |
60 } | 63 } |
61 | 64 |
62 MetricsStateManager::~MetricsStateManager() { | 65 MetricsStateManager::~MetricsStateManager() { |
63 DCHECK(instance_exists_); | 66 DCHECK(instance_exists_); |
64 instance_exists_ = false; | 67 instance_exists_ = false; |
65 } | 68 } |
66 | 69 |
67 bool MetricsStateManager::IsMetricsReportingEnabled() { | 70 bool MetricsStateManager::IsMetricsReportingEnabled() { |
68 // If the user permits metrics reporting with the checkbox in the | 71 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 } | 72 } |
90 | 73 |
91 void MetricsStateManager::ForceClientIdCreation() { | 74 void MetricsStateManager::ForceClientIdCreation() { |
92 if (!client_id_.empty()) | 75 if (!client_id_.empty()) |
93 return; | 76 return; |
94 | 77 |
95 client_id_ = local_state_->GetString(prefs::kMetricsClientID); | 78 client_id_ = local_state_->GetString(prefs::kMetricsClientID); |
96 if (!client_id_.empty()) | 79 if (!client_id_.empty()) |
97 return; | 80 return; |
98 | 81 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 kMaxLowEntropySize)); | 136 kMaxLowEntropySize)); |
154 #else | 137 #else |
155 return scoped_ptr<const base::FieldTrial::EntropyProvider>( | 138 return scoped_ptr<const base::FieldTrial::EntropyProvider>( |
156 new PermutedEntropyProvider(low_entropy_source_value, | 139 new PermutedEntropyProvider(low_entropy_source_value, |
157 kMaxLowEntropySize)); | 140 kMaxLowEntropySize)); |
158 #endif | 141 #endif |
159 } | 142 } |
160 | 143 |
161 // static | 144 // static |
162 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( | 145 scoped_ptr<MetricsStateManager> MetricsStateManager::Create( |
163 PrefService* local_state) { | 146 PrefService* local_state, |
| 147 const base::Callback<bool(void)>& is_reporting_enabled_callback) { |
164 scoped_ptr<MetricsStateManager> result; | 148 scoped_ptr<MetricsStateManager> result; |
165 // Note: |instance_exists_| is updated in the constructor and destructor. | 149 // Note: |instance_exists_| is updated in the constructor and destructor. |
166 if (!instance_exists_) | 150 if (!instance_exists_) { |
167 result.reset(new MetricsStateManager(local_state)); | 151 result.reset( |
| 152 new MetricsStateManager(local_state, is_reporting_enabled_callback)); |
| 153 } |
168 return result.Pass(); | 154 return result.Pass(); |
169 } | 155 } |
170 | 156 |
171 // static | 157 // static |
172 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { | 158 void MetricsStateManager::RegisterPrefs(PrefRegistrySimple* registry) { |
173 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); | 159 registry->RegisterBooleanPref(prefs::kMetricsResetIds, false); |
174 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); | 160 registry->RegisterStringPref(prefs::kMetricsClientID, std::string()); |
175 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); | 161 registry->RegisterInt64Pref(prefs::kMetricsReportingEnabledTimestamp, 0); |
176 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, | 162 registry->RegisterIntegerPref(prefs::kMetricsLowEntropySource, |
177 kLowEntropySourceNotSet); | 163 kLowEntropySourceNotSet); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 | 210 |
225 DCHECK(client_id_.empty()); | 211 DCHECK(client_id_.empty()); |
226 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); | 212 DCHECK_EQ(kLowEntropySourceNotSet, low_entropy_source_); |
227 | 213 |
228 local_state_->ClearPref(prefs::kMetricsClientID); | 214 local_state_->ClearPref(prefs::kMetricsClientID); |
229 local_state_->ClearPref(prefs::kMetricsLowEntropySource); | 215 local_state_->ClearPref(prefs::kMetricsLowEntropySource); |
230 local_state_->ClearPref(prefs::kMetricsResetIds); | 216 local_state_->ClearPref(prefs::kMetricsResetIds); |
231 } | 217 } |
232 | 218 |
233 } // namespace metrics | 219 } // namespace metrics |
OLD | NEW |