summaryrefslogtreecommitdiff
path: root/chromium/components/metrics/demographic_metrics_provider.h
blob: 0091122f69242ed8627e1a6a54a9eb9bbb83be2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_METRICS_DEMOGRAPHIC_METRICS_PROVIDER_H_
#define COMPONENTS_METRICS_DEMOGRAPHIC_METRICS_PROVIDER_H_

#include <memory>

#include "base/time/time.h"
#include "components/metrics/metrics_log_uploader.h"
#include "components/metrics/metrics_provider.h"
#include "components/metrics/ukm_demographic_metrics_provider.h"
#include "components/sync/base/user_demographics.h"
#include "components/sync/driver/sync_service.h"
#include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
#include "third_party/metrics_proto/user_demographics.pb.h"

namespace base {
struct Feature;
}

namespace metrics {

// Provider of the synced user’s noised birth year and gender to the UMA metrics
// server. The synced user's birth year and gender were provided to Google when
// the user created their Google account, to use in accordance with Google's
// privacy policy. The provided birth year and gender are used in aggregate and
// anonymized form to measure usage of Chrome features by age groups and gender
// - helping Chrome ensure features are useful to a wide range of users. Users
// can avoid aggregation of usage data by birth year and gender by either a)
// turning off sending usage statistics to Google or b) turning off sync.
class DemographicMetricsProvider : public MetricsProvider,
                                   public UkmDemographicMetricsProvider {
 public:
  // Interface that represents the client that retrieves Profile information.
  class ProfileClient {
   public:
    virtual ~ProfileClient() = default;

    // Gets the total number of profiles that are on disk (loaded + not loaded)
    // for the browser.
    virtual int GetNumberOfProfilesOnDisk() = 0;

    // Gets a weak pointer to the ProfileSyncService of the Profile.
    virtual syncer::SyncService* GetSyncService() = 0;

    // Gets the network time that represents now.
    virtual base::Time GetNetworkTime() const = 0;
  };

  DemographicMetricsProvider(
      std::unique_ptr<ProfileClient> profile_client,
      MetricsLogUploader::MetricServiceType metrics_service_type);
  ~DemographicMetricsProvider() override;

  // Provides the synced user's noised birth year and gender to a metrics report
  // of type ReportType. This function is templated to support any type of proto
  // metrics report, e.g., ukm::Report and metrics::ChromeUserMetricsExtension.
  // The ReportType should be a proto message class that has a
  // metrics::UserDemographicsProto message field.
  template <class ReportType>
  void ProvideSyncedUserNoisedBirthYearAndGender(ReportType* report) {
    DCHECK(report);

    base::Optional<syncer::UserDemographics> user_demographics =
        ProvideSyncedUserNoisedBirthYearAndGender();
    if (user_demographics.has_value()) {
      report->mutable_user_demographics()->set_birth_year(
          user_demographics.value().birth_year);
      report->mutable_user_demographics()->set_gender(
          user_demographics.value().gender);
    }
  }

  // MetricsProvider:
  void ProvideCurrentSessionData(
      ChromeUserMetricsExtension* uma_proto) override;

  // UkmDemographicMetricsProvider:
  void ProvideSyncedUserNoisedBirthYearAndGenderToReport(
      ukm::Report* report) override;

  // Feature switch to report user's noised birth year and gender.
  static const base::Feature kDemographicMetricsReporting;

 protected:
  // Provides the synced user's noised birth year and gender. Virtual for
  // testing.
  virtual base::Optional<syncer::UserDemographics>
  ProvideSyncedUserNoisedBirthYearAndGender();

 private:
  void LogUserDemographicsStatusInHistogram(
      syncer::UserDemographicsStatus status);

  std::unique_ptr<ProfileClient> profile_client_;

  // The type of the metrics service for which to emit the user demographics
  // status histogram (e.g., UMA).
  const MetricsLogUploader::MetricServiceType metrics_service_type_;

  DISALLOW_COPY_AND_ASSIGN(DemographicMetricsProvider);
};

}  // namespace metrics

#endif  // COMPONENTS_METRICS_DEMOGRAPHIC_METRICS_PROVIDER_H_