1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/metrics_service_client.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "build/build_config.h"
15 #include "components/metrics/metrics_features.h"
16 #include "components/metrics/metrics_switches.h"
17 #include "components/metrics/url_constants.h"
18 #include "metrics_service_client.h"
22 // TODO(b/282078734): Names "max_*" are the original names when the experiment
23 // first started. These should be renamed after the experiment is over.
24 const base::FeatureParam<int> kMinLogQueueBytes{
25 &features::kStructuredMetrics, "max_log_queue_bytes",
29 const base::FeatureParam<int> kMinOngoingLogQueueCount{
30 &features::kStructuredMetrics, "max_ongoing_log_queue_count", 8};
34 // The minimum time in seconds between consecutive metrics report uploads.
35 constexpr int kMetricsUploadIntervalSecMinimum = 20;
37 // Initial logs can be of any size.
38 constexpr size_t kMaxInitialLogSize = 0;
40 // If a metrics log upload fails, and the transmission is over this byte count,
41 // then we will discard the log, and not try to retransmit it. We also don't
42 // persist the log to the prefs for transmission during the next chrome session
43 // if this limit is exceeded.
44 #if BUILDFLAG(IS_CHROMEOS)
45 // Increase CrOS limit to accommodate SampledProfile data (crbug.com/1210595).
46 constexpr size_t kMaxOngoingLogSize = 1024 * 1024; // 1 MiB
48 constexpr size_t kMaxOngoingLogSize = 100 * 1024; // 100 KiB
49 #endif // BUILDFLAG(IS_CHROMEOS)
51 // The minimum number of "initial" logs to save before logs are dropped. Initial
52 // logs contain crash stats, and are pretty small.
53 constexpr size_t kMinInitialLogQueueCount = 20;
57 MetricsServiceClient::MetricsServiceClient() {}
59 MetricsServiceClient::~MetricsServiceClient() {}
61 ukm::UkmService* MetricsServiceClient::GetUkmService() {
65 structured::StructuredMetricsService*
66 MetricsServiceClient::GetStructuredMetricsService() {
70 bool MetricsServiceClient::ShouldUploadMetricsForUserId(uint64_t user_id) {
74 GURL MetricsServiceClient::GetMetricsServerUrl() {
75 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
76 if (command_line->HasSwitch(switches::kUmaServerUrl)) {
77 return GURL(command_line->GetSwitchValueASCII(switches::kUmaServerUrl));
79 return GURL(kNewMetricsServerUrl);
82 GURL MetricsServiceClient::GetInsecureMetricsServerUrl() {
83 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
84 if (command_line->HasSwitch(switches::kUmaInsecureServerUrl)) {
86 command_line->GetSwitchValueASCII(switches::kUmaInsecureServerUrl));
88 return GURL(kNewMetricsServerUrlInsecure);
91 base::TimeDelta MetricsServiceClient::GetUploadInterval() {
92 const base::CommandLine* command_line =
93 base::CommandLine::ForCurrentProcess();
94 // If an upload interval is set from the command line, use that value but
95 // subject it to a minimum threshold to mitigate the risk of DDoS attack.
96 if (command_line->HasSwitch(metrics::switches::kMetricsUploadIntervalSec)) {
97 const std::string switch_value = command_line->GetSwitchValueASCII(
98 metrics::switches::kMetricsUploadIntervalSec);
99 int custom_upload_interval;
100 if (base::StringToInt(switch_value, &custom_upload_interval)) {
101 return base::Seconds(
102 std::max(custom_upload_interval, kMetricsUploadIntervalSecMinimum));
104 LOG(DFATAL) << "Malformed value for --metrics-upload-interval. "
105 << "Expected int, got: " << switch_value;
107 return GetStandardUploadInterval();
110 bool MetricsServiceClient::ShouldStartUpFastForTesting() const {
114 bool MetricsServiceClient::IsReportingPolicyManaged() {
118 EnableMetricsDefault MetricsServiceClient::GetMetricsReportingDefaultState() {
119 return EnableMetricsDefault::DEFAULT_UNKNOWN;
122 bool MetricsServiceClient::IsOnCellularConnection() {
126 bool MetricsServiceClient::IsExternalExperimentAllowlistEnabled() {
130 bool MetricsServiceClient::IsUkmAllowedForAllProfiles() {
134 bool MetricsServiceClient::AreNotificationListenersEnabledOnAllProfiles() {
138 std::string MetricsServiceClient::GetAppPackageNameIfLoggable() {
139 return std::string();
142 std::string MetricsServiceClient::GetUploadSigningKey() {
143 return std::string();
146 bool MetricsServiceClient::ShouldResetClientIdsOnClonedInstall() {
150 base::CallbackListSubscription
151 MetricsServiceClient::AddOnClonedInstallDetectedCallback(
152 base::OnceClosure callback) {
153 return base::CallbackListSubscription();
156 MetricsLogStore::StorageLimits MetricsServiceClient::GetStorageLimits() const {
158 .initial_log_queue_limits =
159 UnsentLogStore::UnsentLogStoreLimits{
160 .min_log_count = kMinInitialLogQueueCount,
161 .min_queue_size_bytes =
162 static_cast<size_t>(kMinLogQueueBytes.Get()),
163 .max_log_size_bytes = static_cast<size_t>(kMaxInitialLogSize),
165 .ongoing_log_queue_limits =
166 UnsentLogStore::UnsentLogStoreLimits{
168 static_cast<size_t>(kMinOngoingLogQueueCount.Get()),
169 .min_queue_size_bytes =
170 static_cast<size_t>(kMinLogQueueBytes.Get()),
171 .max_log_size_bytes = static_cast<size_t>(kMaxOngoingLogSize),
176 void MetricsServiceClient::SetUpdateRunningServicesCallback(
177 const base::RepeatingClosure& callback) {
178 update_running_services_ = callback;
181 void MetricsServiceClient::UpdateRunningServices() {
182 if (update_running_services_) {
183 update_running_services_.Run();
187 bool MetricsServiceClient::IsMetricsReportingForceEnabled() const {
188 return ::metrics::IsMetricsReportingForceEnabled();
191 absl::optional<bool> MetricsServiceClient::GetCurrentUserMetricsConsent()
193 return absl::nullopt;
196 absl::optional<std::string> MetricsServiceClient::GetCurrentUserId() const {
197 return absl::nullopt;
200 } // namespace metrics