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 "app_preloader.h"
17
18 #include <string>
19
20 #include "ability_manager_errors.h"
21 #include "in_process_call_wrapper.h"
22 #include "hilog_tag_wrapper.h"
23 #include "hitrace_meter.h"
24 #include "res_sched_client.h"
25 #include "res_type.h"
26 #include "startup_util.h"
27
28 namespace OHOS {
29 namespace AppExecFwk {
AppPreloader(std::shared_ptr<RemoteClientManager> remoteClientManager)30 AppPreloader::AppPreloader(std::shared_ptr<RemoteClientManager> remoteClientManager)
31 {
32 remoteClientManager_ = remoteClientManager;
33 }
34
PreCheck(const std::string & bundleName,PreloadMode preloadMode)35 bool AppPreloader::PreCheck(const std::string &bundleName, PreloadMode preloadMode)
36 {
37 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
38 TAG_LOGD(AAFwkTag::APPMGR, "PreloadApplication PreCheck, bundleName: %{public}s, preloadMode:%{public}d",
39 bundleName.c_str(), preloadMode);
40 if (preloadMode == PreloadMode::PRE_MAKE || preloadMode == PreloadMode::PRELOAD_MODULE) {
41 return true;
42 }
43 int32_t mode = static_cast<int32_t>(preloadMode);
44 auto allow = ResourceSchedule::ResSchedClient::GetInstance().IsAllowedAppPreload(bundleName, mode);
45 if (!allow) {
46 TAG_LOGI(AAFwkTag::APPMGR, "BundleName: %{public}s not allow preload", bundleName.c_str());
47 return false;
48 }
49 return true;
50 }
51
GeneratePreloadRequest(const std::string & bundleName,int32_t userId,int32_t appIndex,PreloadRequest & request)52 int32_t AppPreloader::GeneratePreloadRequest(const std::string &bundleName, int32_t userId, int32_t appIndex,
53 PreloadRequest &request)
54 {
55 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
56 TAG_LOGD(AAFwkTag::APPMGR, "PreloadApplication GeneratePreloadRequest");
57
58 AAFwk::Want launchWant;
59 if (!GetLaunchWant(bundleName, userId, launchWant)) {
60 TAG_LOGE(AAFwkTag::APPMGR, "GetLaunchWant failed");
61 return AAFwk::ERR_TARGET_BUNDLE_NOT_EXIST;
62 }
63
64 AbilityInfo abilityInfo;
65 if (!GetLaunchAbilityInfo(launchWant, userId, abilityInfo)) {
66 TAG_LOGE(AAFwkTag::APPMGR, "GetLaunchAbilityInfo failed");
67 return AAFwk::ERR_GET_LAUNCH_ABILITY_INFO_FAILED;
68 }
69
70 if (!CheckPreloadConditions(abilityInfo)) {
71 TAG_LOGE(AAFwkTag::APPMGR, "CheckPreloadConditions failed");
72 return AAFwk::ERR_CHECK_PRELOAD_CONDITIONS_FAILED;
73 }
74
75 BundleInfo bundleInfo;
76 HapModuleInfo hapModuleInfo;
77 if (!GetBundleAndHapInfo(bundleName, userId, abilityInfo, bundleInfo, hapModuleInfo)) {
78 TAG_LOGE(AAFwkTag::APPMGR, "GetBundleAndHapInfo failed");
79 return AAFwk::GET_BUNDLE_INFO_FAILED;
80 }
81
82 request.abilityInfo = std::make_shared<AbilityInfo>(abilityInfo);
83 request.appInfo = std::make_shared<ApplicationInfo>(abilityInfo.applicationInfo);
84 request.want = std::make_shared<AAFwk::Want>(launchWant);
85 request.bundleInfo = bundleInfo;
86 request.hapModuleInfo = hapModuleInfo;
87 request.appIndex = appIndex;
88
89 return ERR_OK;
90 }
91
GetLaunchWant(const std::string & bundleName,int32_t userId,AAFwk::Want & launchWant)92 bool AppPreloader::GetLaunchWant(const std::string &bundleName, int32_t userId, AAFwk::Want &launchWant)
93 {
94 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
95 auto bundleMgrHelper = GetBundleManagerHelper();
96 if (!bundleMgrHelper) {
97 TAG_LOGE(AAFwkTag::APPMGR, "null bundleMgrHelper");
98 return false;
99 }
100
101 TAG_LOGD(AAFwkTag::APPMGR, "userId: %{public}d, bundleName: %{public}s", userId, bundleName.c_str());
102 auto errCode = IN_PROCESS_CALL(bundleMgrHelper->GetLaunchWantForBundle(bundleName, launchWant, userId));
103 if (errCode != ERR_OK) {
104 TAG_LOGE(AAFwkTag::APPMGR, "errCode: %{public}d", errCode);
105 return false;
106 }
107 return true;
108 }
109
GetLaunchAbilityInfo(const AAFwk::Want & want,int32_t userId,AbilityInfo & abilityInfo)110 bool AppPreloader::GetLaunchAbilityInfo(const AAFwk::Want &want, int32_t userId, AbilityInfo &abilityInfo)
111 {
112 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
113 auto bundleMgrHelper = GetBundleManagerHelper();
114 if (!bundleMgrHelper) {
115 TAG_LOGE(AAFwkTag::APPMGR, "null bundleMgrHelper");
116 return false;
117 }
118
119 TAG_LOGD(AAFwkTag::APPMGR, "userId: %{public}d", userId);
120 auto abilityInfoFlag = AbilityRuntime::StartupUtil::BuildAbilityInfoFlag();
121 if (!IN_PROCESS_CALL(bundleMgrHelper->QueryAbilityInfo(want, abilityInfoFlag, userId, abilityInfo))) {
122 TAG_LOGE(AAFwkTag::APPMGR, "GetLaunchAbilityInfo failed");
123 return false;
124 }
125
126 return true;
127 }
128
GetBundleAndHapInfo(const std::string & bundleName,int32_t userId,const AbilityInfo & abilityInfo,BundleInfo & bundleInfo,HapModuleInfo & hapModuleInfo)129 bool AppPreloader::GetBundleAndHapInfo(const std::string &bundleName, int32_t userId,
130 const AbilityInfo &abilityInfo, BundleInfo &bundleInfo, HapModuleInfo &hapModuleInfo)
131 {
132 HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
133 auto bundleMgrHelper = GetBundleManagerHelper();
134 if (!bundleMgrHelper) {
135 TAG_LOGE(AAFwkTag::APPMGR, "null bundleMgrHelper");
136 return false;
137 }
138
139 TAG_LOGD(AAFwkTag::APPMGR, "userId: %{public}d, bundleName: %{public}s", userId, bundleName.c_str());
140 auto flags = BundleFlag::GET_BUNDLE_DEFAULT | BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION;
141 if (!IN_PROCESS_CALL(bundleMgrHelper->GetBundleInfo(bundleName,
142 static_cast<BundleFlag>(flags),
143 bundleInfo, userId))) {
144 TAG_LOGE(AAFwkTag::APPMGR, "GetBundleInfo failed");
145 return false;
146 }
147
148 if (!IN_PROCESS_CALL(bundleMgrHelper->GetHapModuleInfo(abilityInfo, userId, hapModuleInfo))) {
149 TAG_LOGE(AAFwkTag::APPMGR, "GetHapModuleInfo failed");
150 return false;
151 }
152 return true;
153 }
154
CheckPreloadConditions(const AbilityInfo & abilityInfo)155 bool AppPreloader::CheckPreloadConditions(const AbilityInfo &abilityInfo)
156 {
157 if (abilityInfo.type != AppExecFwk::AbilityType::PAGE || !abilityInfo.isStageBasedModel) {
158 TAG_LOGE(AAFwkTag::APPMGR, "AbilityType is not UIAbility");
159 return false;
160 }
161 ApplicationInfo appInfo = abilityInfo.applicationInfo;
162 if (abilityInfo.name.empty() || appInfo.name.empty()) {
163 TAG_LOGE(AAFwkTag::APPMGR, "abilityInfo or appInfo name is empty");
164 return false;
165 }
166 if (abilityInfo.applicationName != appInfo.name) {
167 TAG_LOGE(AAFwkTag::APPMGR, "abilityInfo and appInfo have different appName");
168 return false;
169 }
170 return true;
171 }
172
GetBundleManagerHelper()173 std::shared_ptr<BundleMgrHelper> AppPreloader::GetBundleManagerHelper()
174 {
175 if (!remoteClientManager_) {
176 TAG_LOGE(AAFwkTag::APPMGR, "null remoteClientManager_");
177 return nullptr;
178 }
179 return remoteClientManager_->GetBundleManagerHelper();
180 }
181 } // namespace AppExecFwk
182 } // namespace OHOS
183