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