| OLD | NEW |
| (Empty) |
| 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/logging.h" | |
| 8 #include "chrome/browser/metrics/metrics_services_manager_client.h" | |
| 9 #include "components/metrics/metrics_service.h" | |
| 10 #include "components/metrics/metrics_service_client.h" | |
| 11 #include "components/metrics/metrics_state_manager.h" | |
| 12 #include "components/rappor/rappor_service.h" | |
| 13 #include "components/variations/service/variations_service.h" | |
| 14 | |
| 15 MetricsServicesManager::MetricsServicesManager( | |
| 16 scoped_ptr<MetricsServicesManagerClient> client) | |
| 17 : client_(client.Pass()), may_upload_(false), may_record_(false) { | |
| 18 DCHECK(client_); | |
| 19 } | |
| 20 | |
| 21 MetricsServicesManager::~MetricsServicesManager() { | |
| 22 } | |
| 23 | |
| 24 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { | |
| 25 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 26 return GetMetricsServiceClient()->GetMetricsService(); | |
| 27 } | |
| 28 | |
| 29 rappor::RapporService* MetricsServicesManager::GetRapporService() { | |
| 30 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 31 if (!rappor_service_) { | |
| 32 rappor_service_ = client_->CreateRapporService(); | |
| 33 rappor_service_->Initialize(client_->GetURLRequestContext()); | |
| 34 } | |
| 35 return rappor_service_.get(); | |
| 36 } | |
| 37 | |
| 38 variations::VariationsService* MetricsServicesManager::GetVariationsService() { | |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 40 if (!variations_service_) | |
| 41 variations_service_ = client_->CreateVariationsService(); | |
| 42 return variations_service_.get(); | |
| 43 } | |
| 44 | |
| 45 void MetricsServicesManager::OnPluginLoadingError( | |
| 46 const base::FilePath& plugin_path) { | |
| 47 GetMetricsServiceClient()->OnPluginLoadingError(plugin_path); | |
| 48 } | |
| 49 | |
| 50 metrics::MetricsServiceClient* | |
| 51 MetricsServicesManager::GetMetricsServiceClient() { | |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 53 if (!metrics_service_client_) | |
| 54 metrics_service_client_ = client_->CreateMetricsServiceClient(); | |
| 55 return metrics_service_client_.get(); | |
| 56 } | |
| 57 | |
| 58 void MetricsServicesManager::UpdatePermissions(bool may_record, | |
| 59 bool may_upload) { | |
| 60 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 61 // Stash the current permissions so that we can update the RapporService | |
| 62 // correctly when the Rappor preference changes. The metrics recording | |
| 63 // preference partially determines the initial rappor setting, and also | |
| 64 // controls whether FINE metrics are sent. | |
| 65 may_record_ = may_record; | |
| 66 may_upload_ = may_upload; | |
| 67 UpdateRunningServices(); | |
| 68 } | |
| 69 | |
| 70 void MetricsServicesManager::UpdateRunningServices() { | |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 72 metrics::MetricsService* metrics = GetMetricsService(); | |
| 73 | |
| 74 if (client_->OnlyDoMetricsRecording()) { | |
| 75 metrics->StartRecordingForTests(); | |
| 76 GetRapporService()->Update( | |
| 77 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, | |
| 78 false); | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 if (may_record_) { | |
| 83 if (!metrics->recording_active()) | |
| 84 metrics->Start(); | |
| 85 | |
| 86 if (may_upload_) | |
| 87 metrics->EnableReporting(); | |
| 88 else | |
| 89 metrics->DisableReporting(); | |
| 90 } else { | |
| 91 metrics->Stop(); | |
| 92 } | |
| 93 | |
| 94 int recording_groups = 0; | |
| 95 #if defined(GOOGLE_CHROME_BUILD) | |
| 96 if (may_record_) | |
| 97 recording_groups |= rappor::UMA_RAPPOR_GROUP; | |
| 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)) | |
| 107 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; | |
| 108 #endif // defined(GOOGLE_CHROME_BUILD) | |
| 109 GetRapporService()->Update(recording_groups, may_upload_); | |
| 110 } | |
| 111 | |
| 112 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { | |
| 113 return UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload); | |
| 114 } | |
| OLD | NEW |