Chromium Code Reviews| 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 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 kMinPersistedBytes, | 115 kMinPersistedBytes, |
| 116 kMaxLogRetransmitSize), | 116 kMaxLogRetransmitSize), |
| 117 initialize_started_(false), | 117 initialize_started_(false), |
| 118 initialize_complete_(false), | 118 initialize_complete_(false), |
| 119 log_upload_in_progress_(false), | 119 log_upload_in_progress_(false), |
| 120 self_ptr_factory_(this) { | 120 self_ptr_factory_(this) { |
| 121 DCHECK(pref_service_); | 121 DCHECK(pref_service_); |
| 122 DCHECK(client_); | 122 DCHECK(client_); |
| 123 DVLOG(1) << "UkmService::Constructor"; | 123 DVLOG(1) << "UkmService::Constructor"; |
| 124 | 124 |
| 125 persisted_logs_.DeserializeLogs(); | 125 persisted_logs_.LoadPersistedUnsentLogs(); |
| 126 | 126 |
| 127 base::Closure rotate_callback = | 127 base::Closure rotate_callback = |
| 128 base::Bind(&UkmService::RotateLog, self_ptr_factory_.GetWeakPtr()); | 128 base::Bind(&UkmService::RotateLog, self_ptr_factory_.GetWeakPtr()); |
| 129 // MetricsServiceClient outlives UkmService, and | 129 // MetricsServiceClient outlives UkmService, and |
| 130 // MetricsReportingScheduler is tied to the lifetime of |this|. | 130 // MetricsReportingScheduler is tied to the lifetime of |this|. |
| 131 const base::Callback<base::TimeDelta(void)>& get_upload_interval_callback = | 131 const base::Callback<base::TimeDelta(void)>& get_upload_interval_callback = |
| 132 base::Bind(&metrics::MetricsServiceClient::GetStandardUploadInterval, | 132 base::Bind(&metrics::MetricsServiceClient::GetStandardUploadInterval, |
| 133 base::Unretained(client_)); | 133 base::Unretained(client_)); |
| 134 scheduler_.reset(new ukm::MetricsReportingScheduler( | 134 scheduler_.reset(new ukm::MetricsReportingScheduler( |
| 135 rotate_callback, get_upload_interval_callback)); | 135 rotate_callback, get_upload_interval_callback)); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 180 for (auto& provider : metrics_providers_) | 180 for (auto& provider : metrics_providers_) |
| 181 provider->OnRecordingDisabled(); | 181 provider->OnRecordingDisabled(); |
| 182 | 182 |
| 183 scheduler_->Stop(); | 183 scheduler_->Stop(); |
| 184 Flush(); | 184 Flush(); |
| 185 } | 185 } |
| 186 | 186 |
| 187 void UkmService::Flush() { | 187 void UkmService::Flush() { |
| 188 if (initialize_complete_) | 188 if (initialize_complete_) |
| 189 BuildAndStoreLog(); | 189 BuildAndStoreLog(); |
| 190 persisted_logs_.SerializeLogs(); | 190 persisted_logs_.PersistUnsentLogs(); |
| 191 } | 191 } |
| 192 | 192 |
| 193 void UkmService::Purge() { | 193 void UkmService::Purge() { |
| 194 DVLOG(1) << "UkmService::Purge"; | 194 DVLOG(1) << "UkmService::Purge"; |
| 195 persisted_logs_.Purge(); | 195 persisted_logs_.Purge(); |
| 196 sources_.clear(); | 196 sources_.clear(); |
| 197 } | 197 } |
| 198 | 198 |
| 199 void UkmService::ResetClientId() { | 199 void UkmService::ResetClientId() { |
| 200 client_id_ = GenerateClientId(pref_service_); | 200 client_id_ = GenerateClientId(pref_service_); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 223 DCHECK(thread_checker_.CalledOnValidThread()); | 223 DCHECK(thread_checker_.CalledOnValidThread()); |
| 224 DVLOG(1) << "UkmService::FinishedInitTask"; | 224 DVLOG(1) << "UkmService::FinishedInitTask"; |
| 225 initialize_complete_ = true; | 225 initialize_complete_ = true; |
| 226 scheduler_->InitTaskComplete(); | 226 scheduler_->InitTaskComplete(); |
| 227 } | 227 } |
| 228 | 228 |
| 229 void UkmService::RotateLog() { | 229 void UkmService::RotateLog() { |
| 230 DCHECK(thread_checker_.CalledOnValidThread()); | 230 DCHECK(thread_checker_.CalledOnValidThread()); |
| 231 DCHECK(!log_upload_in_progress_); | 231 DCHECK(!log_upload_in_progress_); |
| 232 DVLOG(1) << "UkmService::RotateLog"; | 232 DVLOG(1) << "UkmService::RotateLog"; |
| 233 if (persisted_logs_.empty()) { | 233 if (!persisted_logs_.has_unsent_logs()) { |
|
Alexei Svitkine (slow)
2017/02/22 20:43:46
Nit: No {}'s
Steven Holte
2017/02/22 22:15:54
Done.
| |
| 234 BuildAndStoreLog(); | 234 BuildAndStoreLog(); |
| 235 } | 235 } |
| 236 StartScheduledUpload(); | 236 StartScheduledUpload(); |
| 237 } | 237 } |
| 238 | 238 |
| 239 void UkmService::BuildAndStoreLog() { | 239 void UkmService::BuildAndStoreLog() { |
| 240 DCHECK(thread_checker_.CalledOnValidThread()); | 240 DCHECK(thread_checker_.CalledOnValidThread()); |
| 241 DVLOG(1) << "UkmService::BuildAndStoreLog"; | 241 DVLOG(1) << "UkmService::BuildAndStoreLog"; |
| 242 // Suppress generating a log if we have no new data to include. | 242 // Suppress generating a log if we have no new data to include. |
| 243 if (sources_.empty()) | 243 if (sources_.empty()) |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 261 } | 261 } |
| 262 | 262 |
| 263 std::string serialized_log; | 263 std::string serialized_log; |
| 264 report.SerializeToString(&serialized_log); | 264 report.SerializeToString(&serialized_log); |
| 265 persisted_logs_.StoreLog(serialized_log); | 265 persisted_logs_.StoreLog(serialized_log); |
| 266 } | 266 } |
| 267 | 267 |
| 268 void UkmService::StartScheduledUpload() { | 268 void UkmService::StartScheduledUpload() { |
| 269 DCHECK(thread_checker_.CalledOnValidThread()); | 269 DCHECK(thread_checker_.CalledOnValidThread()); |
| 270 DCHECK(!log_upload_in_progress_); | 270 DCHECK(!log_upload_in_progress_); |
| 271 if (persisted_logs_.empty()) { | 271 if (!persisted_logs_.has_unsent_logs()) { |
| 272 // No logs to send, so early out and schedule the next rotation. | 272 // No logs to send, so early out and schedule the next rotation. |
| 273 scheduler_->UploadFinished(true, /* has_unsent_logs */ false); | 273 scheduler_->UploadFinished(true, /* has_unsent_logs */ false); |
| 274 return; | 274 return; |
| 275 } | 275 } |
| 276 if (!persisted_logs_.has_staged_log()) | 276 if (!persisted_logs_.has_staged_log()) |
| 277 persisted_logs_.StageLog(); | 277 persisted_logs_.StageNextLog(); |
| 278 // TODO(holte): Handle data usage on cellular, etc. | 278 // TODO(holte): Handle data usage on cellular, etc. |
| 279 if (!log_uploader_) { | 279 if (!log_uploader_) { |
| 280 log_uploader_ = client_->CreateUploader( | 280 log_uploader_ = client_->CreateUploader( |
| 281 GetServerUrl(), kMimeType, base::Bind(&UkmService::OnLogUploadComplete, | 281 GetServerUrl(), kMimeType, base::Bind(&UkmService::OnLogUploadComplete, |
| 282 self_ptr_factory_.GetWeakPtr())); | 282 self_ptr_factory_.GetWeakPtr())); |
| 283 } | 283 } |
| 284 log_upload_in_progress_ = true; | 284 log_upload_in_progress_ = true; |
| 285 | 285 |
| 286 const std::string hash = | 286 const std::string hash = |
| 287 base::HexEncode(persisted_logs_.staged_log_hash().data(), | 287 base::HexEncode(persisted_logs_.staged_log_hash().data(), |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 305 if (upload_succeeded) { | 305 if (upload_succeeded) { |
| 306 UMA_HISTOGRAM_COUNTS_10000("UKM.LogSize.OnSuccess", log_size_bytes / 1024); | 306 UMA_HISTOGRAM_COUNTS_10000("UKM.LogSize.OnSuccess", log_size_bytes / 1024); |
| 307 } else if (response_code == 400) { | 307 } else if (response_code == 400) { |
| 308 // Bad syntax. Retransmission won't work. | 308 // Bad syntax. Retransmission won't work. |
| 309 discard_log = true; | 309 discard_log = true; |
| 310 } | 310 } |
| 311 | 311 |
| 312 if (upload_succeeded || discard_log) { | 312 if (upload_succeeded || discard_log) { |
| 313 persisted_logs_.DiscardStagedLog(); | 313 persisted_logs_.DiscardStagedLog(); |
| 314 // Store the updated list to disk now that the removed log is uploaded. | 314 // Store the updated list to disk now that the removed log is uploaded. |
| 315 persisted_logs_.SerializeLogs(); | 315 persisted_logs_.PersistUnsentLogs(); |
| 316 } | 316 } |
| 317 | 317 |
| 318 // Error 400 indicates a problem with the log, not with the server, so | 318 // Error 400 indicates a problem with the log, not with the server, so |
| 319 // don't consider that a sign that the server is in trouble. | 319 // don't consider that a sign that the server is in trouble. |
| 320 bool server_is_healthy = upload_succeeded || response_code == 400; | 320 bool server_is_healthy = upload_succeeded || response_code == 400; |
| 321 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); | 321 scheduler_->UploadFinished(server_is_healthy, |
| 322 persisted_logs_.has_unsent_logs()); | |
| 322 } | 323 } |
| 323 | 324 |
| 324 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { | 325 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { |
| 325 if (!recording_enabled_) { | 326 if (!recording_enabled_) { |
| 326 RecordDroppedSource(DroppedSourceReason::RECORDING_DISABLED); | 327 RecordDroppedSource(DroppedSourceReason::RECORDING_DISABLED); |
| 327 return; | 328 return; |
| 328 } | 329 } |
| 329 if (sources_.size() >= kMaxSources) { | 330 if (sources_.size() >= kMaxSources) { |
| 330 RecordDroppedSource(DroppedSourceReason::MAX_SOURCES_HIT); | 331 RecordDroppedSource(DroppedSourceReason::MAX_SOURCES_HIT); |
| 331 return; | 332 return; |
| 332 } | 333 } |
| 333 | 334 |
| 334 sources_.push_back(std::move(source)); | 335 sources_.push_back(std::move(source)); |
| 335 } | 336 } |
| 336 | 337 |
| 337 } // namespace ukm | 338 } // namespace ukm |
| OLD | NEW |