• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "bundle_info_creator.h"
17 
18 #include "ability_info_utils.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_info_utils.h"
21 #include "bundle_manager_service.h"
22 #include "bundle_res_transform.h"
23 #include "bundle_util.h"
24 #include "bundle_log.h"
25 #include "module_info_utils.h"
26 #include "securec.h"
27 #include "utils.h"
28 
29 namespace OHOS {
SaveBundleInfo(const BundleProfile & bundleProfile,BundleInfo ** bundleInfo)30 uint8_t BundleInfoCreator::SaveBundleInfo(const BundleProfile &bundleProfile, BundleInfo **bundleInfo)
31 {
32     *bundleInfo = reinterpret_cast<BundleInfo *>(AdapterMalloc(sizeof(BundleInfo)));
33     if (*bundleInfo == nullptr) {
34         HILOG_ERROR(HILOG_MODULE_APP, "malloc bundleInfo fail!");
35         return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
36     }
37 
38     if (memset_s(*bundleInfo, sizeof(BundleInfo), 0, sizeof(BundleInfo)) != EOK) {
39         HILOG_ERROR(HILOG_MODULE_APP, "memset bundleInfo fail!");
40         AdapterFree(*bundleInfo);
41         *bundleInfo = nullptr;
42         return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
43     }
44     std::string installDirPath;
45     std::string dataDirPath;
46     BundleInfo *info = ManagerService::GetInstance().QueryBundleInfo(bundleProfile.bundleName);
47     if (info != nullptr) {
48         size_t index = std::string(info->codePath).find_last_of(PATH_SEPARATOR);
49         if (index == std::string::npos) {
50             HILOG_ERROR(HILOG_MODULE_APP, "codePath is invalid!");
51             return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
52         }
53         installDirPath = std::string(info->codePath).substr(0, index);
54         index = std::string(info->dataPath).find_last_of(PATH_SEPARATOR);
55         if (index == std::string::npos) {
56             HILOG_ERROR(HILOG_MODULE_APP, "dataPath is invalid!");
57             return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
58         }
59         dataDirPath = std::string(info->dataPath).substr(0, index);
60     } else {
61         installDirPath = ManagerService::GetInstance().GetCodeDirPath();
62         dataDirPath = ManagerService::GetInstance().GetDataDirPath();
63     }
64 
65     if (!SetBundleInfo(bundleProfile, installDirPath, dataDirPath, *bundleInfo)) {
66         BundleInfoUtils::FreeBundleInfo(*bundleInfo);
67         *bundleInfo = nullptr;
68         return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
69     }
70     return ERR_OK;
71 }
72 
CreateBundleInfo(const BundleProfile & bundleProfile,const std::string & installDirPath,const std::string & dataDirPath,const BundleRes & bundleRes)73 BundleInfo *BundleInfoCreator::CreateBundleInfo(const BundleProfile &bundleProfile, const std::string &installDirPath,
74     const std::string &dataDirPath, const BundleRes &bundleRes)
75 {
76     BundleInfo *bundleInfo = reinterpret_cast<BundleInfo *>(AdapterMalloc(sizeof(BundleInfo)));
77     if (bundleInfo == nullptr) {
78         HILOG_ERROR(HILOG_MODULE_APP, "malloc bundleInfo fail when create bundleInfo!");
79         return nullptr;
80     }
81 
82     int32_t ret = memset_s(bundleInfo, sizeof(BundleInfo), 0, sizeof(BundleInfo));
83     if (ret != EOK) {
84         HILOG_ERROR(HILOG_MODULE_APP, "memset bundleInfo fail when create bundleInfo!");
85         AdapterFree(bundleInfo);
86         return nullptr;
87     }
88 
89     if (!SetBundleInfo(bundleProfile, installDirPath, dataDirPath, bundleInfo)) {
90         HILOG_ERROR(HILOG_MODULE_APP, "set bundleInfo fail when create bundleInfo!");
91         BundleInfoUtils::FreeBundleInfo(bundleInfo);
92         return nullptr;
93     }
94 
95     uint8_t errorCode = BundleResTransform::ConvertResInfoToBundleInfo(bundleInfo->codePath, bundleRes, bundleInfo);
96     if (errorCode != ERR_OK) {
97         HILOG_ERROR(HILOG_MODULE_APP, "convert resInfo to bundleInfo fail when create bundleInfo!");
98         BundleInfoUtils::FreeBundleInfo(bundleInfo);
99         return nullptr;
100     }
101     return bundleInfo;
102 }
103 
SetBundleInfo(const BundleProfile & bundleProfile,const std::string & installDirPath,const std::string & dataDirPath,BundleInfo * bundleInfo)104 bool BundleInfoCreator::SetBundleInfo(const BundleProfile &bundleProfile, const std::string &installDirPath,
105     const std::string &dataDirPath, BundleInfo *bundleInfo)
106 {
107     if (bundleInfo == nullptr || installDirPath.empty() || dataDirPath.empty()) {
108         return false;
109     }
110     bundleInfo->isKeepAlive = bundleProfile.isKeepAlive;
111     bundleInfo->isNativeApp = bundleProfile.isNativeApp;
112     bundleInfo->versionCode = bundleProfile.profileVersion.versionCode;
113     bundleInfo->compatibleApi = bundleProfile.profileApiVersion.minApiVersion;
114     if (bundleProfile.profileApiVersion.maxApiVersion == 0) {
115         bundleInfo->targetApi = bundleInfo->compatibleApi;
116     } else {
117         bundleInfo->targetApi = bundleProfile.profileApiVersion.maxApiVersion;
118     }
119     // set codePath and dataPath
120     std::string codePath = installDirPath + PATH_SEPARATOR + bundleProfile.bundleName;
121     std::string dataPath = dataDirPath + PATH_SEPARATOR + bundleProfile.bundleName;
122     if (codePath.size() > PATH_LENGTH || dataPath.size() > PATH_LENGTH) {
123         HILOG_ERROR(HILOG_MODULE_APP, "codePath or dataPath length exceed max value!");
124         return false;
125     }
126 
127     if (bundleProfile.iconPath != nullptr && !BundleUtil::StartWith(bundleProfile.iconPath,
128         DEFAULT_ICON_SETTING_BEGIN)) {
129         std::string iconPath = codePath + PATH_SEPARATOR + bundleProfile.moduleInfo.moduleName + PATH_SEPARATOR +
130             bundleProfile.iconPath;
131         if (iconPath.size() > PATH_LENGTH) {
132             HILOG_ERROR(HILOG_MODULE_APP, "iconPath length exceed max value!");
133             return false;
134         }
135         if (!BundleInfoUtils::SetBundleInfoBigIconPath(bundleInfo, iconPath.c_str())) {
136             HILOG_ERROR(HILOG_MODULE_APP, "set iconPath in bundleInfo fail!");
137             return false;
138         }
139     }
140 
141     if ((bundleProfile.label != nullptr) && !BundleUtil::StartWith(bundleProfile.label, DEFAULT_LABEL_SETTING)) {
142         if (!BundleInfoUtils::SetBundleInfoLabel(bundleInfo, bundleProfile.label)) {
143             HILOG_ERROR(HILOG_MODULE_APP, "set label in bundleInfo fail!");
144             return false;
145         }
146     }
147 
148     if (!BundleInfoUtils::SetBundleInfoBundleName(bundleInfo, bundleProfile.bundleName) ||
149         !BundleInfoUtils::SetBundleInfoVersionName(bundleInfo, bundleProfile.profileVersion.versionName) ||
150         !BundleInfoUtils::SetBundleInfoCodePath(bundleInfo, codePath.c_str()) ||
151         !BundleInfoUtils::SetBundleInfoDataPath(bundleInfo, dataPath.c_str()) ||
152         (bundleProfile.vendor != nullptr && !BundleInfoUtils::SetBundleInfoVendor(bundleInfo, bundleProfile.vendor)) ||
153         !SetModuleInfos(bundleProfile, bundleInfo) ||
154         (bundleProfile.numOfAbility != 0 && !SetAbilityInfos(bundleProfile, codePath, bundleInfo))) {
155         HILOG_ERROR(HILOG_MODULE_APP, "SetBundleInfo fail!");
156         return false;
157     }
158     return true;
159 }
160 
SetModuleInfos(const BundleProfile & bundleProfile,BundleInfo * bundleInfo)161 bool BundleInfoCreator::SetModuleInfos(const BundleProfile &bundleProfile, BundleInfo *bundleInfo)
162 {
163     if (bundleInfo == nullptr) {
164         return false;
165     }
166     bundleInfo->moduleInfos = reinterpret_cast<ModuleInfo *>(AdapterMalloc(sizeof(ModuleInfo)));
167     if (bundleInfo->moduleInfos == nullptr) {
168         return false;
169     }
170 
171     if (memset_s(bundleInfo->moduleInfos, sizeof(ModuleInfo), 0, sizeof(ModuleInfo)) != EOK) {
172         HILOG_ERROR(HILOG_MODULE_APP, "memset ModuleInfo fail!");
173         AdapterFree(bundleInfo->moduleInfos);
174         bundleInfo->moduleInfos = nullptr;
175         return false;
176     }
177 
178     bundleInfo->numOfModule = 1;
179     if (!ModuleInfoUtils::SetModuleInfoDeviceType(bundleInfo->moduleInfos,
180         const_cast<char **>(bundleProfile.moduleInfo.deviceType), DEVICE_TYPE_SIZE)) {
181         HILOG_ERROR(HILOG_MODULE_APP, "SetModuleInfoDeviceType fail!");
182         return false;
183     }
184     if (!ModuleInfoUtils::SetModuleInfoMetaData(bundleInfo->moduleInfos,
185         const_cast<MetaData **>(bundleProfile.moduleInfo.metaData), METADATA_SIZE)) {
186         HILOG_ERROR(HILOG_MODULE_APP, "SetModuleInfoMetaData fail!");
187         return false;
188     }
189 
190     if (bundleProfile.moduleInfo.description != nullptr &&
191         !BundleUtil::StartWith(bundleProfile.moduleInfo.description, DEFAULT_DESC_SETTING)) {
192         if (!ModuleInfoUtils::SetModuleInfoDescription(bundleInfo->moduleInfos,
193             bundleProfile.moduleInfo.description)) {
194             HILOG_ERROR(HILOG_MODULE_APP, "SetModuleInfo description fail!");
195             return false;
196         }
197     }
198     if ((bundleProfile.moduleInfo.name != nullptr &&
199         !ModuleInfoUtils::SetModuleInfoName(bundleInfo->moduleInfos, bundleProfile.moduleInfo.name)) ||
200         !ModuleInfoUtils::SetModuleInfoModuleName(bundleInfo->moduleInfos, bundleProfile.moduleInfo.moduleName) ||
201         !ModuleInfoUtils::SetModuleInfoModuleType(bundleInfo->moduleInfos, bundleProfile.moduleInfo.moduleType)) {
202         HILOG_ERROR(HILOG_MODULE_APP, "Set name or moduleName or moduleType or description in ModuleInfo fail!");
203         return false;
204     }
205     bundleInfo->moduleInfos->isDeliveryInstall = bundleProfile.moduleInfo.isDeliveryInstall;
206     return true;
207 }
208 
SetAbilityInfos(const BundleProfile & bundleProfile,const std::string & codePath,BundleInfo * bundleInfo)209 bool BundleInfoCreator::SetAbilityInfos(const BundleProfile &bundleProfile, const std::string &codePath,
210     BundleInfo *bundleInfo)
211 {
212     if (bundleInfo == nullptr || bundleProfile.numOfAbility == 0) {
213         return false;
214     }
215     bundleInfo->numOfAbility = bundleProfile.numOfAbility;
216     bundleInfo->abilityInfos = reinterpret_cast<AbilityInfo *>(AdapterMalloc(sizeof(AbilityInfo) *
217         bundleInfo->numOfAbility));
218     if (bundleInfo->abilityInfos == nullptr) {
219         return false;
220     }
221     int32_t ret = memset_s(bundleInfo->abilityInfos, sizeof(AbilityInfo) * bundleInfo->numOfAbility, 0,
222         sizeof(AbilityInfo) * bundleInfo->numOfAbility);
223     if (ret != EOK) {
224         HILOG_ERROR(HILOG_MODULE_APP, "memset AbilityInfo fail!");
225         return false;
226     }
227 
228     for (int32_t i = 0; i < bundleInfo->numOfAbility; i++) {
229         if (!SetAbilityInfo(bundleProfile, codePath, bundleInfo, i)) {
230             return false;
231         }
232     }
233     return true;
234 }
235 
SetAbilityInfo(const BundleProfile & bundleProfile,const std::string & codePath,BundleInfo * bundleInfo,uint32_t index)236 bool BundleInfoCreator::SetAbilityInfo(const BundleProfile &bundleProfile, const std::string &codePath,
237     BundleInfo *bundleInfo, uint32_t index)
238 {
239     if (bundleInfo == nullptr) {
240         return false;
241     }
242 
243     AbilityInfo *abilityInfo = bundleInfo->abilityInfos + index;
244     if (abilityInfo == nullptr) {
245         return false;
246     }
247 
248     if (bundleProfile.abilityInfos[index].iconPath != nullptr &&
249         !BundleUtil::StartWith(bundleProfile.abilityInfos[index].iconPath, DEFAULT_ICON_SETTING_BEGIN)) {
250         std::string iconPath = codePath + PATH_SEPARATOR + bundleProfile.moduleInfo.moduleName + PATH_SEPARATOR +
251             bundleProfile.abilityInfos[index].iconPath;
252         if (iconPath.size() > PATH_LENGTH) {
253             HILOG_ERROR(HILOG_MODULE_APP, "ability iconPath length exceed max value!");
254             return false;
255         }
256         if (!AbilityInfoUtils::SetAbilityInfoIconPath(abilityInfo, iconPath.c_str())) {
257             HILOG_ERROR(HILOG_MODULE_APP, "Set iconPath in AbilityInfos fail!");
258             return false;
259         }
260     }
261 
262     if (bundleProfile.abilityInfos[index].description != nullptr &&
263         !BundleUtil::StartWith(bundleProfile.abilityInfos[index].description, DEFAULT_DESC_SETTING)) {
264         if (!AbilityInfoUtils::SetAbilityInfoDescription(abilityInfo, bundleProfile.abilityInfos[index].description)) {
265             HILOG_ERROR(HILOG_MODULE_APP, "Set description in AbilityInfos fail!");
266             return false;
267         }
268     }
269 
270     if (bundleProfile.abilityInfos[index].label != nullptr &&
271         !BundleUtil::StartWith(bundleProfile.abilityInfos[index].label, DEFAULT_LABEL_SETTING)) {
272         if (!AbilityInfoUtils::SetAbilityInfoLabel(abilityInfo, bundleProfile.abilityInfos[index].label)) {
273             HILOG_ERROR(HILOG_MODULE_APP, "Set label in AbilityInfos fail!");
274             return false;
275         }
276     }
277 
278     abilityInfo->isVisible = bundleProfile.abilityInfos[index].isVisible;
279     abilityInfo->abilityType = bundleProfile.abilityInfos[index].abilityType;
280     abilityInfo->launchMode = bundleProfile.abilityInfos[index].launchMode;
281     if (!AbilityInfoUtils::SetAbilityInfoBundleName(abilityInfo, bundleProfile.bundleName) ||
282         !AbilityInfoUtils::SetAbilityInfoModuleName(abilityInfo, bundleProfile.moduleInfo.moduleName) ||
283         !AbilityInfoUtils::SetAbilityInfoName(abilityInfo, bundleProfile.abilityInfos[index].name)) {
284         HILOG_ERROR(HILOG_MODULE_APP, "Set other value in AbilityInfos fail!");
285         return false;
286     }
287     return true;
288 }
289 } // namespace OHOS