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 #include <vector>
16 #include <shared_mutex>
17
18 #include "bundle_manager.h"
19 #include "bundle_manager_log.h"
20 #include "bundle_info.h"
21 #include "bundle_mgr_proxy.h"
22 #include "common_func.h"
23 #include "bundle_error.h"
24 #include "bundle_manager_sync.h"
25 #include "extension_ability_info.h"
26 #include "ability_info.h"
27 #include "bundle_mgr_client.h"
28
29 namespace OHOS {
30 namespace CJSystemapi {
31
32 namespace BundleManager {
33
GetBundleInfoForSelf(int32_t bundleFlags)34 AppExecFwk::BundleInfo BundleManagerImpl::GetBundleInfoForSelf(int32_t bundleFlags)
35 {
36 LOGI("BundleManagerImpl::GetBundleInfoForSelf inter");
37 auto iBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
38 AppExecFwk::BundleInfo bundleInfo;
39 iBundleMgr->GetBundleInfoForSelf(bundleFlags, bundleInfo);
40 return bundleInfo;
41 }
42
VerifyAbc(std::vector<std::string> abcPaths,bool flag)43 int32_t BundleManagerImpl::VerifyAbc(std::vector<std::string> abcPaths, bool flag)
44 {
45 auto verifyManager = AppExecFwk::CommonFunc::GetVerifyManager();
46 if (verifyManager == nullptr) {
47 LOGE("VerifyAbc failed due to iBundleMgr is null");
48 return ERROR_BUNDLE_SERVICE_EXCEPTION;
49 }
50
51 ErrCode ret = verifyManager->Verify(abcPaths);
52 if (ret == ERR_OK && flag) {
53 verifyManager->RemoveFiles(abcPaths);
54 }
55 return AppExecFwk::CommonFunc::ConvertErrCode(ret);
56 }
57
checkExtensionAbilityInfoExist(const std::string & abilityName,const AppExecFwk::ExtensionAbilityInfo abilityInfo,AppExecFwk::ExtensionAbilityInfo & targetAbilityInfo)58 int32_t checkExtensionAbilityInfoExist(const std::string& abilityName,
59 const AppExecFwk::ExtensionAbilityInfo abilityInfo,
60 AppExecFwk::ExtensionAbilityInfo& targetAbilityInfo)
61 {
62 if (abilityInfo.name == abilityName) {
63 if (!abilityInfo.enabled) {
64 LOGE("checkExtensionAbilityInfoExist failed by ability disabled");
65 return ERROR_ABILITY_IS_DISABLED;
66 }
67 targetAbilityInfo = abilityInfo;
68 return ERR_OK;
69 }
70 return ERROR_ABILITY_NOT_EXIST;
71 }
72
checkExtensionName(const AppExecFwk::ExtensionAbilityInfo & extensionInfo,const std::string & moduleName,const std::string & abilityName,AppExecFwk::ExtensionAbilityInfo & targetExtensionInfo)73 std::tuple<bool, int32_t> checkExtensionName(const AppExecFwk::ExtensionAbilityInfo& extensionInfo,
74 const std::string& moduleName, const std::string& abilityName,
75 AppExecFwk::ExtensionAbilityInfo& targetExtensionInfo)
76 {
77 bool flag = false;
78 if (extensionInfo.moduleName == moduleName) {
79 flag = true;
80 int32_t res = checkExtensionAbilityInfoExist(abilityName, extensionInfo, targetExtensionInfo);
81 if (res != ERROR_ABILITY_NOT_EXIST) {
82 return {flag, res};
83 }
84 }
85 return {flag, ERROR_ABILITY_NOT_EXIST};
86 }
87
CheckExtensionFromBundleInfo(const AppExecFwk::BundleInfo & bundleInfo,const std::string & abilityName,const std::string & moduleName,AppExecFwk::ExtensionAbilityInfo & targetExtensionInfo)88 ErrCode CheckExtensionFromBundleInfo(const AppExecFwk::BundleInfo& bundleInfo, const std::string& abilityName,
89 const std::string& moduleName, AppExecFwk::ExtensionAbilityInfo& targetExtensionInfo)
90 {
91 bool flag = false;
92 int32_t res = SUCCESS_CODE;
93 for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
94 for (const auto& extensionInfo : hapModuleInfo.extensionInfos) {
95 std::tie(flag, res) = checkExtensionName(extensionInfo, moduleName, abilityName, targetExtensionInfo);
96 if (flag == true && res != ERROR_ABILITY_NOT_EXIST) {
97 return res;
98 }
99 }
100 }
101 if (flag) {
102 return ERROR_ABILITY_NOT_EXIST;
103 } else {
104 return ERROR_MODULE_NOT_EXIST;
105 }
106 }
107
GetProfileByExtensionAbility(std::string moduleName,std::string extensionAbilityName,char * metadataName)108 std::tuple<int32_t, std::vector<std::string>> BundleManagerImpl::GetProfileByExtensionAbility(
109 std::string moduleName, std::string extensionAbilityName, char* metadataName)
110 {
111 if (moduleName.empty()) {
112 LOGE("GetProfileByExtensionAbility failed due to empty moduleName");
113 return {ERROR_MODULE_NOT_EXIST, {}};
114 }
115
116 if (extensionAbilityName.empty()) {
117 LOGE("GetProfileByExtensionAbility failed due to empty extensionAbilityName");
118 return {ERROR_ABILITY_NOT_EXIST, {}};
119 }
120 auto naBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
121 if (naBundleMgr == nullptr) {
122 return {ERROR_BUNDLE_SERVICE_EXCEPTION, {}};
123 }
124 auto baseFlag = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
125 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) +
126 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
127 auto getExtensionFlag = baseFlag +
128 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY);
129 AppExecFwk::BundleInfo bundleInfo;
130 ErrCode ret = AppExecFwk::CommonFunc::ConvertErrCode(
131 naBundleMgr->GetBundleInfoForSelf(getExtensionFlag, bundleInfo));
132 if (ret != ERR_OK) {
133 LOGE("GetProfileByExAbility failed");
134 return {ret, {}};
135 }
136 AppExecFwk::ExtensionAbilityInfo targetExtensionInfo;
137 ret = CheckExtensionFromBundleInfo(bundleInfo, extensionAbilityName, moduleName, targetExtensionInfo);
138 if (ret != ERR_OK) {
139 LOGE("GetProfileByExAbility failed by CheckExtensionFromBundleInfo");
140 return {ret, {}};
141 }
142 AppExecFwk::BundleMgrClient client;
143 std::vector<std::string> profileVec;
144 if (!client.GetProfileFromExtension(targetExtensionInfo, metadataName, profileVec)) {
145 LOGE("GetProfileByExAbility failed by GetProfileFromExtension");
146 return {ERROR_PROFILE_NOT_EXIST, {}};
147 }
148 return {SUCCESS_CODE, profileVec};
149 }
150
checkAbilityInfoExist(const std::string & abilityName,const AppExecFwk::AbilityInfo abilityInfo,AppExecFwk::AbilityInfo & targetAbilityInfo)151 int32_t checkAbilityInfoExist(const std::string& abilityName, const AppExecFwk::AbilityInfo abilityInfo,
152 AppExecFwk::AbilityInfo& targetAbilityInfo)
153 {
154 if (abilityInfo.name == abilityName) {
155 if (!abilityInfo.enabled) {
156 LOGE("checkAbilityInfoExist failed by ability disabled");
157 return ERROR_ABILITY_IS_DISABLED;
158 }
159 targetAbilityInfo = abilityInfo;
160 return ERR_OK;
161 }
162 return ERROR_ABILITY_NOT_EXIST;
163 }
164
checkAbilityName(const AppExecFwk::AbilityInfo & abilityInfo,const std::string & moduleName,const std::string & abilityName,AppExecFwk::AbilityInfo & targetAbilityInfo)165 std::tuple<bool, int32_t> checkAbilityName(const AppExecFwk::AbilityInfo& abilityInfo, const std::string& moduleName,
166 const std::string& abilityName, AppExecFwk::AbilityInfo& targetAbilityInfo)
167 {
168 bool flag = false;
169 if (abilityInfo.moduleName == moduleName) {
170 flag = true;
171 int32_t res = checkAbilityInfoExist(abilityName, abilityInfo, targetAbilityInfo);
172 if (res != ERROR_ABILITY_NOT_EXIST) {
173 return {flag, res};
174 }
175 }
176 return {flag, ERROR_ABILITY_NOT_EXIST};
177 }
178
CheckAbilityFromBundleInfo(const AppExecFwk::BundleInfo & bundleInfo,const std::string & abilityName,const std::string & moduleName,AppExecFwk::AbilityInfo & targetAbilityInfo)179 ErrCode CheckAbilityFromBundleInfo(const AppExecFwk::BundleInfo& bundleInfo, const std::string& abilityName,
180 const std::string& moduleName, AppExecFwk::AbilityInfo& targetAbilityInfo)
181 {
182 bool flag = false;
183 int32_t res = SUCCESS_CODE;
184 for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
185 for (const auto& abilityInfo : hapModuleInfo.abilityInfos) {
186 std::tie(flag, res) = checkAbilityName(abilityInfo, moduleName, abilityName, targetAbilityInfo);
187 if (flag == true && res != ERROR_ABILITY_NOT_EXIST) {
188 return res;
189 }
190 }
191 }
192 if (flag) {
193 return ERROR_ABILITY_NOT_EXIST;
194 } else {
195 return ERROR_MODULE_NOT_EXIST;
196 }
197 }
198
GetProfileByAbility(std::string moduleName,std::string abilityName,char * metadataName)199 std::tuple<int32_t, std::vector<std::string>> BundleManagerImpl::GetProfileByAbility(
200 std::string moduleName, std::string abilityName, char* metadataName)
201 {
202 LOGI("GetProfileByAbility called");
203 if (moduleName.empty()) {
204 LOGE("GetProfileByAbility failed due to empty moduleName");
205 return {ERROR_MODULE_NOT_EXIST, {}};
206 }
207 if (abilityName.empty()) {
208 LOGE("GetProfileByAbility failed due to empty abilityName");
209 return {ERROR_ABILITY_NOT_EXIST, {}};
210 }
211 auto iBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
212 if (iBundleMgr == nullptr) {
213 return {ERROR_BUNDLE_SERVICE_EXCEPTION, {}};
214 }
215 auto baseFlag = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
216 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) +
217 static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
218 auto getAbilityFlag = baseFlag + static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY);
219 AppExecFwk::BundleInfo bundleInfo;
220 ErrCode ret = AppExecFwk::CommonFunc::ConvertErrCode(iBundleMgr->GetBundleInfoForSelf(getAbilityFlag, bundleInfo));
221 if (ret != ERR_OK) {
222 LOGE("GetProfileByAbility failed");
223 return {ret, {}};
224 }
225 AppExecFwk::AbilityInfo targetAbilityInfo;
226 ret = CheckAbilityFromBundleInfo(bundleInfo, abilityName, moduleName, targetAbilityInfo);
227 if (ret != ERR_OK) {
228 LOGE("GetProfileByAbility failed by CheckAbilityFromBundleInfo");
229 return {ret, {}};
230 }
231 AppExecFwk::BundleMgrClient client;
232 std::vector<std::string> profileVec;
233 if (!client.GetProfileFromAbility(targetAbilityInfo, metadataName, profileVec)) {
234 LOGE("GetProfileByAbility failed by GetProfileFromAbility");
235 return {ERROR_PROFILE_NOT_EXIST, {}};
236 }
237 return {SUCCESS_CODE, profileVec};
238 }
239
240
241 } // BundleManager
242 } // CJSystemapi
243 } // OHOS