xref: /aosp_15_r20/art/libarttools/include/tools/system_properties.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2022 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBARTTOOLS_INCLUDE_TOOLS_SYSTEM_PROPERTIES_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_LIBARTTOOLS_INCLUDE_TOOLS_SYSTEM_PROPERTIES_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <string>
21*795d594fSAndroid Build Coastguard Worker 
22*795d594fSAndroid Build Coastguard Worker #include "android-base/parsebool.h"
23*795d594fSAndroid Build Coastguard Worker #include "android-base/properties.h"
24*795d594fSAndroid Build Coastguard Worker 
25*795d594fSAndroid Build Coastguard Worker namespace art {
26*795d594fSAndroid Build Coastguard Worker namespace tools {
27*795d594fSAndroid Build Coastguard Worker 
28*795d594fSAndroid Build Coastguard Worker // A class for getting system properties with fallback lookup support. Different from
29*795d594fSAndroid Build Coastguard Worker // android::base::GetProperty, this class is mockable.
30*795d594fSAndroid Build Coastguard Worker class SystemProperties {
31*795d594fSAndroid Build Coastguard Worker  public:
32*795d594fSAndroid Build Coastguard Worker   SystemProperties() = default;
33*795d594fSAndroid Build Coastguard Worker   SystemProperties(const SystemProperties& other) = default;
34*795d594fSAndroid Build Coastguard Worker   SystemProperties& operator=(const SystemProperties& other) = default;
35*795d594fSAndroid Build Coastguard Worker   SystemProperties(SystemProperties&& other) = default;
36*795d594fSAndroid Build Coastguard Worker   SystemProperties& operator=(SystemProperties&& other) = default;
37*795d594fSAndroid Build Coastguard Worker   virtual ~SystemProperties() = default;
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker   // Returns the current value of the system property `key`, or `default_value` if the property
40*795d594fSAndroid Build Coastguard Worker   // doesn't have a value.
Get(const std::string & key,const std::string & default_value)41*795d594fSAndroid Build Coastguard Worker   std::string Get(const std::string& key, const std::string& default_value) const {
42*795d594fSAndroid Build Coastguard Worker     std::string value = GetProperty(key);
43*795d594fSAndroid Build Coastguard Worker     if (!value.empty()) {
44*795d594fSAndroid Build Coastguard Worker       return value;
45*795d594fSAndroid Build Coastguard Worker     }
46*795d594fSAndroid Build Coastguard Worker     return default_value;
47*795d594fSAndroid Build Coastguard Worker   }
48*795d594fSAndroid Build Coastguard Worker 
49*795d594fSAndroid Build Coastguard Worker   // Same as above, but allows specifying one or more fallback keys. The last argument is a string
50*795d594fSAndroid Build Coastguard Worker   // default value that will be used if none of the given keys has a value.
51*795d594fSAndroid Build Coastguard Worker   //
52*795d594fSAndroid Build Coastguard Worker   // Usage:
53*795d594fSAndroid Build Coastguard Worker   //
54*795d594fSAndroid Build Coastguard Worker   // Look up for "key_1", then "key_2", then "key_3". If none of them has a value, return "default":
55*795d594fSAndroid Build Coastguard Worker   //   Get("key_1", "key_2", "key_3", /*default_value=*/"default")
56*795d594fSAndroid Build Coastguard Worker   template <typename... Args>
Get(const std::string & key,const std::string & fallback_key,Args...args)57*795d594fSAndroid Build Coastguard Worker   std::string Get(const std::string& key, const std::string& fallback_key, Args... args) const {
58*795d594fSAndroid Build Coastguard Worker     return Get(key, Get(fallback_key, args...));
59*795d594fSAndroid Build Coastguard Worker   }
60*795d594fSAndroid Build Coastguard Worker 
61*795d594fSAndroid Build Coastguard Worker   // Returns the current value of the system property `key` with zero or more fallback keys, or an
62*795d594fSAndroid Build Coastguard Worker   // empty string if none of the given keys has a value.
63*795d594fSAndroid Build Coastguard Worker   //
64*795d594fSAndroid Build Coastguard Worker   // Usage:
65*795d594fSAndroid Build Coastguard Worker   //
66*795d594fSAndroid Build Coastguard Worker   // Look up for "key_1". If it doesn't have a value, return an empty string:
67*795d594fSAndroid Build Coastguard Worker   //  GetOrEmpty("key_1")
68*795d594fSAndroid Build Coastguard Worker   //
69*795d594fSAndroid Build Coastguard Worker   // Look up for "key_1", then "key_2", then "key_3". If none of them has a value, return an empty
70*795d594fSAndroid Build Coastguard Worker   // string:
71*795d594fSAndroid Build Coastguard Worker   //  GetOrEmpty("key_1", "key_2", "key_3")
72*795d594fSAndroid Build Coastguard Worker   template <typename... Args>
GetOrEmpty(const std::string & key,Args...fallback_keys)73*795d594fSAndroid Build Coastguard Worker   std::string GetOrEmpty(const std::string& key, Args... fallback_keys) const {
74*795d594fSAndroid Build Coastguard Worker     return Get(key, fallback_keys..., /*default_value=*/"");
75*795d594fSAndroid Build Coastguard Worker   }
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   // Returns the current value of the boolean system property `key`, or `default_value` if the
78*795d594fSAndroid Build Coastguard Worker   // property doesn't have a value. See `android::base::ParseBool` for how the value is parsed.
GetBool(const std::string & key,bool default_value)79*795d594fSAndroid Build Coastguard Worker   bool GetBool(const std::string& key, bool default_value) const {
80*795d594fSAndroid Build Coastguard Worker     android::base::ParseBoolResult result = android::base::ParseBool(GetProperty(key));
81*795d594fSAndroid Build Coastguard Worker     if (result != android::base::ParseBoolResult::kError) {
82*795d594fSAndroid Build Coastguard Worker       return result == android::base::ParseBoolResult::kTrue;
83*795d594fSAndroid Build Coastguard Worker     }
84*795d594fSAndroid Build Coastguard Worker     return default_value;
85*795d594fSAndroid Build Coastguard Worker   }
86*795d594fSAndroid Build Coastguard Worker 
87*795d594fSAndroid Build Coastguard Worker   // Same as above, but allows specifying one or more fallback keys. The last argument is a bool
88*795d594fSAndroid Build Coastguard Worker   // default value that will be used if none of the given keys has a value.
89*795d594fSAndroid Build Coastguard Worker   //
90*795d594fSAndroid Build Coastguard Worker   // Usage:
91*795d594fSAndroid Build Coastguard Worker   //
92*795d594fSAndroid Build Coastguard Worker   // Look up for "key_1", then "key_2", then "key_3". If none of them has a value, return true:
93*795d594fSAndroid Build Coastguard Worker   //   Get("key_1", "key_2", "key_3", /*default_value=*/true)
94*795d594fSAndroid Build Coastguard Worker   template <typename... Args>
GetBool(const std::string & key,const std::string & fallback_key,Args...args)95*795d594fSAndroid Build Coastguard Worker   bool GetBool(const std::string& key, const std::string& fallback_key, Args... args) const {
96*795d594fSAndroid Build Coastguard Worker     return GetBool(key, GetBool(fallback_key, args...));
97*795d594fSAndroid Build Coastguard Worker   }
98*795d594fSAndroid Build Coastguard Worker 
99*795d594fSAndroid Build Coastguard Worker  protected:
100*795d594fSAndroid Build Coastguard Worker   // The single source of truth of system properties. Can be mocked in unit tests.
GetProperty(const std::string & key)101*795d594fSAndroid Build Coastguard Worker   virtual std::string GetProperty(const std::string& key) const {
102*795d594fSAndroid Build Coastguard Worker     return android::base::GetProperty(key, /*default_value=*/"");
103*795d594fSAndroid Build Coastguard Worker   }
104*795d594fSAndroid Build Coastguard Worker };
105*795d594fSAndroid Build Coastguard Worker 
106*795d594fSAndroid Build Coastguard Worker }  // namespace tools
107*795d594fSAndroid Build Coastguard Worker }  // namespace art
108*795d594fSAndroid Build Coastguard Worker 
109*795d594fSAndroid Build Coastguard Worker #endif  // ART_LIBARTTOOLS_INCLUDE_TOOLS_SYSTEM_PROPERTIES_H_
110