1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/metrics/metrics_services_manager.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/metrics/chrome_metrics_service_client.h"
13 #include "chrome/browser/metrics/variations/variations_service.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/installer/util/google_update_settings.h"
17 #include "components/metrics/metrics_service.h"
18 #include "components/metrics/metrics_state_manager.h"
19 #include "components/rappor/rappor_service.h"
21 #if defined(OS_CHROMEOS)
22 #include "chrome/browser/chromeos/settings/cros_settings.h"
25 MetricsServicesManager::MetricsServicesManager(PrefService
* local_state
)
26 : local_state_(local_state
) {
30 MetricsServicesManager::~MetricsServicesManager() {
33 metrics::MetricsService
* MetricsServicesManager::GetMetricsService() {
34 DCHECK(thread_checker_
.CalledOnValidThread());
35 return GetChromeMetricsServiceClient()->metrics_service();
38 rappor::RapporService
* MetricsServicesManager::GetRapporService() {
39 DCHECK(thread_checker_
.CalledOnValidThread());
41 rappor_service_
.reset(new rappor::RapporService(local_state_
));
42 return rappor_service_
.get();
45 chrome_variations::VariationsService
*
46 MetricsServicesManager::GetVariationsService() {
47 DCHECK(thread_checker_
.CalledOnValidThread());
48 if (!variations_service_
) {
50 chrome_variations::VariationsService::Create(local_state_
,
51 GetMetricsStateManager());
53 return variations_service_
.get();
56 void MetricsServicesManager::OnPluginLoadingError(
57 const base::FilePath
& plugin_path
) {
58 GetChromeMetricsServiceClient()->LogPluginLoadingError(plugin_path
);
61 ChromeMetricsServiceClient
*
62 MetricsServicesManager::GetChromeMetricsServiceClient() {
63 DCHECK(thread_checker_
.CalledOnValidThread());
64 if (!metrics_service_client_
) {
65 metrics_service_client_
= ChromeMetricsServiceClient::Create(
66 GetMetricsStateManager(), local_state_
);
68 return metrics_service_client_
.get();
71 metrics::MetricsStateManager
* MetricsServicesManager::GetMetricsStateManager() {
72 DCHECK(thread_checker_
.CalledOnValidThread());
73 if (!metrics_state_manager_
) {
74 metrics_state_manager_
= metrics::MetricsStateManager::Create(
76 base::Bind(&MetricsServicesManager::IsMetricsReportingEnabled
,
77 base::Unretained(this)),
78 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo
),
79 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo
));
81 return metrics_state_manager_
.get();
84 // TODO(asvitkine): This function does not report the correct value on Android,
85 // see http://crbug.com/362192.
86 bool MetricsServicesManager::IsMetricsReportingEnabled() const {
87 // If the user permits metrics reporting with the checkbox in the
88 // prefs, we turn on recording. We disable metrics completely for
89 // non-official builds, or when field trials are forced.
90 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kForceFieldTrials
))
94 #if defined(GOOGLE_CHROME_BUILD)
95 #if defined(OS_CHROMEOS)
96 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref
,
99 enabled
= local_state_
->GetBoolean(prefs::kMetricsReportingEnabled
);
100 #endif // #if defined(OS_CHROMEOS)
101 #endif // defined(GOOGLE_CHROME_BUILD)