Make castv2 performance test work.
[chromium-blink-merge.git] / chrome / browser / metrics / metrics_services_manager.cc
blob8c68d0453d70451ab92f965b12dd86b3531a0089
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/browser_process.h"
13 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
14 #include "chrome/browser/metrics/chrome_metrics_service_client.h"
15 #include "chrome/browser/metrics/metrics_reporting_state.h"
16 #include "chrome/browser/metrics/variations/variations_service.h"
17 #include "chrome/browser/ui/browser_otr_state.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/installer/util/google_update_settings.h"
21 #include "components/metrics/metrics_service.h"
22 #include "components/metrics/metrics_state_manager.h"
23 #include "components/rappor/rappor_service.h"
24 #include "content/public/browser/browser_thread.h"
26 #if defined(OS_CHROMEOS)
27 #include "chrome/browser/chromeos/settings/cros_settings.h"
28 #endif
30 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread
31 // because it needs access to IO and cannot work from UI thread.
32 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) {
33 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE,
34 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info));
37 MetricsServicesManager::MetricsServicesManager(PrefService* local_state)
38 : local_state_(local_state),
39 may_upload_(false),
40 may_record_(false) {
41 DCHECK(local_state);
42 pref_change_registrar_.Init(local_state);
45 MetricsServicesManager::~MetricsServicesManager() {
48 metrics::MetricsService* MetricsServicesManager::GetMetricsService() {
49 DCHECK(thread_checker_.CalledOnValidThread());
50 return GetChromeMetricsServiceClient()->metrics_service();
53 rappor::RapporService* MetricsServicesManager::GetRapporService() {
54 DCHECK(thread_checker_.CalledOnValidThread());
55 if (!rappor_service_) {
56 rappor_service_.reset(new rappor::RapporService(
57 local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive)));
58 rappor_service_->Initialize(g_browser_process->system_request_context());
60 return rappor_service_.get();
63 chrome_variations::VariationsService*
64 MetricsServicesManager::GetVariationsService() {
65 DCHECK(thread_checker_.CalledOnValidThread());
66 if (!variations_service_) {
67 variations_service_ =
68 chrome_variations::VariationsService::Create(local_state_,
69 GetMetricsStateManager());
71 return variations_service_.get();
74 void MetricsServicesManager::OnPluginLoadingError(
75 const base::FilePath& plugin_path) {
76 GetChromeMetricsServiceClient()->LogPluginLoadingError(plugin_path);
79 ChromeMetricsServiceClient*
80 MetricsServicesManager::GetChromeMetricsServiceClient() {
81 DCHECK(thread_checker_.CalledOnValidThread());
82 if (!metrics_service_client_) {
83 metrics_service_client_ = ChromeMetricsServiceClient::Create(
84 GetMetricsStateManager(), local_state_);
86 return metrics_service_client_.get();
89 metrics::MetricsStateManager* MetricsServicesManager::GetMetricsStateManager() {
90 DCHECK(thread_checker_.CalledOnValidThread());
91 if (!metrics_state_manager_) {
92 metrics_state_manager_ = metrics::MetricsStateManager::Create(
93 local_state_,
94 base::Bind(&ChromeMetricsServiceAccessor::IsMetricsReportingEnabled),
95 base::Bind(&PostStoreMetricsClientInfo),
96 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo));
98 return metrics_state_manager_.get();
101 void MetricsServicesManager::UpdatePermissions(bool may_record,
102 bool may_upload) {
103 // Stash the current permissions so that we can update the RapporService
104 // correctly when the Rappor preference changes. The metrics recording
105 // preference partially determines the initial rappor setting, and also
106 // controls whether FINE metrics are sent.
107 may_record_ = may_record;
108 may_upload_ = may_upload;
110 metrics::MetricsService* metrics = GetMetricsService();
112 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
114 const bool only_do_metrics_recording =
115 cmdline->HasSwitch(switches::kMetricsRecordingOnly) ||
116 cmdline->HasSwitch(switches::kEnableBenchmarking);
118 if (only_do_metrics_recording) {
119 metrics->StartRecordingForTests();
120 GetRapporService()->Update(rappor::FINE_LEVEL, false);
121 return;
124 if (may_record) {
125 if (!metrics->recording_active())
126 metrics->Start();
128 if (may_upload)
129 metrics->EnableReporting();
130 else
131 metrics->DisableReporting();
132 } else if (metrics->recording_active() || metrics->reporting_active()) {
133 metrics->Stop();
136 rappor::RecordingLevel recording_level = rappor::RECORDING_DISABLED;
137 #if defined(GOOGLE_CHROME_BUILD)
138 if (may_record)
139 recording_level = rappor::FINE_LEVEL;
140 #endif // defined(GOOGLE_CHROME_BUILD)
141 GetRapporService()->Update(recording_level, may_upload);
144 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) {
145 return UpdatePermissions(
146 ChromeMetricsServiceAccessor::IsMetricsReportingEnabled(), may_upload);