Upload upstream chromium 71.0.3578.0
[platform/framework/web/chromium-efl.git] / components / ukm / ukm_reporting_service.cc
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
3 // found in the LICENSE file.
4
5 // ReportingService specialized to report UKM metrics.
6
7 #include "components/ukm/ukm_reporting_service.h"
8
9 #include <memory>
10
11 #include "base/metrics/field_trial_params.h"
12 #include "base/metrics/histogram_functions.h"
13 #include "base/metrics/histogram_macros.h"
14 #include "components/prefs/pref_registry_simple.h"
15 #include "components/ukm/persisted_logs_metrics_impl.h"
16 #include "components/ukm/ukm_pref_names.h"
17 #include "components/ukm/ukm_service.h"
18
19 namespace ukm {
20
21 namespace {
22
23 // The UKM server's URL.
24 constexpr char kMimeType[] = "application/vnd.chrome.ukm";
25
26 // The number of UKM logs that will be stored in PersistedLogs before logs
27 // start being dropped.
28 constexpr int kMinPersistedLogs = 8;
29
30 // The number of bytes UKM logs that will be stored in PersistedLogs before
31 // logs start being dropped.
32 // This ensures that a reasonable amount of history will be stored even if there
33 // is a long series of very small logs.
34 constexpr int kMinPersistedBytes = 300000;
35
36 // If an upload fails, and the transmission was over this byte count, then we
37 // will discard the log, and not try to retransmit it.  We also don't persist
38 // the log to the prefs for transmission during the next chrome session if this
39 // limit is exceeded.
40 constexpr size_t kMaxLogRetransmitSize = 100 * 1024;
41
42 std::string GetServerUrl() {
43   constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm";
44   std::string server_url =
45       base::GetFieldTrialParamValueByFeature(kUkmFeature, "ServerUrl");
46   if (!server_url.empty())
47     return server_url;
48   return kDefaultServerUrl;
49 }
50
51 }  // namespace
52
53 // static
54 void UkmReportingService::RegisterPrefs(PrefRegistrySimple* registry) {
55   registry->RegisterListPref(prefs::kUkmPersistedLogs);
56   // Base class already registered by MetricsReportingService::RegisterPrefs
57   // ReportingService::RegisterPrefs(registry);
58 }
59
60 UkmReportingService::UkmReportingService(metrics::MetricsServiceClient* client,
61                                          PrefService* local_state)
62     : ReportingService(client, local_state, kMaxLogRetransmitSize),
63       persisted_logs_(std::make_unique<ukm::PersistedLogsMetricsImpl>(),
64                       local_state,
65                       prefs::kUkmPersistedLogs,
66                       kMinPersistedLogs,
67                       kMinPersistedBytes,
68                       kMaxLogRetransmitSize) {}
69
70 UkmReportingService::~UkmReportingService() {}
71
72 metrics::LogStore* UkmReportingService::log_store() {
73   return &persisted_logs_;
74 }
75
76 std::string UkmReportingService::GetUploadUrl() const {
77   return GetServerUrl();
78 }
79
80 std::string UkmReportingService::GetInsecureUploadUrl() const {
81   return "";
82 }
83
84 base::StringPiece UkmReportingService::upload_mime_type() const {
85   return kMimeType;
86 }
87
88 metrics::MetricsLogUploader::MetricServiceType
89 UkmReportingService::service_type() const {
90   return metrics::MetricsLogUploader::UKM;
91 }
92
93 void UkmReportingService::LogCellularConstraint(bool upload_canceled) {
94   UMA_HISTOGRAM_BOOLEAN("UKM.LogUpload.Canceled.CellularConstraint",
95                         upload_canceled);
96 }
97
98 void UkmReportingService::LogResponseOrErrorCode(int response_code,
99                                                  int error_code,
100                                                  bool was_https) {
101   // |was_https| is ignored since all UKM logs are received over HTTPS.
102   base::UmaHistogramSparse("UKM.LogUpload.ResponseOrErrorCode",
103                            response_code >= 0 ? response_code : error_code);
104 }
105
106 void UkmReportingService::LogSuccess(size_t log_size) {
107   UMA_HISTOGRAM_COUNTS_10000("UKM.LogSize.OnSuccess", log_size / 1024);
108 }
109
110 void UkmReportingService::LogLargeRejection(size_t log_size) {}
111
112 }  // namespace metrics