xref: /aosp_15_r20/external/cronet/components/miracle_parameter/common/public/miracle_parameter.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 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_MIRACLE_PARAMETER_COMMON_PUBLIC_MIRACLE_PARAMETER_H_
6 #define COMPONENTS_MIRACLE_PARAMETER_COMMON_PUBLIC_MIRACLE_PARAMETER_H_
7 
8 #include "base/component_export.h"
9 #include "base/containers/span.h"
10 #include "base/feature_list.h"
11 #include "base/metrics/field_trial_params.h"
12 
13 namespace miracle_parameter {
14 
15 namespace {
16 
17 template <typename Enum>
GetFieldTrialParamByFeatureAsEnum(const base::Feature & feature,const std::string & param_name,const Enum default_value,const base::span<const typename base::FeatureParam<Enum>::Option> & options)18 Enum GetFieldTrialParamByFeatureAsEnum(
19     const base::Feature& feature,
20     const std::string& param_name,
21     const Enum default_value,
22     const base::span<const typename base::FeatureParam<Enum>::Option>&
23         options) {
24   std::string string_value =
25       base::GetFieldTrialParamValueByFeature(feature, param_name);
26   if (string_value.empty()) {
27     return default_value;
28   }
29 
30   for (const auto& option : options) {
31     if (string_value == option.name) {
32       return option.value;
33     }
34   }
35 
36   base::LogInvalidEnumValue(feature, param_name, string_value,
37                             static_cast<int>(default_value));
38   return default_value;
39 }
40 
41 }  // namespace
42 
43 constexpr int kMiracleParameterMemory512MB = 512;
44 constexpr int kMiracleParameterMemory1GB = 1024;
45 constexpr int kMiracleParameterMemory2GB = 2 * 1024;
46 constexpr int kMiracleParameterMemory4GB = 4 * 1024;
47 constexpr int kMiracleParameterMemory8GB = 8 * 1024;
48 constexpr int kMiracleParameterMemory16GB = 16 * 1024;
49 
50 // GetParamNameWithSuffix put a parameter name suffix based on
51 // the amount of physical memory.
52 //
53 // - "ForLessThan512MB" for less than 512MB memory devices.
54 // - "For512MBTo1GB" for 512MB to 1GB memory devices.
55 // - "For1GBTo2GB" for 1GB to 2GB memory devices.
56 // - "For2GBTo4GB" for 2GB to 4GB memory devices.
57 // - "For4GBTo8GB" for 4GB to 8GB memory devices.
58 // - "For8GBTo16GB" for 8GB to 16GB memory devices.
59 // - "For16GBAndAbove" for 16GB memory and above devices.
60 COMPONENT_EXPORT(MIRACLE_PARAMETER)
61 std::string GetParamNameWithSuffix(const std::string& param_name);
62 
63 // Provides a similar behavior with FeatureParam<std::string> except the return
64 // value is determined by the amount of physical memory.
65 COMPONENT_EXPORT(MIRACLE_PARAMETER)
66 std::string GetMiracleParameterAsString(const base::Feature& feature,
67                                         const std::string& param_name,
68                                         const std::string& default_value);
69 
70 // Provides a similar behavior with FeatureParam<double> except the return value
71 // is determined by the amount of physical memory.
72 COMPONENT_EXPORT(MIRACLE_PARAMETER)
73 double GetMiracleParameterAsDouble(const base::Feature& feature,
74                                    const std::string& param_name,
75                                    double default_value);
76 
77 // Provides a similar behavior with FeatureParam<int> except the return value is
78 // determined by the amount of physical memory.
79 COMPONENT_EXPORT(MIRACLE_PARAMETER)
80 int GetMiracleParameterAsInt(const base::Feature& feature,
81                              const std::string& param_name,
82                              int default_value);
83 
84 // Provides a similar behavior with FeatureParam<bool> except the return value
85 // is determined by the amount of physical memory.
86 COMPONENT_EXPORT(MIRACLE_PARAMETER)
87 bool GetMiracleParameterAsBool(const base::Feature& feature,
88                                const std::string& param_name,
89                                bool default_value);
90 
91 // Provides a similar behavior with FeatureParam<base::TimeDelta> except the
92 // return value is determined by the amount of physical memory.
93 COMPONENT_EXPORT(MIRACLE_PARAMETER)
94 base::TimeDelta GetMiracleParameterAsTimeDelta(const base::Feature& feature,
95                                                const std::string& param_name,
96                                                base::TimeDelta default_value);
97 
98 // Provides a similar behavior with FeatureParam<Enum> except the return value
99 // is determined by the amount of physical memory.
100 template <typename Enum>
GetMiracleParameterAsEnum(const base::Feature & feature,const std::string & param_name,const Enum default_value,const base::span<const typename base::FeatureParam<Enum>::Option> options)101 Enum GetMiracleParameterAsEnum(
102     const base::Feature& feature,
103     const std::string& param_name,
104     const Enum default_value,
105     const base::span<const typename base::FeatureParam<Enum>::Option> options) {
106   return GetFieldTrialParamByFeatureAsEnum(
107       feature, GetParamNameWithSuffix(param_name),
108       GetFieldTrialParamByFeatureAsEnum(feature, param_name, default_value,
109                                         options),
110       options);
111 }
112 
113 #define MIRACLE_PARAMETER_FOR_STRING(function_name, feature, param_name,    \
114                                      default_value)                         \
115   std::string function_name() {                                             \
116     static const std::string value =                                        \
117         miracle_parameter::GetMiracleParameterAsString(feature, param_name, \
118                                                        default_value);      \
119     return value;                                                           \
120   }
121 
122 #define MIRACLE_PARAMETER_FOR_DOUBLE(function_name, feature, param_name,    \
123                                      default_value)                         \
124   double function_name() {                                                  \
125     static const double value =                                             \
126         miracle_parameter::GetMiracleParameterAsDouble(feature, param_name, \
127                                                        default_value);      \
128     return value;                                                           \
129   }
130 
131 #define MIRACLE_PARAMETER_FOR_INT(function_name, feature, param_name,     \
132                                   default_value)                          \
133   int function_name() {                                                   \
134     static const int value = miracle_parameter::GetMiracleParameterAsInt( \
135         feature, param_name, default_value);                              \
136     return value;                                                         \
137   }
138 
139 #define MIRACLE_PARAMETER_FOR_BOOL(function_name, feature, param_name,      \
140                                    default_value)                           \
141   bool function_name() {                                                    \
142     static const bool value = miracle_parameter::GetMiracleParameterAsBool( \
143         feature, param_name, default_value);                                \
144     return value;                                                           \
145   }
146 
147 #define MIRACLE_PARAMETER_FOR_TIME_DELTA(function_name, feature, param_name,   \
148                                          default_value)                        \
149   base::TimeDelta function_name() {                                            \
150     static const base::TimeDelta value =                                       \
151         miracle_parameter::GetMiracleParameterAsTimeDelta(feature, param_name, \
152                                                           default_value);      \
153     return value;                                                              \
154   }
155 
156 #define MIRACLE_PARAMETER_FOR_ENUM(function_name, feature, param_name,      \
157                                    default_value, type, options)            \
158   type function_name() {                                                    \
159     static const type value = miracle_parameter::GetMiracleParameterAsEnum( \
160         feature, param_name, default_value, base::make_span(options));      \
161     return value;                                                           \
162   }
163 
164 }  // namespace miracle_parameter
165 
166 #endif  // COMPONENTS_MIRACLE_PARAMETER_COMMON_PUBLIC_MIRACLE_PARAMETER_H_
167