1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bgtask_config.h"
17 #include "data_storage_helper.h"
18 #include "bgtaskmgr_log_wrapper.h"
19
20 namespace OHOS {
21 namespace BackgroundTaskMgr {
22 namespace {
23 const std::string SUSPEND_MANAGER_CONFIG_FILE = "etc/efficiency_manager/suspend_manager_config.json";
24 const std::string CONFIG_JSON_INDEX_TOP = "params";
25 const std::string CONFIG_JSON_INDEX_SUSPEND_SECOND = "param";
26 const std::string TRANSIENT_ERR_DELAYED_FROZEN_LIST = "transient_err_delayed_frozen_list";
27 const std::string TRANSIENT_EXEMPTED_QUOTA = "transient_exempted_quota";
28 const std::string TRANSIENT_ERR_DELAYED_FROZEN_TIME = "transient_err_delayed_frozen_time";
29 }
30
Init()31 void BgtaskConfig::Init()
32 {
33 if (isInit_) {
34 BGTASK_LOGE("already init config!");
35 return;
36 }
37
38 LoadConfigFile();
39 isInit_ = true;
40 }
41
LoadConfigFile()42 void BgtaskConfig::LoadConfigFile()
43 {
44 nlohmann::json jsonObj;
45 std::string absolutePath = DelayedSingleton<DataStorageHelper>::GetInstance()->
46 GetConfigFileAbsolutePath(SUSPEND_MANAGER_CONFIG_FILE);
47 if (DelayedSingleton<DataStorageHelper>::GetInstance()->ParseJsonValueFromFile(jsonObj, absolutePath) != 0) {
48 BGTASK_LOGE("LoadConfigFile failed");
49 return;
50 }
51 ParseTransientTaskExemptedQuatoList(jsonObj);
52 ParseTransientTaskExemptedQuato(jsonObj);
53 }
54
ParseTransientTaskExemptedQuatoList(const nlohmann::json & jsonObj)55 void BgtaskConfig::ParseTransientTaskExemptedQuatoList(const nlohmann::json &jsonObj)
56 {
57 nlohmann::json appArray;
58 if (jsonObj.is_null() || jsonObj.empty()) {
59 BGTASK_LOGE("jsonObj null");
60 return;
61 }
62 if (!jsonObj.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) ||
63 !jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
64 BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
65 return;
66 }
67 appArray = jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
68 std::lock_guard<std::mutex> lock(configMutex_);
69 for (const auto &app : appArray) {
70 transientTaskExemptedQuatoList_.insert(app);
71 }
72 for (const auto &app : transientTaskExemptedQuatoList_) {
73 BGTASK_LOGI("ParseTransientTaskExemptedQuatoList: %{public}s.", app.c_str());
74 }
75 }
76
AddExemptedQuatoData(const std::string & configData,int32_t sourceType)77 bool BgtaskConfig::AddExemptedQuatoData(const std::string &configData, int32_t sourceType)
78 {
79 const nlohmann::json &jsonObj = nlohmann::json::parse(configData, nullptr, false);
80 if (jsonObj.is_discarded()) {
81 BGTASK_LOGE("jsonObj parse fail");
82 return false;
83 }
84 if (jsonObj.is_null() || jsonObj.empty()) {
85 BGTASK_LOGE("jsonObj null");
86 return false;
87 }
88 if (sourceType == ConfigDataSourceType::CONFIG_CLOUD && !SetCloudConfigParam(jsonObj)) {
89 return false;
90 } else if (sourceType == ConfigDataSourceType::CONFIG_SUSPEND_MANAGER) {
91 nlohmann::json appArray;
92 if (!jsonObj.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) ||
93 !jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
94 BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
95 return false;
96 }
97 appArray = jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
98 std::lock_guard<std::mutex> lock(configMutex_);
99 transientTaskExemptedQuatoList_.clear();
100 for (const auto &app : appArray) {
101 transientTaskExemptedQuatoList_.insert(app);
102 }
103 for (const auto &appName : transientTaskExemptedQuatoList_) {
104 BGTASK_LOGI("transientTaskExemptedQuatoList appName: %{public}s", appName.c_str());
105 }
106
107 if (!jsonObj.contains(TRANSIENT_ERR_DELAYED_FROZEN_TIME) ||
108 !jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_TIME].is_number_integer()) {
109 BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_TIME.c_str());
110 return false;
111 }
112 transientTaskExemptedQuato_ = jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_TIME].get<int>();
113 BGTASK_LOGI("suspend config transientTaskExemptedQuato: %{public}d", transientTaskExemptedQuato_);
114 }
115 return true;
116 }
117
SetCloudConfigParam(const nlohmann::json & jsonObj)118 bool BgtaskConfig::SetCloudConfigParam(const nlohmann::json &jsonObj)
119 {
120 if (!jsonObj.contains(CONFIG_JSON_INDEX_TOP) || !jsonObj[CONFIG_JSON_INDEX_TOP].is_object()) {
121 BGTASK_LOGE("no key %{public}s", CONFIG_JSON_INDEX_TOP.c_str());
122 return false;
123 }
124 nlohmann::json params = jsonObj[CONFIG_JSON_INDEX_TOP];
125
126 if (!params.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) ||
127 !params[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
128 BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
129 return false;
130 }
131 nlohmann::json appArray = params[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
132 std::lock_guard<std::mutex> lock(configMutex_);
133 transientTaskCloudExemptedQuatoList_.clear();
134 for (const auto &app : appArray) {
135 transientTaskCloudExemptedQuatoList_.insert(app);
136 }
137 for (const auto &appName : transientTaskCloudExemptedQuatoList_) {
138 BGTASK_LOGI("transientTaskCloudExemptedQuatoList appName: %{public}s", appName.c_str());
139 }
140
141 if (!params.contains(CONFIG_JSON_INDEX_SUSPEND_SECOND) ||
142 !params[CONFIG_JSON_INDEX_SUSPEND_SECOND].is_object()) {
143 BGTASK_LOGE("no key %{public}s", CONFIG_JSON_INDEX_SUSPEND_SECOND.c_str());
144 return false;
145 }
146 nlohmann::json param = params[CONFIG_JSON_INDEX_SUSPEND_SECOND];
147
148 if (!param.contains(TRANSIENT_EXEMPTED_QUOTA) ||
149 !param[TRANSIENT_EXEMPTED_QUOTA].is_number_integer()) {
150 BGTASK_LOGE("no key %{public}s", TRANSIENT_EXEMPTED_QUOTA.c_str());
151 return false;
152 }
153 transientTaskExemptedQuato_ = param[TRANSIENT_EXEMPTED_QUOTA].get<int>();
154 BGTASK_LOGI("cloud config transientTaskExemptedQuato: %{public}d", transientTaskExemptedQuato_);
155 return true;
156 }
157
ParseTransientTaskExemptedQuato(const nlohmann::json & jsonObj)158 void BgtaskConfig::ParseTransientTaskExemptedQuato(const nlohmann::json &jsonObj)
159 {
160 if (jsonObj.is_null() || jsonObj.empty()) {
161 BGTASK_LOGE("jsonObj null");
162 return;
163 }
164 if (!jsonObj.contains(TRANSIENT_EXEMPTED_QUOTA) || !jsonObj[TRANSIENT_EXEMPTED_QUOTA].is_number_integer()) {
165 BGTASK_LOGE("no key %{public}s", TRANSIENT_EXEMPTED_QUOTA.c_str());
166 return;
167 }
168 std::lock_guard<std::mutex> lock(configMutex_);
169 transientTaskExemptedQuato_ = jsonObj[TRANSIENT_EXEMPTED_QUOTA].get<int32_t>();
170 BGTASK_LOGI("transientTaskExemptedQuato_ %{public}d", transientTaskExemptedQuato_);
171 }
172
IsTransientTaskExemptedQuatoApp(const std::string & bundleName)173 bool BgtaskConfig::IsTransientTaskExemptedQuatoApp(const std::string &bundleName)
174 {
175 std::lock_guard<std::mutex> lock(configMutex_);
176 if (transientTaskCloudExemptedQuatoList_.size() > 0) {
177 return transientTaskCloudExemptedQuatoList_.count(bundleName) > 0;
178 }
179 return transientTaskExemptedQuatoList_.count(bundleName) > 0;
180 }
181
GetTransientTaskExemptedQuato()182 int32_t BgtaskConfig::GetTransientTaskExemptedQuato()
183 {
184 std::lock_guard<std::mutex> lock(configMutex_);
185 return transientTaskExemptedQuato_;
186 }
187 }
188 }