1 /*
2 * Copyright (c) 2023-2025 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 "app_utils.h"
17 #include <unordered_set>
18 #include "json_utils.h"
19 #include "hilog_tag_wrapper.h"
20 #include "nlohmann/json.hpp"
21 #include "parameter.h"
22 #include "parameters.h"
23 #ifdef SUPPORT_GRAPHICS
24 #include "scene_board_judgement.h"
25 #endif // SUPPORT_GRAPHICS
26
27
28 namespace OHOS {
29 namespace AAFwk {
30 namespace {
31 constexpr const char* BUNDLE_NAME_LAUNCHER = "com.ohos.launcher";
32 constexpr const char* BUNDLE_NAME_SCENEBOARD = "com.ohos.sceneboard";
33 constexpr const char* LAUNCHER_ABILITY_NAME = "com.ohos.launcher.MainAbility";
34 constexpr const char* SCENEBOARD_ABILITY_NAME = "com.ohos.sceneboard.MainAbility";
35 constexpr const char* INHERIT_WINDOW_SPLIT_SCREEN_MODE = "persist.sys.abilityms.inherit_window_split_screen_mode";
36 constexpr const char* SUPPORT_ANCO_APP = "persist.sys.abilityms.support_anco_app";
37 constexpr const char* TIMEOUT_UNIT_TIME_RATIO = "persist.sys.abilityms.timeout_unit_time_ratio";
38 constexpr const char* SELECTOR_DIALOG_POSSION = "persist.sys.abilityms.selector_dialog_possion";
39 constexpr const char* START_SPECIFIED_PROCESS = "persist.sys.abilityms.start_specified_process";
40 constexpr const char* USE_MULTI_RENDER_PROCESS = "persist.sys.abilityms.use_multi_render_process";
41 constexpr const char* LIMIT_MAXIMUM_OF_RENDER_PROCESS = "persist.sys.abilityms.limit_maximum_of_render_process";
42 constexpr const char* GRANT_PERSIST_URI_PERMISSION = "persist.sys.abilityms.grant_persist_uri_permission";
43 constexpr const char* GRANT_TEMPORARY_URI_PERMISSION = "persist.sys.abilityms.grant_temporary_uri_permission";
44 constexpr const char* START_OPTIONS_WITH_ANIMATION = "persist.sys.abilityms.start_options_with_animation";
45 constexpr const char* SUPPORT_START_ABILITIES = "persist.sys.abilityms.enable_start_abilities";
46 constexpr const char* MULTI_PROCESS_MODEL = "persist.sys.abilityms.multi_process_model";
47 constexpr const char* PARAM_ANCO_APP_IDENTIFIER = "persist.hmos_fusion_mgr.anco_identifier";
48 constexpr const char* ALLOW_CHILD_PROCESS_IN_MULTI_PROCESS_FEATURE_APP =
49 "const.sys.abilityms.allow_child_process_in_multi_process_feature_app";
50 constexpr const char* MAX_MULTI_PROCESS_FEATURE_CHILD_PROCESS =
51 "const.sys.abilityms.max_multi_process_feature_child_process";
52 constexpr const char* START_OPTIONS_WITH_PROCESS_OPTION = "persist.sys.abilityms.start_options_with_process_option";
53 constexpr const char* MOVE_UI_ABILITY_TO_BACKGROUND_API_ENABLE =
54 "persist.sys.abilityms.move_ui_ability_to_background_api_enable";
55 constexpr const char* CONFIG_PATH = "/etc/ability_runtime/resident_process_in_extreme_memory.json";
56 constexpr const char* RESIDENT_PROCESS_IN_EXTREME_MEMORY = "residentProcessInExtremeMemory";
57 constexpr const char* PROCESS_PROHIBITED_FROM_RESTARTING = "processProhibitedFromRestarting";
58 constexpr const char* BUNDLE_NAME = "bundleName";
59 constexpr const char* REQUIRE_BIGMEMORY_APP = "requireBigMemoryApp";
60 constexpr const char* ABILITY_NAME = "abilityName";
61 constexpr const char* KEY_IDENTIFIER = "identifier";
62 constexpr const char* ALLOW_NATIVE_CHILD_PROCESS_APPS_CONFIG_PATH =
63 "/etc/ability_runtime/allow_native_child_process_apps.json";
64 constexpr const char* KEY_ALLOW_NATIVE_CHILD_PROCESS_APPS = "allowNativeChildProcessApps";
65 constexpr const char* LAUNCH_EMBEDED_UI_ABILITY = "const.abilityms.launch_embeded_ui_ability";
66 const std::string SUPPROT_NATIVE_CHILD_PROCESS = "persist.sys.abilityms.start_native_child_process";
67 const std::string LIMIT_MAXIMUM_EXTENSIONS_OF_PER_PROCESS =
68 "const.sys.abilityms.limit_maximum_extensions_of_per_process";
69 const std::string LIMIT_MAXIMUM_EXTENSIONS_OF_PER_DEVICE =
70 "const.sys.abilityms.limit_maximum_extensions_of_per_device";
71 const std::string CACHE_EXTENSION_TYPES = "const.sys.abilityms.cache_extension";
72 constexpr const char* START_ABILITY_WITHOUT_CALLERTOKEN = "/system/etc/start_ability_without_caller_token.json";
73 constexpr const char* START_ABILITY_WITHOUT_CALLERTOKEN_PATH =
74 "/etc/ability_runtime/start_ability_without_caller_token.json";
75 constexpr const char* START_ABILITY_WITHOUT_CALLERTOKEN_TITLE = "startAbilityWithoutCallerToken";
76 constexpr const char* BROKER_DELEGATE_BUNDLE_NAME = "const.sys.abilityms.broker_delegate_bundle_name";
77 constexpr const char* COLLABORATOR_BROKER_UID = "const.sys.abilityms.collaborator_broker_uid";
78 constexpr const char* COLLABORATOR_BROKER_RESERVE_UID = "const.sys.abilityms.collaborator_broker_reserve_uid";
79 constexpr const char* MAX_CHILD_PROCESS = "const.max_native_child_process";
80 constexpr const char* SUPPORT_MULTI_INSTANCE = "const.abilityms.support_multi_instance";
81 constexpr const char* MIGRATE_CLIENT_BUNDLE_NAME = "const.sys.abilityms.migrate_client_bundle_name";
82 constexpr const char* CONNECT_SUPPORT_CROSS_USER = "const.abilityms.connect_support_cross_user";
83 constexpr const char* SUPPORT_APP_SERVICE_EXTENSION = "const.abilityms.support_app_service";
84 constexpr const char* PRODUCT_PRELOAD_APPLICATION_SETTING_ENABLED = "const.product.preload_application.setting.enabled";
85 constexpr const char* FORBID_START = "persist.sys.abilityms.forbid_start";
86 // Support prepare terminate
87 constexpr int32_t PREPARE_TERMINATE_ENABLE_SIZE = 6;
88 constexpr const char* PREPARE_TERMINATE_ENABLE_PARAMETER = "persist.sys.prepare_terminate";
89 constexpr const char* CACHE_ABILITY_BY_LIST_ENABLE = "persist.sys.abilityms.cache_ability_enable";
90 constexpr const char* CACHE_ABILITY_LIST_PATH = "etc/ability/abilityms_cache_ability.json";
91 constexpr const char* CACHE_PROCESS_NAME = "cache_list";
92 constexpr const char* RESIDENT_WHITE_LIST_PATH = "etc/ability_runtime/resident_process.json";
93 constexpr const char* NORMAL_RESIDENT_APPS = "normal_resident_apps";
94 constexpr const char* ON_NEW_PROCESS_ENABLE_LIST_PATH = "etc/ability_runtime/on_new_process_enable_list.json";
95 constexpr const char* ON_NEW_PROCESS_ENABLE_LIST = "onNewProcessEnableList";
96 }
97
~AppUtils()98 AppUtils::~AppUtils() {}
99
AppUtils()100 AppUtils::AppUtils()
101 {
102 #ifdef SUPPORT_GRAPHICS
103 if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
104 isSceneBoard_ = true;
105 }
106 #endif // SUPPORT_GRAPHICS
107 #ifndef ABILITY_RUNTIME_MEDIA_LIBRARY_ENABLE
108 if (WatchParameter(FORBID_START, AppUtils::ForbidStartCallback, nullptr) != 0) {
109 TAG_LOGI(AAFwkTag::ABILITYMGR, "want param failed: %{public}s", FORBID_START);
110 isForbidStart_.isLoaded = false;
111 }
112 #endif
113 }
114
GetInstance()115 AppUtils &AppUtils::GetInstance()
116 {
117 static AppUtils utils;
118 return utils;
119 }
120
ForbidStartCallback(const char * key,const char *,void *)121 void AppUtils::ForbidStartCallback(const char *key, const char *, void *)
122 {
123 if (strcmp(key, FORBID_START) == 0) {
124 GetInstance().isForbidStart_.value = system::GetBoolParameter(FORBID_START, false);
125 TAG_LOGI(AAFwkTag::ABILITYMGR, "param %{public}s changed: %{public}d", FORBID_START,
126 GetInstance().isForbidStart_.value);
127 }
128 }
129
IsLauncher(const std::string & bundleName) const130 bool AppUtils::IsLauncher(const std::string &bundleName) const
131 {
132 if (isSceneBoard_) {
133 return bundleName == BUNDLE_NAME_SCENEBOARD;
134 }
135
136 return bundleName == BUNDLE_NAME_LAUNCHER;
137 }
138
IsLauncherAbility(const std::string & abilityName) const139 bool AppUtils::IsLauncherAbility(const std::string &abilityName) const
140 {
141 if (isSceneBoard_) {
142 return abilityName == SCENEBOARD_ABILITY_NAME;
143 }
144
145 return abilityName == LAUNCHER_ABILITY_NAME;
146 }
147
IsInheritWindowSplitScreenMode()148 bool AppUtils::IsInheritWindowSplitScreenMode()
149 {
150 if (!isInheritWindowSplitScreenMode_.isLoaded) {
151 isInheritWindowSplitScreenMode_.value = system::GetBoolParameter(INHERIT_WINDOW_SPLIT_SCREEN_MODE, true);
152 isInheritWindowSplitScreenMode_.isLoaded = true;
153 }
154 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isInheritWindowSplitScreenMode_.value);
155 return isInheritWindowSplitScreenMode_.value;
156 }
157
IsSupportAncoApp()158 bool AppUtils::IsSupportAncoApp()
159 {
160 if (!isSupportAncoApp_.isLoaded) {
161 isSupportAncoApp_.value = system::GetBoolParameter(SUPPORT_ANCO_APP, false);
162 isSupportAncoApp_.isLoaded = true;
163 }
164 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportAncoApp_.value);
165 return isSupportAncoApp_.value;
166 }
167
GetTimeoutUnitTimeRatio()168 int32_t AppUtils::GetTimeoutUnitTimeRatio()
169 {
170 if (!timeoutUnitTimeRatio_.isLoaded) {
171 timeoutUnitTimeRatio_.value = system::GetIntParameter<int32_t>(TIMEOUT_UNIT_TIME_RATIO, 1);
172 timeoutUnitTimeRatio_.isLoaded = true;
173 }
174 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", timeoutUnitTimeRatio_.value);
175 return timeoutUnitTimeRatio_.value;
176 }
177
IsSelectorDialogDefaultPossion()178 bool AppUtils::IsSelectorDialogDefaultPossion()
179 {
180 if (!isSelectorDialogDefaultPossion_.isLoaded) {
181 isSelectorDialogDefaultPossion_.value = system::GetBoolParameter(SELECTOR_DIALOG_POSSION, true);
182 isSelectorDialogDefaultPossion_.isLoaded = true;
183 }
184 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSelectorDialogDefaultPossion_.value);
185 return isSelectorDialogDefaultPossion_.value;
186 }
187
IsStartSpecifiedProcess()188 bool AppUtils::IsStartSpecifiedProcess()
189 {
190 if (!isStartSpecifiedProcess_.isLoaded) {
191 isStartSpecifiedProcess_.value = system::GetBoolParameter(START_SPECIFIED_PROCESS, false);
192 isStartSpecifiedProcess_.isLoaded = true;
193 }
194 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isStartSpecifiedProcess_.value);
195 return isStartSpecifiedProcess_.value;
196 }
197
IsUseMultiRenderProcess()198 bool AppUtils::IsUseMultiRenderProcess()
199 {
200 if (!isUseMultiRenderProcess_.isLoaded) {
201 isUseMultiRenderProcess_.value = system::GetBoolParameter(USE_MULTI_RENDER_PROCESS, true);
202 isUseMultiRenderProcess_.isLoaded = true;
203 }
204 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isUseMultiRenderProcess_.value);
205 return isUseMultiRenderProcess_.value;
206 }
207
IsLimitMaximumOfRenderProcess()208 bool AppUtils::IsLimitMaximumOfRenderProcess()
209 {
210 if (!isLimitMaximumOfRenderProcess_.isLoaded) {
211 isLimitMaximumOfRenderProcess_.value = system::GetBoolParameter(LIMIT_MAXIMUM_OF_RENDER_PROCESS, true);
212 isLimitMaximumOfRenderProcess_.isLoaded = true;
213 }
214 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isLimitMaximumOfRenderProcess_.value);
215 return isLimitMaximumOfRenderProcess_.value;
216 }
217
IsGrantPersistUriPermission()218 bool AppUtils::IsGrantPersistUriPermission()
219 {
220 if (!isGrantPersistUriPermission_.isLoaded) {
221 isGrantPersistUriPermission_.value = system::GetBoolParameter(GRANT_PERSIST_URI_PERMISSION, false);
222 isGrantPersistUriPermission_.isLoaded = true;
223 }
224 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isGrantPersistUriPermission_.value);
225 return isGrantPersistUriPermission_.value;
226 }
227
IsSupportGrantUriPermission()228 bool AppUtils::IsSupportGrantUriPermission()
229 {
230 if (!isGrantTempUriPermission_.isLoaded) {
231 isGrantTempUriPermission_.value = system::GetBoolParameter(GRANT_TEMPORARY_URI_PERMISSION, true);
232 isGrantTempUriPermission_.isLoaded = true;
233 }
234 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isGrantTempUriPermission_.value);
235 return isGrantTempUriPermission_.value;
236 }
237
IsStartOptionsWithAnimation()238 bool AppUtils::IsStartOptionsWithAnimation()
239 {
240 if (!isStartOptionsWithAnimation_.isLoaded) {
241 isStartOptionsWithAnimation_.value = system::GetBoolParameter(START_OPTIONS_WITH_ANIMATION, false);
242 isStartOptionsWithAnimation_.isLoaded = true;
243 }
244 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isStartOptionsWithAnimation_.value);
245 return isStartOptionsWithAnimation_.value;
246 }
247
IsSupportStartAbilities()248 bool AppUtils::IsSupportStartAbilities()
249 {
250 if (!isSupportStartAbilities_.isLoaded) {
251 isSupportStartAbilities_.value = system::GetBoolParameter(SUPPORT_START_ABILITIES, false);
252 isSupportStartAbilities_.isLoaded = true;
253 }
254 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportStartAbilities_.value);
255 return isSupportStartAbilities_.value;
256 }
257
IsMultiProcessModel()258 bool AppUtils::IsMultiProcessModel()
259 {
260 if (!isMultiProcessModel_.isLoaded) {
261 isMultiProcessModel_.value = system::GetBoolParameter(MULTI_PROCESS_MODEL, false);
262 isMultiProcessModel_.isLoaded = true;
263 }
264 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isMultiProcessModel_.value);
265 return isMultiProcessModel_.value;
266 }
267
AllowChildProcessInMultiProcessFeatureApp()268 bool AppUtils::AllowChildProcessInMultiProcessFeatureApp()
269 {
270 if (!allowChildProcessInMultiProcessFeatureApp_.isLoaded) {
271 allowChildProcessInMultiProcessFeatureApp_.value =
272 system::GetBoolParameter(ALLOW_CHILD_PROCESS_IN_MULTI_PROCESS_FEATURE_APP, false);
273 allowChildProcessInMultiProcessFeatureApp_.isLoaded = true;
274 }
275 TAG_LOGD(AAFwkTag::DEFAULT, "AllowChildProcessInMultiProcessFeatureApp:%{public}d",
276 allowChildProcessInMultiProcessFeatureApp_.value);
277 return allowChildProcessInMultiProcessFeatureApp_.value;
278 }
279
IsStartOptionsWithProcessOptions()280 bool AppUtils::IsStartOptionsWithProcessOptions()
281 {
282 if (!isStartOptionsWithProcessOptions_.isLoaded) {
283 isStartOptionsWithProcessOptions_.value = system::GetBoolParameter(START_OPTIONS_WITH_PROCESS_OPTION, false);
284 isStartOptionsWithProcessOptions_.isLoaded = true;
285 }
286 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isStartOptionsWithProcessOptions_.value);
287 return isStartOptionsWithProcessOptions_.value;
288 }
289
EnableMoveUIAbilityToBackgroundApi()290 bool AppUtils::EnableMoveUIAbilityToBackgroundApi()
291 {
292 if (!enableMoveUIAbilityToBackgroundApi_.isLoaded) {
293 enableMoveUIAbilityToBackgroundApi_.value =
294 system::GetBoolParameter(MOVE_UI_ABILITY_TO_BACKGROUND_API_ENABLE, true);
295 enableMoveUIAbilityToBackgroundApi_.isLoaded = true;
296 }
297 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", enableMoveUIAbilityToBackgroundApi_.value);
298 return enableMoveUIAbilityToBackgroundApi_.value;
299 }
300
IsLaunchEmbededUIAbility()301 bool AppUtils::IsLaunchEmbededUIAbility()
302 {
303 if (!isLaunchEmbededUIAbility_.isLoaded) {
304 isLaunchEmbededUIAbility_.value = system::GetBoolParameter(LAUNCH_EMBEDED_UI_ABILITY, false);
305 isLaunchEmbededUIAbility_.isLoaded = true;
306 }
307 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isLaunchEmbededUIAbility_.value);
308 return isLaunchEmbededUIAbility_.value;
309 }
310
IsSupportNativeChildProcess()311 bool AppUtils::IsSupportNativeChildProcess()
312 {
313 if (!isSupportNativeChildProcess_.isLoaded) {
314 isSupportNativeChildProcess_.value = system::GetBoolParameter(SUPPROT_NATIVE_CHILD_PROCESS, false);
315 isSupportNativeChildProcess_.isLoaded = true;
316 }
317 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportNativeChildProcess_.value);
318 return isSupportNativeChildProcess_.value;
319 }
320
IsAllowResidentInExtremeMemory(const std::string & bundleName,const std::string & abilityName)321 bool AppUtils::IsAllowResidentInExtremeMemory(const std::string& bundleName, const std::string& abilityName)
322 {
323 std::lock_guard lock(residentProcessInExtremeMemoryMutex_);
324 if (!residentProcessInExtremeMemory_.isLoaded) {
325 LoadResidentProcessInExtremeMemory();
326 residentProcessInExtremeMemory_.isLoaded = true;
327 }
328 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportNativeChildProcess_.value);
329 for (auto &element : residentProcessInExtremeMemory_.value) {
330 if (bundleName == element.first &&
331 (abilityName == "" || abilityName == element.second)) {
332 return true;
333 }
334 }
335 return false;
336 }
337
IsBigMemoryUnrelatedKeepAliveProc(const std::string & bundleName)338 bool AppUtils::IsBigMemoryUnrelatedKeepAliveProc(const std::string &bundleName)
339 {
340 std::lock_guard lock(processProhibitedFromRestartingMutex_);
341 if (!processProhibitedFromRestarting_.isLoaded) {
342 LoadProcessProhibitedFromRestarting();
343 processProhibitedFromRestarting_.isLoaded = true;
344 }
345 TAG_LOGD(AAFwkTag::DEFAULT, "loadJson about processProhibitedFromRestarting %{public}d",
346 processProhibitedFromRestarting_.isLoaded);
347 for (auto &element : processProhibitedFromRestarting_.value) {
348 if (bundleName == element) {
349 return true;
350 }
351 }
352 return false;
353 }
354
IsRequireBigMemoryProcess(const std::string & bundleName)355 bool AppUtils::IsRequireBigMemoryProcess(const std::string &bundleName)
356 {
357 std::lock_guard lock(requireBigMemoryAppMutex_);
358 if (!requireBigMemoryApp_.isLoaded) {
359 LoadRequireBigMemoryApp();
360 requireBigMemoryApp_.isLoaded = true;
361 }
362 TAG_LOGD(AAFwkTag::DEFAULT, "loadJson about requireBigMemoryApp %{public}d",
363 requireBigMemoryApp_.isLoaded);
364 for (auto &element : requireBigMemoryApp_.value) {
365 if (bundleName == element) {
366 return true;
367 }
368 }
369 return false;
370 }
371
LoadProcessProhibitedFromRestarting()372 void AppUtils::LoadProcessProhibitedFromRestarting()
373 {
374 nlohmann::json object;
375 if (!JsonUtils::GetInstance().LoadConfiguration(CONFIG_PATH, object)) {
376 TAG_LOGD(AAFwkTag::ABILITYMGR, "process prohibited invalid");
377 return;
378 }
379 if (!object.contains(PROCESS_PROHIBITED_FROM_RESTARTING) ||
380 !object.at(PROCESS_PROHIBITED_FROM_RESTARTING).is_array()) {
381 TAG_LOGE(AAFwkTag::ABILITYMGR, "process prohibited invalid.");
382 return;
383 }
384
385 for (auto &item : object.at(PROCESS_PROHIBITED_FROM_RESTARTING).items()) {
386 const nlohmann::json& jsonObject = item.value();
387 if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
388 TAG_LOGE(AAFwkTag::ABILITYMGR, "load bundleName failed");
389 return;
390 }
391 std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
392 processProhibitedFromRestarting_.value.emplace_back(bundleName);
393 }
394 }
395
LoadRequireBigMemoryApp()396 void AppUtils::LoadRequireBigMemoryApp()
397 {
398 nlohmann::json object;
399 if (!JsonUtils::GetInstance().LoadConfiguration(CONFIG_PATH, object)) {
400 TAG_LOGD(AAFwkTag::ABILITYMGR, "process prohibited invalid");
401 return;
402 }
403 if (!object.contains(REQUIRE_BIGMEMORY_APP) ||
404 !object.at(REQUIRE_BIGMEMORY_APP).is_array()) {
405 TAG_LOGE(AAFwkTag::ABILITYMGR, "process prohibited invalid.");
406 return;
407 }
408
409 for (auto &item : object.at(REQUIRE_BIGMEMORY_APP).items()) {
410 const nlohmann::json& jsonObject = item.value();
411 if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
412 TAG_LOGE(AAFwkTag::ABILITYMGR, "load bundleName failed");
413 return;
414 }
415 std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
416 requireBigMemoryApp_.value.emplace_back(bundleName);
417 }
418 }
419
LoadResidentProcessInExtremeMemory()420 void AppUtils::LoadResidentProcessInExtremeMemory()
421 {
422 nlohmann::json object;
423 if (!JsonUtils::GetInstance().LoadConfiguration(CONFIG_PATH, object)) {
424 TAG_LOGE(AAFwkTag::ABILITYMGR, "resident process failed");
425 return;
426 }
427 if (!object.contains(RESIDENT_PROCESS_IN_EXTREME_MEMORY) ||
428 !object.at(RESIDENT_PROCESS_IN_EXTREME_MEMORY).is_array()) {
429 TAG_LOGE(AAFwkTag::ABILITYMGR, "resident process invalid");
430 return;
431 }
432
433 for (auto &item : object.at(RESIDENT_PROCESS_IN_EXTREME_MEMORY).items()) {
434 const nlohmann::json& jsonObject = item.value();
435 if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
436 TAG_LOGE(AAFwkTag::ABILITYMGR, "load bundleName failed");
437 return;
438 }
439 if (!jsonObject.contains(ABILITY_NAME) || !jsonObject.at(ABILITY_NAME).is_string()) {
440 TAG_LOGE(AAFwkTag::ABILITYMGR, "load abilityName failed");
441 return;
442 }
443 std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
444 std::string abilityName = jsonObject.at(ABILITY_NAME).get<std::string>();
445 residentProcessInExtremeMemory_.value.emplace_back(std::make_pair(bundleName, abilityName));
446 }
447 }
448
IsAllowNativeChildProcess(const std::string & appIdentifier)449 bool AppUtils::IsAllowNativeChildProcess(const std::string &appIdentifier)
450 {
451 TAG_LOGD(AAFwkTag::DEFAULT, "appId:%{private}s", appIdentifier.c_str());
452 if (!allowStartNativeProcessApps_.isLoaded) {
453 LoadAllowNativeChildProcessApps();
454 allowStartNativeProcessApps_.isLoaded = true;
455 }
456 auto &apps = allowStartNativeProcessApps_.value;
457 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}zu", apps.size());
458 return std::find(apps.begin(), apps.end(), appIdentifier) != apps.end();
459 }
460
LoadAllowNativeChildProcessApps()461 void AppUtils::LoadAllowNativeChildProcessApps()
462 {
463 nlohmann::json object;
464 if (!JsonUtils::GetInstance().LoadConfiguration(ALLOW_NATIVE_CHILD_PROCESS_APPS_CONFIG_PATH, object)) {
465 TAG_LOGE(AAFwkTag::ABILITYMGR, "load child process config failed");
466 return;
467 }
468 if (!object.contains(KEY_ALLOW_NATIVE_CHILD_PROCESS_APPS) ||
469 !object.at(KEY_ALLOW_NATIVE_CHILD_PROCESS_APPS).is_array()) {
470 TAG_LOGE(AAFwkTag::ABILITYMGR, "get key invalid");
471 return;
472 }
473
474 for (auto &item : object.at(KEY_ALLOW_NATIVE_CHILD_PROCESS_APPS).items()) {
475 const nlohmann::json& jsonObject = item.value();
476 if (!jsonObject.contains(KEY_IDENTIFIER) || !jsonObject.at(KEY_IDENTIFIER).is_string()) {
477 TAG_LOGE(AAFwkTag::ABILITYMGR, "load identifier failed");
478 return;
479 }
480 std::string identifier = jsonObject.at(KEY_IDENTIFIER).get<std::string>();
481 allowStartNativeProcessApps_.value.emplace_back(identifier);
482 }
483 }
484
GetLimitMaximumExtensionsPerProc()485 int32_t AppUtils::GetLimitMaximumExtensionsPerProc()
486 {
487 if (!limitMaximumExtensionsPerProc_.isLoaded) {
488 limitMaximumExtensionsPerProc_.value =
489 system::GetIntParameter<int32_t>(LIMIT_MAXIMUM_EXTENSIONS_OF_PER_PROCESS, DEFAULT_MAX_EXT_PER_PROC);
490 limitMaximumExtensionsPerProc_.isLoaded = true;
491 }
492 TAG_LOGD(AAFwkTag::DEFAULT, "limitMaximumExtensionsPerProc: %{public}d", limitMaximumExtensionsPerProc_.value);
493 return limitMaximumExtensionsPerProc_.value;
494 }
495
GetLimitMaximumExtensionsPerDevice()496 int32_t AppUtils::GetLimitMaximumExtensionsPerDevice()
497 {
498 if (!limitMaximumExtensionsPerDevice_.isLoaded) {
499 limitMaximumExtensionsPerDevice_.value =
500 system::GetIntParameter<int32_t>(LIMIT_MAXIMUM_EXTENSIONS_OF_PER_DEVICE, DEFAULT_MAX_EXT_PER_DEV);
501 limitMaximumExtensionsPerDevice_.isLoaded = true;
502 }
503 TAG_LOGD(AAFwkTag::DEFAULT, "limitMaximumExtensionsPerDevice: %{public}d", limitMaximumExtensionsPerDevice_.value);
504 return limitMaximumExtensionsPerDevice_.value;
505 }
506
GetCacheExtensionTypeList()507 std::string AppUtils::GetCacheExtensionTypeList()
508 {
509 std::string cacheExtAbilityTypeList = system::GetParameter(CACHE_EXTENSION_TYPES, "260");
510 TAG_LOGD(AAFwkTag::DEFAULT, "cacheExtAbilityTypeList is %{public}s", cacheExtAbilityTypeList.c_str());
511 return cacheExtAbilityTypeList;
512 }
513
GetAncoAppIdentifiers()514 std::string AppUtils::GetAncoAppIdentifiers()
515 {
516 std::string identifiers = system::GetParameter(PARAM_ANCO_APP_IDENTIFIER, "");
517 return identifiers;
518 }
519
IsAllowStartAbilityWithoutCallerToken(const std::string & bundleName,const std::string & abilityName)520 bool AppUtils::IsAllowStartAbilityWithoutCallerToken(const std::string& bundleName, const std::string& abilityName)
521 {
522 std::lock_guard lock(startAbilityWithoutCallerTokenMutex_);
523 if (!startAbilityWithoutCallerToken_.isLoaded) {
524 LoadStartAbilityWithoutCallerToken();
525 startAbilityWithoutCallerToken_.isLoaded = true;
526 }
527 TAG_LOGD(AAFwkTag::DEFAULT, "isLoaded: %{public}d", startAbilityWithoutCallerToken_.isLoaded);
528 for (auto &element : startAbilityWithoutCallerToken_.value) {
529 if (bundleName == element.first && abilityName == element.second) {
530 TAG_LOGI(AAFwkTag::DEFAULT, "call");
531 return true;
532 }
533 }
534 return false;
535 }
536
LoadStartAbilityWithoutCallerToken()537 void AppUtils::LoadStartAbilityWithoutCallerToken()
538 {
539 nlohmann::json object;
540 if (!JsonUtils::GetInstance().LoadConfiguration(
541 START_ABILITY_WITHOUT_CALLERTOKEN_PATH, object, START_ABILITY_WITHOUT_CALLERTOKEN)) {
542 TAG_LOGE(AAFwkTag::DEFAULT, "token list failed");
543 return;
544 }
545 if (!object.contains(START_ABILITY_WITHOUT_CALLERTOKEN_TITLE) ||
546 !object.at(START_ABILITY_WITHOUT_CALLERTOKEN_TITLE).is_array()) {
547 TAG_LOGE(AAFwkTag::DEFAULT, "token config invalid");
548 return;
549 }
550
551 for (auto &item : object.at(START_ABILITY_WITHOUT_CALLERTOKEN_TITLE).items()) {
552 const nlohmann::json& jsonObject = item.value();
553 if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
554 TAG_LOGE(AAFwkTag::DEFAULT, "load bundleName failed");
555 return;
556 }
557 if (!jsonObject.contains(ABILITY_NAME) || !jsonObject.at(ABILITY_NAME).is_string()) {
558 TAG_LOGE(AAFwkTag::DEFAULT, "load abilityName failed");
559 return;
560 }
561 std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
562 std::string abilityName = jsonObject.at(ABILITY_NAME).get<std::string>();
563 startAbilityWithoutCallerToken_.value.emplace_back(std::make_pair(bundleName, abilityName));
564 }
565 }
566
GetBrokerDelegateBundleName()567 std::string AppUtils::GetBrokerDelegateBundleName()
568 {
569 if (!brokerDelegateBundleName_.isLoaded) {
570 brokerDelegateBundleName_.value = system::GetParameter(BROKER_DELEGATE_BUNDLE_NAME, "");
571 brokerDelegateBundleName_.isLoaded = true;
572 }
573 TAG_LOGD(AAFwkTag::DEFAULT, "shellAssistantBundleName_ is %{public}s", brokerDelegateBundleName_.value.c_str());
574 return brokerDelegateBundleName_.value;
575 }
576
GetCollaboratorBrokerUID()577 int32_t AppUtils::GetCollaboratorBrokerUID()
578 {
579 if (!collaboratorBrokerUid_.isLoaded) {
580 collaboratorBrokerUid_.value = system::GetIntParameter(COLLABORATOR_BROKER_UID, DEFAULT_INVALID_VALUE);
581 collaboratorBrokerUid_.isLoaded = true;
582 }
583 TAG_LOGD(AAFwkTag::DEFAULT, "collaboratorBrokerUid_ is %{public}d", collaboratorBrokerUid_.value);
584 return collaboratorBrokerUid_.value;
585 }
586
GetCollaboratorBrokerReserveUID()587 int32_t AppUtils::GetCollaboratorBrokerReserveUID()
588 {
589 if (!collaboratorBrokerReserveUid_.isLoaded) {
590 collaboratorBrokerReserveUid_.value = system::GetIntParameter(COLLABORATOR_BROKER_RESERVE_UID,
591 DEFAULT_INVALID_VALUE);
592 collaboratorBrokerReserveUid_.isLoaded = true;
593 }
594 TAG_LOGD(AAFwkTag::DEFAULT, "collaboratorBrokerReserveUid_ is %{public}d", collaboratorBrokerReserveUid_.value);
595 return collaboratorBrokerReserveUid_.value;
596 }
597
MaxChildProcess()598 int32_t AppUtils::MaxChildProcess()
599 {
600 if (!maxChildProcess_.isLoaded) {
601 maxChildProcess_.value =
602 system::GetIntParameter<int32_t>(MAX_CHILD_PROCESS, DEFAULT_MAX_CHILD_PROCESS);
603 maxChildProcess_.isLoaded = true;
604 }
605 TAG_LOGD(AAFwkTag::DEFAULT, "MaxChildProcess: %{public}d", maxChildProcess_.value);
606 return maxChildProcess_.value;
607 }
608
MaxMultiProcessFeatureChildProcess()609 int32_t AppUtils::MaxMultiProcessFeatureChildProcess()
610 {
611 if (!maxMultiProcessFeatureChildProcess_.isLoaded) {
612 maxMultiProcessFeatureChildProcess_.value =
613 system::GetIntParameter<int32_t>(MAX_MULTI_PROCESS_FEATURE_CHILD_PROCESS, DEFAULT_MAX_CHILD_PROCESS);
614 maxMultiProcessFeatureChildProcess_.isLoaded = true;
615 }
616 TAG_LOGD(AAFwkTag::DEFAULT, "maxMultiProcessFeatureChildProcess: %{public}d",
617 maxMultiProcessFeatureChildProcess_.value);
618 return maxMultiProcessFeatureChildProcess_.value;
619 }
620
IsSupportMultiInstance()621 bool AppUtils::IsSupportMultiInstance()
622 {
623 if (!isSupportMultiInstance_.isLoaded) {
624 isSupportMultiInstance_.value = system::GetBoolParameter(SUPPORT_MULTI_INSTANCE, false);
625 isSupportMultiInstance_.isLoaded = true;
626 }
627 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportMultiInstance_.value);
628 return isSupportMultiInstance_.value;
629 }
630
GetMigrateClientBundleName()631 std::string AppUtils::GetMigrateClientBundleName()
632 {
633 if (!migrateClientBundleName_.isLoaded) {
634 migrateClientBundleName_.value = system::GetParameter(MIGRATE_CLIENT_BUNDLE_NAME, "");
635 migrateClientBundleName_.isLoaded = true;
636 }
637 TAG_LOGD(AAFwkTag::DEFAULT, "migrateClientBundleName_ is %{public}s", migrateClientBundleName_.value.c_str());
638 return migrateClientBundleName_.value;
639 }
640
IsConnectSupportCrossUser()641 bool AppUtils::IsConnectSupportCrossUser()
642 {
643 std::lock_guard guard(isConnectSupportCrossUserMutex_);
644 if (!isConnectSupportCrossUser_.isLoaded) {
645 isConnectSupportCrossUser_.value = system::GetBoolParameter(CONNECT_SUPPORT_CROSS_USER, false);
646 isConnectSupportCrossUser_.isLoaded = true;
647 }
648 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isConnectSupportCrossUser_.value);
649 return isConnectSupportCrossUser_.value;
650 }
651
IsPrepareTerminateEnabled()652 bool AppUtils::IsPrepareTerminateEnabled()
653 {
654 char value[PREPARE_TERMINATE_ENABLE_SIZE] = "false";
655 int retSysParam = GetParameter(PREPARE_TERMINATE_ENABLE_PARAMETER, "false", value, PREPARE_TERMINATE_ENABLE_SIZE);
656 TAG_LOGI(AAFwkTag::DEFAULT,
657 "isPrepareTerminateEnable, %{public}s value=%{public}s", PREPARE_TERMINATE_ENABLE_PARAMETER, value);
658 if (retSysParam > 0 && !std::strcmp(value, "true")) {
659 return true;
660 }
661 return false;
662 }
663
IsSystemReasonMessage(const std::string & reasonMessage)664 bool AppUtils::IsSystemReasonMessage(const std::string &reasonMessage)
665 {
666 const std::unordered_set<std::string> systemReasonMessagesSet = {
667 "ReasonMessage_SystemShare",
668 "ReasonMessage_DesktopShortcut",
669 };
670 return systemReasonMessagesSet.find(reasonMessage) != systemReasonMessagesSet.end();
671 }
672
IsCacheAbilityEnabled()673 bool AppUtils::IsCacheAbilityEnabled()
674 {
675 return system::GetBoolParameter(CACHE_ABILITY_BY_LIST_ENABLE, false);
676 }
677
LoadCacheAbilityList()678 void AppUtils::LoadCacheAbilityList()
679 {
680 nlohmann::json object;
681 if (!JsonUtils::GetInstance().LoadConfiguration(CACHE_ABILITY_LIST_PATH, object)) {
682 TAG_LOGI(AAFwkTag::ABILITYMGR, "load cache_ability file failed");
683 return;
684 }
685 if (!object.contains(CACHE_PROCESS_NAME) ||
686 !object.at(CACHE_PROCESS_NAME).is_array()) {
687 TAG_LOGE(AAFwkTag::ABILITYMGR, "cache_ability file invalid");
688 return;
689 }
690
691 for (auto &item : object.at(CACHE_PROCESS_NAME).items()) {
692 const nlohmann::json& jsonObject = item.value();
693 if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
694 TAG_LOGE(AAFwkTag::ABILITYMGR, "load cache_ability bundleName failed");
695 return;
696 }
697 if (!jsonObject.contains(ABILITY_NAME) || !jsonObject.at(ABILITY_NAME).is_string()) {
698 TAG_LOGE(AAFwkTag::ABILITYMGR, "load cache_ability abilityName failed");
699 return;
700 }
701 std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
702 std::string abilityName = jsonObject.at(ABILITY_NAME).get<std::string>();
703 cacheAbilityList_.value.emplace_back(std::make_pair(bundleName, abilityName));
704 }
705 }
706
IsCacheExtensionAbilityByList(const std::string & bundleName,const std::string & abilityName)707 bool AppUtils::IsCacheExtensionAbilityByList(const std::string& bundleName, const std::string& abilityName)
708 {
709 if (!cacheAbilityList_.isLoaded) {
710 std::lock_guard lock(cacheAbilityListMutex_);
711 if (!cacheAbilityList_.isLoaded) {
712 LoadCacheAbilityList();
713 cacheAbilityList_.isLoaded = true;
714 }
715 }
716
717 if (cacheAbilityList_.value.empty() || !IsCacheAbilityEnabled()) {
718 return false;
719 }
720
721 for (auto &element : cacheAbilityList_.value) {
722 if (bundleName == element.first && abilityName == element.second) {
723 TAG_LOGI(AAFwkTag::DEFAULT, "cache_ability: %{public}s, %{public}s",
724 bundleName.c_str(), abilityName.c_str());
725 return true;
726 }
727 }
728 return false;
729 }
730
LoadResidentWhiteList()731 void AppUtils::LoadResidentWhiteList()
732 {
733 nlohmann::json object;
734 if (!JsonUtils::GetInstance().LoadConfiguration(RESIDENT_WHITE_LIST_PATH, object)) {
735 TAG_LOGI(AAFwkTag::ABILITYMGR, "load resident white list file failed");
736 return;
737 }
738 if (!object.contains(NORMAL_RESIDENT_APPS) ||
739 !object.at(NORMAL_RESIDENT_APPS).is_array()) {
740 TAG_LOGE(AAFwkTag::ABILITYMGR, "resident white list file invalid");
741 return;
742 }
743
744 for (auto &item : object.at(NORMAL_RESIDENT_APPS).items()) {
745 const nlohmann::json& jsonObject = item.value();
746 if (!jsonObject.is_string()) {
747 TAG_LOGE(AAFwkTag::ABILITYMGR, "load resident white bundleName failed");
748 return;
749 }
750 residentWhiteList_.value.emplace_back(jsonObject.get<std::string>());
751 }
752 }
753
GetResidentWhiteList()754 const std::vector<std::string>& AppUtils::GetResidentWhiteList()
755 {
756 std::lock_guard lock(residentWhiteListMutex_);
757 if (!residentWhiteList_.isLoaded) {
758 LoadResidentWhiteList();
759 residentWhiteList_.isLoaded = true;
760 }
761 return residentWhiteList_.value;
762 }
763
InResidentWhiteList(const std::string & bundleName)764 bool AppUtils::InResidentWhiteList(const std::string &bundleName)
765 {
766 std::lock_guard lock(residentWhiteListMutex_);
767 if (!residentWhiteList_.isLoaded) {
768 LoadResidentWhiteList();
769 residentWhiteList_.isLoaded = true;
770 }
771 for (const auto &item: residentWhiteList_.value) {
772 if (bundleName == item) {
773 return true;
774 }
775 }
776 return false;
777 }
778
LoadOnNewProcessEnableList()779 void AppUtils::LoadOnNewProcessEnableList()
780 {
781 nlohmann::json object;
782 if (!JsonUtils::GetInstance().LoadConfiguration(ON_NEW_PROCESS_ENABLE_LIST_PATH, object)) {
783 TAG_LOGI(AAFwkTag::ABILITYMGR, "load onNewProcessEnableList file failed");
784 return;
785 }
786 if (!object.contains(ON_NEW_PROCESS_ENABLE_LIST) ||
787 !object.at(ON_NEW_PROCESS_ENABLE_LIST).is_array()) {
788 TAG_LOGE(AAFwkTag::ABILITYMGR, "onNewProcessEnableList file invalid");
789 return;
790 }
791 for (auto &item : object.at(ON_NEW_PROCESS_ENABLE_LIST).items()) {
792 const nlohmann::json& jsonObject = item.value();
793 if (!jsonObject.is_string()) {
794 TAG_LOGE(AAFwkTag::ABILITYMGR, "load onNewProcessEnableList bundleName failed");
795 return;
796 }
797 onNewProcessEnableList_.value.emplace_back(jsonObject.get<std::string>());
798 }
799 }
800
InOnNewProcessEnableList(const std::string & bundleName)801 bool AppUtils::InOnNewProcessEnableList(const std::string &bundleName)
802 {
803 std::lock_guard lock(onNewProcessEnableListMutex_);
804 if (!onNewProcessEnableList_.isLoaded) {
805 LoadOnNewProcessEnableList();
806 onNewProcessEnableList_.isLoaded = true;
807 }
808 for (const auto &item: onNewProcessEnableList_.value) {
809 if (bundleName == item) {
810 return true;
811 }
812 }
813 return false;
814 }
815
IsSupportAppServiceExtension()816 bool AppUtils::IsSupportAppServiceExtension()
817 {
818 if (!isSupportAppServiceExtension_.isLoaded) {
819 isSupportAppServiceExtension_.value = system::GetBoolParameter(SUPPORT_APP_SERVICE_EXTENSION, false);
820 isSupportAppServiceExtension_.isLoaded = true;
821 }
822 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportAppServiceExtension_.value);
823 return isSupportAppServiceExtension_.value;
824 }
825
IsPreloadApplicationEnabled()826 bool AppUtils::IsPreloadApplicationEnabled()
827 {
828 if (!isPreloadApplicationEnabled_.isLoaded) {
829 isPreloadApplicationEnabled_.value = system::GetBoolParameter(
830 PRODUCT_PRELOAD_APPLICATION_SETTING_ENABLED, false);
831 isPreloadApplicationEnabled_.isLoaded = true;
832 }
833 TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isPreloadApplicationEnabled_.value);
834 return isPreloadApplicationEnabled_.value;
835 }
836
IsForbidStart()837 bool AppUtils::IsForbidStart()
838 {
839 #ifdef ABILITY_RUNTIME_MEDIA_LIBRARY_ENABLE
840 return false;
841 #else
842 if (!isForbidStart_.isLoaded) {
843 isForbidStart_.value = system::GetBoolParameter(FORBID_START, false);
844 }
845 return isForbidStart_.value;
846 #endif
847 }
848 } // namespace AAFwk
849 } // namespace OHOS
850