• 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_res_transform.h"
17 
18 #include "ability_info_utils.h"
19 #include "appexecfwk_errors.h"
20 #include "bundle_info_utils.h"
21 #include "bundle_util.h"
22 #include "global.h"
23 #include "log.h"
24 #include "module_info_utils.h"
25 
26 namespace OHOS {
ConvertResInfoToBundleInfo(const std::string & path,const BundleRes & bundleRes,BundleInfo * bundleInfo)27 uint8_t BundleResTransform::ConvertResInfoToBundleInfo(const std::string &path, const BundleRes &bundleRes,
28     BundleInfo *bundleInfo)
29 {
30     if (bundleInfo == nullptr) {
31         return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
32     }
33 
34     if (bundleRes.totalNumOfAbilityRes == 0 || bundleInfo->isNativeApp) {
35         return ERR_OK;
36     }
37 
38     char *des = nullptr;
39     std::string resPath = path + PATH_SEPARATOR + bundleInfo->moduleInfos[0].moduleName + ASSETS +
40         bundleInfo->moduleInfos[0].moduleName + RESOURCES_INDEX;
41     if (bundleRes.moduleDescriptionId > 0) {
42         if (!BundleUtil::IsFile(resPath.c_str())) {
43             HILOG_ERROR(HILOG_MODULE_APP, "resource index is not exists!");
44             return ERR_APPEXECFWK_INSTALL_FAILED_RESOURCE_INDEX_NOT_EXISTS;
45         }
46         if (GLOBAL_GetValueById(bundleRes.moduleDescriptionId, resPath.c_str(), &des) < 0) {
47             HILOG_ERROR(HILOG_MODULE_APP, "get moduleInfo description resId fail!");
48             return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_DESCRIPTION_RES_ERROR;
49         }
50         if (!ModuleInfoUtils::SetModuleInfoDescription(bundleInfo->moduleInfos, des)) {
51             AdapterFree(des);
52             HILOG_ERROR(HILOG_MODULE_APP, "set moduleInfo description resId fail!");
53             return ERR_APPEXECFWK_INSTALL_FAILED_INTERNAL_ERROR;
54         }
55         AdapterFree(des);
56     }
57 
58     for (uint32_t i = 0; i < bundleRes.totalNumOfAbilityRes; i++) {
59         if (bundleRes.abilityRes[i].iconId > 0 &&
60             !ConvertIconResToBundleInfo(resPath, bundleRes.abilityRes[i].iconId, bundleInfo, i)) {
61             return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_ICON_RES_ERROR;
62         }
63         if (bundleRes.abilityRes[i].labelId > 0 &&
64             !ConvertLableResToBundleInfo(resPath, bundleRes.abilityRes[i].labelId, bundleInfo, i)) {
65             return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_LABEL_RES_ERROR;
66         }
67         if (bundleRes.abilityRes[i].descriptionId > 0 &&
68             !ConvertDesResIdToBundleInfo(resPath, bundleRes.abilityRes[i].descriptionId, bundleInfo, i)) {
69             return ERR_APPEXECFWK_INSTALL_FAILED_PARSE_DESCRIPTION_RES_ERROR;
70         }
71     }
72     return ERR_OK;
73 }
74 
ConvertIconResToBundleInfo(const std::string & path,uint32_t iconId,BundleInfo * bundleInfo,uint32_t index)75 bool BundleResTransform::ConvertIconResToBundleInfo(const std::string &path, uint32_t iconId, BundleInfo *bundleInfo,
76     uint32_t index)
77 {
78     if (path.empty() || bundleInfo == nullptr) {
79         return false;
80     }
81     if (!BundleUtil::IsFile(path.c_str())) {
82         return false;
83     }
84 
85     char *relativeIconPath = nullptr;
86     if (GLOBAL_GetValueById(iconId, path.c_str(), &relativeIconPath) != 0) {
87         HILOG_ERROR(HILOG_MODULE_APP, "get icon resId fail!");
88         return false;
89     }
90 
91     std::string iconPath = std::string(bundleInfo->codePath) + PATH_SEPARATOR + bundleInfo->moduleInfos[0].moduleName +
92         ASSETS + relativeIconPath;
93     if (!BundleUtil::IsFile(iconPath.c_str())) {
94         HILOG_ERROR(HILOG_MODULE_APP, "icon is not exists!");
95         AdapterFree(relativeIconPath);
96         return false;
97     }
98 
99     if (index == 0) {
100         if (!BundleInfoUtils::SetBundleInfoBigIconPath(bundleInfo, iconPath.c_str())) {
101             HILOG_ERROR(HILOG_MODULE_APP, "set icon resId in bundleInfo fail!");
102             AdapterFree(relativeIconPath);
103             return false;
104         }
105     }
106     if (!AbilityInfoUtils::SetAbilityInfoIconPath(bundleInfo->abilityInfos + index, iconPath.c_str())) {
107         HILOG_ERROR(HILOG_MODULE_APP, "set icon resId in abilityInfo fail!");
108         AdapterFree(relativeIconPath);
109         return false;
110     }
111     AdapterFree(relativeIconPath);
112     return true;
113 }
114 
ConvertLableResToBundleInfo(const std::string & path,uint32_t labelId,BundleInfo * bundleInfo,uint32_t index)115 bool BundleResTransform::ConvertLableResToBundleInfo(const std::string &path, uint32_t labelId, BundleInfo *bundleInfo,
116     uint32_t index)
117 {
118     if (path.empty() || bundleInfo == nullptr) {
119         return false;
120     }
121     if (!BundleUtil::IsFile(path.c_str())) {
122         return false;
123     }
124 
125     char *label = nullptr;
126     if (GLOBAL_GetValueById(labelId, path.c_str(), &label) != 0) {
127         HILOG_ERROR(HILOG_MODULE_APP, "get laebl resId fail!");
128         return false;
129     }
130 
131     if (index == 0) {
132         if (!BundleInfoUtils::SetBundleInfoLabel(bundleInfo, label)) {
133             HILOG_ERROR(HILOG_MODULE_APP, "set label resId in bundleInfo fail!");
134             AdapterFree(label);
135             return false;
136         }
137     }
138     if (!AbilityInfoUtils::SetAbilityInfoLabel(bundleInfo->abilityInfos + index, label)) {
139         HILOG_ERROR(HILOG_MODULE_APP, "set label resId in abilityInfo fail!");
140         AdapterFree(label);
141         return false;
142     }
143     AdapterFree(label);
144     return true;
145 }
146 
ConvertDesResIdToBundleInfo(const std::string & path,uint32_t desId,BundleInfo * bundleInfo,uint32_t index)147 bool BundleResTransform::ConvertDesResIdToBundleInfo(const std::string &path, uint32_t desId, BundleInfo *bundleInfo,
148     uint32_t index)
149 {
150     if (path.empty() || bundleInfo == nullptr) {
151         return false;
152     }
153     if (!BundleUtil::IsFile(path.c_str())) {
154         return false;
155     }
156 
157     char *description = nullptr;
158     if (GLOBAL_GetValueById(desId, path.c_str(), &description) != 0) {
159         HILOG_ERROR(HILOG_MODULE_APP, "get description resId fail!");
160         return false;
161     }
162 
163     if (!AbilityInfoUtils::SetAbilityInfoDescription(bundleInfo->abilityInfos + index, description)) {
164         HILOG_ERROR(HILOG_MODULE_APP, "set description resId in abilityInfo fail!");
165         AdapterFree(description);
166         return false;
167     }
168     AdapterFree(description);
169     return true;
170 }
171 } // namespace OHOS