Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / metrics / metrics_services_manager.cc
blob8465c0f87e42c529ab404817d12f6f334a9b9efe
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"
7 #include <string>
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/browser/ui/browser_otr_state.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/installer/util/google_update_settings.h"
18 #include "components/metrics/metrics_service.h"
19 #include "components/metrics/metrics_state_manager.h"
20 #include "components/rappor/rappor_service.h"
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/chromeos/settings/cros_settings.h"
24 #endif
26 MetricsServicesManager::MetricsServicesManager(PrefService* local_state)
27 : local_state_(local_state) {
28 DCHECK(local_state);
31 MetricsServicesManager::~MetricsServicesManager() {
34 metrics::MetricsService* MetricsServicesManager::GetMetricsService() {
35 DCHECK(thread_checker_.CalledOnValidThread());
36 return GetChromeMetricsServiceClient()->metrics_service();
39 rappor::RapporService* MetricsServicesManager::GetRapporService() {
40 DCHECK(thread_checker_.CalledOnValidThread());
41 if (!rappor_service_)
42 rappor_service_.reset(new rappor::RapporService(
43 local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive)));
44 return rappor_service_.get();
47 chrome_variations::VariationsService*
48 MetricsServicesManager::GetVariationsService() {
49 DCHECK(thread_checker_.CalledOnValidThread());
50 if (!variations_service_) {
51 variations_service_ =
52 chrome_variations::VariationsService::Create(local_state_,
53 GetMetricsStateManager());
55 return variations_service_.get();
58 void MetricsServicesManager::OnPluginLoadingError(
59 const base::FilePath& plugin_path) {
60 GetChromeMetricsServiceClient()->LogPluginLoadingError(plugin_path);
63 ChromeMetricsServiceClient*
64 MetricsServicesManager::GetChromeMetricsServiceClient() {
65 DCHECK(thread_checker_.CalledOnValidThread());
66 if (!metrics_service_client_) {
67 metrics_service_client_ = ChromeMetricsServiceClient::Create(
68 GetMetricsStateManager(), local_state_);
70 return metrics_service_client_.get();
73 metrics::MetricsStateManager* MetricsServicesManager::GetMetricsStateManager() {
74 DCHECK(thread_checker_.CalledOnValidThread());
75 if (!metrics_state_manager_) {
76 metrics_state_manager_ = metrics::MetricsStateManager::Create(
77 local_state_,
78 base::Bind(&MetricsServicesManager::IsMetricsReportingEnabled,
79 base::Unretained(this)),
80 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo),
81 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo));
83 return metrics_state_manager_.get();
86 // TODO(asvitkine): This function does not report the correct value on Android,
87 // see http://crbug.com/362192.
88 bool MetricsServicesManager::IsMetricsReportingEnabled() const {
89 // If the user permits metrics reporting with the checkbox in the
90 // prefs, we turn on recording. We disable metrics completely for
91 // non-official builds, or when field trials are forced.
92 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kForceFieldTrials))
93 return false;
95 bool enabled = false;
96 #if defined(GOOGLE_CHROME_BUILD)
97 #if defined(OS_CHROMEOS)
98 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref,
99 &enabled);
100 #else
101 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled);
102 #endif // #if defined(OS_CHROMEOS)
103 #endif // defined(GOOGLE_CHROME_BUILD)
104 return enabled;