Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(360)

Side by Side Diff: chrome/browser/metrics/metrics_reporting_state.cc

Issue 1411863002: Use kMetricsReportingEnabled instead of kStatsReporingPref on metrics side (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix callback Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.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/installer/util/google_update_settings.h" 12 #include "chrome/installer/util/google_update_settings.h"
13 #include "components/metrics/metrics_pref_names.h" 13 #include "components/metrics/metrics_pref_names.h"
14 #include "components/metrics/metrics_service.h" 14 #include "components/metrics/metrics_service.h"
15 #include "content/public/browser/browser_thread.h" 15 #include "content/public/browser/browser_thread.h"
16 16
17 #if defined(OS_CHROMEOS)
18 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chromeos/settings/cros_settings_names.h"
20 #endif // defined(OS_CHROMEOS)
21
17 namespace { 22 namespace {
18 23
19 enum MetricsReportingChangeHistogramValue { 24 enum MetricsReportingChangeHistogramValue {
20 METRICS_REPORTING_ERROR, 25 METRICS_REPORTING_ERROR,
21 METRICS_REPORTING_DISABLED, 26 METRICS_REPORTING_DISABLED,
22 METRICS_REPORTING_ENABLED, 27 METRICS_REPORTING_ENABLED,
23 METRICS_REPORTING_MAX 28 METRICS_REPORTING_MAX
24 }; 29 };
25 30
26 void RecordMetricsReportingHistogramValue( 31 void RecordMetricsReportingHistogramValue(
(...skipping 24 matching lines...) Expand all
51 void SetMetricsReporting(bool to_update_pref, 56 void SetMetricsReporting(bool to_update_pref,
52 const OnMetricsReportingCallbackType& callback_fn, 57 const OnMetricsReportingCallbackType& callback_fn,
53 bool updated_pref) { 58 bool updated_pref) {
54 metrics::MetricsService* metrics = g_browser_process->metrics_service(); 59 metrics::MetricsService* metrics = g_browser_process->metrics_service();
55 if (metrics) { 60 if (metrics) {
56 if (updated_pref) 61 if (updated_pref)
57 metrics->Start(); 62 metrics->Start();
58 else 63 else
59 metrics->Stop(); 64 metrics->Stop();
60 } 65 }
61 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) 66
67 #if !defined(OS_ANDROID)
62 g_browser_process->local_state()->SetBoolean( 68 g_browser_process->local_state()->SetBoolean(
63 metrics::prefs::kMetricsReportingEnabled, updated_pref); 69 metrics::prefs::kMetricsReportingEnabled, updated_pref);
64 #endif 70 #endif // !defined(OS_ANDROID)
71
65 // When a user opts in to the metrics reporting service, the previously 72 // 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 73 // collected data should be cleared to ensure that nothing is reported before
67 // a user opts in and all reported data is accurate. 74 // a user opts in and all reported data is accurate.
68 if (updated_pref && metrics) 75 if (updated_pref && metrics)
69 metrics->ClearSavedStabilityMetrics(); 76 metrics->ClearSavedStabilityMetrics();
70 77
71 if (to_update_pref == updated_pref) { 78 if (to_update_pref == updated_pref) {
72 RecordMetricsReportingHistogramValue(updated_pref ? 79 RecordMetricsReportingHistogramValue(updated_pref ?
73 METRICS_REPORTING_ENABLED : METRICS_REPORTING_DISABLED); 80 METRICS_REPORTING_ENABLED : METRICS_REPORTING_DISABLED);
74 } else { 81 } else {
75 RecordMetricsReportingHistogramValue(METRICS_REPORTING_ERROR); 82 RecordMetricsReportingHistogramValue(METRICS_REPORTING_ERROR);
76 } 83 }
77 if (!callback_fn.is_null()) 84 if (!callback_fn.is_null())
78 callback_fn.Run(updated_pref); 85 callback_fn.Run(updated_pref);
79 } 86 }
80 87
88 #if defined(OS_CHROMEOS)
89 void onDeviceSettingChange() {
Alexei Svitkine (slow) 2015/11/03 23:18:23 Nit: Capitalize. Also, add a comment.
gayane -on leave until 09-2017 2015/11/05 18:53:32 Done.
90 bool enable_metrics = false;
91 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref,
92 &enable_metrics);
93 InitiateMetricsReportingChange(enable_metrics,
94 OnMetricsReportingCallbackType());
95 }
96 #endif
81 } // namespace 97 } // namespace
Alexei Svitkine (slow) 2015/11/03 23:18:23 Nit: Add an empty line above.
gayane -on leave until 09-2017 2015/11/05 18:53:32 Done.
82 98
99 // TODO(gayane): Instead of checking policy before setting the metrics pref set
100 // the pref and register for notifications for the rest of the changes.
83 void InitiateMetricsReportingChange( 101 void InitiateMetricsReportingChange(
84 bool enabled, 102 bool enabled,
85 const OnMetricsReportingCallbackType& callback_fn) { 103 const OnMetricsReportingCallbackType& callback_fn) {
86 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) 104 #if !defined(OS_ANDROID)
87 if (!IsMetricsReportingUserChangable()) { 105 if (IsMetricsReportingPolicyManaged()) {
88 if (!callback_fn.is_null()) { 106 if (!callback_fn.is_null()) {
89 callback_fn.Run( 107 callback_fn.Run(
90 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled()); 108 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled());
91 } 109 }
92 return; 110 return;
93 } 111 }
94 #endif 112 #endif
95 // Posts to FILE thread as SetGoogleUpdateSettings does IO operations. 113 // Posts to FILE thread as SetGoogleUpdateSettings does IO operations.
96 content::BrowserThread::PostTaskAndReplyWithResult( 114 content::BrowserThread::PostTaskAndReplyWithResult(
97 content::BrowserThread::FILE, 115 content::BrowserThread::FILE,
98 FROM_HERE, 116 FROM_HERE,
99 base::Bind(&SetGoogleUpdateSettings, enabled), 117 base::Bind(&SetGoogleUpdateSettings, enabled),
100 base::Bind(&SetMetricsReporting, enabled, callback_fn)); 118 base::Bind(&SetMetricsReporting, enabled, callback_fn));
101 } 119 }
102 120
103 bool IsMetricsReportingUserChangable() { 121 bool IsMetricsReportingPolicyManaged() {
104 const PrefService* pref_service = g_browser_process->local_state(); 122 const PrefService* pref_service = g_browser_process->local_state();
105 const PrefService::Preference* pref = 123 const PrefService::Preference* pref =
106 pref_service->FindPreference(metrics::prefs::kMetricsReportingEnabled); 124 pref_service->FindPreference(metrics::prefs::kMetricsReportingEnabled);
107 return pref && !pref->IsManaged(); 125 return pref && pref->IsManaged();
108 } 126 }
127
128 void SetupMetricsStateForChromeOS() {
129 #if defined(OS_CHROMEOS)
130 chromeos::CrosSettings::Get()->AddSettingsObserver(
131 chromeos::kStatsReportingPref, base::Bind(&onDeviceSettingChange));
Alexei Svitkine (slow) 2015/11/03 23:18:23 Is it possible to add a unit test for this? i.e.
gayane -on leave until 09-2017 2015/11/05 18:53:32 As to discussion, I added a TODO to add unittests
132
133 onDeviceSettingChange();
134 #endif // defined(OS_CHROMEOS)
135 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698