• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "json_utils.h"
18 #include "hilog_tag_wrapper.h"
19 #include "nlohmann/json.hpp"
20 #include "parameter.h"
21 #include "parameters.h"
22 #ifdef SUPPORT_GRAPHICS
23 #include "scene_board_judgement.h"
24 #endif // SUPPORT_GRAPHICS
25 
26 
27 namespace OHOS {
28 namespace AAFwk {
29 namespace {
30 constexpr const char* BUNDLE_NAME_LAUNCHER = "com.ohos.launcher";
31 constexpr const char* BUNDLE_NAME_SCENEBOARD = "com.ohos.sceneboard";
32 constexpr const char* LAUNCHER_ABILITY_NAME = "com.ohos.launcher.MainAbility";
33 constexpr const char* SCENEBOARD_ABILITY_NAME = "com.ohos.sceneboard.MainAbility";
34 constexpr const char* INHERIT_WINDOW_SPLIT_SCREEN_MODE = "persist.sys.abilityms.inherit_window_split_screen_mode";
35 constexpr const char* SUPPORT_ANCO_APP = "persist.sys.abilityms.support_anco_app";
36 constexpr const char* TIMEOUT_UNIT_TIME_RATIO = "persist.sys.abilityms.timeout_unit_time_ratio";
37 constexpr const char* SELECTOR_DIALOG_POSSION = "persist.sys.abilityms.selector_dialog_possion";
38 constexpr const char* START_SPECIFIED_PROCESS = "persist.sys.abilityms.start_specified_process";
39 constexpr const char* USE_MULTI_RENDER_PROCESS = "persist.sys.abilityms.use_multi_render_process";
40 constexpr const char* LIMIT_MAXIMUM_OF_RENDER_PROCESS = "persist.sys.abilityms.limit_maximum_of_render_process";
41 constexpr const char* GRANT_PERSIST_URI_PERMISSION = "persist.sys.abilityms.grant_persist_uri_permission";
42 constexpr const char* START_OPTIONS_WITH_ANIMATION = "persist.sys.abilityms.start_options_with_animation";
43 constexpr const char* MULTI_PROCESS_MODEL = "persist.sys.abilityms.multi_process_model";
44 constexpr const char* START_OPTIONS_WITH_PROCESS_OPTION = "persist.sys.abilityms.start_options_with_process_option";
45 constexpr const char* MOVE_UI_ABILITY_TO_BACKGROUND_API_ENABLE =
46     "persist.sys.abilityms.move_ui_ability_to_background_api_enable";
47 constexpr const char* CONFIG_PATH = "/etc/ability_runtime/resident_process_in_extreme_memory.json";
48 constexpr const char* RESIDENT_PROCESS_IN_EXTREME_MEMORY = "residentProcessInExtremeMemory";
49 constexpr const char* BUNDLE_NAME = "bundleName";
50 constexpr const char* ABILITY_NAME = "abilityName";
51 constexpr const char* KEY_IDENTIFIER = "identifier";
52 constexpr const char* ALLOW_NATIVE_CHILD_PROCESS_APPS_CONFIG_PATH =
53     "/etc/ability_runtime/allow_native_child_process_apps.json";
54 constexpr const char* KEY_ALLOW_NATIVE_CHILD_PROCESS_APPS = "allowNativeChildProcessApps";
55 constexpr const char* LAUNCH_EMBEDED_UI_ABILITY = "const.abilityms.launch_embeded_ui_ability";
56 const std::string SUPPROT_NATIVE_CHILD_PROCESS = "persist.sys.abilityms.start_native_child_process";
57 const std::string LIMIT_MAXIMUM_EXTENSIONS_OF_PER_PROCESS =
58     "const.sys.abilityms.limit_maximum_extensions_of_per_process";
59 const std::string LIMIT_MAXIMUM_EXTENSIONS_OF_PER_DEVICE =
60     "const.sys.abilityms.limit_maximum_extensions_of_per_device";
61 const std::string CACHE_EXTENSION_TYPES = "const.sys.abilityms.cache_extension";
62 constexpr const char* START_ABILITY_WITHOUT_CALLERTOKEN = "/system/etc/start_ability_without_caller_token.json";
63 constexpr const char* START_ABILITY_WITHOUT_CALLERTOKEN_PATH =
64     "/etc/ability_runtime/start_ability_without_caller_token.json";
65 constexpr const char* START_ABILITY_WITHOUT_CALLERTOKEN_TITLE = "startAbilityWithoutCallerToken";
66 constexpr const char* BROKER_DELEGATE_BUNDLE_NAME = "const.sys.abilityms.broker_delegate_bundle_name";
67 constexpr const char* COLLABORATOR_BROKER_UID = "const.sys.abilityms.collaborator_broker_uid";
68 constexpr const char* COLLABORATOR_BROKER_RESERVE_UID = "const.sys.abilityms.collaborator_broker_reserve_uid";
69 constexpr const char* MAX_CHILD_PROCESS = "const.max_native_child_process";
70 constexpr const char* SUPPORT_MULTI_INSTANCE = "const.abilityms.support_multi_instance";
71 constexpr const char* MIGRATE_CLIENT_BUNDLE_NAME = "const.sys.abilityms.migrate_client_bundle_name";
72 constexpr const char* CONNECT_SUPPORT_CROSS_USER = "const.abilityms.connect_support_cross_user";
73 // Support prepare terminate
74 constexpr int32_t PREPARE_TERMINATE_ENABLE_SIZE = 6;
75 constexpr const char* PREPARE_TERMINATE_ENABLE_PARAMETER = "persist.sys.prepare_terminate";
76 constexpr const char* CACHE_ABILITY_BY_LIST_ENABLE = "persist.sys.abilityms.cache_ability_enable";
77 constexpr const char* CACHE_ABILITY_LIST_PATH = "etc/ability/abilityms_cache_ability.json";
78 constexpr const char* CACHE_PROCESS_NAME = "cache_list";
79 }
80 
~AppUtils()81 AppUtils::~AppUtils() {}
82 
AppUtils()83 AppUtils::AppUtils()
84 {
85     #ifdef SUPPORT_GRAPHICS
86     if (Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
87         isSceneBoard_ = true;
88     }
89     #endif // SUPPORT_GRAPHICS
90 }
91 
GetInstance()92 AppUtils &AppUtils::GetInstance()
93 {
94     static AppUtils utils;
95     return utils;
96 }
97 
IsLauncher(const std::string & bundleName) const98 bool AppUtils::IsLauncher(const std::string &bundleName) const
99 {
100     if (isSceneBoard_) {
101         return bundleName == BUNDLE_NAME_SCENEBOARD;
102     }
103 
104     return bundleName == BUNDLE_NAME_LAUNCHER;
105 }
106 
IsLauncherAbility(const std::string & abilityName) const107 bool AppUtils::IsLauncherAbility(const std::string &abilityName) const
108 {
109     if (isSceneBoard_) {
110         return abilityName == SCENEBOARD_ABILITY_NAME;
111     }
112 
113     return abilityName == LAUNCHER_ABILITY_NAME;
114 }
115 
IsInheritWindowSplitScreenMode()116 bool AppUtils::IsInheritWindowSplitScreenMode()
117 {
118     if (!isInheritWindowSplitScreenMode_.isLoaded) {
119         isInheritWindowSplitScreenMode_.value = system::GetBoolParameter(INHERIT_WINDOW_SPLIT_SCREEN_MODE, true);
120         isInheritWindowSplitScreenMode_.isLoaded = true;
121     }
122     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isInheritWindowSplitScreenMode_.value);
123     return isInheritWindowSplitScreenMode_.value;
124 }
125 
IsSupportAncoApp()126 bool AppUtils::IsSupportAncoApp()
127 {
128     if (!isSupportAncoApp_.isLoaded) {
129         isSupportAncoApp_.value = system::GetBoolParameter(SUPPORT_ANCO_APP, false);
130         isSupportAncoApp_.isLoaded = true;
131     }
132     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportAncoApp_.value);
133     return isSupportAncoApp_.value;
134 }
135 
GetTimeoutUnitTimeRatio()136 int32_t AppUtils::GetTimeoutUnitTimeRatio()
137 {
138     if (!timeoutUnitTimeRatio_.isLoaded) {
139         timeoutUnitTimeRatio_.value = system::GetIntParameter<int32_t>(TIMEOUT_UNIT_TIME_RATIO, 1);
140         timeoutUnitTimeRatio_.isLoaded = true;
141     }
142     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", timeoutUnitTimeRatio_.value);
143     return timeoutUnitTimeRatio_.value;
144 }
145 
IsSelectorDialogDefaultPossion()146 bool AppUtils::IsSelectorDialogDefaultPossion()
147 {
148     if (!isSelectorDialogDefaultPossion_.isLoaded) {
149         isSelectorDialogDefaultPossion_.value = system::GetBoolParameter(SELECTOR_DIALOG_POSSION, true);
150         isSelectorDialogDefaultPossion_.isLoaded = true;
151     }
152     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSelectorDialogDefaultPossion_.value);
153     return isSelectorDialogDefaultPossion_.value;
154 }
155 
IsStartSpecifiedProcess()156 bool AppUtils::IsStartSpecifiedProcess()
157 {
158     if (!isStartSpecifiedProcess_.isLoaded) {
159         isStartSpecifiedProcess_.value = system::GetBoolParameter(START_SPECIFIED_PROCESS, false);
160         isStartSpecifiedProcess_.isLoaded = true;
161     }
162     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isStartSpecifiedProcess_.value);
163     return isStartSpecifiedProcess_.value;
164 }
165 
IsUseMultiRenderProcess()166 bool AppUtils::IsUseMultiRenderProcess()
167 {
168     if (!isUseMultiRenderProcess_.isLoaded) {
169         isUseMultiRenderProcess_.value = system::GetBoolParameter(USE_MULTI_RENDER_PROCESS, true);
170         isUseMultiRenderProcess_.isLoaded = true;
171     }
172     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isUseMultiRenderProcess_.value);
173     return isUseMultiRenderProcess_.value;
174 }
175 
IsLimitMaximumOfRenderProcess()176 bool AppUtils::IsLimitMaximumOfRenderProcess()
177 {
178     if (!isLimitMaximumOfRenderProcess_.isLoaded) {
179         isLimitMaximumOfRenderProcess_.value = system::GetBoolParameter(LIMIT_MAXIMUM_OF_RENDER_PROCESS, true);
180         isLimitMaximumOfRenderProcess_.isLoaded = true;
181     }
182     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isLimitMaximumOfRenderProcess_.value);
183     return isLimitMaximumOfRenderProcess_.value;
184 }
185 
IsGrantPersistUriPermission()186 bool AppUtils::IsGrantPersistUriPermission()
187 {
188     if (!isGrantPersistUriPermission_.isLoaded) {
189         isGrantPersistUriPermission_.value = system::GetBoolParameter(GRANT_PERSIST_URI_PERMISSION, false);
190         isGrantPersistUriPermission_.isLoaded = true;
191     }
192     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isGrantPersistUriPermission_.value);
193     return isGrantPersistUriPermission_.value;
194 }
195 
IsStartOptionsWithAnimation()196 bool AppUtils::IsStartOptionsWithAnimation()
197 {
198     if (!isStartOptionsWithAnimation_.isLoaded) {
199         isStartOptionsWithAnimation_.value = system::GetBoolParameter(START_OPTIONS_WITH_ANIMATION, false);
200         isStartOptionsWithAnimation_.isLoaded = true;
201     }
202     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isStartOptionsWithAnimation_.value);
203     return isStartOptionsWithAnimation_.value;
204 }
205 
IsMultiProcessModel()206 bool AppUtils::IsMultiProcessModel()
207 {
208     if (!isMultiProcessModel_.isLoaded) {
209         isMultiProcessModel_.value = system::GetBoolParameter(MULTI_PROCESS_MODEL, false);
210         isMultiProcessModel_.isLoaded = true;
211     }
212     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isMultiProcessModel_.value);
213     return isMultiProcessModel_.value;
214 }
215 
IsStartOptionsWithProcessOptions()216 bool AppUtils::IsStartOptionsWithProcessOptions()
217 {
218     if (!isStartOptionsWithProcessOptions_.isLoaded) {
219         isStartOptionsWithProcessOptions_.value = system::GetBoolParameter(START_OPTIONS_WITH_PROCESS_OPTION, false);
220         isStartOptionsWithProcessOptions_.isLoaded = true;
221     }
222     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isStartOptionsWithProcessOptions_.value);
223     return isStartOptionsWithProcessOptions_.value;
224 }
225 
EnableMoveUIAbilityToBackgroundApi()226 bool AppUtils::EnableMoveUIAbilityToBackgroundApi()
227 {
228     if (!enableMoveUIAbilityToBackgroundApi_.isLoaded) {
229         enableMoveUIAbilityToBackgroundApi_.value =
230             system::GetBoolParameter(MOVE_UI_ABILITY_TO_BACKGROUND_API_ENABLE, true);
231         enableMoveUIAbilityToBackgroundApi_.isLoaded = true;
232     }
233     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", enableMoveUIAbilityToBackgroundApi_.value);
234     return enableMoveUIAbilityToBackgroundApi_.value;
235 }
236 
IsLaunchEmbededUIAbility()237 bool AppUtils::IsLaunchEmbededUIAbility()
238 {
239     if (!isLaunchEmbededUIAbility_.isLoaded) {
240         isLaunchEmbededUIAbility_.value = system::GetBoolParameter(LAUNCH_EMBEDED_UI_ABILITY, false);
241         isLaunchEmbededUIAbility_.isLoaded = true;
242     }
243     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isLaunchEmbededUIAbility_.value);
244     return isLaunchEmbededUIAbility_.value;
245 }
246 
IsSupportNativeChildProcess()247 bool AppUtils::IsSupportNativeChildProcess()
248 {
249     if (!isSupportNativeChildProcess_.isLoaded) {
250         isSupportNativeChildProcess_.value = system::GetBoolParameter(SUPPROT_NATIVE_CHILD_PROCESS, false);
251         isSupportNativeChildProcess_.isLoaded = true;
252     }
253     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportNativeChildProcess_.value);
254     return isSupportNativeChildProcess_.value;
255 }
256 
IsAllowResidentInExtremeMemory(const std::string & bundleName,const std::string & abilityName)257 bool AppUtils::IsAllowResidentInExtremeMemory(const std::string& bundleName, const std::string& abilityName)
258 {
259     std::lock_guard lock(residentProcessInExtremeMemoryMutex_);
260     if (!residentProcessInExtremeMemory_.isLoaded) {
261         LoadResidentProcessInExtremeMemory();
262         residentProcessInExtremeMemory_.isLoaded = true;
263     }
264     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportNativeChildProcess_.value);
265     for (auto &element : residentProcessInExtremeMemory_.value) {
266         if (bundleName == element.first &&
267             (abilityName == "" || abilityName == element.second)) {
268             return true;
269         }
270     }
271     return false;
272 }
273 
LoadResidentProcessInExtremeMemory()274 void AppUtils::LoadResidentProcessInExtremeMemory()
275 {
276     nlohmann::json object;
277     if (!JsonUtils::GetInstance().LoadConfiguration(CONFIG_PATH, object)) {
278         TAG_LOGE(AAFwkTag::ABILITYMGR, "resident process failed");
279         return;
280     }
281     if (!object.contains(RESIDENT_PROCESS_IN_EXTREME_MEMORY) ||
282         !object.at(RESIDENT_PROCESS_IN_EXTREME_MEMORY).is_array()) {
283         TAG_LOGE(AAFwkTag::ABILITYMGR, "resident process invalid");
284         return;
285     }
286 
287     for (auto &item : object.at(RESIDENT_PROCESS_IN_EXTREME_MEMORY).items()) {
288         const nlohmann::json& jsonObject = item.value();
289         if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
290             TAG_LOGE(AAFwkTag::ABILITYMGR, "load bundleName failed");
291             return;
292         }
293         if (!jsonObject.contains(ABILITY_NAME) || !jsonObject.at(ABILITY_NAME).is_string()) {
294             TAG_LOGE(AAFwkTag::ABILITYMGR, "load abilityName failed");
295             return;
296         }
297         std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
298         std::string abilityName = jsonObject.at(ABILITY_NAME).get<std::string>();
299         residentProcessInExtremeMemory_.value.emplace_back(std::make_pair(bundleName, abilityName));
300     }
301 }
302 
IsAllowNativeChildProcess(const std::string & appIdentifier)303 bool AppUtils::IsAllowNativeChildProcess(const std::string &appIdentifier)
304 {
305     TAG_LOGD(AAFwkTag::DEFAULT, "appId:%{private}s", appIdentifier.c_str());
306     if (!allowStartNativeProcessApps_.isLoaded) {
307         LoadAllowNativeChildProcessApps();
308         allowStartNativeProcessApps_.isLoaded = true;
309     }
310     auto &apps = allowStartNativeProcessApps_.value;
311     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}zu", apps.size());
312     return std::find(apps.begin(), apps.end(), appIdentifier) != apps.end();
313 }
314 
LoadAllowNativeChildProcessApps()315 void AppUtils::LoadAllowNativeChildProcessApps()
316 {
317     nlohmann::json object;
318     if (!JsonUtils::GetInstance().LoadConfiguration(ALLOW_NATIVE_CHILD_PROCESS_APPS_CONFIG_PATH, object)) {
319         TAG_LOGE(AAFwkTag::ABILITYMGR, "load child process config failed");
320         return;
321     }
322     if (!object.contains(KEY_ALLOW_NATIVE_CHILD_PROCESS_APPS) ||
323         !object.at(KEY_ALLOW_NATIVE_CHILD_PROCESS_APPS).is_array()) {
324         TAG_LOGE(AAFwkTag::ABILITYMGR, "get key invalid");
325         return;
326     }
327 
328     for (auto &item : object.at(KEY_ALLOW_NATIVE_CHILD_PROCESS_APPS).items()) {
329         const nlohmann::json& jsonObject = item.value();
330         if (!jsonObject.contains(KEY_IDENTIFIER) || !jsonObject.at(KEY_IDENTIFIER).is_string()) {
331             TAG_LOGE(AAFwkTag::ABILITYMGR, "load identifier failed");
332             return;
333         }
334         std::string identifier = jsonObject.at(KEY_IDENTIFIER).get<std::string>();
335         allowStartNativeProcessApps_.value.emplace_back(identifier);
336     }
337 }
338 
GetLimitMaximumExtensionsPerProc()339 int32_t AppUtils::GetLimitMaximumExtensionsPerProc()
340 {
341     if (!limitMaximumExtensionsPerProc_.isLoaded) {
342         limitMaximumExtensionsPerProc_.value =
343             system::GetIntParameter<int32_t>(LIMIT_MAXIMUM_EXTENSIONS_OF_PER_PROCESS, DEFAULT_MAX_EXT_PER_PROC);
344         limitMaximumExtensionsPerProc_.isLoaded = true;
345     }
346     TAG_LOGD(AAFwkTag::DEFAULT, "limitMaximumExtensionsPerProc: %{public}d", limitMaximumExtensionsPerProc_.value);
347     return limitMaximumExtensionsPerProc_.value;
348 }
349 
GetLimitMaximumExtensionsPerDevice()350 int32_t AppUtils::GetLimitMaximumExtensionsPerDevice()
351 {
352     if (!limitMaximumExtensionsPerDevice_.isLoaded) {
353         limitMaximumExtensionsPerDevice_.value =
354             system::GetIntParameter<int32_t>(LIMIT_MAXIMUM_EXTENSIONS_OF_PER_DEVICE, DEFAULT_MAX_EXT_PER_DEV);
355         limitMaximumExtensionsPerDevice_.isLoaded = true;
356     }
357     TAG_LOGD(AAFwkTag::DEFAULT, "limitMaximumExtensionsPerDevice: %{public}d", limitMaximumExtensionsPerDevice_.value);
358     return limitMaximumExtensionsPerDevice_.value;
359 }
360 
GetCacheExtensionTypeList()361 std::string AppUtils::GetCacheExtensionTypeList()
362 {
363     std::string cacheExtAbilityTypeList = system::GetParameter(CACHE_EXTENSION_TYPES, "260");
364     TAG_LOGD(AAFwkTag::DEFAULT, "cacheExtAbilityTypeList is %{public}s", cacheExtAbilityTypeList.c_str());
365     return cacheExtAbilityTypeList;
366 }
367 
IsAllowStartAbilityWithoutCallerToken(const std::string & bundleName,const std::string & abilityName)368 bool AppUtils::IsAllowStartAbilityWithoutCallerToken(const std::string& bundleName, const std::string& abilityName)
369 {
370     std::lock_guard lock(startAbilityWithoutCallerTokenMutex_);
371     if (!startAbilityWithoutCallerToken_.isLoaded) {
372         LoadStartAbilityWithoutCallerToken();
373         startAbilityWithoutCallerToken_.isLoaded = true;
374     }
375     TAG_LOGD(AAFwkTag::DEFAULT, "isLoaded: %{public}d", startAbilityWithoutCallerToken_.isLoaded);
376     for (auto &element : startAbilityWithoutCallerToken_.value) {
377         if (bundleName == element.first && abilityName == element.second) {
378             TAG_LOGI(AAFwkTag::DEFAULT, "call");
379             return true;
380         }
381     }
382     return false;
383 }
384 
LoadStartAbilityWithoutCallerToken()385 void AppUtils::LoadStartAbilityWithoutCallerToken()
386 {
387     nlohmann::json object;
388     if (!JsonUtils::GetInstance().LoadConfiguration(
389         START_ABILITY_WITHOUT_CALLERTOKEN_PATH, object, START_ABILITY_WITHOUT_CALLERTOKEN)) {
390         TAG_LOGE(AAFwkTag::DEFAULT, "token list failed");
391         return;
392     }
393     if (!object.contains(START_ABILITY_WITHOUT_CALLERTOKEN_TITLE) ||
394         !object.at(START_ABILITY_WITHOUT_CALLERTOKEN_TITLE).is_array()) {
395         TAG_LOGE(AAFwkTag::DEFAULT, "token config invalid");
396         return;
397     }
398 
399     for (auto &item : object.at(START_ABILITY_WITHOUT_CALLERTOKEN_TITLE).items()) {
400         const nlohmann::json& jsonObject = item.value();
401         if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
402             TAG_LOGE(AAFwkTag::DEFAULT, "load bundleName failed");
403             return;
404         }
405         if (!jsonObject.contains(ABILITY_NAME) || !jsonObject.at(ABILITY_NAME).is_string()) {
406             TAG_LOGE(AAFwkTag::DEFAULT, "load abilityName failed");
407             return;
408         }
409         std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
410         std::string abilityName = jsonObject.at(ABILITY_NAME).get<std::string>();
411         startAbilityWithoutCallerToken_.value.emplace_back(std::make_pair(bundleName, abilityName));
412     }
413 }
414 
GetBrokerDelegateBundleName()415 std::string AppUtils::GetBrokerDelegateBundleName()
416 {
417     if (!brokerDelegateBundleName_.isLoaded) {
418         brokerDelegateBundleName_.value = system::GetParameter(BROKER_DELEGATE_BUNDLE_NAME, "");
419         brokerDelegateBundleName_.isLoaded = true;
420     }
421     TAG_LOGD(AAFwkTag::DEFAULT, "shellAssistantBundleName_ is %{public}s", brokerDelegateBundleName_.value.c_str());
422     return brokerDelegateBundleName_.value;
423 }
424 
GetCollaboratorBrokerUID()425 int32_t AppUtils::GetCollaboratorBrokerUID()
426 {
427     if (!collaboratorBrokerUid_.isLoaded) {
428         collaboratorBrokerUid_.value = system::GetIntParameter(COLLABORATOR_BROKER_UID, DEFAULT_INVALID_VALUE);
429         collaboratorBrokerUid_.isLoaded = true;
430     }
431     TAG_LOGD(AAFwkTag::DEFAULT, "collaboratorBrokerUid_ is %{public}d", collaboratorBrokerUid_.value);
432     return collaboratorBrokerUid_.value;
433 }
434 
GetCollaboratorBrokerReserveUID()435 int32_t AppUtils::GetCollaboratorBrokerReserveUID()
436 {
437     if (!collaboratorBrokerReserveUid_.isLoaded) {
438         collaboratorBrokerReserveUid_.value = system::GetIntParameter(COLLABORATOR_BROKER_RESERVE_UID,
439             DEFAULT_INVALID_VALUE);
440         collaboratorBrokerReserveUid_.isLoaded = true;
441     }
442     TAG_LOGD(AAFwkTag::DEFAULT, "collaboratorBrokerReserveUid_ is %{public}d", collaboratorBrokerReserveUid_.value);
443     return collaboratorBrokerReserveUid_.value;
444 }
445 
MaxChildProcess()446 int32_t AppUtils::MaxChildProcess()
447 {
448     if (!maxChildProcess_.isLoaded) {
449         maxChildProcess_.value =
450             system::GetIntParameter<int32_t>(MAX_CHILD_PROCESS, DEFAULT_MAX_CHILD_PROCESS);
451         maxChildProcess_.isLoaded = true;
452     }
453     TAG_LOGD(AAFwkTag::DEFAULT, "MaxChildProcess: %{public}d", maxChildProcess_.value);
454     return maxChildProcess_.value;
455 }
456 
IsSupportMultiInstance()457 bool AppUtils::IsSupportMultiInstance()
458 {
459     if (!isSupportMultiInstance_.isLoaded) {
460         isSupportMultiInstance_.value = system::GetBoolParameter(SUPPORT_MULTI_INSTANCE, false);
461         isSupportMultiInstance_.isLoaded = true;
462     }
463     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isSupportMultiInstance_.value);
464     return isSupportMultiInstance_.value;
465 }
466 
GetMigrateClientBundleName()467 std::string AppUtils::GetMigrateClientBundleName()
468 {
469     if (!migrateClientBundleName_.isLoaded) {
470         migrateClientBundleName_.value = system::GetParameter(MIGRATE_CLIENT_BUNDLE_NAME, "");
471         migrateClientBundleName_.isLoaded = true;
472     }
473     TAG_LOGD(AAFwkTag::DEFAULT, "migrateClientBundleName_ is %{public}s", migrateClientBundleName_.value.c_str());
474     return migrateClientBundleName_.value;
475 }
476 
IsConnectSupportCrossUser()477 bool AppUtils::IsConnectSupportCrossUser()
478 {
479     std::lock_guard guard(isConnectSupportCrossUserMutex_);
480     if (!isConnectSupportCrossUser_.isLoaded) {
481         isConnectSupportCrossUser_.value = system::GetBoolParameter(CONNECT_SUPPORT_CROSS_USER, false);
482         isConnectSupportCrossUser_.isLoaded = true;
483     }
484     TAG_LOGD(AAFwkTag::DEFAULT, "called %{public}d", isConnectSupportCrossUser_.value);
485     return isConnectSupportCrossUser_.value;
486 }
487 
IsPrepareTerminateEnabled()488 bool AppUtils::IsPrepareTerminateEnabled()
489 {
490     char value[PREPARE_TERMINATE_ENABLE_SIZE] = "false";
491     int retSysParam = GetParameter(PREPARE_TERMINATE_ENABLE_PARAMETER, "false", value, PREPARE_TERMINATE_ENABLE_SIZE);
492     TAG_LOGI(AAFwkTag::DEFAULT,
493         "isPrepareTerminateEnable, %{public}s value=%{public}s", PREPARE_TERMINATE_ENABLE_PARAMETER, value);
494     if (retSysParam > 0 && !std::strcmp(value, "true")) {
495         return true;
496     }
497     return false;
498 }
499 
IsCacheAbilityEnabled()500     bool AppUtils::IsCacheAbilityEnabled()
501     {
502         return system::GetBoolParameter(CACHE_ABILITY_BY_LIST_ENABLE, false);
503     }
504 
LoadCacheAbilityList()505     void AppUtils::LoadCacheAbilityList()
506     {
507         nlohmann::json object;
508         if (!JsonUtils::GetInstance().LoadConfiguration(CACHE_ABILITY_LIST_PATH, object)) {
509             TAG_LOGI(AAFwkTag::ABILITYMGR, "load cache_ability file failed");
510             return;
511         }
512         if (!object.contains(CACHE_PROCESS_NAME) ||
513             !object.at(CACHE_PROCESS_NAME).is_array()) {
514             TAG_LOGE(AAFwkTag::ABILITYMGR, "cache_ability file invalid");
515             return;
516         }
517 
518         for (auto &item : object.at(CACHE_PROCESS_NAME).items()) {
519             const nlohmann::json& jsonObject = item.value();
520             if (!jsonObject.contains(BUNDLE_NAME) || !jsonObject.at(BUNDLE_NAME).is_string()) {
521                 TAG_LOGE(AAFwkTag::ABILITYMGR, "load cache_ability bundleName failed");
522                 return;
523             }
524             if (!jsonObject.contains(ABILITY_NAME) || !jsonObject.at(ABILITY_NAME).is_string()) {
525                 TAG_LOGE(AAFwkTag::ABILITYMGR, "load cache_ability abilityName failed");
526                 return;
527             }
528             std::string bundleName = jsonObject.at(BUNDLE_NAME).get<std::string>();
529             std::string abilityName = jsonObject.at(ABILITY_NAME).get<std::string>();
530             cacheAbilityList_.value.emplace_back(std::make_pair(bundleName, abilityName));
531         }
532     }
533 
IsCacheExtensionAbilityByList(const std::string & bundleName,const std::string & abilityName)534     bool AppUtils::IsCacheExtensionAbilityByList(const std::string& bundleName, const std::string& abilityName)
535     {
536         /* only load once, mutex lock already in caller function */
537         if (!cacheAbilityList_.isLoaded) {
538             LoadCacheAbilityList();
539             cacheAbilityList_.isLoaded = true;
540         }
541 
542         if (cacheAbilityList_.value.empty() || !IsCacheAbilityEnabled()) {
543             return false;
544         }
545 
546         for (auto &element : cacheAbilityList_.value) {
547             if (bundleName == element.first && abilityName == element.second) {
548                 TAG_LOGI(AAFwkTag::DEFAULT, "cache_ability: %{public}s, %{public}s",
549                          bundleName.c_str(), abilityName.c_str());
550                 return true;
551             }
552         }
553         return false;
554     }
555 
556 }  // namespace AAFwk
557 }  // namespace OHOS
558