| 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 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 // Suppress generating a log if we have no new data to include. | 327 // Suppress generating a log if we have no new data to include. |
| 328 // TODO(zhenw): add a histogram here to debug if this case is hitting a lot. | 328 // TODO(zhenw): add a histogram here to debug if this case is hitting a lot. |
| 329 if (sources_.empty() && entries_.empty()) | 329 if (sources_.empty() && entries_.empty()) |
| 330 return; | 330 return; |
| 331 | 331 |
| 332 Report report; | 332 Report report; |
| 333 report.set_client_id(client_id_); | 333 report.set_client_id(client_id_); |
| 334 if (ShouldRecordSessionId()) | 334 if (ShouldRecordSessionId()) |
| 335 report.set_session_id(session_id_); | 335 report.set_session_id(session_id_); |
| 336 | 336 |
| 337 for (const auto& source : sources_) { | 337 for (const auto& kv : sources_) { |
| 338 Source* proto_source = report.add_sources(); | 338 Source* proto_source = report.add_sources(); |
| 339 source->PopulateProto(proto_source); | 339 kv.second->PopulateProto(proto_source); |
| 340 if (!ShouldRecordInitialUrl()) | 340 if (!ShouldRecordInitialUrl()) |
| 341 proto_source->clear_initial_url(); | 341 proto_source->clear_initial_url(); |
| 342 } | 342 } |
| 343 for (const auto& entry : entries_) { | 343 for (const auto& entry : entries_) { |
| 344 Entry* proto_entry = report.add_entries(); | 344 Entry* proto_entry = report.add_entries(); |
| 345 entry->PopulateProto(proto_entry); | 345 entry->PopulateProto(proto_entry); |
| 346 } | 346 } |
| 347 | 347 |
| 348 UMA_HISTOGRAM_COUNTS_1000("UKM.Sources.SerializedCount", sources_.size()); | 348 UMA_HISTOGRAM_COUNTS_1000("UKM.Sources.SerializedCount", sources_.size()); |
| 349 UMA_HISTOGRAM_COUNTS_1000("UKM.Entries.SerializedCount", entries_.size()); | 349 UMA_HISTOGRAM_COUNTS_1000("UKM.Entries.SerializedCount", entries_.size()); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 } | 418 } |
| 419 } | 419 } |
| 420 | 420 |
| 421 // Error 400 indicates a problem with the log, not with the server, so | 421 // Error 400 indicates a problem with the log, not with the server, so |
| 422 // don't consider that a sign that the server is in trouble. | 422 // don't consider that a sign that the server is in trouble. |
| 423 bool server_is_healthy = upload_succeeded || response_code == 400; | 423 bool server_is_healthy = upload_succeeded || response_code == 400; |
| 424 scheduler_->UploadFinished(server_is_healthy, | 424 scheduler_->UploadFinished(server_is_healthy, |
| 425 persisted_logs_.has_unsent_logs()); | 425 persisted_logs_.has_unsent_logs()); |
| 426 } | 426 } |
| 427 | 427 |
| 428 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { | |
| 429 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 430 | |
| 431 if (!recording_enabled_) { | |
| 432 RecordDroppedSource(DroppedDataReason::RECORDING_DISABLED); | |
| 433 return; | |
| 434 } | |
| 435 if (sources_.size() >= GetMaxSources()) { | |
| 436 RecordDroppedSource(DroppedDataReason::MAX_HIT); | |
| 437 return; | |
| 438 } | |
| 439 | |
| 440 sources_.push_back(std::move(source)); | |
| 441 } | |
| 442 | |
| 443 // static | 428 // static |
| 444 int32_t UkmService::GetNewSourceID() { | 429 int32_t UkmService::GetNewSourceID() { |
| 445 static int32_t next_source_id = 0; | 430 static int32_t next_source_id = 0; |
| 446 return next_source_id++; | 431 return next_source_id++; |
| 447 } | 432 } |
| 448 | 433 |
| 449 std::unique_ptr<UkmEntryBuilder> UkmService::GetEntryBuilder( | 434 std::unique_ptr<UkmEntryBuilder> UkmService::GetEntryBuilder( |
| 450 int32_t source_id, | 435 int32_t source_id, |
| 451 const char* event_name) { | 436 const char* event_name) { |
| 452 return std::unique_ptr<UkmEntryBuilder>(new UkmEntryBuilder( | 437 return std::unique_ptr<UkmEntryBuilder>(new UkmEntryBuilder( |
| 453 base::Bind(&UkmService::AddEntry, base::Unretained(this)), source_id, | 438 base::Bind(&UkmService::AddEntry, base::Unretained(this)), source_id, |
| 454 event_name)); | 439 event_name)); |
| 455 } | 440 } |
| 456 | 441 |
| 457 void UkmService::UpdateSourceURL(int32_t source_id, const GURL& url) { | 442 void UkmService::UpdateSourceURL(int32_t source_id, const GURL& url) { |
| 458 DCHECK(thread_checker_.CalledOnValidThread()); | 443 DCHECK(thread_checker_.CalledOnValidThread()); |
| 459 | 444 |
| 460 if (!recording_enabled_) { | 445 if (!recording_enabled_) { |
| 461 RecordDroppedSource(DroppedDataReason::RECORDING_DISABLED); | 446 RecordDroppedSource(DroppedDataReason::RECORDING_DISABLED); |
| 462 return; | 447 return; |
| 463 } | 448 } |
| 464 | 449 |
| 465 // Update the pre-existing source if there is any. This happens when the | 450 // Update the pre-existing source if there is any. This happens when the |
| 466 // initial URL is different from the committed URL for the same source, e.g., | 451 // initial URL is different from the committed URL for the same source, e.g., |
| 467 // when there is redirection. | 452 // when there is redirection. |
| 468 for (auto& source : sources_) { | 453 if (base::ContainsKey(sources_, source_id)) { |
| 469 if (source_id != source->id()) | 454 sources_[source_id]->UpdateUrl(url); |
| 470 continue; | |
| 471 | |
| 472 source->UpdateUrl(url); | |
| 473 return; | 455 return; |
| 474 } | 456 } |
| 475 | 457 |
| 476 if (sources_.size() >= GetMaxSources()) { | 458 if (sources_.size() >= GetMaxSources()) { |
| 477 RecordDroppedSource(DroppedDataReason::MAX_HIT); | 459 RecordDroppedSource(DroppedDataReason::MAX_HIT); |
| 478 return; | 460 return; |
| 479 } | 461 } |
| 480 std::unique_ptr<UkmSource> source = base::MakeUnique<UkmSource>(); | 462 std::unique_ptr<UkmSource> source = base::MakeUnique<UkmSource>(); |
| 481 source->set_id(source_id); | 463 source->set_id(source_id); |
| 482 source->set_url(url); | 464 source->set_url(url); |
| 483 sources_.push_back(std::move(source)); | 465 sources_.insert(std::make_pair(source_id, std::move(source))); |
| 484 } | 466 } |
| 485 | 467 |
| 486 void UkmService::AddEntry(std::unique_ptr<UkmEntry> entry) { | 468 void UkmService::AddEntry(std::unique_ptr<UkmEntry> entry) { |
| 487 DCHECK(thread_checker_.CalledOnValidThread()); | 469 DCHECK(thread_checker_.CalledOnValidThread()); |
| 488 | 470 |
| 489 if (!recording_enabled_) { | 471 if (!recording_enabled_) { |
| 490 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED); | 472 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED); |
| 491 return; | 473 return; |
| 492 } | 474 } |
| 493 if (entries_.size() >= GetMaxEntries()) { | 475 if (entries_.size() >= GetMaxEntries()) { |
| 494 RecordDroppedEntry(DroppedDataReason::MAX_HIT); | 476 RecordDroppedEntry(DroppedDataReason::MAX_HIT); |
| 495 return; | 477 return; |
| 496 } | 478 } |
| 497 | 479 |
| 498 entries_.push_back(std::move(entry)); | 480 entries_.push_back(std::move(entry)); |
| 499 } | 481 } |
| 500 | 482 |
| 501 } // namespace ukm | 483 } // namespace ukm |
| OLD | NEW |