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

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: 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/browser/chromeos/settings/cros_settings.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"
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_ENABLED,
22 METRICS_REPORTING_DISABLED,
23 METRICS_REPORTING_MAX,
24 };
25
26 void RecordMetricsReportingHistogramValue(
27 MetricsReportingChangeHistogramValue value) {
28 UMA_HISTOGRAM_ENUMERATION(
29 "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();
19 36 bool success = (enabled == updated_pref);
20 if (enabled != update_pref) 37 if (!success)
21 DVLOG(1) << "Unable to set crash report status to " << enabled; 38 DVLOG(1) << "Unable to set crash report status to " << enabled;
22 39
40 return success;
41 }
42
43 // Does the necessary changes for MetricsReportingEnabled changes which needs
44 // to be done in the main thread.
45 void SetMetricsReporting(bool enabled,
46 base::Callback<void(bool)> callback_fn,
47 bool success) {
23 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent 48 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent
24 // succeeds. 49 // succeeds.
25 enabled = update_pref; 50 metrics::MetricsService* metrics = g_browser_process->metrics_service();
51 if (success) {
52 if (metrics) {
53 if (enabled)
54 metrics->Start();
55 else
56 metrics->Stop();
57 }
58 #if !defined(OS_CHROMEOS)
59 g_browser_process->local_state()->SetBoolean(
60 prefs::kMetricsReportingEnabled, enabled);
61 #endif
62 RecordMetricsReportingHistogramValue(enabled? METRICS_REPORTING_ENABLED
Alexei Svitkine (slow) 2014/09/04 18:45:26 Nit: Spaces around : and ?.
gayane -on leave until 09-2017 2014/09/04 21:30:12 Done.
63 :METRICS_REPORTING_DISABLED);
Alexei Svitkine (slow) 2014/09/04 18:45:26 Nit: Put ":" on previous line and align the two co
gayane -on leave until 09-2017 2014/09/04 21:30:12 Done.
64 }
65 if (!callback_fn.is_null())
66 callback_fn.Run(success);
67 }
26 68
27 metrics::MetricsService* metrics = g_browser_process->metrics_service(); 69 } // namespace
28 if (metrics) { 70
29 if (enabled) 71 void InitiateMetricsReportingChange(bool enabled,
30 metrics->Start(); 72 base::Callback<void(bool)> callback_fn) {
31 else 73 if (!IsMetricsReportingUserChangable() && !callback_fn.is_null()) {
Alexei Svitkine (slow) 2014/09/04 18:45:26 If the pref is changeable, we should do an early r
gayane -on leave until 09-2017 2014/09/04 21:30:12 Done.
32 metrics->Stop(); 74 callback_fn.Run(false);
75 return;
33 } 76 }
34 77
35 return enabled; 78 // Posts on FILE thread as SetGoogleUpdateSettings modifies the local state.
79 content::BrowserThread::PostTaskAndReplyWithResult(
80 content::BrowserThread::FILE,
81 FROM_HERE,
82 base::Bind(&SetGoogleUpdateSettings, enabled),
83 base::Bind(&SetMetricsReporting, enabled, callback_fn));
36 } 84 }
85
86 bool IsMetricsReportingEnabled() {
87 const PrefService* pref_service = g_browser_process->local_state();
88 return pref_service->GetBoolean(prefs::kMetricsReportingEnabled);
89 }
90
91 bool IsMetricsReportingUserChangable() {
92 const PrefService* pref_service = g_browser_process->local_state();
93
94 const PrefService::Preference* pref = pref_service->FindPreference(
Alexei Svitkine (slow) 2014/09/04 18:45:26 Nit: Wrap after = instead.
gayane -on leave until 09-2017 2014/09/04 21:30:12 Done.
95 prefs::kMetricsReportingEnabled);
96 return pref && pref->IsManaged();
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698