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/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"
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
),
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(
95 base::Bind(&ChromeMetricsServiceAccessor::IsMetricsReportingEnabled
),
96 base::Bind(&PostStoreMetricsClientInfo
),
97 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo
));
99 return metrics_state_manager_
.get();
102 bool MetricsServicesManager::GetSafeBrowsingState() {
103 // Start listening for updates to SB service state. This is done here instead
104 // of in the constructor to avoid errors from trying to instantiate SB
105 // service before the IO thread exists.
106 SafeBrowsingService
* sb_service
= g_browser_process
->safe_browsing_service();
107 if (!sb_state_subscription_
&& sb_service
) {
108 // base::Unretained(this) is safe here since this object owns the
109 // sb_state_subscription_ which owns the pointer.
110 sb_state_subscription_
= sb_service
->RegisterStateCallback(
111 base::Bind(&MetricsServicesManager::UpdateRunningServices
,
112 base::Unretained(this)));
115 return sb_service
&& sb_service
->enabled_by_prefs();
118 void MetricsServicesManager::UpdatePermissions(bool may_record
,
120 DCHECK(thread_checker_
.CalledOnValidThread());
121 // Stash the current permissions so that we can update the RapporService
122 // correctly when the Rappor preference changes. The metrics recording
123 // preference partially determines the initial rappor setting, and also
124 // controls whether FINE metrics are sent.
125 may_record_
= may_record
;
126 may_upload_
= may_upload
;
127 UpdateRunningServices();
130 void MetricsServicesManager::UpdateRunningServices() {
131 DCHECK(thread_checker_
.CalledOnValidThread());
132 metrics::MetricsService
* metrics
= GetMetricsService();
134 const base::CommandLine
* cmdline
= base::CommandLine::ForCurrentProcess();
136 const bool only_do_metrics_recording
=
137 cmdline
->HasSwitch(switches::kMetricsRecordingOnly
) ||
138 cmdline
->HasSwitch(switches::kEnableBenchmarking
);
140 if (only_do_metrics_recording
) {
141 metrics
->StartRecordingForTests();
142 GetRapporService()->Update(
143 rappor::UMA_RAPPOR_GROUP
| rappor::SAFEBROWSING_RAPPOR_GROUP
,
149 if (!metrics
->recording_active())
153 metrics
->EnableReporting();
155 metrics
->DisableReporting();
160 int recording_groups
= 0;
161 #if defined(GOOGLE_CHROME_BUILD)
163 recording_groups
|= rappor::UMA_RAPPOR_GROUP
;
164 if (GetSafeBrowsingState())
165 recording_groups
|= rappor::SAFEBROWSING_RAPPOR_GROUP
;
166 #endif // defined(GOOGLE_CHROME_BUILD)
167 GetRapporService()->Update(recording_groups
, may_upload_
);
170 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload
) {
171 return UpdatePermissions(
172 ChromeMetricsServiceAccessor::IsMetricsReportingEnabled(), may_upload
);