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