metrics_service_client.cc 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/metrics/metrics_service_client.h"
  5. #include <algorithm>
  6. #include <string>
  7. #include "base/command_line.h"
  8. #include "base/logging.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/strings/string_util.h"
  11. #include "build/build_config.h"
  12. #include "components/metrics/metrics_switches.h"
  13. #include "components/metrics/url_constants.h"
  14. namespace metrics {
  15. namespace {
  16. // The minimum time in seconds between consecutive metrics report uploads.
  17. constexpr int kMetricsUploadIntervalSecMinimum = 20;
  18. // If a metrics log upload fails, and the transmission is over this byte count,
  19. // then we will discard the log, and not try to retransmit it. We also don't
  20. // persist the log to the prefs for transmission during the next chrome session
  21. // if this limit is exceeded.
  22. #if BUILDFLAG(IS_CHROMEOS)
  23. // Increase CrOS limit to accommodate SampledProfile data (crbug.com/1210595).
  24. constexpr size_t kMaxOngoingLogSize = 1024 * 1024; // 1 MiB
  25. #else
  26. constexpr size_t kMaxOngoingLogSize = 100 * 1024; // 100 KiB
  27. #endif // BUILDFLAG(IS_CHROMEOS)
  28. // The number of bytes of logs to save of each type (initial/ongoing). This
  29. // ensures that a reasonable amount of history will be stored even if there is a
  30. // long series of very small logs.
  31. constexpr size_t kMinLogQueueSize = 300 * 1024; // 300 KiB
  32. // The minimum number of "initial" logs to save, and hope to send during a
  33. // future Chrome session. Initial logs contain crash stats, and are pretty
  34. // small.
  35. constexpr size_t kMinInitialLogQueueCount = 20;
  36. // The minimum number of ongoing logs to save persistently, and hope to send
  37. // during a this or future sessions. Note that each log may be pretty large, as
  38. // presumably the related "initial" log wasn't sent (probably nothing was, as
  39. // the user was probably off-line). As a result, the log probably kept
  40. // accumulating while the "initial" log was stalled, and couldn't be sent. As a
  41. // result, we don't want to save too many of these mega-logs. A "standard
  42. // shutdown" will create a small log, including just the data that was not yet
  43. // been transmitted, and that is normal (to have exactly one ongoing_log_ at
  44. // startup).
  45. constexpr size_t kMinOngoingLogQueueCount = 8;
  46. } // namespace
  47. MetricsServiceClient::MetricsServiceClient() {}
  48. MetricsServiceClient::~MetricsServiceClient() {}
  49. ukm::UkmService* MetricsServiceClient::GetUkmService() {
  50. return nullptr;
  51. }
  52. bool MetricsServiceClient::ShouldUploadMetricsForUserId(uint64_t user_id) {
  53. return true;
  54. }
  55. GURL MetricsServiceClient::GetMetricsServerUrl() {
  56. #ifndef NDEBUG
  57. // Only allow overriding the server URL through the command line in debug
  58. // builds. This is to prevent, for example, rerouting metrics due to malware.
  59. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  60. if (command_line->HasSwitch(switches::kUmaServerUrl))
  61. return GURL(command_line->GetSwitchValueASCII(switches::kUmaServerUrl));
  62. #endif // NDEBUG
  63. return GURL(kNewMetricsServerUrl);
  64. }
  65. GURL MetricsServiceClient::GetInsecureMetricsServerUrl() {
  66. #ifndef NDEBUG
  67. // Only allow overriding the server URL through the command line in debug
  68. // builds. This is to prevent, for example, rerouting metrics due to malware.
  69. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  70. if (command_line->HasSwitch(switches::kUmaInsecureServerUrl)) {
  71. return GURL(
  72. command_line->GetSwitchValueASCII(switches::kUmaInsecureServerUrl));
  73. }
  74. #endif // NDEBUG
  75. return GURL(kNewMetricsServerUrlInsecure);
  76. }
  77. base::TimeDelta MetricsServiceClient::GetUploadInterval() {
  78. const base::CommandLine* command_line =
  79. base::CommandLine::ForCurrentProcess();
  80. // If an upload interval is set from the command line, use that value but
  81. // subject it to a minimum threshold to mitigate the risk of DDoS attack.
  82. if (command_line->HasSwitch(metrics::switches::kMetricsUploadIntervalSec)) {
  83. const std::string switch_value = command_line->GetSwitchValueASCII(
  84. metrics::switches::kMetricsUploadIntervalSec);
  85. int custom_upload_interval;
  86. if (base::StringToInt(switch_value, &custom_upload_interval)) {
  87. return base::Seconds(
  88. std::max(custom_upload_interval, kMetricsUploadIntervalSecMinimum));
  89. }
  90. LOG(DFATAL) << "Malformed value for --metrics-upload-interval. "
  91. << "Expected int, got: " << switch_value;
  92. }
  93. return GetStandardUploadInterval();
  94. }
  95. bool MetricsServiceClient::ShouldStartUpFastForTesting() const {
  96. return false;
  97. }
  98. bool MetricsServiceClient::IsReportingPolicyManaged() {
  99. return false;
  100. }
  101. EnableMetricsDefault MetricsServiceClient::GetMetricsReportingDefaultState() {
  102. return EnableMetricsDefault::DEFAULT_UNKNOWN;
  103. }
  104. bool MetricsServiceClient::IsUMACellularUploadLogicEnabled() {
  105. return false;
  106. }
  107. bool MetricsServiceClient::IsExternalExperimentAllowlistEnabled() {
  108. return true;
  109. }
  110. bool MetricsServiceClient::IsUkmAllowedForAllProfiles() {
  111. return false;
  112. }
  113. bool MetricsServiceClient::IsUkmAllowedWithExtensionsForAllProfiles() {
  114. return false;
  115. }
  116. bool MetricsServiceClient::AreNotificationListenersEnabledOnAllProfiles() {
  117. return false;
  118. }
  119. std::string MetricsServiceClient::GetAppPackageNameIfLoggable() {
  120. return std::string();
  121. }
  122. std::string MetricsServiceClient::GetUploadSigningKey() {
  123. return std::string();
  124. }
  125. bool MetricsServiceClient::ShouldResetClientIdsOnClonedInstall() {
  126. return false;
  127. }
  128. MetricsLogStore::StorageLimits MetricsServiceClient::GetStorageLimits() const {
  129. return {
  130. /*min_initial_log_queue_count=*/kMinInitialLogQueueCount,
  131. /*min_initial_log_queue_size=*/kMinLogQueueSize,
  132. /*min_ongoing_log_queue_count=*/kMinOngoingLogQueueCount,
  133. /*min_ongoing_log_queue_size=*/kMinLogQueueSize,
  134. /*max_ongoing_log_size=*/kMaxOngoingLogSize,
  135. };
  136. }
  137. void MetricsServiceClient::SetUpdateRunningServicesCallback(
  138. const base::RepeatingClosure& callback) {
  139. update_running_services_ = callback;
  140. }
  141. void MetricsServiceClient::UpdateRunningServices() {
  142. if (update_running_services_)
  143. update_running_services_.Run();
  144. }
  145. bool MetricsServiceClient::IsMetricsReportingForceEnabled() const {
  146. return ::metrics::IsMetricsReportingForceEnabled();
  147. }
  148. absl::optional<bool> MetricsServiceClient::GetCurrentUserMetricsConsent()
  149. const {
  150. return absl::nullopt;
  151. }
  152. absl::optional<std::string> MetricsServiceClient::GetCurrentUserId() const {
  153. return absl::nullopt;
  154. }
  155. } // namespace metrics