Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(493)

Side by Side Diff: components/metrics_services_manager/metrics_services_manager.cc

Issue 2653693004: UKM Sync Observer (Closed)
Patch Set: Suppress recording and fix tests Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 ukm::UkmService* ukm = GetUkmService();
Alexei Svitkine (slow) 2017/02/04 00:52:23 Nit: Make a helper function to update ukm state an
Steven Holte 2017/02/06 21:57:54 Done.
127 if (ukm) {
128 if (may_record_ &&
129 metrics_service_client_->IsHistorySyncEnabledOnAllProfiles()) {
130 ukm->EnableRecording();
131 if (may_upload_)
132 ukm->EnableReporting();
133 else
134 ukm->DisableReporting();
135 } else {
136 ukm->DisableRecording();
137 ukm->DisableReporting();
138 }
139 }
140
122 int recording_groups = 0; 141 int recording_groups = 0;
123 #if defined(GOOGLE_CHROME_BUILD) 142 #if defined(GOOGLE_CHROME_BUILD)
124 if (may_record_) 143 if (may_record_)
125 recording_groups |= rappor::UMA_RAPPOR_GROUP; 144 recording_groups |= rappor::UMA_RAPPOR_GROUP;
126 145
127 // NOTE: It is safe to use a raw pointer to |this| because this object owns 146 // NOTE: It is safe to use a raw pointer to |this| because this object owns
128 // |client_|, and the contract of 147 // |client_|, and the contract of
129 // MetricsServicesManagerClient::IsSafeBrowsingEnabled() states that the 148 // MetricsServicesManagerClient::IsSafeBrowsingEnabled() states that the
130 // callback passed in must not be used beyond the lifetime of the client 149 // callback passed in must not be used beyond the lifetime of the client
131 // instance. 150 // instance.
132 base::Closure on_safe_browsing_update_callback = base::Bind( 151 base::Closure on_safe_browsing_update_callback = base::Bind(
133 &MetricsServicesManager::UpdateRunningServices, base::Unretained(this)); 152 &MetricsServicesManager::UpdateRunningServices, base::Unretained(this));
134 if (client_->IsSafeBrowsingEnabled(on_safe_browsing_update_callback)) 153 if (client_->IsSafeBrowsingEnabled(on_safe_browsing_update_callback))
135 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; 154 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP;
136 #endif // defined(GOOGLE_CHROME_BUILD) 155 #endif // defined(GOOGLE_CHROME_BUILD)
137 GetRapporServiceImpl()->Update(recording_groups, may_upload_); 156 GetRapporServiceImpl()->Update(recording_groups, may_upload_);
138 } 157 }
139 158
140 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { 159 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) {
141 UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload); 160 UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload);
142 } 161 }
143 162
144 } // namespace metrics_services_manager 163 } // namespace metrics_services_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698