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