metrics_service_accessor.h 2.6 KB

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