| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/metrics/metrics_services_manager.h" | 5 #include "chrome/browser/metrics/metrics_services_manager.h" |
| 6 | 6 |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/command_line.h" | |
| 10 #include "base/logging.h" | 7 #include "base/logging.h" |
| 11 #include "base/prefs/pref_service.h" | 8 #include "chrome/browser/metrics/metrics_services_manager_client.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/metrics/variations/ui_string_overrider_factory.h" | |
| 18 #include "chrome/browser/profiles/profile.h" | |
| 19 #include "chrome/browser/profiles/profile_manager.h" | |
| 20 #include "chrome/browser/ui/browser_otr_state.h" | |
| 21 #include "chrome/common/chrome_switches.h" | |
| 22 #include "chrome/common/pref_names.h" | |
| 23 #include "chrome/installer/util/google_update_settings.h" | |
| 24 #include "components/metrics/metrics_service.h" | 9 #include "components/metrics/metrics_service.h" |
| 10 #include "components/metrics/metrics_service_client.h" |
| 25 #include "components/metrics/metrics_state_manager.h" | 11 #include "components/metrics/metrics_state_manager.h" |
| 26 #include "components/rappor/rappor_service.h" | 12 #include "components/rappor/rappor_service.h" |
| 27 #include "components/variations/service/variations_service.h" | 13 #include "components/variations/service/variations_service.h" |
| 28 #include "content/public/browser/browser_thread.h" | |
| 29 | 14 |
| 30 #if defined(OS_CHROMEOS) | 15 MetricsServicesManager::MetricsServicesManager( |
| 31 #include "chrome/browser/chromeos/settings/cros_settings.h" | 16 scoped_ptr<MetricsServicesManagerClient> client) |
| 32 #endif | 17 : client_(client.Pass()), may_upload_(false), may_record_(false) { |
| 33 | 18 DCHECK(client_); |
| 34 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread | |
| 35 // because it needs access to IO and cannot work from UI thread. | |
| 36 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) { | |
| 37 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, | |
| 38 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info)); | |
| 39 } | |
| 40 | |
| 41 MetricsServicesManager::MetricsServicesManager(PrefService* local_state) | |
| 42 : local_state_(local_state), | |
| 43 may_upload_(false), | |
| 44 may_record_(false) { | |
| 45 DCHECK(local_state); | |
| 46 } | 19 } |
| 47 | 20 |
| 48 MetricsServicesManager::~MetricsServicesManager() { | 21 MetricsServicesManager::~MetricsServicesManager() { |
| 49 } | 22 } |
| 50 | 23 |
| 51 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { | 24 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | 25 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 return GetChromeMetricsServiceClient()->metrics_service(); | 26 return GetMetricsServiceClient()->GetMetricsService(); |
| 54 } | 27 } |
| 55 | 28 |
| 56 rappor::RapporService* MetricsServicesManager::GetRapporService() { | 29 rappor::RapporService* MetricsServicesManager::GetRapporService() { |
| 57 DCHECK(thread_checker_.CalledOnValidThread()); | 30 DCHECK(thread_checker_.CalledOnValidThread()); |
| 58 if (!rappor_service_) { | 31 if (!rappor_service_) { |
| 59 rappor_service_.reset(new rappor::RapporService( | 32 rappor_service_ = client_->CreateRapporService(); |
| 60 local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive))); | 33 rappor_service_->Initialize(client_->GetURLRequestContext()); |
| 61 rappor_service_->Initialize(g_browser_process->system_request_context()); | |
| 62 } | 34 } |
| 63 return rappor_service_.get(); | 35 return rappor_service_.get(); |
| 64 } | 36 } |
| 65 | 37 |
| 66 variations::VariationsService* MetricsServicesManager::GetVariationsService() { | 38 variations::VariationsService* MetricsServicesManager::GetVariationsService() { |
| 67 DCHECK(thread_checker_.CalledOnValidThread()); | 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 68 if (!variations_service_) { | 40 if (!variations_service_) |
| 69 variations_service_ = variations::VariationsService::Create( | 41 variations_service_ = client_->CreateVariationsService(); |
| 70 make_scoped_ptr(new ChromeVariationsServiceClient()), local_state_, | |
| 71 GetMetricsStateManager(), switches::kDisableBackgroundNetworking, | |
| 72 chrome_variations::CreateUIStringOverrider()); | |
| 73 } | |
| 74 return variations_service_.get(); | 42 return variations_service_.get(); |
| 75 } | 43 } |
| 76 | 44 |
| 77 void MetricsServicesManager::OnPluginLoadingError( | 45 void MetricsServicesManager::OnPluginLoadingError( |
| 78 const base::FilePath& plugin_path) { | 46 const base::FilePath& plugin_path) { |
| 79 GetChromeMetricsServiceClient()->LogPluginLoadingError(plugin_path); | 47 GetMetricsServiceClient()->OnPluginLoadingError(plugin_path); |
| 80 } | 48 } |
| 81 | 49 |
| 82 ChromeMetricsServiceClient* | 50 metrics::MetricsServiceClient* |
| 83 MetricsServicesManager::GetChromeMetricsServiceClient() { | 51 MetricsServicesManager::GetMetricsServiceClient() { |
| 84 DCHECK(thread_checker_.CalledOnValidThread()); | 52 DCHECK(thread_checker_.CalledOnValidThread()); |
| 85 if (!metrics_service_client_) { | 53 if (!metrics_service_client_) |
| 86 metrics_service_client_ = ChromeMetricsServiceClient::Create( | 54 metrics_service_client_ = client_->CreateMetricsServiceClient(); |
| 87 GetMetricsStateManager(), local_state_); | |
| 88 } | |
| 89 return metrics_service_client_.get(); | 55 return metrics_service_client_.get(); |
| 90 } | 56 } |
| 91 | 57 |
| 92 metrics::MetricsStateManager* MetricsServicesManager::GetMetricsStateManager() { | |
| 93 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 94 if (!metrics_state_manager_) { | |
| 95 metrics_state_manager_ = metrics::MetricsStateManager::Create( | |
| 96 local_state_, | |
| 97 base::Bind( | |
| 98 &ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled), | |
| 99 base::Bind(&PostStoreMetricsClientInfo), | |
| 100 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo)); | |
| 101 } | |
| 102 return metrics_state_manager_.get(); | |
| 103 } | |
| 104 | |
| 105 bool MetricsServicesManager::GetSafeBrowsingState() { | |
| 106 // Start listening for updates to SB service state. This is done here instead | |
| 107 // of in the constructor to avoid errors from trying to instantiate SB | |
| 108 // service before the IO thread exists. | |
| 109 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); | |
| 110 if (!sb_state_subscription_ && sb_service) { | |
| 111 // base::Unretained(this) is safe here since this object owns the | |
| 112 // sb_state_subscription_ which owns the pointer. | |
| 113 sb_state_subscription_ = sb_service->RegisterStateCallback( | |
| 114 base::Bind(&MetricsServicesManager::UpdateRunningServices, | |
| 115 base::Unretained(this))); | |
| 116 } | |
| 117 | |
| 118 return sb_service && sb_service->enabled_by_prefs(); | |
| 119 } | |
| 120 | |
| 121 void MetricsServicesManager::UpdatePermissions(bool may_record, | 58 void MetricsServicesManager::UpdatePermissions(bool may_record, |
| 122 bool may_upload) { | 59 bool may_upload) { |
| 123 DCHECK(thread_checker_.CalledOnValidThread()); | 60 DCHECK(thread_checker_.CalledOnValidThread()); |
| 124 // Stash the current permissions so that we can update the RapporService | 61 // Stash the current permissions so that we can update the RapporService |
| 125 // correctly when the Rappor preference changes. The metrics recording | 62 // correctly when the Rappor preference changes. The metrics recording |
| 126 // preference partially determines the initial rappor setting, and also | 63 // preference partially determines the initial rappor setting, and also |
| 127 // controls whether FINE metrics are sent. | 64 // controls whether FINE metrics are sent. |
| 128 may_record_ = may_record; | 65 may_record_ = may_record; |
| 129 may_upload_ = may_upload; | 66 may_upload_ = may_upload; |
| 130 UpdateRunningServices(); | 67 UpdateRunningServices(); |
| 131 } | 68 } |
| 132 | 69 |
| 133 void MetricsServicesManager::UpdateRunningServices() { | 70 void MetricsServicesManager::UpdateRunningServices() { |
| 134 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
| 135 metrics::MetricsService* metrics = GetMetricsService(); | 72 metrics::MetricsService* metrics = GetMetricsService(); |
| 136 | 73 |
| 137 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 74 if (client_->OnlyDoMetricsRecording()) { |
| 138 | |
| 139 const bool only_do_metrics_recording = | |
| 140 cmdline->HasSwitch(switches::kMetricsRecordingOnly) || | |
| 141 cmdline->HasSwitch(switches::kEnableBenchmarking); | |
| 142 | |
| 143 if (only_do_metrics_recording) { | |
| 144 metrics->StartRecordingForTests(); | 75 metrics->StartRecordingForTests(); |
| 145 GetRapporService()->Update( | 76 GetRapporService()->Update( |
| 146 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, | 77 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, |
| 147 false); | 78 false); |
| 148 return; | 79 return; |
| 149 } | 80 } |
| 150 | 81 |
| 151 if (may_record_) { | 82 if (may_record_) { |
| 152 if (!metrics->recording_active()) | 83 if (!metrics->recording_active()) |
| 153 metrics->Start(); | 84 metrics->Start(); |
| 154 | 85 |
| 155 if (may_upload_) | 86 if (may_upload_) |
| 156 metrics->EnableReporting(); | 87 metrics->EnableReporting(); |
| 157 else | 88 else |
| 158 metrics->DisableReporting(); | 89 metrics->DisableReporting(); |
| 159 } else { | 90 } else { |
| 160 metrics->Stop(); | 91 metrics->Stop(); |
| 161 } | 92 } |
| 162 | 93 |
| 163 int recording_groups = 0; | 94 int recording_groups = 0; |
| 164 #if defined(GOOGLE_CHROME_BUILD) | 95 #if defined(GOOGLE_CHROME_BUILD) |
| 165 if (may_record_) | 96 if (may_record_) |
| 166 recording_groups |= rappor::UMA_RAPPOR_GROUP; | 97 recording_groups |= rappor::UMA_RAPPOR_GROUP; |
| 167 if (GetSafeBrowsingState()) | 98 |
| 99 // NOTE: It is safe to use a raw pointer to |this| because this object owns |
| 100 // |client_|, and the contract of |
| 101 // MetricsServicesManagerClient::IsSafeBrowsingEnabled() states that the |
| 102 // callback passed in must not be used beyond the lifetime of the client |
| 103 // instance. |
| 104 base::Closure on_safe_browsing_update_callback = base::Bind( |
| 105 &MetricsServicesManager::UpdateRunningServices, base::Unretained(this)); |
| 106 if (client_->IsSafeBrowsingEnabled(on_safe_browsing_update_callback) |
| 168 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; | 107 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; |
| 169 #endif // defined(GOOGLE_CHROME_BUILD) | 108 #endif // defined(GOOGLE_CHROME_BUILD) |
| 170 GetRapporService()->Update(recording_groups, may_upload_); | 109 GetRapporService()->Update(recording_groups, may_upload_); |
| 171 } | 110 } |
| 172 | 111 |
| 173 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { | 112 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { |
| 174 return UpdatePermissions( | 113 return UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload); |
| 175 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled(), | |
| 176 may_upload); | |
| 177 } | 114 } |
| OLD | NEW |