• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_log_wrapper.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     APP_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         APP_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 
checkExtensionAbilityName(const AppExecFwk::ExtensionAbilityInfo & extensionInfo,const std::string & abilityName,AppExecFwk::ExtensionAbilityInfo & targetExtensionInfo)58 std::tuple<bool, int32_t> checkExtensionAbilityName(const AppExecFwk::ExtensionAbilityInfo& extensionInfo,
59     const std::string& abilityName, AppExecFwk::ExtensionAbilityInfo& targetExtensionInfo)
60 {
61     bool ifExists = false;
62     if (extensionInfo.name == abilityName) {
63         ifExists = true;
64         if (!extensionInfo.enabled) {
65             APP_LOGI("extension disabled");
66             return {ifExists, ERROR_ABILITY_IS_DISABLED};
67         }
68         targetExtensionInfo = extensionInfo;
69     }
70     return {ifExists, SUCCESS_CODE};
71 }
72 
CheckExtensionFromBundleInfo(const AppExecFwk::BundleInfo & bundleInfo,const std::string & abilityName,const std::string & moduleName,AppExecFwk::ExtensionAbilityInfo & targetExtensionInfo)73 ErrCode CheckExtensionFromBundleInfo(const AppExecFwk::BundleInfo& bundleInfo, const std::string& abilityName,
74     const std::string& moduleName, AppExecFwk::ExtensionAbilityInfo& targetExtensionInfo)
75 {
76     bool hasFoundModule = false;
77     bool ifExists = false;
78     int32_t res = SUCCESS_CODE;
79     for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
80         if (hapModuleInfo.moduleName != moduleName) {
81             continue;
82         }
83         hasFoundModule = true;
84         for (const auto& extensionInfo : hapModuleInfo.extensionInfos) {
85             std::tie(ifExists, res) = checkExtensionAbilityName(extensionInfo, abilityName, targetExtensionInfo);
86             if (res == ERROR_ABILITY_IS_DISABLED || (res == SUCCESS_CODE && ifExists == true)) {
87                 return res;
88             }
89         }
90     }
91     return hasFoundModule ? ERROR_ABILITY_NOT_EXIST : ERROR_MODULE_NOT_EXIST;
92 }
93 
GetProfileByExtensionAbility(std::string moduleName,std::string extensionAbilityName,char * metadataName)94 std::tuple<int32_t, std::vector<std::string>> BundleManagerImpl::GetProfileByExtensionAbility(
95     std::string moduleName, std::string extensionAbilityName, char* metadataName)
96 {
97     auto naBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
98     if (naBundleMgr == nullptr) {
99         return {ERROR_BUNDLE_SERVICE_EXCEPTION, {}};
100     }
101 
102     if (extensionAbilityName.empty()) {
103         APP_LOGE("GetProfileByExtensionAbility failed due to empty extensionAbilityName");
104         return {ERROR_ABILITY_NOT_EXIST, {}};
105     }
106 
107     if (moduleName.empty()) {
108         APP_LOGE("GetProfileByExtensionAbility failed due to empty moduleName");
109         return {ERROR_MODULE_NOT_EXIST, {}};
110     }
111 
112     auto baseFlag = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
113            static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) +
114            static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
115     auto getExtensionFlag = baseFlag +
116         static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY);
117     AppExecFwk::BundleInfo bundleInfo;
118     ErrCode ret = AppExecFwk::CommonFunc::ConvertErrCode(
119         naBundleMgr->GetBundleInfoForSelf(getExtensionFlag, bundleInfo));
120     if (ret != ERR_OK) {
121         APP_LOGE("GetProfileByExAbility failed");
122         return {ret, {}};
123     }
124     AppExecFwk::ExtensionAbilityInfo targetExtensionInfo;
125     ret = CheckExtensionFromBundleInfo(bundleInfo, extensionAbilityName, moduleName, targetExtensionInfo);
126     if (ret != ERR_OK) {
127         APP_LOGE("GetProfileByExAbility failed by CheckExtensionFromBundleInfo");
128         return {ret, {}};
129     }
130     AppExecFwk::BundleMgrClient client;
131     std::vector<std::string> profileVec;
132     if (!client.GetProfileFromExtension(targetExtensionInfo, metadataName, profileVec)) {
133         APP_LOGE("GetProfileByExAbility failed by GetProfileFromExtension");
134         return {ERROR_PROFILE_NOT_EXIST, {}};
135     }
136     return {SUCCESS_CODE, profileVec};
137 }
138 
checkAbilityName(const AppExecFwk::AbilityInfo & abilityInfo,const std::string & abilityName,AppExecFwk::AbilityInfo & targetAbilityInfo)139 std::tuple<bool, int32_t> checkAbilityName(const AppExecFwk::AbilityInfo& abilityInfo,
140     const std::string& abilityName, AppExecFwk::AbilityInfo& targetAbilityInfo)
141 {
142     bool ifExists = false;
143     if (abilityInfo.name == abilityName) {
144         ifExists = true;
145         if (!abilityInfo.enabled) {
146             APP_LOGI("ability disabled");
147             return {ifExists, ERROR_ABILITY_IS_DISABLED};
148         }
149         targetAbilityInfo = abilityInfo;
150     }
151     return {ifExists, SUCCESS_CODE};
152 }
153 
CheckAbilityFromBundleInfo(const AppExecFwk::BundleInfo & bundleInfo,const std::string & abilityName,const std::string & moduleName,AppExecFwk::AbilityInfo & targetAbilityInfo)154 ErrCode CheckAbilityFromBundleInfo(const AppExecFwk::BundleInfo& bundleInfo, const std::string& abilityName,
155     const std::string& moduleName, AppExecFwk::AbilityInfo& targetAbilityInfo)
156 {
157     bool hasFoundModule = false;
158     bool ifExists = false;
159     int32_t res = SUCCESS_CODE;
160     for (const auto& hapModuleInfo : bundleInfo.hapModuleInfos) {
161         if (hapModuleInfo.moduleName != moduleName) {
162             continue;
163         }
164         hasFoundModule = true;
165         for (const auto& abilityInfo : hapModuleInfo.abilityInfos) {
166             std::tie(ifExists, res) = checkAbilityName(abilityInfo, abilityName, targetAbilityInfo);
167             if (res == ERROR_ABILITY_IS_DISABLED || (res == SUCCESS_CODE && ifExists == true)) {
168                 return res;
169             }
170         }
171     }
172     return hasFoundModule ? ERROR_ABILITY_NOT_EXIST : ERROR_MODULE_NOT_EXIST;
173 }
174 
GetProfileByAbility(std::string moduleName,std::string abilityName,char * metadataName)175 std::tuple<int32_t, std::vector<std::string>> BundleManagerImpl::GetProfileByAbility(
176     std::string moduleName, std::string abilityName, char* metadataName)
177 {
178     APP_LOGI("GetProfileByAbility called");
179     auto iBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
180     if (iBundleMgr == nullptr) {
181         return {ERROR_BUNDLE_SERVICE_EXCEPTION, {}};
182     }
183 
184     if (abilityName.empty()) {
185         APP_LOGE("GetProfileByAbility failed due to empty abilityName");
186         return {ERROR_ABILITY_NOT_EXIST, {}};
187     }
188 
189     if (moduleName.empty()) {
190         APP_LOGE("GetProfileByAbility failed due to empty moduleName");
191         return {ERROR_MODULE_NOT_EXIST, {}};
192     }
193 
194     auto baseFlag = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
195            static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) +
196            static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
197     auto getAbilityFlag = baseFlag + static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY);
198     AppExecFwk::BundleInfo bundleInfo;
199     ErrCode ret = AppExecFwk::CommonFunc::ConvertErrCode(iBundleMgr->GetBundleInfoForSelf(getAbilityFlag, bundleInfo));
200     if (ret != ERR_OK) {
201         APP_LOGE("GetProfileByAbility failed");
202         return {ret, {}};
203     }
204     AppExecFwk::AbilityInfo targetAbilityInfo;
205     ret = CheckAbilityFromBundleInfo(bundleInfo, abilityName, moduleName, targetAbilityInfo);
206     if (ret != ERR_OK) {
207         APP_LOGE("GetProfileByAbility failed by CheckAbilityFromBundleInfo");
208         return {ret, {}};
209     }
210     AppExecFwk::BundleMgrClient client;
211     std::vector<std::string> profileVec;
212     if (!client.GetProfileFromAbility(targetAbilityInfo, metadataName, profileVec)) {
213         APP_LOGE("GetProfileByAbility failed by GetProfileFromAbility");
214         return {ERROR_PROFILE_NOT_EXIST, {}};
215     }
216     return {SUCCESS_CODE, profileVec};
217 }
218 
InnerCanOpenLink(std::string link,int32_t & code)219 bool BundleManagerImpl::InnerCanOpenLink(std::string link, int32_t& code)
220 {
221     bool canOpen = false;
222     auto iBundleMgr = AppExecFwk::CommonFunc::GetBundleMgr();
223     if (iBundleMgr == nullptr) {
224         APP_LOGE("can not get iBundleMgr");
225         code = ERROR_BUNDLE_SERVICE_EXCEPTION;
226         return canOpen;
227     }
228     code = AppExecFwk::CommonFunc::ConvertErrCode(
229         iBundleMgr->CanOpenLink(link, canOpen));
230     if (code != NO_ERROR) {
231         APP_LOGE("CanOpenLink failed");
232         return canOpen;
233     }
234     return canOpen;
235 }
236 
237 } // BundleManager
238 } // CJSystemapi
239 } // OHOS