• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "ams_configuration_parameter.h"
17 #include "config_policy_utils.h"
18 #include "hilog_wrapper.h"
19 
20 namespace OHOS {
21 namespace AAFwk {
22 namespace {
23 const int LOAD_CONFIGURATION_FAILED = -1;
24 const int LOAD_CONFIGURATION_SUCCESS = 0;
25 const int32_t TIME_OUT_UNIT_TIME_RATIO = 10;
26 }
27 
AmsConfigurationParameter()28 AmsConfigurationParameter::AmsConfigurationParameter()
29 {
30     std::string deviceType = OHOS::system::GetParameter("const.product.devicetype", "unknown");
31     isPcDevice_ = (deviceType == "tablet" || deviceType == "pc" || deviceType == "2in1");
32 }
33 
GetInstance()34 AmsConfigurationParameter &AmsConfigurationParameter::GetInstance()
35 {
36     static AmsConfigurationParameter amsConfiguration;
37     return amsConfiguration;
38 }
39 
40 using json = nlohmann::json;
41 
Parse()42 void AmsConfigurationParameter::Parse()
43 {
44     auto ref = LoadAmsConfiguration(AmsConfig::AMS_CONFIG_FILE_PATH);
45 
46     char buf[MAX_PATH_LEN] = { 0 };
47     char *filePath = GetOneCfgFile(AmsConfig::PICKER_CONFIG_FILE_PATH, buf, MAX_PATH_LEN);
48     if (filePath == nullptr || filePath[0] == '\0' || strlen(filePath) > MAX_PATH_LEN) {
49         HILOG_ERROR("Can not get config file");
50         LoadUIExtensionPickerConfig(AmsConfig::PICKER_CONFIG_FILE_PATH_DEFAULT);
51     }
52     std::string customConfig = filePath;
53     HILOG_INFO("The configuration file path is :%{private}s", customConfig.c_str());
54     LoadUIExtensionPickerConfig(customConfig);
55     HILOG_INFO("load config ref : %{public}d", ref);
56 }
57 
NonConfigFile() const58 bool AmsConfigurationParameter::NonConfigFile() const
59 {
60     return nonConfigFile_;
61 }
62 
GetMissionSaveTime() const63 int AmsConfigurationParameter::GetMissionSaveTime() const
64 {
65     return missionSaveTime_;
66 }
67 
GetOrientation() const68 std::string AmsConfigurationParameter::GetOrientation() const
69 {
70     return orientation_;
71 }
72 
GetANRTimeOutTime() const73 int AmsConfigurationParameter::GetANRTimeOutTime() const
74 {
75     return anrTime_;
76 }
77 
GetAMSTimeOutTime() const78 int AmsConfigurationParameter::GetAMSTimeOutTime() const
79 {
80     return amsTime_;
81 }
82 
GetMaxRestartNum(bool isRootLauncher) const83 int AmsConfigurationParameter::GetMaxRestartNum(bool isRootLauncher) const
84 {
85     return (isRootLauncher ? maxRootLauncherRestartNum_ : maxResidentRestartNum_);
86 }
87 
GetRestartIntervalTime() const88 int AmsConfigurationParameter::GetRestartIntervalTime() const
89 {
90     return restartIntervalTime_;
91 }
92 
GetDeviceType() const93 std::string AmsConfigurationParameter::GetDeviceType() const
94 {
95     return deviceType_;
96 }
97 
GetBootAnimationTimeoutTime() const98 int AmsConfigurationParameter::GetBootAnimationTimeoutTime() const
99 {
100     return bootAnimationTime_;
101 }
102 
GetAppStartTimeoutTime() const103 int AmsConfigurationParameter::GetAppStartTimeoutTime() const
104 {
105     if (isPcDevice_) {
106         return timeoutUnitTime_ * TIME_OUT_UNIT_TIME_RATIO;
107     }
108     return timeoutUnitTime_;
109 }
110 
SetPickerJsonObject(nlohmann::json Object)111 void AmsConfigurationParameter::SetPickerJsonObject(nlohmann::json Object)
112 {
113     if (Object.contains(AmsConfig::PICKER_CONFIGURATION)) {
114         pickerJsonObject_ = Object.at(AmsConfig::PICKER_CONFIGURATION);
115     }
116 }
117 
GetPickerJsonObject() const118 nlohmann::json AmsConfigurationParameter::GetPickerJsonObject() const
119 {
120     return pickerJsonObject_;
121 }
122 
GetPickerMap() const123 const std::map<std::string, std::string>& AmsConfigurationParameter::GetPickerMap() const
124 {
125     return picker_;
126 }
127 
LoadUIExtensionPickerConfig(const std::string & filePath)128 void AmsConfigurationParameter::LoadUIExtensionPickerConfig(const std::string &filePath)
129 {
130     HILOG_INFO("%{public}s", __func__);
131     std::ifstream inFile;
132     inFile.open(filePath, std::ios::in);
133     if (!inFile.is_open()) {
134         HILOG_ERROR("read picker config error");
135         return;
136     }
137 
138     json pickerJson;
139     inFile >> pickerJson;
140     inFile.close();
141     if (pickerJson.is_discarded()) {
142         HILOG_ERROR("json discarded error");
143         return;
144     }
145 
146     if (pickerJson.is_null() || pickerJson.empty()) {
147         HILOG_ERROR("invalid jsonObj");
148         return;
149     }
150 
151     if (!pickerJson.contains(AmsConfig::UIEATENSION)) {
152         HILOG_ERROR("json config not contains the key");
153         return;
154     }
155 
156     if (pickerJson[AmsConfig::UIEATENSION].is_null()
157         || !pickerJson[AmsConfig::UIEATENSION].is_array()
158         || pickerJson[AmsConfig::UIEATENSION].empty()) {
159         HILOG_ERROR("invalid obj");
160         return;
161     }
162 
163     for (auto extension : pickerJson[AmsConfig::UIEATENSION]) {
164         if (extension[AmsConfig::UIEATENSION_TYPE].is_null()
165             || !extension[AmsConfig::UIEATENSION_TYPE].is_string()
166             || extension[AmsConfig::UIEATENSION_TYPE_PICKER].is_null()
167             || !extension[AmsConfig::UIEATENSION_TYPE_PICKER].is_string()) {
168             HILOG_ERROR("invalid key or value");
169             continue;
170         }
171         std::string type = extension[AmsConfig::UIEATENSION_TYPE].get<std::string>();
172         std::string typePicker = extension[AmsConfig::UIEATENSION_TYPE_PICKER].get<std::string>();
173         HILOG_INFO("type is %{public}s, typePicker is %{public}s", type.c_str(), typePicker.c_str());
174         picker_[type] = typePicker;
175     }
176     pickerJson.clear();
177     HILOG_INFO("read config success");
178 }
179 
LoadAmsConfiguration(const std::string & filePath)180 int AmsConfigurationParameter::LoadAmsConfiguration(const std::string &filePath)
181 {
182     HILOG_DEBUG("%{public}s", __func__);
183     int ret[2] = {0};
184     std::ifstream inFile;
185     inFile.open(filePath, std::ios::in);
186     if (!inFile.is_open()) {
187         HILOG_INFO("read ams config error ...");
188         nonConfigFile_ = true;
189         return READ_FAIL;
190     }
191 
192     json amsJson;
193     inFile >> amsJson;
194     if (amsJson.is_discarded()) {
195         HILOG_INFO("json discarded error ...");
196         nonConfigFile_ = true;
197         inFile.close();
198         return READ_JSON_FAIL;
199     }
200 
201     ret[0] = LoadAppConfigurationForStartUpService(amsJson);
202     if (ret[0] != 0) {
203         HILOG_ERROR("LoadAppConfigurationForStartUpService return error");
204     }
205 
206     ret[1] = LoadAppConfigurationForMemoryThreshold(amsJson);
207     if (ret[1] != 0) {
208         HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error");
209     }
210 
211     LoadSystemConfiguration(amsJson);
212     SetPickerJsonObject(amsJson);
213     amsJson.clear();
214     inFile.close();
215 
216     for (const auto& i : ret) {
217         if (i != 0) {
218             HILOG_ERROR("json no have service item ...");
219             return READ_JSON_FAIL;
220         }
221     }
222 
223     HILOG_INFO("read ams config success!");
224     return READ_OK;
225 }
226 
LoadAppConfigurationForStartUpService(nlohmann::json & Object)227 int AmsConfigurationParameter::LoadAppConfigurationForStartUpService(nlohmann::json& Object)
228 {
229     if (!Object.contains(AmsConfig::SERVICE_ITEM_AMS)) {
230         return LOAD_CONFIGURATION_FAILED;
231     }
232     UpdateStartUpServiceConfigInteger(Object, AmsConfig::MISSION_SAVE_TIME, missionSaveTime_);
233     UpdateStartUpServiceConfigInteger(Object, AmsConfig::APP_NOT_RESPONSE_PROCESS_TIMEOUT_TIME, anrTime_);
234     UpdateStartUpServiceConfigInteger(Object, AmsConfig::AMS_TIMEOUT_TIME, amsTime_);
235     UpdateStartUpServiceConfigInteger(Object, AmsConfig::ROOT_LAUNCHER_RESTART_MAX, maxRootLauncherRestartNum_);
236     UpdateStartUpServiceConfigInteger(Object, AmsConfig::RESIDENT_RESTART_MAX, maxResidentRestartNum_);
237     UpdateStartUpServiceConfigInteger(Object, AmsConfig::RESTART_INTERVAL_TIME, restartIntervalTime_);
238     UpdateStartUpServiceConfigString(Object, AmsConfig::DEVICE_TYPE, deviceType_);
239     UpdateStartUpServiceConfigInteger(Object, AmsConfig::BOOT_ANIMATION_TIMEOUT_TIME, bootAnimationTime_);
240     UpdateStartUpServiceConfigInteger(Object, AmsConfig::TIMEOUT_UNIT_TIME, timeoutUnitTime_);
241     return LOAD_CONFIGURATION_SUCCESS;
242 }
243 
LoadAppConfigurationForMemoryThreshold(nlohmann::json & Object)244 int AmsConfigurationParameter::LoadAppConfigurationForMemoryThreshold(nlohmann::json &Object)
245 {
246     int ret = 0;
247     if (!Object.contains("memorythreshold")) {
248         HILOG_ERROR("LoadAppConfigurationForMemoryThreshold return error");
249         ret = -1;
250     }
251 
252     return ret;
253 }
254 
LoadSystemConfiguration(nlohmann::json & Object)255 int AmsConfigurationParameter::LoadSystemConfiguration(nlohmann::json& Object)
256 {
257     if (Object.contains(AmsConfig::SYSTEM_CONFIGURATION) &&
258         Object.at(AmsConfig::SYSTEM_CONFIGURATION).contains(AmsConfig::SYSTEM_ORIENTATION) &&
259         Object.at(AmsConfig::SYSTEM_CONFIGURATION).at(AmsConfig::SYSTEM_ORIENTATION).is_string()) {
260         orientation_ = Object.at(AmsConfig::SYSTEM_CONFIGURATION).at(AmsConfig::SYSTEM_ORIENTATION).get<std::string>();
261         return READ_OK;
262     }
263 
264     return READ_FAIL;
265 }
266 
CheckServiceConfigEnable(nlohmann::json & Object,const std::string & configName,JsonValueType type)267 bool AmsConfigurationParameter::CheckServiceConfigEnable(nlohmann::json& Object, const std::string &configName,
268     JsonValueType type)
269 {
270     if (Object.contains(AmsConfig::SERVICE_ITEM_AMS) &&
271         Object.at(AmsConfig::SERVICE_ITEM_AMS).contains(configName)) {
272         switch (type) {
273             case JsonValueType::NUMBER: {
274                 return Object.at(AmsConfig::SERVICE_ITEM_AMS).at(configName).is_number();
275             }
276             case JsonValueType::STRING: {
277                 return Object.at(AmsConfig::SERVICE_ITEM_AMS).at(configName).is_string();
278             }
279             case JsonValueType::BOOLEAN: {
280                 return Object.at(AmsConfig::SERVICE_ITEM_AMS).at(configName).is_boolean();
281             }
282             default: {
283                 return false;
284             }
285         }
286     }
287     return false;
288 }
289 
UpdateStartUpServiceConfigInteger(nlohmann::json & Object,const std::string & configName,int32_t & value)290 void AmsConfigurationParameter::UpdateStartUpServiceConfigInteger(nlohmann::json& Object,
291     const std::string &configName, int32_t &value)
292 {
293     if (CheckServiceConfigEnable(Object, configName, JsonValueType::NUMBER)) {
294         value = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(configName).get<int>();
295     }
296 }
297 
UpdateStartUpServiceConfigString(nlohmann::json & Object,const std::string & configName,std::string & value)298 void AmsConfigurationParameter::UpdateStartUpServiceConfigString(nlohmann::json& Object,
299     const std::string &configName, std::string &value)
300 {
301     if (CheckServiceConfigEnable(Object, configName, JsonValueType::STRING)) {
302         value = Object.at(AmsConfig::SERVICE_ITEM_AMS).at(configName).get<std::string>();
303     }
304 }
305 }  // namespace AAFwk
306 }  // namespace OHOS
307