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" | |
Alexei Svitkine (slow)
2014/09/05 20:50:24
Do you need the CrOS includes (this one and below)
gayane -on leave until 09-2017
2014/09/08 18:32:46
Done.
| |
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_DISABLED, | |
22 METRICS_REPORTING_ENABLED, | |
23 METRICS_REPORTING_MAX | |
24 }; | |
25 | |
26 void RecordMetricsReportingHistogramValue( | |
27 MetricsReportingChangeHistogramValue value) { | |
28 UMA_HISTOGRAM_ENUMERATION( | |
29 "UMA.MetricsReporting", value, METRICS_REPORTING_MAX); | |
30 } | |
31 | |
32 // Tries to set crash stats upload consent to |enabled|. | |
33 bool SetGoogleUpdateSettings(bool enabled) { | |
17 GoogleUpdateSettings::SetCollectStatsConsent(enabled); | 34 GoogleUpdateSettings::SetCollectStatsConsent(enabled); |
18 bool update_pref = GoogleUpdateSettings::GetCollectStatsConsent(); | 35 bool updated_pref = GoogleUpdateSettings::GetCollectStatsConsent(); |
19 | 36 bool success = (enabled == updated_pref); |
20 if (enabled != update_pref) | 37 if (!success) |
21 DVLOG(1) << "Unable to set crash report status to " << enabled; | 38 DVLOG(1) << "Unable to set crash report status to " << enabled; |
Alexei Svitkine (slow)
2014/09/05 20:50:24
Nit: Update this message while you're changing thi
gayane -on leave until 09-2017
2014/09/08 18:32:46
Done.
| |
22 | 39 |
40 return success; | |
41 } | |
42 | |
43 // Does the necessary changes for MetricsReportingEnabled changes which needs | |
44 // to be done in the main thread. | |
45 void SetMetricsReporting(bool enabled, | |
46 base::Callback<void(bool)> callback_fn, | |
Alexei Svitkine (slow)
2014/09/05 20:50:24
Nit: pass the callback by const ref.
gayane -on leave until 09-2017
2014/09/08 18:32:46
Done.
| |
47 bool success) { | |
23 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent | 48 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent |
24 // succeeds. | 49 // succeeds. |
25 enabled = update_pref; | 50 metrics::MetricsService* metrics = g_browser_process->metrics_service(); |
51 if (success) { | |
52 if (metrics) { | |
53 if (enabled) | |
54 metrics->Start(); | |
55 else | |
56 metrics->Stop(); | |
57 } | |
58 #if !defined(OS_CHROMEOS) | |
59 g_browser_process->local_state()->SetBoolean( | |
60 prefs::kMetricsReportingEnabled, enabled); | |
61 #endif | |
62 RecordMetricsReportingHistogramValue(enabled ? METRICS_REPORTING_ENABLED : | |
63 METRICS_REPORTING_DISABLED); | |
64 } | |
65 if (!callback_fn.is_null()) | |
66 callback_fn.Run(success); | |
67 } | |
26 | 68 |
27 metrics::MetricsService* metrics = g_browser_process->metrics_service(); | 69 } // namespace |
28 if (metrics) { | 70 |
29 if (enabled) | 71 void InitiateMetricsReportingChange(bool enabled, |
30 metrics->Start(); | 72 base::Callback<void(bool)> callback_fn) { |
Alexei Svitkine (slow)
2014/09/05 20:50:24
Nit: pass the callback by const ref.
gayane -on leave until 09-2017
2014/09/08 18:32:46
Done.
| |
31 else | 73 if (!IsMetricsReportingUserChangable()) { |
32 metrics->Stop(); | 74 if (!callback_fn.is_null()) |
75 callback_fn.Run(false); | |
76 return; | |
33 } | 77 } |
34 | 78 |
35 return enabled; | 79 // Posts on FILE thread as SetGoogleUpdateSettings modifies the local state. |
Alexei Svitkine (slow)
2014/09/05 20:50:24
This comment is incorrect. "Local State" in fact n
gayane -on leave until 09-2017
2014/09/08 18:32:46
Done.
On 2014/09/05 20:50:24, Alexei Svitkine wro
| |
80 content::BrowserThread::PostTaskAndReplyWithResult( | |
81 content::BrowserThread::FILE, | |
82 FROM_HERE, | |
83 base::Bind(&SetGoogleUpdateSettings, enabled), | |
84 base::Bind(&SetMetricsReporting, enabled, callback_fn)); | |
36 } | 85 } |
86 | |
87 bool IsMetricsReportingUserChangable() { | |
Alexei Svitkine (slow)
2014/09/05 20:50:24
This isn't correct for CrOS or Android.
1. We sho
gayane -on leave until 09-2017
2014/09/08 18:32:46
- Comment added in the .h file for this function a
| |
88 const PrefService* pref_service = g_browser_process->local_state(); | |
89 | |
90 const PrefService::Preference* pref = | |
91 pref_service->FindPreference(prefs::kMetricsReportingEnabled); | |
92 return pref && pref->IsManaged(); | |
93 } | |
OLD | NEW |