• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "form_bms_helper.h"
17 
18 #include "ability_manager_interface.h"
19 #ifndef SUPPORT_ERMS
20 #include "erms_mgr_interface.h"
21 #include "erms_mgr_param.h"
22 #endif
23 #include "fms_log_wrapper.h"
24 #include "form_mgr_errors.h"
25 #include "if_system_ability_manager.h"
26 #include "in_process_call_wrapper.h"
27 #include "ipc_skeleton.h"
28 #include "iservice_registry.h"
29 #include "system_ability_definition.h"
30 
31 namespace OHOS {
32 namespace AppExecFwk {
FormBmsHelper()33 FormBmsHelper::FormBmsHelper()
34 {}
35 
~FormBmsHelper()36 FormBmsHelper::~FormBmsHelper()
37 {}
38 
GetBundleMgr()39 sptr<IBundleMgr> FormBmsHelper::GetBundleMgr()
40 {
41     HILOG_DEBUG("called.");
42     if (iBundleMgr_ == nullptr) {
43         std::lock_guard<std::mutex> lock(ibundleMutex_);
44         if (iBundleMgr_ == nullptr) {
45             sptr<ISystemAbilityManager> systemAbilityManager =
46             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
47             auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
48             if (remoteObject == nullptr) {
49                 HILOG_ERROR("error, failed to get bundle manager service.");
50                 return nullptr;
51             }
52 
53             iBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
54             if (iBundleMgr_ == nullptr) {
55                 HILOG_ERROR("error, failed to get bundle manager service");
56                 return nullptr;
57             }
58         }
59     }
60 
61     return iBundleMgr_;
62 }
63 
SetBundleManager(const sptr<IBundleMgr> & bundleManager)64 void FormBmsHelper::SetBundleManager(const sptr<IBundleMgr> &bundleManager)
65 {
66     HILOG_DEBUG("SetBundleManager called.");
67     iBundleMgr_ = bundleManager;
68 }
69 
70 /**
71  * @brief Notify module removable.
72  * @param bundleName Provider ability bundleName.
73  * @param moduleName Provider ability moduleName.
74  */
NotifyModuleRemovable(const std::string & bundleName,const std::string & moduleName)75 void FormBmsHelper::NotifyModuleRemovable(const std::string &bundleName, const std::string &moduleName)
76 {
77     HILOG_DEBUG("notify module removable, bundleName:%{public}s, moduleName:%{public}s",
78         bundleName.c_str(), moduleName.c_str());
79     if (bundleName.empty() || moduleName.empty()) {
80         return;
81     }
82 
83     std::string key = GenerateModuleKey(bundleName, moduleName);
84     HILOG_DEBUG("begin to notify %{public}s removable", key.c_str());
85     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
86     if (iBundleMgr == nullptr) {
87         HILOG_ERROR("failed to get IBundleMgr.");
88         return;
89     }
90 
91     std::string originId = IPCSkeleton::ResetCallingIdentity();
92 
93     IPCSkeleton::SetCallingIdentity(originId);
94 }
95 /**
96  * @brief Notify module not removable.
97  * @param bundleName Provider ability bundleName.
98  * @param moduleName Provider ability moduleName.
99  */
NotifyModuleNotRemovable(const std::string & bundleName,const std::string & moduleName)100 void FormBmsHelper::NotifyModuleNotRemovable(const std::string &bundleName, const std::string &moduleName)
101 {
102     HILOG_INFO("bundleName:%{public}s, moduleName:%{public}s",
103         bundleName.c_str(), moduleName.c_str());
104     if (bundleName.empty() || moduleName.empty()) {
105         return;
106     }
107     std::string key = GenerateModuleKey(bundleName, moduleName);
108     HILOG_DEBUG("begin to notify %{public}s not removable", key.c_str());
109     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
110     if (iBundleMgr == nullptr) {
111         HILOG_ERROR("failed to get IBundleMgr.");
112         return;
113     }
114 
115     if (!IN_PROCESS_CALL(iBundleMgr->SetModuleRemovable(bundleName, moduleName, false))) {
116         HILOG_ERROR("set not removable failed.");
117         return;
118     }
119     return;
120 }
121 
GenerateModuleKey(const std::string & bundleName,const std::string & moduleName) const122 std::string FormBmsHelper::GenerateModuleKey(const std::string &bundleName, const std::string &moduleName) const
123 {
124     return bundleName + "#" + moduleName;
125 }
126 
GetBundlePackInfo(const std::string & bundleName,const int32_t userId,BundlePackInfo & bundlePackInfo)127 bool FormBmsHelper::GetBundlePackInfo(const std::string &bundleName, const int32_t userId,
128     BundlePackInfo &bundlePackInfo)
129 {
130     HILOG_INFO("bundleName:%{public}s", bundleName.c_str());
131     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
132     if (iBundleMgr == nullptr) {
133         HILOG_ERROR("failed to get IBundleMgr.");
134         return false;
135     }
136 
137     if (IN_PROCESS_CALL(iBundleMgr->GetBundlePackInfo(bundleName, GET_PACK_INFO_ALL, bundlePackInfo, userId))
138         != ERR_OK) {
139         HILOG_ERROR("error, failed to get bundle pack info.");
140         return false;
141     }
142 
143     HILOG_DEBUG("get bundle pack info success");
144     return true;
145 }
146 
GetAbilityInfo(const AAFwk::Want & want,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)147 bool FormBmsHelper::GetAbilityInfo(const AAFwk::Want &want, int32_t userId, AbilityInfo &abilityInfo,
148     ExtensionAbilityInfo &extensionInfo)
149 {
150     HILOG_DEBUG("GetAbilityInfo called.");
151     ElementName element = want.GetElement();
152     std::string bundleName = element.GetBundleName();
153     std::string abilityName = element.GetAbilityName();
154     if (bundleName.empty() || abilityName.empty()) {
155         HILOG_ERROR("invalid want in explicit query ability info");
156         return false;
157     }
158 
159     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
160     if (iBundleMgr == nullptr) {
161         HILOG_ERROR("iBundleMgr is nullptr");
162         return false;
163     }
164     IN_PROCESS_CALL(iBundleMgr->QueryAbilityInfo(want, AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT,
165         userId, abilityInfo));
166     if (abilityInfo.name.empty() || abilityInfo.bundleName.empty()) {
167         HILOG_INFO("get ability info empty, try to get extension info.");
168         std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
169         IN_PROCESS_CALL(iBundleMgr->QueryExtensionAbilityInfos(want, AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT,
170             userId, extensionInfos));
171         if (extensionInfos.empty()) {
172             HILOG_ERROR("get extension info failed.");
173             return false;
174         }
175         extensionInfo = extensionInfos.front();
176         if (extensionInfo.bundleName.empty() || extensionInfo.name.empty()) {
177             HILOG_ERROR("get extension info empty.");
178             return false;
179         }
180     }
181     return true;
182 }
183 
GetAbilityInfoByAction(const std::string & action,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionAbilityInfo)184 bool FormBmsHelper::GetAbilityInfoByAction(const std::string &action, int32_t userId,
185     AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionAbilityInfo)
186 {
187     HILOG_DEBUG("called.");
188     if (action.empty()) {
189         HILOG_ERROR("input parasm error.");
190         return false;
191     }
192 
193     Want wantAction;
194     wantAction.SetAction(action);
195     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
196     if (iBundleMgr == nullptr) {
197         HILOG_ERROR("iBundleMgr is nullptr");
198         return false;
199     }
200 
201     return (IN_PROCESS_CALL(iBundleMgr->ImplicitQueryInfoByPriority(wantAction,
202         AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT, userId, abilityInfo, extensionAbilityInfo)));
203 }
204 
GetBundleInfo(const std::string & bundleName,int32_t userId,BundleInfo & bundleInfo)205 bool FormBmsHelper::GetBundleInfo(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)
206 {
207     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
208     if (iBundleMgr == nullptr) {
209         HILOG_ERROR("iBundleMgr is nullptr");
210         return false;
211     }
212 
213     int32_t flags = BundleFlag::GET_BUNDLE_WITH_ABILITIES;
214     return (IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId)));
215 }
216 
GetBundleInfoWithPermission(const std::string & bundleName,int32_t userId,BundleInfo & bundleInfo)217 bool FormBmsHelper::GetBundleInfoWithPermission(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)
218 {
219     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
220     if (iBundleMgr == nullptr) {
221         HILOG_ERROR("iBundleMgr is nullptr");
222         return false;
223     }
224 
225     int32_t flags = BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION;
226     return (IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId)));
227 }
228 
GetCallerBundleName(std::string & callerBundleName)229 int32_t FormBmsHelper::GetCallerBundleName(std::string &callerBundleName)
230 {
231     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
232     if (iBundleMgr == nullptr) {
233         HILOG_ERROR("failed to get IBundleMgr.");
234         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
235     }
236     auto callingUid = IPCSkeleton::GetCallingUid();
237     if (IN_PROCESS_CALL(iBundleMgr->GetNameForUid(callingUid, callerBundleName)) != ERR_OK) {
238         HILOG_ERROR("failed to get form config info.");
239         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
240     }
241     return ERR_OK;
242 }
243 
GetBundleNameByUid(const int32_t uid,std::string & bundleName)244 int32_t FormBmsHelper::GetBundleNameByUid(const int32_t uid, std::string &bundleName)
245 {
246     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
247     if (iBundleMgr == nullptr) {
248         HILOG_ERROR("failed to get IBundleMgr.");
249         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
250     }
251 
252     if (IN_PROCESS_CALL(iBundleMgr->GetNameForUid(uid, bundleName)) != ERR_OK) {
253         HILOG_ERROR("failed to get bundle name by uid.");
254         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
255     }
256     return ERR_OK;
257 }
258 
GetUidByBundleName(const std::string & bundleName,const int32_t userId)259 int32_t FormBmsHelper::GetUidByBundleName(const std::string &bundleName, const int32_t userId)
260 {
261     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
262     if (iBundleMgr == nullptr) {
263         HILOG_ERROR("failed to get IBundleMgr.");
264         return INVALID_UID;
265     }
266     return IN_PROCESS_CALL(iBundleMgr->GetUidByBundleName(bundleName, userId));
267 }
268 
GetCompileMode(const std::string & bundleName,const std::string & moduleName,int32_t userId,int32_t & compileMode)269 bool FormBmsHelper::GetCompileMode(const std::string &bundleName, const std::string &moduleName,
270     int32_t userId, int32_t &compileMode)
271 {
272     HILOG_DEBUG("called.");
273     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
274     if (iBundleMgr == nullptr) {
275         HILOG_ERROR("iBundleMgr is nullptr");
276         return false;
277     }
278     int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
279     BundleInfo bundleInfo;
280     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
281         HILOG_ERROR("Get bundle info failed.");
282         return false;
283     }
284 
285     for (const auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
286         if (!moduleName.empty() && hapModuleInfo.moduleName != moduleName) {
287             continue;
288         }
289         compileMode = static_cast<int32_t>(hapModuleInfo.compileMode);
290         return true;
291     }
292 
293     HILOG_ERROR("Get compile mode failed.");
294     return false;
295 }
296 
GetCompatibleVersionCode(const std::string & bundleName,int32_t userId,int32_t & minCompatibleVersionCode)297 bool FormBmsHelper::GetCompatibleVersionCode(
298     const std::string& bundleName, int32_t userId, int32_t& minCompatibleVersionCode)
299 {
300     HILOG_DEBUG("called.");
301     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
302     if (iBundleMgr == nullptr) {
303         HILOG_ERROR("iBundleMgr is nullptr");
304         return false;
305     }
306     int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
307     BundleInfo bundleInfo;
308     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
309         HILOG_ERROR("Get bundle info failed.");
310         return false;
311     }
312 
313     minCompatibleVersionCode = bundleInfo.minCompatibleVersionCode;
314     return true;
315 }
316 
GetProxyDataInfos(const std::string & bundleName,const std::string & moduleName,int32_t userId,std::vector<ProxyData> & proxyData)317 ErrCode FormBmsHelper::GetProxyDataInfos(const std::string &bundleName, const std::string &moduleName,
318     int32_t userId, std::vector<ProxyData> &proxyData)
319 {
320     HILOG_DEBUG("called.");
321     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
322     if (iBundleMgr == nullptr) {
323         HILOG_ERROR("iBundleMgr is nullptr");
324         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
325     }
326 
327     return IN_PROCESS_CALL(iBundleMgr->GetProxyDataInfos(bundleName, moduleName, proxyData, userId));
328 }
329 
GetAllProxyDataInfos(int32_t userId,std::vector<ProxyData> & proxyData)330 ErrCode FormBmsHelper::GetAllProxyDataInfos(int32_t userId, std::vector<ProxyData> &proxyData)
331 {
332     HILOG_DEBUG("called.");
333     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
334     if (iBundleMgr == nullptr) {
335         HILOG_ERROR("iBundleMgr is nullptr");
336         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
337     }
338 
339     return IN_PROCESS_CALL(iBundleMgr->GetAllProxyDataInfos(proxyData, userId));
340 }
341 
GetApplicationInfo(const std::string & bundleName,int32_t userId,ApplicationInfo & appInfo)342 ErrCode FormBmsHelper::GetApplicationInfo(const std::string &bundleName, int32_t userId, ApplicationInfo &appInfo)
343 {
344     HILOG_DEBUG("called.");
345     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
346     if (iBundleMgr == nullptr) {
347         HILOG_ERROR("iBundleMgr is nullptr");
348         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
349     }
350 
351     return IN_PROCESS_CALL(iBundleMgr->GetApplicationInfoV9(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
352         userId, appInfo));
353 }
354 } // namespace AppExecFwk
355 } // namespace OHOS
356