1 /*
2 * Copyright (c) 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 "preload_manager_service.h"
17
18 #include "ability_manager_errors.h"
19 #include "ability_manager_service.h"
20 #include "ability_util.h"
21 #include "app_scheduler.h"
22 #include "app_utils.h"
23 #include "hilog_tag_wrapper.h"
24 #include "in_process_call_wrapper.h"
25 #include "permission_verification.h"
26 #include "process_options.h"
27 #include "start_options.h"
28
29 namespace OHOS {
30 namespace AAFwk {
GetInstance()31 PreloadManagerService &PreloadManagerService::GetInstance()
32 {
33 static PreloadManagerService instance;
34 return instance;
35 }
36
PreloadManagerService()37 PreloadManagerService::PreloadManagerService() {}
38
~PreloadManagerService()39 PreloadManagerService::~PreloadManagerService() {}
40
PreloadApplication(const std::string & bundleName,int32_t userId,int32_t appIndex)41 int32_t PreloadManagerService::PreloadApplication(const std::string &bundleName, int32_t userId, int32_t appIndex)
42 {
43 if (!AppUtils::GetInstance().IsPreloadApplicationEnabled() || appIndex != 0) {
44 TAG_LOGE(AAFwkTag::ABILITYMGR, "preload application not supported");
45 return ERR_CAPABILITY_NOT_SUPPORT;
46 }
47 if (!PermissionVerification::GetInstance()->VerifyPreloadApplicationPermission()) {
48 TAG_LOGE(AAFwkTag::ABILITYMGR, "no preload permission");
49 return ERR_PERMISSION_DENIED;
50 }
51 userId = DelayedSingleton<AbilityManagerService>::GetInstance()->GetValidUserId(userId);
52 if (!DelayedSingleton<AbilityManagerService>::GetInstance()->JudgeMultiUserConcurrency(userId)) {
53 TAG_LOGE(AAFwkTag::ABILITYMGR, "multi-user non-concurrent unsatisfied:%{public}d", ERR_CROSS_USER);
54 return ERR_CROSS_USER;
55 }
56
57 bool isExist = false;
58 int32_t ret = ERR_OK;
59 CHECK_TRUE_RETURN_RET((ret = DelayedSingleton<AppScheduler>::GetInstance()->CheckPreloadAppRecordExist(
60 bundleName, userId, appIndex, isExist)) != ERR_OK, ret, "CheckPreloadAppRecordExist failed");
61 CHECK_TRUE_RETURN_RET(isExist, ERR_PRELOAD_APP_RECORD_ALREADY_EXIST, "already started");
62
63 auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
64 CHECK_POINTER_AND_RETURN(bundleMgrHelper, INNER_ERR);
65
66 Want launchWant;
67 auto errCode = IN_PROCESS_CALL(bundleMgrHelper->GetLaunchWantForBundle(bundleName, launchWant, userId));
68 if (errCode != ERR_OK) {
69 TAG_LOGE(AAFwkTag::ABILITYMGR, "getLaunchWantForBundle returns %{public}d", errCode);
70 return errCode;
71 }
72
73 AppExecFwk::AbilityInfo abilityInfo;
74 CHECK_TRUE_RETURN_RET(!IN_PROCESS_CALL(bundleMgrHelper->QueryAbilityInfo(launchWant,
75 AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION, userId, abilityInfo)),
76 RESOLVE_ABILITY_ERR, "failed to get abilityInfo");
77 AppExecFwk::AppPreloadPhase appPreloadPhase = abilityInfo.applicationInfo.appPreloadPhase;
78 if (appPreloadPhase <= AppExecFwk::AppPreloadPhase::DEFAULT ||
79 appPreloadPhase > AppExecFwk::AppPreloadPhase::WINDOW_STAGE_CREATED) {
80 TAG_LOGE(AAFwkTag::ABILITYMGR, "invalid preload phase:%{public}d", static_cast<int32_t>(appPreloadPhase));
81 return ERR_INVALID_APP_PRELOAD_PHASE;
82 }
83 if (appPreloadPhase <= AppExecFwk::AppPreloadPhase::ABILITY_STAGE_CREATED) {
84 TAG_LOGI(AAFwkTag::ABILITYMGR, "preload to phase:%{public}d", static_cast<int32_t>(appPreloadPhase));
85 AppExecFwk::PreloadPhase preloadPhase = static_cast<AppExecFwk::PreloadPhase>(appPreloadPhase);
86 return DelayedSingleton<AppScheduler>::GetInstance()->PreloadApplicationByPhase(
87 bundleName, userId, appIndex, preloadPhase);
88 }
89 TAG_LOGI(AAFwkTag::ABILITYMGR, "preload to window stage create");
90 StartOptions options;
91 options.processOptions = std::make_shared<ProcessOptions>();
92 options.processOptions->startupVisibility = StartupVisibility::STARTUP_HIDE;
93 options.processOptions->isPreloadStart = true;
94 return DelayedSingleton<AbilityManagerService>::GetInstance()->StartAbility(launchWant, options, nullptr, userId);
95 }
96 } // namespace AAFwk
97 } // namespace OHOS
98