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 #include "parameters.h"
20
21 namespace OHOS {
22 namespace BackgroundTaskMgr {
23 namespace {
24 const std::string SUSPEND_MANAGER_CONFIG_FILE = "etc/efficiency_manager/suspend_manager_config.json";
25 const std::string CONFIG_JSON_INDEX_TOP = "params";
26 const std::string CONFIG_JSON_INDEX_SUSPEND_SECOND = "param";
27 const std::string TRANSIENT_ERR_DELAYED_FROZEN_LIST = "transient_err_delayed_frozen_list";
28 const std::string TRANSIENT_EXEMPTED_QUOTA = "transient_exempted_quota";
29 const std::string TRANSIENT_ERR_DELAYED_FROZEN_TIME = "transient_err_delayed_frozen_time";
30 }
31
Init()32 void BgtaskConfig::Init()
33 {
34 if (isInit_) {
35 BGTASK_LOGE("already init config!");
36 return;
37 }
38
39 LoadConfigFile();
40 isInit_ = true;
41 }
42
LoadConfigFile()43 void BgtaskConfig::LoadConfigFile()
44 {
45 nlohmann::json jsonObj;
46 std::string absolutePath = DelayedSingleton<DataStorageHelper>::GetInstance()->
47 GetConfigFileAbsolutePath(SUSPEND_MANAGER_CONFIG_FILE);
48 if (DelayedSingleton<DataStorageHelper>::GetInstance()->ParseJsonValueFromFile(jsonObj, absolutePath) != 0) {
49 BGTASK_LOGE("LoadConfigFile failed");
50 return;
51 }
52 ParseTransientTaskExemptedQuatoList(jsonObj);
53 ParseTransientTaskExemptedQuato(jsonObj);
54 }
55
ParseTransientTaskExemptedQuatoList(const nlohmann::json & jsonObj)56 void BgtaskConfig::ParseTransientTaskExemptedQuatoList(const nlohmann::json &jsonObj)
57 {
58 nlohmann::json appArray;
59 if (jsonObj.is_null() || jsonObj.empty()) {
60 BGTASK_LOGE("jsonObj null");
61 return;
62 }
63 if (!jsonObj.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) ||
64 !jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
65 BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
66 return;
67 }
68 appArray = jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
69 std::lock_guard<std::mutex> lock(configMutex_);
70 for (const auto &app : appArray) {
71 transientTaskExemptedQuatoList_.insert(app);
72 }
73 for (const auto &app : transientTaskExemptedQuatoList_) {
74 BGTASK_LOGI("ParseTransientTaskExemptedQuatoList: %{public}s.", app.c_str());
75 }
76 }
77
AddExemptedQuatoData(const std::string & configData,int32_t sourceType)78 bool BgtaskConfig::AddExemptedQuatoData(const std::string &configData, int32_t sourceType)
79 {
80 const nlohmann::json &jsonObj = nlohmann::json::parse(configData, nullptr, false);
81 if (jsonObj.is_discarded()) {
82 BGTASK_LOGE("jsonObj parse fail");
83 return false;
84 }
85 if (jsonObj.is_null() || jsonObj.empty()) {
86 BGTASK_LOGE("jsonObj null");
87 return false;
88 }
89 if (sourceType == ConfigDataSourceType::CONFIG_CLOUD && !SetCloudConfigParam(jsonObj)) {
90 return false;
91 } else if (sourceType == ConfigDataSourceType::CONFIG_SUSPEND_MANAGER) {
92 nlohmann::json appArray;
93 if (!jsonObj.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) ||
94 !jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
95 BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
96 return false;
97 }
98 appArray = jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
99 std::lock_guard<std::mutex> lock(configMutex_);
100 transientTaskExemptedQuatoList_.clear();
101 for (const auto &app : appArray) {
102 transientTaskExemptedQuatoList_.insert(app);
103 }
104 for (const auto &appName : transientTaskExemptedQuatoList_) {
105 BGTASK_LOGI("transientTaskExemptedQuatoList appName: %{public}s", appName.c_str());
106 }
107
108 if (!jsonObj.contains(TRANSIENT_ERR_DELAYED_FROZEN_TIME) ||
109 !jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_TIME].is_number_integer()) {
110 BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_TIME.c_str());
111 return false;
112 }
113 transientTaskExemptedQuato_ = jsonObj[TRANSIENT_ERR_DELAYED_FROZEN_TIME].get<int>();
114 BGTASK_LOGI("suspend config transientTaskExemptedQuato: %{public}d", transientTaskExemptedQuato_);
115 }
116 return true;
117 }
118
SetCloudConfigParam(const nlohmann::json & jsonObj)119 bool BgtaskConfig::SetCloudConfigParam(const nlohmann::json &jsonObj)
120 {
121 if (!jsonObj.contains(CONFIG_JSON_INDEX_TOP) || !jsonObj[CONFIG_JSON_INDEX_TOP].is_object()) {
122 BGTASK_LOGE("no key %{public}s", CONFIG_JSON_INDEX_TOP.c_str());
123 return false;
124 }
125 nlohmann::json params = jsonObj[CONFIG_JSON_INDEX_TOP];
126
127 if (!params.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) ||
128 !params[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
129 BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
130 return false;
131 }
132 nlohmann::json appArray = params[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
133 std::lock_guard<std::mutex> lock(configMutex_);
134 transientTaskCloudExemptedQuatoList_.clear();
135 for (const auto &app : appArray) {
136 transientTaskCloudExemptedQuatoList_.insert(app);
137 }
138 for (const auto &appName : transientTaskCloudExemptedQuatoList_) {
139 BGTASK_LOGI("transientTaskCloudExemptedQuatoList appName: %{public}s", appName.c_str());
140 }
141
142 if (!params.contains(CONFIG_JSON_INDEX_SUSPEND_SECOND) ||
143 !params[CONFIG_JSON_INDEX_SUSPEND_SECOND].is_object()) {
144 BGTASK_LOGE("no key %{public}s", CONFIG_JSON_INDEX_SUSPEND_SECOND.c_str());
145 return false;
146 }
147 nlohmann::json param = params[CONFIG_JSON_INDEX_SUSPEND_SECOND];
148
149 if (!param.contains(TRANSIENT_EXEMPTED_QUOTA) ||
150 !param[TRANSIENT_EXEMPTED_QUOTA].is_number_integer()) {
151 BGTASK_LOGE("no key %{public}s", TRANSIENT_EXEMPTED_QUOTA.c_str());
152 return false;
153 }
154 transientTaskExemptedQuato_ = param[TRANSIENT_EXEMPTED_QUOTA].get<int>();
155 BGTASK_LOGI("cloud config transientTaskExemptedQuato: %{public}d", transientTaskExemptedQuato_);
156 return true;
157 }
158
ParseTransientTaskExemptedQuato(const nlohmann::json & jsonObj)159 void BgtaskConfig::ParseTransientTaskExemptedQuato(const nlohmann::json &jsonObj)
160 {
161 if (jsonObj.is_null() || jsonObj.empty()) {
162 BGTASK_LOGE("jsonObj null");
163 return;
164 }
165 if (!jsonObj.contains(TRANSIENT_EXEMPTED_QUOTA) || !jsonObj[TRANSIENT_EXEMPTED_QUOTA].is_number_integer()) {
166 BGTASK_LOGE("no key %{public}s", TRANSIENT_EXEMPTED_QUOTA.c_str());
167 return;
168 }
169 std::lock_guard<std::mutex> lock(configMutex_);
170 transientTaskExemptedQuato_ = jsonObj[TRANSIENT_EXEMPTED_QUOTA].get<int32_t>();
171 BGTASK_LOGI("transientTaskExemptedQuato_ %{public}d", transientTaskExemptedQuato_);
172 }
173
IsTransientTaskExemptedQuatoApp(const std::string & bundleName)174 bool BgtaskConfig::IsTransientTaskExemptedQuatoApp(const std::string &bundleName)
175 {
176 std::lock_guard<std::mutex> lock(configMutex_);
177 if (transientTaskCloudExemptedQuatoList_.size() > 0) {
178 return transientTaskCloudExemptedQuatoList_.count(bundleName) > 0;
179 }
180 return transientTaskExemptedQuatoList_.count(bundleName) > 0;
181 }
182
GetTransientTaskExemptedQuato()183 int32_t BgtaskConfig::GetTransientTaskExemptedQuato()
184 {
185 std::lock_guard<std::mutex> lock(configMutex_);
186 return transientTaskExemptedQuato_;
187 }
188 }
189 }
190