| 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 around before dropping new ones. |
| 60 const size_t kMaxSourcesLength = 100; |
| 61 |
| 57 std::string GetServerUrl() { | 62 std::string GetServerUrl() { |
| 58 std::string server_url = | 63 std::string server_url = |
| 59 variations::GetVariationParamValueByFeature(kUkmFeature, "ServerUrl"); | 64 variations::GetVariationParamValueByFeature(kUkmFeature, "ServerUrl"); |
| 60 if (!server_url.empty()) | 65 if (!server_url.empty()) |
| 61 return server_url; | 66 return server_url; |
| 62 return kDefaultServerUrl; | 67 return kDefaultServerUrl; |
| 63 } | 68 } |
| 64 | 69 |
| 65 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { | 70 uint64_t LoadOrGenerateClientId(PrefService* pref_service) { |
| 66 uint64_t client_id = pref_service->GetInt64(prefs::kUkmClientId); | 71 uint64_t client_id = pref_service->GetInt64(prefs::kUkmClientId); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 145 |
| 141 void UkmService::Flush() { | 146 void UkmService::Flush() { |
| 142 if (initialize_complete_) | 147 if (initialize_complete_) |
| 143 BuildAndStoreLog(); | 148 BuildAndStoreLog(); |
| 144 persisted_logs_.SerializeLogs(); | 149 persisted_logs_.SerializeLogs(); |
| 145 } | 150 } |
| 146 | 151 |
| 147 void UkmService::Purge() { | 152 void UkmService::Purge() { |
| 148 DVLOG(1) << "UkmService::Purge"; | 153 DVLOG(1) << "UkmService::Purge"; |
| 149 persisted_logs_.Purge(); | 154 persisted_logs_.Purge(); |
| 150 // TODO(oystein): Delete any stored sources. | 155 sources_.clear(); |
| 151 } | 156 } |
| 152 | 157 |
| 153 // static | 158 // static |
| 154 void UkmService::RegisterPrefs(PrefRegistrySimple* registry) { | 159 void UkmService::RegisterPrefs(PrefRegistrySimple* registry) { |
| 155 registry->RegisterInt64Pref(prefs::kUkmClientId, 0); | 160 registry->RegisterInt64Pref(prefs::kUkmClientId, 0); |
| 156 registry->RegisterListPref(prefs::kUkmPersistedLogs); | 161 registry->RegisterListPref(prefs::kUkmPersistedLogs); |
| 157 } | 162 } |
| 158 | 163 |
| 159 void UkmService::StartInitTask() { | 164 void UkmService::StartInitTask() { |
| 160 DCHECK(thread_checker_.CalledOnValidThread()); | 165 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 180 } | 185 } |
| 181 StartScheduledUpload(); | 186 StartScheduledUpload(); |
| 182 } | 187 } |
| 183 | 188 |
| 184 void UkmService::BuildAndStoreLog() { | 189 void UkmService::BuildAndStoreLog() { |
| 185 DCHECK(thread_checker_.CalledOnValidThread()); | 190 DCHECK(thread_checker_.CalledOnValidThread()); |
| 186 DVLOG(1) << "UkmService::BuildAndStoreLog"; | 191 DVLOG(1) << "UkmService::BuildAndStoreLog"; |
| 187 Report report; | 192 Report report; |
| 188 report.set_client_id(client_id_); | 193 report.set_client_id(client_id_); |
| 189 // TODO(holte): Populate system_profile. | 194 // TODO(holte): Populate system_profile. |
| 190 // TODO(oystein): Populate sources. | 195 for (auto& source : sources_) { |
| 196 Source* proto_source = report.add_sources(); |
| 197 source->PopulateProto(proto_source); |
| 198 } |
| 199 sources_.clear(); |
| 200 |
| 191 std::string serialized_log; | 201 std::string serialized_log; |
| 192 report.SerializeToString(&serialized_log); | 202 report.SerializeToString(&serialized_log); |
| 193 persisted_logs_.StoreLog(serialized_log); | 203 persisted_logs_.StoreLog(serialized_log); |
| 194 } | 204 } |
| 195 | 205 |
| 196 void UkmService::StartScheduledUpload() { | 206 void UkmService::StartScheduledUpload() { |
| 197 DCHECK(thread_checker_.CalledOnValidThread()); | 207 DCHECK(thread_checker_.CalledOnValidThread()); |
| 198 DCHECK(!log_upload_in_progress_); | 208 DCHECK(!log_upload_in_progress_); |
| 199 if (!persisted_logs_.has_staged_log()) | 209 if (!persisted_logs_.has_staged_log()) |
| 200 persisted_logs_.StageLog(); | 210 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. | 247 // Store the updated list to disk now that the removed log is uploaded. |
| 238 persisted_logs_.SerializeLogs(); | 248 persisted_logs_.SerializeLogs(); |
| 239 } | 249 } |
| 240 | 250 |
| 241 // Error 400 indicates a problem with the log, not with the server, so | 251 // 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. | 252 // don't consider that a sign that the server is in trouble. |
| 243 bool server_is_healthy = upload_succeeded || response_code == 400; | 253 bool server_is_healthy = upload_succeeded || response_code == 400; |
| 244 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); | 254 scheduler_->UploadFinished(server_is_healthy, !persisted_logs_.empty()); |
| 245 } | 255 } |
| 246 | 256 |
| 257 void UkmService::RecordSource(std::unique_ptr<UkmSource> source) { |
| 258 if (sources_.size() >= kMaxSourcesLength) |
| 259 return; |
| 260 |
| 261 sources_.push_back(std::move(source)); |
| 262 } |
| 263 |
| 247 } // namespace ukm | 264 } // namespace ukm |
| OLD | NEW |