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 #include "components/miracle_parameter/common/public/miracle_parameter.h"
6
7 #include "base/command_line.h"
8 #include "base/strings/strcat.h"
9 #include "base/system/sys_info.h"
10
11 namespace miracle_parameter {
12
GetParamNameWithSuffix(const std::string & param_name)13 std::string GetParamNameWithSuffix(const std::string& param_name) {
14 // `base::SysInfo::AmountOfPhysicalMemoryMB()` refers to CommandLine
15 // internally. If the CommandLine is not initialized, we return early to avoid
16 // a crash.
17 if (!base::CommandLine::InitializedForCurrentProcess()) {
18 return param_name;
19 }
20 int physical_memory_mb = base::SysInfo::AmountOfPhysicalMemoryMB();
21 const char* suffix =
22 physical_memory_mb < kMiracleParameterMemory512MB ? "ForLessThan512MB"
23 : physical_memory_mb < kMiracleParameterMemory1GB ? "For512MBTo1GB"
24 : physical_memory_mb < kMiracleParameterMemory2GB ? "For1GBTo2GB"
25 : physical_memory_mb < kMiracleParameterMemory4GB ? "For2GBTo4GB"
26 : physical_memory_mb < kMiracleParameterMemory8GB ? "For4GBTo8GB"
27 : physical_memory_mb < kMiracleParameterMemory16GB ? "For8GBTo16GB"
28 : "For16GBAndAbove";
29 return base::StrCat({param_name, suffix});
30 }
31
GetMiracleParameterAsString(const base::Feature & feature,const std::string & param_name,const std::string & default_value)32 std::string GetMiracleParameterAsString(const base::Feature& feature,
33 const std::string& param_name,
34 const std::string& default_value) {
35 return base::GetFieldTrialParamByFeatureAsString(
36 feature, GetParamNameWithSuffix(param_name),
37 base::GetFieldTrialParamByFeatureAsString(feature, param_name,
38 default_value));
39 }
40
GetMiracleParameterAsDouble(const base::Feature & feature,const std::string & param_name,double default_value)41 double GetMiracleParameterAsDouble(const base::Feature& feature,
42 const std::string& param_name,
43 double default_value) {
44 return base::GetFieldTrialParamByFeatureAsDouble(
45 feature, GetParamNameWithSuffix(param_name),
46 base::GetFieldTrialParamByFeatureAsDouble(feature, param_name,
47 default_value));
48 }
49
GetMiracleParameterAsInt(const base::Feature & feature,const std::string & param_name,int default_value)50 int GetMiracleParameterAsInt(const base::Feature& feature,
51 const std::string& param_name,
52 int default_value) {
53 return base::GetFieldTrialParamByFeatureAsInt(
54 feature, GetParamNameWithSuffix(param_name),
55 base::GetFieldTrialParamByFeatureAsInt(feature, param_name,
56 default_value));
57 }
58
GetMiracleParameterAsBool(const base::Feature & feature,const std::string & param_name,bool default_value)59 bool GetMiracleParameterAsBool(const base::Feature& feature,
60 const std::string& param_name,
61 bool default_value) {
62 return base::GetFieldTrialParamByFeatureAsBool(
63 feature, GetParamNameWithSuffix(param_name),
64 base::GetFieldTrialParamByFeatureAsBool(feature, param_name,
65 default_value));
66 }
67
GetMiracleParameterAsTimeDelta(const base::Feature & feature,const std::string & param_name,base::TimeDelta default_value)68 base::TimeDelta GetMiracleParameterAsTimeDelta(const base::Feature& feature,
69 const std::string& param_name,
70 base::TimeDelta default_value) {
71 return base::GetFieldTrialParamByFeatureAsTimeDelta(
72 feature, GetParamNameWithSuffix(param_name),
73 base::GetFieldTrialParamByFeatureAsTimeDelta(feature, param_name,
74 default_value));
75 }
76
77 } // namespace miracle_parameter
78