OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/ukm/ukm_service.h" | 5 #include "components/ukm/ukm_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 const size_t kMaxSources = 100; | 62 const size_t kMaxSources = 100; |
63 | 63 |
64 std::string GetServerUrl() { | 64 std::string GetServerUrl() { |
65 std::string server_url = | 65 std::string server_url = |
66 variations::GetVariationParamValueByFeature(kUkmFeature, "ServerUrl"); | 66 variations::GetVariationParamValueByFeature(kUkmFeature, "ServerUrl"); |
67 if (!server_url.empty()) | 67 if (!server_url.empty()) |
68 return server_url; | 68 return server_url; |
69 return kDefaultServerUrl; | 69 return kDefaultServerUrl; |
70 } | 70 } |
71 | 71 |
72 // Generates a new client id and stores it in prefs. | |
73 uint64_t GenerateClientId(PrefService* pref_service) { | |
74 uint64_t client_id = 0; | |
75 while (!client_id) | |
76 client_id = base::RandUint64(); | |
77 pref_service->SetInt64(prefs::kUkmClientId, client_id); | |
78 return client_id; | |
79 } | |
80 | |
72 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { | 81 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { |
73 uint64_t client_id = pref_service->GetInt64(prefs::kUkmClientId); | 82 uint64_t client_id = pref_service->GetInt64(prefs::kUkmClientId); |
74 if (!client_id) { | 83 if (!client_id) { |
Alexei Svitkine (slow)
2017/02/04 00:52:23
Nit: No {}
Steven Holte
2017/02/06 21:57:54
Done.
| |
75 // Generate and store a new client id. | 84 client_id = GenerateClientId(pref_service); |
76 while (!client_id) | |
77 client_id = base::RandUint64(); | |
78 pref_service->SetInt64(prefs::kUkmClientId, client_id); | |
79 } | 85 } |
80 return client_id; | 86 return client_id; |
81 } | 87 } |
82 | 88 |
89 enum class DroppedSourceReason { | |
90 NOT_DROPPED = 0, | |
91 RECORDING_DISABLED = 1, | |
92 MAX_SOURCES_HIT = 2, | |
93 NUM_DROPPED_SOURCES_REASONS | |
94 }; | |
95 | |
96 void RecordDroppedSource(DroppedSourceReason reason) { | |
97 UMA_HISTOGRAM_ENUMERATION( | |
98 "UKM.Sources.Dropped", static_cast<int>(reason), | |
99 static_cast<int>(DroppedSourceReason::NUM_DROPPED_SOURCES_REASONS)); | |
100 } | |
101 | |
83 } // namespace | 102 } // namespace |
84 | 103 |
85 const base::Feature kUkmFeature = {"Ukm", base::FEATURE_DISABLED_BY_DEFAULT}; | 104 const base::Feature kUkmFeature = {"Ukm", base::FEATURE_DISABLED_BY_DEFAULT}; |
86 | 105 |
87 UkmService::UkmService(PrefService* pref_service, | 106 UkmService::UkmService(PrefService* pref_service, |
88 metrics::MetricsServiceClient* client) | 107 metrics::MetricsServiceClient* client) |
89 : pref_service_(pref_service), | 108 : pref_service_(pref_service), |
109 recording_enabled_(false), | |
90 client_(client), | 110 client_(client), |
91 persisted_logs_(std::unique_ptr<ukm::PersistedLogsMetricsImpl>( | 111 persisted_logs_(std::unique_ptr<ukm::PersistedLogsMetricsImpl>( |
92 new ukm::PersistedLogsMetricsImpl()), | 112 new ukm::PersistedLogsMetricsImpl()), |
93 pref_service, | 113 pref_service, |
94 prefs::kUkmPersistedLogs, | 114 prefs::kUkmPersistedLogs, |
95 kMinPersistedLogs, | 115 kMinPersistedLogs, |
96 kMinPersistedBytes, | 116 kMinPersistedBytes, |
97 kMaxLogRetransmitSize), | 117 kMaxLogRetransmitSize), |
98 initialize_started_(false), | 118 initialize_started_(false), |
99 initialize_complete_(false), | 119 initialize_complete_(false), |
(...skipping 24 matching lines...) Expand all Loading... | |
124 DCHECK(thread_checker_.CalledOnValidThread()); | 144 DCHECK(thread_checker_.CalledOnValidThread()); |
125 DVLOG(1) << "UkmService::Initialize"; | 145 DVLOG(1) << "UkmService::Initialize"; |
126 initialize_started_ = true; | 146 initialize_started_ = true; |
127 | 147 |
128 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 148 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
129 FROM_HERE, | 149 FROM_HERE, |
130 base::Bind(&UkmService::StartInitTask, self_ptr_factory_.GetWeakPtr()), | 150 base::Bind(&UkmService::StartInitTask, self_ptr_factory_.GetWeakPtr()), |
131 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); | 151 base::TimeDelta::FromSeconds(kInitializationDelaySeconds)); |
132 } | 152 } |
133 | 153 |
154 void UkmService::EnableRecording() { | |
155 recording_enabled_ = true; | |
156 } | |
157 | |
158 void UkmService::DisableRecording() { | |
159 recording_enabled_ = false; | |
160 } | |
161 | |
134 void UkmService::EnableReporting() { | 162 void UkmService::EnableReporting() { |
135 DCHECK(thread_checker_.CalledOnValidThread()); | 163 DCHECK(thread_checker_.CalledOnValidThread()); |
136 DVLOG(1) << "UkmService::EnableReporting"; | 164 DVLOG(1) << "UkmService::EnableReporting"; |
137 if (!initialize_started_) | 165 if (!initialize_started_) |
138 Initialize(); | 166 Initialize(); |
139 scheduler_->Start(); | 167 scheduler_->Start(); |
140 } | 168 } |
141 | 169 |
142 void UkmService::DisableReporting() { | 170 void UkmService::DisableReporting() { |
143 DCHECK(thread_checker_.CalledOnValidThread()); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
144 DVLOG(1) << "UkmService::DisableReporting"; | 172 DVLOG(1) << "UkmService::DisableReporting"; |
145 scheduler_->Stop(); | 173 scheduler_->Stop(); |
146 Flush(); | 174 Flush(); |
147 } | 175 } |
148 | 176 |
149 void UkmService::Flush() { | 177 void UkmService::Flush() { |
150 if (initialize_complete_) | 178 if (initialize_complete_) |
151 BuildAndStoreLog(); | 179 BuildAndStoreLog(); |
152 persisted_logs_.SerializeLogs(); | 180 persisted_logs_.SerializeLogs(); |
153 } | 181 } |
154 | 182 |
155 void UkmService::Purge() { | 183 void UkmService::Purge() { |
156 DVLOG(1) << "UkmService::Purge"; | 184 DVLOG(1) << "UkmService::Purge"; |
157 persisted_logs_.Purge(); | 185 persisted_logs_.Purge(); |
158 sources_.clear(); | 186 sources_.clear(); |
159 } | 187 } |
160 | 188 |
189 void UkmService::ResetClientId() { | |
190 client_id_ = GenerateClientId(pref_service_); | |
191 } | |
192 | |
161 // static | 193 // static |
162 void UkmService::RegisterPrefs(PrefRegistrySimple* registry) { | 194 void UkmService::RegisterPrefs(PrefRegistrySimple* registry) { |
163 registry->RegisterInt64Pref(prefs::kUkmClientId, 0); | 195 registry->RegisterInt64Pref(prefs::kUkmClientId, 0); |
164 registry->RegisterListPref(prefs::kUkmPersistedLogs); | 196 registry->RegisterListPref(prefs::kUkmPersistedLogs); |
165 } | 197 } |
166 | 198 |
167 void UkmService::StartInitTask() { | 199 void UkmService::StartInitTask() { |
168 DCHECK(thread_checker_.CalledOnValidThread()); | 200 DCHECK(thread_checker_.CalledOnValidThread()); |
169 DVLOG(1) << "UkmService::StartInitTask"; | 201 DVLOG(1) << "UkmService::StartInitTask"; |
170 client_id_ = LoadOrGenerateClientId(pref_service_); | 202 client_id_ = LoadOrGenerateClientId(pref_service_); |
(...skipping 14 matching lines...) Expand all Loading... | |
185 DVLOG(1) << "UkmService::RotateLog"; | 217 DVLOG(1) << "UkmService::RotateLog"; |
186 if (persisted_logs_.empty()) { | 218 if (persisted_logs_.empty()) { |
187 BuildAndStoreLog(); | 219 BuildAndStoreLog(); |
188 } | 220 } |
189 StartScheduledUpload(); | 221 StartScheduledUpload(); |
190 } | 222 } |
191 | 223 |
192 void UkmService::BuildAndStoreLog() { | 224 void UkmService::BuildAndStoreLog() { |
193 DCHECK(thread_checker_.CalledOnValidThread()); | 225 DCHECK(thread_checker_.CalledOnValidThread()); |
194 DVLOG(1) << "UkmService::BuildAndStoreLog"; | 226 DVLOG(1) << "UkmService::BuildAndStoreLog"; |
227 // Suppress generating a log if we have no new data to include. | |
228 if (sources_.empty()) | |
229 return; | |
230 | |
195 Report report; | 231 Report report; |
196 report.set_client_id(client_id_); | 232 report.set_client_id(client_id_); |
197 | 233 |
198 for (const auto& source : sources_) { | 234 for (const auto& source : sources_) { |
199 Source* proto_source = report.add_sources(); | 235 Source* proto_source = report.add_sources(); |
200 source->PopulateProto(proto_source); | 236 source->PopulateProto(proto_source); |
201 } | 237 } |
202 UMA_HISTOGRAM_COUNTS_1000("UKM.Sources.SerializedCount", sources_.size()); | 238 UMA_HISTOGRAM_COUNTS_1000("UKM.Sources.SerializedCount", sources_.size()); |
203 sources_.clear(); | 239 sources_.clear(); |
204 | 240 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 persisted_logs_.SerializeLogs(); | 291 persisted_logs_.SerializeLogs(); |
256 } | 292 } |
257 | 293 |
258 // Error 400 indicates a problem with the log, not with the server, so | 294 // Error 400 indicates a problem with the log, not with the server, so |
259 // don't consider that a sign that the server is in trouble. | 295 // don't consider that a sign that the server is in trouble. |
260 bool server_is_healthy = upload_succeeded || response_code == 400; | 296 bool server_is_healthy = upload_succeeded || response_code == 400; |
261 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); | 297 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); |
262 } | 298 } |
263 | 299 |
264 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { | 300 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { |
301 if (!recording_enabled_) { | |
302 RecordDroppedSource(DroppedSourceReason::RECORDING_DISABLED); | |
303 return; | |
304 } | |
265 if (sources_.size() >= kMaxSources) { | 305 if (sources_.size() >= kMaxSources) { |
266 UMA_HISTOGRAM_BOOLEAN("UKM.Sources.MaxSourcesHit", true); | 306 RecordDroppedSource(DroppedSourceReason::MAX_SOURCES_HIT); |
267 return; | 307 return; |
268 } | 308 } |
269 | 309 |
270 sources_.push_back(std::move(source)); | 310 sources_.push_back(std::move(source)); |
271 } | 311 } |
272 | 312 |
273 } // namespace ukm | 313 } // namespace ukm |
OLD | NEW |