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 #include "FlagProvider.h"
18
19 using android::modules::sdklevel::IsAtLeastS;
20 using server_configurable_flags::GetServerConfigurableFlag;
21 using std::string;
22 using std::vector;
23
24 namespace android {
25 namespace os {
26 namespace statsd {
27
FlagProvider()28 FlagProvider::FlagProvider()
29 : mIsAtLeastSFunc(IsAtLeastS), mGetServerFlagFunc(GetServerConfigurableFlag) {
30 }
31
getInstance()32 FlagProvider& FlagProvider::getInstance() {
33 static FlagProvider instance;
34 return instance;
35 }
36
getFlagString(const string & flagName,const string & defaultValue) const37 string FlagProvider::getFlagString(const string& flagName, const string& defaultValue) const {
38 return getFlagStringInternal(flagName, defaultValue, /* isBootFlag= */ false);
39 }
40
getFlagBool(const string & flagName,const string & defaultValue) const41 bool FlagProvider::getFlagBool(const string& flagName, const string& defaultValue) const {
42 return getFlagStringInternal(flagName, defaultValue, /* isBootFlag= */ false) == FLAG_TRUE;
43 }
44
getBootFlagString(const string & flagName,const string & defaultValue) const45 string FlagProvider::getBootFlagString(const string& flagName, const string& defaultValue) const {
46 return getFlagStringInternal(flagName, defaultValue, /* isBootFlag= */ true);
47 }
48
getBootFlagBool(const string & flagName,const string & defaultValue) const49 bool FlagProvider::getBootFlagBool(const string& flagName, const string& defaultValue) const {
50 return getFlagStringInternal(flagName, defaultValue, /* isBootFlag= */ true) == FLAG_TRUE;
51 }
52
initBootFlags(const vector<string> & flags)53 void FlagProvider::initBootFlags(const vector<string>& flags) {
54 std::lock_guard<std::mutex> lock(mFlagsMutex);
55 mBootFlags.clear();
56 for (const string& flagName : flags) {
57 string flagVal = mGetServerFlagFunc(STATSD_NATIVE_BOOT_NAMESPACE, flagName, FLAG_EMPTY);
58 if (flagVal != FLAG_EMPTY) {
59 mBootFlags[flagName] = flagVal;
60 }
61 }
62 }
63
overrideFlag(const string & flagName,const std::string & flagValue,const bool isBootFlag)64 void FlagProvider::overrideFlag(const string& flagName, const std::string& flagValue,
65 const bool isBootFlag) {
66 std::lock_guard<std::mutex> lock(mFlagsMutex);
67 mLocalFlags[getLocalFlagKey(flagName, isBootFlag)] = flagValue;
68 }
69
overrideFuncs(const IsAtLeastSFunc & isAtLeastSFunc,const GetServerFlagFunc & getServerFlagFunc)70 void FlagProvider::overrideFuncs(const IsAtLeastSFunc& isAtLeastSFunc,
71 const GetServerFlagFunc& getServerFlagFunc) {
72 std::lock_guard<std::mutex> lock(mFlagsMutex);
73 overrideFuncsLocked(isAtLeastSFunc, getServerFlagFunc);
74 }
75
overrideFuncsLocked(const IsAtLeastSFunc & isAtLeastSFunc,const GetServerFlagFunc & getServerFlagFunc)76 void FlagProvider::overrideFuncsLocked(const IsAtLeastSFunc& isAtLeastSFunc,
77 const GetServerFlagFunc& getServerFlagFunc) {
78 mIsAtLeastSFunc = isAtLeastSFunc;
79 mGetServerFlagFunc = getServerFlagFunc;
80 }
81
getLocalFlagKey(const string & flagName,const bool isBootFlag) const82 string FlagProvider::getLocalFlagKey(const string& flagName, const bool isBootFlag) const {
83 return isBootFlag ? STATSD_NATIVE_BOOT_NAMESPACE + "." + flagName
84 : STATSD_NATIVE_NAMESPACE + "." + flagName;
85 }
86
getFlagStringInternal(const std::string & flagName,const std::string & defaultValue,const bool isBootFlag) const87 string FlagProvider::getFlagStringInternal(const std::string& flagName,
88 const std::string& defaultValue,
89 const bool isBootFlag) const {
90 std::lock_guard<std::mutex> lock(mFlagsMutex);
91 if (!mIsAtLeastSFunc()) {
92 return defaultValue;
93 }
94 string localFlagKey = getLocalFlagKey(flagName, isBootFlag);
95 if (mLocalFlags.find(localFlagKey) != mLocalFlags.end()) {
96 return mLocalFlags.at(localFlagKey);
97 }
98 if (!isBootFlag) {
99 return mGetServerFlagFunc(STATSD_NATIVE_NAMESPACE, flagName, defaultValue);
100 }
101 const auto& it = mBootFlags.find(flagName);
102 return it == mBootFlags.end() ? defaultValue : it->second;
103 }
104
105 } // namespace statsd
106 } // namespace os
107 } // namespace android
108