diff --git a/etc/init/edm.cfg b/etc/init/edm.cfg index 91549d1aa5b2d3ca4fb08bea35eb290a8b9374eb..196f0995bb1342d7b6e56f341d8c9b6dcfcbe3fb 100644 --- a/etc/init/edm.cfg +++ b/etc/init/edm.cfg @@ -55,6 +55,7 @@ "ohos.permission.FACTORY_RESET", "ohos.permission.GET_BUNDLE_INFO", "ohos.permission.GET_BUNDLE_INFO_PRIVILEGED", + "ohos.permission.GET_BUNDLE_RESOURCES", "ohos.permission.GET_INSTALLED_BUNDLE_LIST", "ohos.permission.GET_NETWORK_INFO", "ohos.permission.GET_TELEPHONY_STATE", diff --git a/services/edm/include/query_policy/installed_bundle_info_list_query.h b/services/edm/include/query_policy/installed_bundle_info_list_query.h index 94e9396e8ba3017e081daa79afe8d3bd5732dcc5..49e53c0e3d7eb033c0d533a630a9a910518fbfb4 100644 --- a/services/edm/include/query_policy/installed_bundle_info_list_query.h +++ b/services/edm/include/query_policy/installed_bundle_info_list_query.h @@ -17,6 +17,7 @@ #define SERVICES_EDM_INCLUDE_QUERY_INSTALLED_BUNDLE_INFO_LIST_QUERY_H #include "bundle_info.h" +#include "bundle_resource_info.h" #include "edm_bundle_info.h" #include "ipolicy_query.h" @@ -41,6 +42,10 @@ private: bool ConvertResourceInfo(OHOS::AppExecFwk::Resource &resource, EdmResource &edmResource); bool WriteVectorToParcelIntelligent(std::vector &parcelableVector, MessageParcel &reply); bool WriteParcelableIntoAshmem(MessageParcel &tempParcel, MessageParcel &reply); + bool GetBundleInfosData(std::vector &bundleInfos, + std::vector &bundleResourceInfos, int32_t userId); + void AssembleBundleResourceInfo(std::vector &edmBundleInfos, + std::vector &bundleResourceInfos); int32_t AllocatAshmemNum(); std::mutex bundleAshmemMutex_; diff --git a/services/edm/src/query_policy/installed_bundle_info_list_query.cpp b/services/edm/src/query_policy/installed_bundle_info_list_query.cpp index b7c1c1338d5e27230e92cbd1d83e309715dffa78..f79430f88b6ff7db48eb169fa6b34717d04bd92b 100644 --- a/services/edm/src/query_policy/installed_bundle_info_list_query.cpp +++ b/services/edm/src/query_policy/installed_bundle_info_list_query.cpp @@ -21,6 +21,7 @@ #include "bundle_info.h" #include "bundle_mgr_proxy.h" +#include "bundle_resource_proxy.h" #include "edm_errors.h" #include "edm_log.h" #include "edm_sys_manager.h" @@ -44,24 +45,15 @@ ErrCode InstalledBundleInfoListQuery::QueryPolicy(std::string &policyData, Messa int32_t userId) { EDMLOGI("InstalledBundleInfoListQuery QueryPolicy"); - auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - sptr proxy = iface_cast(remoteObject); - if (!proxy) { - EDMLOGE("InstalledBundleInfoListQuery QueryPolicy GetAppControlProxy failed."); - return EdmReturnErrCode::SYSTEM_ABNORMALLY; - } - - const int32_t bundleFlags = - static_cast(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) | - static_cast(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO); std::vector bundleInfos; - ErrCode getResult = proxy->GetBundleInfosV9(bundleFlags, bundleInfos, userId); - if (FAILED(getResult)) { - EDMLOGE("InstalledBundleInfoListQuery QueryPolicy getResult failed."); + std::vector bundleResourceInfos; + bool queryResult = GetBundleInfosData(bundleInfos, bundleResourceInfos, userId); + if (!queryResult) { + EDMLOGE("InstalledBundleInfoListQuery QueryPolicy from bms failed."); return EdmReturnErrCode::SYSTEM_ABNORMALLY; } - if (bundleInfos.empty()) { - EDMLOGI("InstalledBundleInfoListQuery QueryPolicy getResult empty."); + if (bundleInfos.empty() || bundleResourceInfos.empty()) { + EDMLOGI("InstalledBundleInfoListQuery QueryPolicy queryResult empty."); return ERR_OK; } std::vector edmBundleInfos; @@ -74,6 +66,7 @@ ErrCode InstalledBundleInfoListQuery::QueryPolicy(std::string &policyData, Messa } edmBundleInfos.emplace_back(edmBundleInfo); } + AssembleBundleResourceInfo(edmBundleInfos, bundleResourceInfos); bool writeRet = WriteVectorToParcelIntelligent(edmBundleInfos, reply); if (!writeRet) { EDMLOGE("InstalledBundleInfoListQuery QueryPolicy WriteVectorToParcelIntelligent failed"); @@ -82,6 +75,38 @@ ErrCode InstalledBundleInfoListQuery::QueryPolicy(std::string &policyData, Messa return ERR_OK; } +bool InstalledBundleInfoListQuery::GetBundleInfosData(std::vector &bundleInfos, + std::vector &bundleResourceInfos, int32_t userId) +{ + auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + sptr proxy = iface_cast(remoteObject); + if (!proxy) { + EDMLOGE("InstalledBundleInfoListQuery QueryPolicy getBundleMgr failed."); + return false; + } + int32_t bundleFlags = + static_cast(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION) | + static_cast(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO); + ErrCode getResult = proxy->GetBundleInfosV9(bundleFlags, bundleInfos, userId); + if (FAILED(getResult)) { + EDMLOGE("InstalledBundleInfoListQuery QueryPolicy getResult failed."); + return false; + } + auto bundleResourceProxy = proxy->GetBundleResourceProxy(); + if (!bundleResourceProxy) { + EDMLOGE("InstalledBundleInfoListQuery QueryPolicy GetBundleResourceProxy failed."); + return false; + } + uint32_t bundleResourceFlags = + static_cast(OHOS::AppExecFwk::ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL); + ErrCode resourceResult = bundleResourceProxy->GetAllBundleResourceInfo(bundleResourceFlags, bundleResourceInfos); + if (FAILED(resourceResult)) { + EDMLOGE("InstalledBundleInfoListQuery QueryPolicy resourceResult failed."); + return false; + } + return true; +} + bool InstalledBundleInfoListQuery::ConvertBundleInfoList(OHOS::AppExecFwk::BundleInfo &bundleInfo, EdmBundleInfo &edmBundleInfo) { @@ -246,5 +271,21 @@ int32_t InstalledBundleInfoListQuery::AllocatAshmemNum() return ashmemNum_++; } +void InstalledBundleInfoListQuery::AssembleBundleResourceInfo(std::vector &edmBundleInfos, + std::vector &bundleResourceInfos) +{ + std::unordered_map labelMap; + for (const auto &item : bundleResourceInfos) { + labelMap[item.bundleName] = item.label; + } + for (auto &item: edmBundleInfos) { + if (labelMap.find(item.applicationInfo.name) != labelMap.end()) { + item.applicationInfo.label = labelMap[item.applicationInfo.name]; + } else { + EDMLOGW("not find label info %{public}s", item.applicationInfo.name.c_str()); + } + } +} + } // namespace EDM } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/services/edm/src/installed_bundle_info_list_query_test.cpp b/test/unittest/services/edm/src/installed_bundle_info_list_query_test.cpp index c15c868925d83e5b34c430175a1705056043605f..19fcaf4fb9dfb9e2153af54cbe24562dcb5604ad 100644 --- a/test/unittest/services/edm/src/installed_bundle_info_list_query_test.cpp +++ b/test/unittest/services/edm/src/installed_bundle_info_list_query_test.cpp @@ -179,6 +179,95 @@ HWTEST_F(InstalledBundleInfoListQueryTest, TestWriteVectorToParcelIntelligent, T int32_t size = 0; ASSERT_TRUE(reply.ReadInt32(size) && (size != 0)); } + +/** + * @tc.name: TestAssembleBundleResourceInfoNormal + * @tc.desc: Test InstalledBundleInfoListQuery::AssembleBundleResourceInfo func. + * @tc.type: FUNC + */ +HWTEST_F(InstalledBundleInfoListQueryTest, TestAssembleBundleResourceInfoNormal, TestSize.Level1) +{ + std::string commonNameA = "nameA"; + std::string commonNameB = "nameB"; + std::string commonResourceLableA = "resourceInfoLableA"; + std::string commonResourceLableB = "resourceInfoLableB"; + EdmBundleInfo edmBundleInfoA; + edmBundleInfoA.applicationInfo.name = commonNameA; + edmBundleInfoA.applicationInfo.label = ""; + + EdmBundleInfo edmBundleInfoB; + edmBundleInfoB.applicationInfo.name = commonNameB; + edmBundleInfoB.applicationInfo.label = ""; + + std::vector edmBundleInfos; + edmBundleInfos.emplace_back(edmBundleInfoA); + edmBundleInfos.emplace_back(edmBundleInfoB); + + std::vector bundleResourceInfos; + OHOS::AppExecFwk::BundleResourceInfo bundleResourceInfoA; + bundleResourceInfoA.bundleName = commonNameA; + bundleResourceInfoA.label = commonResourceLableA; + OHOS::AppExecFwk::BundleResourceInfo bundleResourceInfoB; + bundleResourceInfoB.bundleName = commonNameB; + bundleResourceInfoB.label = commonResourceLableB; + bundleResourceInfos.emplace_back(bundleResourceInfoA); + bundleResourceInfos.emplace_back(bundleResourceInfoB); + + std::shared_ptr queryObj = std::make_shared(); + queryObj->AssembleBundleResourceInfo(edmBundleInfos, bundleResourceInfos); + ASSERT_EQ(edmBundleInfos[0].applicationInfo.label, commonResourceLableA); + ASSERT_EQ(edmBundleInfos[1].applicationInfo.label, commonResourceLableB); +} + +/** + * @tc.name: TestAssembleBundleResourceInfoNotMatch + * @tc.desc: Test InstalledBundleInfoListQuery::AssembleBundleResourceInfo func. + * @tc.type: FUNC + */ +HWTEST_F(InstalledBundleInfoListQueryTest, TestAssembleBundleResourceInfoNotMatch, TestSize.Level1) +{ + std::string commonNameA = "nameA"; + std::string commonNameB = "nameB"; + std::string commonResourceLableA = "resourceInfoLableA"; + std::string commonResourceLableB = "resourceInfoLableB"; + EdmBundleInfo edmBundleInfoA; + edmBundleInfoA.applicationInfo.name = commonNameA; + edmBundleInfoA.applicationInfo.label = ""; + + EdmBundleInfo edmBundleInfoB; + edmBundleInfoB.applicationInfo.name = commonNameB; + edmBundleInfoB.applicationInfo.label = ""; + + std::vector edmBundleInfos; + edmBundleInfos.emplace_back(edmBundleInfoA); + edmBundleInfos.emplace_back(edmBundleInfoB); + + + std::vector bundleResourceInfos; + OHOS::AppExecFwk::BundleResourceInfo bundleResourceInfoA; + bundleResourceInfoA.bundleName = "nameC"; + bundleResourceInfoA.label = commonResourceLableA; + bundleResourceInfos.emplace_back(bundleResourceInfoA); + + std::shared_ptr queryObj = std::make_shared(); + queryObj->AssembleBundleResourceInfo(edmBundleInfos, bundleResourceInfos); + ASSERT_EQ(edmBundleInfos[0].applicationInfo.label, ""); +} + +/** + * @tc.name: TestAssembleBundleResourceInfoEmpty + * @tc.desc: Test InstalledBundleInfoListQuery::AssembleBundleResourceInfo func. + * @tc.type: FUNC + */ +HWTEST_F(InstalledBundleInfoListQueryTest, TestAssembleBundleResourceInfoEmpty, TestSize.Level1) +{ + std::vector edmBundleInfos; + std::vector bundleResourceInfos; + + std::shared_ptr queryObj = std::make_shared(); + queryObj->AssembleBundleResourceInfo(edmBundleInfos, bundleResourceInfos); + ASSERT_TRUE(edmBundleInfos.empty()); +} } // namespace TEST } // namespace EDM } // namespace OHOS