!14486 Add json config file suport

Merge pull request !14486 from yaretskiynikita/master
This commit is contained in:
openharmony_ci 2024-09-10 13:41:50 +00:00 committed by Gitee
commit b7977ad047
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 44 additions and 4 deletions

View File

@ -94,6 +94,7 @@ if (current_os != "ohos") {
"bundle_framework:appexecfwk_base",
"bundle_framework:appexecfwk_core",
"c_utils:utils",
"config_policy:configpolicy_util",
"graphic_surface:surface",
"hilog:libhilog",
"init:libbeget_proxy",

View File

@ -24,13 +24,15 @@
#include <vector>
#include "json/json.h"
#include "config_policy_utils.h"
#include "wrapper_log.h"
namespace OHOS {
namespace {
const std::string JSON_CONFIG_PATH = "etc/graphics_game/config/graphics_game.json";
const std::string DEFAULT_JSON_CONFIG = R"__(
{
"enableAppMode" : true,
@ -83,28 +85,64 @@ bool EglSystemLayersManager::GetProcessName(pid_t pid, char *pname, int len)
return true;
}
bool EglSystemLayersManager::GetJsonConfig(Json::Value &configData)
bool EglSystemLayersManager::GetDefaultJsonConfig(Json::Value &configData)
{
WLOGD("Read default json config");
Json::CharReaderBuilder builder;
Json::CharReader *charReader = builder.newCharReader();
if (!charReader) {
WLOGE("Failed to create new Json::CharReader");
return false;
}
const std::unique_ptr<Json::CharReader> reader(charReader);
JSONCPP_STRING errs;
bool ret = reader->parse(DEFAULT_JSON_CONFIG.c_str(),
DEFAULT_JSON_CONFIG.c_str() + static_cast<int>(DEFAULT_JSON_CONFIG.length()), &configData, &errs);
if (!ret) {
WLOGE("json parse error: %{private}s", errs.c_str());
WLOGE("default json config parse error: %{private}s", errs.c_str());
return false;
} else {
WLOGD("json parse success");
WLOGD("default json config parse success");
}
return true;
}
bool EglSystemLayersManager::GetJsonConfig(Json::Value &configData)
{
char pathBuf[MAX_PATH_LEN] = {'\0'};
char *path = GetOneCfgFile(JSON_CONFIG_PATH.c_str(), pathBuf, MAX_PATH_LEN);
if (!path) {
WLOGE("Failed to find system config path");
return GetDefaultJsonConfig(configData);
}
std::ifstream configFile(std::string(pathBuf), std::ifstream::in);
if (!configFile.good()) {
WLOGE("Failed to open system json config file");
return GetDefaultJsonConfig(configData);
}
Json::CharReaderBuilder builder;
JSONCPP_STRING errs;
bool readSuccess = Json::parseFromStream(builder, configFile, &configData, &errs);
if (!readSuccess) {
WLOGE("Failed to parse system json config file, error: %{private}s", errs.c_str());
return GetDefaultJsonConfig(configData);
}
std::string hookLayerName("HOOK_LAYER");
if (!configData.isMember(hookLayerName)) {
WLOGE("Failed to find %{private}s section in system json config file", hookLayerName.c_str());
return GetDefaultJsonConfig(configData);
}
configData = configData[hookLayerName];
return true;
}
std::vector<std::string> EglSystemLayersManager::GetStringVectorFromJson(const Json::Value &jsonVector)
{
std::vector<std::string> stringVector{};

View File

@ -50,6 +50,7 @@ public:
private:
bool GetProcessName(pid_t pid, char *pname, int len);
bool GetJsonConfig(Json::Value &configData);
bool GetDefaultJsonConfig(Json::Value &configData);
std::vector<std::string> GetSystemLayersFromConfig(Json::Value &appModeSection,
const std::string &processName);
std::vector<std::string> GetStringVectorFromJson(const Json::Value &jsonVector);