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. 4 5 #include "chrome/browser/metrics/metrics_services_manager.h" 6 7 #include "base/command_line.h" 8 #include "base/prefs/pref_service.h" 9 #include "chrome/browser/metrics/chrome_metrics_service_client.h" 10 #include "chrome/browser/metrics/variations/variations_service.h" 11 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/pref_names.h" 13 #include "components/metrics/metrics_service.h" 14 #include "components/metrics/metrics_state_manager.h" 15 #include "components/rappor/rappor_service.h" 16 17 #if defined(OS_CHROMEOS) 18 #include "chrome/browser/chromeos/settings/cros_settings.h" 19 #endif 20 21 MetricsServicesManager::MetricsServicesManager(PrefService* local_state) 22 : local_state_(local_state) { 23 DCHECK(local_state); 24 } 25 26 MetricsServicesManager::~MetricsServicesManager() { 27 } 28 29 MetricsService* MetricsServicesManager::GetMetricsService() { 30 DCHECK(thread_checker_.CalledOnValidThread()); 31 return GetChromeMetricsServiceClient()->metrics_service(); 32 } 33 34 rappor::RapporService* MetricsServicesManager::GetRapporService() { 35 DCHECK(thread_checker_.CalledOnValidThread()); 36 if (!rappor_service_) 37 rappor_service_.reset(new rappor::RapporService); 38 return rappor_service_.get(); 39 } 40 41 chrome_variations::VariationsService* 42 MetricsServicesManager::GetVariationsService() { 43 DCHECK(thread_checker_.CalledOnValidThread()); 44 if (!variations_service_) { 45 variations_service_ = 46 chrome_variations::VariationsService::Create(local_state_, 47 GetMetricsStateManager()); 48 } 49 return variations_service_.get(); 50 } 51 52 void MetricsServicesManager::OnPluginLoadingError( 53 const base::FilePath& plugin_path) { 54 GetChromeMetricsServiceClient()->LogPluginLoadingError(plugin_path); 55 } 56 57 ChromeMetricsServiceClient* 58 MetricsServicesManager::GetChromeMetricsServiceClient() { 59 DCHECK(thread_checker_.CalledOnValidThread()); 60 if (!metrics_service_client_) { 61 metrics_service_client_ = ChromeMetricsServiceClient::Create( 62 GetMetricsStateManager(), local_state_); 63 } 64 return metrics_service_client_.get(); 65 } 66 67 metrics::MetricsStateManager* MetricsServicesManager::GetMetricsStateManager() { 68 DCHECK(thread_checker_.CalledOnValidThread()); 69 if (!metrics_state_manager_) { 70 metrics_state_manager_ = metrics::MetricsStateManager::Create( 71 local_state_, 72 base::Bind(&MetricsServicesManager::IsMetricsReportingEnabled, 73 base::Unretained(this))); 74 } 75 return metrics_state_manager_.get(); 76 } 77 78 // TODO(asvitkine): This function does not report the correct value on Android, 79 // see http://crbug.com/362192. 80 bool MetricsServicesManager::IsMetricsReportingEnabled() const { 81 // If the user permits metrics reporting with the checkbox in the 82 // prefs, we turn on recording. We disable metrics completely for 83 // non-official builds, or when field trials are forced. 84 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kForceFieldTrials)) 85 return false; 86 87 bool enabled = false; 88 #if defined(GOOGLE_CHROME_BUILD) 89 #if defined(OS_CHROMEOS) 90 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, 91 &enabled); 92 #else 93 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled); 94 #endif // #if defined(OS_CHROMEOS) 95 #endif // defined(GOOGLE_CHROME_BUILD) 96 return enabled; 97 } 98