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/policy/browser_policy_connector_chromeos.h" | |
12 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" | |
13 #include "chrome/common/pref_names.h" | |
9 #include "chrome/installer/util/google_update_settings.h" | 14 #include "chrome/installer/util/google_update_settings.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 metrics reporting status to |enabled| and returns whatever is | |
34 // the result of the update. | |
35 bool SetGoogleUpdateSettings(bool enabled) { | |
17 GoogleUpdateSettings::SetCollectStatsConsent(enabled); | 36 GoogleUpdateSettings::SetCollectStatsConsent(enabled); |
18 bool update_pref = GoogleUpdateSettings::GetCollectStatsConsent(); | 37 bool updated_pref = GoogleUpdateSettings::GetCollectStatsConsent(); |
38 if (enabled != updated_pref) | |
39 DVLOG(1) << "Unable to set metrics reporting status to " << enabled; | |
19 | 40 |
20 if (enabled != update_pref) | 41 return updated_pref; |
21 DVLOG(1) << "Unable to set crash report status to " << enabled; | 42 } |
22 | 43 |
23 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent | 44 // Does the necessary changes for MetricsReportingEnabled changes which needs |
24 // succeeds. | 45 // to be done in the main thread. |
25 enabled = update_pref; | 46 // As arguments this function gets: |
26 | 47 // |to_update_pref| which indicates what the desired update should be, |
48 // |callback_fn| is the callback function to be called in the end | |
49 // |updated_pref| is the result of attempted update. | |
50 // Update considers to be successful if |to_update_pref| and |updated_pref| are | |
51 // the same. | |
52 void SetMetricsReporting(bool to_update_pref, | |
53 const OnMetricsReportingCallbackType& callback_fn, | |
54 bool updated_pref) { | |
27 metrics::MetricsService* metrics = g_browser_process->metrics_service(); | 55 metrics::MetricsService* metrics = g_browser_process->metrics_service(); |
28 if (metrics) { | 56 if (metrics) { |
29 if (enabled) | 57 if (updated_pref) |
30 metrics->Start(); | 58 metrics->Start(); |
31 else | 59 else |
32 metrics->Stop(); | 60 metrics->Stop(); |
33 } | 61 } |
62 #if !defined(OS_CHROMEOS) | |
Alexei Svitkine (slow)
2014/09/15 21:36:44
I would also expand this ifdef to be !ANDROID too.
gayane -on leave until 09-2017
2014/09/16 17:28:52
Done.
| |
63 g_browser_process->local_state()->SetBoolean( | |
64 prefs::kMetricsReportingEnabled, updated_pref); | |
65 #endif | |
66 if (to_update_pref == updated_pref) { | |
67 RecordMetricsReportingHistogramValue(updated_pref ? | |
68 METRICS_REPORTING_ENABLED : METRICS_REPORTING_DISABLED); | |
69 } else { | |
70 RecordMetricsReportingHistogramValue(METRICS_REPORTING_ERROR); | |
71 } | |
72 if (!callback_fn.is_null()) | |
73 callback_fn.Run(updated_pref); | |
74 } | |
34 | 75 |
35 return enabled; | 76 } // namespace |
77 | |
78 void InitiateMetricsReportingChange( | |
79 bool enabled, | |
80 const OnMetricsReportingCallbackType& callback_fn) { | |
81 if (!IsMetricsReportingUserChangable()) { | |
82 if (!callback_fn.is_null()) { | |
83 callback_fn.Run( | |
84 ChromeMetricsServiceAccessor::IsMetricsReportingEnabled()); | |
85 } | |
86 return; | |
87 } | |
88 | |
89 // Posts to FILE thread as SetGoogleUpdateSettings does IO operations. | |
90 content::BrowserThread::PostTaskAndReplyWithResult( | |
91 content::BrowserThread::FILE, | |
92 FROM_HERE, | |
93 base::Bind(&SetGoogleUpdateSettings, enabled), | |
94 base::Bind(&SetMetricsReporting, enabled, callback_fn)); | |
36 } | 95 } |
96 | |
97 bool IsMetricsReportingUserChangable() { | |
98 #if defined(GOOGLE_CHROME_BUILD) | |
99 #if defined(OS_CHROMEOS) | |
100 policy::BrowserPolicyConnectorChromeOS* connector = | |
101 g_browser_process->platform_part()->browser_policy_connector_chromeos(); | |
102 return connector->IsEnterpriseManaged(); | |
103 #else | |
104 const PrefService* pref_service = g_browser_process->local_state(); | |
105 #if defined(OS_ANDROID) | |
Alexei Svitkine (slow)
2014/09/15 20:59:43
Why is all of this needed?
I thought this functio
gayane -on leave until 09-2017
2014/09/15 21:09:17
So the thing is that initially the plan was to use
Alexei Svitkine (slow)
2014/09/15 21:36:44
I see. But InitiateMetricsReportingChange() doesn'
gayane -on leave until 09-2017
2014/09/16 17:28:52
Done.
| |
106 const PrefService::Preference* pref = | |
107 pref_service->FindPreference(prefs::kCrashReportingEnabled); | |
108 #else | |
109 const PrefService::Preference* pref = | |
110 pref_service->FindPreference(prefs::kMetricsReportingEnabled); | |
111 #endif | |
112 return pref && !pref->IsManaged(); | |
113 #endif | |
114 #else | |
115 return true; | |
116 #endif | |
117 | |
118 } | |
OLD | NEW |