diff --git a/services/bundlemgr/include/bundle_data_mgr.h b/services/bundlemgr/include/bundle_data_mgr.h index 3f4959512..5be781c02 100644 --- a/services/bundlemgr/include/bundle_data_mgr.h +++ b/services/bundlemgr/include/bundle_data_mgr.h @@ -847,6 +847,7 @@ public: std::string &profile) const; bool GetFingerprints(const std::string &bundleName, std::vector &fingerPrints) const; ErrCode GetInnerBundleInfoByUid(const int uid, InnerBundleInfo &innerBundleInfo) const; + std::string GetModuleNameByBundleAndAbility(const std::string& bundleName, const std::string& abilityName); private: /** diff --git a/services/bundlemgr/src/bundle_data_mgr.cpp b/services/bundlemgr/src/bundle_data_mgr.cpp index 3b054304f..a2f6a6087 100644 --- a/services/bundlemgr/src/bundle_data_mgr.cpp +++ b/services/bundlemgr/src/bundle_data_mgr.cpp @@ -5733,5 +5733,27 @@ bool BundleDataMgr::IsUpdateInnerBundleInfoSatisified(const InnerBundleInfo &old { return !oldInfo.HasEntry() || oldInfo.GetEntryInstallationFree() || newInfo.HasEntry(); } + +std::string BundleDataMgr::GetModuleNameByBundleAndAbility( + const std::string& bundleName, const std::string& abilityName) +{ + if (bundleName.empty() || abilityName.empty()) { + APP_LOGE("bundleName or abilityName is empty"); + return std::string(); + } + std::shared_lock lock(bundleInfoMutex_); + auto innerBundleInfo = bundleInfos_.find(bundleName); + if (innerBundleInfo == bundleInfos_.end()) { + APP_LOGE("can not find bundle %{public}s.", bundleName.c_str()); + return std::string(); + } + auto abilityInfo = innerBundleInfo->second.FindAbilityInfoV9(Constants::EMPTY_STRING, abilityName); + if (!abilityInfo) { + APP_LOGE("bundleName:%{public}s, abilityName:%{public}s can find moduleName", + bundleName.c_str(), abilityName.c_str()); + return std::string(); + } + return abilityInfo->moduleName; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/src/bundle_mgr_host_impl.cpp b/services/bundlemgr/src/bundle_mgr_host_impl.cpp index 5bf39ad5e..49692b485 100644 --- a/services/bundlemgr/src/bundle_mgr_host_impl.cpp +++ b/services/bundlemgr/src/bundle_mgr_host_impl.cpp @@ -1640,10 +1640,7 @@ ErrCode BundleMgrHostImpl::SetAbilityEnabled(const AbilityInfo &abilityInfo, boo } std::string moduleName = abilityInfo.moduleName; if (moduleName.empty()) { - AbilityInfo info; - if (GetAbilityInfo(abilityInfo.bundleName, abilityInfo.name, info)) { - moduleName = info.moduleName; - } + moduleName = dataMgr->GetModuleNameByBundleAndAbility(abilityInfo.bundleName, abilityInfo.name); } BundleResourceHelper::SetAbilityEnabled(abilityInfo.bundleName, moduleName, abilityInfo.name, isEnabled, userId); EventReport::SendComponentStateSysEvent(abilityInfo.bundleName, abilityInfo.name, userId, isEnabled, false); diff --git a/services/bundlemgr/src/bundle_resource/bundle_resource_callback.cpp b/services/bundlemgr/src/bundle_resource/bundle_resource_callback.cpp index f30a52b30..e4c80184e 100644 --- a/services/bundlemgr/src/bundle_resource/bundle_resource_callback.cpp +++ b/services/bundlemgr/src/bundle_resource/bundle_resource_callback.cpp @@ -118,10 +118,10 @@ bool BundleResourceCallback::OnBundleStatusChanged( APP_LOGE("bundleName is empty"); return false; } - if (userId != Constants::START_USERID) { + if (userId != Constants::DEFAULT_USERID) { int32_t currentUserId = AccountHelper::GetCurrentActiveUserId(); - if (currentUserId != userId) { - APP_LOGE("userId: %{public}d, currentUserId :%{public}d", userId, currentUserId); + if ((currentUserId > 0) && (currentUserId != userId)) { + APP_LOGE("userId: %{public}d, currentUserId :%{public}d not same", userId, currentUserId); return false; } } @@ -154,11 +154,10 @@ bool BundleResourceCallback::OnAbilityStatusChanged(const std::string &bundleNam APP_LOGE("bundleName or moduleName or abilityName is empty"); return false; } - - if (userId != Constants::START_USERID) { + if (userId != Constants::DEFAULT_USERID) { int32_t currentUserId = AccountHelper::GetCurrentActiveUserId(); - if (currentUserId != userId) { - APP_LOGE("wtt userId: %{public}d, currentUserId :%{public}d not same", userId, currentUserId); + if ((currentUserId > 0) && (currentUserId != userId)) { + APP_LOGE("userId: %{public}d, currentUserId :%{public}d not same", userId, currentUserId); return false; } } diff --git a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp index 740c1b2e7..1955965aa 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_data_mgr_test.cpp @@ -3681,6 +3681,33 @@ HWTEST_F(BmsBundleDataMgrTest, ExplicitQueryAbilityInfoV9_0300, Function | Small EXPECT_EQ(testRet, ERR_BUNDLE_MANAGER_ABILITY_NOT_EXIST); } +/** + * @tc.number: GetModuleNameByBundleAndAbility_0100 + * @tc.name: GetModuleNameByBundleAndAbility + * @tc.desc: GetModuleNameByBundleAndAbility + */ +HWTEST_F(BmsBundleDataMgrTest, GetModuleNameByBundleAndAbility_0100, Function | SmallTest | Level0) +{ + std::string moduleName = GetBundleDataMgr()->GetModuleNameByBundleAndAbility("", ABILITY_NAME_TEST); + EXPECT_TRUE(moduleName.empty()); + + moduleName = GetBundleDataMgr()->GetModuleNameByBundleAndAbility(BUNDLE_NAME_TEST, ""); + EXPECT_TRUE(moduleName.empty()); + + moduleName = GetBundleDataMgr()->GetModuleNameByBundleAndAbility(BUNDLE_NAME_TEST, ABILITY_NAME_TEST); + EXPECT_TRUE(moduleName.empty()); + + MockInstallBundle(BUNDLE_NAME_TEST, MODULE_NAME_TEST, ABILITY_NAME_TEST); + + moduleName = GetBundleDataMgr()->GetModuleNameByBundleAndAbility(BUNDLE_NAME_TEST, MODULE_NAME_TEST); + EXPECT_TRUE(moduleName.empty()); + + moduleName = GetBundleDataMgr()->GetModuleNameByBundleAndAbility(BUNDLE_NAME_TEST, ABILITY_NAME_TEST); + EXPECT_EQ(moduleName, MODULE_NAME_TEST); + + MockUninstallBundle(BUNDLE_NAME_TEST); +} + /** * @tc.number: FilterAbilityInfosByModuleName_0100 * @tc.name: test FilterAbilityInfosByModuleName