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

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

Issue 506663003: Consolidates accessing and setting the UMA pref to be within metrics code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes, more comments Created 6 years, 3 months 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/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"
Alexei Svitkine (slow) 2014/09/11 17:17:51 Is this header used? If not, remove.
gayane -on leave until 09-2017 2014/09/11 19:04:10 Done.
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 base::Callback<void(bool)> 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)
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 ? METRICS_REPORTING_ENABLE D :
Alexei Svitkine (slow) 2014/09/11 17:17:51 Nit: Make it fit within 80 chars/line.
gayane -on leave until 09-2017 2014/09/11 19:04:10 Done.
68 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 base::Callback<void(bool)> 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 const PrefService* pref_service = g_browser_process->local_state();
99
100 const PrefService::Preference* pref =
101 pref_service->FindPreference(prefs::kMetricsReportingEnabled);
102 return pref && pref->IsManaged();
103 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698