1 /* 2 * Copyright (C) 2021 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 #pragma once 18 19 #include <unordered_map> 20 #include <vector> 21 22 #include <android-modules-utils/sdk_level.h> 23 #include <gtest/gtest_prod.h> 24 #include <server_configurable_flags/get_flags.h> 25 26 #include <mutex> 27 #include <string> 28 29 namespace android { 30 namespace os { 31 namespace statsd { 32 33 using GetServerFlagFunc = 34 std::function<std::string(const std::string&, const std::string&, const std::string&)>; 35 using IsAtLeastSFunc = std::function<bool()>; 36 37 const std::string STATSD_NATIVE_NAMESPACE = "statsd_native"; 38 const std::string STATSD_NATIVE_BOOT_NAMESPACE = "statsd_native_boot"; 39 40 const std::string OPTIMIZATION_SOCKET_PARSING_FLAG = "optimization_socket_parsing"; 41 const std::string STATSD_INIT_COMPLETED_NO_DELAY_FLAG = "statsd_init_completed_no_delay"; 42 43 const std::string FLAG_TRUE = "true"; 44 const std::string FLAG_FALSE = "false"; 45 const std::string FLAG_EMPTY = ""; 46 47 class FlagProvider { 48 public: 49 static FlagProvider& getInstance(); 50 51 std::string getFlagString(const std::string& flagName, const std::string& defaultValue) const; 52 53 // Returns true IFF flagName has a value of "true". 54 bool getFlagBool(const std::string& flagName, const std::string& defaultValue) const; 55 56 std::string getBootFlagString(const std::string& flagName, 57 const std::string& defaultValue) const; 58 59 // Returns true IFF flagName has a value of "true". 60 bool getBootFlagBool(const std::string& flagName, const std::string& defaultValue) const; 61 62 // Queries the boot flags. Should only be called once at boot. 63 void initBootFlags(const std::vector<std::string>& flags); 64 65 private: 66 FlagProvider(); 67 68 // TODO(b/194347008): Remove the GetServerConfigurableFlag override. 69 void overrideFuncs( 70 const IsAtLeastSFunc& isAtLeastSFunc = &android::modules::sdklevel::IsAtLeastS, 71 const GetServerFlagFunc& getServerFlagFunc = 72 &server_configurable_flags::GetServerConfigurableFlag); 73 74 void overrideFuncsLocked( 75 const IsAtLeastSFunc& isAtLeastSFunc = &android::modules::sdklevel::IsAtLeastS, 76 const GetServerFlagFunc& getServerFlagFunc = 77 &server_configurable_flags::GetServerConfigurableFlag); 78 resetOverrides()79 inline void resetOverrides() { 80 std::lock_guard<std::mutex> lock(mFlagsMutex); 81 overrideFuncsLocked(); 82 mLocalFlags.clear(); 83 } 84 85 void overrideFlag(const std::string& flagName, const std::string& flagValue, 86 const bool isBootFlag = false); 87 88 std::string getFlagStringInternal(const std::string& flagName, const std::string& flagValue, 89 const bool isBootFlag) const; 90 91 std::string getLocalFlagKey(const std::string& flagName, const bool isBootFlag = false) const; 92 93 IsAtLeastSFunc mIsAtLeastSFunc; 94 GetServerFlagFunc mGetServerFlagFunc; 95 96 // Flag values updated only at boot. Used to store boot flags. 97 std::unordered_map<std::string, std::string> mBootFlags; 98 99 // Flag values to be locally overwritten. Only used in tests. 100 std::unordered_map<std::string, std::string> mLocalFlags; 101 102 mutable std::mutex mFlagsMutex; 103 104 friend class ConfigUpdateE2eTest; 105 friend class ConfigUpdateTest; 106 friend class EventMetricE2eTest; 107 friend class ValueMetricE2eTest; 108 friend class GaugeMetricE2ePulledTest; 109 friend class GaugeMetricE2ePushedTest; 110 friend class EventMetricProducerTest; 111 friend class FlagProviderTest_RMinus; 112 friend class FlagProviderTest_SPlus; 113 friend class FlagProviderTest_SPlus_RealValues; 114 friend class KllMetricE2eAbTest; 115 friend class MetricsManagerTest; 116 friend class StatsLogProcessorTest; 117 friend class StatsLogProcessorTestRestricted; 118 friend class RestrictedEventMetricProducerTest; 119 friend class RestrictedConfigE2ETest; 120 friend class RestrictedEventMetricE2eTest; 121 friend class LogEvent_FieldRestrictionTest; 122 123 FRIEND_TEST(ConfigUpdateE2eTest, TestEventMetric); 124 FRIEND_TEST(ConfigUpdateE2eTest, TestGaugeMetric); 125 FRIEND_TEST(ConfigUpdateE2eTest, TestConfigUpdateRestrictedDelegateCleared); 126 FRIEND_TEST(EventMetricE2eTest, TestEventMetricDataAggregated); 127 FRIEND_TEST(EventMetricProducerTest, TestOneAtomTagAggregatedEvents); 128 FRIEND_TEST(EventMetricProducerTest, TestTwoAtomTagAggregatedEvents); 129 FRIEND_TEST(GaugeMetricE2ePulledTest, TestRandomSamplePulledEventsNoCondition); 130 FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagTrue); 131 FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagFalse); 132 FRIEND_TEST(FlagProviderTest_SPlus, TestOverrideLocalFlags); 133 FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagEmptyDefaultFalse); 134 FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagEmptyDefaultTrue); 135 FRIEND_TEST(FlagProviderTest_SPlus_RealValues, TestGetBootFlagBoolServerFlagTrue); 136 FRIEND_TEST(FlagProviderTest_SPlus_RealValues, TestGetBootFlagBoolServerFlagFalse); 137 FRIEND_TEST(MetricsManagerTest_SPlus, TestRestrictedMetricsConfig); 138 FRIEND_TEST(RestrictedEventMetricE2eTest, TestFlagDisabled); 139 FRIEND_TEST(LogEventTest, TestRestrictionCategoryAnnotation); 140 FRIEND_TEST(LogEventTest, TestInvalidRestrictionCategoryAnnotation); 141 FRIEND_TEST(LogEventTest, TestRestrictionCategoryAnnotationFlagDisabled); 142 FRIEND_TEST(LogEvent_FieldRestrictionTest, TestFieldRestrictionAnnotation); 143 FRIEND_TEST(LogEvent_FieldRestrictionTest, TestInvalidAnnotationIntType); 144 FRIEND_TEST(LogEvent_FieldRestrictionTest, TestInvalidAnnotationAtomLevel); 145 FRIEND_TEST(LogEvent_FieldRestrictionTest, TestRestrictionCategoryAnnotationFlagDisabled); 146 }; 147 148 } // namespace statsd 149 } // namespace os 150 } // namespace android 151