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 #include "fms_log_wrapper.h"
20 #include "form_mgr_errors.h"
21 #include "if_system_ability_manager.h"
22 #include "in_process_call_wrapper.h"
23 #include "ipc_skeleton.h"
24 #include "iservice_registry.h"
25 #include "system_ability_definition.h"
26 
27 namespace OHOS {
28 namespace AppExecFwk {
FormBmsHelper()29 FormBmsHelper::FormBmsHelper()
30 {
31     HILOG_INFO("call");
32 }
33 
~FormBmsHelper()34 FormBmsHelper::~FormBmsHelper()
35 {
36     HILOG_INFO("call");
37 }
38 
GetBundleMgr()39 sptr<IBundleMgr> FormBmsHelper::GetBundleMgr()
40 {
41     HILOG_DEBUG("call");
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("fail get bundle manager service");
50                 return nullptr;
51             }
52 
53             iBundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
54             if (iBundleMgr_ == nullptr) {
55                 HILOG_ERROR("fail get bundle manager service");
56                 return nullptr;
57             }
58         }
59     }
60 
61     return iBundleMgr_;
62 }
63 
GetBundleInstaller()64 sptr<IBundleInstaller> FormBmsHelper::GetBundleInstaller()
65 {
66     HILOG_DEBUG("call");
67     if (bundleInstallerProxy_ == nullptr) {
68         sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
69         if (iBundleMgr != nullptr) {
70             bundleInstallerProxy_ = iBundleMgr->GetBundleInstaller();
71         }
72     }
73     return bundleInstallerProxy_;
74 }
75 
76 
SetBundleManager(const sptr<IBundleMgr> &bundleManager)77 void FormBmsHelper::SetBundleManager(const sptr<IBundleMgr> &bundleManager)
78 {
79     HILOG_DEBUG("call");
80     iBundleMgr_ = bundleManager;
81 }
82 
83 /**
84  * @brief Notify module removable.
85  * @param bundleName Provider ability bundleName.
86  * @param moduleName Provider ability moduleName.
87  */
NotifyModuleRemovable(const std::string &bundleName, const std::string &moduleName)88 void FormBmsHelper::NotifyModuleRemovable(const std::string &bundleName, const std::string &moduleName)
89 {
90     HILOG_DEBUG("notify module removable, bundleName:%{public}s, moduleName:%{public}s",
91         bundleName.c_str(), moduleName.c_str());
92     if (bundleName.empty() || moduleName.empty()) {
93         return;
94     }
95 
96     std::string key = GenerateModuleKey(bundleName, moduleName);
97     HILOG_DEBUG("begin to notify %{public}s removable", key.c_str());
98     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
99     if (iBundleMgr == nullptr) {
100         HILOG_ERROR("get IBundleMgr failed");
101         return;
102     }
103 
104     std::string originId = IPCSkeleton::ResetCallingIdentity();
105 
106     IPCSkeleton::SetCallingIdentity(originId);
107 }
108 /**
109  * @brief Notify module not removable.
110  * @param bundleName Provider ability bundleName.
111  * @param moduleName Provider ability moduleName.
112  */
NotifyModuleNotRemovable(const std::string &bundleName, const std::string &moduleName)113 void FormBmsHelper::NotifyModuleNotRemovable(const std::string &bundleName, const std::string &moduleName)
114 {
115     HILOG_INFO("bundleName:%{public}s, moduleName:%{public}s",
116         bundleName.c_str(), moduleName.c_str());
117     if (bundleName.empty() || moduleName.empty()) {
118         return;
119     }
120     std::string key = GenerateModuleKey(bundleName, moduleName);
121     HILOG_DEBUG("begin to notify %{public}s not removable", key.c_str());
122     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
123     if (iBundleMgr == nullptr) {
124         HILOG_ERROR("get IBundleMgr failed");
125         return;
126     }
127 
128     if (!IN_PROCESS_CALL(iBundleMgr->SetModuleRemovable(bundleName, moduleName, false))) {
129         HILOG_ERROR("set not removable failed");
130         return;
131     }
132     return;
133 }
134 
GenerateModuleKey(const std::string &bundleName, const std::string &moduleName) const135 std::string FormBmsHelper::GenerateModuleKey(const std::string &bundleName, const std::string &moduleName) const
136 {
137     return bundleName + "#" + moduleName;
138 }
139 
GetBundlePackInfo(const std::string &bundleName, const int32_t userId, BundlePackInfo &bundlePackInfo)140 bool FormBmsHelper::GetBundlePackInfo(const std::string &bundleName, const int32_t userId,
141     BundlePackInfo &bundlePackInfo)
142 {
143     HILOG_INFO("bundleName:%{public}s", bundleName.c_str());
144     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
145     if (iBundleMgr == nullptr) {
146         HILOG_ERROR("get IBundleMgr failed");
147         return false;
148     }
149 
150     if (IN_PROCESS_CALL(iBundleMgr->GetBundlePackInfo(bundleName, GET_PACK_INFO_ALL, bundlePackInfo, userId))
151         != ERR_OK) {
152         HILOG_ERROR("fail get bundle pack info");
153         return false;
154     }
155 
156     HILOG_DEBUG("get bundle pack info success");
157     return true;
158 }
159 
GetAbilityInfo(const AAFwk::Want &want, int32_t userId, AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo)160 bool FormBmsHelper::GetAbilityInfo(const AAFwk::Want &want, int32_t userId, AbilityInfo &abilityInfo,
161     ExtensionAbilityInfo &extensionInfo)
162 {
163     HILOG_DEBUG("call");
164     ElementName element = want.GetElement();
165     std::string bundleName = element.GetBundleName();
166     std::string abilityName = element.GetAbilityName();
167     if (bundleName.empty() || abilityName.empty()) {
168         HILOG_ERROR("invalid want in explicit query ability info");
169         return false;
170     }
171 
172     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
173     if (iBundleMgr == nullptr) {
174         HILOG_ERROR("null iBundleMgr");
175         return false;
176     }
177     IN_PROCESS_CALL(iBundleMgr->QueryAbilityInfo(want, AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT,
178         userId, abilityInfo));
179     if (abilityInfo.name.empty() || abilityInfo.bundleName.empty()) {
180         HILOG_INFO("get ability info empty,try to get extension info");
181         std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
182         IN_PROCESS_CALL(iBundleMgr->QueryExtensionAbilityInfos(want, AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT,
183             userId, extensionInfos));
184         if (extensionInfos.empty()) {
185             HILOG_ERROR("get extension info failed");
186             return false;
187         }
188         extensionInfo = extensionInfos.front();
189         if (extensionInfo.bundleName.empty() || extensionInfo.name.empty()) {
190             HILOG_ERROR("get extension info empty");
191             return false;
192         }
193     }
194     return true;
195 }
196 
GetAbilityInfoByAction(const std::string &action, int32_t userId, AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionAbilityInfo)197 bool FormBmsHelper::GetAbilityInfoByAction(const std::string &action, int32_t userId,
198     AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionAbilityInfo)
199 {
200     HILOG_DEBUG("call");
201     if (action.empty()) {
202         HILOG_ERROR("input parasm error");
203         return false;
204     }
205 
206     Want wantAction;
207     wantAction.SetAction(action);
208     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
209     if (iBundleMgr == nullptr) {
210         HILOG_ERROR("null iBundleMgr");
211         return false;
212     }
213 
214     return (IN_PROCESS_CALL(iBundleMgr->ImplicitQueryInfoByPriority(wantAction,
215         AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT, userId, abilityInfo, extensionAbilityInfo)));
216 }
217 
GetBundleInfo(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)218 bool FormBmsHelper::GetBundleInfo(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)
219 {
220     return GetBundleInfoByFlags(bundleName, BundleFlag::GET_BUNDLE_WITH_ABILITIES, userId, bundleInfo);
221 }
222 
GetBundleInfoWithPermission(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)223 bool FormBmsHelper::GetBundleInfoWithPermission(const std::string &bundleName, int32_t userId, BundleInfo &bundleInfo)
224 {
225     return GetBundleInfoByFlags(bundleName, BundleFlag::GET_BUNDLE_WITH_REQUESTED_PERMISSION, userId, bundleInfo);
226 }
227 
GetBundleInfoDefault(const std::string& bundleName, int32_t userId, BundleInfo &bundleInfo)228 bool FormBmsHelper::GetBundleInfoDefault(const std::string& bundleName, int32_t userId, BundleInfo &bundleInfo)
229 {
230     return GetBundleInfoByFlags(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, userId, bundleInfo);
231 }
232 
GetBundleInfoByFlags(const std::string& bundleName, int32_t flags, int32_t userId, BundleInfo &bundleInfo)233 bool FormBmsHelper::GetBundleInfoByFlags(const std::string& bundleName, int32_t flags, int32_t userId,
234     BundleInfo &bundleInfo)
235 {
236     HILOG_DEBUG("call");
237     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
238     if (iBundleMgr == nullptr) {
239         HILOG_ERROR("null iBundleMgr");
240         return false;
241     }
242     return (IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId)));
243 }
244 
GetBundleInfoV9(const std::string& bundleName, int32_t userId, BundleInfo &bundleInfo)245 ErrCode FormBmsHelper::GetBundleInfoV9(const std::string& bundleName, int32_t userId, BundleInfo &bundleInfo)
246 {
247     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
248     if (iBundleMgr == nullptr) {
249         HILOG_ERROR("get IBundleMgr failed");
250         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
251     }
252 
253     if (IN_PROCESS_CALL(iBundleMgr->GetBundleInfoV9(bundleName,
254         (static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE) +
255         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_ABILITY) +
256         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) +
257         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE) +
258         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO) +
259         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY) +
260         static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA)),
261         bundleInfo, userId)) != ERR_OK) {
262         HILOG_ERROR("get bundleInfo failed");
263         return ERR_APPEXECFWK_FORM_GET_BUNDLE_FAILED;
264     }
265     return ERR_OK;
266 }
267 
GetCallerBundleName(std::string &callerBundleName)268 int32_t FormBmsHelper::GetCallerBundleName(std::string &callerBundleName)
269 {
270     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
271     if (iBundleMgr == nullptr) {
272         HILOG_ERROR("get IBundleMgr failed");
273         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
274     }
275     auto callingUid = IPCSkeleton::GetCallingUid();
276     if (IN_PROCESS_CALL(iBundleMgr->GetNameForUid(callingUid, callerBundleName)) != ERR_OK) {
277         HILOG_ERROR("fail get form config info");
278         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
279     }
280     HILOG_INFO("get form config info end");
281     return ERR_OK;
282 }
283 
GetBundleNameByUid(const int32_t uid, std::string &bundleName)284 int32_t FormBmsHelper::GetBundleNameByUid(const int32_t uid, std::string &bundleName)
285 {
286     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
287     if (iBundleMgr == nullptr) {
288         HILOG_ERROR("get IBundleMgr failed");
289         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
290     }
291 
292     if (IN_PROCESS_CALL(iBundleMgr->GetNameForUid(uid, bundleName)) != ERR_OK) {
293         HILOG_ERROR("fail get bundle name by uid");
294         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
295     }
296     return ERR_OK;
297 }
298 
GetUidByBundleName(const std::string &bundleName, const int32_t userId)299 int32_t FormBmsHelper::GetUidByBundleName(const std::string &bundleName, const int32_t userId)
300 {
301     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
302     if (iBundleMgr == nullptr) {
303         HILOG_ERROR("get IBundleMgr failed");
304         return INVALID_UID;
305     }
306     return IN_PROCESS_CALL(iBundleMgr->GetUidByBundleName(bundleName, userId));
307 }
308 
GetCompileMode(const std::string &bundleName, const std::string &moduleName, int32_t userId, int32_t &compileMode)309 bool FormBmsHelper::GetCompileMode(const std::string &bundleName, const std::string &moduleName,
310     int32_t userId, int32_t &compileMode)
311 {
312     HILOG_DEBUG("call");
313     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
314     if (iBundleMgr == nullptr) {
315         HILOG_ERROR("null iBundleMgr");
316         return false;
317     }
318     int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
319     BundleInfo bundleInfo;
320     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
321         HILOG_ERROR("Get bundle info failed");
322         return false;
323     }
324 
325     for (const auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
326         if (!moduleName.empty() && hapModuleInfo.moduleName != moduleName) {
327             continue;
328         }
329         compileMode = static_cast<int32_t>(hapModuleInfo.compileMode);
330         return true;
331     }
332 
333     HILOG_ERROR("Get compile mode failed");
334     return false;
335 }
336 
GetCompatibleVersion(const std::string& bundleName, int32_t userId, int32_t& compatibleVersion)337 bool FormBmsHelper::GetCompatibleVersion(const std::string& bundleName, int32_t userId, int32_t& compatibleVersion)
338 {
339     HILOG_DEBUG("call");
340     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
341     if (iBundleMgr == nullptr) {
342         HILOG_ERROR("null iBundleMgr");
343         return false;
344     }
345     int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
346     BundleInfo bundleInfo;
347     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
348         HILOG_ERROR("Get bundle info failed");
349         return false;
350     }
351 
352     compatibleVersion = static_cast<int32_t>(bundleInfo.compatibleVersion);
353     return true;
354 }
355 
GetProxyDataInfos(const std::string &bundleName, const std::string &moduleName, int32_t userId, std::vector<ProxyData> &proxyData)356 ErrCode FormBmsHelper::GetProxyDataInfos(const std::string &bundleName, const std::string &moduleName,
357     int32_t userId, std::vector<ProxyData> &proxyData)
358 {
359     HILOG_DEBUG("call");
360     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
361     if (iBundleMgr == nullptr) {
362         HILOG_ERROR("null iBundleMgr");
363         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
364     }
365 
366     return IN_PROCESS_CALL(iBundleMgr->GetProxyDataInfos(bundleName, moduleName, proxyData, userId));
367 }
368 
GetAllProxyDataInfos(int32_t userId, std::vector<ProxyData> &proxyData)369 ErrCode FormBmsHelper::GetAllProxyDataInfos(int32_t userId, std::vector<ProxyData> &proxyData)
370 {
371     HILOG_DEBUG("call");
372     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
373     if (iBundleMgr == nullptr) {
374         HILOG_ERROR("null iBundleMgr");
375         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
376     }
377 
378     return IN_PROCESS_CALL(iBundleMgr->GetAllProxyDataInfos(proxyData, userId));
379 }
380 
GetApplicationInfo(const std::string &bundleName, int32_t userId, ApplicationInfo &appInfo)381 ErrCode FormBmsHelper::GetApplicationInfo(const std::string &bundleName, int32_t userId, ApplicationInfo &appInfo)
382 {
383     HILOG_DEBUG("call");
384     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
385     if (iBundleMgr == nullptr) {
386         HILOG_ERROR("null iBundleMgr");
387         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
388     }
389 
390     return IN_PROCESS_CALL(iBundleMgr->GetApplicationInfoV9(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
391         userId, appInfo));
392 }
393 
RegisterBundleEventCallback()394 ErrCode FormBmsHelper::RegisterBundleEventCallback()
395 {
396     if (!hasRegisterBundleEvent_) {
397         std::lock_guard<std::mutex> lock(registerMutex_);
398         if (!hasRegisterBundleEvent_) {
399             HILOG_INFO("call");
400             sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
401             if (iBundleMgr == nullptr) {
402                 return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
403             }
404             formBundleEventCallback_ = new (std::nothrow) FormBundleEventCallback();
405             if (formBundleEventCallback_ == nullptr) {
406                 HILOG_ERROR("allocate formBundleEventCallback_ failed");
407                 return ERR_APPEXECFWK_FORM_COMMON_CODE;
408             }
409             if (!iBundleMgr->RegisterBundleEventCallback(formBundleEventCallback_)) {
410                 HILOG_ERROR("RegisterBundleEventCallback failed");
411                 return ERR_APPEXECFWK_FORM_COMMON_CODE;
412             }
413             hasRegisterBundleEvent_ = true;
414         }
415     }
416 
417     return ERR_OK;
418 }
419 
UnregisterBundleEventCallback()420 ErrCode FormBmsHelper::UnregisterBundleEventCallback()
421 {
422     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
423     if (iBundleMgr == nullptr) {
424         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
425     }
426     if (!iBundleMgr->UnregisterBundleEventCallback(formBundleEventCallback_)) {
427         HILOG_ERROR("RegisterBundleEventCallback failed");
428         return ERR_APPEXECFWK_FORM_COMMON_CODE;
429     }
430     formBundleEventCallback_ = nullptr;
431     hasRegisterBundleEvent_ = false;
432     return ERR_OK;
433 }
434 } // namespace AppExecFwk
435 } // namespace OHOS
436