| 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 "components/metrics_services_manager/metrics_services_manager.h" | 5 #include "components/metrics_services_manager/metrics_services_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "components/metrics/metrics_service.h" | 10 #include "components/metrics/metrics_service.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 GetMetricsServiceClient()->OnPluginLoadingError(plugin_path); | 61 GetMetricsServiceClient()->OnPluginLoadingError(plugin_path); |
| 62 } | 62 } |
| 63 | 63 |
| 64 void MetricsServicesManager::OnRendererProcessCrash() { | 64 void MetricsServicesManager::OnRendererProcessCrash() { |
| 65 GetMetricsServiceClient()->OnRendererProcessCrash(); | 65 GetMetricsServiceClient()->OnRendererProcessCrash(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 metrics::MetricsServiceClient* | 68 metrics::MetricsServiceClient* |
| 69 MetricsServicesManager::GetMetricsServiceClient() { | 69 MetricsServicesManager::GetMetricsServiceClient() { |
| 70 DCHECK(thread_checker_.CalledOnValidThread()); | 70 DCHECK(thread_checker_.CalledOnValidThread()); |
| 71 if (!metrics_service_client_) | 71 if (!metrics_service_client_) { |
| 72 metrics_service_client_ = client_->CreateMetricsServiceClient(); | 72 metrics_service_client_ = client_->CreateMetricsServiceClient(); |
| 73 // base::Unretained is safe since |this| owns the metrics_service_client_. |
| 74 metrics_service_client_->SetUpdateRunningServicesCallback( |
| 75 base::Bind(&MetricsServicesManager::UpdateRunningServices, |
| 76 base::Unretained(this))); |
| 77 } |
| 73 return metrics_service_client_.get(); | 78 return metrics_service_client_.get(); |
| 74 } | 79 } |
| 75 | 80 |
| 76 void MetricsServicesManager::UpdatePermissions(bool may_record, | 81 void MetricsServicesManager::UpdatePermissions(bool current_may_record, |
| 77 bool may_upload) { | 82 bool current_may_upload) { |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); | 83 DCHECK(thread_checker_.CalledOnValidThread()); |
| 84 // If the user has opted out of metrics, delete local UKM state. |
| 85 if (may_record_ && !current_may_record) { |
| 86 ukm::UkmService* ukm = GetUkmService(); |
| 87 if (ukm) { |
| 88 ukm->Purge(); |
| 89 ukm->ResetClientId(); |
| 90 } |
| 91 } |
| 92 |
| 79 // Stash the current permissions so that we can update the RapporServiceImpl | 93 // Stash the current permissions so that we can update the RapporServiceImpl |
| 80 // correctly when the Rappor preference changes. The metrics recording | 94 // correctly when the Rappor preference changes. The metrics recording |
| 81 // preference partially determines the initial rappor setting, and also | 95 // preference partially determines the initial rappor setting, and also |
| 82 // controls whether FINE metrics are sent. | 96 // controls whether FINE metrics are sent. |
| 83 may_record_ = may_record; | 97 may_record_ = current_may_record; |
| 84 may_upload_ = may_upload; | 98 may_upload_ = current_may_upload; |
| 85 UpdateRunningServices(); | 99 UpdateRunningServices(); |
| 86 } | 100 } |
| 87 | 101 |
| 88 void MetricsServicesManager::UpdateRunningServices() { | 102 void MetricsServicesManager::UpdateRunningServices() { |
| 89 DCHECK(thread_checker_.CalledOnValidThread()); | 103 DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 metrics::MetricsService* metrics = GetMetricsService(); | 104 metrics::MetricsService* metrics = GetMetricsService(); |
| 91 ukm::UkmService* ukm = GetUkmService(); | |
| 92 | 105 |
| 93 if (client_->OnlyDoMetricsRecording()) { | 106 if (client_->OnlyDoMetricsRecording()) { |
| 94 metrics->StartRecordingForTests(); | 107 metrics->StartRecordingForTests(); |
| 95 GetRapporServiceImpl()->Update( | 108 GetRapporServiceImpl()->Update( |
| 96 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, false); | 109 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, false); |
| 97 return; | 110 return; |
| 98 } | 111 } |
| 99 | 112 |
| 100 client_->UpdateRunningServices(may_record_, may_upload_); | 113 client_->UpdateRunningServices(may_record_, may_upload_); |
| 101 | 114 |
| 102 if (may_record_) { | 115 if (may_record_) { |
| 103 if (!metrics->recording_active()) | 116 if (!metrics->recording_active()) |
| 104 metrics->Start(); | 117 metrics->Start(); |
| 105 | 118 if (may_upload_) |
| 106 if (may_upload_) { | |
| 107 metrics->EnableReporting(); | 119 metrics->EnableReporting(); |
| 108 #if !defined(OFFICIAL_BUILD) | 120 else |
| 109 // TODO(holte): Make UKM check sync state instead of official build. | |
| 110 if (ukm) | |
| 111 ukm->EnableReporting(); | |
| 112 #endif | |
| 113 } else { | |
| 114 metrics->DisableReporting(); | 121 metrics->DisableReporting(); |
| 115 if (ukm) | |
| 116 ukm->DisableReporting(); | |
| 117 } | |
| 118 } else { | 122 } else { |
| 119 metrics->Stop(); | 123 metrics->Stop(); |
| 120 } | 124 } |
| 121 | 125 |
| 126 UpdateUkmService(); |
| 127 |
| 122 int recording_groups = 0; | 128 int recording_groups = 0; |
| 123 #if defined(GOOGLE_CHROME_BUILD) | 129 #if defined(GOOGLE_CHROME_BUILD) |
| 124 if (may_record_) | 130 if (may_record_) |
| 125 recording_groups |= rappor::UMA_RAPPOR_GROUP; | 131 recording_groups |= rappor::UMA_RAPPOR_GROUP; |
| 126 | 132 |
| 127 // NOTE: It is safe to use a raw pointer to |this| because this object owns | 133 // NOTE: It is safe to use a raw pointer to |this| because this object owns |
| 128 // |client_|, and the contract of | 134 // |client_|, and the contract of |
| 129 // MetricsServicesManagerClient::IsSafeBrowsingEnabled() states that the | 135 // MetricsServicesManagerClient::IsSafeBrowsingEnabled() states that the |
| 130 // callback passed in must not be used beyond the lifetime of the client | 136 // callback passed in must not be used beyond the lifetime of the client |
| 131 // instance. | 137 // instance. |
| 132 base::Closure on_safe_browsing_update_callback = base::Bind( | 138 base::Closure on_safe_browsing_update_callback = base::Bind( |
| 133 &MetricsServicesManager::UpdateRunningServices, base::Unretained(this)); | 139 &MetricsServicesManager::UpdateRunningServices, base::Unretained(this)); |
| 134 if (client_->IsSafeBrowsingEnabled(on_safe_browsing_update_callback)) | 140 if (client_->IsSafeBrowsingEnabled(on_safe_browsing_update_callback)) |
| 135 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; | 141 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; |
| 136 #endif // defined(GOOGLE_CHROME_BUILD) | 142 #endif // defined(GOOGLE_CHROME_BUILD) |
| 137 GetRapporServiceImpl()->Update(recording_groups, may_upload_); | 143 GetRapporServiceImpl()->Update(recording_groups, may_upload_); |
| 138 } | 144 } |
| 139 | 145 |
| 146 void MetricsServicesManager::UpdateUkmService() { |
| 147 ukm::UkmService* ukm = GetUkmService(); |
| 148 if (!ukm) |
| 149 return; |
| 150 bool sync_enabled = |
| 151 metrics_service_client_->IsHistorySyncEnabledOnAllProfiles(); |
| 152 if (may_record_ && sync_enabled) { |
| 153 ukm->EnableRecording(); |
| 154 if (may_upload_) |
| 155 ukm->EnableReporting(); |
| 156 else |
| 157 ukm->DisableReporting(); |
| 158 } else { |
| 159 ukm->DisableRecording(); |
| 160 ukm->DisableReporting(); |
| 161 } |
| 162 } |
| 163 |
| 140 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { | 164 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { |
| 141 UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload); | 165 UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload); |
| 142 } | 166 } |
| 143 | 167 |
| 144 } // namespace metrics_services_manager | 168 } // namespace metrics_services_manager |
| OLD | NEW |