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