• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "bundle_data_mgr.h"
17 
18 #include <chrono>
19 #include <cinttypes>
20 
21 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
22 #include "os_account_info.h"
23 #endif
24 #include "account_helper.h"
25 #include "app_log_wrapper.h"
26 #include "bundle_constants.h"
27 #ifdef BMS_RDB_ENABLE
28 #include "bundle_data_storage_rdb.h"
29 #include "preinstall_data_storage_rdb.h"
30 #else
31 #include "bundle_data_storage_database.h"
32 #include "preinstall_data_storage.h"
33 #endif
34 #include "bundle_event_callback_death_recipient.h"
35 #include "bundle_mgr_service.h"
36 #include "bundle_status_callback_death_recipient.h"
37 #include "bundle_util.h"
38 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
39 #include "default_app_mgr.h"
40 #endif
41 #include "installd_client.h"
42 #include "ipc_skeleton.h"
43 #include "json_serializer.h"
44 #ifdef GLOBAL_I18_ENABLE
45 #include "locale_info.h"
46 #endif
47 #include "nlohmann/json.hpp"
48 #include "free_install_params.h"
49 #include "parameters.h"
50 #include "singleton.h"
51 
52 namespace OHOS {
53 namespace AppExecFwk {
54 namespace {
55 constexpr int MAX_EVENT_CALL_BACK_SIZE = 100;
56 constexpr const char* GLOBAL_RESOURCE_BUNDLE_NAME = "ohos.global.systemres";
57 }
BundleDataMgr()58 BundleDataMgr::BundleDataMgr()
59 {
60     InitStateTransferMap();
61 #ifdef BMS_RDB_ENABLE
62     dataStorage_ = std::make_shared<BundleDataStorageRdb>();
63     preInstallDataStorage_ = std::make_shared<PreInstallDataStorageRdb>();
64 #else
65     dataStorage_ = std::make_shared<BundleDataStorageDatabase>();
66     preInstallDataStorage_ = std::make_shared<PreInstallDataStorage>();
67 #endif
68     sandboxAppHelper_ = DelayedSingleton<BundleSandboxAppHelper>::GetInstance();
69     bundleStateStorage_ = std::make_shared<BundleStateStorage>();
70     baseAppUid_ = system::GetIntParameter<int32_t>("const.product.baseappid", Constants::BASE_APP_UID);
71     if (baseAppUid_ < Constants::BASE_APP_UID || baseAppUid_ >= Constants::MAX_APP_UID) {
72         baseAppUid_ = Constants::BASE_APP_UID;
73     }
74     APP_LOGI("BundleDataMgr instance is created");
75 }
76 
~BundleDataMgr()77 BundleDataMgr::~BundleDataMgr()
78 {
79     APP_LOGI("BundleDataMgr instance is destroyed");
80     installStates_.clear();
81     transferStates_.clear();
82     bundleInfos_.clear();
83 }
84 
LoadDataFromPersistentStorage()85 bool BundleDataMgr::LoadDataFromPersistentStorage()
86 {
87     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
88     LoadAllPreInstallBundleInfos(preInstallBundleInfos_);
89     // Judge whether bundleState json db exists.
90     // If it does not exist, create it and return the judgment result.
91     bool bundleStateDbExist = bundleStateStorage_->HasBundleUserInfoJsonDb();
92     if (!dataStorage_->LoadAllData(bundleInfos_)) {
93         APP_LOGE("LoadAllData failed");
94         return false;
95     }
96 
97     if (bundleInfos_.empty()) {
98         APP_LOGW("persistent data is empty");
99         return false;
100     }
101 
102     for (const auto &item : bundleInfos_) {
103         std::lock_guard<std::mutex> stateLock(stateMutex_);
104         installStates_.emplace(item.first, InstallState::INSTALL_SUCCESS);
105     }
106 
107     RestoreUidAndGid();
108     if (!bundleStateDbExist) {
109         // Compatible old bundle status in kV db
110         CompatibleOldBundleStateInKvDb();
111     } else {
112         ResetBundleStateData();
113         // Load all bundle status from json db.
114         LoadAllBundleStateDataFromJsonDb();
115     }
116 
117     SetInitialUserFlag(true);
118     return true;
119 }
120 
CompatibleOldBundleStateInKvDb()121 void BundleDataMgr::CompatibleOldBundleStateInKvDb()
122 {
123     for (auto& bundleInfoItem : bundleInfos_) {
124         for (auto& innerBundleUserInfoItem : bundleInfoItem.second.GetInnerBundleUserInfos()) {
125             auto& bundleUserInfo = innerBundleUserInfoItem.second.bundleUserInfo;
126             if (bundleUserInfo.IsInitialState()) {
127                 continue;
128             }
129 
130             // save old bundle state to json db
131             bundleStateStorage_->SaveBundleStateStorage(
132                 bundleInfoItem.first, bundleUserInfo.userId, bundleUserInfo);
133         }
134     }
135 }
136 
LoadAllBundleStateDataFromJsonDb()137 void BundleDataMgr::LoadAllBundleStateDataFromJsonDb()
138 {
139     APP_LOGD("Load all bundle state start");
140     std::map<std::string, std::map<int32_t, BundleUserInfo>> bundleStateInfos;
141     if (!bundleStateStorage_->LoadAllBundleStateData(bundleStateInfos) || bundleStateInfos.empty()) {
142         APP_LOGE("Load all bundle state failed");
143         return;
144     }
145 
146     for (auto& bundleState : bundleStateInfos) {
147         auto infoItem = bundleInfos_.find(bundleState.first);
148         if (infoItem == bundleInfos_.end()) {
149             APP_LOGE("BundleName(%{public}s) not exist in cache", bundleState.first.c_str());
150             continue;
151         }
152 
153         InnerBundleInfo& newInfo = infoItem->second;
154         for (auto& bundleUserState : bundleState.second) {
155             auto& tempUserInfo = bundleUserState.second;
156             newInfo.SetApplicationEnabled(tempUserInfo.enabled, bundleUserState.first);
157             for (auto& disabledAbility : tempUserInfo.disabledAbilities) {
158                 newInfo.SetAbilityEnabled(bundleState.first, "", disabledAbility, false, bundleUserState.first);
159             }
160         }
161     }
162 
163     APP_LOGD("Load all bundle state end");
164 }
165 
ResetBundleStateData()166 void BundleDataMgr::ResetBundleStateData()
167 {
168     for (auto& bundleInfoItem : bundleInfos_) {
169         bundleInfoItem.second.ResetBundleState(Constants::ALL_USERID);
170     }
171 }
172 
UpdateBundleInstallState(const std::string & bundleName,const InstallState state)173 bool BundleDataMgr::UpdateBundleInstallState(const std::string &bundleName, const InstallState state)
174 {
175     if (bundleName.empty()) {
176         APP_LOGW("update result:fail, reason:bundle name is empty");
177         return false;
178     }
179 
180     // always keep lock bundleInfoMutex_ before locking stateMutex_ to avoid deadlock
181     std::lock_guard<std::mutex> lck(bundleInfoMutex_);
182     std::lock_guard<std::mutex> lock(stateMutex_);
183     auto item = installStates_.find(bundleName);
184     if (item == installStates_.end()) {
185         if (state == InstallState::INSTALL_START) {
186             installStates_.emplace(bundleName, state);
187             APP_LOGD("update result:success, state:INSTALL_START");
188             return true;
189         }
190         APP_LOGW("update result:fail, reason:incorrect state");
191         return false;
192     }
193 
194     auto stateRange = transferStates_.equal_range(state);
195     for (auto previousState = stateRange.first; previousState != stateRange.second; ++previousState) {
196         if (item->second == previousState->second) {
197             APP_LOGD("update result:success, current:%{public}d, state:%{public}d", previousState->second, state);
198             if (IsDeleteDataState(state)) {
199                 installStates_.erase(item);
200                 DeleteBundleInfo(bundleName, state);
201                 return true;
202             }
203             item->second = state;
204             return true;
205         }
206     }
207     APP_LOGW("update result:fail, reason:incorrect current:%{public}d, state:%{public}d", item->second, state);
208     return false;
209 }
210 
AddInnerBundleInfo(const std::string & bundleName,InnerBundleInfo & info)211 bool BundleDataMgr::AddInnerBundleInfo(const std::string &bundleName, InnerBundleInfo &info)
212 {
213     APP_LOGD("to save info:%{public}s", info.GetBundleName().c_str());
214     if (bundleName.empty()) {
215         APP_LOGW("save info fail, empty bundle name");
216         return false;
217     }
218 
219     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
220     auto infoItem = bundleInfos_.find(bundleName);
221     if (infoItem != bundleInfos_.end()) {
222         APP_LOGE("bundle info already exist");
223         return false;
224     }
225     std::lock_guard<std::mutex> stateLock(stateMutex_);
226     auto statusItem = installStates_.find(bundleName);
227     if (statusItem == installStates_.end()) {
228         APP_LOGE("save info fail, app:%{public}s is not installed", bundleName.c_str());
229         return false;
230     }
231     if (statusItem->second == InstallState::INSTALL_START) {
232         APP_LOGD("save bundle:%{public}s info", bundleName.c_str());
233         if (info.GetBaseApplicationInfo().needAppDetail) {
234             AddAppDetailAbilityInfo(info);
235         }
236         if (dataStorage_->SaveStorageBundleInfo(info)) {
237             APP_LOGI("write storage success bundle:%{public}s", bundleName.c_str());
238             bundleInfos_.emplace(bundleName, info);
239             return true;
240         }
241     }
242     return false;
243 }
244 
AddNewModuleInfo(const std::string & bundleName,const InnerBundleInfo & newInfo,InnerBundleInfo & oldInfo)245 bool BundleDataMgr::AddNewModuleInfo(
246     const std::string &bundleName, const InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo)
247 {
248     APP_LOGD("add new module info module name %{public}s ", newInfo.GetCurrentModulePackage().c_str());
249     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
250     auto infoItem = bundleInfos_.find(bundleName);
251     if (infoItem == bundleInfos_.end()) {
252         APP_LOGE("bundle info not exist");
253         return false;
254     }
255     std::lock_guard<std::mutex> stateLock(stateMutex_);
256     auto statusItem = installStates_.find(bundleName);
257     if (statusItem == installStates_.end()) {
258         APP_LOGE("save info fail, app:%{public}s is not updated", bundleName.c_str());
259         return false;
260     }
261     if (statusItem->second == InstallState::UPDATING_SUCCESS) {
262         APP_LOGD("save bundle:%{public}s info", bundleName.c_str());
263         if (!oldInfo.HasEntry() || oldInfo.GetEntryInstallationFree()) {
264             oldInfo.UpdateBaseBundleInfo(newInfo.GetBaseBundleInfo(), newInfo.HasEntry());
265             oldInfo.UpdateBaseApplicationInfo(newInfo.GetBaseApplicationInfo());
266             oldInfo.UpdateRemovable(
267                 newInfo.IsPreInstallApp(), newInfo.IsRemovable());
268             oldInfo.SetAppPrivilegeLevel(newInfo.GetAppPrivilegeLevel());
269             oldInfo.SetAllowedAcls(newInfo.GetAllowedAcls());
270         }
271         oldInfo.UpdateNativeLibAttrs(newInfo.GetBaseApplicationInfo());
272         oldInfo.UpdateArkNativeAttrs(newInfo.GetBaseApplicationInfo());
273         oldInfo.SetAsanLogPath(newInfo.GetAsanLogPath());
274         oldInfo.SetAsanEnabled(newInfo.GetAsanEnabled());
275         oldInfo.SetBundlePackInfo(newInfo.GetBundlePackInfo());
276         oldInfo.AddModuleInfo(newInfo);
277         oldInfo.UpdateAppDetailAbilityAttrs();
278         oldInfo.SetBundleStatus(InnerBundleInfo::BundleStatus::ENABLED);
279         if (dataStorage_->SaveStorageBundleInfo(oldInfo)) {
280             APP_LOGI("update storage success bundle:%{public}s", bundleName.c_str());
281             bundleInfos_.at(bundleName) = oldInfo;
282             return true;
283         }
284     }
285     return false;
286 }
287 
RemoveModuleInfo(const std::string & bundleName,const std::string & modulePackage,InnerBundleInfo & oldInfo)288 bool BundleDataMgr::RemoveModuleInfo(
289     const std::string &bundleName, const std::string &modulePackage, InnerBundleInfo &oldInfo)
290 {
291     APP_LOGD("remove module info:%{public}s/%{public}s", bundleName.c_str(), modulePackage.c_str());
292     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
293     auto infoItem = bundleInfos_.find(bundleName);
294     if (infoItem == bundleInfos_.end()) {
295         APP_LOGE("bundle info not exist");
296         return false;
297     }
298     std::lock_guard<std::mutex> stateLock(stateMutex_);
299     auto statusItem = installStates_.find(bundleName);
300     if (statusItem == installStates_.end()) {
301         APP_LOGE("save info fail, app:%{public}s is not updated", bundleName.c_str());
302         return false;
303     }
304     if (statusItem->second == InstallState::UNINSTALL_START || statusItem->second == InstallState::ROLL_BACK) {
305         APP_LOGD("save bundle:%{public}s info", bundleName.c_str());
306         oldInfo.RemoveModuleInfo(modulePackage);
307         oldInfo.SetBundleStatus(InnerBundleInfo::BundleStatus::ENABLED);
308         if (dataStorage_->SaveStorageBundleInfo(oldInfo)) {
309             APP_LOGI("update storage success bundle:%{public}s", bundleName.c_str());
310             bundleInfos_.at(bundleName) = oldInfo;
311             return true;
312         }
313         APP_LOGD("after delete modulePackage:%{public}s info", modulePackage.c_str());
314     }
315     return true;
316 }
317 
AddInnerBundleUserInfo(const std::string & bundleName,const InnerBundleUserInfo & newUserInfo)318 bool BundleDataMgr::AddInnerBundleUserInfo(
319     const std::string &bundleName, const InnerBundleUserInfo& newUserInfo)
320 {
321     APP_LOGD("AddInnerBundleUserInfo:%{public}s", bundleName.c_str());
322     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
323     auto infoItem = bundleInfos_.find(bundleName);
324     if (infoItem == bundleInfos_.end()) {
325         APP_LOGE("bundle info not exist");
326         return false;
327     }
328 
329     std::lock_guard<std::mutex> stateLock(stateMutex_);
330     auto& info = bundleInfos_.at(bundleName);
331     info.AddInnerBundleUserInfo(newUserInfo);
332     info.SetBundleStatus(InnerBundleInfo::BundleStatus::ENABLED);
333     if (!dataStorage_->SaveStorageBundleInfo(info)) {
334         APP_LOGE("update storage failed bundle:%{public}s", bundleName.c_str());
335         return false;
336     }
337     return true;
338 }
339 
RemoveInnerBundleUserInfo(const std::string & bundleName,int32_t userId)340 bool BundleDataMgr::RemoveInnerBundleUserInfo(
341     const std::string &bundleName, int32_t userId)
342 {
343     APP_LOGD("RemoveInnerBundleUserInfo:%{public}s", bundleName.c_str());
344     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
345     auto infoItem = bundleInfos_.find(bundleName);
346     if (infoItem == bundleInfos_.end()) {
347         APP_LOGE("bundle info not exist");
348         return false;
349     }
350 
351     std::lock_guard<std::mutex> stateLock(stateMutex_);
352     auto& info = bundleInfos_.at(bundleName);
353     info.RemoveInnerBundleUserInfo(userId);
354     info.SetBundleStatus(InnerBundleInfo::BundleStatus::ENABLED);
355     if (!dataStorage_->SaveStorageBundleInfo(info)) {
356         APP_LOGE("update storage failed bundle:%{public}s", bundleName.c_str());
357         return false;
358     }
359 
360     bundleStateStorage_->DeleteBundleState(bundleName, userId);
361     return true;
362 }
363 
UpdateInnerBundleInfo(const std::string & bundleName,const InnerBundleInfo & newInfo,InnerBundleInfo & oldInfo)364 bool BundleDataMgr::UpdateInnerBundleInfo(
365     const std::string &bundleName, const InnerBundleInfo &newInfo, InnerBundleInfo &oldInfo)
366 {
367     APP_LOGD("UpdateInnerBundleInfo:%{public}s", bundleName.c_str());
368     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
369     auto infoItem = bundleInfos_.find(bundleName);
370     if (infoItem == bundleInfos_.end()) {
371         APP_LOGE("bundle info not exist");
372         return false;
373     }
374     std::lock_guard<std::mutex> stateLock(stateMutex_);
375     auto statusItem = installStates_.find(bundleName);
376     if (statusItem == installStates_.end()) {
377         APP_LOGE("save info fail, app:%{public}s is not updated", bundleName.c_str());
378         return false;
379     }
380     // ROLL_BACK and USER_CHANGE should not be here
381     if (statusItem->second == InstallState::UPDATING_SUCCESS
382         || statusItem->second == InstallState::ROLL_BACK
383         || statusItem->second == InstallState::USER_CHANGE) {
384         APP_LOGD("begin to update, bundleName : %{public}s, moduleName : %{public}s",
385             bundleName.c_str(), newInfo.GetCurrentModulePackage().c_str());
386         bool needAppDetail = oldInfo.GetBaseApplicationInfo().needAppDetail;
387         bool isOldInfoHasEntry = oldInfo.HasEntry();
388         oldInfo.UpdateModuleInfo(newInfo);
389         // 1.exist entry, update entry.
390         // 2.only exist feature, update feature.
391         if (newInfo.HasEntry() || !isOldInfoHasEntry || oldInfo.GetEntryInstallationFree()) {
392             oldInfo.UpdateBaseBundleInfo(newInfo.GetBaseBundleInfo(), newInfo.HasEntry());
393             oldInfo.UpdateBaseApplicationInfo(newInfo.GetBaseApplicationInfo());
394             oldInfo.UpdateRemovable(
395                 newInfo.IsPreInstallApp(), newInfo.IsRemovable());
396             oldInfo.SetAppType(newInfo.GetAppType());
397             oldInfo.SetAppFeature(newInfo.GetAppFeature());
398             oldInfo.SetAppPrivilegeLevel(newInfo.GetAppPrivilegeLevel());
399             oldInfo.SetAllowedAcls(newInfo.GetAllowedAcls());
400         }
401         oldInfo.UpdateAppDetailAbilityAttrs();
402         if (!needAppDetail && oldInfo.GetBaseApplicationInfo().needAppDetail) {
403             AddAppDetailAbilityInfo(oldInfo);
404         }
405         oldInfo.UpdateNativeLibAttrs(newInfo.GetBaseApplicationInfo());
406         oldInfo.UpdateArkNativeAttrs(newInfo.GetBaseApplicationInfo());
407         oldInfo.SetAsanLogPath(newInfo.GetAsanLogPath());
408         oldInfo.SetAsanEnabled(newInfo.GetAsanEnabled());
409         oldInfo.SetAppCrowdtestDeadline(newInfo.GetAppCrowdtestDeadline());
410         oldInfo.SetBundlePackInfo(newInfo.GetBundlePackInfo());
411         oldInfo.SetBundleStatus(InnerBundleInfo::BundleStatus::ENABLED);
412         oldInfo.SetAppProvisionMetadata(newInfo.GetAppProvisionMetadata());
413         if (!dataStorage_->SaveStorageBundleInfo(oldInfo)) {
414             APP_LOGE("update storage failed bundle:%{public}s", bundleName.c_str());
415             return false;
416         }
417         APP_LOGI("update storage success bundle:%{public}s", bundleName.c_str());
418         bundleInfos_.at(bundleName) = oldInfo;
419         return true;
420     }
421     return false;
422 }
423 
QueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,int32_t appIndex) const424 bool BundleDataMgr::QueryAbilityInfo(const Want &want, int32_t flags, int32_t userId, AbilityInfo &abilityInfo,
425     int32_t appIndex) const
426 {
427     int32_t requestUserId = GetUserId(userId);
428     if (requestUserId == Constants::INVALID_USERID) {
429         return false;
430     }
431 
432     ElementName element = want.GetElement();
433     std::string bundleName = element.GetBundleName();
434     std::string abilityName = element.GetAbilityName();
435     APP_LOGD("QueryAbilityInfo bundle name:%{public}s, ability name:%{public}s",
436         bundleName.c_str(), abilityName.c_str());
437     // explicit query
438     if (!bundleName.empty() && !abilityName.empty()) {
439         bool ret = ExplicitQueryAbilityInfo(want, flags, requestUserId, abilityInfo, appIndex);
440         if (!ret) {
441             APP_LOGE("explicit queryAbilityInfo error");
442             return false;
443         }
444         return true;
445     }
446     std::vector<AbilityInfo> abilityInfos;
447     bool ret = ImplicitQueryAbilityInfos(want, flags, requestUserId, abilityInfos, appIndex);
448     if (!ret) {
449         APP_LOGE("implicit queryAbilityInfos error");
450         return false;
451     }
452     if (abilityInfos.size() == 0) {
453         APP_LOGE("no matching abilityInfo");
454         return false;
455     }
456     abilityInfo = abilityInfos[0];
457     return true;
458 }
459 
QueryAbilityInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos) const460 bool BundleDataMgr::QueryAbilityInfos(
461     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos) const
462 {
463     int32_t requestUserId = GetUserId(userId);
464     if (requestUserId == Constants::INVALID_USERID) {
465         return false;
466     }
467 
468     ElementName element = want.GetElement();
469     std::string bundleName = element.GetBundleName();
470     std::string abilityName = element.GetAbilityName();
471     APP_LOGD("QueryAbilityInfos bundle name:%{public}s, ability name:%{public}s",
472         bundleName.c_str(), abilityName.c_str());
473     // explicit query
474     if (!bundleName.empty() && !abilityName.empty()) {
475         AbilityInfo abilityInfo;
476         bool ret = ExplicitQueryAbilityInfo(want, flags, requestUserId, abilityInfo);
477         if (!ret) {
478             APP_LOGE("explicit queryAbilityInfo error");
479             return false;
480         }
481         abilityInfos.emplace_back(abilityInfo);
482         return true;
483     }
484     // implicit query
485     bool ret = ImplicitQueryAbilityInfos(want, flags, requestUserId, abilityInfos);
486     if (!ret) {
487         APP_LOGE("implicit queryAbilityInfos error");
488         return false;
489     }
490     if (abilityInfos.size() == 0) {
491         APP_LOGE("no matching abilityInfo");
492         return false;
493     }
494     return true;
495 }
496 
QueryAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos) const497 ErrCode BundleDataMgr::QueryAbilityInfosV9(
498     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos) const
499 {
500     int32_t requestUserId = GetUserId(userId);
501     if (requestUserId == Constants::INVALID_USERID) {
502         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
503     }
504 
505     ElementName element = want.GetElement();
506     std::string bundleName = element.GetBundleName();
507     std::string abilityName = element.GetAbilityName();
508     APP_LOGD("QueryAbilityInfosV9 bundle name:%{public}s, ability name:%{public}s",
509         bundleName.c_str(), abilityName.c_str());
510     // explicit query
511     if (!bundleName.empty() && !abilityName.empty()) {
512         AbilityInfo abilityInfo;
513         ErrCode ret = ExplicitQueryAbilityInfoV9(want, flags, requestUserId, abilityInfo);
514         if (ret != ERR_OK) {
515             APP_LOGE("explicit queryAbilityInfoV9 error");
516             return ret;
517         }
518         abilityInfos.emplace_back(abilityInfo);
519         return ERR_OK;
520     }
521     // implicit query
522     ErrCode ret = ImplicitQueryAbilityInfosV9(want, flags, requestUserId, abilityInfos);
523     if (ret != ERR_OK) {
524         APP_LOGE("implicit queryAbilityInfosV9 error");
525         return ret;
526     }
527     if (abilityInfos.empty()) {
528         APP_LOGE("no matching abilityInfo");
529         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
530     }
531     return ERR_OK;
532 }
533 
ExplicitQueryAbilityInfo(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,int32_t appIndex) const534 bool BundleDataMgr::ExplicitQueryAbilityInfo(const Want &want, int32_t flags, int32_t userId,
535     AbilityInfo &abilityInfo, int32_t appIndex) const
536 {
537     ElementName element = want.GetElement();
538     std::string bundleName = element.GetBundleName();
539     std::string abilityName = element.GetAbilityName();
540     std::string moduleName = element.GetModuleName();
541     APP_LOGD("ExplicitQueryAbilityInfo bundleName:%{public}s, moduleName:%{public}s, abilityName:%{public}s",
542         bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
543     APP_LOGD("flags:%{public}d, userId:%{public}d", flags, userId);
544 
545     int32_t requestUserId = GetUserId(userId);
546     if (requestUserId == Constants::INVALID_USERID) {
547         return false;
548     }
549 
550     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
551     InnerBundleInfo innerBundleInfo;
552     if ((appIndex == 0) && (!GetInnerBundleInfoWithFlags(bundleName, flags, innerBundleInfo, requestUserId))) {
553         APP_LOGE("ExplicitQueryAbiliyInfo failed");
554         return false;
555     }
556     // explict query from sandbox manager
557     if (appIndex > 0) {
558         if (sandboxAppHelper_ == nullptr) {
559             APP_LOGE("sandboxAppHelper_ is nullptr");
560             return false;
561         }
562         auto ret = sandboxAppHelper_->GetSandboxAppInfo(bundleName, appIndex, requestUserId, innerBundleInfo);
563         if (ret != ERR_OK) {
564             APP_LOGE("obtain innerBundleInfo of sandbox app failed due to errCode %{public}d", ret);
565             return false;
566         }
567     }
568 
569     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
570     auto ability = innerBundleInfo.FindAbilityInfo(bundleName, moduleName, abilityName, responseUserId);
571     if (!ability) {
572         APP_LOGE("ability not found");
573         return false;
574     }
575     return QueryAbilityInfoWithFlags(ability, flags, responseUserId, innerBundleInfo, abilityInfo);
576 }
577 
ExplicitQueryAbilityInfoV9(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,int32_t appIndex) const578 ErrCode BundleDataMgr::ExplicitQueryAbilityInfoV9(const Want &want, int32_t flags, int32_t userId,
579     AbilityInfo &abilityInfo, int32_t appIndex) const
580 {
581     ElementName element = want.GetElement();
582     std::string bundleName = element.GetBundleName();
583     std::string abilityName = element.GetAbilityName();
584     std::string moduleName = element.GetModuleName();
585     APP_LOGD("ExplicitQueryAbilityInfoV9 bundleName:%{public}s, moduleName:%{public}s, abilityName:%{public}s",
586         bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
587     APP_LOGD("flags:%{public}d, userId:%{public}d", flags, userId);
588     int32_t requestUserId = GetUserId(userId);
589     if (requestUserId == Constants::INVALID_USERID) {
590         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
591     }
592     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
593     InnerBundleInfo innerBundleInfo;
594     if (appIndex == 0) {
595         ErrCode ret = GetInnerBundleInfoWithFlagsV9(bundleName, flags, innerBundleInfo, requestUserId);
596         if (ret != ERR_OK) {
597             APP_LOGE("ExplicitQueryAbilityInfoV9 failed");
598             return ret;
599         }
600     }
601     // explict query from sandbox manager
602     if (appIndex > 0) {
603         if (sandboxAppHelper_ == nullptr) {
604             APP_LOGE("sandboxAppHelper_ is nullptr");
605             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
606         }
607         auto ret = sandboxAppHelper_->GetSandboxAppInfo(bundleName, appIndex, requestUserId, innerBundleInfo);
608         if (ret != ERR_OK) {
609             APP_LOGE("obtain innerBundleInfo of sandbox app failed due to errCode %{public}d", ret);
610             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
611         }
612     }
613 
614     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
615     auto ability = innerBundleInfo.FindAbilityInfoV9(bundleName, moduleName, abilityName);
616     if (!ability) {
617         APP_LOGE("ability not found");
618         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
619     }
620 
621     return QueryAbilityInfoWithFlagsV9(ability, flags, responseUserId, innerBundleInfo, abilityInfo);
622 }
623 
FilterAbilityInfosByModuleName(const std::string & moduleName,std::vector<AbilityInfo> & abilityInfos) const624 void BundleDataMgr::FilterAbilityInfosByModuleName(const std::string &moduleName,
625     std::vector<AbilityInfo> &abilityInfos) const
626 {
627     APP_LOGD("BundleDataMgr::FilterAbilityInfosByModuleName moduleName: %{public}s", moduleName.c_str());
628     if (moduleName.empty()) {
629         return;
630     }
631     for (auto iter = abilityInfos.begin(); iter != abilityInfos.end();) {
632         if (iter->moduleName != moduleName) {
633             iter = abilityInfos.erase(iter);
634         } else {
635             ++iter;
636         }
637     }
638 }
639 
ImplicitQueryAbilityInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,int32_t appIndex) const640 bool BundleDataMgr::ImplicitQueryAbilityInfos(
641     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos, int32_t appIndex) const
642 {
643     int32_t requestUserId = GetUserId(userId);
644     if (requestUserId == Constants::INVALID_USERID) {
645         return false;
646     }
647 
648     if (want.GetAction().empty() && want.GetEntities().empty()
649         && want.GetUriString().empty() && want.GetType().empty()) {
650         APP_LOGE("param invalid");
651         return false;
652     }
653     APP_LOGD("action:%{public}s, uri:%{private}s, type:%{public}s",
654         want.GetAction().c_str(), want.GetUriString().c_str(), want.GetType().c_str());
655     APP_LOGD("flags:%{public}d, userId:%{public}d", flags, userId);
656     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
657     if (bundleInfos_.empty()) {
658         APP_LOGE("bundleInfos_ is empty");
659         return false;
660     }
661     std::string bundleName = want.GetElement().GetBundleName();
662     if (!bundleName.empty()) {
663         // query in current bundleName
664         if (!ImplicitQueryCurAbilityInfos(want, flags, requestUserId, abilityInfos, appIndex)) {
665             APP_LOGE("ImplicitQueryCurAbilityInfos failed");
666             return false;
667         }
668     } else {
669         // query all
670         ImplicitQueryAllAbilityInfos(want, flags, requestUserId, abilityInfos, appIndex);
671     }
672     // sort by priority, descending order.
673     if (abilityInfos.size() > 1) {
674         std::stable_sort(abilityInfos.begin(), abilityInfos.end(),
675             [](AbilityInfo a, AbilityInfo b) { return a.priority > b.priority; });
676     }
677     return true;
678 }
679 
ImplicitQueryAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,int32_t appIndex) const680 ErrCode BundleDataMgr::ImplicitQueryAbilityInfosV9(
681     const Want &want, int32_t flags, int32_t userId, std::vector<AbilityInfo> &abilityInfos, int32_t appIndex) const
682 {
683     int32_t requestUserId = GetUserId(userId);
684     if (requestUserId == Constants::INVALID_USERID) {
685         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
686     }
687 
688     if (want.GetAction().empty() && want.GetEntities().empty()
689         && want.GetUriString().empty() && want.GetType().empty()) {
690         APP_LOGE("param invalid");
691         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
692     }
693     APP_LOGD("action:%{public}s, uri:%{private}s, type:%{public}s",
694         want.GetAction().c_str(), want.GetUriString().c_str(), want.GetType().c_str());
695     APP_LOGD("flags:%{public}d, userId:%{public}d", flags, userId);
696     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
697     if (bundleInfos_.empty()) {
698         APP_LOGE("bundleInfos_ is empty");
699         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
700     }
701     std::string bundleName = want.GetElement().GetBundleName();
702     if (!bundleName.empty()) {
703         // query in current bundleName
704         ErrCode ret = ImplicitQueryCurAbilityInfosV9(want, flags, requestUserId, abilityInfos, appIndex);
705         if (ret != ERR_OK) {
706             APP_LOGE("ImplicitQueryCurAbilityInfosV9 failed");
707             return ret;
708         }
709     } else {
710         // query all
711         ImplicitQueryAllAbilityInfosV9(want, flags, requestUserId, abilityInfos, appIndex);
712     }
713     // sort by priority, descending order.
714     if (abilityInfos.size() > 1) {
715         std::stable_sort(abilityInfos.begin(), abilityInfos.end(),
716             [](AbilityInfo a, AbilityInfo b) { return a.priority > b.priority; });
717     }
718     return ERR_OK;
719 }
720 
QueryAbilityInfoWithFlags(const std::optional<AbilityInfo> & option,int32_t flags,int32_t userId,const InnerBundleInfo & innerBundleInfo,AbilityInfo & info) const721 bool BundleDataMgr::QueryAbilityInfoWithFlags(const std::optional<AbilityInfo> &option, int32_t flags, int32_t userId,
722     const InnerBundleInfo &innerBundleInfo, AbilityInfo &info) const
723 {
724     APP_LOGD("begin to QueryAbilityInfoWithFlags.");
725     if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_SYSTEMAPP_ONLY) == GET_ABILITY_INFO_SYSTEMAPP_ONLY &&
726         !innerBundleInfo.IsSystemApp()) {
727         APP_LOGE("no system app ability info for this calling");
728         return false;
729     }
730     if (!(static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_DISABLE)) {
731         if (!innerBundleInfo.IsAbilityEnabled((*option), userId)) {
732             APP_LOGE("ability:%{public}s is disabled", option->name.c_str());
733             return false;
734         }
735     }
736     info = (*option);
737     if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_PERMISSION) != GET_ABILITY_INFO_WITH_PERMISSION) {
738         info.permissions.clear();
739     }
740     if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_METADATA) != GET_ABILITY_INFO_WITH_METADATA) {
741         info.metaData.customizeData.clear();
742         info.metadata.clear();
743     }
744     if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_APPLICATION) == GET_ABILITY_INFO_WITH_APPLICATION) {
745         innerBundleInfo.GetApplicationInfo(
746             ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, userId, info.applicationInfo);
747     }
748     // set uid for NAPI cache use
749     InnerBundleUserInfo innerBundleUserInfo;
750     if (innerBundleInfo.GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
751         info.uid = innerBundleUserInfo.uid;
752     }
753     return true;
754 }
755 
QueryAbilityInfoWithFlagsV9(const std::optional<AbilityInfo> & option,int32_t flags,int32_t userId,const InnerBundleInfo & innerBundleInfo,AbilityInfo & info) const756 ErrCode BundleDataMgr::QueryAbilityInfoWithFlagsV9(const std::optional<AbilityInfo> &option,
757     int32_t flags, int32_t userId, const InnerBundleInfo &innerBundleInfo, AbilityInfo &info) const
758 {
759     APP_LOGD("begin to QueryAbilityInfoWithFlagsV9.");
760     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_ONLY_SYSTEM_APP)) ==
761         static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_ONLY_SYSTEM_APP) &&
762         !innerBundleInfo.IsSystemApp()) {
763         APP_LOGE("target not system app");
764         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
765     }
766     if (!(static_cast<uint32_t>(flags) & static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_DISABLE))) {
767         if (!innerBundleInfo.IsAbilityEnabled((*option), userId)) {
768             APP_LOGE("ability:%{public}s is disabled", option->name.c_str());
769             return ERR_BUNDLE_MANAGER_ABILITY_DISABLED;
770         }
771     }
772     info = (*option);
773     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION)) !=
774         static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION)) {
775         info.permissions.clear();
776     }
777     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_METADATA)) !=
778         static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_METADATA)) {
779         info.metaData.customizeData.clear();
780         info.metadata.clear();
781     }
782     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION)) ==
783         static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION)) {
784         innerBundleInfo.GetApplicationInfoV9(static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_DEFAULT),
785             userId, info.applicationInfo);
786     }
787     // set uid for NAPI cache use
788     InnerBundleUserInfo innerBundleUserInfo;
789     if (innerBundleInfo.GetInnerBundleUserInfo(userId, innerBundleUserInfo)) {
790         info.uid = innerBundleUserInfo.uid;
791     }
792     return ERR_OK;
793 }
794 
ImplicitQueryCurAbilityInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,int32_t appIndex) const795 bool BundleDataMgr::ImplicitQueryCurAbilityInfos(const Want &want, int32_t flags, int32_t userId,
796     std::vector<AbilityInfo> &abilityInfos, int32_t appIndex) const
797 {
798     APP_LOGD("begin to ImplicitQueryCurAbilityInfos.");
799     std::string bundleName = want.GetElement().GetBundleName();
800     InnerBundleInfo innerBundleInfo;
801     if ((appIndex == 0) && (!GetInnerBundleInfoWithFlags(bundleName, flags, innerBundleInfo, userId))) {
802         APP_LOGE("ImplicitQueryCurAbilityInfos failed");
803         return false;
804     }
805     if (appIndex > 0) {
806         if (sandboxAppHelper_ == nullptr) {
807             APP_LOGE("sandboxAppHelper_ is nullptr");
808             return false;
809         }
810         auto ret = sandboxAppHelper_->GetSandboxAppInfo(bundleName, appIndex, userId, innerBundleInfo);
811         if (ret != ERR_OK) {
812             APP_LOGE("obtain innerBundleInfo of sandbox app failed due to errCode %{public}d", ret);
813             return false;
814         }
815     }
816     int32_t responseUserId = innerBundleInfo.GetResponseUserId(userId);
817     GetMatchAbilityInfos(want, flags, innerBundleInfo, responseUserId, abilityInfos);
818     FilterAbilityInfosByModuleName(want.GetElement().GetModuleName(), abilityInfos);
819     return true;
820 }
821 
ImplicitQueryCurAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,int32_t appIndex) const822 ErrCode BundleDataMgr::ImplicitQueryCurAbilityInfosV9(const Want &want, int32_t flags, int32_t userId,
823     std::vector<AbilityInfo> &abilityInfos, int32_t appIndex) const
824 {
825     APP_LOGD("begin to ImplicitQueryCurAbilityInfosV9.");
826     std::string bundleName = want.GetElement().GetBundleName();
827     InnerBundleInfo innerBundleInfo;
828     if (appIndex == 0) {
829         ErrCode ret = GetInnerBundleInfoWithFlagsV9(bundleName, flags, innerBundleInfo, userId);
830         if (ret != ERR_OK) {
831             APP_LOGE("ImplicitQueryCurAbilityInfosV9 failed");
832             return ret;
833         }
834     }
835     if (appIndex > 0) {
836         if (sandboxAppHelper_ == nullptr) {
837             APP_LOGE("sandboxAppHelper_ is nullptr");
838             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
839         }
840         auto ret = sandboxAppHelper_->GetSandboxAppInfo(bundleName, appIndex, userId, innerBundleInfo);
841         if (ret != ERR_OK) {
842             APP_LOGE("obtain innerBundleInfo of sandbox app failed due to errCode %{public}d", ret);
843             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
844         }
845     }
846     int32_t responseUserId = innerBundleInfo.GetResponseUserId(userId);
847     GetMatchAbilityInfosV9(want, flags, innerBundleInfo, responseUserId, abilityInfos);
848     FilterAbilityInfosByModuleName(want.GetElement().GetModuleName(), abilityInfos);
849     return ERR_OK;
850 }
851 
ImplicitQueryAllAbilityInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,int32_t appIndex) const852 void BundleDataMgr::ImplicitQueryAllAbilityInfos(const Want &want, int32_t flags, int32_t userId,
853     std::vector<AbilityInfo> &abilityInfos, int32_t appIndex) const
854 {
855     APP_LOGD("begin to ImplicitQueryAllAbilityInfos.");
856     // query from bundleInfos_
857     if (appIndex == 0) {
858         for (const auto &item : bundleInfos_) {
859             InnerBundleInfo innerBundleInfo;
860             if (!GetInnerBundleInfoWithFlags(
861                 item.first, flags, innerBundleInfo, userId)) {
862                 APP_LOGW("ImplicitQueryAllAbilityInfos failed");
863                 continue;
864             }
865 
866             int32_t responseUserId = innerBundleInfo.GetResponseUserId(userId);
867             GetMatchAbilityInfos(want, flags, innerBundleInfo, responseUserId, abilityInfos);
868         }
869     } else {
870         // query from sandbox manager for sandbox bundle
871         if (sandboxAppHelper_ == nullptr) {
872             APP_LOGE("sandboxAppHelper_ is nullptr");
873             return;
874         }
875         auto sandboxMap = sandboxAppHelper_->GetSandboxAppInfoMap();
876         for (const auto &item : sandboxMap) {
877             InnerBundleInfo info;
878             size_t pos = item.first.rfind(Constants::FILE_UNDERLINE);
879             if (pos == std::string::npos) {
880                 APP_LOGW("sandbox map contains invalid element");
881                 continue;
882             }
883             std::string innerBundleName = item.first.substr(0, pos);
884             if (sandboxAppHelper_->GetSandboxAppInfo(innerBundleName, appIndex, userId, info) != ERR_OK) {
885                 APP_LOGW("obtain innerBundleInfo of sandbox app failed");
886                 continue;
887             }
888 
889             int32_t responseUserId = info.GetResponseUserId(userId);
890             GetMatchAbilityInfos(want, flags, info, responseUserId, abilityInfos);
891         }
892     }
893     APP_LOGD("finish to ImplicitQueryAllAbilityInfos.");
894 }
895 
ImplicitQueryAllAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,int32_t appIndex) const896 void BundleDataMgr::ImplicitQueryAllAbilityInfosV9(const Want &want, int32_t flags, int32_t userId,
897     std::vector<AbilityInfo> &abilityInfos, int32_t appIndex) const
898 {
899     APP_LOGD("begin to ImplicitQueryAllAbilityInfosV9.");
900     // query from bundleInfos_
901     if (appIndex == 0) {
902         for (const auto &item : bundleInfos_) {
903             InnerBundleInfo innerBundleInfo;
904             ErrCode ret = GetInnerBundleInfoWithFlagsV9(item.first, flags, innerBundleInfo, userId);
905             if (ret != ERR_OK) {
906                 APP_LOGW("ImplicitQueryAllAbilityInfosV9 failed");
907                 continue;
908             }
909 
910             int32_t responseUserId = innerBundleInfo.GetResponseUserId(userId);
911             GetMatchAbilityInfosV9(want, flags, innerBundleInfo, responseUserId, abilityInfos);
912         }
913     } else {
914         // query from sandbox manager for sandbox bundle
915         if (sandboxAppHelper_ == nullptr) {
916             APP_LOGE("sandboxAppHelper_ is nullptr");
917             return;
918         }
919         auto sandboxMap = sandboxAppHelper_->GetSandboxAppInfoMap();
920         for (const auto &item : sandboxMap) {
921             InnerBundleInfo info;
922             size_t pos = item.first.rfind(Constants::FILE_UNDERLINE);
923             if (pos == std::string::npos) {
924                 APP_LOGW("sandbox map contains invalid element");
925                 continue;
926             }
927             std::string innerBundleName = item.first.substr(0, pos);
928             if (sandboxAppHelper_->GetSandboxAppInfo(innerBundleName, appIndex, userId, info) != ERR_OK) {
929                 APP_LOGW("obtain innerBundleInfo of sandbox app failed");
930                 continue;
931             }
932 
933             int32_t responseUserId = info.GetResponseUserId(userId);
934             GetMatchAbilityInfosV9(want, flags, info, responseUserId, abilityInfos);
935         }
936     }
937     APP_LOGD("finish to ImplicitQueryAllAbilityInfosV9.");
938 }
939 
GetMatchAbilityInfos(const Want & want,int32_t flags,const InnerBundleInfo & info,int32_t userId,std::vector<AbilityInfo> & abilityInfos) const940 void BundleDataMgr::GetMatchAbilityInfos(const Want &want, int32_t flags,
941     const InnerBundleInfo &info, int32_t userId, std::vector<AbilityInfo> &abilityInfos) const
942 {
943     if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_SYSTEMAPP_ONLY) == GET_ABILITY_INFO_SYSTEMAPP_ONLY &&
944         !info.IsSystemApp()) {
945         return;
946     }
947     std::map<std::string, std::vector<Skill>> skillInfos = info.GetInnerSkillInfos();
948     for (const auto &abilityInfoPair : info.GetInnerAbilityInfos()) {
949         auto skillsPair = skillInfos.find(abilityInfoPair.first);
950         if (skillsPair == skillInfos.end()) {
951             continue;
952         }
953         for (const Skill &skill : skillsPair->second) {
954             if (skill.Match(want)) {
955                 AbilityInfo abilityinfo = abilityInfoPair.second;
956                 if (abilityinfo.name == Constants::APP_DETAIL_ABILITY) {
957                     continue;
958                 }
959                 if (!(static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_DISABLE)) {
960                     if (!info.IsAbilityEnabled(abilityinfo, GetUserId(userId))) {
961                         APP_LOGW("GetMatchAbilityInfos %{public}s is disabled", abilityinfo.name.c_str());
962                         continue;
963                     }
964                 }
965                 if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_APPLICATION) ==
966                     GET_ABILITY_INFO_WITH_APPLICATION) {
967                     info.GetApplicationInfo(
968                         ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, userId,
969                         abilityinfo.applicationInfo);
970                 }
971                 if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_PERMISSION) !=
972                     GET_ABILITY_INFO_WITH_PERMISSION) {
973                     abilityinfo.permissions.clear();
974                 }
975                 if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_METADATA) != GET_ABILITY_INFO_WITH_METADATA) {
976                     abilityinfo.metaData.customizeData.clear();
977                     abilityinfo.metadata.clear();
978                 }
979                 abilityInfos.emplace_back(abilityinfo);
980                 break;
981             }
982         }
983     }
984 }
985 
GetMatchAbilityInfosV9(const Want & want,int32_t flags,const InnerBundleInfo & info,int32_t userId,std::vector<AbilityInfo> & abilityInfos) const986 void BundleDataMgr::GetMatchAbilityInfosV9(const Want &want, int32_t flags,
987     const InnerBundleInfo &info, int32_t userId, std::vector<AbilityInfo> &abilityInfos) const
988 {
989     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_ONLY_SYSTEM_APP)) ==
990         static_cast<int32_t>((GetAbilityInfoFlag::GET_ABILITY_INFO_ONLY_SYSTEM_APP)) &&
991         !info.IsSystemApp()) {
992         APP_LOGE("target not system app");
993         return;
994     }
995     std::map<std::string, std::vector<Skill>> skillInfos = info.GetInnerSkillInfos();
996     for (const auto &abilityInfoPair : info.GetInnerAbilityInfos()) {
997         auto skillsPair = skillInfos.find(abilityInfoPair.first);
998         if (skillsPair == skillInfos.end()) {
999             continue;
1000         }
1001         for (const Skill &skill : skillsPair->second) {
1002             if (skill.Match(want)) {
1003                 AbilityInfo abilityinfo = abilityInfoPair.second;
1004                 if (abilityinfo.name == Constants::APP_DETAIL_ABILITY) {
1005                     continue;
1006                 }
1007                 if (!(static_cast<uint32_t>(flags) & static_cast<int32_t>(
1008                     GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_DISABLE))) {
1009                     if (!info.IsAbilityEnabled(abilityinfo, GetUserId(userId))) {
1010                         APP_LOGW("GetMatchAbilityInfos %{public}s is disabled", abilityinfo.name.c_str());
1011                         continue;
1012                     }
1013                 }
1014                 if ((static_cast<uint32_t>(flags) &
1015                     static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION)) ==
1016                     static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION)) {
1017                     info.GetApplicationInfoV9(static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_DEFAULT),
1018                         userId, abilityinfo.applicationInfo);
1019                 }
1020                 if ((static_cast<uint32_t>(flags) &
1021                     static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION)) !=
1022                     static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_PERMISSION)) {
1023                     abilityinfo.permissions.clear();
1024                 }
1025                 if ((static_cast<uint32_t>(flags) &
1026                     static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_METADATA)) !=
1027                     static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_METADATA)) {
1028                     abilityinfo.metaData.customizeData.clear();
1029                     abilityinfo.metadata.clear();
1030                 }
1031                 abilityInfos.emplace_back(abilityinfo);
1032                 break;
1033             }
1034         }
1035     }
1036 }
1037 
GetMatchLauncherAbilityInfos(const Want & want,const InnerBundleInfo & info,std::vector<AbilityInfo> & abilityInfos,int32_t userId) const1038 void BundleDataMgr::GetMatchLauncherAbilityInfos(const Want& want,
1039     const InnerBundleInfo& info, std::vector<AbilityInfo>& abilityInfos, int32_t userId) const
1040 {
1041     int32_t requestUserId = GetUserId(userId);
1042     if (requestUserId == Constants::INVALID_USERID) {
1043         return;
1044     }
1045     int32_t responseUserId = info.GetResponseUserId(requestUserId);
1046     bool isExist = false;
1047     std::map<std::string, std::vector<Skill>> skillInfos = info.GetInnerSkillInfos();
1048     for (const auto& abilityInfoPair : info.GetInnerAbilityInfos()) {
1049         auto skillsPair = skillInfos.find(abilityInfoPair.first);
1050         if (skillsPair == skillInfos.end()) {
1051             continue;
1052         }
1053         for (const Skill& skill : skillsPair->second) {
1054             if (skill.MatchLauncher(want) && (abilityInfoPair.second.type == AbilityType::PAGE)) {
1055                 isExist = true;
1056                 AbilityInfo abilityinfo = abilityInfoPair.second;
1057                 info.GetApplicationInfo(ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT,
1058                     responseUserId, abilityinfo.applicationInfo);
1059                 abilityInfos.emplace_back(abilityinfo);
1060                 break;
1061             }
1062         }
1063     }
1064     // add app detail ability
1065     if (!isExist && info.GetBaseApplicationInfo().needAppDetail) {
1066         std::string bundleName = info.GetBundleName();
1067         APP_LOGD("bundleName: %{public}s add detail ability info.", bundleName.c_str());
1068         std::string moduleName = "";
1069         auto ability = info.FindAbilityInfo(bundleName, moduleName, Constants::APP_DETAIL_ABILITY, responseUserId);
1070         if (!ability) {
1071             APP_LOGE("bundleName: %{public}s can not find app detail ability.", bundleName.c_str());
1072             return;
1073         }
1074         if (!info.GetIsNewVersion()) {
1075             ability->applicationInfo.label = bundleName;
1076         }
1077         abilityInfos.emplace_back(*ability);
1078     }
1079 }
1080 
AddAppDetailAbilityInfo(InnerBundleInfo & info) const1081 void BundleDataMgr::AddAppDetailAbilityInfo(InnerBundleInfo &info) const
1082 {
1083     AbilityInfo appDetailAbility;
1084     appDetailAbility.name = Constants::APP_DETAIL_ABILITY;
1085     appDetailAbility.bundleName = info.GetBundleName();
1086     std::vector<std::string> moduleNameVec;
1087     info.GetModuleNames(moduleNameVec);
1088     if (!moduleNameVec.empty()) {
1089         appDetailAbility.moduleName = moduleNameVec[0];
1090     } else {
1091         APP_LOGE("AddAppDetailAbilityInfo error: %{public}s has no module.", appDetailAbility.bundleName.c_str());
1092     }
1093     appDetailAbility.enabled = true;
1094     appDetailAbility.type = AbilityType::PAGE;
1095     appDetailAbility.package = info.GetCurrentModulePackage();
1096     appDetailAbility.isNativeAbility = true;
1097 
1098     ApplicationInfo applicationInfo = info.GetBaseApplicationInfo();
1099     appDetailAbility.applicationName = applicationInfo.name;
1100     appDetailAbility.labelId = applicationInfo.labelId;
1101     if (!info.GetIsNewVersion()) {
1102         appDetailAbility.labelId = 0;
1103     }
1104     appDetailAbility.iconId = applicationInfo.iconId;
1105     if ((appDetailAbility.iconId == 0) || !info.GetIsNewVersion()) {
1106         APP_LOGD("AddAppDetailAbilityInfo appDetailAbility.iconId is 0.");
1107         // get system resource icon Id
1108         auto iter = bundleInfos_.find(GLOBAL_RESOURCE_BUNDLE_NAME);
1109         if (iter != bundleInfos_.end()) {
1110             APP_LOGD("AddAppDetailAbilityInfo get system resource iconId");
1111             appDetailAbility.iconId = iter->second.GetBaseApplicationInfo().iconId;
1112         } else {
1113             APP_LOGE("AddAppDetailAbilityInfo error: ohos.global.systemres does not exist.");
1114         }
1115     }
1116     // not show in the mission list
1117     appDetailAbility.removeMissionAfterTerminate = true;
1118 
1119     std::string keyName;
1120     keyName.append(appDetailAbility.bundleName).append(".")
1121         .append(appDetailAbility.package).append(".").append(appDetailAbility.name);
1122     info.InsertAbilitiesInfo(keyName, appDetailAbility);
1123 }
1124 
QueryLauncherAbilityInfos(const Want & want,uint32_t userId,std::vector<AbilityInfo> & abilityInfos) const1125 bool BundleDataMgr::QueryLauncherAbilityInfos(
1126     const Want& want, uint32_t userId, std::vector<AbilityInfo>& abilityInfos) const
1127 {
1128     int32_t requestUserId = GetUserId(userId);
1129     if (requestUserId == Constants::INVALID_USERID) {
1130         return false;
1131     }
1132 
1133     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1134     if (bundleInfos_.empty()) {
1135         APP_LOGE("bundleInfos_ is empty");
1136         return false;
1137     }
1138 
1139     ElementName element = want.GetElement();
1140     std::string bundleName = element.GetBundleName();
1141     if (bundleName.empty()) {
1142         // query all launcher ability
1143         for (const auto &item : bundleInfos_) {
1144             const InnerBundleInfo &info = item.second;
1145             if (info.IsDisabled()) {
1146                 APP_LOGI("app %{public}s is disabled", info.GetBundleName().c_str());
1147                 continue;
1148             }
1149             GetMatchLauncherAbilityInfos(want, info, abilityInfos, requestUserId);
1150         }
1151         return true;
1152     } else {
1153         // query definite abilitys by bundle name
1154         auto item = bundleInfos_.find(bundleName);
1155         if (item == bundleInfos_.end()) {
1156             APP_LOGE("no bundleName %{public}s found", bundleName.c_str());
1157             return false;
1158         }
1159         GetMatchLauncherAbilityInfos(want, item->second, abilityInfos, requestUserId);
1160         FilterAbilityInfosByModuleName(element.GetModuleName(), abilityInfos);
1161         return true;
1162     }
1163 }
1164 
QueryAbilityInfoByUri(const std::string & abilityUri,int32_t userId,AbilityInfo & abilityInfo) const1165 bool BundleDataMgr::QueryAbilityInfoByUri(
1166     const std::string &abilityUri, int32_t userId, AbilityInfo &abilityInfo) const
1167 {
1168     APP_LOGD("abilityUri is %{private}s", abilityUri.c_str());
1169     int32_t requestUserId = GetUserId(userId);
1170     if (requestUserId == Constants::INVALID_USERID) {
1171         return false;
1172     }
1173 
1174     if (abilityUri.empty()) {
1175         return false;
1176     }
1177     if (abilityUri.find(Constants::DATA_ABILITY_URI_PREFIX) == std::string::npos) {
1178         return false;
1179     }
1180     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1181     if (bundleInfos_.empty()) {
1182         APP_LOGE("bundleInfos_ data is empty");
1183         return false;
1184     }
1185     std::string noPpefixUri = abilityUri.substr(strlen(Constants::DATA_ABILITY_URI_PREFIX));
1186     auto posFirstSeparator = noPpefixUri.find(Constants::DATA_ABILITY_URI_SEPARATOR);
1187     if (posFirstSeparator == std::string::npos) {
1188         return false;
1189     }
1190     auto posSecondSeparator = noPpefixUri.find(Constants::DATA_ABILITY_URI_SEPARATOR, posFirstSeparator + 1);
1191     std::string uri;
1192     if (posSecondSeparator == std::string::npos) {
1193         uri = noPpefixUri.substr(posFirstSeparator + 1, noPpefixUri.size() - posFirstSeparator - 1);
1194     } else {
1195         uri = noPpefixUri.substr(posFirstSeparator + 1, posSecondSeparator - posFirstSeparator - 1);
1196     }
1197 
1198     for (const auto &item : bundleInfos_) {
1199         const InnerBundleInfo &info = item.second;
1200         if (info.IsDisabled()) {
1201             APP_LOGE("app %{public}s is disabled", info.GetBundleName().c_str());
1202             continue;
1203         }
1204 
1205         int32_t responseUserId = info.GetResponseUserId(requestUserId);
1206         if (!info.GetApplicationEnabled(responseUserId)) {
1207             continue;
1208         }
1209 
1210         auto ability = info.FindAbilityInfoByUri(uri);
1211         if (!ability) {
1212             continue;
1213         }
1214 
1215         abilityInfo = (*ability);
1216         info.GetApplicationInfo(
1217             ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, responseUserId,
1218             abilityInfo.applicationInfo);
1219         return true;
1220     }
1221 
1222     APP_LOGE("query abilityUri(%{private}s) failed.", abilityUri.c_str());
1223     return false;
1224 }
1225 
QueryAbilityInfosByUri(const std::string & abilityUri,std::vector<AbilityInfo> & abilityInfos)1226 bool BundleDataMgr::QueryAbilityInfosByUri(const std::string &abilityUri, std::vector<AbilityInfo> &abilityInfos)
1227 {
1228     APP_LOGI("abilityUri is %{private}s", abilityUri.c_str());
1229     if (abilityUri.empty()) {
1230         return false;
1231     }
1232     if (abilityUri.find(Constants::DATA_ABILITY_URI_PREFIX) == std::string::npos) {
1233         return false;
1234     }
1235     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1236     if (bundleInfos_.empty()) {
1237         APP_LOGI("bundleInfos_ data is empty");
1238         return false;
1239     }
1240     std::string noPpefixUri = abilityUri.substr(strlen(Constants::DATA_ABILITY_URI_PREFIX));
1241     auto posFirstSeparator = noPpefixUri.find(Constants::DATA_ABILITY_URI_SEPARATOR);
1242     if (posFirstSeparator == std::string::npos) {
1243         return false;
1244     }
1245     auto posSecondSeparator = noPpefixUri.find(Constants::DATA_ABILITY_URI_SEPARATOR, posFirstSeparator + 1);
1246     std::string uri;
1247     if (posSecondSeparator == std::string::npos) {
1248         uri = noPpefixUri.substr(posFirstSeparator + 1, noPpefixUri.size() - posFirstSeparator - 1);
1249     } else {
1250         uri = noPpefixUri.substr(posFirstSeparator + 1, posSecondSeparator - posFirstSeparator - 1);
1251     }
1252 
1253     for (auto &item : bundleInfos_) {
1254         InnerBundleInfo &info = item.second;
1255         if (info.IsDisabled()) {
1256             APP_LOGI("app %{public}s is disabled", info.GetBundleName().c_str());
1257             continue;
1258         }
1259         info.FindAbilityInfosByUri(uri, abilityInfos, GetUserId());
1260     }
1261     if (abilityInfos.size() == 0) {
1262         return false;
1263     }
1264 
1265     return true;
1266 }
1267 
GetApplicationInfo(const std::string & appName,int32_t flags,const int userId,ApplicationInfo & appInfo) const1268 bool BundleDataMgr::GetApplicationInfo(
1269     const std::string &appName, int32_t flags, const int userId, ApplicationInfo &appInfo) const
1270 {
1271     int32_t requestUserId = GetUserId(userId);
1272     if (requestUserId == Constants::INVALID_USERID) {
1273         return false;
1274     }
1275 
1276     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1277     InnerBundleInfo innerBundleInfo;
1278     if (!GetInnerBundleInfoWithFlags(appName, flags, innerBundleInfo, requestUserId)) {
1279         APP_LOGE("GetApplicationInfo failed");
1280         return false;
1281     }
1282 
1283     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
1284     innerBundleInfo.GetApplicationInfo(flags, responseUserId, appInfo);
1285     return true;
1286 }
1287 
GetApplicationInfoV9(const std::string & appName,int32_t flags,int32_t userId,ApplicationInfo & appInfo) const1288 ErrCode BundleDataMgr::GetApplicationInfoV9(
1289     const std::string &appName, int32_t flags, int32_t userId, ApplicationInfo &appInfo) const
1290 {
1291     int32_t requestUserId = GetUserId(userId);
1292     if (requestUserId == Constants::INVALID_USERID) {
1293         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1294     }
1295 
1296     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1297     InnerBundleInfo innerBundleInfo;
1298     int32_t flag = 0;
1299     if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE))
1300         == static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE)) {
1301         flag = static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE);
1302     }
1303     auto ret = GetInnerBundleInfoWithBundleFlagsV9(appName, flag, innerBundleInfo, requestUserId);
1304     if (ret != ERR_OK) {
1305         APP_LOGE("GetApplicationInfoV9 failed");
1306         return ret;
1307     }
1308 
1309     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
1310     ret = innerBundleInfo.GetApplicationInfoV9(flags, responseUserId, appInfo);
1311     if (ret != ERR_OK) {
1312         APP_LOGE("GetApplicationInfoV9 failed");
1313         return ret;
1314     }
1315     return ret;
1316 }
1317 
GetApplicationInfos(int32_t flags,const int userId,std::vector<ApplicationInfo> & appInfos) const1318 bool BundleDataMgr::GetApplicationInfos(
1319     int32_t flags, const int userId, std::vector<ApplicationInfo> &appInfos) const
1320 {
1321     int32_t requestUserId = GetUserId(userId);
1322     if (requestUserId == Constants::INVALID_USERID) {
1323         return false;
1324     }
1325 
1326     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1327     if (bundleInfos_.empty()) {
1328         APP_LOGE("bundleInfos_ data is empty");
1329         return false;
1330     }
1331 
1332     bool find = false;
1333     for (const auto &item : bundleInfos_) {
1334         const InnerBundleInfo &info = item.second;
1335         if (info.IsDisabled()) {
1336             APP_LOGE("app %{public}s is disabled", info.GetBundleName().c_str());
1337             continue;
1338         }
1339         int32_t responseUserId = info.GetResponseUserId(requestUserId);
1340         if (!(static_cast<uint32_t>(flags) & GET_APPLICATION_INFO_WITH_DISABLE)
1341             && !info.GetApplicationEnabled(responseUserId)) {
1342             APP_LOGD("bundleName: %{public}s is disabled", info.GetBundleName().c_str());
1343             continue;
1344         }
1345         ApplicationInfo appInfo;
1346         info.GetApplicationInfo(flags, responseUserId, appInfo);
1347         appInfos.emplace_back(appInfo);
1348         find = true;
1349     }
1350     APP_LOGD("get installed bundles success");
1351     return find;
1352 }
1353 
GetApplicationInfosV9(int32_t flags,int32_t userId,std::vector<ApplicationInfo> & appInfos) const1354 ErrCode BundleDataMgr::GetApplicationInfosV9(
1355     int32_t flags, int32_t userId, std::vector<ApplicationInfo> &appInfos) const
1356 {
1357     int32_t requestUserId = GetUserId(userId);
1358     if (requestUserId == Constants::INVALID_USERID) {
1359         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1360     }
1361 
1362     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1363     if (bundleInfos_.empty()) {
1364         APP_LOGE("bundleInfos_ data is empty");
1365         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1366     }
1367     for (const auto &item : bundleInfos_) {
1368         const InnerBundleInfo &info = item.second;
1369         if (info.IsDisabled()) {
1370             APP_LOGE("app %{public}s is disabled", info.GetBundleName().c_str());
1371             continue;
1372         }
1373         int32_t responseUserId = info.GetResponseUserId(requestUserId);
1374         if (!(static_cast<uint32_t>(flags) &
1375             static_cast<int32_t>(GetApplicationFlag::GET_APPLICATION_INFO_WITH_DISABLE))
1376             && !info.GetApplicationEnabled(responseUserId)) {
1377             APP_LOGD("bundleName: %{public}s is disabled", info.GetBundleName().c_str());
1378             continue;
1379         }
1380         ApplicationInfo appInfo;
1381         auto res = info.GetApplicationInfoV9(flags, responseUserId, appInfo);
1382         if (res != ERR_OK) {
1383             return res;
1384         }
1385         appInfos.emplace_back(appInfo);
1386     }
1387     APP_LOGD("get installed bundles success");
1388     return ERR_OK;
1389 }
1390 
GetBundleInfo(const std::string & bundleName,int32_t flags,BundleInfo & bundleInfo,int32_t userId) const1391 bool BundleDataMgr::GetBundleInfo(
1392     const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId) const
1393 {
1394     std::vector<InnerBundleUserInfo> innerBundleUserInfos;
1395     if (userId == Constants::ANY_USERID) {
1396         if (!GetInnerBundleUserInfos(bundleName, innerBundleUserInfos)) {
1397             APP_LOGE("no userInfos for this bundle(%{public}s)", bundleName.c_str());
1398             return false;
1399         }
1400         userId = innerBundleUserInfos.begin()->bundleUserInfo.userId;
1401     }
1402 
1403     int32_t requestUserId = GetUserId(userId);
1404     if (requestUserId == Constants::INVALID_USERID) {
1405         return false;
1406     }
1407     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1408     InnerBundleInfo innerBundleInfo;
1409     if (!GetInnerBundleInfoWithFlags(bundleName, flags, innerBundleInfo, requestUserId)) {
1410         APP_LOGE("GetBundleInfo failed");
1411         return false;
1412     }
1413 
1414     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
1415     innerBundleInfo.GetBundleInfo(flags, bundleInfo, responseUserId);
1416     APP_LOGD("get bundleInfo(%{public}s) successfully in user(%{public}d)", bundleName.c_str(), userId);
1417     return true;
1418 }
1419 
GetBundleInfoV9(const std::string & bundleName,int32_t flags,BundleInfo & bundleInfo,int32_t userId) const1420 ErrCode BundleDataMgr::GetBundleInfoV9(
1421     const std::string &bundleName, int32_t flags, BundleInfo &bundleInfo, int32_t userId) const
1422 {
1423     std::vector<InnerBundleUserInfo> innerBundleUserInfos;
1424     if (userId == Constants::ANY_USERID) {
1425         if (!GetInnerBundleUserInfos(bundleName, innerBundleUserInfos)) {
1426             APP_LOGE("no userInfos for this bundle(%{public}s)", bundleName.c_str());
1427             return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1428         }
1429         userId = innerBundleUserInfos.begin()->bundleUserInfo.userId;
1430     }
1431 
1432     int32_t requestUserId = GetUserId(userId);
1433     if (requestUserId == Constants::INVALID_USERID) {
1434         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1435     }
1436     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1437     InnerBundleInfo innerBundleInfo;
1438 
1439     auto ret = GetInnerBundleInfoWithBundleFlagsV9(bundleName, flags, innerBundleInfo, requestUserId);
1440     if (ret != ERR_OK) {
1441         APP_LOGE("GetBundleInfoV9 failed, error code: %{public}d", ret);
1442         return ret;
1443     }
1444 
1445     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
1446     innerBundleInfo.GetBundleInfoV9(flags, bundleInfo, responseUserId);
1447     APP_LOGD("get bundleInfo(%{public}s) successfully in user(%{public}d)", bundleName.c_str(), userId);
1448     return ERR_OK;
1449 }
1450 
GetBundlePackInfo(const std::string & bundleName,int32_t flags,BundlePackInfo & bundlePackInfo,int32_t userId) const1451 ErrCode BundleDataMgr::GetBundlePackInfo(
1452     const std::string &bundleName, int32_t flags, BundlePackInfo &bundlePackInfo, int32_t userId) const
1453 {
1454     APP_LOGD("Service BundleDataMgr GetBundlePackInfo start");
1455     int32_t requestUserId;
1456     if (userId == Constants::UNSPECIFIED_USERID) {
1457         requestUserId = GetUserIdByCallingUid();
1458     } else {
1459         requestUserId = userId;
1460     }
1461 
1462     if (requestUserId == Constants::INVALID_USERID) {
1463         APP_LOGE("getBundlePackInfo userId is invalid");
1464         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1465     }
1466     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1467     InnerBundleInfo innerBundleInfo;
1468     if (!GetInnerBundleInfoWithFlags(bundleName, flags, innerBundleInfo, requestUserId)) {
1469         APP_LOGE("GetBundlePackInfo failed");
1470         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
1471     }
1472     BundlePackInfo innerBundlePackInfo = innerBundleInfo.GetBundlePackInfo();
1473     if (static_cast<uint32_t>(flags) & GET_PACKAGES) {
1474         bundlePackInfo.packages = innerBundlePackInfo.packages;
1475         return ERR_OK;
1476     }
1477     if (static_cast<uint32_t>(flags) & GET_BUNDLE_SUMMARY) {
1478         bundlePackInfo.summary.app = innerBundlePackInfo.summary.app;
1479         bundlePackInfo.summary.modules = innerBundlePackInfo.summary.modules;
1480         return ERR_OK;
1481     }
1482     if (static_cast<uint32_t>(flags) & GET_MODULE_SUMMARY) {
1483         bundlePackInfo.summary.modules = innerBundlePackInfo.summary.modules;
1484         return ERR_OK;
1485     }
1486     bundlePackInfo = innerBundlePackInfo;
1487     return ERR_OK;
1488 }
1489 
GetBundleInfosByMetaData(const std::string & metaData,std::vector<BundleInfo> & bundleInfos) const1490 bool BundleDataMgr::GetBundleInfosByMetaData(
1491     const std::string &metaData, std::vector<BundleInfo> &bundleInfos) const
1492 {
1493     if (metaData.empty()) {
1494         APP_LOGE("bundle name is empty");
1495         return false;
1496     }
1497 
1498     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1499     if (bundleInfos_.empty()) {
1500         APP_LOGE("bundleInfos_ data is empty");
1501         return false;
1502     }
1503 
1504     bool find = false;
1505     int32_t requestUserId = GetUserId();
1506     for (const auto &item : bundleInfos_) {
1507         const InnerBundleInfo &info = item.second;
1508         if (info.IsDisabled()) {
1509             APP_LOGW("app %{public}s is disabled", info.GetBundleName().c_str());
1510             continue;
1511         }
1512         if (info.CheckSpecialMetaData(metaData)) {
1513             BundleInfo bundleInfo;
1514             int32_t responseUserId = info.GetResponseUserId(requestUserId);
1515             info.GetBundleInfo(
1516                 BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, responseUserId);
1517             bundleInfos.emplace_back(bundleInfo);
1518             find = true;
1519         }
1520     }
1521     return find;
1522 }
1523 
GetBundleList(std::vector<std::string> & bundleNames,int32_t userId) const1524 bool BundleDataMgr::GetBundleList(
1525     std::vector<std::string> &bundleNames, int32_t userId) const
1526 {
1527     int32_t requestUserId = GetUserId(userId);
1528     if (requestUserId == Constants::INVALID_USERID) {
1529         return false;
1530     }
1531 
1532     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1533     if (bundleInfos_.empty()) {
1534         APP_LOGE("bundleInfos_ data is empty");
1535         return false;
1536     }
1537 
1538     bool find = false;
1539     for (const auto &infoItem : bundleInfos_) {
1540         InnerBundleInfo innerBundleInfo;
1541         if (!GetInnerBundleInfoWithFlags(infoItem.first, BundleFlag::GET_BUNDLE_DEFAULT,
1542             innerBundleInfo, requestUserId)) {
1543             continue;
1544         }
1545 
1546         bundleNames.emplace_back(infoItem.first);
1547         find = true;
1548     }
1549     APP_LOGD("user(%{public}d) get installed bundles list result(%{public}d)", userId, find);
1550     return find;
1551 }
1552 
GetBundleInfos(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId) const1553 bool BundleDataMgr::GetBundleInfos(
1554     int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId) const
1555 {
1556     if (userId == Constants::ALL_USERID) {
1557         return GetAllBundleInfos(flags, bundleInfos);
1558     }
1559 
1560     int32_t requestUserId = GetUserId(userId);
1561     if (requestUserId == Constants::INVALID_USERID) {
1562         return false;
1563     }
1564 
1565     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1566     if (bundleInfos_.empty()) {
1567         APP_LOGE("bundleInfos_ data is empty");
1568         return false;
1569     }
1570 
1571     bool find = false;
1572     for (const auto &item : bundleInfos_) {
1573         InnerBundleInfo innerBundleInfo;
1574         if (!GetInnerBundleInfoWithFlags(
1575             item.first, flags, innerBundleInfo, requestUserId)) {
1576             continue;
1577         }
1578 
1579         BundleInfo bundleInfo;
1580         int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
1581         if (!innerBundleInfo.GetBundleInfo(flags, bundleInfo, responseUserId)) {
1582             continue;
1583         }
1584         bundleInfos.emplace_back(bundleInfo);
1585         find = true;
1586     }
1587     APP_LOGD("get bundleInfos result(%{public}d) in user(%{public}d).", find, userId);
1588     return find;
1589 }
1590 
GetAllBundleInfos(int32_t flags,std::vector<BundleInfo> & bundleInfos) const1591 bool BundleDataMgr::GetAllBundleInfos(int32_t flags, std::vector<BundleInfo> &bundleInfos) const
1592 {
1593     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1594     if (bundleInfos_.empty()) {
1595         APP_LOGE("bundleInfos_ data is empty");
1596         return false;
1597     }
1598 
1599     bool find = false;
1600     for (const auto &item : bundleInfos_) {
1601         const InnerBundleInfo &info = item.second;
1602         if (info.IsDisabled()) {
1603             APP_LOGD("app %{public}s is disabled", info.GetBundleName().c_str());
1604             continue;
1605         }
1606         BundleInfo bundleInfo;
1607         info.GetBundleInfo(flags, bundleInfo, Constants::ALL_USERID);
1608         bundleInfos.emplace_back(bundleInfo);
1609         find = true;
1610     }
1611 
1612     APP_LOGD("get all bundleInfos result(%{public}d).", find);
1613     return find;
1614 }
1615 
GetBundleInfosV9(int32_t flags,std::vector<BundleInfo> & bundleInfos,int32_t userId) const1616 ErrCode BundleDataMgr::GetBundleInfosV9(int32_t flags, std::vector<BundleInfo> &bundleInfos, int32_t userId) const
1617 {
1618     if (userId == Constants::ALL_USERID) {
1619         return GetAllBundleInfosV9(flags, bundleInfos);
1620     }
1621 
1622     int32_t requestUserId = GetUserId(userId);
1623     if (requestUserId == Constants::INVALID_USERID) {
1624         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1625     }
1626 
1627     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1628     if (bundleInfos_.empty()) {
1629         APP_LOGE("bundleInfos_ data is empty");
1630         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1631     }
1632 
1633     for (const auto &item : bundleInfos_) {
1634         const InnerBundleInfo &innerBundleInfo = item.second;
1635         int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
1636         auto flag = GET_BASIC_APPLICATION_INFO;
1637         if ((static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE))
1638             == static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE)) {
1639             flag = GET_APPLICATION_INFO_WITH_DISABLE;
1640         }
1641         if (CheckInnerBundleInfoWithFlags(innerBundleInfo, flag, responseUserId) != ERR_OK) {
1642             continue;
1643         }
1644 
1645         BundleInfo bundleInfo;
1646         if (innerBundleInfo.GetBundleInfoV9(flags, bundleInfo, responseUserId) != ERR_OK) {
1647             continue;
1648         }
1649 
1650         bundleInfos.emplace_back(bundleInfo);
1651     }
1652     return ERR_OK;
1653 }
1654 
CheckInnerBundleInfoWithFlags(const InnerBundleInfo & innerBundleInfo,int32_t flags,int32_t userId) const1655 ErrCode BundleDataMgr::CheckInnerBundleInfoWithFlags(
1656     const InnerBundleInfo &innerBundleInfo, int32_t flags, int32_t userId) const
1657 {
1658     if (userId == Constants::INVALID_USERID) {
1659         APP_LOGE("bundleName: %{public}s status is disabled", innerBundleInfo.GetBundleName().c_str());
1660         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
1661     }
1662 
1663     if (innerBundleInfo.IsDisabled()) {
1664         APP_LOGE("bundleName: %{public}s status is disabled", innerBundleInfo.GetBundleName().c_str());
1665         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
1666     }
1667 
1668     if (!(static_cast<uint32_t>(flags) & GET_APPLICATION_INFO_WITH_DISABLE)
1669         && !innerBundleInfo.GetApplicationEnabled(userId)) {
1670         APP_LOGE("bundleName: %{public}s is disabled", innerBundleInfo.GetBundleName().c_str());
1671         return ERR_BUNDLE_MANAGER_APPLICATION_DISABLED;
1672     }
1673 
1674     return ERR_OK;
1675 }
1676 
GetAllBundleInfosV9(int32_t flags,std::vector<BundleInfo> & bundleInfos) const1677 ErrCode BundleDataMgr::GetAllBundleInfosV9(int32_t flags, std::vector<BundleInfo> &bundleInfos) const
1678 {
1679     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1680     if (bundleInfos_.empty()) {
1681         APP_LOGE("bundleInfos_ data is empty");
1682         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1683     }
1684 
1685     for (const auto &item : bundleInfos_) {
1686         const InnerBundleInfo &info = item.second;
1687         if (info.IsDisabled()) {
1688             APP_LOGD("app %{public}s is disabled", info.GetBundleName().c_str());
1689             continue;
1690         }
1691         BundleInfo bundleInfo;
1692         info.GetBundleInfoV9(flags, bundleInfo, Constants::ALL_USERID);
1693         bundleInfos.emplace_back(bundleInfo);
1694     }
1695     return ERR_OK;
1696 }
1697 
GetBundleNameForUid(const int uid,std::string & bundleName) const1698 bool BundleDataMgr::GetBundleNameForUid(const int uid, std::string &bundleName) const
1699 {
1700     InnerBundleInfo innerBundleInfo;
1701     if (GetInnerBundleInfoByUid(uid, innerBundleInfo) != ERR_OK) {
1702         APP_LOGW("get innerBundleInfo from bundleInfo_ by uid failed.");
1703         if (sandboxAppHelper_ == nullptr) {
1704             APP_LOGE("sandboxAppHelper_ is nullptr");
1705             return false;
1706         }
1707         if (sandboxAppHelper_->GetInnerBundleInfoByUid(uid, innerBundleInfo) != ERR_OK) {
1708             APP_LOGE("get innerBundleInfo by uid failed.");
1709             return false;
1710         }
1711     }
1712 
1713     bundleName = innerBundleInfo.GetBundleName();
1714     return true;
1715 }
1716 
GetInnerBundleInfoByUid(const int uid,InnerBundleInfo & innerBundleInfo) const1717 ErrCode BundleDataMgr::GetInnerBundleInfoByUid(const int uid, InnerBundleInfo &innerBundleInfo) const
1718 {
1719     int32_t userId = GetUserIdByUid(uid);
1720     if (userId == Constants::UNSPECIFIED_USERID || userId == Constants::INVALID_USERID) {
1721         APP_LOGE("the uid %{public}d is illegal when get bundleName by uid.", uid);
1722         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1723     }
1724 
1725     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1726     if (bundleInfos_.empty()) {
1727         APP_LOGE("bundleInfos_ data is empty");
1728         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1729     }
1730 
1731     for (const auto &item : bundleInfos_) {
1732         const InnerBundleInfo &info = item.second;
1733         if (info.IsDisabled()) {
1734             APP_LOGW("app %{public}s is disabled", info.GetBundleName().c_str());
1735             continue;
1736         }
1737         if (info.GetUid(userId) == uid) {
1738             innerBundleInfo = info;
1739             return ERR_OK;
1740         }
1741     }
1742 
1743     APP_LOGD("the uid(%{public}d) is not exists.", uid);
1744     return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
1745 }
1746 
GetBundleStats(const std::string & bundleName,const int32_t userId,std::vector<int64_t> & bundleStats) const1747 bool BundleDataMgr::GetBundleStats(
1748     const std::string &bundleName, const int32_t userId, std::vector<int64_t> &bundleStats) const
1749 {
1750     auto infoItem = bundleInfos_.find(bundleName);
1751     if (infoItem == bundleInfos_.end()) {
1752         return false;
1753     }
1754     int32_t responseUserId = infoItem->second.GetResponseUserId(userId);
1755     if (InstalldClient::GetInstance()->GetBundleStats(bundleName, responseUserId, bundleStats) != ERR_OK) {
1756         APP_LOGE("bundle%{public}s GetBundleStats failed ", bundleName.c_str());
1757         return false;
1758     }
1759     if (infoItem->second.IsPreInstallApp() && !bundleStats.empty()) {
1760         for (const auto &innerModuleInfo : infoItem->second.GetInnerModuleInfos()) {
1761             bundleStats[0] += BundleUtil::GetFileSize(innerModuleInfo.second.hapPath);
1762         }
1763     }
1764     return true;
1765 }
1766 
1767 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
GetBundleSpaceSize(const std::string & bundleName) const1768 int64_t BundleDataMgr::GetBundleSpaceSize(const std::string &bundleName) const
1769 {
1770     int32_t userId = AccountHelper::GetCurrentActiveUserId();
1771     int64_t spaceSize = 0;
1772     if (userId == Constants::INVALID_USERID) {
1773         APP_LOGE("userId is invalid");
1774         return spaceSize;
1775     }
1776     std::vector<int64_t> bundleStats;
1777     if (InstalldClient::GetInstance()->GetBundleStats(bundleName, userId, bundleStats) != ERR_OK) {
1778         APP_LOGE("GetBundleStats: bundleName: %{public}s failed", bundleName.c_str());
1779         return spaceSize;
1780     }
1781     spaceSize = std::accumulate(bundleStats.begin(), bundleStats.end(), spaceSize);
1782     APP_LOGI("%{public}s spaceSize:%{public}" PRId64, bundleName.c_str(), spaceSize);
1783     return spaceSize;
1784 }
1785 
GetAllFreeInstallBundleSpaceSize() const1786 int64_t BundleDataMgr::GetAllFreeInstallBundleSpaceSize() const
1787 {
1788     int32_t userId = AccountHelper::GetCurrentActiveUserId();
1789     int64_t allSize = 0;
1790     std::vector<BundleInfo> bundleInfos;
1791 
1792     if (userId != Constants::INVALID_USERID && GetBundleInfos(GET_ALL_APPLICATION_INFO, bundleInfos, userId) == true) {
1793         for (const auto &item : bundleInfos) {
1794             APP_LOGD("%{public}s freeInstall:%{public}d", item.name.c_str(), item.applicationInfo.isFreeInstallApp);
1795             if (item.applicationInfo.isFreeInstallApp) {
1796                 allSize += GetBundleSpaceSize(item.name);
1797             }
1798         }
1799     }
1800 
1801     APP_LOGI("All freeInstall app size:%{public}" PRId64, allSize);
1802     return allSize;
1803 }
1804 #endif
1805 
GetBundlesForUid(const int uid,std::vector<std::string> & bundleNames) const1806 bool BundleDataMgr::GetBundlesForUid(const int uid, std::vector<std::string> &bundleNames) const
1807 {
1808     InnerBundleInfo innerBundleInfo;
1809     if (GetInnerBundleInfoByUid(uid, innerBundleInfo) != ERR_OK) {
1810         APP_LOGE("get innerBundleInfo by uid failed.");
1811         return false;
1812     }
1813 
1814     bundleNames.emplace_back(innerBundleInfo.GetBundleName());
1815     return true;
1816 }
1817 
GetNameForUid(const int uid,std::string & name) const1818 ErrCode BundleDataMgr::GetNameForUid(const int uid, std::string &name) const
1819 {
1820     InnerBundleInfo innerBundleInfo;
1821     ErrCode ret = GetInnerBundleInfoByUid(uid, innerBundleInfo);
1822     if (ret != ERR_OK) {
1823         APP_LOGW("get innerBundleInfo from bundleInfo_ by uid failed.");
1824         if (sandboxAppHelper_ == nullptr) {
1825             APP_LOGE("sandboxAppHelper_ is nullptr");
1826             return ERR_BUNDLE_MANAGER_INVALID_UID;
1827         }
1828         if (sandboxAppHelper_->GetInnerBundleInfoByUid(uid, innerBundleInfo) != ERR_OK) {
1829             APP_LOGE("get innerBundleInfo by uid failed.");
1830             return ERR_BUNDLE_MANAGER_INVALID_UID;
1831         }
1832     }
1833 
1834     name = innerBundleInfo.GetBundleName();
1835     return ERR_OK;
1836 }
1837 
GetBundleGids(const std::string & bundleName,std::vector<int> & gids) const1838 bool BundleDataMgr::GetBundleGids(const std::string &bundleName, std::vector<int> &gids) const
1839 {
1840     int32_t requestUserId = GetUserId();
1841     InnerBundleUserInfo innerBundleUserInfo;
1842     if (!GetInnerBundleUserInfoByUserId(bundleName, requestUserId, innerBundleUserInfo)) {
1843         APP_LOGE("the user(%{public}d) is not exists in bundleName(%{public}s) .",
1844             requestUserId, bundleName.c_str());
1845         return false;
1846     }
1847 
1848     gids = innerBundleUserInfo.gids;
1849     return true;
1850 }
1851 
GetBundleGidsByUid(const std::string & bundleName,const int & uid,std::vector<int> & gids) const1852 bool BundleDataMgr::GetBundleGidsByUid(
1853     const std::string &bundleName, const int &uid, std::vector<int> &gids) const
1854 {
1855     return true;
1856 }
1857 
QueryKeepAliveBundleInfos(std::vector<BundleInfo> & bundleInfos) const1858 bool BundleDataMgr::QueryKeepAliveBundleInfos(std::vector<BundleInfo> &bundleInfos) const
1859 {
1860     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1861     if (bundleInfos_.empty()) {
1862         APP_LOGE("bundleInfos_ data is empty");
1863         return false;
1864     }
1865 
1866     int32_t requestUserId = GetUserId();
1867     for (const auto &info : bundleInfos_) {
1868         if (info.second.IsDisabled()) {
1869             APP_LOGW("app %{public}s is disabled", info.second.GetBundleName().c_str());
1870             continue;
1871         }
1872         if (info.second.GetIsKeepAlive()) {
1873             BundleInfo bundleInfo;
1874             int32_t responseUserId = info.second.GetResponseUserId(requestUserId);
1875             info.second.GetBundleInfo(BundleFlag::GET_BUNDLE_WITH_ABILITIES, bundleInfo, responseUserId);
1876             if (bundleInfo.name == "") {
1877                 continue;
1878             }
1879             bundleInfos.emplace_back(bundleInfo);
1880         }
1881     }
1882     return !(bundleInfos.empty());
1883 }
1884 
GetAbilityLabel(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,std::string & label) const1885 ErrCode BundleDataMgr::GetAbilityLabel(const std::string &bundleName, const std::string &moduleName,
1886     const std::string &abilityName, std::string &label) const
1887 {
1888 #ifdef GLOBAL_RESMGR_ENABLE
1889     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1890     InnerBundleInfo innerBundleInfo;
1891     ErrCode ret =
1892         GetInnerBundleInfoWithFlagsV9(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, innerBundleInfo, GetUserId());
1893     if (ret != ERR_OK) {
1894         return ret;
1895     }
1896     AbilityInfo abilityInfo;
1897     if (moduleName.empty()) {
1898         auto ability = innerBundleInfo.FindAbilityInfoV9(bundleName, moduleName, abilityName);
1899         if (!ability) {
1900             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
1901         }
1902         abilityInfo = *ability;
1903     } else {
1904         ret = innerBundleInfo.FindAbilityInfo(bundleName, moduleName, abilityName, abilityInfo);
1905         if (ret != ERR_OK) {
1906             APP_LOGE("%{public}s:FindAbilityInfo failed: %{public}d", bundleName.c_str(), ret);
1907             return ret;
1908         }
1909     }
1910     bool isEnable = false;
1911     ret = innerBundleInfo.IsAbilityEnabledV9(abilityInfo, GetUserId(), isEnable);
1912     if (ret != ERR_OK) {
1913         return ret;
1914     }
1915     if (!isEnable) {
1916         APP_LOGE("%{public}s ability disabled: %{public}s", bundleName.c_str(), abilityName.c_str());
1917         return ERR_BUNDLE_MANAGER_ABILITY_DISABLED;
1918     }
1919     if (abilityInfo.labelId == 0) {
1920         label = abilityInfo.label;
1921         return ERR_OK;
1922     }
1923     std::shared_ptr<OHOS::Global::Resource::ResourceManager> resourceManager =
1924         GetResourceManager(bundleName, abilityInfo.moduleName, GetUserId());
1925     if (resourceManager == nullptr) {
1926         APP_LOGE("InitResourceManager failed");
1927         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1928     }
1929     auto state = resourceManager->GetStringById(static_cast<uint32_t>(abilityInfo.labelId), label);
1930     if (state != OHOS::Global::Resource::RState::SUCCESS) {
1931         APP_LOGE("ResourceManager GetStringById failed");
1932         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1933     }
1934     return ERR_OK;
1935 #else
1936     APP_LOGW("GLOBAL_RES_MGR_ENABLE is false");
1937     return ERR_BUNDLE_MANAGER_GLOBAL_RES_MGR_ENABLE_DISABLED;
1938 #endif
1939 }
1940 
GetHapModuleInfo(const AbilityInfo & abilityInfo,HapModuleInfo & hapModuleInfo,int32_t userId) const1941 bool BundleDataMgr::GetHapModuleInfo(
1942     const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo, int32_t userId) const
1943 {
1944     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1945     int32_t requestUserId = GetUserId(userId);
1946     if (requestUserId == Constants::INVALID_USERID) {
1947         return false;
1948     }
1949 
1950     if (bundleInfos_.empty()) {
1951         APP_LOGE("bundleInfos_ data is empty");
1952         return false;
1953     }
1954 
1955     APP_LOGD("GetHapModuleInfo %{public}s", abilityInfo.bundleName.c_str());
1956     auto infoItem = bundleInfos_.find(abilityInfo.bundleName);
1957     if (infoItem == bundleInfos_.end()) {
1958         return false;
1959     }
1960 
1961     const InnerBundleInfo &innerBundleInfo = infoItem->second;
1962     if (innerBundleInfo.IsDisabled()) {
1963         APP_LOGE("app %{public}s is disabled", innerBundleInfo.GetBundleName().c_str());
1964         return false;
1965     }
1966 
1967     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
1968     auto module = innerBundleInfo.FindHapModuleInfo(abilityInfo.package, responseUserId);
1969     if (!module) {
1970         APP_LOGE("can not find module %{public}s", abilityInfo.package.c_str());
1971         return false;
1972     }
1973     hapModuleInfo = *module;
1974     return true;
1975 }
1976 
GetLaunchWantForBundle(const std::string & bundleName,Want & want,int32_t userId) const1977 ErrCode BundleDataMgr::GetLaunchWantForBundle(
1978     const std::string &bundleName, Want &want, int32_t userId) const
1979 {
1980     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
1981     InnerBundleInfo innerBundleInfo;
1982     ErrCode ret = GetInnerBundleInfoWithFlagsV9(
1983         bundleName, BundleFlag::GET_BUNDLE_DEFAULT, innerBundleInfo, userId);
1984     if (ret != ERR_OK) {
1985         APP_LOGE("GetInnerBundleInfoWithFlagsV9 failed");
1986         return ret;
1987     }
1988 
1989     std::string mainAbility = innerBundleInfo.GetMainAbility();
1990     if (mainAbility.empty()) {
1991         APP_LOGE("no main ability in the bundle %{public}s", bundleName.c_str());
1992         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
1993     }
1994 
1995     want.SetElementName("", bundleName, mainAbility);
1996     want.SetAction(Constants::ACTION_HOME);
1997     want.AddEntity(Constants::ENTITY_HOME);
1998     return ERR_OK;
1999 }
2000 
CheckIsSystemAppByUid(const int uid) const2001 bool BundleDataMgr::CheckIsSystemAppByUid(const int uid) const
2002 {
2003     // If the value of uid is 0 (ROOT_UID) or 1000 (BMS_UID),
2004     // the uid should be the system uid.
2005     if (uid == Constants::ROOT_UID || uid == Constants::BMS_UID) {
2006         return true;
2007     }
2008 
2009     InnerBundleInfo innerBundleInfo;
2010     if (GetInnerBundleInfoByUid(uid, innerBundleInfo) != ERR_OK) {
2011         return false;
2012     }
2013 
2014     return innerBundleInfo.IsSystemApp();
2015 }
2016 
InitStateTransferMap()2017 void BundleDataMgr::InitStateTransferMap()
2018 {
2019     transferStates_.emplace(InstallState::INSTALL_SUCCESS, InstallState::INSTALL_START);
2020     transferStates_.emplace(InstallState::INSTALL_FAIL, InstallState::INSTALL_START);
2021     transferStates_.emplace(InstallState::UNINSTALL_START, InstallState::INSTALL_SUCCESS);
2022     transferStates_.emplace(InstallState::UNINSTALL_START, InstallState::INSTALL_START);
2023     transferStates_.emplace(InstallState::UNINSTALL_START, InstallState::UPDATING_SUCCESS);
2024     transferStates_.emplace(InstallState::UNINSTALL_FAIL, InstallState::UNINSTALL_START);
2025     transferStates_.emplace(InstallState::UNINSTALL_SUCCESS, InstallState::UNINSTALL_START);
2026     transferStates_.emplace(InstallState::UPDATING_START, InstallState::INSTALL_SUCCESS);
2027     transferStates_.emplace(InstallState::UPDATING_SUCCESS, InstallState::UPDATING_START);
2028     transferStates_.emplace(InstallState::UPDATING_FAIL, InstallState::UPDATING_START);
2029     transferStates_.emplace(InstallState::UPDATING_FAIL, InstallState::INSTALL_START);
2030     transferStates_.emplace(InstallState::UPDATING_START, InstallState::INSTALL_START);
2031     transferStates_.emplace(InstallState::INSTALL_SUCCESS, InstallState::UPDATING_START);
2032     transferStates_.emplace(InstallState::INSTALL_SUCCESS, InstallState::UPDATING_SUCCESS);
2033     transferStates_.emplace(InstallState::INSTALL_SUCCESS, InstallState::UNINSTALL_START);
2034     transferStates_.emplace(InstallState::UPDATING_START, InstallState::UPDATING_SUCCESS);
2035     transferStates_.emplace(InstallState::ROLL_BACK, InstallState::UPDATING_START);
2036     transferStates_.emplace(InstallState::ROLL_BACK, InstallState::UPDATING_SUCCESS);
2037     transferStates_.emplace(InstallState::INSTALL_SUCCESS, InstallState::ROLL_BACK);
2038     transferStates_.emplace(InstallState::UNINSTALL_START, InstallState::USER_CHANGE);
2039     transferStates_.emplace(InstallState::UPDATING_START, InstallState::USER_CHANGE);
2040     transferStates_.emplace(InstallState::INSTALL_SUCCESS, InstallState::USER_CHANGE);
2041     transferStates_.emplace(InstallState::UPDATING_SUCCESS, InstallState::USER_CHANGE);
2042     transferStates_.emplace(InstallState::USER_CHANGE, InstallState::INSTALL_SUCCESS);
2043     transferStates_.emplace(InstallState::USER_CHANGE, InstallState::UPDATING_SUCCESS);
2044     transferStates_.emplace(InstallState::USER_CHANGE, InstallState::UPDATING_START);
2045 }
2046 
IsDeleteDataState(const InstallState state) const2047 bool BundleDataMgr::IsDeleteDataState(const InstallState state) const
2048 {
2049     return (state == InstallState::INSTALL_FAIL || state == InstallState::UNINSTALL_FAIL ||
2050             state == InstallState::UNINSTALL_SUCCESS || state == InstallState::UPDATING_FAIL);
2051 }
2052 
IsDisableState(const InstallState state) const2053 bool BundleDataMgr::IsDisableState(const InstallState state) const
2054 {
2055     if (state == InstallState::UPDATING_START || state == InstallState::UNINSTALL_START) {
2056         return true;
2057     }
2058     return false;
2059 }
2060 
DeleteBundleInfo(const std::string & bundleName,const InstallState state)2061 void BundleDataMgr::DeleteBundleInfo(const std::string &bundleName, const InstallState state)
2062 {
2063     if (InstallState::INSTALL_FAIL == state) {
2064         APP_LOGW("del fail, bundle:%{public}s has no installed info", bundleName.c_str());
2065         return;
2066     }
2067 
2068     auto infoItem = bundleInfos_.find(bundleName);
2069     if (infoItem != bundleInfos_.end()) {
2070         APP_LOGD("del bundle name:%{public}s", bundleName.c_str());
2071         const InnerBundleInfo &innerBundleInfo = infoItem->second;
2072         RecycleUidAndGid(innerBundleInfo);
2073         bool ret = dataStorage_->DeleteStorageBundleInfo(innerBundleInfo);
2074         if (!ret) {
2075             APP_LOGW("delete storage error name:%{public}s", bundleName.c_str());
2076         }
2077         bundleInfos_.erase(bundleName);
2078     }
2079 }
2080 
IsAppOrAbilityInstalled(const std::string & bundleName) const2081 bool BundleDataMgr::IsAppOrAbilityInstalled(const std::string &bundleName) const
2082 {
2083     if (bundleName.empty()) {
2084         APP_LOGW("name:%{public}s empty", bundleName.c_str());
2085         return false;
2086     }
2087 
2088     std::lock_guard<std::mutex> lock(stateMutex_);
2089     auto statusItem = installStates_.find(bundleName);
2090     if (statusItem == installStates_.end()) {
2091         APP_LOGW("name:%{public}s not find", bundleName.c_str());
2092         return false;
2093     }
2094 
2095     if (statusItem->second == InstallState::INSTALL_SUCCESS) {
2096         return true;
2097     }
2098 
2099     APP_LOGW("name:%{public}s not install success", bundleName.c_str());
2100     return false;
2101 }
2102 
GetInnerBundleInfoWithFlags(const std::string & bundleName,const int32_t flags,InnerBundleInfo & info,int32_t userId) const2103 bool BundleDataMgr::GetInnerBundleInfoWithFlags(const std::string &bundleName,
2104     const int32_t flags, InnerBundleInfo &info, int32_t userId) const
2105 {
2106     int32_t requestUserId = GetUserId(userId);
2107     if (requestUserId == Constants::INVALID_USERID) {
2108         return false;
2109     }
2110 
2111     if (bundleInfos_.empty()) {
2112         APP_LOGE("bundleInfos_ data is empty");
2113         return false;
2114     }
2115     APP_LOGD("GetInnerBundleInfoWithFlags: %{public}s", bundleName.c_str());
2116     auto item = bundleInfos_.find(bundleName);
2117     if (item == bundleInfos_.end()) {
2118         APP_LOGE("GetInnerBundleInfoWithFlags: bundleName not find");
2119         return false;
2120     }
2121     const InnerBundleInfo &innerBundleInfo = item->second;
2122     if (innerBundleInfo.IsDisabled()) {
2123         APP_LOGE("bundleName: %{public}s status is disabled", innerBundleInfo.GetBundleName().c_str());
2124         return false;
2125     }
2126 
2127     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
2128     if (!(static_cast<uint32_t>(flags) & GET_APPLICATION_INFO_WITH_DISABLE)
2129         && !innerBundleInfo.GetApplicationEnabled(responseUserId)) {
2130         APP_LOGE("bundleName: %{public}s is disabled", innerBundleInfo.GetBundleName().c_str());
2131         return false;
2132     }
2133     info = innerBundleInfo;
2134     return true;
2135 }
2136 
GetInnerBundleInfoWithFlagsV9(const std::string & bundleName,const int32_t flags,InnerBundleInfo & info,int32_t userId) const2137 ErrCode BundleDataMgr::GetInnerBundleInfoWithFlagsV9(const std::string &bundleName,
2138     const int32_t flags, InnerBundleInfo &info, int32_t userId) const
2139 {
2140     int32_t requestUserId = GetUserId(userId);
2141     if (requestUserId == Constants::INVALID_USERID) {
2142         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
2143     }
2144 
2145     if (bundleInfos_.empty()) {
2146         APP_LOGE("bundleInfos_ data is empty");
2147         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2148     }
2149     APP_LOGD("GetInnerBundleInfoWithFlagsV9: %{public}s", bundleName.c_str());
2150     auto item = bundleInfos_.find(bundleName);
2151     if (item == bundleInfos_.end()) {
2152         APP_LOGE("GetInnerBundleInfoWithFlagsV9: bundleName not find");
2153         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2154     }
2155     const InnerBundleInfo &innerBundleInfo = item->second;
2156     if (innerBundleInfo.IsDisabled()) {
2157         APP_LOGE("bundleName: %{public}s status is disabled", innerBundleInfo.GetBundleName().c_str());
2158         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2159     }
2160 
2161     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
2162     bool isEnabled;
2163     auto ret = innerBundleInfo.GetApplicationEnabledV9(responseUserId, isEnabled);
2164     if (ret != ERR_OK) {
2165         return ret;
2166     }
2167     if (!(static_cast<uint32_t>(flags) & static_cast<int32_t>(GetAbilityInfoFlag::GET_ABILITY_INFO_WITH_DISABLE))
2168         && !isEnabled) {
2169         APP_LOGE("bundleName: %{public}s is disabled", innerBundleInfo.GetBundleName().c_str());
2170         return ERR_BUNDLE_MANAGER_APPLICATION_DISABLED;
2171     }
2172     info = innerBundleInfo;
2173     return ERR_OK;
2174 }
2175 
GetInnerBundleInfoWithBundleFlagsV9(const std::string & bundleName,const int32_t flags,InnerBundleInfo & info,int32_t userId) const2176 ErrCode BundleDataMgr::GetInnerBundleInfoWithBundleFlagsV9(const std::string &bundleName,
2177     const int32_t flags, InnerBundleInfo &info, int32_t userId) const
2178 {
2179     int32_t requestUserId = GetUserId(userId);
2180     if (requestUserId == Constants::INVALID_USERID) {
2181         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
2182     }
2183 
2184     if (bundleInfos_.empty()) {
2185         APP_LOGE("bundleInfos_ data is empty");
2186         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2187     }
2188     APP_LOGD("GetInnerBundleInfoWithFlagsV9: %{public}s", bundleName.c_str());
2189     auto item = bundleInfos_.find(bundleName);
2190     if (item == bundleInfos_.end()) {
2191         APP_LOGE("GetInnerBundleInfoWithFlagsV9: bundleName not find");
2192         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2193     }
2194     const InnerBundleInfo &innerBundleInfo = item->second;
2195     if (innerBundleInfo.IsDisabled()) {
2196         APP_LOGE("bundleName: %{public}s status is disabled", innerBundleInfo.GetBundleName().c_str());
2197         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
2198     }
2199 
2200     int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
2201     bool isEnabled;
2202     auto ret = innerBundleInfo.GetApplicationEnabledV9(responseUserId, isEnabled);
2203     if (ret != ERR_OK) {
2204         return ret;
2205     }
2206     if (!(static_cast<uint32_t>(flags) & static_cast<int32_t>(GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_DISABLE))
2207         && !isEnabled) {
2208         APP_LOGE("bundleName: %{public}s is disabled", innerBundleInfo.GetBundleName().c_str());
2209         return ERR_BUNDLE_MANAGER_APPLICATION_DISABLED;
2210     }
2211     info = innerBundleInfo;
2212     return ERR_OK;
2213 }
2214 
GetInnerBundleInfo(const std::string & bundleName,InnerBundleInfo & info)2215 bool BundleDataMgr::GetInnerBundleInfo(const std::string &bundleName, InnerBundleInfo &info)
2216 {
2217     APP_LOGD("GetInnerBundleInfo %{public}s", bundleName.c_str());
2218     if (bundleName.empty()) {
2219         APP_LOGE("bundleName is empty");
2220         return false;
2221     }
2222 
2223     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2224     auto infoItem = bundleInfos_.find(bundleName);
2225     if (infoItem == bundleInfos_.end()) {
2226         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2227         return false;
2228     }
2229     infoItem->second.SetBundleStatus(InnerBundleInfo::BundleStatus::DISABLED);
2230     info = infoItem->second;
2231     return true;
2232 }
2233 
DisableBundle(const std::string & bundleName)2234 bool BundleDataMgr::DisableBundle(const std::string &bundleName)
2235 {
2236     APP_LOGD("DisableBundle %{public}s", bundleName.c_str());
2237     if (bundleName.empty()) {
2238         APP_LOGE("bundleName empty");
2239         return false;
2240     }
2241 
2242     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2243     auto infoItem = bundleInfos_.find(bundleName);
2244     if (infoItem == bundleInfos_.end()) {
2245         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2246         return false;
2247     }
2248     infoItem->second.SetBundleStatus(InnerBundleInfo::BundleStatus::DISABLED);
2249     return true;
2250 }
2251 
EnableBundle(const std::string & bundleName)2252 bool BundleDataMgr::EnableBundle(const std::string &bundleName)
2253 {
2254     APP_LOGD("EnableBundle %{public}s", bundleName.c_str());
2255     if (bundleName.empty()) {
2256         APP_LOGE("bundleName empty");
2257         return false;
2258     }
2259 
2260     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2261     auto infoItem = bundleInfos_.find(bundleName);
2262     if (infoItem == bundleInfos_.end()) {
2263         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2264         return false;
2265     }
2266     infoItem->second.SetBundleStatus(InnerBundleInfo::BundleStatus::ENABLED);
2267     return true;
2268 }
2269 
IsApplicationEnabled(const std::string & bundleName,bool & isEnabled) const2270 ErrCode BundleDataMgr::IsApplicationEnabled(const std::string &bundleName, bool &isEnabled) const
2271 {
2272     APP_LOGD("IsApplicationEnabled %{public}s", bundleName.c_str());
2273     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2274     auto infoItem = bundleInfos_.find(bundleName);
2275     if (infoItem == bundleInfos_.end()) {
2276         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2277         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2278     }
2279     int32_t responseUserId = infoItem->second.GetResponseUserId(GetUserId());
2280     ErrCode ret = infoItem->second.GetApplicationEnabledV9(responseUserId, isEnabled);
2281     if (ret != ERR_OK) {
2282         APP_LOGE("GetApplicationEnabled failed: %{public}s", bundleName.c_str());
2283     }
2284     return ret;
2285 }
2286 
SetApplicationEnabled(const std::string & bundleName,bool isEnable,int32_t userId)2287 ErrCode BundleDataMgr::SetApplicationEnabled(const std::string &bundleName, bool isEnable, int32_t userId)
2288 {
2289     APP_LOGD("SetApplicationEnabled %{public}s", bundleName.c_str());
2290     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2291     int32_t requestUserId = GetUserId(userId);
2292     if (requestUserId == Constants::INVALID_USERID) {
2293         APP_LOGE("Request userId is invalid");
2294         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2295     }
2296     auto infoItem = bundleInfos_.find(bundleName);
2297     if (infoItem == bundleInfos_.end()) {
2298         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2299         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2300     }
2301 
2302     InnerBundleInfo& newInfo = infoItem->second;
2303     auto ret = newInfo.SetApplicationEnabled(isEnable, requestUserId);
2304     if (ret != ERR_OK) {
2305         return ret;
2306     }
2307     InnerBundleUserInfo innerBundleUserInfo;
2308     if (!newInfo.GetInnerBundleUserInfo(requestUserId, innerBundleUserInfo)) {
2309         APP_LOGE("can not find bundleUserInfo in userId: %{public}d", requestUserId);
2310         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2311     }
2312 
2313     if (innerBundleUserInfo.bundleUserInfo.IsInitialState()) {
2314         bundleStateStorage_->DeleteBundleState(bundleName, requestUserId);
2315     } else {
2316         bundleStateStorage_->SaveBundleStateStorage(
2317             bundleName, requestUserId, innerBundleUserInfo.bundleUserInfo);
2318     }
2319     return ERR_OK;
2320 }
2321 
SetModuleRemovable(const std::string & bundleName,const std::string & moduleName,bool isEnable)2322 bool BundleDataMgr::SetModuleRemovable(const std::string &bundleName, const std::string &moduleName, bool isEnable)
2323 {
2324     if (bundleName.empty() || moduleName.empty()) {
2325         APP_LOGE("bundleName or moduleName is empty");
2326         return false;
2327     }
2328     int32_t userId = AccountHelper::GetCurrentActiveUserId();
2329     if (userId == Constants::INVALID_USERID) {
2330         APP_LOGE("get a invalid userid");
2331         return false;
2332     }
2333     APP_LOGD("bundleName:%{public}s, moduleName:%{public}s, userId:%{public}d",
2334         bundleName.c_str(), moduleName.c_str(), userId);
2335     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2336     auto infoItem = bundleInfos_.find(bundleName);
2337     if (infoItem == bundleInfos_.end()) {
2338         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2339         return false;
2340     }
2341     InnerBundleInfo newInfo = infoItem->second;
2342     bool ret = newInfo.SetModuleRemovable(moduleName, isEnable, userId);
2343     if (ret && dataStorage_->SaveStorageBundleInfo(newInfo)) {
2344         ret = infoItem->second.SetModuleRemovable(moduleName, isEnable, userId);
2345 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
2346         if (isEnable) {
2347             // call clean task
2348             APP_LOGD("bundle:%{public}s isEnable:%{public}d ret:%{public}d call clean task",
2349                 bundleName.c_str(), isEnable, ret);
2350             DelayedSingleton<BundleMgrService>::GetInstance()->GetAgingMgr()->Start(
2351                 BundleAgingMgr::AgingTriggertype::UPDATE_REMOVABLE_FLAG);
2352         }
2353 #endif
2354         return ret;
2355     } else {
2356         APP_LOGE("bundle:%{public}s SetModuleRemoved failed", bundleName.c_str());
2357         return false;
2358     }
2359 }
2360 
IsModuleRemovable(const std::string & bundleName,const std::string & moduleName,bool & isRemovable) const2361 ErrCode BundleDataMgr::IsModuleRemovable(const std::string &bundleName, const std::string &moduleName,
2362     bool &isRemovable) const
2363 {
2364     if (bundleName.empty() || moduleName.empty()) {
2365         APP_LOGE("bundleName or moduleName is empty");
2366         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
2367     }
2368     int32_t userId = AccountHelper::GetCurrentActiveUserId();
2369     if (userId == Constants::INVALID_USERID) {
2370         APP_LOGE("get a invalid userid");
2371         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
2372     }
2373     APP_LOGD("bundleName:%{public}s, moduleName:%{public}s, userId:%{public}d",
2374         bundleName.c_str(), moduleName.c_str(), userId);
2375     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2376     auto infoItem = bundleInfos_.find(bundleName);
2377     if (infoItem == bundleInfos_.end()) {
2378         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2379         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2380     }
2381     InnerBundleInfo newInfo = infoItem->second;
2382     return newInfo.IsModuleRemovable(moduleName, userId, isRemovable);
2383 }
2384 
IsAbilityEnabled(const AbilityInfo & abilityInfo,bool & isEnable) const2385 ErrCode BundleDataMgr::IsAbilityEnabled(const AbilityInfo &abilityInfo, bool &isEnable) const
2386 {
2387     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2388     auto infoItem = bundleInfos_.find(abilityInfo.bundleName);
2389     if (infoItem == bundleInfos_.end()) {
2390         APP_LOGE("can not find bundle %{public}s", abilityInfo.bundleName.c_str());
2391         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2392     }
2393     InnerBundleInfo innerBundleInfo = infoItem->second;
2394     auto ability = innerBundleInfo.FindAbilityInfoV9(
2395         abilityInfo.bundleName, abilityInfo.moduleName, abilityInfo.name);
2396     if (!ability) {
2397         APP_LOGE("ability not found");
2398         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
2399     }
2400     int32_t responseUserId = innerBundleInfo.GetResponseUserId(GetUserId());
2401     return innerBundleInfo.IsAbilityEnabledV9((*ability), responseUserId, isEnable);
2402 }
2403 
SetAbilityEnabled(const AbilityInfo & abilityInfo,bool isEnabled,int32_t userId)2404 ErrCode BundleDataMgr::SetAbilityEnabled(const AbilityInfo &abilityInfo, bool isEnabled, int32_t userId)
2405 {
2406     APP_LOGD("SetAbilityEnabled %{public}s", abilityInfo.name.c_str());
2407     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2408     int32_t requestUserId = GetUserId(userId);
2409     if (requestUserId == Constants::INVALID_USERID) {
2410         APP_LOGE("Request userId is invalid");
2411         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2412     }
2413     auto infoItem = bundleInfos_.find(abilityInfo.bundleName);
2414     if (infoItem == bundleInfos_.end()) {
2415         APP_LOGE("can not find bundle %{public}s", abilityInfo.bundleName.c_str());
2416         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2417     }
2418     InnerBundleInfo& newInfo = infoItem->second;
2419     ErrCode ret = newInfo.SetAbilityEnabled(abilityInfo.bundleName, abilityInfo.moduleName,
2420         abilityInfo.name, isEnabled, userId);
2421     if (ret != ERR_OK) {
2422         return ret;
2423     }
2424     InnerBundleUserInfo innerBundleUserInfo;
2425     if (!newInfo.GetInnerBundleUserInfo(requestUserId, innerBundleUserInfo)) {
2426         APP_LOGE("can not find bundleUserInfo in userId: %{public}d", requestUserId);
2427         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2428     }
2429 
2430     if (innerBundleUserInfo.bundleUserInfo.IsInitialState()) {
2431         bundleStateStorage_->DeleteBundleState(abilityInfo.bundleName, requestUserId);
2432     } else {
2433         bundleStateStorage_->SaveBundleStateStorage(
2434             abilityInfo.bundleName, requestUserId, innerBundleUserInfo.bundleUserInfo);
2435     }
2436     return ERR_OK;
2437 }
2438 
GetSandboxAppHelper() const2439 std::shared_ptr<BundleSandboxAppHelper> BundleDataMgr::GetSandboxAppHelper() const
2440 {
2441     return sandboxAppHelper_;
2442 }
2443 
RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)2444 bool BundleDataMgr::RegisterBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
2445 {
2446     APP_LOGD("RegisterBundleStatusCallback %{public}s", bundleStatusCallback->GetBundleName().c_str());
2447     std::unique_lock<std::shared_mutex> lock(callbackMutex_);
2448     callbackList_.emplace_back(bundleStatusCallback);
2449     if (bundleStatusCallback->AsObject() != nullptr) {
2450         sptr<BundleStatusCallbackDeathRecipient> deathRecipient =
2451             new (std::nothrow) BundleStatusCallbackDeathRecipient();
2452         if (deathRecipient == nullptr) {
2453             APP_LOGE("deathRecipient is null");
2454             return false;
2455         }
2456         bundleStatusCallback->AsObject()->AddDeathRecipient(deathRecipient);
2457     }
2458     return true;
2459 }
2460 
RegisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)2461 bool BundleDataMgr::RegisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
2462 {
2463     if (bundleEventCallback == nullptr) {
2464         APP_LOGE("bundleEventCallback is null");
2465         return false;
2466     }
2467     std::lock_guard<std::mutex> lock(eventCallbackMutex_);
2468     if (eventCallbackList_.size() >= MAX_EVENT_CALL_BACK_SIZE) {
2469         APP_LOGE("eventCallbackList_ reach max size %{public}d", MAX_EVENT_CALL_BACK_SIZE);
2470         return false;
2471     }
2472     if (bundleEventCallback->AsObject() != nullptr) {
2473         sptr<BundleEventCallbackDeathRecipient> deathRecipient =
2474             new (std::nothrow) BundleEventCallbackDeathRecipient();
2475         if (deathRecipient == nullptr) {
2476             APP_LOGE("deathRecipient is null");
2477             return false;
2478         }
2479         bundleEventCallback->AsObject()->AddDeathRecipient(deathRecipient);
2480     }
2481     eventCallbackList_.emplace_back(bundleEventCallback);
2482     return true;
2483 }
2484 
UnregisterBundleEventCallback(const sptr<IBundleEventCallback> & bundleEventCallback)2485 bool BundleDataMgr::UnregisterBundleEventCallback(const sptr<IBundleEventCallback> &bundleEventCallback)
2486 {
2487     APP_LOGD("begin to UnregisterBundleEventCallback");
2488     if (bundleEventCallback == nullptr) {
2489         APP_LOGE("bundleEventCallback is null");
2490         return false;
2491     }
2492     std::lock_guard<std::mutex> lock(eventCallbackMutex_);
2493     eventCallbackList_.erase(std::remove_if(eventCallbackList_.begin(), eventCallbackList_.end(),
2494         [&bundleEventCallback](const sptr<IBundleEventCallback> &callback) {
2495             return callback->AsObject() == bundleEventCallback->AsObject();
2496         }), eventCallbackList_.end());
2497     return true;
2498 }
2499 
NotifyBundleEventCallback(const EventFwk::CommonEventData & eventData) const2500 void BundleDataMgr::NotifyBundleEventCallback(const EventFwk::CommonEventData &eventData) const
2501 {
2502     APP_LOGD("begin to NotifyBundleEventCallback");
2503     std::lock_guard<std::mutex> lock(eventCallbackMutex_);
2504     for (const auto &callback : eventCallbackList_) {
2505         callback->OnReceiveEvent(eventData);
2506     }
2507     APP_LOGD("finish to NotifyBundleEventCallback");
2508 }
2509 
ClearBundleStatusCallback(const sptr<IBundleStatusCallback> & bundleStatusCallback)2510 bool BundleDataMgr::ClearBundleStatusCallback(const sptr<IBundleStatusCallback> &bundleStatusCallback)
2511 {
2512     APP_LOGD("ClearBundleStatusCallback %{public}s", bundleStatusCallback->GetBundleName().c_str());
2513     std::unique_lock<std::shared_mutex> lock(callbackMutex_);
2514     callbackList_.erase(std::remove_if(callbackList_.begin(),
2515         callbackList_.end(),
2516         [&](const sptr<IBundleStatusCallback> &callback) {
2517             return callback->AsObject() == bundleStatusCallback->AsObject();
2518         }),
2519         callbackList_.end());
2520     return true;
2521 }
2522 
UnregisterBundleStatusCallback()2523 bool BundleDataMgr::UnregisterBundleStatusCallback()
2524 {
2525     std::unique_lock<std::shared_mutex> lock(callbackMutex_);
2526     callbackList_.clear();
2527     return true;
2528 }
2529 
GenerateUidAndGid(InnerBundleUserInfo & innerBundleUserInfo)2530 bool BundleDataMgr::GenerateUidAndGid(InnerBundleUserInfo &innerBundleUserInfo)
2531 {
2532     if (innerBundleUserInfo.bundleName.empty()) {
2533         APP_LOGE("bundleName is null.");
2534         return false;
2535     }
2536 
2537     int32_t bundleId = Constants::INVALID_BUNDLEID;
2538     if (!GenerateBundleId(innerBundleUserInfo.bundleName, bundleId)) {
2539         APP_LOGE("Generate bundleId failed.");
2540         return false;
2541     }
2542 
2543     innerBundleUserInfo.uid = innerBundleUserInfo.bundleUserInfo.userId * Constants::BASE_USER_RANGE
2544         + bundleId % Constants::BASE_USER_RANGE;
2545     innerBundleUserInfo.gids.emplace_back(innerBundleUserInfo.uid);
2546     return true;
2547 }
2548 
GenerateBundleId(const std::string & bundleName,int32_t & bundleId)2549 bool BundleDataMgr::GenerateBundleId(const std::string &bundleName, int32_t &bundleId)
2550 {
2551     std::lock_guard<std::mutex> lock(bundleIdMapMutex_);
2552     if (bundleIdMap_.empty()) {
2553         APP_LOGI("first app install");
2554         bundleId = baseAppUid_;
2555         bundleIdMap_.emplace(bundleId, bundleName);
2556         return true;
2557     }
2558 
2559     for (const auto &innerBundleId : bundleIdMap_) {
2560         if (innerBundleId.second == bundleName) {
2561             bundleId = innerBundleId.first;
2562             return true;
2563         }
2564     }
2565 
2566     for (int32_t i = baseAppUid_; i < bundleIdMap_.rbegin()->first; ++i) {
2567         if (bundleIdMap_.find(i) == bundleIdMap_.end()) {
2568             APP_LOGI("the %{public}d app install", i);
2569             bundleId = i;
2570             bundleIdMap_.emplace(bundleId, bundleName);
2571             BundleUtil::MakeFsConfig(bundleName, bundleId, Constants::HMDFS_CONFIG_PATH);
2572             BundleUtil::MakeFsConfig(bundleName, bundleId, Constants::SHAREFS_CONFIG_PATH);
2573             return true;
2574         }
2575     }
2576 
2577     if (bundleIdMap_.rbegin()->first == Constants::MAX_APP_UID) {
2578         APP_LOGE("the bundleId exceeding the maximum value.");
2579         return false;
2580     }
2581 
2582     bundleId = bundleIdMap_.rbegin()->first + 1;
2583     bundleIdMap_.emplace(bundleId, bundleName);
2584     BundleUtil::MakeFsConfig(bundleName, bundleId, Constants::HMDFS_CONFIG_PATH);
2585     BundleUtil::MakeFsConfig(bundleName, bundleId, Constants::SHAREFS_CONFIG_PATH);
2586     return true;
2587 }
2588 
SetModuleUpgradeFlag(const std::string & bundleName,const std::string & moduleName,const int32_t upgradeFlag)2589 ErrCode BundleDataMgr::SetModuleUpgradeFlag(const std::string &bundleName,
2590     const std::string &moduleName, const int32_t upgradeFlag)
2591 {
2592     APP_LOGD("SetModuleUpgradeFlag %{public}d", upgradeFlag);
2593     if (bundleName.empty() || moduleName.empty()) {
2594         APP_LOGE("bundleName or moduleName is empty");
2595         return ERR_BUNDLE_MANAGER_PARAM_ERROR;
2596     }
2597     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2598     auto infoItem = bundleInfos_.find(bundleName);
2599     if (infoItem == bundleInfos_.end()) {
2600         return ERR_BUNDLE_MANAGER_BUNDLE_NOT_EXIST;
2601     }
2602     InnerBundleInfo &newInfo = infoItem->second;
2603     ErrCode setFlag = newInfo.SetModuleUpgradeFlag(moduleName, upgradeFlag);
2604     if (setFlag == ERR_OK) {
2605         if (dataStorage_->SaveStorageBundleInfo(newInfo)) {
2606             return ERR_OK;
2607         }
2608         return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR;
2609     }
2610     APP_LOGE("dataStorage SetModuleUpgradeFlag %{public}s failed", bundleName.c_str());
2611     return setFlag;
2612 }
2613 
GetModuleUpgradeFlag(const std::string & bundleName,const std::string & moduleName) const2614 int32_t BundleDataMgr::GetModuleUpgradeFlag(const std::string &bundleName, const std::string &moduleName) const
2615 {
2616     APP_LOGD("bundleName is bundleName:%{public}s, moduleName:%{public}s", bundleName.c_str(), moduleName.c_str());
2617     if (bundleName.empty() || moduleName.empty()) {
2618         APP_LOGE("bundleName or moduleName is empty");
2619         return false;
2620     }
2621     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2622     auto infoItem = bundleInfos_.find(bundleName);
2623     if (infoItem == bundleInfos_.end()) {
2624         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2625         return false;
2626     }
2627     InnerBundleInfo newInfo = infoItem->second;
2628     return newInfo.GetModuleUpgradeFlag(moduleName);
2629 }
2630 
StoreSandboxPersistentInfo(const std::string & bundleName,const SandboxAppPersistentInfo & info)2631 void BundleDataMgr::StoreSandboxPersistentInfo(const std::string &bundleName, const SandboxAppPersistentInfo &info)
2632 {
2633     if (bundleName.empty()) {
2634         APP_LOGW("StoreSandboxPersistentInfo bundleName is empty");
2635         return;
2636     }
2637     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2638     if (bundleInfos_.find(bundleName) == bundleInfos_.end()) {
2639         APP_LOGW("can not find bundle %{public}s", bundleName.c_str());
2640         return;
2641     }
2642     bundleInfos_[bundleName].AddSandboxPersistentInfo(info);
2643     SaveInnerBundleInfo(bundleInfos_[bundleName]);
2644 }
2645 
DeleteSandboxPersistentInfo(const std::string & bundleName,const SandboxAppPersistentInfo & info)2646 void BundleDataMgr::DeleteSandboxPersistentInfo(const std::string &bundleName, const SandboxAppPersistentInfo &info)
2647 {
2648     if (bundleName.empty()) {
2649         APP_LOGW("DeleteSandboxPersistentInfo bundleName is empty");
2650         return;
2651     }
2652     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2653     if (bundleInfos_.find(bundleName) == bundleInfos_.end()) {
2654         APP_LOGW("can not find bundle %{public}s", bundleName.c_str());
2655         return;
2656     }
2657     bundleInfos_[bundleName].RemoveSandboxPersistentInfo(info);
2658     SaveInnerBundleInfo(bundleInfos_[bundleName]);
2659 }
2660 
RecycleUidAndGid(const InnerBundleInfo & info)2661 void BundleDataMgr::RecycleUidAndGid(const InnerBundleInfo &info)
2662 {
2663     auto userInfos = info.GetInnerBundleUserInfos();
2664     if (userInfos.empty()) {
2665         return;
2666     }
2667 
2668     auto innerBundleUserInfo = userInfos.begin()->second;
2669     int32_t bundleId = innerBundleUserInfo.uid -
2670         innerBundleUserInfo.bundleUserInfo.userId * Constants::BASE_USER_RANGE;
2671     std::lock_guard<std::mutex> lock(bundleIdMapMutex_);
2672     auto infoItem = bundleIdMap_.find(bundleId);
2673     if (infoItem == bundleIdMap_.end()) {
2674         return;
2675     }
2676 
2677     bundleIdMap_.erase(bundleId);
2678     BundleUtil::RemoveFsConfig(innerBundleUserInfo.bundleName, Constants::HMDFS_CONFIG_PATH);
2679     BundleUtil::RemoveFsConfig(innerBundleUserInfo.bundleName, Constants::SHAREFS_CONFIG_PATH);
2680 }
2681 
RestoreUidAndGid()2682 bool BundleDataMgr::RestoreUidAndGid()
2683 {
2684     for (const auto &info : bundleInfos_) {
2685         bool onlyInsertOne = false;
2686         for (auto infoItem : info.second.GetInnerBundleUserInfos()) {
2687             auto innerBundleUserInfo = infoItem.second;
2688             AddUserId(innerBundleUserInfo.bundleUserInfo.userId);
2689             if (!onlyInsertOne) {
2690                 onlyInsertOne = true;
2691                 int32_t bundleId = innerBundleUserInfo.uid -
2692                     innerBundleUserInfo.bundleUserInfo.userId * Constants::BASE_USER_RANGE;
2693                 std::lock_guard<std::mutex> lock(bundleIdMapMutex_);
2694                 auto item = bundleIdMap_.find(bundleId);
2695                 if (item == bundleIdMap_.end()) {
2696                     bundleIdMap_.emplace(bundleId, innerBundleUserInfo.bundleName);
2697                 } else {
2698                     bundleIdMap_[bundleId] = innerBundleUserInfo.bundleName;
2699                 }
2700                 BundleUtil::MakeFsConfig(innerBundleUserInfo.bundleName, bundleId, Constants::HMDFS_CONFIG_PATH);
2701                 BundleUtil::MakeFsConfig(innerBundleUserInfo.bundleName, bundleId, Constants::SHAREFS_CONFIG_PATH);
2702             }
2703         }
2704     }
2705     return true;
2706 }
2707 
GetBundleMutex(const std::string & bundleName)2708 std::mutex &BundleDataMgr::GetBundleMutex(const std::string &bundleName)
2709 {
2710     bundleMutex_.lock_shared();
2711     auto it = bundleMutexMap_.find(bundleName);
2712     if (it == bundleMutexMap_.end()) {
2713         bundleMutex_.unlock_shared();
2714         std::unique_lock lock {bundleMutex_};
2715         return bundleMutexMap_[bundleName];
2716     }
2717     bundleMutex_.unlock_shared();
2718     return it->second;
2719 }
2720 
GetProvisionId(const std::string & bundleName,std::string & provisionId) const2721 bool BundleDataMgr::GetProvisionId(const std::string &bundleName, std::string &provisionId) const
2722 {
2723     APP_LOGD("GetProvisionId %{public}s", bundleName.c_str());
2724     if (bundleName.empty()) {
2725         APP_LOGE("bundleName empty");
2726         return false;
2727     }
2728     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2729     auto infoItem = bundleInfos_.find(bundleName);
2730     if (infoItem == bundleInfos_.end()) {
2731         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2732         return false;
2733     }
2734     provisionId = infoItem->second.GetProvisionId();
2735     return true;
2736 }
2737 
GetAppFeature(const std::string & bundleName,std::string & appFeature) const2738 bool BundleDataMgr::GetAppFeature(const std::string &bundleName, std::string &appFeature) const
2739 {
2740     APP_LOGD("GetAppFeature %{public}s", bundleName.c_str());
2741     if (bundleName.empty()) {
2742         APP_LOGE("bundleName empty");
2743         return false;
2744     }
2745     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2746     auto infoItem = bundleInfos_.find(bundleName);
2747     if (infoItem == bundleInfos_.end()) {
2748         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
2749         return false;
2750     }
2751     appFeature = infoItem->second.GetAppFeature();
2752     return true;
2753 }
2754 
SetInitialUserFlag(bool flag)2755 void BundleDataMgr::SetInitialUserFlag(bool flag)
2756 {
2757     APP_LOGD("SetInitialUserFlag %{public}d", flag);
2758     if (!initialUserFlag_ && flag && bundlePromise_ != nullptr) {
2759         bundlePromise_->NotifyAllTasksExecuteFinished();
2760     }
2761 
2762     initialUserFlag_ = flag;
2763 }
2764 
CheckPublicKeys(const std::string & firstBundleName,const std::string & secondBundleName) const2765 int BundleDataMgr::CheckPublicKeys(const std::string &firstBundleName, const std::string &secondBundleName) const
2766 {
2767     APP_LOGD("CheckPublicKeys %{public}s and %{public}s", firstBundleName.c_str(), secondBundleName.c_str());
2768     if (firstBundleName.empty() || secondBundleName.empty()) {
2769         APP_LOGE("bundleName empty");
2770         return Constants::SIGNATURE_UNKNOWN_BUNDLE;
2771     }
2772     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2773     auto firstInfoItem = bundleInfos_.find(firstBundleName);
2774     if (firstInfoItem == bundleInfos_.end()) {
2775         APP_LOGE("can not find bundle %{public}s", firstBundleName.c_str());
2776         return Constants::SIGNATURE_UNKNOWN_BUNDLE;
2777     }
2778     auto secondInfoItem = bundleInfos_.find(secondBundleName);
2779     if (secondInfoItem == bundleInfos_.end()) {
2780         APP_LOGE("can not find bundle %{public}s", secondBundleName.c_str());
2781         return Constants::SIGNATURE_UNKNOWN_BUNDLE;
2782     }
2783     auto firstProvisionId = firstInfoItem->second.GetProvisionId();
2784     auto secondProvisionId = secondInfoItem->second.GetProvisionId();
2785     return (firstProvisionId == secondProvisionId) ? Constants::SIGNATURE_MATCHED : Constants::SIGNATURE_NOT_MATCHED;
2786 }
2787 
GetDataStorage() const2788 std::shared_ptr<IBundleDataStorage> BundleDataMgr::GetDataStorage() const
2789 {
2790     return dataStorage_;
2791 }
2792 
GetAllFormsInfo(std::vector<FormInfo> & formInfos) const2793 bool BundleDataMgr::GetAllFormsInfo(std::vector<FormInfo> &formInfos) const
2794 {
2795     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2796     if (bundleInfos_.empty()) {
2797         APP_LOGE("bundleInfos_ data is empty");
2798         return false;
2799     }
2800     auto result = false;
2801     for (const auto &item : bundleInfos_) {
2802         if (item.second.IsDisabled()) {
2803             APP_LOGW("app %{public}s is disabled", item.second.GetBundleName().c_str());
2804             continue;
2805         }
2806         item.second.GetFormsInfoByApp(formInfos);
2807         result = true;
2808     }
2809     APP_LOGD("all the form infos find success");
2810     return result;
2811 }
2812 
GetFormsInfoByModule(const std::string & bundleName,const std::string & moduleName,std::vector<FormInfo> & formInfos) const2813 bool BundleDataMgr::GetFormsInfoByModule(
2814     const std::string &bundleName, const std::string &moduleName, std::vector<FormInfo> &formInfos) const
2815 {
2816     if (bundleName.empty()) {
2817         APP_LOGW("bundle name is empty");
2818         return false;
2819     }
2820     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2821     if (bundleInfos_.empty()) {
2822         APP_LOGE("bundleInfos_ data is empty");
2823         return false;
2824     }
2825     auto infoItem = bundleInfos_.find(bundleName);
2826     if (infoItem == bundleInfos_.end()) {
2827         return false;
2828     }
2829     if (infoItem->second.IsDisabled()) {
2830         APP_LOGE("app %{public}s is disabled", infoItem->second.GetBundleName().c_str());
2831         return false;
2832     }
2833     infoItem->second.GetFormsInfoByModule(moduleName, formInfos);
2834     if (formInfos.empty()) {
2835         return false;
2836     }
2837     APP_LOGE("module forminfo find success");
2838     return true;
2839 }
2840 
GetFormsInfoByApp(const std::string & bundleName,std::vector<FormInfo> & formInfos) const2841 bool BundleDataMgr::GetFormsInfoByApp(const std::string &bundleName, std::vector<FormInfo> &formInfos) const
2842 {
2843     if (bundleName.empty()) {
2844         APP_LOGW("bundle name is empty");
2845         return false;
2846     }
2847     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2848     if (bundleInfos_.empty()) {
2849         APP_LOGE("bundleInfos_ data is empty");
2850         return false;
2851     }
2852     auto infoItem = bundleInfos_.find(bundleName);
2853     if (infoItem == bundleInfos_.end()) {
2854         return false;
2855     }
2856     if (infoItem->second.IsDisabled()) {
2857         APP_LOGE("app %{public}s is disabled", infoItem->second.GetBundleName().c_str());
2858         return false;
2859     }
2860     infoItem->second.GetFormsInfoByApp(formInfos);
2861     APP_LOGE("App forminfo find success");
2862     return true;
2863 }
2864 
GetShortcutInfos(const std::string & bundleName,int32_t userId,std::vector<ShortcutInfo> & shortcutInfos) const2865 bool BundleDataMgr::GetShortcutInfos(
2866     const std::string &bundleName, int32_t userId, std::vector<ShortcutInfo> &shortcutInfos) const
2867 {
2868     int32_t requestUserId = GetUserId(userId);
2869     if (requestUserId == Constants::INVALID_USERID) {
2870         return false;
2871     }
2872 
2873     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2874     InnerBundleInfo innerBundleInfo;
2875     if (!GetInnerBundleInfoWithFlags(
2876         bundleName, BundleFlag::GET_BUNDLE_DEFAULT, innerBundleInfo, requestUserId)) {
2877         APP_LOGE("GetShortcutInfos failed");
2878         return false;
2879     }
2880     innerBundleInfo.GetShortcutInfos(shortcutInfos);
2881     return true;
2882 }
2883 
GetShortcutInfoV9(const std::string & bundleName,int32_t userId,std::vector<ShortcutInfo> & shortcutInfos) const2884 ErrCode BundleDataMgr::GetShortcutInfoV9(
2885     const std::string &bundleName, int32_t userId, std::vector<ShortcutInfo> &shortcutInfos) const
2886 {
2887     int32_t requestUserId = GetUserId(userId);
2888     if (requestUserId == Constants::INVALID_USERID) {
2889         APP_LOGE("input invalid userid");
2890         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
2891     }
2892     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2893     InnerBundleInfo innerBundleInfo;
2894     ErrCode ret = GetInnerBundleInfoWithFlagsV9(bundleName,
2895         BundleFlag::GET_BUNDLE_DEFAULT, innerBundleInfo, requestUserId);
2896     if (ret != ERR_OK) {
2897         APP_LOGE("GetInnerBundleInfoWithFlagsV9 failed");
2898         return ret;
2899     }
2900 
2901     innerBundleInfo.GetShortcutInfos(shortcutInfos);
2902     return ERR_OK;
2903 }
2904 
GetAllCommonEventInfo(const std::string & eventKey,std::vector<CommonEventInfo> & commonEventInfos) const2905 bool BundleDataMgr::GetAllCommonEventInfo(const std::string &eventKey,
2906     std::vector<CommonEventInfo> &commonEventInfos) const
2907 {
2908     if (eventKey.empty()) {
2909         APP_LOGW("event key is empty");
2910         return false;
2911     }
2912     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
2913     if (bundleInfos_.empty()) {
2914         APP_LOGI("bundleInfos_ data is empty");
2915         return false;
2916     }
2917     for (const auto &item : bundleInfos_) {
2918         const InnerBundleInfo &info = item.second;
2919         if (info.IsDisabled()) {
2920             APP_LOGI("app %{public}s is disabled", info.GetBundleName().c_str());
2921             continue;
2922         }
2923         info.GetCommonEvents(eventKey, commonEventInfos);
2924     }
2925     if (commonEventInfos.size() == 0) {
2926         APP_LOGE("commonEventInfos is empty");
2927         return false;
2928     }
2929     APP_LOGE("commonEventInfos find success");
2930     return true;
2931 }
2932 
SavePreInstallBundleInfo(const std::string & bundleName,const PreInstallBundleInfo & preInstallBundleInfo)2933 bool BundleDataMgr::SavePreInstallBundleInfo(
2934     const std::string &bundleName, const PreInstallBundleInfo &preInstallBundleInfo)
2935 {
2936     std::lock_guard<std::mutex> lock(preInstallInfoMutex_);
2937     if (preInstallDataStorage_ == nullptr) {
2938         return false;
2939     }
2940 
2941     if (preInstallDataStorage_->SavePreInstallStorageBundleInfo(preInstallBundleInfo)) {
2942         auto info = std::find_if(
2943             preInstallBundleInfos_.begin(), preInstallBundleInfos_.end(), preInstallBundleInfo);
2944         if (info != preInstallBundleInfos_.end()) {
2945             *info = preInstallBundleInfo;
2946         } else {
2947             preInstallBundleInfos_.emplace_back(preInstallBundleInfo);
2948         }
2949         APP_LOGD("write storage success bundle:%{public}s", bundleName.c_str());
2950         return true;
2951     }
2952 
2953     return false;
2954 }
2955 
DeletePreInstallBundleInfo(const std::string & bundleName,const PreInstallBundleInfo & preInstallBundleInfo)2956 bool BundleDataMgr::DeletePreInstallBundleInfo(
2957     const std::string &bundleName, const PreInstallBundleInfo &preInstallBundleInfo)
2958 {
2959     std::lock_guard<std::mutex> lock(preInstallInfoMutex_);
2960     if (preInstallDataStorage_ == nullptr) {
2961         return false;
2962     }
2963 
2964     if (preInstallDataStorage_->DeletePreInstallStorageBundleInfo(preInstallBundleInfo)) {
2965         auto info = std::find_if(
2966             preInstallBundleInfos_.begin(), preInstallBundleInfos_.end(), preInstallBundleInfo);
2967         if (info != preInstallBundleInfos_.end()) {
2968             preInstallBundleInfos_.erase(info);
2969         }
2970         APP_LOGI("Delete PreInstall Storage success bundle:%{public}s", bundleName.c_str());
2971         return true;
2972     }
2973 
2974     return false;
2975 }
2976 
GetPreInstallBundleInfo(const std::string & bundleName,PreInstallBundleInfo & preInstallBundleInfo)2977 bool BundleDataMgr::GetPreInstallBundleInfo(
2978     const std::string &bundleName, PreInstallBundleInfo &preInstallBundleInfo)
2979 {
2980     std::lock_guard<std::mutex> lock(preInstallInfoMutex_);
2981     if (bundleName.empty()) {
2982         APP_LOGE("bundleName is empty");
2983         return false;
2984     }
2985 
2986     preInstallBundleInfo.SetBundleName(bundleName);
2987     auto info = std::find_if(
2988         preInstallBundleInfos_.begin(), preInstallBundleInfos_.end(), preInstallBundleInfo);
2989     if (info != preInstallBundleInfos_.end()) {
2990         preInstallBundleInfo = *info;
2991         return true;
2992     }
2993 
2994     APP_LOGE("get preInstall bundleInfo failed by bundle(%{public}s).", bundleName.c_str());
2995     return false;
2996 }
2997 
LoadAllPreInstallBundleInfos(std::vector<PreInstallBundleInfo> & preInstallBundleInfos)2998 bool BundleDataMgr::LoadAllPreInstallBundleInfos(std::vector<PreInstallBundleInfo> &preInstallBundleInfos)
2999 {
3000     if (preInstallDataStorage_ == nullptr) {
3001         return false;
3002     }
3003 
3004     if (preInstallDataStorage_->LoadAllPreInstallBundleInfos(preInstallBundleInfos)) {
3005         APP_LOGD("load all storage success");
3006         return true;
3007     }
3008 
3009     return false;
3010 }
3011 
SaveInnerBundleInfo(const InnerBundleInfo & info) const3012 bool BundleDataMgr::SaveInnerBundleInfo(const InnerBundleInfo &info) const
3013 {
3014     APP_LOGD("write install InnerBundleInfo to storage with bundle:%{public}s", info.GetBundleName().c_str());
3015     if (dataStorage_->SaveStorageBundleInfo(info)) {
3016         APP_LOGD("save install InnerBundleInfo successfully");
3017         return true;
3018     }
3019     APP_LOGE("save install InnerBundleInfo failed!");
3020     return false;
3021 }
3022 
GetInnerBundleUserInfoByUserId(const std::string & bundleName,int32_t userId,InnerBundleUserInfo & innerBundleUserInfo) const3023 bool BundleDataMgr::GetInnerBundleUserInfoByUserId(const std::string &bundleName,
3024     int32_t userId, InnerBundleUserInfo &innerBundleUserInfo) const
3025 {
3026     APP_LOGD("get user info start: bundleName: (%{public}s)  userId: (%{public}d) ",
3027         bundleName.c_str(), userId);
3028     int32_t requestUserId = GetUserId(userId);
3029     if (requestUserId == Constants::INVALID_USERID) {
3030         return false;
3031     }
3032 
3033     if (bundleName.empty()) {
3034         APP_LOGW("bundle name is empty");
3035         return false;
3036     }
3037 
3038     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3039     if (bundleInfos_.empty()) {
3040         APP_LOGE("bundleInfos data is empty");
3041         return false;
3042     }
3043 
3044     auto infoItem = bundleInfos_.find(bundleName);
3045     if (infoItem == bundleInfos_.end()) {
3046         return false;
3047     }
3048     if (infoItem->second.IsDisabled()) {
3049         APP_LOGE("app %{public}s is disabled", infoItem->second.GetBundleName().c_str());
3050         return false;
3051     }
3052 
3053     return infoItem->second.GetInnerBundleUserInfo(requestUserId, innerBundleUserInfo);
3054 }
3055 
GetUserId(int32_t userId) const3056 int32_t BundleDataMgr::GetUserId(int32_t userId) const
3057 {
3058     if (userId == Constants::ANY_USERID || userId == Constants::ALL_USERID) {
3059         return userId;
3060     }
3061 
3062     if (userId == Constants::UNSPECIFIED_USERID) {
3063         userId = GetUserIdByCallingUid();
3064     }
3065 
3066     if (!HasUserId(userId)) {
3067         APP_LOGE("user is not existed.");
3068         userId = Constants::INVALID_USERID;
3069     }
3070 
3071     return userId;
3072 }
3073 
GetUserIdByUid(int32_t uid) const3074 int32_t BundleDataMgr::GetUserIdByUid(int32_t uid) const
3075 {
3076     return BundleUtil::GetUserIdByUid(uid);
3077 }
3078 
AddUserId(int32_t userId)3079 void BundleDataMgr::AddUserId(int32_t userId)
3080 {
3081     std::lock_guard<std::mutex> lock(multiUserIdSetMutex_);
3082     auto item = multiUserIdsSet_.find(userId);
3083     if (item != multiUserIdsSet_.end()) {
3084         return;
3085     }
3086 
3087     multiUserIdsSet_.insert(userId);
3088 }
3089 
RemoveUserId(int32_t userId)3090 void BundleDataMgr::RemoveUserId(int32_t userId)
3091 {
3092     std::lock_guard<std::mutex> lock(multiUserIdSetMutex_);
3093     auto item = multiUserIdsSet_.find(userId);
3094     if (item == multiUserIdsSet_.end()) {
3095         return;
3096     }
3097 
3098     multiUserIdsSet_.erase(item);
3099 }
3100 
HasUserId(int32_t userId) const3101 bool BundleDataMgr::HasUserId(int32_t userId) const
3102 {
3103     std::lock_guard<std::mutex> lock(multiUserIdSetMutex_);
3104     return multiUserIdsSet_.find(userId) != multiUserIdsSet_.end();
3105 }
3106 
GetUserIdByCallingUid() const3107 int32_t BundleDataMgr::GetUserIdByCallingUid() const
3108 {
3109     return BundleUtil::GetUserIdByCallingUid();
3110 }
3111 
GetAllUser() const3112 std::set<int32_t> BundleDataMgr::GetAllUser() const
3113 {
3114     std::lock_guard<std::mutex> lock(multiUserIdSetMutex_);
3115     return multiUserIdsSet_;
3116 }
3117 
GetInnerBundleUserInfos(const std::string & bundleName,std::vector<InnerBundleUserInfo> & innerBundleUserInfos) const3118 bool BundleDataMgr::GetInnerBundleUserInfos(
3119     const std::string &bundleName, std::vector<InnerBundleUserInfo> &innerBundleUserInfos) const
3120 {
3121     APP_LOGD("get all user info in bundle(%{public}s)", bundleName.c_str());
3122     if (bundleName.empty()) {
3123         APP_LOGW("bundle name is empty");
3124         return false;
3125     }
3126 
3127     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3128     if (bundleInfos_.empty()) {
3129         APP_LOGE("bundleInfos data is empty");
3130         return false;
3131     }
3132 
3133     auto infoItem = bundleInfos_.find(bundleName);
3134     if (infoItem == bundleInfos_.end()) {
3135         return false;
3136     }
3137     if (infoItem->second.IsDisabled()) {
3138         APP_LOGE("app %{public}s is disabled", infoItem->second.GetBundleName().c_str());
3139         return false;
3140     }
3141 
3142     for (auto userInfo : infoItem->second.GetInnerBundleUserInfos()) {
3143         innerBundleUserInfos.emplace_back(userInfo.second);
3144     }
3145 
3146     return !innerBundleUserInfos.empty();
3147 }
3148 
GetAppPrivilegeLevel(const std::string & bundleName,int32_t userId)3149 std::string BundleDataMgr::GetAppPrivilegeLevel(const std::string &bundleName, int32_t userId)
3150 {
3151     APP_LOGD("GetAppPrivilegeLevel:%{public}s, userId:%{public}d", bundleName.c_str(), userId);
3152     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3153     InnerBundleInfo info;
3154     if (!GetInnerBundleInfoWithFlags(bundleName, 0, info, userId)) {
3155         return Constants::EMPTY_STRING;
3156     }
3157 
3158     return info.GetAppPrivilegeLevel();
3159 }
3160 
QueryExtensionAbilityInfos(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos,int32_t appIndex) const3161 bool BundleDataMgr::QueryExtensionAbilityInfos(const Want &want, int32_t flags, int32_t userId,
3162     std::vector<ExtensionAbilityInfo> &extensionInfos, int32_t appIndex) const
3163 {
3164     int32_t requestUserId = GetUserId(userId);
3165     if (requestUserId == Constants::INVALID_USERID) {
3166         return false;
3167     }
3168 
3169     ElementName element = want.GetElement();
3170     std::string bundleName = element.GetBundleName();
3171     std::string extensionName = element.GetAbilityName();
3172     APP_LOGD("bundle name:%{public}s, extension name:%{public}s",
3173         bundleName.c_str(), extensionName.c_str());
3174     // explicit query
3175     if (!bundleName.empty() && !extensionName.empty()) {
3176         ExtensionAbilityInfo info;
3177         bool ret = ExplicitQueryExtensionInfo(want, flags, requestUserId, info, appIndex);
3178         if (!ret) {
3179             APP_LOGE("explicit queryExtensionInfo error");
3180             return false;
3181         }
3182         extensionInfos.emplace_back(info);
3183         return true;
3184     }
3185 
3186     bool ret = ImplicitQueryExtensionInfos(want, flags, requestUserId, extensionInfos, appIndex);
3187     if (!ret) {
3188         APP_LOGE("implicit queryExtensionAbilityInfos error");
3189         return false;
3190     }
3191     if (extensionInfos.size() == 0) {
3192         APP_LOGE("no matching abilityInfo");
3193         return false;
3194     }
3195     APP_LOGD("query extensionAbilityInfo successfully");
3196     return true;
3197 }
3198 
QueryExtensionAbilityInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos,int32_t appIndex) const3199 ErrCode BundleDataMgr::QueryExtensionAbilityInfosV9(const Want &want, int32_t flags, int32_t userId,
3200     std::vector<ExtensionAbilityInfo> &extensionInfos, int32_t appIndex) const
3201 {
3202     int32_t requestUserId = GetUserId(userId);
3203     if (requestUserId == Constants::INVALID_USERID) {
3204         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
3205     }
3206 
3207     ElementName element = want.GetElement();
3208     std::string bundleName = element.GetBundleName();
3209     std::string extensionName = element.GetAbilityName();
3210     APP_LOGD("bundle name:%{public}s, extension name:%{public}s",
3211         bundleName.c_str(), extensionName.c_str());
3212     // explicit query
3213     if (!bundleName.empty() && !extensionName.empty()) {
3214         ExtensionAbilityInfo info;
3215         ErrCode ret = ExplicitQueryExtensionInfoV9(want, flags, requestUserId, info, appIndex);
3216         if (ret != ERR_OK) {
3217             APP_LOGE("explicit queryExtensionInfo error");
3218             return ret;
3219         }
3220         extensionInfos.emplace_back(info);
3221         return ERR_OK;
3222     }
3223 
3224     ErrCode ret = ImplicitQueryExtensionInfosV9(want, flags, requestUserId, extensionInfos, appIndex);
3225     if (ret != ERR_OK) {
3226         APP_LOGE("ImplicitQueryExtensionInfosV9 error");
3227         return ret;
3228     }
3229     if (extensionInfos.empty()) {
3230         APP_LOGE("no matching abilityInfo");
3231         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3232     }
3233     APP_LOGD("QueryExtensionAbilityInfosV9 success");
3234     return ERR_OK;
3235 }
3236 
ExplicitQueryExtensionInfo(const Want & want,int32_t flags,int32_t userId,ExtensionAbilityInfo & extensionInfo,int32_t appIndex) const3237 bool BundleDataMgr::ExplicitQueryExtensionInfo(const Want &want, int32_t flags, int32_t userId,
3238     ExtensionAbilityInfo &extensionInfo, int32_t appIndex) const
3239 {
3240     ElementName element = want.GetElement();
3241     std::string bundleName = element.GetBundleName();
3242     std::string moduleName = element.GetModuleName();
3243     std::string extensionName = element.GetAbilityName();
3244     APP_LOGD("bundleName:%{public}s, moduleName:%{public}s, abilityName:%{public}s",
3245         bundleName.c_str(), moduleName.c_str(), extensionName.c_str());
3246     APP_LOGD("flags:%{public}d, userId:%{public}d", flags, userId);
3247     int32_t requestUserId = GetUserId(userId);
3248     if (requestUserId == Constants::INVALID_USERID) {
3249         return false;
3250     }
3251     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3252     InnerBundleInfo innerBundleInfo;
3253     if ((appIndex == 0) && (!GetInnerBundleInfoWithFlags(bundleName, flags, innerBundleInfo, requestUserId))) {
3254         APP_LOGE("ExplicitQueryExtensionInfo failed");
3255         return false;
3256     }
3257     if (appIndex > 0) {
3258         if (sandboxAppHelper_ == nullptr) {
3259             APP_LOGE("sandboxAppHelper_ is nullptr");
3260             return false;
3261         }
3262         auto ret = sandboxAppHelper_->GetSandboxAppInfo(bundleName, appIndex, requestUserId, innerBundleInfo);
3263         if (ret != ERR_OK) {
3264             APP_LOGE("obtain innerBundleInfo of sandbox app failed due to errCode %{public}d", ret);
3265             return false;
3266         }
3267     }
3268     auto extension = innerBundleInfo.FindExtensionInfo(bundleName, moduleName, extensionName);
3269     if (!extension) {
3270         APP_LOGE("extensionAbility not found or disabled");
3271         return false;
3272     }
3273     if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_PERMISSION) != GET_ABILITY_INFO_WITH_PERMISSION) {
3274         extension->permissions.clear();
3275     }
3276     if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_METADATA) != GET_ABILITY_INFO_WITH_METADATA) {
3277         extension->metadata.clear();
3278     }
3279     extensionInfo = (*extension);
3280     if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_APPLICATION) == GET_ABILITY_INFO_WITH_APPLICATION) {
3281         int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
3282         innerBundleInfo.GetApplicationInfo(
3283             ApplicationFlag::GET_BASIC_APPLICATION_INFO |
3284             ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, responseUserId,
3285             extensionInfo.applicationInfo);
3286     }
3287     return true;
3288 }
3289 
ExplicitQueryExtensionInfoV9(const Want & want,int32_t flags,int32_t userId,ExtensionAbilityInfo & extensionInfo,int32_t appIndex) const3290 ErrCode BundleDataMgr::ExplicitQueryExtensionInfoV9(const Want &want, int32_t flags, int32_t userId,
3291     ExtensionAbilityInfo &extensionInfo, int32_t appIndex) const
3292 {
3293     ElementName element = want.GetElement();
3294     std::string bundleName = element.GetBundleName();
3295     std::string moduleName = element.GetModuleName();
3296     std::string extensionName = element.GetAbilityName();
3297     APP_LOGD("bundleName:%{public}s, moduleName:%{public}s, abilityName:%{public}s",
3298         bundleName.c_str(), moduleName.c_str(), extensionName.c_str());
3299     APP_LOGD("flags:%{public}d, userId:%{public}d", flags, userId);
3300     int32_t requestUserId = GetUserId(userId);
3301     if (requestUserId == Constants::INVALID_USERID) {
3302         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
3303     }
3304     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3305     InnerBundleInfo innerBundleInfo;
3306     if (appIndex == 0) {
3307         ErrCode ret = GetInnerBundleInfoWithFlagsV9(bundleName, flags, innerBundleInfo, requestUserId);
3308         if (ret != ERR_OK) {
3309             APP_LOGE("ExplicitQueryExtensionInfoV9 failed");
3310             return ret;
3311         }
3312     }
3313     if (appIndex > 0) {
3314         if (sandboxAppHelper_ == nullptr) {
3315             APP_LOGE("sandboxAppHelper_ is nullptr");
3316             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3317         }
3318         auto ret = sandboxAppHelper_->GetSandboxAppInfo(bundleName, appIndex, requestUserId, innerBundleInfo);
3319         if (ret != ERR_OK) {
3320             APP_LOGE("obtain innerBundleInfo of sandbox app failed due to errCode %{public}d", ret);
3321             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3322         }
3323     }
3324     auto extension = innerBundleInfo.FindExtensionInfo(bundleName, moduleName, extensionName);
3325     if (!extension) {
3326         APP_LOGE("extensionAbility not found or disabled");
3327         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3328     }
3329     if ((static_cast<uint32_t>(flags) &
3330         static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_PERMISSION)) !=
3331         static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_PERMISSION)) {
3332         extension->permissions.clear();
3333     }
3334     if ((static_cast<uint32_t>(flags) &
3335         static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_METADATA)) !=
3336         static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_METADATA)) {
3337         extension->metadata.clear();
3338     }
3339     extensionInfo = (*extension);
3340     if ((static_cast<uint32_t>(flags) &
3341         static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_APPLICATION)) ==
3342         static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_APPLICATION)) {
3343         int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
3344         innerBundleInfo.GetApplicationInfoV9(static_cast<int32_t>(
3345             GetApplicationFlag::GET_APPLICATION_INFO_DEFAULT), responseUserId, extensionInfo.applicationInfo);
3346     }
3347     return ERR_OK;
3348 }
3349 
FilterExtensionAbilityInfosByModuleName(const std::string & moduleName,std::vector<ExtensionAbilityInfo> & extensionInfos) const3350 void BundleDataMgr::FilterExtensionAbilityInfosByModuleName(const std::string &moduleName,
3351     std::vector<ExtensionAbilityInfo> &extensionInfos) const
3352 {
3353     APP_LOGD("BundleDataMgr::FilterExtensionAbilityInfosByModuleName moduleName: %{public}s", moduleName.c_str());
3354     if (moduleName.empty()) {
3355         return;
3356     }
3357     for (auto iter = extensionInfos.begin(); iter != extensionInfos.end();) {
3358         if (iter->moduleName != moduleName) {
3359             iter = extensionInfos.erase(iter);
3360         } else {
3361             ++iter;
3362         }
3363     }
3364 }
3365 
ImplicitQueryExtensionInfos(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos,int32_t appIndex) const3366 bool BundleDataMgr::ImplicitQueryExtensionInfos(const Want &want, int32_t flags, int32_t userId,
3367     std::vector<ExtensionAbilityInfo> &extensionInfos, int32_t appIndex) const
3368 {
3369     if (want.GetAction().empty() && want.GetEntities().empty()
3370         && want.GetUriString().empty() && want.GetType().empty()) {
3371         APP_LOGE("param invalid");
3372         return false;
3373     }
3374     APP_LOGD("action:%{public}s, uri:%{private}s, type:%{public}s",
3375         want.GetAction().c_str(), want.GetUriString().c_str(), want.GetType().c_str());
3376     APP_LOGD("flags:%{public}d, userId:%{public}d", flags, userId);
3377 
3378     int32_t requestUserId = GetUserId(userId);
3379     if (requestUserId == Constants::INVALID_USERID) {
3380         return false;
3381     }
3382     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3383     std::string bundleName = want.GetElement().GetBundleName();
3384     if (!bundleName.empty()) {
3385         // query in current bundle
3386         if (!ImplicitQueryCurExtensionInfos(want, flags, requestUserId, extensionInfos, appIndex)) {
3387             APP_LOGE("ImplicitQueryCurExtensionInfos failed");
3388             return false;
3389         }
3390     } else {
3391         // query all
3392         ImplicitQueryAllExtensionInfos(want, flags, requestUserId, extensionInfos, appIndex);
3393     }
3394     // sort by priority, descending order.
3395     if (extensionInfos.size() > 1) {
3396         std::stable_sort(extensionInfos.begin(), extensionInfos.end(),
3397             [](ExtensionAbilityInfo a, ExtensionAbilityInfo b) { return a.priority > b.priority; });
3398     }
3399     return true;
3400 }
3401 
ImplicitQueryExtensionInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & extensionInfos,int32_t appIndex) const3402 ErrCode BundleDataMgr::ImplicitQueryExtensionInfosV9(const Want &want, int32_t flags, int32_t userId,
3403     std::vector<ExtensionAbilityInfo> &extensionInfos, int32_t appIndex) const
3404 {
3405     if (want.GetAction().empty() && want.GetEntities().empty()
3406         && want.GetUriString().empty() && want.GetType().empty()) {
3407         APP_LOGE("param invalid");
3408         return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3409     }
3410     APP_LOGD("action:%{public}s, uri:%{private}s, type:%{public}s",
3411         want.GetAction().c_str(), want.GetUriString().c_str(), want.GetType().c_str());
3412     APP_LOGD("flags:%{public}d, userId:%{public}d", flags, userId);
3413 
3414     int32_t requestUserId = GetUserId(userId);
3415     if (requestUserId == Constants::INVALID_USERID) {
3416         return ERR_BUNDLE_MANAGER_INVALID_USER_ID;
3417     }
3418     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3419     std::string bundleName = want.GetElement().GetBundleName();
3420     if (!bundleName.empty()) {
3421         // query in current bundle
3422         ErrCode ret = ImplicitQueryCurExtensionInfosV9(want, flags, requestUserId, extensionInfos, appIndex);
3423         if (ret != ERR_OK) {
3424             APP_LOGE("ImplicitQueryCurExtensionInfos failed");
3425             return ret;
3426         }
3427     } else {
3428         // query all
3429         ImplicitQueryAllExtensionInfosV9(want, flags, requestUserId, extensionInfos, appIndex);
3430     }
3431     // sort by priority, descending order.
3432     if (extensionInfos.size() > 1) {
3433         std::stable_sort(extensionInfos.begin(), extensionInfos.end(),
3434             [](ExtensionAbilityInfo a, ExtensionAbilityInfo b) { return a.priority > b.priority; });
3435     }
3436     return ERR_OK;
3437 }
3438 
ImplicitQueryCurExtensionInfos(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & infos,int32_t appIndex) const3439 bool BundleDataMgr::ImplicitQueryCurExtensionInfos(const Want &want, int32_t flags, int32_t userId,
3440     std::vector<ExtensionAbilityInfo> &infos, int32_t appIndex) const
3441 {
3442     APP_LOGD("begin to ImplicitQueryCurExtensionInfos");
3443     std::string bundleName = want.GetElement().GetBundleName();
3444     InnerBundleInfo innerBundleInfo;
3445     if ((appIndex == 0) && (!GetInnerBundleInfoWithFlags(bundleName, flags, innerBundleInfo, userId))) {
3446         APP_LOGE("ImplicitQueryExtensionAbilityInfos failed");
3447         return false;
3448     }
3449     if (appIndex > 0) {
3450         if (sandboxAppHelper_ == nullptr) {
3451             APP_LOGE("sandboxAppHelper_ is nullptr");
3452             return false;
3453         }
3454         auto ret = sandboxAppHelper_->GetSandboxAppInfo(bundleName, appIndex, userId, innerBundleInfo);
3455         if (ret != ERR_OK) {
3456             APP_LOGE("obtain innerBundleInfo of sandbox app failed due to errCode %{public}d", ret);
3457             return false;
3458         }
3459     }
3460     int32_t responseUserId = innerBundleInfo.GetResponseUserId(userId);
3461     GetMatchExtensionInfos(want, flags, responseUserId, innerBundleInfo, infos);
3462     FilterExtensionAbilityInfosByModuleName(want.GetElement().GetModuleName(), infos);
3463     APP_LOGD("finish to ImplicitQueryCurExtensionInfos");
3464     return true;
3465 }
3466 
ImplicitQueryCurExtensionInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & infos,int32_t appIndex) const3467 ErrCode BundleDataMgr::ImplicitQueryCurExtensionInfosV9(const Want &want, int32_t flags, int32_t userId,
3468     std::vector<ExtensionAbilityInfo> &infos, int32_t appIndex) const
3469 {
3470     APP_LOGD("begin to ImplicitQueryCurExtensionInfosV9");
3471     std::string bundleName = want.GetElement().GetBundleName();
3472     InnerBundleInfo innerBundleInfo;
3473     if (appIndex == 0) {
3474         ErrCode ret = GetInnerBundleInfoWithFlagsV9(bundleName, flags, innerBundleInfo, userId);
3475         if (ret != ERR_OK) {
3476             APP_LOGE("GetInnerBundleInfoWithFlagsV9 failed");
3477             return ret;
3478         }
3479     }
3480     if (appIndex > 0) {
3481         if (sandboxAppHelper_ == nullptr) {
3482             APP_LOGE("sandboxAppHelper_ is nullptr");
3483             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3484         }
3485         auto ret = sandboxAppHelper_->GetSandboxAppInfo(bundleName, appIndex, userId, innerBundleInfo);
3486         if (ret != ERR_OK) {
3487             APP_LOGE("obtain innerBundleInfo of sandbox app failed due to errCode %{public}d", ret);
3488             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
3489         }
3490     }
3491     int32_t responseUserId = innerBundleInfo.GetResponseUserId(userId);
3492     GetMatchExtensionInfosV9(want, flags, responseUserId, innerBundleInfo, infos);
3493     FilterExtensionAbilityInfosByModuleName(want.GetElement().GetModuleName(), infos);
3494     APP_LOGD("finish to ImplicitQueryCurExtensionInfosV9");
3495     return ERR_OK;
3496 }
3497 
ImplicitQueryAllExtensionInfos(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & infos,int32_t appIndex) const3498 void BundleDataMgr::ImplicitQueryAllExtensionInfos(const Want &want, int32_t flags, int32_t userId,
3499     std::vector<ExtensionAbilityInfo> &infos, int32_t appIndex) const
3500 {
3501     APP_LOGD("begin to ImplicitQueryAllExtensionInfos");
3502     // query from bundleInfos_
3503     if (appIndex == 0) {
3504         for (const auto &item : bundleInfos_) {
3505             InnerBundleInfo innerBundleInfo;
3506             if (!GetInnerBundleInfoWithFlags(item.first, flags, innerBundleInfo, userId)) {
3507                 APP_LOGE("ImplicitQueryExtensionAbilityInfos failed");
3508                 continue;
3509             }
3510             int32_t responseUserId = innerBundleInfo.GetResponseUserId(userId);
3511             GetMatchExtensionInfos(want, flags, responseUserId, innerBundleInfo, infos);
3512         }
3513     } else {
3514         // query from sandbox manager for sandbox bundle
3515         if (sandboxAppHelper_ == nullptr) {
3516             APP_LOGE("sandboxAppHelper_ is nullptr");
3517             return;
3518         }
3519         auto sandboxMap = sandboxAppHelper_->GetSandboxAppInfoMap();
3520         for (const auto &item : sandboxMap) {
3521             InnerBundleInfo info;
3522             size_t pos = item.first.rfind(Constants::FILE_UNDERLINE);
3523             if (pos == std::string::npos) {
3524                 APP_LOGW("sandbox map contains invalid element");
3525                 continue;
3526             }
3527             std::string innerBundleName = item.first.substr(0, pos);
3528             if (sandboxAppHelper_->GetSandboxAppInfo(innerBundleName, appIndex, userId, info) != ERR_OK) {
3529                 APP_LOGW("obtain innerBundleInfo of sandbox app failed");
3530                 continue;
3531             }
3532 
3533             int32_t responseUserId = info.GetResponseUserId(userId);
3534             GetMatchExtensionInfos(want, flags, responseUserId, info, infos);
3535         }
3536     }
3537     APP_LOGD("finish to ImplicitQueryAllExtensionInfos");
3538 }
3539 
ImplicitQueryAllExtensionInfosV9(const Want & want,int32_t flags,int32_t userId,std::vector<ExtensionAbilityInfo> & infos,int32_t appIndex) const3540 void BundleDataMgr::ImplicitQueryAllExtensionInfosV9(const Want &want, int32_t flags, int32_t userId,
3541     std::vector<ExtensionAbilityInfo> &infos, int32_t appIndex) const
3542 {
3543     APP_LOGD("begin to ImplicitQueryAllExtensionInfosV9");
3544     // query from bundleInfos_
3545     if (appIndex == 0) {
3546         for (const auto &item : bundleInfos_) {
3547             InnerBundleInfo innerBundleInfo;
3548             ErrCode ret = GetInnerBundleInfoWithFlagsV9(item.first, flags, innerBundleInfo, userId);
3549             if (ret != ERR_OK) {
3550                 APP_LOGE("ImplicitQueryExtensionAbilityInfos failed");
3551                 continue;
3552             }
3553             int32_t responseUserId = innerBundleInfo.GetResponseUserId(userId);
3554             GetMatchExtensionInfosV9(want, flags, responseUserId, innerBundleInfo, infos);
3555         }
3556     } else {
3557         // query from sandbox manager for sandbox bundle
3558         if (sandboxAppHelper_ == nullptr) {
3559             APP_LOGE("sandboxAppHelper_ is nullptr");
3560             return;
3561         }
3562         auto sandboxMap = sandboxAppHelper_->GetSandboxAppInfoMap();
3563         for (const auto &item : sandboxMap) {
3564             InnerBundleInfo info;
3565             size_t pos = item.first.rfind(Constants::FILE_UNDERLINE);
3566             if (pos == std::string::npos) {
3567                 APP_LOGW("sandbox map contains invalid element");
3568                 continue;
3569             }
3570             std::string innerBundleName = item.first.substr(0, pos);
3571             if (sandboxAppHelper_->GetSandboxAppInfo(innerBundleName, appIndex, userId, info) != ERR_OK) {
3572                 APP_LOGW("obtain innerBundleInfo of sandbox app failed");
3573                 continue;
3574             }
3575 
3576             int32_t responseUserId = info.GetResponseUserId(userId);
3577             GetMatchExtensionInfosV9(want, flags, responseUserId, info, infos);
3578         }
3579     }
3580     APP_LOGD("finish to ImplicitQueryAllExtensionInfosV9");
3581 }
3582 
GetMatchExtensionInfos(const Want & want,int32_t flags,const int32_t & userId,const InnerBundleInfo & info,std::vector<ExtensionAbilityInfo> & infos) const3583 void BundleDataMgr::GetMatchExtensionInfos(const Want &want, int32_t flags, const int32_t &userId,
3584     const InnerBundleInfo &info, std::vector<ExtensionAbilityInfo> &infos) const
3585 {
3586     auto extensionSkillInfos = info.GetExtensionSkillInfos();
3587     auto extensionInfos = info.GetInnerExtensionInfos();
3588     for (const auto &skillInfos : extensionSkillInfos) {
3589         for (const auto &skill : skillInfos.second) {
3590             if (!skill.Match(want)) {
3591                 continue;
3592             }
3593             if (extensionInfos.find(skillInfos.first) == extensionInfos.end()) {
3594                 APP_LOGW("cannot find the extension info with %{public}s", skillInfos.first.c_str());
3595                 break;
3596             }
3597             ExtensionAbilityInfo extensionInfo = extensionInfos[skillInfos.first];
3598             if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_APPLICATION) ==
3599                 GET_ABILITY_INFO_WITH_APPLICATION) {
3600                 info.GetApplicationInfo(
3601                     ApplicationFlag::GET_BASIC_APPLICATION_INFO |
3602                     ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, userId,
3603                     extensionInfo.applicationInfo);
3604             }
3605             if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_PERMISSION) !=
3606                 GET_ABILITY_INFO_WITH_PERMISSION) {
3607                 extensionInfo.permissions.clear();
3608             }
3609             if ((static_cast<uint32_t>(flags) & GET_ABILITY_INFO_WITH_METADATA) != GET_ABILITY_INFO_WITH_METADATA) {
3610                 extensionInfo.metadata.clear();
3611             }
3612             infos.emplace_back(extensionInfo);
3613             break;
3614         }
3615     }
3616 }
3617 
GetMatchExtensionInfosV9(const Want & want,int32_t flags,int32_t userId,const InnerBundleInfo & info,std::vector<ExtensionAbilityInfo> & infos) const3618 void BundleDataMgr::GetMatchExtensionInfosV9(const Want &want, int32_t flags, int32_t userId,
3619     const InnerBundleInfo &info, std::vector<ExtensionAbilityInfo> &infos) const
3620 {
3621     auto extensionSkillInfos = info.GetExtensionSkillInfos();
3622     auto extensionInfos = info.GetInnerExtensionInfos();
3623     for (const auto &skillInfos : extensionSkillInfos) {
3624         for (const auto &skill : skillInfos.second) {
3625             if (!skill.Match(want)) {
3626                 continue;
3627             }
3628             if (extensionInfos.find(skillInfos.first) == extensionInfos.end()) {
3629                 APP_LOGW("cannot find the extension info with %{public}s", skillInfos.first.c_str());
3630                 break;
3631             }
3632             ExtensionAbilityInfo extensionInfo = extensionInfos[skillInfos.first];
3633             if ((static_cast<uint32_t>(flags) &
3634                 static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_APPLICATION)) ==
3635                 static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_APPLICATION)) {
3636                 info.GetApplicationInfoV9(static_cast<int32_t>(
3637                     GetApplicationFlag::GET_APPLICATION_INFO_DEFAULT), userId, extensionInfo.applicationInfo);
3638             }
3639             if ((static_cast<uint32_t>(flags) &
3640                 static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_PERMISSION)) !=
3641                 static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_PERMISSION)) {
3642                 extensionInfo.permissions.clear();
3643             }
3644             if ((static_cast<uint32_t>(flags) &
3645                 static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_METADATA)) !=
3646                 static_cast<int32_t>(GetExtensionAbilityInfoFlag::GET_EXTENSION_ABILITY_INFO_WITH_METADATA)) {
3647                 extensionInfo.metadata.clear();
3648             }
3649             infos.emplace_back(extensionInfo);
3650             break;
3651         }
3652     }
3653 }
3654 
QueryExtensionAbilityInfos(const ExtensionAbilityType & extensionType,const int32_t & userId,std::vector<ExtensionAbilityInfo> & extensionInfos) const3655 bool BundleDataMgr::QueryExtensionAbilityInfos(const ExtensionAbilityType &extensionType, const int32_t &userId,
3656     std::vector<ExtensionAbilityInfo> &extensionInfos) const
3657 {
3658     int32_t requestUserId = GetUserId(userId);
3659     if (requestUserId == Constants::INVALID_USERID) {
3660         return false;
3661     }
3662     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3663     for (const auto &item : bundleInfos_) {
3664         InnerBundleInfo innerBundleInfo;
3665         if (!GetInnerBundleInfoWithFlags(item.first, 0, innerBundleInfo, requestUserId)) {
3666             APP_LOGE("QueryExtensionAbilityInfos failed");
3667             continue;
3668         }
3669         auto innerExtensionInfos = innerBundleInfo.GetInnerExtensionInfos();
3670         int32_t responseUserId = innerBundleInfo.GetResponseUserId(requestUserId);
3671         for (const auto &info : innerExtensionInfos) {
3672             if (info.second.type == extensionType) {
3673                 ExtensionAbilityInfo extensionAbilityInfo = info.second;
3674                 innerBundleInfo.GetApplicationInfo(
3675                     ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, responseUserId,
3676                     extensionAbilityInfo.applicationInfo);
3677                 extensionInfos.emplace_back(extensionAbilityInfo);
3678             }
3679         }
3680     }
3681     return true;
3682 }
3683 
GetAccessibleAppCodePaths(int32_t userId) const3684 std::vector<std::string> BundleDataMgr::GetAccessibleAppCodePaths(int32_t userId) const
3685 {
3686     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3687     std::vector<std::string> vec;
3688     if (bundleInfos_.empty()) {
3689         APP_LOGE("bundleInfos_ is empty");
3690         return vec;
3691     }
3692 
3693     for (const auto &item : bundleInfos_) {
3694         const InnerBundleInfo &info = item.second;
3695         auto userInfoMap = info.GetInnerBundleUserInfos();
3696         for (const auto &userInfo : userInfoMap) {
3697             auto innerUserId = userInfo.second.bundleUserInfo.userId;
3698             if (((innerUserId == 0) || (innerUserId == userId)) && info.IsAccessible()) {
3699                 vec.emplace_back(info.GetAppCodePath());
3700             }
3701         }
3702     }
3703     return vec;
3704 }
3705 
QueryExtensionAbilityInfoByUri(const std::string & uri,int32_t userId,ExtensionAbilityInfo & extensionAbilityInfo) const3706 bool BundleDataMgr::QueryExtensionAbilityInfoByUri(const std::string &uri, int32_t userId,
3707     ExtensionAbilityInfo &extensionAbilityInfo) const
3708 {
3709     int32_t requestUserId = GetUserId(userId);
3710     if (requestUserId == Constants::INVALID_USERID) {
3711         APP_LOGE("invalid userId -1");
3712         return false;
3713     }
3714     if (uri.empty()) {
3715         APP_LOGE("uri empty");
3716         return false;
3717     }
3718     // example of valid param uri : fileShare:///com.example.FileShare/person/10
3719     // example of convertUri : fileShare://com.example.FileShare
3720     size_t schemePos = uri.find(Constants::PARAM_URI_SEPARATOR);
3721     if (schemePos == uri.npos) {
3722         APP_LOGE("uri not include :///, invalid");
3723         return false;
3724     }
3725     size_t cutPos = uri.find(Constants::SEPARATOR, schemePos + Constants::PARAM_URI_SEPARATOR_LEN);
3726     // 1. cut string
3727     std::string convertUri = uri;
3728     if (cutPos != uri.npos) {
3729         convertUri = uri.substr(0, cutPos);
3730     }
3731     // 2. replace :/// with ://
3732     convertUri.replace(schemePos, Constants::PARAM_URI_SEPARATOR_LEN,
3733         Constants::URI_SEPARATOR);
3734     APP_LOGD("convertUri : %{private}s", convertUri.c_str());
3735 
3736     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3737     if (bundleInfos_.empty()) {
3738         APP_LOGE("bundleInfos_ data is empty");
3739         return false;
3740     }
3741     for (const auto &item : bundleInfos_) {
3742         const InnerBundleInfo &info = item.second;
3743         if (info.IsDisabled()) {
3744             APP_LOGE("app %{public}s is disabled", info.GetBundleName().c_str());
3745             continue;
3746         }
3747 
3748         int32_t responseUserId = info.GetResponseUserId(requestUserId);
3749         if (!info.GetApplicationEnabled(responseUserId)) {
3750             continue;
3751         }
3752 
3753         bool ret = info.FindExtensionAbilityInfoByUri(convertUri, extensionAbilityInfo);
3754         if (!ret) {
3755             continue;
3756         }
3757         info.GetApplicationInfo(
3758             ApplicationFlag::GET_APPLICATION_INFO_WITH_CERTIFICATE_FINGERPRINT, responseUserId,
3759             extensionAbilityInfo.applicationInfo);
3760         return true;
3761     }
3762     APP_LOGE("QueryExtensionAbilityInfoByUri (%{private}s) failed.", uri.c_str());
3763     return false;
3764 }
3765 
GetAllUriPrefix(std::vector<std::string> & uriPrefixList,int32_t userId,const std::string & excludeModule) const3766 void BundleDataMgr::GetAllUriPrefix(std::vector<std::string> &uriPrefixList, int32_t userId,
3767     const std::string &excludeModule) const
3768 {
3769     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3770     APP_LOGD("begin to GetAllUriPrefix, userId : %{public}d, excludeModule : %{public}s",
3771         userId, excludeModule.c_str());
3772     if (bundleInfos_.empty()) {
3773         APP_LOGE("bundleInfos_ is empty");
3774         return;
3775     }
3776     for (const auto &item : bundleInfos_) {
3777         item.second.GetUriPrefixList(uriPrefixList, userId, excludeModule);
3778         item.second.GetUriPrefixList(uriPrefixList, Constants::DEFAULT_USERID, excludeModule);
3779     }
3780 }
3781 
GetStringById(const std::string & bundleName,const std::string & moduleName,uint32_t resId,int32_t userId,const std::string & localeInfo)3782 std::string BundleDataMgr::GetStringById(const std::string &bundleName, const std::string &moduleName,
3783     uint32_t resId, int32_t userId, const std::string &localeInfo)
3784 {
3785     APP_LOGD("GetStringById:%{public}s , %{public}s, %{public}d", bundleName.c_str(), moduleName.c_str(), resId);
3786 #ifdef GLOBAL_RESMGR_ENABLE
3787     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3788     std::shared_ptr<OHOS::Global::Resource::ResourceManager> resourceManager =
3789         GetResourceManager(bundleName, moduleName, userId);
3790     if (resourceManager == nullptr) {
3791         APP_LOGE("InitResourceManager failed");
3792         return Constants::EMPTY_STRING;
3793     }
3794     std::string label;
3795     OHOS::Global::Resource::RState errValue = resourceManager->GetStringById(resId, label);
3796     if (errValue != OHOS::Global::Resource::RState::SUCCESS) {
3797         APP_LOGE("GetStringById failed");
3798         return Constants::EMPTY_STRING;
3799     }
3800     return label;
3801 #else
3802     APP_LOGW("GLOBAL_RESMGR_ENABLE is false");
3803     return Constants::EMPTY_STRING;
3804 #endif
3805 }
3806 
GetIconById(const std::string & bundleName,const std::string & moduleName,uint32_t resId,uint32_t density,int32_t userId)3807 std::string BundleDataMgr::GetIconById(
3808     const std::string &bundleName, const std::string &moduleName, uint32_t resId, uint32_t density, int32_t userId)
3809 {
3810     APP_LOGI("GetIconById bundleName:%{public}s, moduleName:%{public}s, resId:%{public}d, density:%{public}d",
3811         bundleName.c_str(), moduleName.c_str(), resId, density);
3812 #ifdef GLOBAL_RESMGR_ENABLE
3813     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3814     std::shared_ptr<OHOS::Global::Resource::ResourceManager> resourceManager =
3815         GetResourceManager(bundleName, moduleName, userId);
3816     if (resourceManager == nullptr) {
3817         APP_LOGE("InitResourceManager failed");
3818         return Constants::EMPTY_STRING;
3819     }
3820     std::string base64;
3821     OHOS::Global::Resource::RState errValue = resourceManager->GetMediaBase64DataById(resId, density, base64);
3822     if (errValue != OHOS::Global::Resource::RState::SUCCESS) {
3823         APP_LOGE("GetIconById failed");
3824         return Constants::EMPTY_STRING;
3825     }
3826     return base64;
3827 #else
3828     APP_LOGW("GLOBAL_RESMGR_ENABLE is false");
3829     return Constants::EMPTY_STRING;
3830 #endif
3831 }
3832 
3833 #ifdef GLOBAL_RESMGR_ENABLE
GetResourceManager(const std::string & bundleName,const std::string & moduleName,int32_t userId,const std::string & localeInfo) const3834 std::shared_ptr<Global::Resource::ResourceManager> BundleDataMgr::GetResourceManager(
3835     const std::string &bundleName, const std::string &moduleName, int32_t userId, const std::string &localeInfo) const
3836 {
3837     InnerBundleInfo innerBundleInfo;
3838     if (!GetInnerBundleInfoWithFlags(bundleName, BundleFlag::GET_BUNDLE_DEFAULT, innerBundleInfo, userId)) {
3839         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
3840         return nullptr;
3841     }
3842     int32_t responseUserId = innerBundleInfo.GetResponseUserId(userId);
3843     BundleInfo bundleInfo;
3844     innerBundleInfo.GetBundleInfo(BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, responseUserId);
3845     std::shared_ptr<Global::Resource::ResourceManager> resourceManager(Global::Resource::CreateResourceManager());
3846     for (auto hapModuleInfo : bundleInfo.hapModuleInfos) {
3847         std::string moduleResPath;
3848         if (moduleName.empty() || moduleName == hapModuleInfo.moduleName) {
3849             moduleResPath = hapModuleInfo.hapPath.empty() ? hapModuleInfo.resourcePath : hapModuleInfo.hapPath;
3850         }
3851         if (!moduleResPath.empty()) {
3852             APP_LOGD("DistributedBms::InitResourceManager, moduleResPath: %{private}s", moduleResPath.c_str());
3853             if (!resourceManager->AddResource(moduleResPath.c_str())) {
3854                 APP_LOGW("DistributedBms::InitResourceManager AddResource failed");
3855             }
3856         }
3857     }
3858 
3859     std::unique_ptr<Global::Resource::ResConfig> resConfig(Global::Resource::CreateResConfig());
3860 #ifdef GLOBAL_I18_ENABLE
3861     std::map<std::string, std::string> configs;
3862     OHOS::Global::I18n::LocaleInfo locale(localeInfo, configs);
3863     resConfig->SetLocaleInfo(locale.GetLanguage().c_str(), locale.GetScript().c_str(), locale.GetRegion().c_str());
3864 #endif
3865     resourceManager->UpdateResConfig(*resConfig);
3866     return resourceManager;
3867 }
3868 #endif
3869 
3870 #ifdef BUNDLE_FRAMEWORK_FREE_INSTALL
GetRemovableBundleNameVec(std::map<std::string,int> & bundlenameAndUids)3871 bool BundleDataMgr::GetRemovableBundleNameVec(std::map<std::string, int>& bundlenameAndUids)
3872 {
3873     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3874     if (bundleInfos_.empty()) {
3875         APP_LOGE("bundleInfos_ is data is empty.");
3876         return false;
3877     }
3878     for (auto &it : bundleInfos_) {
3879         APP_LOGD("bundleName: %{public}s", it.first.c_str());
3880         int32_t userId = AccountHelper::GetCurrentActiveUserId();
3881         APP_LOGD("bundle userId= %{public}d", userId);
3882         if (!it.second.HasInnerBundleUserInfo(userId)) {
3883             continue;
3884         }
3885         if (it.second.IsBundleRemovable(userId)) {
3886             bundlenameAndUids.emplace(it.first, it.second.GetUid(userId));
3887         }
3888     }
3889     return true;
3890 }
3891 #endif
3892 
QueryAllDeviceIds(std::vector<std::string> & deviceIds)3893 bool BundleDataMgr::QueryAllDeviceIds(std::vector<std::string> &deviceIds)
3894 {
3895 #ifdef DEVICE_MANAGER_ENABLE
3896     auto deviceManager = DelayedSingleton<BundleMgrService>::GetInstance()->GetDeviceManager();
3897     if (deviceManager == nullptr) {
3898         APP_LOGE("deviceManager is nullptr");
3899         return false;
3900     }
3901     return deviceManager->GetAllDeviceList(deviceIds);
3902 #else
3903     return true;
3904 #endif
3905 }
3906 
GetAllPreInstallBundleInfos()3907 const std::vector<PreInstallBundleInfo>& BundleDataMgr::GetAllPreInstallBundleInfos()
3908 {
3909     std::lock_guard<std::mutex> lock(preInstallInfoMutex_);
3910     return preInstallBundleInfos_;
3911 }
3912 
ImplicitQueryInfoByPriority(const Want & want,int32_t flags,int32_t userId,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo)3913 bool BundleDataMgr::ImplicitQueryInfoByPriority(const Want &want, int32_t flags, int32_t userId,
3914     AbilityInfo &abilityInfo, ExtensionAbilityInfo &extensionInfo)
3915 {
3916     int32_t requestUserId = GetUserId(userId);
3917     if (requestUserId == Constants::INVALID_USERID) {
3918         APP_LOGE("invalid userId");
3919         return false;
3920     }
3921     std::vector<AbilityInfo> abilityInfos;
3922     bool abilityValid =
3923         ImplicitQueryAbilityInfos(want, flags, requestUserId, abilityInfos) && (abilityInfos.size() > 0);
3924     std::vector<ExtensionAbilityInfo> extensionInfos;
3925     bool extensionValid =
3926         ImplicitQueryExtensionInfos(want, flags, requestUserId, extensionInfos) && (extensionInfos.size() > 0);
3927     if (!abilityValid && !extensionValid) {
3928         // both invalid
3929         APP_LOGE("can't find target AbilityInfo or ExtensionAbilityInfo");
3930         return false;
3931     }
3932     if (abilityValid && extensionValid) {
3933         // both valid
3934         if (abilityInfos[0].priority >= extensionInfos[0].priority) {
3935             APP_LOGD("find target AbilityInfo with higher priority, name : %{public}s", abilityInfos[0].name.c_str());
3936             abilityInfo = abilityInfos[0];
3937         } else {
3938             APP_LOGD("find target ExtensionAbilityInfo with higher priority, name : %{public}s",
3939                 extensionInfos[0].name.c_str());
3940             extensionInfo = extensionInfos[0];
3941         }
3942     } else if (abilityValid) {
3943         // only ability valid
3944         APP_LOGD("find target AbilityInfo, name : %{public}s", abilityInfos[0].name.c_str());
3945         abilityInfo = abilityInfos[0];
3946     } else {
3947         // only extension valid
3948         APP_LOGD("find target ExtensionAbilityInfo, name : %{public}s", extensionInfos[0].name.c_str());
3949         extensionInfo = extensionInfos[0];
3950     }
3951     return true;
3952 }
3953 
ImplicitQueryInfos(const Want & want,int32_t flags,int32_t userId,std::vector<AbilityInfo> & abilityInfos,std::vector<ExtensionAbilityInfo> & extensionInfos)3954 bool BundleDataMgr::ImplicitQueryInfos(const Want &want, int32_t flags, int32_t userId,
3955     std::vector<AbilityInfo> &abilityInfos, std::vector<ExtensionAbilityInfo> &extensionInfos)
3956 {
3957     // step1 : find default infos
3958 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
3959     std::string type = want.GetType();
3960     APP_LOGD("type : %{public}s", type.c_str());
3961     BundleInfo bundleInfo;
3962     ErrCode ret = DefaultAppMgr::GetInstance().GetDefaultApplication(userId, type, bundleInfo);
3963     if (ret == ERR_OK) {
3964         if (bundleInfo.abilityInfos.size() == 1) {
3965             abilityInfos = bundleInfo.abilityInfos;
3966             APP_LOGD("find default ability.");
3967             return true;
3968         } else if (bundleInfo.extensionInfos.size() == 1) {
3969             extensionInfos = bundleInfo.extensionInfos;
3970             APP_LOGD("find default extension.");
3971             return true;
3972         } else {
3973             APP_LOGD("GetDefaultApplication failed.");
3974         }
3975     }
3976 #endif
3977     // step2 : implicit query infos
3978     bool abilityRet =
3979         ImplicitQueryAbilityInfos(want, flags, userId, abilityInfos) && (abilityInfos.size() > 0);
3980     APP_LOGD("abilityRet: %{public}d, abilityInfos size: %{public}zu", abilityRet, abilityInfos.size());
3981 
3982     bool extensionRet =
3983         ImplicitQueryExtensionInfos(want, flags, userId, extensionInfos) && (extensionInfos.size() > 0);
3984     APP_LOGD("extensionRet: %{public}d, extensionInfos size: %{public}zu", extensionRet, extensionInfos.size());
3985     return abilityRet || extensionRet;
3986 }
3987 
GetAllDependentModuleNames(const std::string & bundleName,const std::string & moduleName,std::vector<std::string> & dependentModuleNames)3988 bool BundleDataMgr::GetAllDependentModuleNames(const std::string &bundleName, const std::string &moduleName,
3989     std::vector<std::string> &dependentModuleNames)
3990 {
3991     APP_LOGD("GetAllDependentModuleNames bundleName: %{public}s, moduleName: %{public}s",
3992         bundleName.c_str(), moduleName.c_str());
3993     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
3994     auto item = bundleInfos_.find(bundleName);
3995     if (item == bundleInfos_.end()) {
3996         APP_LOGE("GetAllDependentModuleNames: bundleName not find");
3997         return false;
3998     }
3999     const InnerBundleInfo &innerBundleInfo = item->second;
4000     return innerBundleInfo.GetAllDependentModuleNames(moduleName, dependentModuleNames);
4001 }
4002 
SetDisposedStatus(const std::string & bundleName,int32_t status)4003 bool BundleDataMgr::SetDisposedStatus(const std::string &bundleName, int32_t status)
4004 {
4005     APP_LOGD("SetDisposedStatus: bundleName: %{public}s, status: %{public}d", bundleName.c_str(), status);
4006     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
4007     auto item = bundleInfos_.find(bundleName);
4008     if (item == bundleInfos_.end()) {
4009         APP_LOGE("SetDisposedStatus: bundleName: %{public}s not find", bundleName.c_str());
4010         return false;
4011     }
4012     auto& info = bundleInfos_.at(bundleName);
4013     info.SetDisposedStatus(status);
4014     if (!dataStorage_->SaveStorageBundleInfo(info)) {
4015         APP_LOGE("update storage failed bundle:%{public}s", bundleName.c_str());
4016         return false;
4017     }
4018     return true;
4019 }
4020 
UpdateRemovable(const std::string & bundleName,bool removable)4021 void BundleDataMgr::UpdateRemovable(
4022     const std::string &bundleName, bool removable)
4023 {
4024     APP_LOGD("UpdateRemovable %{public}s", bundleName.c_str());
4025     if (bundleName.empty()) {
4026         APP_LOGE("bundleName is empty");
4027         return;
4028     }
4029 
4030     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
4031     auto infoItem = bundleInfos_.find(bundleName);
4032     if (infoItem == bundleInfos_.end()) {
4033         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
4034         return;
4035     }
4036 
4037     if (infoItem->second.IsRemovable() != removable) {
4038         infoItem->second.UpdateRemovable(true, removable);
4039         SaveInnerBundleInfo(infoItem->second);
4040     }
4041 }
4042 
UpdatePrivilegeCapability(const std::string & bundleName,const ApplicationInfo & appInfo)4043 void BundleDataMgr::UpdatePrivilegeCapability(
4044     const std::string &bundleName, const ApplicationInfo &appInfo)
4045 {
4046     APP_LOGD("UpdatePrivilegeCapability %{public}s", bundleName.c_str());
4047     if (bundleName.empty()) {
4048         APP_LOGE("bundleName is empty");
4049         return;
4050     }
4051 
4052     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
4053     auto infoItem = bundleInfos_.find(bundleName);
4054     if (infoItem == bundleInfos_.end()) {
4055         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
4056         return;
4057     }
4058 
4059     infoItem->second.UpdatePrivilegeCapability(appInfo);
4060 }
4061 
FetchInnerBundleInfo(const std::string & bundleName,InnerBundleInfo & innerBundleInfo)4062 bool BundleDataMgr::FetchInnerBundleInfo(
4063     const std::string &bundleName, InnerBundleInfo &innerBundleInfo)
4064 {
4065     APP_LOGD("FetchInnerBundleInfo %{public}s", bundleName.c_str());
4066     if (bundleName.empty()) {
4067         APP_LOGE("bundleName is empty");
4068         return false;
4069     }
4070 
4071     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
4072     auto infoItem = bundleInfos_.find(bundleName);
4073     if (infoItem == bundleInfos_.end()) {
4074         APP_LOGE("can not find bundle %{public}s", bundleName.c_str());
4075         return false;
4076     }
4077 
4078     innerBundleInfo = infoItem->second;
4079     return true;
4080 }
4081 
GetDisposedStatus(const std::string & bundleName)4082 int32_t BundleDataMgr::GetDisposedStatus(const std::string &bundleName)
4083 {
4084     APP_LOGD("GetDisposedStatus: bundleName: %{public}s", bundleName.c_str());
4085     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
4086     auto item = bundleInfos_.find(bundleName);
4087     if (item == bundleInfos_.end()) {
4088         APP_LOGE("GetDisposedStatus: bundleName: %{public}s not find", bundleName.c_str());
4089         return Constants::DEFAULT_DISPOSED_STATUS;
4090     }
4091     auto& info = bundleInfos_.at(bundleName);
4092     return info.GetDisposedStatus();
4093 }
4094 
4095 #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP
QueryInfoAndSkillsByElement(int32_t userId,const Element & element,AbilityInfo & abilityInfo,ExtensionAbilityInfo & extensionInfo,std::vector<Skill> & skills) const4096 bool BundleDataMgr::QueryInfoAndSkillsByElement(int32_t userId, const Element& element,
4097     AbilityInfo& abilityInfo, ExtensionAbilityInfo& extensionInfo, std::vector<Skill>& skills) const
4098 {
4099     APP_LOGD("begin to QueryInfoAndSkillsByElement.");
4100     const std::string& bundleName = element.bundleName;
4101     const std::string& moduleName = element.moduleName;
4102     const std::string& abilityName = element.abilityName;
4103     const std::string& extensionName = element.extensionName;
4104     Want want;
4105     ElementName elementName("", bundleName, abilityName, moduleName);
4106     want.SetElement(elementName);
4107     bool isAbility = !element.abilityName.empty();
4108     bool ret = false;
4109     if (isAbility) {
4110         // get ability info
4111         ret = ExplicitQueryAbilityInfo(want, GET_ABILITY_INFO_DEFAULT, userId, abilityInfo);
4112         if (!ret) {
4113             APP_LOGE("ExplicitQueryAbilityInfo failed.");
4114             return false;
4115         }
4116     } else {
4117         // get extension info
4118         elementName.SetAbilityName(extensionName);
4119         want.SetElement(elementName);
4120         ret = ExplicitQueryExtensionInfo(want, GET_EXTENSION_INFO_DEFAULT, userId, extensionInfo);
4121         if (!ret) {
4122             APP_LOGE("ExplicitQueryExtensionInfo failed.");
4123             return false;
4124         }
4125     }
4126 
4127     // get skills info
4128     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
4129     if (bundleInfos_.empty()) {
4130         APP_LOGE("bundleInfos_ is empty.");
4131         return false;
4132     }
4133     auto item = bundleInfos_.find(bundleName);
4134     if (item == bundleInfos_.end()) {
4135         APP_LOGE("can't find bundleName : %{public}s.", bundleName.c_str());
4136         return false;
4137     }
4138     const InnerBundleInfo& innerBundleInfo = item->second;
4139     if (isAbility) {
4140         std::string key;
4141         key.append(bundleName).append(".").append(abilityInfo.package).append(".").append(abilityName);
4142         APP_LOGD("begin to find ability skills, key : %{public}s.", key.c_str());
4143         for (const auto& infoItem : innerBundleInfo.GetInnerSkillInfos()) {
4144             if (infoItem.first == key) {
4145                 skills = infoItem.second;
4146                 APP_LOGD("find ability skills success.");
4147                 break;
4148             }
4149         }
4150     } else {
4151         std::string key;
4152         key.append(bundleName).append(".").append(moduleName).append(".").append(extensionName);
4153         APP_LOGD("begin to find extension skills, key : %{public}s.", key.c_str());
4154         for (const auto& infoItem : innerBundleInfo.GetExtensionSkillInfos()) {
4155             if (infoItem.first == key) {
4156                 skills = infoItem.second;
4157                 APP_LOGD("find extension skills success.");
4158                 break;
4159             }
4160         }
4161     }
4162     APP_LOGD("QueryInfoAndSkillsByElement success.");
4163     return true;
4164 }
4165 
GetElement(int32_t userId,const ElementName & elementName,Element & element) const4166 bool BundleDataMgr::GetElement(int32_t userId, const ElementName& elementName, Element& element) const
4167 {
4168     APP_LOGD("begin to GetElement.");
4169     const std::string& bundleName = elementName.GetBundleName();
4170     const std::string& moduleName = elementName.GetModuleName();
4171     const std::string& abilityName = elementName.GetAbilityName();
4172     if (bundleName.empty() || moduleName.empty() || abilityName.empty()) {
4173         APP_LOGE("bundleName or moduleName or abilityName is empty.");
4174         return false;
4175     }
4176     Want want;
4177     want.SetElement(elementName);
4178     AbilityInfo abilityInfo;
4179     bool ret = ExplicitQueryAbilityInfo(want, GET_ABILITY_INFO_DEFAULT, userId, abilityInfo);
4180     if (ret) {
4181         APP_LOGD("ElementName is ability.");
4182         element.bundleName = bundleName;
4183         element.moduleName = moduleName;
4184         element.abilityName = abilityName;
4185         return true;
4186     }
4187 
4188     ExtensionAbilityInfo extensionInfo;
4189     ret = ExplicitQueryExtensionInfo(want, GET_EXTENSION_INFO_DEFAULT, userId, extensionInfo);
4190     if (ret) {
4191         APP_LOGD("ElementName is extension.");
4192         element.bundleName = bundleName;
4193         element.moduleName = moduleName;
4194         element.extensionName = abilityName;
4195         return true;
4196     }
4197 
4198     APP_LOGE("ElementName doesn't exist.");
4199     return false;
4200 }
4201 #endif
4202 
GetMediaData(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,std::unique_ptr<uint8_t[]> & mediaDataPtr,size_t & len,int32_t userId) const4203 ErrCode BundleDataMgr::GetMediaData(const std::string &bundleName, const std::string &moduleName,
4204     const std::string &abilityName, std::unique_ptr<uint8_t[]> &mediaDataPtr, size_t &len, int32_t userId) const
4205 {
4206     APP_LOGI("begin to GetMediaData.");
4207 #ifdef GLOBAL_RESMGR_ENABLE
4208     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
4209     InnerBundleInfo innerBundleInfo;
4210     ErrCode errCode = GetInnerBundleInfoWithFlagsV9(
4211         bundleName, BundleFlag::GET_BUNDLE_DEFAULT, innerBundleInfo, GetUserId(userId));
4212     if (errCode != ERR_OK) {
4213         return errCode;
4214     }
4215     AbilityInfo abilityInfo;
4216     if (moduleName.empty()) {
4217         auto ability = innerBundleInfo.FindAbilityInfoV9(bundleName, moduleName, abilityName);
4218         if (!ability) {
4219             return ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST;
4220         }
4221         abilityInfo = *ability;
4222     } else {
4223         errCode = innerBundleInfo.FindAbilityInfo(bundleName, moduleName, abilityName, abilityInfo);
4224         if (errCode != ERR_OK) {
4225             APP_LOGE("%{public}s:FindAbilityInfo failed: %{public}d", bundleName.c_str(), errCode);
4226             return errCode;
4227         }
4228     }
4229     bool isEnable;
4230     errCode = innerBundleInfo.IsAbilityEnabledV9(abilityInfo, GetUserId(userId), isEnable);
4231     if (errCode != ERR_OK) {
4232         return errCode;
4233     }
4234     if (!isEnable) {
4235         APP_LOGE("%{public}s ability disabled: %{public}s", bundleName.c_str(), abilityName.c_str());
4236         return ERR_BUNDLE_MANAGER_ABILITY_DISABLED;
4237     }
4238     std::shared_ptr<Global::Resource::ResourceManager> resourceManager =
4239         GetResourceManager(bundleName, abilityInfo.moduleName, GetUserId(userId));
4240     if (resourceManager == nullptr) {
4241         APP_LOGE("InitResourceManager failed");
4242         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4243     }
4244     OHOS::Global::Resource::RState ret =
4245         resourceManager->GetMediaDataById(static_cast<uint32_t>(abilityInfo.iconId), len, mediaDataPtr);
4246     if (ret != OHOS::Global::Resource::RState::SUCCESS || mediaDataPtr == nullptr || len == 0) {
4247         APP_LOGE("GetMediaDataById failed");
4248         return ERR_BUNDLE_MANAGER_INTERNAL_ERROR;
4249     }
4250     return ERR_OK;
4251 #else
4252     APP_LOGW("GLOBAL_RES_MGR_ENABLE is false");
4253     return ERR_BUNDLE_MANAGER_GLOBAL_RES_MGR_ENABLE_DISABLED;
4254 #endif
4255 }
4256 
GetStatusCallbackMutex()4257 std::shared_mutex &BundleDataMgr::GetStatusCallbackMutex()
4258 {
4259     return callbackMutex_;
4260 }
4261 
GetCallBackList() const4262 std::vector<sptr<IBundleStatusCallback>> BundleDataMgr::GetCallBackList() const
4263 {
4264     return callbackList_;
4265 }
4266 
UpdateQuickFixInnerBundleInfo(const std::string & bundleName,const InnerBundleInfo & innerBundleInfo)4267 bool BundleDataMgr::UpdateQuickFixInnerBundleInfo(const std::string &bundleName,
4268     const InnerBundleInfo &innerBundleInfo)
4269 {
4270     APP_LOGD("to update info:%{public}s", bundleName.c_str());
4271     if (bundleName.empty()) {
4272         APP_LOGE("update info fail, empty bundle name");
4273         return false;
4274     }
4275 
4276     std::lock_guard<std::mutex> lock(bundleInfoMutex_);
4277     auto infoItem = bundleInfos_.find(bundleName);
4278     if (infoItem == bundleInfos_.end()) {
4279         APP_LOGE("bundle info is not existed");
4280         return false;
4281     }
4282 
4283     if (dataStorage_->SaveStorageBundleInfo(innerBundleInfo)) {
4284         bundleInfos_.at(bundleName) = innerBundleInfo;
4285         return true;
4286     }
4287     APP_LOGE("to update info:%{public}s failed", bundleName.c_str());
4288     return false;
4289 }
4290 
GetProvisionMetadata(const std::string & bundleName,int32_t userId,std::vector<Metadata> & provisionMetadatas) const4291 ErrCode BundleDataMgr::GetProvisionMetadata(const std::string &bundleName, int32_t userId,
4292     std::vector<Metadata> &provisionMetadatas) const
4293 {
4294     // Reserved interface
4295     return ERR_OK;
4296 }
4297 }  // namespace AppExecFwk
4298 }  // namespace OHOS
4299