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 by RSS", 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, "PreloadApplication 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, "PreloadApplication GetLaunchAbilityInfo failed");
67         return AAFwk::ERR_GET_LAUNCH_ABILITY_INFO_FAILED;
68     }
69 
70     if (!CheckPreloadConditions(abilityInfo)) {
71         TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication 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, "PreloadApplication 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, "bundleMgrHelper is nullptr.");
98         return false;
99     }
100 
101     auto errCode = IN_PROCESS_CALL(bundleMgrHelper->GetLaunchWantForBundle(bundleName, launchWant, userId));
102     if (errCode != ERR_OK) {
103         TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication GetLaunchWantForBundle failed, errCode: %{public}d.", errCode);
104         return false;
105     }
106     return true;
107 }
108 
GetLaunchAbilityInfo(const AAFwk::Want & want,int32_t userId,AbilityInfo & abilityInfo)109 bool AppPreloader::GetLaunchAbilityInfo(const AAFwk::Want &want, int32_t userId, AbilityInfo &abilityInfo)
110 {
111     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
112     auto bundleMgrHelper = GetBundleManagerHelper();
113     if (!bundleMgrHelper) {
114         TAG_LOGE(AAFwkTag::APPMGR, "bundleMgrHelper is nullptr.");
115         return false;
116     }
117 
118     auto abilityInfoFlag = AbilityRuntime::StartupUtil::BuildAbilityInfoFlag();
119     if (!IN_PROCESS_CALL(bundleMgrHelper->QueryAbilityInfo(want, abilityInfoFlag, userId, abilityInfo))) {
120         TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication GetLaunchAbilityInfo failed.");
121         return false;
122     }
123 
124     return true;
125 }
126 
GetBundleAndHapInfo(const std::string & bundleName,int32_t userId,const AbilityInfo & abilityInfo,BundleInfo & bundleInfo,HapModuleInfo & hapModuleInfo)127 bool AppPreloader::GetBundleAndHapInfo(const std::string &bundleName, int32_t userId,
128     const AbilityInfo &abilityInfo, BundleInfo &bundleInfo, HapModuleInfo &hapModuleInfo)
129 {
130     HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
131     auto bundleMgrHelper = GetBundleManagerHelper();
132     if (!bundleMgrHelper) {
133         TAG_LOGE(AAFwkTag::APPMGR, "bundleMgrHelper is nullptr.");
134         return false;
135     }
136 
137     auto flags = BundleFlag::GET_BUNDLE_DEFAULT | BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION;
138     if (!IN_PROCESS_CALL(bundleMgrHelper->GetBundleInfo(bundleName,
139         static_cast<BundleFlag>(flags),
140         bundleInfo, userId))) {
141         TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication GetBundleInfo failed.");
142         return false;
143     }
144 
145     if (!IN_PROCESS_CALL(bundleMgrHelper->GetHapModuleInfo(abilityInfo, userId, hapModuleInfo))) {
146         TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication GetHapModuleInfo failed.");
147         return false;
148     }
149     return true;
150 }
151 
CheckPreloadConditions(const AbilityInfo & abilityInfo)152 bool AppPreloader::CheckPreloadConditions(const AbilityInfo &abilityInfo)
153 {
154     if (abilityInfo.type != AppExecFwk::AbilityType::PAGE || !abilityInfo.isStageBasedModel) {
155         TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication Launch Ability type is not UIAbility");
156         return false;
157     }
158     ApplicationInfo appInfo = abilityInfo.applicationInfo;
159     if (abilityInfo.name.empty() || appInfo.name.empty()) {
160         TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication abilityInfo or appInfo name is empty");
161         return false;
162     }
163     if (abilityInfo.applicationName != appInfo.name) {
164         TAG_LOGE(AAFwkTag::APPMGR, "PreloadApplication abilityInfo and appInfo have different appName, \
165         don't load for it");
166         return false;
167     }
168     return true;
169 }
170 
GetBundleManagerHelper()171 std::shared_ptr<BundleMgrHelper> AppPreloader::GetBundleManagerHelper()
172 {
173     if (!remoteClientManager_) {
174         TAG_LOGE(AAFwkTag::APPMGR, "remoteClientManager_ is nullptr.");
175         return nullptr;
176     }
177     return remoteClientManager_->GetBundleManagerHelper();
178 }
179 }  // namespace AppExecFwk
180 }  // namespace OHOS
181