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

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: android check removed 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"
10 #include "components/metrics/metrics_service.h" 14 #include "components/metrics/metrics_service.h"
15 #include "content/public/browser/browser_thread.h"
11 16
12 bool ResolveMetricsReportingEnabled(bool enabled) { 17 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 18
19 enum MetricsReportingChangeHistogramValue {
20 METRICS_REPORTING_ERROR,
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.Toggle", value, METRICS_REPORTING_MAX);
30 }
31
32 // Tries to set metrics reporting status to |enabled| and returns whatever is
33 // the result of the update.
34 bool SetGoogleUpdateSettings(bool enabled) {
17 GoogleUpdateSettings::SetCollectStatsConsent(enabled); 35 GoogleUpdateSettings::SetCollectStatsConsent(enabled);
18 bool update_pref = GoogleUpdateSettings::GetCollectStatsConsent(); 36 bool updated_pref = GoogleUpdateSettings::GetCollectStatsConsent();
37 if (enabled != updated_pref)
38 DVLOG(1) << "Unable to set metrics reporting status to " << enabled;
19 39
20 if (enabled != update_pref) 40 return updated_pref;
21 DVLOG(1) << "Unable to set crash report status to " << enabled; 41 }
22 42
23 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent 43 // Does the necessary changes for MetricsReportingEnabled changes which needs
24 // succeeds. 44 // to be done in the main thread.
25 enabled = update_pref; 45 // As arguments this function gets:
26 46 // |to_update_pref| which indicates what the desired update should be,
47 // |callback_fn| is the callback function to be called in the end
48 // |updated_pref| is the result of attempted update.
49 // Update considers to be successful if |to_update_pref| and |updated_pref| are
50 // the same.
51 void SetMetricsReporting(bool to_update_pref,
52 const OnMetricsReportingCallbackType& callback_fn,
53 bool updated_pref) {
27 metrics::MetricsService* metrics = g_browser_process->metrics_service(); 54 metrics::MetricsService* metrics = g_browser_process->metrics_service();
28 if (metrics) { 55 if (metrics) {
29 if (enabled) 56 if (updated_pref)
30 metrics->Start(); 57 metrics->Start();
31 else 58 else
32 metrics->Stop(); 59 metrics->Stop();
33 } 60 }
61 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
62 g_browser_process->local_state()->SetBoolean(
63 prefs::kMetricsReportingEnabled, updated_pref);
64 #endif
65 if (to_update_pref == updated_pref) {
66 RecordMetricsReportingHistogramValue(updated_pref ?
67 METRICS_REPORTING_ENABLED : METRICS_REPORTING_DISABLED);
68 } else {
69 RecordMetricsReportingHistogramValue(METRICS_REPORTING_ERROR);
70 }
71 if (!callback_fn.is_null())
72 callback_fn.Run(updated_pref);
73 }
34 74
35 return enabled; 75 } // namespace
76
77 void InitiateMetricsReportingChange(
78 bool enabled,
79 const OnMetricsReportingCallbackType& callback_fn) {
80 #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
81 if (!IsMetricsReportingUserChangable()) {
82 if (!callback_fn.is_null()) {
83 callback_fn.Run(
84 ChromeMetricsServiceAccessor::IsMetricsReportingEnabled());
85 }
86 return;
87 }
88 #endif
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 const PrefService::Preference* pref =
100 pref_service->FindPreference(prefs::kMetricsReportingEnabled);
101 return pref && !pref->IsManaged();
102 }
OLDNEW
« no previous file with comments | « chrome/browser/metrics/metrics_reporting_state.h ('k') | chrome/browser/resources/options/browser_options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698