• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 CONTINUOUS_TASK_KEEPING_EXEMPTED_LIST = "continuous_task_keeping_exemption_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     std::lock_guard<std::mutex> lock(configMutex_);
127     if (params.contains(TRANSIENT_ERR_DELAYED_FROZEN_LIST) &&
128         params[TRANSIENT_ERR_DELAYED_FROZEN_LIST].is_array()) {
129         nlohmann::json appArray = params[TRANSIENT_ERR_DELAYED_FROZEN_LIST];
130         transientTaskCloudExemptedQuatoList_.clear();
131         for (const auto &app : appArray) {
132             transientTaskCloudExemptedQuatoList_.insert(app);
133         }
134         for (const auto &appName : transientTaskCloudExemptedQuatoList_) {
135             BGTASK_LOGI("transientTaskCloudExemptedQuatoList appName: %{public}s", appName.c_str());
136         }
137     } else {
138         BGTASK_LOGE("no key %{public}s", TRANSIENT_ERR_DELAYED_FROZEN_LIST.c_str());
139     }
140     if (params.contains(CONTINUOUS_TASK_KEEPING_EXEMPTED_LIST) &&
141         params[CONTINUOUS_TASK_KEEPING_EXEMPTED_LIST].is_array()) {
142         nlohmann::json appArrayTaskKeeping = params[CONTINUOUS_TASK_KEEPING_EXEMPTED_LIST];
143         taskKeepingExemptedQuatoList_.clear();
144         for (const auto &app : appArrayTaskKeeping) {
145             taskKeepingExemptedQuatoList_.insert(app);
146         }
147         for (const auto &appName : taskKeepingExemptedQuatoList_) {
148             BGTASK_LOGI("taskKeepingExemptedQuatoList_ appName: %{public}s", appName.c_str());
149         }
150     } else {
151         BGTASK_LOGE("no key %{public}s", CONTINUOUS_TASK_KEEPING_EXEMPTED_LIST.c_str());
152     }
153     if (params.contains(CONFIG_JSON_INDEX_SUSPEND_SECOND) &&
154         params[CONFIG_JSON_INDEX_SUSPEND_SECOND].is_object()) {
155         nlohmann::json param = params[CONFIG_JSON_INDEX_SUSPEND_SECOND];
156         if (param.contains(TRANSIENT_EXEMPTED_QUOTA) &&
157             !param[TRANSIENT_EXEMPTED_QUOTA].is_number_integer()) {
158             transientTaskExemptedQuato_ = param[TRANSIENT_EXEMPTED_QUOTA].get<int>();
159             BGTASK_LOGI("cloud config transientTaskExemptedQuato: %{public}d", transientTaskExemptedQuato_);
160         } else {
161             BGTASK_LOGE("no key %{public}s", TRANSIENT_EXEMPTED_QUOTA.c_str());
162         }
163     } else {
164         BGTASK_LOGE("no key %{public}s", CONFIG_JSON_INDEX_SUSPEND_SECOND.c_str());
165     }
166     return true;
167 }
168 
ParseTransientTaskExemptedQuato(const nlohmann::json & jsonObj)169 void BgtaskConfig::ParseTransientTaskExemptedQuato(const nlohmann::json &jsonObj)
170 {
171     if (jsonObj.is_null() || jsonObj.empty()) {
172         BGTASK_LOGE("jsonObj null");
173         return;
174     }
175     if (!jsonObj.contains(TRANSIENT_EXEMPTED_QUOTA) || !jsonObj[TRANSIENT_EXEMPTED_QUOTA].is_number_integer()) {
176         BGTASK_LOGE("no key %{public}s", TRANSIENT_EXEMPTED_QUOTA.c_str());
177         return;
178     }
179     std::lock_guard<std::mutex> lock(configMutex_);
180     transientTaskExemptedQuato_ = jsonObj[TRANSIENT_EXEMPTED_QUOTA].get<int32_t>();
181     BGTASK_LOGI("transientTaskExemptedQuato_ %{public}d", transientTaskExemptedQuato_);
182 }
183 
IsTransientTaskExemptedQuatoApp(const std::string & bundleName)184 bool BgtaskConfig::IsTransientTaskExemptedQuatoApp(const std::string &bundleName)
185 {
186     std::lock_guard<std::mutex> lock(configMutex_);
187     if (transientTaskCloudExemptedQuatoList_.size() > 0) {
188         return transientTaskCloudExemptedQuatoList_.count(bundleName) > 0;
189     }
190     return transientTaskExemptedQuatoList_.count(bundleName) > 0;
191 }
192 
IsTaskKeepingExemptedQuatoApp(const std::string & bundleName)193 bool BgtaskConfig::IsTaskKeepingExemptedQuatoApp(const std::string &bundleName)
194 {
195     std::lock_guard<std::mutex> lock(configMutex_);
196     return taskKeepingExemptedQuatoList_.count(bundleName) > 0;
197 }
198 
GetTransientTaskExemptedQuato()199 int32_t BgtaskConfig::GetTransientTaskExemptedQuato()
200 {
201     std::lock_guard<std::mutex> lock(configMutex_);
202     return transientTaskExemptedQuato_;
203 }
204 }
205 }