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/callback.h" | 7 #include "base/callback.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" | 11 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h" |
12 #include "chrome/common/crash_keys.h" | |
12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
13 #include "chrome/installer/util/google_update_settings.h" | 14 #include "chrome/installer/util/google_update_settings.h" |
14 #include "components/metrics/metrics_pref_names.h" | 15 #include "components/metrics/metrics_pref_names.h" |
15 #include "components/metrics/metrics_service.h" | 16 #include "components/metrics/metrics_service.h" |
16 #include "components/metrics_services_manager/metrics_services_manager.h" | 17 #include "components/metrics_services_manager/metrics_services_manager.h" |
17 #include "components/prefs/pref_registry_simple.h" | 18 #include "components/prefs/pref_registry_simple.h" |
18 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 | 21 |
21 #if defined(OS_CHROMEOS) | 22 #if defined(OS_CHROMEOS) |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 // to be done in the main thread. | 54 // to be done in the main thread. |
54 // As arguments this function gets: | 55 // As arguments this function gets: |
55 // |to_update_pref| which indicates what the desired update should be, | 56 // |to_update_pref| which indicates what the desired update should be, |
56 // |callback_fn| is the callback function to be called in the end | 57 // |callback_fn| is the callback function to be called in the end |
57 // |updated_pref| is the result of attempted update. | 58 // |updated_pref| is the result of attempted update. |
58 // Update considers to be successful if |to_update_pref| and |updated_pref| are | 59 // Update considers to be successful if |to_update_pref| and |updated_pref| are |
59 // the same. | 60 // the same. |
60 void SetMetricsReporting(bool to_update_pref, | 61 void SetMetricsReporting(bool to_update_pref, |
61 const OnMetricsReportingCallbackType& callback_fn, | 62 const OnMetricsReportingCallbackType& callback_fn, |
62 bool updated_pref) { | 63 bool updated_pref) { |
63 metrics::MetricsService* metrics = g_browser_process->metrics_service(); | |
64 | |
65 #if !defined(OS_ANDROID) | 64 #if !defined(OS_ANDROID) |
66 g_browser_process->local_state()->SetBoolean( | 65 g_browser_process->local_state()->SetBoolean( |
67 metrics::prefs::kMetricsReportingEnabled, updated_pref); | 66 metrics::prefs::kMetricsReportingEnabled, updated_pref); |
68 #endif // !defined(OS_ANDROID) | 67 #endif // !defined(OS_ANDROID) |
69 | 68 |
70 // Clear the client id pref when opting out. Note: Mirrors code in | 69 UpdateMetricsPrefsOnPermissionChange(updated_pref); |
71 // uma_session_stats.cc. TODO(asvitkine): Unify. | |
72 if (!updated_pref) { | |
73 // Note: Clearing client id will not affect the running state (e.g. field | |
74 // trial randomization), as the pref is only read on startup. | |
75 g_browser_process->local_state()->ClearPref( | |
76 metrics::prefs::kMetricsClientID); | |
77 g_browser_process->local_state()->ClearPref( | |
78 metrics::prefs::kMetricsReportingEnabledTimestamp); | |
79 } | |
80 | 70 |
81 // Uses the current state of whether reporting is enabled to enable services. | 71 // Uses the current state of whether reporting is enabled to enable services. |
82 g_browser_process->GetMetricsServicesManager()->UpdateUploadPermissions(true); | 72 g_browser_process->GetMetricsServicesManager()->UpdateUploadPermissions(true); |
83 | 73 |
84 // When a user opts in to the metrics reporting service, the previously | |
85 // collected data should be cleared to ensure that nothing is reported before | |
86 // a user opts in and all reported data is accurate. | |
87 // TODO(asvitkine): This logic should be added to uma_session_stats.cc too. | |
88 if (updated_pref && metrics) | |
89 metrics->ClearSavedStabilityMetrics(); | |
90 | |
91 if (to_update_pref == updated_pref) { | 74 if (to_update_pref == updated_pref) { |
92 RecordMetricsReportingHistogramValue(updated_pref ? | 75 RecordMetricsReportingHistogramValue(updated_pref ? |
93 METRICS_REPORTING_ENABLED : METRICS_REPORTING_DISABLED); | 76 METRICS_REPORTING_ENABLED : METRICS_REPORTING_DISABLED); |
94 } else { | 77 } else { |
95 RecordMetricsReportingHistogramValue(METRICS_REPORTING_ERROR); | 78 RecordMetricsReportingHistogramValue(METRICS_REPORTING_ERROR); |
96 } | 79 } |
97 if (!callback_fn.is_null()) | 80 if (!callback_fn.is_null()) |
98 callback_fn.Run(updated_pref); | 81 callback_fn.Run(updated_pref); |
99 } | 82 } |
100 | 83 |
(...skipping 30 matching lines...) Expand all Loading... | |
131 } | 114 } |
132 #endif | 115 #endif |
133 // Posts to FILE thread as SetGoogleUpdateSettings does IO operations. | 116 // Posts to FILE thread as SetGoogleUpdateSettings does IO operations. |
134 content::BrowserThread::PostTaskAndReplyWithResult( | 117 content::BrowserThread::PostTaskAndReplyWithResult( |
135 content::BrowserThread::FILE, | 118 content::BrowserThread::FILE, |
136 FROM_HERE, | 119 FROM_HERE, |
137 base::Bind(&SetGoogleUpdateSettings, enabled), | 120 base::Bind(&SetGoogleUpdateSettings, enabled), |
138 base::Bind(&SetMetricsReporting, enabled, callback_fn)); | 121 base::Bind(&SetMetricsReporting, enabled, callback_fn)); |
139 } | 122 } |
140 | 123 |
124 void UpdateMetricsPrefsOnPermissionChange(bool metrics_enabled) { | |
jwd
2016/08/16 20:30:04
Didn't think to ask this in the last review, but t
Alexei Svitkine (slow)
2016/08/16 21:07:50
It's definitely a good question. There's a couple
| |
125 if (metrics_enabled) { | |
126 // When a user opts in to the metrics reporting service, the previously | |
127 // collected data should be cleared to ensure that nothing is reported | |
128 // before a user opts in and all reported data is accurate. | |
129 g_browser_process->metrics_service()->ClearSavedStabilityMetrics(); | |
130 } else { | |
131 // Clear the client id pref when opting out. | |
132 // Note: Clearing client id will not affect the running state (e.g. field | |
133 // trial randomization), as the pref is only read on startup. | |
134 g_browser_process->local_state()->ClearPref( | |
135 metrics::prefs::kMetricsClientID); | |
136 g_browser_process->local_state()->ClearPref( | |
137 metrics::prefs::kMetricsReportingEnabledTimestamp); | |
138 crash_keys::ClearMetricsClientId(); | |
139 } | |
140 } | |
141 | |
141 bool IsMetricsReportingPolicyManaged() { | 142 bool IsMetricsReportingPolicyManaged() { |
142 const PrefService* pref_service = g_browser_process->local_state(); | 143 const PrefService* pref_service = g_browser_process->local_state(); |
143 const PrefService::Preference* pref = | 144 const PrefService::Preference* pref = |
144 pref_service->FindPreference(metrics::prefs::kMetricsReportingEnabled); | 145 pref_service->FindPreference(metrics::prefs::kMetricsReportingEnabled); |
145 return pref && pref->IsManaged(); | 146 return pref && pref->IsManaged(); |
146 } | 147 } |
147 | 148 |
148 // TODO(gayane): Add unittest which will check that observer on device settings | 149 // TODO(gayane): Add unittest which will check that observer on device settings |
149 // will trigger this function and kMetricsReportinEnabled as well as metrics | 150 // will trigger this function and kMetricsReportinEnabled as well as metrics |
150 // service state will be updated accordingly. | 151 // service state will be updated accordingly. |
151 void SetupMetricsStateForChromeOS() { | 152 void SetupMetricsStateForChromeOS() { |
152 #if defined(OS_CHROMEOS) | 153 #if defined(OS_CHROMEOS) |
153 chromeos::CrosSettings::Get()->AddSettingsObserver( | 154 chromeos::CrosSettings::Get()->AddSettingsObserver( |
154 chromeos::kStatsReportingPref, base::Bind(&OnDeviceSettingChange)); | 155 chromeos::kStatsReportingPref, base::Bind(&OnDeviceSettingChange)); |
155 | 156 |
156 OnDeviceSettingChange(); | 157 OnDeviceSettingChange(); |
157 #endif // defined(OS_CHROMEOS) | 158 #endif // defined(OS_CHROMEOS) |
158 } | 159 } |
OLD | NEW |