• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TOOLS_SYSTEM_PROPERTIES_H_
18 #define ART_LIBARTTOOLS_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   virtual ~SystemProperties() = default;
33 
34   // Returns the current value of the system property `key`, or `default_value` if the property
35   // doesn't have a value.
Get(const std::string & key,const std::string & default_value)36   std::string Get(const std::string& key, const std::string& default_value) const {
37     std::string value = GetProperty(key);
38     if (!value.empty()) {
39       return value;
40     }
41     return default_value;
42   }
43 
44   // Same as above, but allows specifying one or more fallback keys. The last argument is a string
45   // default value that will be used if none of the given keys has a value.
46   //
47   // Usage:
48   //
49   // Look up for "key_1", then "key_2", then "key_3". If none of them has a value, return "default":
50   //   Get("key_1", "key_2", "key_3", /*default_value=*/"default")
51   template <typename... Args>
Get(const std::string & key,const std::string & fallback_key,Args...args)52   std::string Get(const std::string& key, const std::string& fallback_key, Args... args) const {
53     return Get(key, Get(fallback_key, args...));
54   }
55 
56   // Returns the current value of the system property `key` with zero or more fallback keys, or an
57   // empty string if none of the given keys has a value.
58   //
59   // Usage:
60   //
61   // Look up for "key_1". If it doesn't have a value, return an empty string:
62   //  GetOrEmpty("key_1")
63   //
64   // Look up for "key_1", then "key_2", then "key_3". If none of them has a value, return an empty
65   // string:
66   //  GetOrEmpty("key_1", "key_2", "key_3")
67   template <typename... Args>
GetOrEmpty(const std::string & key,Args...fallback_keys)68   std::string GetOrEmpty(const std::string& key, Args... fallback_keys) const {
69     return Get(key, fallback_keys..., /*default_value=*/"");
70   }
71 
72   // Returns the current value of the boolean system property `key`, or `default_value` if the
73   // property doesn't have a value. See `android::base::ParseBool` for how the value is parsed.
GetBool(const std::string & key,bool default_value)74   bool GetBool(const std::string& key, bool default_value) const {
75     android::base::ParseBoolResult result = android::base::ParseBool(GetProperty(key));
76     if (result != android::base::ParseBoolResult::kError) {
77       return result == android::base::ParseBoolResult::kTrue;
78     }
79     return default_value;
80   }
81 
82   // Same as above, but allows specifying one or more fallback keys. The last argument is a bool
83   // default value that will be used if none of the given keys has a value.
84   //
85   // Usage:
86   //
87   // Look up for "key_1", then "key_2", then "key_3". If none of them has a value, return true:
88   //   Get("key_1", "key_2", "key_3", /*default_value=*/true)
89   template <typename... Args>
GetBool(const std::string & key,const std::string & fallback_key,Args...args)90   bool GetBool(const std::string& key, const std::string& fallback_key, Args... args) const {
91     return GetBool(key, GetBool(fallback_key, args...));
92   }
93 
94  protected:
95   // The single source of truth of system properties. Can be mocked in unit tests.
GetProperty(const std::string & key)96   virtual std::string GetProperty(const std::string& key) const {
97     return android::base::GetProperty(key, /*default_value=*/"");
98   }
99 };
100 
101 }  // namespace tools
102 }  // namespace art
103 
104 #endif  // ART_LIBARTTOOLS_TOOLS_SYSTEM_PROPERTIES_H_
105