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("failed to get IBundleMgr.");
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("GetBundleInfo, failed to get bundle info.");
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     return ERR_OK;
281 }
282 
GetBundleNameByUid(const int32_t uid,std::string & bundleName)283 int32_t FormBmsHelper::GetBundleNameByUid(const int32_t uid, std::string &bundleName)
284 {
285     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
286     if (iBundleMgr == nullptr) {
287         HILOG_ERROR("get IBundleMgr failed");
288         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
289     }
290 
291     if (IN_PROCESS_CALL(iBundleMgr->GetNameForUid(uid, bundleName)) != ERR_OK) {
292         HILOG_ERROR("fail get bundle name by uid");
293         return ERR_APPEXECFWK_FORM_GET_INFO_FAILED;
294     }
295     return ERR_OK;
296 }
297 
GetUidByBundleName(const std::string & bundleName,const int32_t userId)298 int32_t FormBmsHelper::GetUidByBundleName(const std::string &bundleName, const int32_t userId)
299 {
300     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
301     if (iBundleMgr == nullptr) {
302         HILOG_ERROR("get IBundleMgr failed");
303         return INVALID_UID;
304     }
305     return IN_PROCESS_CALL(iBundleMgr->GetUidByBundleName(bundleName, userId));
306 }
307 
GetCompileMode(const std::string & bundleName,const std::string & moduleName,int32_t userId,int32_t & compileMode)308 bool FormBmsHelper::GetCompileMode(const std::string &bundleName, const std::string &moduleName,
309     int32_t userId, int32_t &compileMode)
310 {
311     HILOG_DEBUG("call");
312     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
313     if (iBundleMgr == nullptr) {
314         HILOG_ERROR("null iBundleMgr");
315         return false;
316     }
317     int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
318     BundleInfo bundleInfo;
319     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
320         HILOG_ERROR("Get bundle info failed");
321         return false;
322     }
323 
324     for (const auto &hapModuleInfo : bundleInfo.hapModuleInfos) {
325         if (!moduleName.empty() && hapModuleInfo.moduleName != moduleName) {
326             continue;
327         }
328         compileMode = static_cast<int32_t>(hapModuleInfo.compileMode);
329         return true;
330     }
331 
332     HILOG_ERROR("Get compile mode failed");
333     return false;
334 }
335 
GetCompatibleVersion(const std::string & bundleName,int32_t userId,int32_t & compatibleVersion)336 bool FormBmsHelper::GetCompatibleVersion(const std::string& bundleName, int32_t userId, int32_t& compatibleVersion)
337 {
338     HILOG_DEBUG("call");
339     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
340     if (iBundleMgr == nullptr) {
341         HILOG_ERROR("null iBundleMgr");
342         return false;
343     }
344     int32_t flags = BundleFlag::GET_BUNDLE_DEFAULT;
345     BundleInfo bundleInfo;
346     if (!IN_PROCESS_CALL(iBundleMgr->GetBundleInfo(bundleName, flags, bundleInfo, userId))) {
347         HILOG_ERROR("Get bundle info failed");
348         return false;
349     }
350 
351     compatibleVersion = static_cast<int32_t>(bundleInfo.compatibleVersion);
352     return true;
353 }
354 
GetProxyDataInfos(const std::string & bundleName,const std::string & moduleName,int32_t userId,std::vector<ProxyData> & proxyData)355 ErrCode FormBmsHelper::GetProxyDataInfos(const std::string &bundleName, const std::string &moduleName,
356     int32_t userId, std::vector<ProxyData> &proxyData)
357 {
358     HILOG_DEBUG("call");
359     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
360     if (iBundleMgr == nullptr) {
361         HILOG_ERROR("null iBundleMgr");
362         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
363     }
364 
365     return IN_PROCESS_CALL(iBundleMgr->GetProxyDataInfos(bundleName, moduleName, proxyData, userId));
366 }
367 
GetAllProxyDataInfos(int32_t userId,std::vector<ProxyData> & proxyData)368 ErrCode FormBmsHelper::GetAllProxyDataInfos(int32_t userId, std::vector<ProxyData> &proxyData)
369 {
370     HILOG_DEBUG("call");
371     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
372     if (iBundleMgr == nullptr) {
373         HILOG_ERROR("null iBundleMgr");
374         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
375     }
376 
377     return IN_PROCESS_CALL(iBundleMgr->GetAllProxyDataInfos(proxyData, userId));
378 }
379 
GetApplicationInfo(const std::string & bundleName,int32_t userId,ApplicationInfo & appInfo)380 ErrCode FormBmsHelper::GetApplicationInfo(const std::string &bundleName, int32_t userId, ApplicationInfo &appInfo)
381 {
382     HILOG_DEBUG("call");
383     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
384     if (iBundleMgr == nullptr) {
385         HILOG_ERROR("null iBundleMgr");
386         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
387     }
388 
389     return IN_PROCESS_CALL(iBundleMgr->GetApplicationInfoV9(bundleName, AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT,
390         userId, appInfo));
391 }
392 
RegisterBundleEventCallback()393 ErrCode FormBmsHelper::RegisterBundleEventCallback()
394 {
395     if (!hasRegisterBundleEvent_) {
396         std::lock_guard<std::mutex> lock(registerMutex_);
397         if (!hasRegisterBundleEvent_) {
398             HILOG_INFO("call");
399             sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
400             if (iBundleMgr == nullptr) {
401                 return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
402             }
403             formBundleEventCallback_ = new (std::nothrow) FormBundleEventCallback();
404             if (formBundleEventCallback_ == nullptr) {
405                 HILOG_ERROR("allocate formBundleEventCallback_ failed");
406                 return ERR_APPEXECFWK_FORM_COMMON_CODE;
407             }
408             if (!iBundleMgr->RegisterBundleEventCallback(formBundleEventCallback_)) {
409                 HILOG_ERROR("RegisterBundleEventCallback failed");
410                 return ERR_APPEXECFWK_FORM_COMMON_CODE;
411             }
412             hasRegisterBundleEvent_ = true;
413         }
414     }
415 
416     return ERR_OK;
417 }
418 
UnregisterBundleEventCallback()419 ErrCode FormBmsHelper::UnregisterBundleEventCallback()
420 {
421     sptr<IBundleMgr> iBundleMgr = GetBundleMgr();
422     if (iBundleMgr == nullptr) {
423         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
424     }
425     if (!iBundleMgr->UnregisterBundleEventCallback(formBundleEventCallback_)) {
426         HILOG_ERROR("RegisterBundleEventCallback failed");
427         return ERR_APPEXECFWK_FORM_COMMON_CODE;
428     }
429     formBundleEventCallback_ = nullptr;
430     hasRegisterBundleEvent_ = false;
431     return ERR_OK;
432 }
433 } // namespace AppExecFwk
434 } // namespace OHOS
435