field_trials_provider.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include "components/metrics/field_trials_provider.h"
  5. #include <string>
  6. #include <vector>
  7. #include "base/strings/string_piece.h"
  8. #include "components/variations/active_field_trials.h"
  9. #include "components/variations/synthetic_trial_registry.h"
  10. #include "third_party/metrics_proto/system_profile.pb.h"
  11. namespace variations {
  12. namespace {
  13. void WriteFieldTrials(const std::vector<ActiveGroupId>& field_trial_ids,
  14. metrics::SystemProfileProto* system_profile) {
  15. for (const ActiveGroupId& id : field_trial_ids) {
  16. metrics::SystemProfileProto::FieldTrial* field_trial =
  17. system_profile->add_field_trial();
  18. field_trial->set_name_id(id.name);
  19. field_trial->set_group_id(id.group);
  20. }
  21. }
  22. } // namespace
  23. FieldTrialsProvider::FieldTrialsProvider(SyntheticTrialRegistry* registry,
  24. base::StringPiece suffix)
  25. : registry_(registry), suffix_(suffix) {}
  26. FieldTrialsProvider::~FieldTrialsProvider() = default;
  27. void FieldTrialsProvider::GetFieldTrialIds(
  28. std::vector<ActiveGroupId>* field_trial_ids) const {
  29. variations::GetFieldTrialActiveGroupIds(suffix_, field_trial_ids);
  30. }
  31. void FieldTrialsProvider::ProvideSystemProfileMetrics(
  32. metrics::SystemProfileProto* system_profile_proto) {
  33. // ProvideSystemProfileMetricsWithLogCreationTime() should be called instead.
  34. NOTREACHED();
  35. }
  36. void FieldTrialsProvider::ProvideSystemProfileMetricsWithLogCreationTime(
  37. base::TimeTicks log_creation_time,
  38. metrics::SystemProfileProto* system_profile_proto) {
  39. // TODO(crbug/1090497): Maybe call ProvideCurrentSessionData() instead from
  40. // places in which ProvideSystemProfileMetricsWithLogCreationTime() is called,
  41. // e.g. startup_data.cc and background_tracing_metrics_provider.cc.
  42. log_creation_time_ = log_creation_time;
  43. const std::string& version = variations::GetSeedVersion();
  44. if (!version.empty())
  45. system_profile_proto->set_variations_seed_version(version);
  46. // TODO(crbug/1090098): Determine whether this can be deleted.
  47. GetAndWriteFieldTrials(system_profile_proto);
  48. }
  49. void FieldTrialsProvider::ProvideCurrentSessionData(
  50. metrics::ChromeUserMetricsExtension* uma_proto) {
  51. // This function is called from both
  52. // ProvideSystemProfileMetricsWithLogCreationTime() and
  53. // ProvideCurrentSessionData() so that field trials activated in other metrics
  54. // providers are captured. We need both calls because in some scenarios in
  55. // which this class is used, only the former function gets called.
  56. DCHECK(!log_creation_time_.is_null());
  57. GetAndWriteFieldTrials(uma_proto->mutable_system_profile());
  58. }
  59. void FieldTrialsProvider::SetLogCreationTimeForTesting(base::TimeTicks time) {
  60. log_creation_time_ = time;
  61. }
  62. void FieldTrialsProvider::GetAndWriteFieldTrials(
  63. metrics::SystemProfileProto* system_profile_proto) const {
  64. system_profile_proto->clear_field_trial();
  65. std::vector<ActiveGroupId> field_trials;
  66. GetFieldTrialIds(&field_trials);
  67. WriteFieldTrials(field_trials, system_profile_proto);
  68. // May be null in tests.
  69. if (registry_) {
  70. std::vector<ActiveGroupId> synthetic_trials;
  71. registry_->GetSyntheticFieldTrialsOlderThan(log_creation_time_,
  72. &synthetic_trials, suffix_);
  73. WriteFieldTrials(synthetic_trials, system_profile_proto);
  74. }
  75. }
  76. } // namespace variations