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

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: Return updated state instead of success identifier + minor changes 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/common/pref_names.h"
9 #include "chrome/installer/util/google_update_settings.h" 12 #include "chrome/installer/util/google_update_settings.h"
13 #include "chromeos/settings/cros_settings_names.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", value, METRICS_REPORTING_MAX);
30 }
31
32 // Tries to set crash stats upload consent to |enabled|.
33 bool SetGoogleUpdateSettings(bool enabled) {
17 GoogleUpdateSettings::SetCollectStatsConsent(enabled); 34 GoogleUpdateSettings::SetCollectStatsConsent(enabled);
18 bool update_pref = GoogleUpdateSettings::GetCollectStatsConsent(); 35 bool updated_pref = GoogleUpdateSettings::GetCollectStatsConsent();
36 if (enabled != updated_pref)
37 DVLOG(1) << "Unable to set metrics reporting status to " << enabled;
19 38
20 if (enabled != update_pref) 39 return updated_pref;
21 DVLOG(1) << "Unable to set crash report status to " << enabled; 40 }
22 41
42 // Does the necessary changes for MetricsReportingEnabled changes which needs
43 // to be done in the main thread.
44 void SetMetricsReporting(bool enabled,
45 const base::Callback<void(bool)> callback_fn,
46 bool updated_pref) {
23 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent 47 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent
24 // succeeds. 48 // succeeds.
25 enabled = update_pref;
26
27 metrics::MetricsService* metrics = g_browser_process->metrics_service(); 49 metrics::MetricsService* metrics = g_browser_process->metrics_service();
28 if (metrics) { 50 if (metrics) {
29 if (enabled) 51 if (updated_pref)
30 metrics->Start(); 52 metrics->Start();
31 else 53 else
32 metrics->Stop(); 54 metrics->Stop();
33 } 55 }
56 #if !defined(OS_CHROMEOS)
57 g_browser_process->local_state()->SetBoolean(
58 prefs::kMetricsReportingEnabled, enabled);
59 #endif
60 if (enabled == updated_pref) {
61 RecordMetricsReportingHistogramValue(enabled ? METRICS_REPORTING_ENABLED :
62 METRICS_REPORTING_DISABLED);
63 }
64 else {
Alexei Svitkine (slow) 2014/09/09 15:27:15 Nit: Put on same line as }
gayane -on leave until 09-2017 2014/09/09 19:40:51 Done.
65 RecordMetricsReportingHistogramValue(METRICS_REPORTING_ERROR);
66 }
67 if (!callback_fn.is_null())
68 callback_fn.Run(updated_pref);
69 }
34 70
35 return enabled; 71 } // namespace
72
73 void InitiateMetricsReportingChange(
74 bool enabled, const base::Callback<void(bool)> callback_fn) {
Alexei Svitkine (slow) 2014/09/09 15:27:15 If you're wrapping the first param, then it should
gayane -on leave until 09-2017 2014/09/09 19:40:51 Done.
75 if (!IsMetricsReportingUserChangable()) {
76 if (!callback_fn.is_null())
77 callback_fn.Run(false);
78 return;
79 }
80
81 // Posts on FILE thread as SetGoogleUpdateSettings does IO operations.
Alexei Svitkine (slow) 2014/09/09 15:27:15 Nit: "Posts on" -> "Posts to".
gayane -on leave until 09-2017 2014/09/09 19:40:51 Done.
82 content::BrowserThread::PostTaskAndReplyWithResult(
83 content::BrowserThread::FILE,
84 FROM_HERE,
85 base::Bind(&SetGoogleUpdateSettings, enabled),
86 base::Bind(&SetMetricsReporting, enabled, callback_fn));
36 } 87 }
88
89 bool IsMetricsReportingUserChangable() {
90 const PrefService* pref_service = g_browser_process->local_state();
91
92 const PrefService::Preference* pref =
93 pref_service->FindPreference(prefs::kMetricsReportingEnabled);
94 return pref && pref->IsManaged();
95 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698