| 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 |
| 11 #include "base/feature_list.h" | 11 #include "base/feature_list.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
| 16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "components/metrics/metrics_log_uploader.h" | 18 #include "components/metrics/metrics_log_uploader.h" |
| 19 #include "components/metrics/metrics_service_client.h" | 19 #include "components/metrics/metrics_service_client.h" |
| 20 #include "components/metrics/proto/ukm/report.pb.h" | 20 #include "components/metrics/proto/ukm/report.pb.h" |
| 21 #include "components/metrics/proto/ukm/source.pb.h" |
| 21 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
| 22 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 23 #include "components/ukm/metrics_reporting_scheduler.h" | 24 #include "components/ukm/metrics_reporting_scheduler.h" |
| 24 #include "components/ukm/persisted_logs_metrics_impl.h" | 25 #include "components/ukm/persisted_logs_metrics_impl.h" |
| 25 #include "components/ukm/ukm_pref_names.h" | 26 #include "components/ukm/ukm_pref_names.h" |
| 27 #include "components/ukm/ukm_source.h" |
| 26 #include "components/variations/variations_associated_data.h" | 28 #include "components/variations/variations_associated_data.h" |
| 27 | 29 |
| 28 namespace ukm { | 30 namespace ukm { |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 constexpr char kMimeType[] = "application/vnd.chrome.ukm"; | 34 constexpr char kMimeType[] = "application/vnd.chrome.ukm"; |
| 33 | 35 |
| 34 // The UKM server's URL. | 36 // The UKM server's URL. |
| 35 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm"; | 37 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm"; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 47 // This ensures that a reasonable amount of history will be stored even if there | 49 // This ensures that a reasonable amount of history will be stored even if there |
| 48 // is a long series of very small logs. | 50 // is a long series of very small logs. |
| 49 constexpr int kMinPersistedBytes = 300000; | 51 constexpr int kMinPersistedBytes = 300000; |
| 50 | 52 |
| 51 // If an upload fails, and the transmission was over this byte count, then we | 53 // If an upload fails, and the transmission was over this byte count, then we |
| 52 // will discard the log, and not try to retransmit it. We also don't persist | 54 // will discard the log, and not try to retransmit it. We also don't persist |
| 53 // the log to the prefs for transmission during the next chrome session if this | 55 // the log to the prefs for transmission during the next chrome session if this |
| 54 // limit is exceeded. | 56 // limit is exceeded. |
| 55 constexpr size_t kMaxLogRetransmitSize = 100 * 1024; | 57 constexpr size_t kMaxLogRetransmitSize = 100 * 1024; |
| 56 | 58 |
| 59 // Maximum number of Sources we'll keep in memory before discarding any |
| 60 // new ones being added. |
| 61 const size_t kMaxSources = 100; |
| 62 |
| 57 std::string GetServerUrl() { | 63 std::string GetServerUrl() { |
| 58 std::string server_url = | 64 std::string server_url = |
| 59 variations::GetVariationParamValueByFeature(kUkmFeature, "ServerUrl"); | 65 variations::GetVariationParamValueByFeature(kUkmFeature, "ServerUrl"); |
| 60 if (!server_url.empty()) | 66 if (!server_url.empty()) |
| 61 return server_url; | 67 return server_url; |
| 62 return kDefaultServerUrl; | 68 return kDefaultServerUrl; |
| 63 } | 69 } |
| 64 | 70 |
| 65 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { | 71 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { |
| 66 uint64_t client_id = pref_service->GetInt64(prefs::kUkmClientId); | 72 uint64_t client_id = pref_service->GetInt64(prefs::kUkmClientId); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 146 |
| 141 void UkmService::Flush() { | 147 void UkmService::Flush() { |
| 142 if (initialize_complete_) | 148 if (initialize_complete_) |
| 143 BuildAndStoreLog(); | 149 BuildAndStoreLog(); |
| 144 persisted_logs_.SerializeLogs(); | 150 persisted_logs_.SerializeLogs(); |
| 145 } | 151 } |
| 146 | 152 |
| 147 void UkmService::Purge() { | 153 void UkmService::Purge() { |
| 148 DVLOG(1) << "UkmService::Purge"; | 154 DVLOG(1) << "UkmService::Purge"; |
| 149 persisted_logs_.Purge(); | 155 persisted_logs_.Purge(); |
| 150 // TODO(oystein): Delete any stored sources. | 156 sources_.clear(); |
| 151 } | 157 } |
| 152 | 158 |
| 153 // static | 159 // static |
| 154 void UkmService::RegisterPrefs(PrefRegistrySimple* registry) { | 160 void UkmService::RegisterPrefs(PrefRegistrySimple* registry) { |
| 155 registry->RegisterInt64Pref(prefs::kUkmClientId, 0); | 161 registry->RegisterInt64Pref(prefs::kUkmClientId, 0); |
| 156 registry->RegisterListPref(prefs::kUkmPersistedLogs); | 162 registry->RegisterListPref(prefs::kUkmPersistedLogs); |
| 157 } | 163 } |
| 158 | 164 |
| 159 void UkmService::StartInitTask() { | 165 void UkmService::StartInitTask() { |
| 160 DCHECK(thread_checker_.CalledOnValidThread()); | 166 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 180 } | 186 } |
| 181 StartScheduledUpload(); | 187 StartScheduledUpload(); |
| 182 } | 188 } |
| 183 | 189 |
| 184 void UkmService::BuildAndStoreLog() { | 190 void UkmService::BuildAndStoreLog() { |
| 185 DCHECK(thread_checker_.CalledOnValidThread()); | 191 DCHECK(thread_checker_.CalledOnValidThread()); |
| 186 DVLOG(1) << "UkmService::BuildAndStoreLog"; | 192 DVLOG(1) << "UkmService::BuildAndStoreLog"; |
| 187 Report report; | 193 Report report; |
| 188 report.set_client_id(client_id_); | 194 report.set_client_id(client_id_); |
| 189 // TODO(holte): Populate system_profile. | 195 // TODO(holte): Populate system_profile. |
| 190 // TODO(oystein): Populate sources. | 196 |
| 197 for (const auto& source : sources_) { |
| 198 Source* proto_source = report.add_sources(); |
| 199 source->PopulateProto(proto_source); |
| 200 } |
| 201 UMA_HISTOGRAM_COUNTS_1000("UKM.Sources.SerializedCount", sources_.size()); |
| 202 sources_.clear(); |
| 203 |
| 191 std::string serialized_log; | 204 std::string serialized_log; |
| 192 report.SerializeToString(&serialized_log); | 205 report.SerializeToString(&serialized_log); |
| 193 persisted_logs_.StoreLog(serialized_log); | 206 persisted_logs_.StoreLog(serialized_log); |
| 194 } | 207 } |
| 195 | 208 |
| 196 void UkmService::StartScheduledUpload() { | 209 void UkmService::StartScheduledUpload() { |
| 197 DCHECK(thread_checker_.CalledOnValidThread()); | 210 DCHECK(thread_checker_.CalledOnValidThread()); |
| 198 DCHECK(!log_upload_in_progress_); | 211 DCHECK(!log_upload_in_progress_); |
| 199 if (!persisted_logs_.has_staged_log()) | 212 if (!persisted_logs_.has_staged_log()) |
| 200 persisted_logs_.StageLog(); | 213 persisted_logs_.StageLog(); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 // Store the updated list to disk now that the removed log is uploaded. | 250 // Store the updated list to disk now that the removed log is uploaded. |
| 238 persisted_logs_.SerializeLogs(); | 251 persisted_logs_.SerializeLogs(); |
| 239 } | 252 } |
| 240 | 253 |
| 241 // Error 400 indicates a problem with the log, not with the server, so | 254 // Error 400 indicates a problem with the log, not with the server, so |
| 242 // don't consider that a sign that the server is in trouble. | 255 // don't consider that a sign that the server is in trouble. |
| 243 bool server_is_healthy = upload_succeeded || response_code == 400; | 256 bool server_is_healthy = upload_succeeded || response_code == 400; |
| 244 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); | 257 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); |
| 245 } | 258 } |
| 246 | 259 |
| 260 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { |
| 261 if (sources_.size() >= kMaxSources) { |
| 262 UMA_HISTOGRAM_BOOLEAN("UKM.Sources.MaxSourcesHit", true); |
| 263 return; |
| 264 } |
| 265 |
| 266 sources_.push_back(std::move(source)); |
| 267 } |
| 268 |
| 247 } // namespace ukm | 269 } // namespace ukm |
| OLD | NEW |