| 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 20 matching lines...) Expand all Loading... |
| 31 #include "components/ukm/ukm_entry_builder.h" | 31 #include "components/ukm/ukm_entry_builder.h" |
| 32 #include "components/ukm/ukm_pref_names.h" | 32 #include "components/ukm/ukm_pref_names.h" |
| 33 #include "components/ukm/ukm_source.h" | 33 #include "components/ukm/ukm_source.h" |
| 34 | 34 |
| 35 namespace ukm { | 35 namespace ukm { |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 constexpr char kMimeType[] = "application/vnd.chrome.ukm"; | 39 constexpr char kMimeType[] = "application/vnd.chrome.ukm"; |
| 40 | 40 |
| 41 // The UKM server's URL. | |
| 42 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm"; | |
| 43 | |
| 44 // The delay, in seconds, after starting recording before doing expensive | 41 // The delay, in seconds, after starting recording before doing expensive |
| 45 // initialization work. | 42 // initialization work. |
| 46 constexpr int kInitializationDelaySeconds = 5; | 43 constexpr int kInitializationDelaySeconds = 5; |
| 47 | 44 |
| 48 // The number of UKM logs that will be stored in PersistedLogs before logs | 45 // The number of UKM logs that will be stored in PersistedLogs before logs |
| 49 // start being dropped. | 46 // start being dropped. |
| 50 constexpr int kMinPersistedLogs = 8; | 47 constexpr int kMinPersistedLogs = 8; |
| 51 | 48 |
| 52 // The number of bytes UKM logs that will be stored in PersistedLogs before | 49 // The number of bytes UKM logs that will be stored in PersistedLogs before |
| 53 // logs start being dropped. | 50 // logs start being dropped. |
| 54 // This ensures that a reasonable amount of history will be stored even if there | 51 // This ensures that a reasonable amount of history will be stored even if there |
| 55 // is a long series of very small logs. | 52 // is a long series of very small logs. |
| 56 constexpr int kMinPersistedBytes = 300000; | 53 constexpr int kMinPersistedBytes = 300000; |
| 57 | 54 |
| 58 // If an upload fails, and the transmission was over this byte count, then we | 55 // If an upload fails, and the transmission was over this byte count, then we |
| 59 // will discard the log, and not try to retransmit it. We also don't persist | 56 // will discard the log, and not try to retransmit it. We also don't persist |
| 60 // the log to the prefs for transmission during the next chrome session if this | 57 // the log to the prefs for transmission during the next chrome session if this |
| 61 // limit is exceeded. | 58 // limit is exceeded. |
| 62 constexpr size_t kMaxLogRetransmitSize = 100 * 1024; | 59 constexpr size_t kMaxLogRetransmitSize = 100 * 1024; |
| 63 | 60 |
| 64 // Maximum number of Sources we'll keep in memory before discarding any | 61 // Gets the UKM Server URL. |
| 65 // new ones being added. | |
| 66 const size_t kMaxSources = 500; | |
| 67 | |
| 68 // Maximum number of Entries we'll keep in memory before discarding any | |
| 69 // new ones being added. | |
| 70 const size_t kMaxEntries = 5000; | |
| 71 | |
| 72 std::string GetServerUrl() { | 62 std::string GetServerUrl() { |
| 63 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm"; |
| 73 std::string server_url = | 64 std::string server_url = |
| 74 base::GetFieldTrialParamValueByFeature(kUkmFeature, "ServerUrl"); | 65 base::GetFieldTrialParamValueByFeature(kUkmFeature, "ServerUrl"); |
| 75 if (!server_url.empty()) | 66 if (!server_url.empty()) |
| 76 return server_url; | 67 return server_url; |
| 77 return kDefaultServerUrl; | 68 return kDefaultServerUrl; |
| 78 } | 69 } |
| 79 | 70 |
| 71 // Gets the maximum number of Sources we'll keep in memory before discarding any |
| 72 // new ones being added. |
| 73 size_t GetMaxSources() { |
| 74 constexpr size_t kDefaultMaxSources = 500; |
| 75 return static_cast<size_t>(base::GetFieldTrialParamByFeatureAsInt( |
| 76 kUkmFeature, "MaxSources", kDefaultMaxSources)); |
| 77 } |
| 78 |
| 79 // Gets the maximum number of Entries we'll keep in memory before discarding any |
| 80 // new ones being added. |
| 81 size_t GetMaxEntries() { |
| 82 constexpr size_t kDefaultMaxEntries = 5000; |
| 83 return static_cast<size_t>(base::GetFieldTrialParamByFeatureAsInt( |
| 84 kUkmFeature, "MaxEntries", kDefaultMaxEntries)); |
| 85 } |
| 86 |
| 87 // True if we should record the initial_url field of the UKM Source proto. |
| 80 bool ShouldRecordInitialUrl() { | 88 bool ShouldRecordInitialUrl() { |
| 81 return base::GetFieldTrialParamByFeatureAsBool(kUkmFeature, | 89 return base::GetFieldTrialParamByFeatureAsBool(kUkmFeature, |
| 82 "RecordInitialUrl", false); | 90 "RecordInitialUrl", false); |
| 83 } | 91 } |
| 84 | 92 |
| 93 // True if we should record session ids in the UKM Report proto. |
| 85 bool ShouldRecordSessionId() { | 94 bool ShouldRecordSessionId() { |
| 86 return base::GetFieldTrialParamByFeatureAsBool(kUkmFeature, "RecordSessionId", | 95 return base::GetFieldTrialParamByFeatureAsBool(kUkmFeature, "RecordSessionId", |
| 87 false); | 96 false); |
| 88 } | 97 } |
| 89 | 98 |
| 90 // Generates a new client id and stores it in prefs. | 99 // Generates a new client id and stores it in prefs. |
| 91 uint64_t GenerateClientId(PrefService* pref_service) { | 100 uint64_t GenerateClientId(PrefService* pref_service) { |
| 92 uint64_t client_id = 0; | 101 uint64_t client_id = 0; |
| 93 while (!client_id) | 102 while (!client_id) |
| 94 client_id = base::RandUint64(); | 103 client_id = base::RandUint64(); |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 persisted_logs_.has_unsent_logs()); | 420 persisted_logs_.has_unsent_logs()); |
| 412 } | 421 } |
| 413 | 422 |
| 414 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { | 423 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { |
| 415 DCHECK(thread_checker_.CalledOnValidThread()); | 424 DCHECK(thread_checker_.CalledOnValidThread()); |
| 416 | 425 |
| 417 if (!recording_enabled_) { | 426 if (!recording_enabled_) { |
| 418 RecordDroppedSource(DroppedDataReason::RECORDING_DISABLED); | 427 RecordDroppedSource(DroppedDataReason::RECORDING_DISABLED); |
| 419 return; | 428 return; |
| 420 } | 429 } |
| 421 if (sources_.size() >= kMaxSources) { | 430 if (sources_.size() >= GetMaxSources()) { |
| 422 RecordDroppedSource(DroppedDataReason::MAX_HIT); | 431 RecordDroppedSource(DroppedDataReason::MAX_HIT); |
| 423 return; | 432 return; |
| 424 } | 433 } |
| 425 | 434 |
| 426 sources_.push_back(std::move(source)); | 435 sources_.push_back(std::move(source)); |
| 427 } | 436 } |
| 428 | 437 |
| 429 // static | 438 // static |
| 430 int32_t UkmService::GetNewSourceID() { | 439 int32_t UkmService::GetNewSourceID() { |
| 431 static int32_t next_source_id = 0; | 440 static int32_t next_source_id = 0; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 452 // initial URL is different from the committed URL for the same source, e.g., | 461 // initial URL is different from the committed URL for the same source, e.g., |
| 453 // when there is redirection. | 462 // when there is redirection. |
| 454 for (auto& source : sources_) { | 463 for (auto& source : sources_) { |
| 455 if (source_id != source->id()) | 464 if (source_id != source->id()) |
| 456 continue; | 465 continue; |
| 457 | 466 |
| 458 source->UpdateUrl(url); | 467 source->UpdateUrl(url); |
| 459 return; | 468 return; |
| 460 } | 469 } |
| 461 | 470 |
| 462 if (sources_.size() >= kMaxSources) { | 471 if (sources_.size() >= GetMaxSources()) { |
| 463 RecordDroppedSource(DroppedDataReason::MAX_HIT); | 472 RecordDroppedSource(DroppedDataReason::MAX_HIT); |
| 464 return; | 473 return; |
| 465 } | 474 } |
| 466 std::unique_ptr<UkmSource> source = base::MakeUnique<UkmSource>(); | 475 std::unique_ptr<UkmSource> source = base::MakeUnique<UkmSource>(); |
| 467 source->set_id(source_id); | 476 source->set_id(source_id); |
| 468 source->set_url(url); | 477 source->set_url(url); |
| 469 sources_.push_back(std::move(source)); | 478 sources_.push_back(std::move(source)); |
| 470 } | 479 } |
| 471 | 480 |
| 472 void UkmService::AddEntry(std::unique_ptr<UkmEntry> entry) { | 481 void UkmService::AddEntry(std::unique_ptr<UkmEntry> entry) { |
| 473 DCHECK(thread_checker_.CalledOnValidThread()); | 482 DCHECK(thread_checker_.CalledOnValidThread()); |
| 474 | 483 |
| 475 if (!recording_enabled_) { | 484 if (!recording_enabled_) { |
| 476 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED); | 485 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED); |
| 477 return; | 486 return; |
| 478 } | 487 } |
| 479 if (entries_.size() >= kMaxEntries) { | 488 if (entries_.size() >= GetMaxEntries()) { |
| 480 RecordDroppedEntry(DroppedDataReason::MAX_HIT); | 489 RecordDroppedEntry(DroppedDataReason::MAX_HIT); |
| 481 return; | 490 return; |
| 482 } | 491 } |
| 483 | 492 |
| 484 entries_.push_back(std::move(entry)); | 493 entries_.push_back(std::move(entry)); |
| 485 } | 494 } |
| 486 | 495 |
| 487 } // namespace ukm | 496 } // namespace ukm |
| OLD | NEW |