Chromium Code Reviews| 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. | |
|
Alexei Svitkine (slow)
2017/03/14 15:40:56
Is it comma-separated or something? Mention.
Or a
rkaplow
2017/03/14 18:01:41
Done.
| |
| 74 std::string GetWhitelistEntries() { | |
| 75 return base::GetFieldTrialParamValueByFeature(kUkmFeature, | |
| 76 "WhitelistEntries"); | |
| 77 } | |
| 78 | |
| 79 // Returns true if we should use the whitelist of Entries to record. | |
| 80 bool UseWhitelistEntries() { | |
| 81 // If the Field Trial param is empty, we will default to not using the | |
| 82 // whitelist. | |
| 83 return GetWhitelistEntries() != std::string(); | |
|
Alexei Svitkine (slow)
2017/03/14 15:40:56
Nit: ! .empty()
rkaplow
2017/03/14 18:01:41
Acknowledged.
| |
| 84 } | |
| 85 | |
| 71 // Gets the maximum number of Sources we'll keep in memory before discarding any | 86 // Gets the maximum number of Sources we'll keep in memory before discarding any |
| 72 // new ones being added. | 87 // new ones being added. |
| 73 size_t GetMaxSources() { | 88 size_t GetMaxSources() { |
| 74 constexpr size_t kDefaultMaxSources = 500; | 89 constexpr size_t kDefaultMaxSources = 500; |
| 75 return static_cast<size_t>(base::GetFieldTrialParamByFeatureAsInt( | 90 return static_cast<size_t>(base::GetFieldTrialParamByFeatureAsInt( |
| 76 kUkmFeature, "MaxSources", kDefaultMaxSources)); | 91 kUkmFeature, "MaxSources", kDefaultMaxSources)); |
| 77 } | 92 } |
| 78 | 93 |
| 79 // Gets the maximum number of Entries we'll keep in memory before discarding any | 94 // Gets the maximum number of Entries we'll keep in memory before discarding any |
| 80 // new ones being added. | 95 // 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); | 134 int32_t session_id = pref_service->GetInteger(prefs::kUkmSessionId); |
| 120 ++session_id; // increment session id, once per session | 135 ++session_id; // increment session id, once per session |
| 121 pref_service->SetInteger(prefs::kUkmSessionId, session_id); | 136 pref_service->SetInteger(prefs::kUkmSessionId, session_id); |
| 122 return session_id; | 137 return session_id; |
| 123 } | 138 } |
| 124 | 139 |
| 125 enum class DroppedDataReason { | 140 enum class DroppedDataReason { |
| 126 NOT_DROPPED = 0, | 141 NOT_DROPPED = 0, |
| 127 RECORDING_DISABLED = 1, | 142 RECORDING_DISABLED = 1, |
| 128 MAX_HIT = 2, | 143 MAX_HIT = 2, |
| 144 NOT_WHITELISTED = 3, | |
| 129 NUM_DROPPED_DATA_REASONS | 145 NUM_DROPPED_DATA_REASONS |
| 130 }; | 146 }; |
| 131 | 147 |
| 132 void RecordDroppedSource(DroppedDataReason reason) { | 148 void RecordDroppedSource(DroppedDataReason reason) { |
| 133 UMA_HISTOGRAM_ENUMERATION( | 149 UMA_HISTOGRAM_ENUMERATION( |
| 134 "UKM.Sources.Dropped", static_cast<int>(reason), | 150 "UKM.Sources.Dropped", static_cast<int>(reason), |
| 135 static_cast<int>(DroppedDataReason::NUM_DROPPED_DATA_REASONS)); | 151 static_cast<int>(DroppedDataReason::NUM_DROPPED_DATA_REASONS)); |
| 136 } | 152 } |
| 137 | 153 |
| 138 void RecordDroppedEntry(DroppedDataReason reason) { | 154 void RecordDroppedEntry(DroppedDataReason reason) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 // MetricsServiceClient outlives UkmService, and | 190 // MetricsServiceClient outlives UkmService, and |
| 175 // MetricsReportingScheduler is tied to the lifetime of |this|. | 191 // MetricsReportingScheduler is tied to the lifetime of |this|. |
| 176 const base::Callback<base::TimeDelta(void)>& get_upload_interval_callback = | 192 const base::Callback<base::TimeDelta(void)>& get_upload_interval_callback = |
| 177 base::Bind(&metrics::MetricsServiceClient::GetStandardUploadInterval, | 193 base::Bind(&metrics::MetricsServiceClient::GetStandardUploadInterval, |
| 178 base::Unretained(client_)); | 194 base::Unretained(client_)); |
| 179 scheduler_.reset(new ukm::MetricsReportingScheduler( | 195 scheduler_.reset(new ukm::MetricsReportingScheduler( |
| 180 rotate_callback, get_upload_interval_callback)); | 196 rotate_callback, get_upload_interval_callback)); |
| 181 | 197 |
| 182 for (auto& provider : metrics_providers_) | 198 for (auto& provider : metrics_providers_) |
| 183 provider->Init(); | 199 provider->Init(); |
| 200 | |
| 201 StoreWhitelistedEntries(); | |
| 184 } | 202 } |
| 185 | 203 |
| 186 UkmService::~UkmService() { | 204 UkmService::~UkmService() { |
| 187 DisableReporting(); | 205 DisableReporting(); |
| 188 } | 206 } |
| 189 | 207 |
| 190 void UkmService::Initialize() { | 208 void UkmService::Initialize() { |
| 191 DCHECK(thread_checker_.CalledOnValidThread()); | 209 DCHECK(thread_checker_.CalledOnValidThread()); |
| 192 DCHECK(!initialize_started_); | 210 DCHECK(!initialize_started_); |
| 193 DVLOG(1) << "UkmService::Initialize"; | 211 DVLOG(1) << "UkmService::Initialize"; |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 | 504 |
| 487 if (!recording_enabled_) { | 505 if (!recording_enabled_) { |
| 488 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED); | 506 RecordDroppedEntry(DroppedDataReason::RECORDING_DISABLED); |
| 489 return; | 507 return; |
| 490 } | 508 } |
| 491 if (entries_.size() >= GetMaxEntries()) { | 509 if (entries_.size() >= GetMaxEntries()) { |
| 492 RecordDroppedEntry(DroppedDataReason::MAX_HIT); | 510 RecordDroppedEntry(DroppedDataReason::MAX_HIT); |
| 493 return; | 511 return; |
| 494 } | 512 } |
| 495 | 513 |
| 514 if (UseWhitelistEntries() && | |
|
Alexei Svitkine (slow)
2017/03/14 15:40:56
Nit: I think it would be cleaner to just check !wh
rkaplow
2017/03/14 18:01:41
agreed
| |
| 515 !base::ContainsKey(whitelisted_entry_hashes_, entry->event_hash())) { | |
| 516 RecordDroppedEntry(DroppedDataReason::NOT_WHITELISTED); | |
| 517 return; | |
| 518 } | |
| 519 | |
| 496 entries_.push_back(std::move(entry)); | 520 entries_.push_back(std::move(entry)); |
| 497 } | 521 } |
| 498 | 522 |
| 523 void UkmService::StoreWhitelistedEntries() { | |
| 524 const auto entries = | |
| 525 base::SplitString(GetWhitelistEntries(), ",", base::TRIM_WHITESPACE, | |
| 526 base::SPLIT_WANT_NONEMPTY); | |
| 527 for (const auto& entry_string : entries) { | |
| 528 whitelisted_entry_hashes_.insert(base::HashMetricName(entry_string)); | |
| 529 } | |
| 530 } | |
| 531 | |
| 499 } // namespace ukm | 532 } // namespace ukm |
| OLD | NEW |