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

Side by Side Diff: components/ukm/ukm_service.cc

Issue 2737953002: UKM: Remove UkmService::RecordSource (Closed)
Patch Set: Created 3 years, 9 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 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
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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 persisted_logs_.PersistUnsentLogs(); 416 persisted_logs_.PersistUnsentLogs();
417 } 417 }
418 418
419 // Error 400 indicates a problem with the log, not with the server, so 419 // Error 400 indicates a problem with the log, not with the server, so
420 // don't consider that a sign that the server is in trouble. 420 // don't consider that a sign that the server is in trouble.
421 bool server_is_healthy = upload_succeeded || response_code == 400; 421 bool server_is_healthy = upload_succeeded || response_code == 400;
422 scheduler_->UploadFinished(server_is_healthy, 422 scheduler_->UploadFinished(server_is_healthy,
423 persisted_logs_.has_unsent_logs()); 423 persisted_logs_.has_unsent_logs());
424 } 424 }
425 425
426 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) {
427 DCHECK(thread_checker_.CalledOnValidThread());
428
429 if (!recording_enabled_) {
430 RecordDroppedSource(DroppedDataReason::RECORDING_DISABLED);
431 return;
432 }
433 if (sources_.size() >= GetMaxSources()) {
434 RecordDroppedSource(DroppedDataReason::MAX_HIT);
435 return;
436 }
437
438 sources_.push_back(std::move(source));
439 }
440
441 // static 426 // static
442 int32_t UkmService::GetNewSourceID() { 427 int32_t UkmService::GetNewSourceID() {
443 static int32_t next_source_id = 0; 428 static int32_t next_source_id = 0;
444 return next_source_id++; 429 return next_source_id++;
445 } 430 }
446 431
447 std::unique_ptr<UkmEntryBuilder> UkmService::GetEntryBuilder( 432 std::unique_ptr<UkmEntryBuilder> UkmService::GetEntryBuilder(
448 int32_t source_id, 433 int32_t source_id,
449 const char* event_name) { 434 const char* event_name) {
450 return std::unique_ptr<UkmEntryBuilder>(new UkmEntryBuilder( 435 return std::unique_ptr<UkmEntryBuilder>(new UkmEntryBuilder(
451 base::Bind(&UkmService::AddEntry, base::Unretained(this)), source_id, 436 base::Bind(&UkmService::AddEntry, base::Unretained(this)), source_id,
452 event_name)); 437 event_name));
453 } 438 }
454 439
455 void UkmService::UpdateSourceURL(int32_t source_id, const GURL& url) { 440 void UkmService::UpdateSourceURL(int32_t source_id, const GURL& url) {
456 DCHECK(thread_checker_.CalledOnValidThread()); 441 DCHECK(thread_checker_.CalledOnValidThread());
457 442
458 if (!recording_enabled_) { 443 if (!recording_enabled_) {
459 RecordDroppedSource(DroppedDataReason::RECORDING_DISABLED); 444 RecordDroppedSource(DroppedDataReason::RECORDING_DISABLED);
460 return; 445 return;
461 } 446 }
462 447
463 // Update the pre-existing source if there is any. This happens when the 448 // Update the pre-existing source if there is any. This happens when the
464 // initial URL is different from the committed URL for the same source, e.g., 449 // initial URL is different from the committed URL for the same source, e.g.,
465 // when there is redirection. 450 // when there is redirection.
466 for (auto& source : sources_) { 451 if (sources_.find(source_id) != sources_.end()) {
rkaplow 2017/03/08 18:49:20 can use ContainsKey https://codesearch.chromium.o
Zhen Wang 2017/03/08 19:25:51 Done.
467 if (source_id != source->id()) 452 DCHECK(sources_[source_id]);
rkaplow 2017/03/08 18:49:20 i don't think this dcheck is needed, by definition
Zhen Wang 2017/03/08 19:25:52 Done.
468 continue; 453 sources_[source_id]->UpdateUrl(url);
469
470 source->UpdateUrl(url);
471 return; 454 return;
472 } 455 }
473 456
474 if (sources_.size() >= GetMaxSources()) { 457 if (sources_.size() >= GetMaxSources()) {
475 RecordDroppedSource(DroppedDataReason::MAX_HIT); 458 RecordDroppedSource(DroppedDataReason::MAX_HIT);
476 return; 459 return;
477 } 460 }
478 std::unique_ptr<UkmSource> source = base::MakeUnique<UkmSource>(); 461 std::unique_ptr<UkmSource> source = base::MakeUnique<UkmSource>();
479 source->set_id(source_id); 462 source->set_id(source_id);
480 source->set_url(url); 463 source->set_url(url);
481 sources_.push_back(std::move(source)); 464 sources_.insert(std::make_pair(source_id, std::move(source)));
482 } 465 }
483 466
484 void UkmService::AddEntry(std::unique_ptr<UkmEntry> entry) { 467 void UkmService::AddEntry(std::unique_ptr<UkmEntry> entry) {
485 DCHECK(thread_checker_.CalledOnValidThread()); 468 DCHECK(thread_checker_.CalledOnValidThread());
486 469
487 if (!recording_enabled_) { 470 if (!recording_enabled_) {
488 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED); 471 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED);
489 return; 472 return;
490 } 473 }
491 if (entries_.size() >= GetMaxEntries()) { 474 if (entries_.size() >= GetMaxEntries()) {
492 RecordDroppedEntry(DroppedDataReason::MAX_HIT); 475 RecordDroppedEntry(DroppedDataReason::MAX_HIT);
493 return; 476 return;
494 } 477 }
495 478
496 entries_.push_back(std::move(entry)); 479 entries_.push_back(std::move(entry));
497 } 480 }
498 481
499 } // namespace ukm 482 } // namespace ukm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698