Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / metrics / metrics_services_manager.cc
blobf6c1f1f117a211dd71db87cf7895716c703c300c
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/chrome_variations_service_client.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/ui/browser_otr_state.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/installer/util/google_update_settings.h"
23 #include "components/metrics/metrics_service.h"
24 #include "components/metrics/metrics_state_manager.h"
25 #include "components/rappor/rappor_service.h"
26 #include "components/variations/service/variations_service.h"
27 #include "content/public/browser/browser_thread.h"
29 #if defined(OS_CHROMEOS)
30 #include "chrome/browser/chromeos/settings/cros_settings.h"
31 #endif
33 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread
34 // because it needs access to IO and cannot work from UI thread.
35 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) {
36 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE,
37 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info));
40 MetricsServicesManager::MetricsServicesManager(PrefService* local_state)
41 : local_state_(local_state),
42 may_upload_(false),
43 may_record_(false) {
44 DCHECK(local_state);
47 MetricsServicesManager::~MetricsServicesManager() {
50 metrics::MetricsService* MetricsServicesManager::GetMetricsService() {
51 DCHECK(thread_checker_.CalledOnValidThread());
52 return GetChromeMetricsServiceClient()->metrics_service();
55 rappor::RapporService* MetricsServicesManager::GetRapporService() {
56 DCHECK(thread_checker_.CalledOnValidThread());
57 if (!rappor_service_) {
58 rappor_service_.reset(new rappor::RapporService(
59 local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive)));
60 rappor_service_->Initialize(g_browser_process->system_request_context());
62 return rappor_service_.get();
65 variations::VariationsService* MetricsServicesManager::GetVariationsService() {
66 DCHECK(thread_checker_.CalledOnValidThread());
67 if (!variations_service_) {
68 variations_service_ = variations::VariationsService::Create(
69 make_scoped_ptr(new ChromeVariationsServiceClient()), local_state_,
70 GetMetricsStateManager(), switches::kDisableBackgroundNetworking);
72 return variations_service_.get();
75 void MetricsServicesManager::OnPluginLoadingError(
76 const base::FilePath& plugin_path) {
77 GetChromeMetricsServiceClient()->LogPluginLoadingError(plugin_path);
80 ChromeMetricsServiceClient*
81 MetricsServicesManager::GetChromeMetricsServiceClient() {
82 DCHECK(thread_checker_.CalledOnValidThread());
83 if (!metrics_service_client_) {
84 metrics_service_client_ = ChromeMetricsServiceClient::Create(
85 GetMetricsStateManager(), local_state_);
87 return metrics_service_client_.get();
90 metrics::MetricsStateManager* MetricsServicesManager::GetMetricsStateManager() {
91 DCHECK(thread_checker_.CalledOnValidThread());
92 if (!metrics_state_manager_) {
93 metrics_state_manager_ = metrics::MetricsStateManager::Create(
94 local_state_,
95 base::Bind(
96 &ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled),
97 base::Bind(&PostStoreMetricsClientInfo),
98 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo));
100 return metrics_state_manager_.get();
103 bool MetricsServicesManager::GetSafeBrowsingState() {
104 // Start listening for updates to SB service state. This is done here instead
105 // of in the constructor to avoid errors from trying to instantiate SB
106 // service before the IO thread exists.
107 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service();
108 if (!sb_state_subscription_ && sb_service) {
109 // base::Unretained(this) is safe here since this object owns the
110 // sb_state_subscription_ which owns the pointer.
111 sb_state_subscription_ = sb_service->RegisterStateCallback(
112 base::Bind(&MetricsServicesManager::UpdateRunningServices,
113 base::Unretained(this)));
116 return sb_service && sb_service->enabled_by_prefs();
119 void MetricsServicesManager::UpdatePermissions(bool may_record,
120 bool may_upload) {
121 DCHECK(thread_checker_.CalledOnValidThread());
122 // Stash the current permissions so that we can update the RapporService
123 // correctly when the Rappor preference changes. The metrics recording
124 // preference partially determines the initial rappor setting, and also
125 // controls whether FINE metrics are sent.
126 may_record_ = may_record;
127 may_upload_ = may_upload;
128 UpdateRunningServices();
131 void MetricsServicesManager::UpdateRunningServices() {
132 DCHECK(thread_checker_.CalledOnValidThread());
133 metrics::MetricsService* metrics = GetMetricsService();
135 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
137 const bool only_do_metrics_recording =
138 cmdline->HasSwitch(switches::kMetricsRecordingOnly) ||
139 cmdline->HasSwitch(switches::kEnableBenchmarking);
141 if (only_do_metrics_recording) {
142 metrics->StartRecordingForTests();
143 GetRapporService()->Update(
144 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP,
145 false);
146 return;
149 if (may_record_) {
150 if (!metrics->recording_active())
151 metrics->Start();
153 if (may_upload_)
154 metrics->EnableReporting();
155 else
156 metrics->DisableReporting();
157 } else {
158 metrics->Stop();
161 int recording_groups = 0;
162 #if defined(GOOGLE_CHROME_BUILD)
163 if (may_record_)
164 recording_groups |= rappor::UMA_RAPPOR_GROUP;
165 if (GetSafeBrowsingState())
166 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP;
167 #endif // defined(GOOGLE_CHROME_BUILD)
168 GetRapporService()->Update(recording_groups, may_upload_);
171 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) {
172 return UpdatePermissions(
173 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled(),
174 may_upload);