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/metrics/chrome_metrics_service_accessor.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 namespace { |
13 // GoogleUpdateSettings touches the disk from the UI thread. MetricsService | |
14 // also calls GoogleUpdateSettings below. http://crbug/62626 | |
15 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
16 | 19 |
20 enum MetricsReportingChangeHistogramValue { | |
21 METRICS_REPORTING_ERROR, | |
22 METRICS_REPORTING_DISABLED, | |
23 METRICS_REPORTING_ENABLED, | |
24 METRICS_REPORTING_MAX | |
25 }; | |
26 | |
27 void RecordMetricsReportingHistogramValue( | |
28 MetricsReportingChangeHistogramValue value) { | |
29 UMA_HISTOGRAM_ENUMERATION( | |
30 "UMA.MetricsReporting.Toggle", value, METRICS_REPORTING_MAX); | |
31 } | |
32 | |
33 // Tries to set crash stats upload consent to |enabled|. | |
34 bool SetGoogleUpdateSettings(bool enabled) { | |
17 GoogleUpdateSettings::SetCollectStatsConsent(enabled); | 35 GoogleUpdateSettings::SetCollectStatsConsent(enabled); |
18 bool update_pref = GoogleUpdateSettings::GetCollectStatsConsent(); | 36 bool updated_pref = GoogleUpdateSettings::GetCollectStatsConsent(); |
37 if (enabled != updated_pref) | |
38 DVLOG(1) << "Unable to set metrics reporting status to " << enabled; | |
19 | 39 |
20 if (enabled != update_pref) | 40 return updated_pref; |
21 DVLOG(1) << "Unable to set crash report status to " << enabled; | 41 } |
22 | 42 |
43 // Does the necessary changes for MetricsReportingEnabled changes which needs | |
44 // to be done in the main thread. | |
45 void SetMetricsReporting(bool enabled, | |
Alexei Svitkine (slow)
2014/09/11 15:33:43
If you're keeping both params, please document som
gayane -on leave until 09-2017
2014/09/11 17:06:08
Done.
| |
46 const base::Callback<void(bool)> callback_fn, | |
47 bool updated_pref) { | |
23 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent | 48 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent |
24 // succeeds. | 49 // succeeds. |
25 enabled = update_pref; | |
26 | |
27 metrics::MetricsService* metrics = g_browser_process->metrics_service(); | 50 metrics::MetricsService* metrics = g_browser_process->metrics_service(); |
28 if (metrics) { | 51 if (metrics) { |
29 if (enabled) | 52 if (updated_pref) |
30 metrics->Start(); | 53 metrics->Start(); |
31 else | 54 else |
32 metrics->Stop(); | 55 metrics->Stop(); |
33 } | 56 } |
57 #if !defined(OS_CHROMEOS) | |
58 g_browser_process->local_state()->SetBoolean( | |
59 prefs::kMetricsReportingEnabled, enabled); | |
Alexei Svitkine (slow)
2014/09/11 15:33:43
This should be using updated_pref.
gayane -on leave until 09-2017
2014/09/11 17:06:08
Done.
| |
60 #endif | |
61 if (enabled == updated_pref) { | |
62 RecordMetricsReportingHistogramValue(enabled ? METRICS_REPORTING_ENABLED : | |
63 METRICS_REPORTING_DISABLED); | |
64 } else { | |
65 RecordMetricsReportingHistogramValue(METRICS_REPORTING_ERROR); | |
66 } | |
67 if (!callback_fn.is_null()) | |
68 callback_fn.Run(updated_pref); | |
69 } | |
34 | 70 |
35 return enabled; | 71 } // namespace |
72 | |
73 void InitiateMetricsReportingChange( | |
74 bool enabled, | |
75 const base::Callback<void(bool)> callback_fn) { | |
76 if (!IsMetricsReportingUserChangable()) { | |
77 if (!callback_fn.is_null()) | |
Alexei Svitkine (slow)
2014/09/11 15:33:43
Nit: Add {}'s since body is multi-line now.
gayane -on leave until 09-2017
2014/09/11 17:06:08
Done.
| |
78 callback_fn.Run( | |
79 ChromeMetricsServiceAccessor::IsMetricsReportingEnabled()); | |
80 return; | |
81 } | |
82 | |
83 // Posts to FILE thread as SetGoogleUpdateSettings does IO operations. | |
84 content::BrowserThread::PostTaskAndReplyWithResult( | |
85 content::BrowserThread::FILE, | |
86 FROM_HERE, | |
87 base::Bind(&SetGoogleUpdateSettings, enabled), | |
88 base::Bind(&SetMetricsReporting, enabled, callback_fn)); | |
36 } | 89 } |
90 | |
91 bool IsMetricsReportingUserChangable() { | |
92 const PrefService* pref_service = g_browser_process->local_state(); | |
93 | |
94 const PrefService::Preference* pref = | |
95 pref_service->FindPreference(prefs::kMetricsReportingEnabled); | |
96 return pref && pref->IsManaged(); | |
97 } | |
OLD | NEW |