| 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/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/feature_list.h" | 12 #include "base/feature_list.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/metrics/field_trial.h" | 14 #include "base/metrics/field_trial.h" |
| 15 #include "base/metrics/field_trial_params.h" | 15 #include "base/metrics/field_trial_params.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/metrics/metrics_hashes.h" |
| 17 #include "base/rand_util.h" | 18 #include "base/rand_util.h" |
| 18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_split.h" |
| 19 #include "base/time/time.h" | 21 #include "base/time/time.h" |
| 20 #include "components/metrics/metrics_log.h" | 22 #include "components/metrics/metrics_log.h" |
| 21 #include "components/metrics/metrics_log_uploader.h" | 23 #include "components/metrics/metrics_log_uploader.h" |
| 22 #include "components/metrics/metrics_service_client.h" | 24 #include "components/metrics/metrics_service_client.h" |
| 23 #include "components/metrics/proto/ukm/entry.pb.h" | 25 #include "components/metrics/proto/ukm/entry.pb.h" |
| 24 #include "components/metrics/proto/ukm/report.pb.h" | 26 #include "components/metrics/proto/ukm/report.pb.h" |
| 25 #include "components/metrics/proto/ukm/source.pb.h" | 27 #include "components/metrics/proto/ukm/source.pb.h" |
| 26 #include "components/prefs/pref_registry_simple.h" | 28 #include "components/prefs/pref_registry_simple.h" |
| 27 #include "components/prefs/pref_service.h" | 29 #include "components/prefs/pref_service.h" |
| 28 #include "components/ukm/metrics_reporting_scheduler.h" | 30 #include "components/ukm/metrics_reporting_scheduler.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 // Gets the UKM Server URL. | 63 // Gets the UKM Server URL. |
| 62 std::string GetServerUrl() { | 64 std::string GetServerUrl() { |
| 63 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm"; | 65 constexpr char kDefaultServerUrl[] = "https://clients4.google.com/ukm"; |
| 64 std::string server_url = | 66 std::string server_url = |
| 65 base::GetFieldTrialParamValueByFeature(kUkmFeature, "ServerUrl"); | 67 base::GetFieldTrialParamValueByFeature(kUkmFeature, "ServerUrl"); |
| 66 if (!server_url.empty()) | 68 if (!server_url.empty()) |
| 67 return server_url; | 69 return server_url; |
| 68 return kDefaultServerUrl; | 70 return kDefaultServerUrl; |
| 69 } | 71 } |
| 70 | 72 |
| 73 // Gets the list of whitelisted Entries as string. Format is a comma seperated |
| 74 // list of Entry names (as strings). |
| 75 std::string GetWhitelistEntries() { |
| 76 return base::GetFieldTrialParamValueByFeature(kUkmFeature, |
| 77 "WhitelistEntries"); |
| 78 } |
| 79 |
| 71 // Gets the maximum number of Sources we'll keep in memory before discarding any | 80 // Gets the maximum number of Sources we'll keep in memory before discarding any |
| 72 // new ones being added. | 81 // new ones being added. |
| 73 size_t GetMaxSources() { | 82 size_t GetMaxSources() { |
| 74 constexpr size_t kDefaultMaxSources = 500; | 83 constexpr size_t kDefaultMaxSources = 500; |
| 75 return static_cast<size_t>(base::GetFieldTrialParamByFeatureAsInt( | 84 return static_cast<size_t>(base::GetFieldTrialParamByFeatureAsInt( |
| 76 kUkmFeature, "MaxSources", kDefaultMaxSources)); | 85 kUkmFeature, "MaxSources", kDefaultMaxSources)); |
| 77 } | 86 } |
| 78 | 87 |
| 79 // Gets the maximum number of Entries we'll keep in memory before discarding any | 88 // Gets the maximum number of Entries we'll keep in memory before discarding any |
| 80 // new ones being added. | 89 // new ones being added. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 int32_t session_id = pref_service->GetInteger(prefs::kUkmSessionId); | 128 int32_t session_id = pref_service->GetInteger(prefs::kUkmSessionId); |
| 120 ++session_id; // increment session id, once per session | 129 ++session_id; // increment session id, once per session |
| 121 pref_service->SetInteger(prefs::kUkmSessionId, session_id); | 130 pref_service->SetInteger(prefs::kUkmSessionId, session_id); |
| 122 return session_id; | 131 return session_id; |
| 123 } | 132 } |
| 124 | 133 |
| 125 enum class DroppedDataReason { | 134 enum class DroppedDataReason { |
| 126 NOT_DROPPED = 0, | 135 NOT_DROPPED = 0, |
| 127 RECORDING_DISABLED = 1, | 136 RECORDING_DISABLED = 1, |
| 128 MAX_HIT = 2, | 137 MAX_HIT = 2, |
| 138 NOT_WHITELISTED = 3, |
| 129 NUM_DROPPED_DATA_REASONS | 139 NUM_DROPPED_DATA_REASONS |
| 130 }; | 140 }; |
| 131 | 141 |
| 132 void RecordDroppedSource(DroppedDataReason reason) { | 142 void RecordDroppedSource(DroppedDataReason reason) { |
| 133 UMA_HISTOGRAM_ENUMERATION( | 143 UMA_HISTOGRAM_ENUMERATION( |
| 134 "UKM.Sources.Dropped", static_cast<int>(reason), | 144 "UKM.Sources.Dropped", static_cast<int>(reason), |
| 135 static_cast<int>(DroppedDataReason::NUM_DROPPED_DATA_REASONS)); | 145 static_cast<int>(DroppedDataReason::NUM_DROPPED_DATA_REASONS)); |
| 136 } | 146 } |
| 137 | 147 |
| 138 void RecordDroppedEntry(DroppedDataReason reason) { | 148 void RecordDroppedEntry(DroppedDataReason reason) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 // MetricsServiceClient outlives UkmService, and | 184 // MetricsServiceClient outlives UkmService, and |
| 175 // MetricsReportingScheduler is tied to the lifetime of |this|. | 185 // MetricsReportingScheduler is tied to the lifetime of |this|. |
| 176 const base::Callback<base::TimeDelta(void)>& get_upload_interval_callback = | 186 const base::Callback<base::TimeDelta(void)>& get_upload_interval_callback = |
| 177 base::Bind(&metrics::MetricsServiceClient::GetStandardUploadInterval, | 187 base::Bind(&metrics::MetricsServiceClient::GetStandardUploadInterval, |
| 178 base::Unretained(client_)); | 188 base::Unretained(client_)); |
| 179 scheduler_.reset(new ukm::MetricsReportingScheduler( | 189 scheduler_.reset(new ukm::MetricsReportingScheduler( |
| 180 rotate_callback, get_upload_interval_callback)); | 190 rotate_callback, get_upload_interval_callback)); |
| 181 | 191 |
| 182 for (auto& provider : metrics_providers_) | 192 for (auto& provider : metrics_providers_) |
| 183 provider->Init(); | 193 provider->Init(); |
| 194 |
| 195 StoreWhitelistedEntries(); |
| 184 } | 196 } |
| 185 | 197 |
| 186 UkmService::~UkmService() { | 198 UkmService::~UkmService() { |
| 187 DisableReporting(); | 199 DisableReporting(); |
| 188 } | 200 } |
| 189 | 201 |
| 190 void UkmService::Initialize() { | 202 void UkmService::Initialize() { |
| 191 DCHECK(thread_checker_.CalledOnValidThread()); | 203 DCHECK(thread_checker_.CalledOnValidThread()); |
| 192 DCHECK(!initialize_started_); | 204 DCHECK(!initialize_started_); |
| 193 DVLOG(1) << "UkmService::Initialize"; | 205 DVLOG(1) << "UkmService::Initialize"; |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 | 482 |
| 471 if (!recording_enabled_) { | 483 if (!recording_enabled_) { |
| 472 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED); | 484 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED); |
| 473 return; | 485 return; |
| 474 } | 486 } |
| 475 if (entries_.size() >= GetMaxEntries()) { | 487 if (entries_.size() >= GetMaxEntries()) { |
| 476 RecordDroppedEntry(DroppedDataReason::MAX_HIT); | 488 RecordDroppedEntry(DroppedDataReason::MAX_HIT); |
| 477 return; | 489 return; |
| 478 } | 490 } |
| 479 | 491 |
| 492 if (!whitelisted_entry_hashes_.empty() && |
| 493 !base::ContainsKey(whitelisted_entry_hashes_, entry->event_hash())) { |
| 494 RecordDroppedEntry(DroppedDataReason::NOT_WHITELISTED); |
| 495 return; |
| 496 } |
| 497 |
| 480 entries_.push_back(std::move(entry)); | 498 entries_.push_back(std::move(entry)); |
| 481 } | 499 } |
| 482 | 500 |
| 501 void UkmService::StoreWhitelistedEntries() { |
| 502 const auto entries = |
| 503 base::SplitString(GetWhitelistEntries(), ",", base::TRIM_WHITESPACE, |
| 504 base::SPLIT_WANT_NONEMPTY); |
| 505 for (const auto& entry_string : entries) { |
| 506 whitelisted_entry_hashes_.insert(base::HashMetricName(entry_string)); |
| 507 } |
| 508 } |
| 509 |
| 483 } // namespace ukm | 510 } // namespace ukm |
| OLD | NEW |