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