metrics_service_accessor.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "components/metrics/metrics_service_accessor.h"
  5. #include "base/base_switches.h"
  6. #include "build/branding_buildflags.h"
  7. #include "components/metrics/metrics_pref_names.h"
  8. #include "components/metrics/metrics_service.h"
  9. #include "components/metrics/metrics_switches.h"
  10. #include "components/prefs/pref_service.h"
  11. #include "components/variations/hashing.h"
  12. #include "components/variations/synthetic_trial_registry.h"
  13. namespace metrics {
  14. namespace {
  15. bool g_force_official_enabled_test = false;
  16. bool IsMetricsReportingEnabledForOfficialBuild(PrefService* pref_service) {
  17. return pref_service->GetBoolean(prefs::kMetricsReportingEnabled);
  18. }
  19. } // namespace
  20. // static
  21. bool MetricsServiceAccessor::IsMetricsReportingEnabled(
  22. PrefService* pref_service) {
  23. if (IsMetricsReportingForceEnabled()) {
  24. LOG(WARNING) << "Metrics Reporting is force enabled, data will be sent to "
  25. "servers. Should not be used for tests.";
  26. return true;
  27. }
  28. #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
  29. return IsMetricsReportingEnabledForOfficialBuild(pref_service);
  30. #else
  31. // In non-official builds, disable metrics reporting completely.
  32. return g_force_official_enabled_test
  33. ? IsMetricsReportingEnabledForOfficialBuild(pref_service)
  34. : false;
  35. #endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
  36. }
  37. // static
  38. bool MetricsServiceAccessor::RegisterSyntheticFieldTrial(
  39. MetricsService* metrics_service,
  40. base::StringPiece trial_name,
  41. base::StringPiece group_name,
  42. variations::SyntheticTrialAnnotationMode annotation_mode) {
  43. if (!metrics_service)
  44. return false;
  45. variations::SyntheticTrialGroup trial_group(trial_name, group_name,
  46. annotation_mode);
  47. metrics_service->GetSyntheticTrialRegistry()->RegisterSyntheticFieldTrial(
  48. trial_group);
  49. return true;
  50. }
  51. // static
  52. void MetricsServiceAccessor::SetForceIsMetricsReportingEnabledPrefLookup(
  53. bool value) {
  54. g_force_official_enabled_test = value;
  55. }
  56. // static
  57. bool MetricsServiceAccessor::IsForceMetricsReportingEnabledPrefLookup() {
  58. return g_force_official_enabled_test;
  59. }
  60. } // namespace metrics