blob: ef4c99353f3d5d85da7210d4bacfab35671b9229 [file] [log] [blame]
[email protected]c3cac952014-05-09 01:51:181// 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.
4
blundell695d61f2015-10-21 11:25:535#include "components/metrics_services_manager/metrics_services_manager.h"
[email protected]c3cac952014-05-09 01:51:186
dcheng51606352015-12-26 21:16:237#include <utility>
8
asvitkinede445d92017-06-14 21:37:589#include "base/command_line.h"
[email protected]8e885de2014-07-22 23:36:5310#include "base/logging.h"
[email protected]d6147bd2014-06-11 01:58:1911#include "components/metrics/metrics_service.h"
blundellfecea528d2015-10-21 10:10:2212#include "components/metrics/metrics_service_client.h"
[email protected]16a30912014-06-04 00:20:0413#include "components/metrics/metrics_state_manager.h"
asvitkinede445d92017-06-14 21:37:5814#include "components/metrics/metrics_switches.h"
blundell695d61f2015-10-21 11:25:5315#include "components/metrics_services_manager/metrics_services_manager_client.h"
nzolghadrd87a308d2016-12-07 15:45:5616#include "components/rappor/rappor_service_impl.h"
holte7b74c622017-01-23 23:13:0717#include "components/ukm/ukm_service.h"
blundell0300cdea2015-09-03 12:47:4418#include "components/variations/service/variations_service.h"
[email protected]c3cac952014-05-09 01:51:1819
blundell695d61f2015-10-21 11:25:5320namespace metrics_services_manager {
21
blundellfecea528d2015-10-21 10:10:2222MetricsServicesManager::MetricsServicesManager(
dcheng84c358e2016-04-26 07:05:5323 std::unique_ptr<MetricsServicesManagerClient> client)
dcheng51606352015-12-26 21:16:2324 : client_(std::move(client)), may_upload_(false), may_record_(false) {
blundellfecea528d2015-10-21 10:10:2225 DCHECK(client_);
[email protected]c3cac952014-05-09 01:51:1826}
27
blundell695d61f2015-10-21 11:25:5328MetricsServicesManager::~MetricsServicesManager() {}
[email protected]c3cac952014-05-09 01:51:1829
robliao4bee5d92016-09-15 14:37:3730std::unique_ptr<const base::FieldTrial::EntropyProvider>
31MetricsServicesManager::CreateEntropyProvider() {
32 return client_->CreateEntropyProvider();
33}
34
asvitkinecbd420732014-08-26 22:15:4035metrics::MetricsService* MetricsServicesManager::GetMetricsService() {
[email protected]c3cac952014-05-09 01:51:1836 DCHECK(thread_checker_.CalledOnValidThread());
blundellfecea528d2015-10-21 10:10:2237 return GetMetricsServiceClient()->GetMetricsService();
[email protected]c3cac952014-05-09 01:51:1838}
39
nzolghadrd87a308d2016-12-07 15:45:5640rappor::RapporServiceImpl* MetricsServicesManager::GetRapporServiceImpl() {
[email protected]c3cac952014-05-09 01:51:1841 DCHECK(thread_checker_.CalledOnValidThread());
holte5a7ed7c2015-01-09 23:52:4642 if (!rappor_service_) {
nzolghadrd87a308d2016-12-07 15:45:5643 rappor_service_ = client_->CreateRapporServiceImpl();
blundellfecea528d2015-10-21 10:10:2244 rappor_service_->Initialize(client_->GetURLRequestContext());
holte5a7ed7c2015-01-09 23:52:4645 }
[email protected]c3cac952014-05-09 01:51:1846 return rappor_service_.get();
47}
48
holte7b74c622017-01-23 23:13:0749ukm::UkmService* MetricsServicesManager::GetUkmService() {
50 DCHECK(thread_checker_.CalledOnValidThread());
51 return GetMetricsServiceClient()->GetUkmService();
52}
53
blundell57bcfed2015-09-04 08:44:4554variations::VariationsService* MetricsServicesManager::GetVariationsService() {
[email protected]c3cac952014-05-09 01:51:1855 DCHECK(thread_checker_.CalledOnValidThread());
blundellfecea528d2015-10-21 10:10:2256 if (!variations_service_)
57 variations_service_ = client_->CreateVariationsService();
[email protected]c3cac952014-05-09 01:51:1858 return variations_service_.get();
59}
60
[email protected]544246e2014-06-06 11:22:2861void MetricsServicesManager::OnPluginLoadingError(
62 const base::FilePath& plugin_path) {
blundellfecea528d2015-10-21 10:10:2263 GetMetricsServiceClient()->OnPluginLoadingError(plugin_path);
[email protected]4a55a712014-06-08 16:50:3464}
65
blundell6eb91b7b2015-10-23 07:04:4366void MetricsServicesManager::OnRendererProcessCrash() {
67 GetMetricsServiceClient()->OnRendererProcessCrash();
68}
69
blundellfecea528d2015-10-21 10:10:2270metrics::MetricsServiceClient*
71MetricsServicesManager::GetMetricsServiceClient() {
[email protected]4a55a712014-06-08 16:50:3472 DCHECK(thread_checker_.CalledOnValidThread());
holte1334c0aa2017-02-09 22:52:4173 if (!metrics_service_client_) {
blundellfecea528d2015-10-21 10:10:2274 metrics_service_client_ = client_->CreateMetricsServiceClient();
holte1334c0aa2017-02-09 22:52:4175 // base::Unretained is safe since |this| owns the metrics_service_client_.
76 metrics_service_client_->SetUpdateRunningServicesCallback(
77 base::Bind(&MetricsServicesManager::UpdateRunningServices,
78 base::Unretained(this)));
79 }
[email protected]4a55a712014-06-08 16:50:3480 return metrics_service_client_.get();
[email protected]544246e2014-06-06 11:22:2881}
82
holte1334c0aa2017-02-09 22:52:4183void MetricsServicesManager::UpdatePermissions(bool current_may_record,
84 bool current_may_upload) {
holte585f4662015-06-18 19:13:5285 DCHECK(thread_checker_.CalledOnValidThread());
holte1334c0aa2017-02-09 22:52:4186 // If the user has opted out of metrics, delete local UKM state.
87 if (may_record_ && !current_may_record) {
88 ukm::UkmService* ukm = GetUkmService();
89 if (ukm) {
90 ukm->Purge();
91 ukm->ResetClientId();
92 }
93 }
94
nzolghadrd87a308d2016-12-07 15:45:5695 // Stash the current permissions so that we can update the RapporServiceImpl
holte873fb7c92015-02-19 23:41:2696 // correctly when the Rappor preference changes. The metrics recording
97 // preference partially determines the initial rappor setting, and also
98 // controls whether FINE metrics are sent.
holte1334c0aa2017-02-09 22:52:4199 may_record_ = current_may_record;
100 may_upload_ = current_may_upload;
holte585f4662015-06-18 19:13:52101 UpdateRunningServices();
102}
holte873fb7c92015-02-19 23:41:26103
holte585f4662015-06-18 19:13:52104void MetricsServicesManager::UpdateRunningServices() {
105 DCHECK(thread_checker_.CalledOnValidThread());
holte5a7ed7c2015-01-09 23:52:46106 metrics::MetricsService* metrics = GetMetricsService();
107
asvitkinede445d92017-06-14 21:37:58108 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
109 if (cmdline->HasSwitch(metrics::switches::kMetricsRecordingOnly)) {
holte5a7ed7c2015-01-09 23:52:46110 metrics->StartRecordingForTests();
holte0036d9b2017-03-15 21:49:16111 GetRapporServiceImpl()->Update(true, false);
holte5a7ed7c2015-01-09 23:52:46112 return;
113 }
114
jwdc882e48d2016-08-19 00:14:42115 client_->UpdateRunningServices(may_record_, may_upload_);
116
holte585f4662015-06-18 19:13:52117 if (may_record_) {
holte5a7ed7c2015-01-09 23:52:46118 if (!metrics->recording_active())
119 metrics->Start();
holte1334c0aa2017-02-09 22:52:41120 if (may_upload_)
holte5a7ed7c2015-01-09 23:52:46121 metrics->EnableReporting();
holte1334c0aa2017-02-09 22:52:41122 else
holte5a7ed7c2015-01-09 23:52:46123 metrics->DisableReporting();
wittman622851e2015-07-31 18:13:40124 } else {
holte5a7ed7c2015-01-09 23:52:46125 metrics->Stop();
126 }
127
holte1334c0aa2017-02-09 22:52:41128 UpdateUkmService();
129
holte0036d9b2017-03-15 21:49:16130 GetRapporServiceImpl()->Update(may_record_, may_upload_);
holte5a7ed7c2015-01-09 23:52:46131}
132
holte1334c0aa2017-02-09 22:52:41133void MetricsServicesManager::UpdateUkmService() {
134 ukm::UkmService* ukm = GetUkmService();
135 if (!ukm)
136 return;
137 bool sync_enabled =
holte8d28e6692017-05-04 23:50:43138 client_->IsMetricsReportingForceEnabled() ||
holte1334c0aa2017-02-09 22:52:41139 metrics_service_client_->IsHistorySyncEnabledOnAllProfiles();
140 if (may_record_ && sync_enabled) {
141 ukm->EnableRecording();
142 if (may_upload_)
143 ukm->EnableReporting();
144 else
145 ukm->DisableReporting();
146 } else {
147 ukm->DisableRecording();
148 ukm->DisableReporting();
149 }
150}
151
anthonyvd40dd0302015-02-19 16:11:34152void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) {
holte8d28e6692017-05-04 23:50:43153 UpdatePermissions((client_->IsMetricsReportingForceEnabled() ||
154 client_->IsMetricsReportingEnabled()),
155 client_->IsMetricsReportingForceEnabled() || may_upload);
[email protected]3c70256f2014-05-22 03:02:12156}
blundell695d61f2015-10-21 11:25:53157
158} // namespace metrics_services_manager