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 FLAG_TRUE = "true"; 41 const std::string FLAG_FALSE = "false"; 42 const std::string FLAG_EMPTY = ""; 43 44 class FlagProvider { 45 public: 46 static FlagProvider& getInstance(); 47 48 std::string getFlagString(const std::string& flagName, const std::string& defaultValue) const; 49 50 // Returns true IFF flagName has a value of "true". 51 bool getFlagBool(const std::string& flagName, const std::string& defaultValue) const; 52 53 std::string getBootFlagString(const std::string& flagName, 54 const std::string& defaultValue) const; 55 56 // Returns true IFF flagName has a value of "true". 57 bool getBootFlagBool(const std::string& flagName, const std::string& defaultValue) const; 58 59 // Queries the boot flags. Should only be called once at boot. 60 void initBootFlags(const std::vector<std::string>& flags); 61 62 private: 63 FlagProvider(); 64 65 // TODO(b/194347008): Remove the GetServerConfigurableFlag override. 66 void overrideFuncs( 67 const IsAtLeastSFunc& isAtLeastSFunc = &android::modules::sdklevel::IsAtLeastS, 68 const GetServerFlagFunc& getServerFlagFunc = 69 &server_configurable_flags::GetServerConfigurableFlag); 70 71 void overrideFuncsLocked( 72 const IsAtLeastSFunc& isAtLeastSFunc = &android::modules::sdklevel::IsAtLeastS, 73 const GetServerFlagFunc& getServerFlagFunc = 74 &server_configurable_flags::GetServerConfigurableFlag); 75 resetOverrides()76 inline void resetOverrides() { 77 std::lock_guard<std::mutex> lock(mFlagsMutex); 78 overrideFuncsLocked(); 79 mLocalFlags.clear(); 80 } 81 82 void overrideFlag(const std::string& flagName, const std::string& flagValue, 83 const bool isBootFlag = false); 84 85 std::string getFlagStringInternal(const std::string& flagName, const std::string& flagValue, 86 const bool isBootFlag) const; 87 88 std::string getLocalFlagKey(const std::string& flagName, const bool isBootFlag = false) const; 89 90 IsAtLeastSFunc mIsAtLeastSFunc; 91 GetServerFlagFunc mGetServerFlagFunc; 92 93 // Flag values updated only at boot. Used to store boot flags. 94 std::unordered_map<std::string, std::string> mBootFlags; 95 96 // Flag values to be locally overwritten. Only used in tests. 97 std::unordered_map<std::string, std::string> mLocalFlags; 98 99 mutable std::mutex mFlagsMutex; 100 101 friend class ConfigUpdateE2eTest; 102 friend class ConfigUpdateTest; 103 friend class EventMetricE2eTest; 104 friend class GaugeMetricE2ePulledTest; 105 friend class GaugeMetricE2ePushedTest; 106 friend class EventMetricProducerTest; 107 friend class FlagProviderTest_RMinus; 108 friend class FlagProviderTest_SPlus; 109 friend class FlagProviderTest_SPlus_RealValues; 110 friend class KllMetricE2eAbTest; 111 friend class MetricsManagerTest; 112 friend class PartialBucketE2e_AppUpgradeDefaultTest; 113 114 FRIEND_TEST(ConfigUpdateE2eTest, TestKllMetric_KllDisabledBeforeConfigUpdate); 115 FRIEND_TEST(ConfigUpdateE2eTest, TestEventMetric); 116 FRIEND_TEST(ConfigUpdateE2eTest, TestGaugeMetric); 117 FRIEND_TEST(EventMetricE2eTest, TestEventMetricDataAggregated); 118 FRIEND_TEST(EventMetricProducerTest, TestOneAtomTagAggregatedEvents); 119 FRIEND_TEST(EventMetricProducerTest, TestTwoAtomTagAggregatedEvents); 120 FRIEND_TEST(GaugeMetricE2ePulledTest, TestRandomSamplePulledEventsNoCondition); 121 FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagTrue); 122 FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagFalse); 123 FRIEND_TEST(FlagProviderTest_SPlus, TestOverrideLocalFlags); 124 FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagEmptyDefaultFalse); 125 FRIEND_TEST(FlagProviderTest_SPlus, TestGetFlagBoolServerFlagEmptyDefaultTrue); 126 FRIEND_TEST(FlagProviderTest_SPlus_RealValues, TestGetBootFlagBoolServerFlagTrue); 127 FRIEND_TEST(FlagProviderTest_SPlus_RealValues, TestGetBootFlagBoolServerFlagFalse); 128 FRIEND_TEST(NumericValueMetricProducerTest_SubsetDimensions, TestSubsetDimensions); 129 FRIEND_TEST(PartialBucketE2e_AppUpgradeDefaultTest, TestCountMetricDefaultFalse); 130 FRIEND_TEST(PartialBucketE2e_AppUpgradeDefaultTest, TestCountMetricDefaultTrue); 131 }; 132 133 } // namespace statsd 134 } // namespace os 135 } // namespace android 136