xref: /aosp_15_r20/external/cronet/components/metrics/metrics_service_accessor.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_METRICS_METRICS_SERVICE_ACCESSOR_H_
6 #define COMPONENTS_METRICS_METRICS_SERVICE_ACCESSOR_H_
7 
8 #include <stdint.h>
9 
10 #include "base/strings/string_piece.h"
11 #include "components/variations/synthetic_trials.h"
12 
13 class PrefService;
14 
15 namespace metrics {
16 
17 class MetricsService;
18 
19 // This class limits and documents access to metrics service helper methods.
20 // These methods are protected so each user has to inherit own program-specific
21 // specialization and enable access there by declaring friends.
22 class MetricsServiceAccessor {
23  public:
24   MetricsServiceAccessor(const MetricsServiceAccessor&) = delete;
25   MetricsServiceAccessor& operator=(const MetricsServiceAccessor&) = delete;
26 
27   // Returns the value assigned by
28   // SetForceIsMetricsReportingEnabledPrefLookup(). Default value is false.
29   static bool IsForceMetricsReportingEnabledPrefLookup();
30 
31  protected:
32   // Constructor declared as protected to enable inheritance. Descendants should
33   // disallow instantiation.
MetricsServiceAccessor()34   MetricsServiceAccessor() {}
35 
36   // Returns whether metrics reporting is enabled, using the value of the
37   // kMetricsReportingEnabled pref in |pref_service| to determine whether user
38   // has enabled reporting.
39   static bool IsMetricsReportingEnabled(PrefService* pref_service);
40 
41   // Registers a field trial name and group with |metrics_service| (if not
42   // null), to be used to annotate a UMA report with a particular configuration
43   // state. The |annotation_mode| parameter determines when UMA reports should
44   // start being annotated with this trial and group. Returns true on success.
45   // See the comment on SyntheticTrialRegistry::RegisterSyntheticFieldTrial()
46   // and ChromeMetricsServiceAccessor::RegisterSyntheticFieldTrial() for more
47   // details.
48   static bool RegisterSyntheticFieldTrial(
49       MetricsService* metrics_service,
50       base::StringPiece trial_name,
51       base::StringPiece group_name,
52       variations::SyntheticTrialAnnotationMode annotation_mode);
53 
54   // IsMetricsReportingEnabled() in non-official builds unconditionally returns
55   // false. This results in different behavior for tests running in official vs
56   // non-official builds. To get consistent behavior call this with true, which
57   // forces non-official builds to look at the prefs value official builds look
58   // at.
59   static void SetForceIsMetricsReportingEnabledPrefLookup(bool value);
60 };
61 
62 }  // namespace metrics
63 
64 #endif  // COMPONENTS_METRICS_METRICS_SERVICE_ACCESSOR_H_
65