Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / metrics / metrics_reporting_state.cc
bloba4a61feab95f0e73724040a49381e9a47db08fa1
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
3 // found in the LICENSE file.
5 #include "chrome/browser/metrics/metrics_reporting_state.h"
7 #include "base/callback.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_service.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/installer/util/google_update_settings.h"
14 #include "components/metrics/metrics_service.h"
15 #include "content/public/browser/browser_thread.h"
17 namespace {
19 enum MetricsReportingChangeHistogramValue {
20 METRICS_REPORTING_ERROR,
21 METRICS_REPORTING_DISABLED,
22 METRICS_REPORTING_ENABLED,
23 METRICS_REPORTING_MAX
26 void RecordMetricsReportingHistogramValue(
27 MetricsReportingChangeHistogramValue value) {
28 UMA_HISTOGRAM_ENUMERATION(
29 "UMA.MetricsReporting.Toggle", value, METRICS_REPORTING_MAX);
32 // Tries to set metrics reporting status to |enabled| and returns whatever is
33 // the result of the update.
34 bool SetGoogleUpdateSettings(bool enabled) {
35 GoogleUpdateSettings::SetCollectStatsConsent(enabled);
36 bool updated_pref = GoogleUpdateSettings::GetCollectStatsConsent();
37 if (enabled != updated_pref)
38 DVLOG(1) << "Unable to set metrics reporting status to " << enabled;
40 return updated_pref;
43 // Does the necessary changes for MetricsReportingEnabled changes which needs
44 // to be done in the main thread.
45 // As arguments this function gets:
46 // |to_update_pref| which indicates what the desired update should be,
47 // |callback_fn| is the callback function to be called in the end
48 // |updated_pref| is the result of attempted update.
49 // Update considers to be successful if |to_update_pref| and |updated_pref| are
50 // the same.
51 void SetMetricsReporting(bool to_update_pref,
52 const OnMetricsReportingCallbackType& callback_fn,
53 bool updated_pref) {
54 metrics::MetricsService* metrics = g_browser_process->metrics_service();
55 if (metrics) {
56 if (updated_pref)
57 metrics->Start();
58 else
59 metrics->Stop();
61 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
62 g_browser_process->local_state()->SetBoolean(
63 prefs::kMetricsReportingEnabled, updated_pref);
64 #endif
65 // When a user opts in to the metrics reporting service, the previously
66 // collected data should be cleared to ensure that nothing is reported before
67 // a user opts in and all reported data is accurate.
68 if (updated_pref && metrics)
69 metrics->ClearSavedStabilityMetrics();
71 if (to_update_pref == updated_pref) {
72 RecordMetricsReportingHistogramValue(updated_pref ?
73 METRICS_REPORTING_ENABLED : METRICS_REPORTING_DISABLED);
74 } else {
75 RecordMetricsReportingHistogramValue(METRICS_REPORTING_ERROR);
77 if (!callback_fn.is_null())
78 callback_fn.Run(updated_pref);
81 } // namespace
83 void InitiateMetricsReportingChange(
84 bool enabled,
85 const OnMetricsReportingCallbackType& callback_fn) {
86 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
87 if (!IsMetricsReportingUserChangable()) {
88 if (!callback_fn.is_null()) {
89 callback_fn.Run(
90 ChromeMetricsServiceAccessor::IsMetricsReportingEnabled());
92 return;
94 #endif
95 // Posts to FILE thread as SetGoogleUpdateSettings does IO operations.
96 content::BrowserThread::PostTaskAndReplyWithResult(
97 content::BrowserThread::FILE,
98 FROM_HERE,
99 base::Bind(&SetGoogleUpdateSettings, enabled),
100 base::Bind(&SetMetricsReporting, enabled, callback_fn));
103 bool IsMetricsReportingUserChangable() {
104 const PrefService* pref_service = g_browser_process->local_state();
105 const PrefService::Preference* pref =
106 pref_service->FindPreference(prefs::kMetricsReportingEnabled);
107 return pref && !pref->IsManaged();