Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / metrics / metrics_services_manager.cc
blob705def008bfacf5ada05c976eaa7f355638181f9
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/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 "content/public/browser/browser_thread.h"
28 #if defined(OS_CHROMEOS)
29 #include "chrome/browser/chromeos/settings/cros_settings.h"
30 #endif
32 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread
33 // because it needs access to IO and cannot work from UI thread.
34 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) {
35 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE,
36 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info));
39 MetricsServicesManager::MetricsServicesManager(PrefService* local_state)
40 : local_state_(local_state),
41 may_upload_(false),
42 may_record_(false) {
43 DCHECK(local_state);
46 MetricsServicesManager::~MetricsServicesManager() {
49 metrics::MetricsService* MetricsServicesManager::GetMetricsService() {
50 DCHECK(thread_checker_.CalledOnValidThread());
51 return GetChromeMetricsServiceClient()->metrics_service();
54 rappor::RapporService* MetricsServicesManager::GetRapporService() {
55 DCHECK(thread_checker_.CalledOnValidThread());
56 if (!rappor_service_) {
57 rappor_service_.reset(new rappor::RapporService(
58 local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive)));
59 rappor_service_->Initialize(g_browser_process->system_request_context());
61 return rappor_service_.get();
64 chrome_variations::VariationsService*
65 MetricsServicesManager::GetVariationsService() {
66 DCHECK(thread_checker_.CalledOnValidThread());
67 if (!variations_service_) {
68 variations_service_ =
69 chrome_variations::VariationsService::Create(local_state_,
70 GetMetricsStateManager());
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(&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,
119 bool may_upload) {
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,
144 false);
145 return;
148 if (may_record_) {
149 if (!metrics->recording_active())
150 metrics->Start();
152 if (may_upload_)
153 metrics->EnableReporting();
154 else
155 metrics->DisableReporting();
156 } else if (metrics->recording_active() || metrics->reporting_active()) {
157 metrics->Stop();
160 int recording_groups = 0;
161 #if defined(GOOGLE_CHROME_BUILD)
162 if (may_record_)
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);