OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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_reporting_state.h" | 5 #include "chrome/browser/metrics/metrics_reporting_state.h" |
6 | 6 |
7 #include "base/threading/thread_restrictions.h" | 7 #include "base/callback.h" |
| 8 #include "base/metrics/histogram.h" |
| 9 #include "base/prefs/pref_service.h" |
8 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 12 #include "chrome/common/pref_names.h" |
9 #include "chrome/installer/util/google_update_settings.h" | 13 #include "chrome/installer/util/google_update_settings.h" |
| 14 #include "chromeos/settings/cros_settings_names.h" |
10 #include "components/metrics/metrics_service.h" | 15 #include "components/metrics/metrics_service.h" |
| 16 #include "content/public/browser/browser_thread.h" |
11 | 17 |
12 bool ResolveMetricsReportingEnabled(bool enabled) { | 18 enum MetricsReportingChangeHistogramValue { |
13 // GoogleUpdateSettings touches the disk from the UI thread. MetricsService | 19 METRICS_REPORTING_ENABLED, |
14 // also calls GoogleUpdateSettings below. http://crbug/62626 | 20 METRICS_REPORTING_DISABLED, |
15 base::ThreadRestrictions::ScopedAllowIO allow_io; | 21 METRICS_REPORTING_MAX, |
| 22 }; |
16 | 23 |
| 24 void RecordMetricsReportingHistogramValue( |
| 25 MetricsReportingChangeHistogramValue value){ |
| 26 UMA_HISTOGRAM_ENUMERATION( |
| 27 "MetricsReporting", value, METRICS_REPORTING_MAX); |
| 28 } |
| 29 |
| 30 bool SetGoogleUpdateSettings(bool enabled) { |
17 GoogleUpdateSettings::SetCollectStatsConsent(enabled); | 31 GoogleUpdateSettings::SetCollectStatsConsent(enabled); |
18 bool update_pref = GoogleUpdateSettings::GetCollectStatsConsent(); | 32 bool updated_pref = GoogleUpdateSettings::GetCollectStatsConsent(); |
19 | 33 bool success = (enabled == updated_pref); |
20 if (enabled != update_pref) | 34 if (!success) |
21 DVLOG(1) << "Unable to set crash report status to " << enabled; | 35 DVLOG(1) << "Unable to set crash report status to " << enabled; |
22 | 36 |
| 37 return success; |
| 38 } |
| 39 |
| 40 void SetMetricsReporting(base::Callback<void(bool)> callback_fn, |
| 41 bool enabled, bool success) { |
23 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent | 42 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent |
24 // succeeds. | 43 // succeeds. |
25 enabled = update_pref; | 44 MetricsService* metrics = g_browser_process->metrics_service(); |
| 45 if (success) { |
| 46 if (metrics) { |
| 47 if (enabled) |
| 48 metrics->Start(); |
| 49 else |
| 50 metrics->Stop(); |
| 51 } |
| 52 #if !defined(OS_CHROMEOS) |
| 53 g_browser_process->local_state()->SetBoolean( |
| 54 prefs::kMetricsReportingEnabled, enabled); |
| 55 #endif |
| 56 RecordMetricsReportingHistogramValue(enabled? METRICS_REPORTING_ENABLED |
| 57 :METRICS_REPORTING_DISABLED); |
| 58 } |
| 59 if (!callback_fn.is_null()) |
| 60 callback_fn.Run(success); |
| 61 } |
26 | 62 |
27 MetricsService* metrics = g_browser_process->metrics_service(); | 63 void InitiateMetricsReportingChange( |
28 if (metrics) { | 64 base::Callback<void(bool)> callback_fn, bool enabled) { |
29 if (enabled) | 65 content::BrowserThread::PostTaskAndReplyWithResult( |
30 metrics->Start(); | 66 content::BrowserThread::FILE, |
31 else | 67 FROM_HERE, |
32 metrics->Stop(); | 68 base::Bind(&SetGoogleUpdateSettings, enabled), |
33 } | 69 base::Bind(&SetMetricsReporting, callback_fn, enabled)); |
34 | |
35 return enabled; | |
36 } | 70 } |
OLD | NEW |