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.
4
5 #include "components/metrics/metrics_service_client.h"
6
7 #include <algorithm>
8 #include <string>
9
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"
19
20 namespace metrics {
21
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",
26 300 * 1024 // 300 KiB
27 };
28
29 const base::FeatureParam<int> kMinOngoingLogQueueCount{
30 &features::kStructuredMetrics, "max_ongoing_log_queue_count", 8};
31
32 namespace {
33
34 // The minimum time in seconds between consecutive metrics report uploads.
35 constexpr int kMetricsUploadIntervalSecMinimum = 20;
36
37 // Initial logs can be of any size.
38 constexpr size_t kMaxInitialLogSize = 0;
39
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
47 #else
48 constexpr size_t kMaxOngoingLogSize = 100 * 1024; // 100 KiB
49 #endif // BUILDFLAG(IS_CHROMEOS)
50
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;
54
55 } // namespace
56
MetricsServiceClient()57 MetricsServiceClient::MetricsServiceClient() {}
58
~MetricsServiceClient()59 MetricsServiceClient::~MetricsServiceClient() {}
60
GetUkmService()61 ukm::UkmService* MetricsServiceClient::GetUkmService() {
62 return nullptr;
63 }
64
65 structured::StructuredMetricsService*
GetStructuredMetricsService()66 MetricsServiceClient::GetStructuredMetricsService() {
67 return nullptr;
68 }
69
ShouldUploadMetricsForUserId(uint64_t user_id)70 bool MetricsServiceClient::ShouldUploadMetricsForUserId(uint64_t user_id) {
71 return true;
72 }
73
GetMetricsServerUrl()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));
78 }
79 return GURL(kNewMetricsServerUrl);
80 }
81
GetInsecureMetricsServerUrl()82 GURL MetricsServiceClient::GetInsecureMetricsServerUrl() {
83 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
84 if (command_line->HasSwitch(switches::kUmaInsecureServerUrl)) {
85 return GURL(
86 command_line->GetSwitchValueASCII(switches::kUmaInsecureServerUrl));
87 }
88 return GURL(kNewMetricsServerUrlInsecure);
89 }
90
GetUploadInterval()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));
103 }
104 LOG(DFATAL) << "Malformed value for --metrics-upload-interval. "
105 << "Expected int, got: " << switch_value;
106 }
107 return GetStandardUploadInterval();
108 }
109
ShouldStartUpFastForTesting() const110 bool MetricsServiceClient::ShouldStartUpFastForTesting() const {
111 return false;
112 }
113
IsReportingPolicyManaged()114 bool MetricsServiceClient::IsReportingPolicyManaged() {
115 return false;
116 }
117
GetMetricsReportingDefaultState()118 EnableMetricsDefault MetricsServiceClient::GetMetricsReportingDefaultState() {
119 return EnableMetricsDefault::DEFAULT_UNKNOWN;
120 }
121
IsOnCellularConnection()122 bool MetricsServiceClient::IsOnCellularConnection() {
123 return false;
124 }
125
IsExternalExperimentAllowlistEnabled()126 bool MetricsServiceClient::IsExternalExperimentAllowlistEnabled() {
127 return true;
128 }
129
IsUkmAllowedForAllProfiles()130 bool MetricsServiceClient::IsUkmAllowedForAllProfiles() {
131 return false;
132 }
133
AreNotificationListenersEnabledOnAllProfiles()134 bool MetricsServiceClient::AreNotificationListenersEnabledOnAllProfiles() {
135 return false;
136 }
137
GetAppPackageNameIfLoggable()138 std::string MetricsServiceClient::GetAppPackageNameIfLoggable() {
139 return std::string();
140 }
141
GetUploadSigningKey()142 std::string MetricsServiceClient::GetUploadSigningKey() {
143 return std::string();
144 }
145
ShouldResetClientIdsOnClonedInstall()146 bool MetricsServiceClient::ShouldResetClientIdsOnClonedInstall() {
147 return false;
148 }
149
150 base::CallbackListSubscription
AddOnClonedInstallDetectedCallback(base::OnceClosure callback)151 MetricsServiceClient::AddOnClonedInstallDetectedCallback(
152 base::OnceClosure callback) {
153 return base::CallbackListSubscription();
154 }
155
GetStorageLimits() const156 MetricsLogStore::StorageLimits MetricsServiceClient::GetStorageLimits() const {
157 return {
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),
164 },
165 .ongoing_log_queue_limits =
166 UnsentLogStore::UnsentLogStoreLimits{
167 .min_log_count =
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),
172 },
173 };
174 }
175
SetUpdateRunningServicesCallback(const base::RepeatingClosure & callback)176 void MetricsServiceClient::SetUpdateRunningServicesCallback(
177 const base::RepeatingClosure& callback) {
178 update_running_services_ = callback;
179 }
180
UpdateRunningServices()181 void MetricsServiceClient::UpdateRunningServices() {
182 if (update_running_services_) {
183 update_running_services_.Run();
184 }
185 }
186
IsMetricsReportingForceEnabled() const187 bool MetricsServiceClient::IsMetricsReportingForceEnabled() const {
188 return ::metrics::IsMetricsReportingForceEnabled();
189 }
190
GetCurrentUserMetricsConsent() const191 absl::optional<bool> MetricsServiceClient::GetCurrentUserMetricsConsent()
192 const {
193 return absl::nullopt;
194 }
195
GetCurrentUserId() const196 absl::optional<std::string> MetricsServiceClient::GetCurrentUserId() const {
197 return absl::nullopt;
198 }
199
200 } // namespace metrics
201