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

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: access to prefs moved from browser_options_handle to metrics_reposting_state 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 {
Alexei Svitkine (slow) 2014/09/04 13:58:03 Nit: Add a blank line after this.
gayane -on leave until 09-2017 2014/09/04 18:35:47 Done.
13 // GoogleUpdateSettings touches the disk from the UI thread. MetricsService 19 enum MetricsReportingChangeHistogramValue {
14 // also calls GoogleUpdateSettings below. http://crbug/62626 20 METRICS_REPORTING_ENABLED,
15 base::ThreadRestrictions::ScopedAllowIO allow_io; 21 METRICS_REPORTING_DISABLED,
22 METRICS_REPORTING_MAX,
23 };
16 24
25 void RecordMetricsReportingHistogramValue(
26 MetricsReportingChangeHistogramValue value) {
27 UMA_HISTOGRAM_ENUMERATION(
28 "MetricsReporting", value, METRICS_REPORTING_MAX);
29 }
30
31 // Tries to set crash stats upload consent to |enabled|.
32 bool SetGoogleUpdateSettings(bool enabled) {
17 GoogleUpdateSettings::SetCollectStatsConsent(enabled); 33 GoogleUpdateSettings::SetCollectStatsConsent(enabled);
18 bool update_pref = GoogleUpdateSettings::GetCollectStatsConsent(); 34 bool updated_pref = GoogleUpdateSettings::GetCollectStatsConsent();
19 35 bool success = (enabled == updated_pref);
20 if (enabled != update_pref) 36 if (!success)
21 DVLOG(1) << "Unable to set crash report status to " << enabled; 37 DVLOG(1) << "Unable to set crash report status to " << enabled;
22 38
39 return success;
40 }
41 // Does the necessary changes for MetricsReportingEnabled changes which needs
Alexei Svitkine (slow) 2014/09/04 13:58:03 Nit: Add a blank line before this.
gayane -on leave until 09-2017 2014/09/04 18:35:46 Done.
42 // to be done in the main thread.
43 void SetMetricsReporting(base::Callback<void(bool)> callback_fn,
44 bool enabled, bool success) {
Alexei Svitkine (slow) 2014/09/04 13:58:03 Nit: 1 param per line. Align params.
gayane -on leave until 09-2017 2014/09/04 18:35:47 Done.
23 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent 45 // Only change the pref if GoogleUpdateSettings::GetCollectStatsConsent
24 // succeeds. 46 // succeeds.
25 enabled = update_pref; 47 metrics::MetricsService* metrics = g_browser_process->metrics_service();
48 if (success) {
49 if (metrics) {
50 if (enabled)
51 metrics->Start();
52 else
53 metrics->Stop();
54 }
55 #if !defined(OS_CHROMEOS)
56 g_browser_process->local_state()->SetBoolean(
57 prefs::kMetricsReportingEnabled, enabled);
Alexei Svitkine (slow) 2014/09/04 13:58:03 Nit: Indent 2 more. But also, I think before this
gayane -on leave until 09-2017 2014/09/04 18:35:47 Added a check in InitiateMetricsReportingChange
58 #endif
59 RecordMetricsReportingHistogramValue(enabled? METRICS_REPORTING_ENABLED
60 :METRICS_REPORTING_DISABLED);
61 }
62 if (!callback_fn.is_null())
63 callback_fn.Run(success);
64 }
65 } // namespace
Alexei Svitkine (slow) 2014/09/04 13:58:03 Nit: Add a blank line before this.
gayane -on leave until 09-2017 2014/09/04 18:35:47 Done.
26 66
27 metrics::MetricsService* metrics = g_browser_process->metrics_service(); 67 void InitiateMetricsReportingChange(
28 if (metrics) { 68 base::Callback<void(bool)> callback_fn, bool enabled) {
Alexei Svitkine (slow) 2014/09/04 13:58:03 Nit: 1 param per line.
gayane -on leave until 09-2017 2014/09/04 18:35:47 Done.
29 if (enabled) 69 content::BrowserThread::PostTaskAndReplyWithResult(
Alexei Svitkine (slow) 2014/09/04 13:58:03 Add a comment why this is posted to the FILE threa
gayane -on leave until 09-2017 2014/09/04 18:35:47 Done.
30 metrics->Start(); 70 content::BrowserThread::FILE,
31 else 71 FROM_HERE,
32 metrics->Stop(); 72 base::Bind(&SetGoogleUpdateSettings, enabled),
33 } 73 base::Bind(&SetMetricsReporting, callback_fn, enabled));
74 }
34 75
35 return enabled; 76 bool IsMetricsReportingEnabled() {
77 const PrefService* pref_service = g_browser_process->local_state();
78 return pref_service->GetBoolean(prefs::kMetricsReportingEnabled);
36 } 79 }
80
81 bool IsMetricsReportingUserChangable() {
82 const PrefService* pref_service = g_browser_process->local_state();
83
84 const PrefService::Preference* pref = pref_service->FindPreference(
85 prefs::kMetricsReportingEnabled);
86 return pref && pref->IsManaged();
87 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698