| Index: components/metrics_services_manager/metrics_services_manager.cc | 
| diff --git a/components/metrics_services_manager/metrics_services_manager.cc b/components/metrics_services_manager/metrics_services_manager.cc | 
| index e1ec65096b3e21b946d8fdb24bb489d1c3b6fc71..7b87f884a91557cc8324d8fd549feb9aa10960ed 100644 | 
| --- a/components/metrics_services_manager/metrics_services_manager.cc | 
| +++ b/components/metrics_services_manager/metrics_services_manager.cc | 
| @@ -68,27 +68,40 @@ void MetricsServicesManager::OnRendererProcessCrash() { | 
| metrics::MetricsServiceClient* | 
| MetricsServicesManager::GetMetricsServiceClient() { | 
| DCHECK(thread_checker_.CalledOnValidThread()); | 
| -  if (!metrics_service_client_) | 
| +  if (!metrics_service_client_) { | 
| metrics_service_client_ = client_->CreateMetricsServiceClient(); | 
| +    // base::Unretained is safe since |this| owns the metrics_service_client_. | 
| +    metrics_service_client_->SetUpdateRunningServicesCallback( | 
| +        base::Bind(&MetricsServicesManager::UpdateRunningServices, | 
| +                   base::Unretained(this))); | 
| +  } | 
| return metrics_service_client_.get(); | 
| } | 
|  | 
| -void MetricsServicesManager::UpdatePermissions(bool may_record, | 
| -                                               bool may_upload) { | 
| +void MetricsServicesManager::UpdatePermissions(bool current_may_record, | 
| +                                               bool current_may_upload) { | 
| DCHECK(thread_checker_.CalledOnValidThread()); | 
| +  // If the user has opted out of metrics, delete local UKM state. | 
| +  if (may_record_ && !current_may_record) { | 
| +    ukm::UkmService* ukm = GetUkmService(); | 
| +    if (ukm) { | 
| +      ukm->Purge(); | 
| +      ukm->ResetClientId(); | 
| +    } | 
| +  } | 
| + | 
| // Stash the current permissions so that we can update the RapporServiceImpl | 
| // correctly when the Rappor preference changes.  The metrics recording | 
| // preference partially determines the initial rappor setting, and also | 
| // controls whether FINE metrics are sent. | 
| -  may_record_ = may_record; | 
| -  may_upload_ = may_upload; | 
| +  may_record_ = current_may_record; | 
| +  may_upload_ = current_may_upload; | 
| UpdateRunningServices(); | 
| } | 
|  | 
| void MetricsServicesManager::UpdateRunningServices() { | 
| DCHECK(thread_checker_.CalledOnValidThread()); | 
| metrics::MetricsService* metrics = GetMetricsService(); | 
| -  ukm::UkmService* ukm = GetUkmService(); | 
|  | 
| if (client_->OnlyDoMetricsRecording()) { | 
| metrics->StartRecordingForTests(); | 
| @@ -102,23 +115,16 @@ void MetricsServicesManager::UpdateRunningServices() { | 
| if (may_record_) { | 
| if (!metrics->recording_active()) | 
| metrics->Start(); | 
| - | 
| -    if (may_upload_) { | 
| +    if (may_upload_) | 
| metrics->EnableReporting(); | 
| -#if !defined(OFFICIAL_BUILD) | 
| -      // TODO(holte): Make UKM check sync state instead of official build. | 
| -      if (ukm) | 
| -        ukm->EnableReporting(); | 
| -#endif | 
| -    } else { | 
| +    else | 
| metrics->DisableReporting(); | 
| -      if (ukm) | 
| -        ukm->DisableReporting(); | 
| -    } | 
| } else { | 
| metrics->Stop(); | 
| } | 
|  | 
| +  UpdateUkmService(); | 
| + | 
| int recording_groups = 0; | 
| #if defined(GOOGLE_CHROME_BUILD) | 
| if (may_record_) | 
| @@ -137,6 +143,24 @@ void MetricsServicesManager::UpdateRunningServices() { | 
| GetRapporServiceImpl()->Update(recording_groups, may_upload_); | 
| } | 
|  | 
| +void MetricsServicesManager::UpdateUkmService() { | 
| +  ukm::UkmService* ukm = GetUkmService(); | 
| +  if (!ukm) | 
| +    return; | 
| +  bool sync_enabled = | 
| +      metrics_service_client_->IsHistorySyncEnabledOnAllProfiles(); | 
| +  if (may_record_ && sync_enabled) { | 
| +    ukm->EnableRecording(); | 
| +    if (may_upload_) | 
| +      ukm->EnableReporting(); | 
| +    else | 
| +      ukm->DisableReporting(); | 
| +  } else { | 
| +    ukm->DisableRecording(); | 
| +    ukm->DisableReporting(); | 
| +  } | 
| +} | 
| + | 
| void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { | 
| UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload); | 
| } | 
|  |