11ebd3d54Sopenharmony_ci/*
21ebd3d54Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
31ebd3d54Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41ebd3d54Sopenharmony_ci * you may not use this file except in compliance with the License.
51ebd3d54Sopenharmony_ci * You may obtain a copy of the License at
61ebd3d54Sopenharmony_ci *
71ebd3d54Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81ebd3d54Sopenharmony_ci *
91ebd3d54Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101ebd3d54Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111ebd3d54Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121ebd3d54Sopenharmony_ci * See the License for the specific language governing permissions and
131ebd3d54Sopenharmony_ci * limitations under the License.
141ebd3d54Sopenharmony_ci */
151ebd3d54Sopenharmony_ci
161ebd3d54Sopenharmony_ci#include "bgtask_config.h"
171ebd3d54Sopenharmony_ci#include "data_storage_helper.h"
181ebd3d54Sopenharmony_ci#include "bgtaskmgr_log_wrapper.h"
191ebd3d54Sopenharmony_ci#include "parameters.h"
201ebd3d54Sopenharmony_ci
211ebd3d54Sopenharmony_cinamespace OHOS {
221ebd3d54Sopenharmony_cinamespace BackgroundTaskMgr {
231ebd3d54Sopenharmony_cinamespace {
241ebd3d54Sopenharmony_ciconst std::string SUSPEND_MANAGER_CONFIG_FILE = "etc/efficiency_manager/suspend_manager_config.json";
251ebd3d54Sopenharmony_ciconst std::string CONFIG_JSON_INDEX_TOP = "params";
261ebd3d54Sopenharmony_ciconst std::string CONFIG_JSON_INDEX_SUSPEND_SECOND = "param";
271ebd3d54Sopenharmony_ciconst std::string TRANSIENT_ERR_DELAYED_FROZEN_LIST = "transient_err_delayed_frozen_list";
281ebd3d54Sopenharmony_ciconst std::string TRANSIENT_EXEMPTED_QUOTA = "transient_exempted_quota";
291ebd3d54Sopenharmony_ciconst std::string TRANSIENT_ERR_DELAYED_FROZEN_TIME = "transient_err_delayed_frozen_time";
301ebd3d54Sopenharmony_ci}
311ebd3d54Sopenharmony_ci
321ebd3d54Sopenharmony_civoid BgtaskConfig::Init()
331ebd3d54Sopenharmony_ci{
341ebd3d54Sopenharmony_ci    if (isInit_) {
351ebd3d54Sopenharmony_ci        BGTASK_LOGE("already init config!");
361ebd3d54Sopenharmony_ci        return;
371ebd3d54Sopenharmony_ci    }
381ebd3d54Sopenharmony_ci
391ebd3d54Sopenharmony_ci    LoadConfigFile();
401ebd3d54Sopenharmony_ci    isInit_ = true;
411ebd3d54Sopenharmony_ci}
421ebd3d54Sopenharmony_ci
431ebd3d54Sopenharmony_civoid BgtaskConfig::LoadConfigFile()
441ebd3d54Sopenharmony_ci{
451ebd3d54Sopenharmony_ci    nlohmann::json jsonObj;
461ebd3d54Sopenharmony_ci    std::string absolutePath = DelayedSingleton<DataStorageHelper>::GetInstance()->
471ebd3d54Sopenharmony_ci        GetConfigFileAbsolutePath(SUSPEND_MANAGER_CONFIG_FILE);
481ebd3d54Sopenharmony_ci    if (DelayedSingleton<DataStorageHelper>::GetInstance()->ParseJsonValueFromFile(jsonObj, absolutePath) != 0) {
491ebd3d54Sopenharmony_ci        BGTASK_LOGE("LoadConfigFile failed");
501ebd3d54Sopenharmony_ci        return;
511ebd3d54Sopenharmony_ci    }
521ebd3d54Sopenharmony_ci    ParseTransientTaskExemptedQuatoList(jsonObj);
531ebd3d54Sopenharmony_ci    ParseTransientTaskExemptedQuato(jsonObj);
541ebd3d54Sopenharmony_ci}
551ebd3d54Sopenharmony_ci
561ebd3d54Sopenharmony_civoid BgtaskConfig::ParseTransientTaskExemptedQuatoList(const nlohmann::json &jsonObj)
571ebd3d54Sopenharmony_ci{
581ebd3d54Sopenharmony_ci    nlohmann::json appArray;
591ebd3d54Sopenharmony_ci    if (jsonObj.is_null() || jsonObj.empty()) {
601ebd3d54Sopenharmony_ci        BGTASK_LOGE("jsonObj null");
611ebd3d54Sopenharmony_ci        return;
621ebd3d54Sopenharmony_ci    }
631ebd3d54Sopenharmony_ci    if (!jsonObj.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) ||
641ebd3d54Sopenharmony_ci        !jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
651ebd3d54Sopenharmony_ci        BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
661ebd3d54Sopenharmony_ci        return;
671ebd3d54Sopenharmony_ci    }
681ebd3d54Sopenharmony_ci    appArray = jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
691ebd3d54Sopenharmony_ci    std::lock_guard<std::mutex> lock(configMutex_);
701ebd3d54Sopenharmony_ci    for (const auto &app : appArray) {
711ebd3d54Sopenharmony_ci        transientTaskExemptedQuatoList_.insert(app);
721ebd3d54Sopenharmony_ci    }
731ebd3d54Sopenharmony_ci    for (const auto &app : transientTaskExemptedQuatoList_) {
741ebd3d54Sopenharmony_ci        BGTASK_LOGI("ParseTransientTaskExemptedQuatoList: %{public}s.", app.c_str());
751ebd3d54Sopenharmony_ci    }
761ebd3d54Sopenharmony_ci}
771ebd3d54Sopenharmony_ci
781ebd3d54Sopenharmony_cibool BgtaskConfig::AddExemptedQuatoData(const std::string &configData, int32_t sourceType)
791ebd3d54Sopenharmony_ci{
801ebd3d54Sopenharmony_ci    const nlohmann::json &jsonObj = nlohmann::json::parse(configData, nullptr, false);
811ebd3d54Sopenharmony_ci    if (jsonObj.is_discarded()) {
821ebd3d54Sopenharmony_ci        BGTASK_LOGE("jsonObj parse fail");
831ebd3d54Sopenharmony_ci        return false;
841ebd3d54Sopenharmony_ci    }
851ebd3d54Sopenharmony_ci    if (jsonObj.is_null() || jsonObj.empty()) {
861ebd3d54Sopenharmony_ci        BGTASK_LOGE("jsonObj null");
871ebd3d54Sopenharmony_ci        return false;
881ebd3d54Sopenharmony_ci    }
891ebd3d54Sopenharmony_ci    if (sourceType == ConfigDataSourceType::CONFIG_CLOUD && !SetCloudConfigParam(jsonObj)) {
901ebd3d54Sopenharmony_ci        return false;
911ebd3d54Sopenharmony_ci    } else if (sourceType == ConfigDataSourceType::CONFIG_SUSPEND_MANAGER) {
921ebd3d54Sopenharmony_ci        nlohmann::json appArray;
931ebd3d54Sopenharmony_ci        if (!jsonObj.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) ||
941ebd3d54Sopenharmony_ci            !jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
951ebd3d54Sopenharmony_ci            BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
961ebd3d54Sopenharmony_ci            return false;
971ebd3d54Sopenharmony_ci        }
981ebd3d54Sopenharmony_ci        appArray = jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
991ebd3d54Sopenharmony_ci        std::lock_guard<std::mutex> lock(configMutex_);
1001ebd3d54Sopenharmony_ci        transientTaskExemptedQuatoList_.clear();
1011ebd3d54Sopenharmony_ci        for (const auto &app : appArray) {
1021ebd3d54Sopenharmony_ci            transientTaskExemptedQuatoList_.insert(app);
1031ebd3d54Sopenharmony_ci        }
1041ebd3d54Sopenharmony_ci        for (const auto &appName : transientTaskExemptedQuatoList_) {
1051ebd3d54Sopenharmony_ci            BGTASK_LOGI("transientTaskExemptedQuatoList appName: %{public}s", appName.c_str());
1061ebd3d54Sopenharmony_ci        }
1071ebd3d54Sopenharmony_ci
1081ebd3d54Sopenharmony_ci        if (!jsonObj.contains(TRANSIENT_ERR_DELAYED_FROZEN_TIME) ||
1091ebd3d54Sopenharmony_ci            !jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_TIME].is_number_integer()) {
1101ebd3d54Sopenharmony_ci            BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_TIME.c_str());
1111ebd3d54Sopenharmony_ci            return false;
1121ebd3d54Sopenharmony_ci        }
1131ebd3d54Sopenharmony_ci        transientTaskExemptedQuato_ = jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_TIME].get<int>();
1141ebd3d54Sopenharmony_ci        BGTASK_LOGI("suspend config transientTaskExemptedQuato: %{public}d", transientTaskExemptedQuato_);
1151ebd3d54Sopenharmony_ci    }
1161ebd3d54Sopenharmony_ci    return true;
1171ebd3d54Sopenharmony_ci}
1181ebd3d54Sopenharmony_ci
1191ebd3d54Sopenharmony_cibool BgtaskConfig::SetCloudConfigParam(const nlohmann::json &jsonObj)
1201ebd3d54Sopenharmony_ci{
1211ebd3d54Sopenharmony_ci    if (!jsonObj.contains(CONFIG_JSON_INDEX_TOP) || !jsonObj[CONFIG_JSON_INDEX_TOP].is_object()) {
1221ebd3d54Sopenharmony_ci        BGTASK_LOGE("no key %{public}s", CONFIG_JSON_INDEX_TOP.c_str());
1231ebd3d54Sopenharmony_ci        return false;
1241ebd3d54Sopenharmony_ci    }
1251ebd3d54Sopenharmony_ci    nlohmann::json params = jsonObj[CONFIG_JSON_INDEX_TOP];
1261ebd3d54Sopenharmony_ci
1271ebd3d54Sopenharmony_ci    if (!params.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) ||
1281ebd3d54Sopenharmony_ci        !params[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
1291ebd3d54Sopenharmony_ci        BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
1301ebd3d54Sopenharmony_ci        return false;
1311ebd3d54Sopenharmony_ci    }
1321ebd3d54Sopenharmony_ci    nlohmann::json appArray = params[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
1331ebd3d54Sopenharmony_ci    std::lock_guard<std::mutex> lock(configMutex_);
1341ebd3d54Sopenharmony_ci    transientTaskCloudExemptedQuatoList_.clear();
1351ebd3d54Sopenharmony_ci    for (const auto &app : appArray) {
1361ebd3d54Sopenharmony_ci        transientTaskCloudExemptedQuatoList_.insert(app);
1371ebd3d54Sopenharmony_ci    }
1381ebd3d54Sopenharmony_ci    for (const auto &appName : transientTaskCloudExemptedQuatoList_) {
1391ebd3d54Sopenharmony_ci        BGTASK_LOGI("transientTaskCloudExemptedQuatoList appName: %{public}s", appName.c_str());
1401ebd3d54Sopenharmony_ci    }
1411ebd3d54Sopenharmony_ci
1421ebd3d54Sopenharmony_ci    if (!params.contains(CONFIG_JSON_INDEX_SUSPEND_SECOND) ||
1431ebd3d54Sopenharmony_ci        !params[CONFIG_JSON_INDEX_SUSPEND_SECOND].is_object()) {
1441ebd3d54Sopenharmony_ci        BGTASK_LOGE("no key %{public}s", CONFIG_JSON_INDEX_SUSPEND_SECOND.c_str());
1451ebd3d54Sopenharmony_ci        return false;
1461ebd3d54Sopenharmony_ci    }
1471ebd3d54Sopenharmony_ci    nlohmann::json param = params[CONFIG_JSON_INDEX_SUSPEND_SECOND];
1481ebd3d54Sopenharmony_ci
1491ebd3d54Sopenharmony_ci    if (!param.contains(TRANSIENT_EXEMPTED_QUOTA) ||
1501ebd3d54Sopenharmony_ci        !param[TRANSIENT_EXEMPTED_QUOTA].is_number_integer()) {
1511ebd3d54Sopenharmony_ci        BGTASK_LOGE("no key %{public}s", TRANSIENT_EXEMPTED_QUOTA.c_str());
1521ebd3d54Sopenharmony_ci        return false;
1531ebd3d54Sopenharmony_ci    }
1541ebd3d54Sopenharmony_ci    transientTaskExemptedQuato_ = param[TRANSIENT_EXEMPTED_QUOTA].get<int>();
1551ebd3d54Sopenharmony_ci    BGTASK_LOGI("cloud config transientTaskExemptedQuato: %{public}d", transientTaskExemptedQuato_);
1561ebd3d54Sopenharmony_ci    return true;
1571ebd3d54Sopenharmony_ci}
1581ebd3d54Sopenharmony_ci
1591ebd3d54Sopenharmony_civoid BgtaskConfig::ParseTransientTaskExemptedQuato(const nlohmann::json &jsonObj)
1601ebd3d54Sopenharmony_ci{
1611ebd3d54Sopenharmony_ci    if (jsonObj.is_null() || jsonObj.empty()) {
1621ebd3d54Sopenharmony_ci        BGTASK_LOGE("jsonObj null");
1631ebd3d54Sopenharmony_ci        return;
1641ebd3d54Sopenharmony_ci    }
1651ebd3d54Sopenharmony_ci    if (!jsonObj.contains(TRANSIENT_EXEMPTED_QUOTA) || !jsonObj[TRANSIENT_EXEMPTED_QUOTA].is_number_integer()) {
1661ebd3d54Sopenharmony_ci        BGTASK_LOGE("no key %{public}s", TRANSIENT_EXEMPTED_QUOTA.c_str());
1671ebd3d54Sopenharmony_ci        return;
1681ebd3d54Sopenharmony_ci    }
1691ebd3d54Sopenharmony_ci    std::lock_guard<std::mutex> lock(configMutex_);
1701ebd3d54Sopenharmony_ci    transientTaskExemptedQuato_ = jsonObj[TRANSIENT_EXEMPTED_QUOTA].get<int32_t>();
1711ebd3d54Sopenharmony_ci    BGTASK_LOGI("transientTaskExemptedQuato_ %{public}d", transientTaskExemptedQuato_);
1721ebd3d54Sopenharmony_ci}
1731ebd3d54Sopenharmony_ci
1741ebd3d54Sopenharmony_cibool BgtaskConfig::IsTransientTaskExemptedQuatoApp(const std::string &bundleName)
1751ebd3d54Sopenharmony_ci{
1761ebd3d54Sopenharmony_ci    std::lock_guard<std::mutex> lock(configMutex_);
1771ebd3d54Sopenharmony_ci    if (transientTaskCloudExemptedQuatoList_.size() > 0) {
1781ebd3d54Sopenharmony_ci        return transientTaskCloudExemptedQuatoList_.count(bundleName) > 0;
1791ebd3d54Sopenharmony_ci    }
1801ebd3d54Sopenharmony_ci    return transientTaskExemptedQuatoList_.count(bundleName) > 0;
1811ebd3d54Sopenharmony_ci}
1821ebd3d54Sopenharmony_ci
1831ebd3d54Sopenharmony_ciint32_t BgtaskConfig::GetTransientTaskExemptedQuato()
1841ebd3d54Sopenharmony_ci{
1851ebd3d54Sopenharmony_ci    std::lock_guard<std::mutex> lock(configMutex_);
1861ebd3d54Sopenharmony_ci    return transientTaskExemptedQuato_;
1871ebd3d54Sopenharmony_ci}
1881ebd3d54Sopenharmony_ci}
1891ebd3d54Sopenharmony_ci}