File: metrics_services_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (196 lines) | stat: -rw-r--r-- 6,807 bytes parent folder | download | duplicates (5)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_METRICS_SERVICES_MANAGER_METRICS_SERVICES_MANAGER_H_
#define COMPONENTS_METRICS_SERVICES_MANAGER_METRICS_SERVICES_MANAGER_H_

#include <memory>

#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/threading/thread_checker.h"
#include "components/variations/synthetic_trial_registry.h"

namespace metrics {
class MetricsService;
class MetricsServiceClient;
class ClonedInstallDetector;
}  // namespace metrics

namespace metrics::structured {
class StructuredMetricsService;
}

namespace search_engines {
class SearchEngineChoiceServiceClient;
}

namespace ukm {
class UkmService;
}

namespace metrics::dwa {
class DwaService;
}

namespace variations {
class EntropyProviders;
class SyntheticTrialRegistry;
class VariationsService;
}  // namespace variations

class IdentifiabilityStudyState;

namespace metrics_services_manager {

class MetricsServicesManagerClient;

// MetricsServicesManager is a helper class for embedders that use the various
// metrics-related services in a Chrome-like fashion: MetricsService (via its
// client) and VariationsService.
class MetricsServicesManager {
 public:
  using OnDidStartLoadingCb = base::RepeatingClosure;
  using OnDidStopLoadingCb = base::RepeatingClosure;
  using OnRendererUnresponsiveCb = base::RepeatingClosure;

  // Creates the MetricsServicesManager with the given client.
  explicit MetricsServicesManager(
      std::unique_ptr<MetricsServicesManagerClient> client);

  MetricsServicesManager(const MetricsServicesManager&) = delete;
  MetricsServicesManager& operator=(const MetricsServicesManager&) = delete;

  virtual ~MetricsServicesManager();

  // Instantiates the FieldTrialList using Chrome's default entropy provider.
  //
  // Side effect: Initializes the CleanExitBeacon.
  void InstantiateFieldTrialList() const;

  // Returns the SyntheticTrialRegistry, creating it if it hasn't been created
  // yet.
  variations::SyntheticTrialRegistry* GetSyntheticTrialRegistry();

  // Returns the MetricsService, creating it if it hasn't been created yet (and
  // additionally creating the MetricsServiceClient in that case).
  metrics::MetricsService* GetMetricsService();

  // Returns the UkmService, creating it if it hasn't been created yet.
  ukm::UkmService* GetUkmService();

  // Returns the DwaService, creating it if it hasn't been created yet.
  metrics::dwa::DwaService* GetDwaService();

  // Returns the IdentifiabilityStudyState, if it has been created, and nullptr
  // otherwise.
  IdentifiabilityStudyState* GetIdentifiabilityStudyState();

  // Returns the StructuredMetricsService associated with the
  // |metrics_service_client_|.
  metrics::structured::StructuredMetricsService* GetStructuredMetricsService();

  // Returns the VariationsService, creating it if it hasn't been created yet.
  variations::VariationsService* GetVariationsService();

  // Returns an |OnDidStartLoadingCb| callback.
  OnDidStartLoadingCb GetOnDidStartLoadingCb();

  // Returns an |OnDidStopLoadingCb| callback.
  OnDidStopLoadingCb GetOnDidStopLoadingCb();

  // Returns an |OnRendererUnresponsiveCb| callback.
  OnRendererUnresponsiveCb GetOnRendererUnresponsiveCb();

  // Updates the managed services when permissions for uploading metrics change.
  // Note: Normally, uploads will happen when collection is enabled, but the
  // `may_upload` params allows disabling uploads separately from collection
  // (e.g. if network is unavailable).
  void UpdateUploadPermissions(bool may_upload = true);

  // Gets the current state of metric reporting.
  bool IsMetricsReportingEnabled() const;

  // Gets the current state of metrics consent.
  bool IsMetricsConsentGiven() const;

  // Returns true iff UKM is allowed for all profiles.
  bool IsUkmAllowedForAllProfiles();

  // Returns true iff DWA is allowed for all profiles.
  bool IsDwaAllowedForAllProfiles();

  // Returns a low entropy provider.
  std::unique_ptr<const variations::EntropyProviders>
  CreateEntropyProvidersForTesting();

  // Returns the ClonedInstallDetector associated with the `client_`.
  metrics::ClonedInstallDetector* GetClonedInstallDetectorForTesting();

 private:
  friend class search_engines::SearchEngineChoiceServiceClient;

  // Returns the ClonedInstallDetector associated with the `client_`.
  // Marked as private (exposed selectively via friend classes) for the metrics
  // team to be able to control and monitor if/how this function gets called.
  const metrics::ClonedInstallDetector& GetClonedInstallDetector() const;

  // Returns the MetricsServiceClient, creating it if it hasn't been
  // created yet (and additionally creating the MetricsService in that case).
  metrics::MetricsServiceClient* GetMetricsServiceClient();

  // Updates which services are running to match current permissions.
  void UpdateRunningServices();

  // Updates the state of UkmService to match current permissions.
  void UpdateUkmService();

  // Updates the state of StructuredMetricsService to match current permissions.
  void UpdateStructuredMetricsService();

  // Updates the state of DwaService to match current permissions.
  void UpdateDwaService();

  // Updates the managed services when permissions for recording/uploading
  // metrics change.
  void UpdatePermissions(bool current_may_record,
                         bool current_consent_given,
                         bool current_may_upload);

  // Called when loading state changed.
  void LoadingStateChanged(bool is_loading);

  // Used by |GetOnRendererUnresponsiveCb| to construct the callback that will
  // be run by |MetricsServicesWebContentsObserver|.
  void OnRendererUnresponsive();

  // The client passed in from the embedder.
  const std::unique_ptr<MetricsServicesManagerClient> client_;

  // Ensures that all functions are called from the same thread.
  base::ThreadChecker thread_checker_;

  // The current metrics reporting setting.
  bool may_upload_ = false;

  // The current metrics recording setting.
  bool may_record_ = false;

  // The current metrics setting for reporting metrics.
  bool consent_given_ = false;

  std::unique_ptr<variations::SyntheticTrialRegistry> synthetic_trial_registry_;

  // The MetricsServiceClient. Owns the MetricsService.
  std::unique_ptr<metrics::MetricsServiceClient> metrics_service_client_;

  // The VariationsService, for server-side experiments infrastructure.
  std::unique_ptr<variations::VariationsService> variations_service_;

  base::WeakPtrFactory<MetricsServicesManager> weak_ptr_factory_{this};
};

}  // namespace metrics_services_manager

#endif  // COMPONENTS_METRICS_SERVICES_MANAGER_METRICS_SERVICES_MANAGER_H_