demographic_metrics_provider.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2019 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. #ifndef COMPONENTS_METRICS_DEMOGRAPHICS_DEMOGRAPHIC_METRICS_PROVIDER_H_
  5. #define COMPONENTS_METRICS_DEMOGRAPHICS_DEMOGRAPHIC_METRICS_PROVIDER_H_
  6. #include <memory>
  7. #include "base/time/time.h"
  8. #include "components/metrics/demographics/user_demographics.h"
  9. #include "components/metrics/metrics_log_uploader.h"
  10. #include "components/metrics/metrics_provider.h"
  11. #include "components/metrics/ukm_demographic_metrics_provider.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. #include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
  14. #include "third_party/metrics_proto/user_demographics.pb.h"
  15. class PrefService;
  16. namespace base {
  17. struct Feature;
  18. }
  19. namespace syncer {
  20. class SyncService;
  21. }
  22. namespace metrics {
  23. // Provider of the synced user’s noised birth year and gender to the UMA metrics
  24. // server. The synced user's birth year and gender were provided to Google when
  25. // the user created their Google account, to use in accordance with Google's
  26. // privacy policy. The provided birth year and gender are used in aggregate and
  27. // anonymized form to measure usage of Chrome features by age groups and gender
  28. // - helping Chrome ensure features are useful to a wide range of users. Users
  29. // can avoid aggregation of usage data by birth year and gender by either a)
  30. // turning off sending usage statistics to Google or b) turning off sync.
  31. class DemographicMetricsProvider : public MetricsProvider,
  32. public UkmDemographicMetricsProvider {
  33. public:
  34. // Interface that represents the client that retrieves Profile information.
  35. class ProfileClient {
  36. public:
  37. virtual ~ProfileClient() = default;
  38. // Gets the total number of profiles that are on disk (loaded + not loaded)
  39. // for the browser.
  40. virtual int GetNumberOfProfilesOnDisk() = 0;
  41. // Gets a pointer to the SyncService of the profile, which is required to
  42. // determine whether priority preferences carrying demographics information
  43. // is being synced.
  44. virtual syncer::SyncService* GetSyncService() = 0;
  45. // Gets a pointer to the PrefService of the profile.
  46. virtual PrefService* GetPrefService() = 0;
  47. // Gets the network time that represents now.
  48. // TODO(crbug/1145655): Remove this function and replace with
  49. // base::Time::Now().
  50. virtual base::Time GetNetworkTime() const = 0;
  51. };
  52. DemographicMetricsProvider(
  53. std::unique_ptr<ProfileClient> profile_client,
  54. MetricsLogUploader::MetricServiceType metrics_service_type);
  55. DemographicMetricsProvider(const DemographicMetricsProvider&) = delete;
  56. DemographicMetricsProvider& operator=(const DemographicMetricsProvider&) =
  57. delete;
  58. ~DemographicMetricsProvider() override;
  59. // Provides the synced user's noised birth year and gender to a metrics report
  60. // of type ReportType. This function is templated to support any type of proto
  61. // metrics report, e.g., ukm::Report and metrics::ChromeUserMetricsExtension.
  62. // The ReportType should be a proto message class that has a
  63. // metrics::UserDemographicsProto message field.
  64. template <class ReportType>
  65. void ProvideSyncedUserNoisedBirthYearAndGender(ReportType* report) {
  66. DCHECK(report);
  67. absl::optional<UserDemographics> user_demographics =
  68. ProvideSyncedUserNoisedBirthYearAndGender();
  69. if (user_demographics.has_value()) {
  70. report->mutable_user_demographics()->set_birth_year(
  71. user_demographics.value().birth_year);
  72. report->mutable_user_demographics()->set_gender(
  73. user_demographics.value().gender);
  74. }
  75. }
  76. // MetricsProvider:
  77. void ProvideCurrentSessionData(
  78. ChromeUserMetricsExtension* uma_proto) override;
  79. // UkmDemographicMetricsProvider:
  80. void ProvideSyncedUserNoisedBirthYearAndGenderToReport(
  81. ukm::Report* report) override;
  82. // Feature switch to report user's noised birth year and gender.
  83. static const base::Feature kDemographicMetricsReporting;
  84. private:
  85. // Provides the synced user's noised birth year and gender.
  86. absl::optional<UserDemographics> ProvideSyncedUserNoisedBirthYearAndGender();
  87. void LogUserDemographicsStatusInHistogram(UserDemographicsStatus status);
  88. std::unique_ptr<ProfileClient> profile_client_;
  89. // The type of the metrics service for which to emit the user demographics
  90. // status histogram (e.g., UMA).
  91. const MetricsLogUploader::MetricServiceType metrics_service_type_;
  92. };
  93. } // namespace metrics
  94. #endif // COMPONENTS_METRICS_DEMOGRAPHICS_DEMOGRAPHIC_METRICS_PROVIDER_H_