diff --git a/interfaces/inner_api/appexecfwk_base/BUILD.gn b/interfaces/inner_api/appexecfwk_base/BUILD.gn index c9acf3d432770883c47f78b87a2ebdbcfe3bcf85..a12b1dce4c2cfb8b2e5cc9b80333840dd238a147 100644 --- a/interfaces/inner_api/appexecfwk_base/BUILD.gn +++ b/interfaces/inner_api/appexecfwk_base/BUILD.gn @@ -56,6 +56,7 @@ ohos_shared_library("appexecfwk_base") { "src/bundle_resource/bundle_resource_info.cpp", "src/bundle_resource/bundle_system_state.cpp", "src/bundle_resource/launcher_ability_resource_info.cpp", + "src/bundle_resource/bundle_resource_key.cpp", "src/bundle_storage_stats.cpp", "src/bundle_user_info.cpp", "src/clone/clone_param.cpp", diff --git a/interfaces/inner_api/appexecfwk_base/include/bundle_resource/bundle_resource_key.h b/interfaces/inner_api/appexecfwk_base/include/bundle_resource/bundle_resource_key.h new file mode 100644 index 0000000000000000000000000000000000000000..6e0785392010cb3880e682a9e274c16379158f47 --- /dev/null +++ b/interfaces/inner_api/appexecfwk_base/include/bundle_resource/bundle_resource_key.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_BUNDLEMANAGER_INTERFACES_INNERKITS_APPEXECFWK_BASE_INCLUDE_BUNDLE_RESOURCE_KEY_H +#define FOUNDATION_BUNDLEMANAGER_INTERFACES_INNERKITS_APPEXECFWK_BASE_INCLUDE_BUNDLE_RESOURCE_KEY_H + +#include + +#include "parcel.h" + +namespace OHOS { +namespace AppExecFwk { +struct BundleResourceKey : Parcelable { + int32_t appIndex = 0; + std::string bundleName; + std::string moduleName; + std::string abilityName; + + bool ReadFromParcel(Parcel &parcel); + virtual bool Marshalling(Parcel &parcel) const override; + static BundleResourceKey *Unmarshalling(Parcel &parcel); +}; +} // AppExecFwk +} // OHOS +#endif // FOUNDATION_BUNDLEMANAGER_INTERFACES_INNERKITS_APPEXECFWK_BASE_INCLUDE_BUNDLE_RESOURCE_KEY_H + \ No newline at end of file diff --git a/interfaces/inner_api/appexecfwk_base/src/bundle_resource/bundle_resource_key.cpp b/interfaces/inner_api/appexecfwk_base/src/bundle_resource/bundle_resource_key.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d401eeaf1ea1a52f36912c2c74a3aa9bb4525e0e --- /dev/null +++ b/interfaces/inner_api/appexecfwk_base/src/bundle_resource/bundle_resource_key.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bundle_resource_key.h" + +#include "app_log_wrapper.h" +#include "nlohmann/json.hpp" +#include "parcel_macro.h" +#include "string_ex.h" + +namespace OHOS { +namespace AppExecFwk { +bool BundleResourceKey::ReadFromParcel(Parcel &parcel) +{ + std::u16string bundleNameVal; + READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, bundleNameVal); + bundleName = Str16ToStr8(bundleNameVal); + + std::u16string moduleNameVal; + READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, moduleNameVal); + moduleName = Str16ToStr8(moduleNameVal); + + std::u16string abilityNameVal; + READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, abilityNameVal); + abilityName = Str16ToStr8(abilityNameVal); + + READ_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex); + return true; +} + +bool BundleResourceKey::Marshalling(Parcel &parcel) const +{ + WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(bundleName)); + WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(moduleName)); + WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(String16, parcel, Str8ToStr16(abilityName)); + WRITE_PARCEL_AND_RETURN_FALSE_IF_FAIL(Int32, parcel, appIndex); + return true; +} + +BundleResourceKey *BundleResourceKey::Unmarshalling(Parcel &parcel) +{ + BundleResourceKey *info = new (std::nothrow) BundleResourceKey(); + if ((info != nullptr) && !info->ReadFromParcel(parcel)) { + APP_LOGW("read from parcel failed"); + delete info; + info = nullptr; + } + return info; +} +} // AppExecFwk +} // OHOS + \ No newline at end of file diff --git a/interfaces/inner_api/appexecfwk_core/include/bundle_framework_core_ipc_interface_code.h b/interfaces/inner_api/appexecfwk_core/include/bundle_framework_core_ipc_interface_code.h index 37e2396f02d3643de755d64b334fc757a9fb7819..01483d6bb08a4dc03d466fb2b1ae2f43d50da813 100644 --- a/interfaces/inner_api/appexecfwk_core/include/bundle_framework_core_ipc_interface_code.h +++ b/interfaces/inner_api/appexecfwk_core/include/bundle_framework_core_ipc_interface_code.h @@ -366,6 +366,7 @@ enum class BundleResourceInterfaceCode : uint8_t { ADD_RESOURCE_INFO_BY_ABILITY = 5, DELETE_RESOURCE_INFO = 6, GET_EXTENSION_ABILITY_RESOURCE_INFO = 7, + GET_LAUNCHER_ABILITY_RESOURCE_INFOS_BY_BUNDLE_LIST = 8, }; /* SAID: 401-127 Interface No.127 subservice also provides the following interfaces */ diff --git a/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_host.h b/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_host.h index a5589b1ef64b15402e038126e3509f8b65b56509..95e1df09e2c19fa884236c0715836bd1f21354f7 100644 --- a/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_host.h +++ b/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_host.h @@ -43,6 +43,8 @@ private: ErrCode HandleGetAllLauncherAbilityResourceInfo(MessageParcel &data, MessageParcel &reply); + ErrCode HandleGetLauncherAbilityResourceInfosByBundleList(MessageParcel &data, MessageParcel &reply); + int32_t AllocatAshmemNum(); ErrCode WriteParcelableIntoAshmem(MessageParcel &tempParcel, MessageParcel &reply); @@ -61,6 +63,9 @@ private: template ErrCode WriteVectorToParcel(std::vector &parcelVector, MessageParcel &reply); + template + ErrCode GetVectorParcelInfo(MessageParcel &data, std::vector &parcelInfos); + int32_t ashmemNum_ = 0; std::mutex bundleAshmemMutex_; diff --git a/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_interface.h b/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_interface.h index c181b22693730d9d095e36f3ba8983aefc996fa4..04b2d5eb5bb6dcc5d47f4205fc42c37cfe66f6fe 100644 --- a/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_interface.h +++ b/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_interface.h @@ -19,6 +19,7 @@ #include "appexecfwk_errors.h" #include "bundle_resource/bundle_resource_info.h" #include "bundle_resource/launcher_ability_resource_info.h" +#include "bundle_resource/bundle_resource_key.h" #include "extension_ability_info.h" #include "iremote_broker.h" @@ -52,6 +53,12 @@ public: return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; } + virtual ErrCode GetLauncherAbilityResourceInfosByBundleList(std::vector &resourceKeys, + const uint32_t flags, std::vector &launcherAbilityResourceInfos) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } + virtual ErrCode AddResourceInfoByBundleName(const std::string &bundleName, const int32_t userId) { return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; diff --git a/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_proxy.h b/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_proxy.h index df55c14522413a0ab48f6476d7412071f1718811..939ccea17652a15ee678798a36e5e8ccb0e3b779 100644 --- a/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_proxy.h +++ b/interfaces/inner_api/appexecfwk_core/include/bundle_resource/bundle_resource_proxy.h @@ -38,6 +38,9 @@ public: virtual ErrCode GetAllLauncherAbilityResourceInfo(const uint32_t flags, std::vector &launcherAbilityResourceInfos) override; + + virtual ErrCode GetLauncherAbilityResourceInfosByBundleList(std::vector &resourceKeys, + const uint32_t flags, std::vector &launcherAbilityResourceInfos) override; virtual ErrCode AddResourceInfoByBundleName(const std::string &bundleName, const int32_t userId) override; @@ -60,6 +63,9 @@ private: ErrCode GetParcelInfoFromAshMem(MessageParcel &reply, void *&data); + template + ErrCode WriteVectorToParcel(std::vector &parcelVector, MessageParcel &reply); + bool SendRequest(BundleResourceInterfaceCode code, MessageParcel &data, MessageParcel &reply); static inline BrokerDelegator delegator_; diff --git a/interfaces/inner_api/appexecfwk_core/src/bundle_resource/bundle_resource_host.cpp b/interfaces/inner_api/appexecfwk_core/src/bundle_resource/bundle_resource_host.cpp index ed9a86a1832cf8dbf2f3a2a2444f28d36d796de5..226445c8ca87215a1108faef7394397a88214af5 100644 --- a/interfaces/inner_api/appexecfwk_core/src/bundle_resource/bundle_resource_host.cpp +++ b/interfaces/inner_api/appexecfwk_core/src/bundle_resource/bundle_resource_host.cpp @@ -15,15 +15,20 @@ #include "bundle_resource_host.h" +#include "securec.h" + #include "app_log_wrapper.h" #include "bundle_framework_core_ipc_interface_code.h" #include "bundle_memory_guard.h" +#include "bundle_resource_key.h" #include "hitrace_meter.h" #include "datetime_ex.h" #include "ipc_types.h" #include "json_util.h" #include "string_ex.h" #include "extension_ability_info.h" +#include "parcel.h" +#include "parcel_macro.h" namespace OHOS { namespace AppExecFwk { @@ -31,6 +36,28 @@ namespace { constexpr size_t MAX_PARCEL_CAPACITY = 1024 * 1024 * 1024; // max allow 1 GB resource size constexpr size_t MAX_IPC_ALLOWED_CAPACITY = 100 * 1024 * 1024; // max ipc size 100MB const std::string BUNDLE_RESOURCE_ASHMEM_NAME = "bundleResourceAshemeName"; +bool GetData(size_t size, const void *data, void *&buffer) +{ + if (data == nullptr) { + APP_LOGE("failed due to null data"); + return false; + } + if ((size == 0) || size > MAX_PARCEL_CAPACITY) { + APP_LOGE("failed due to wrong size"); + return false; + } + buffer = malloc(size); + if (buffer == nullptr) { + APP_LOGE("failed due to malloc buffer failed"); + return false; + } + if (memcpy_s(buffer, size, data, size) != EOK) { + free(buffer); + APP_LOGE("failed due to memcpy_s failed"); + return false; + } + return true; +} } BundleResourceHost::BundleResourceHost() { @@ -75,6 +102,9 @@ int32_t BundleResourceHost::OnRemoteRequest(uint32_t code, MessageParcel &data, case static_cast(BundleResourceInterfaceCode::GET_EXTENSION_ABILITY_RESOURCE_INFO): errCode = this->HandleGetExtensionAbilityResourceInfo(data, reply); break; + case static_cast(BundleResourceInterfaceCode::GET_LAUNCHER_ABILITY_RESOURCE_INFOS_BY_BUNDLE_LIST): + errCode = this->HandleGetLauncherAbilityResourceInfosByBundleList(data, reply); + break; default: APP_LOGW("bundle resource host receives unknown %{public}u", code); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -151,6 +181,29 @@ ErrCode BundleResourceHost::HandleGetAllLauncherAbilityResourceInfo(MessageParce return ERR_OK; } +ErrCode BundleResourceHost::HandleGetLauncherAbilityResourceInfosByBundleList(MessageParcel &data, + MessageParcel &reply) +{ + HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); + std::vector resourceKeys; + auto ret = GetVectorParcelInfo(data, resourceKeys); + if (ret != ERR_OK) { + APP_LOGE("read resourceKeys failed"); + return ret; + } + uint32_t flags = data.ReadUint32(); + std::vector launcherAbilityResourceInfos; + ret = GetLauncherAbilityResourceInfosByBundleList(resourceKeys, flags, launcherAbilityResourceInfos); + if (!reply.WriteInt32(ret)) { + APP_LOGE("write failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + if (ret == ERR_OK) { + return WriteVectorToParcel(launcherAbilityResourceInfos, reply); + } + return ERR_OK; +} + ErrCode BundleResourceHost::HandleAddResourceInfoByBundleName(MessageParcel &data, MessageParcel &reply) { HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr); @@ -305,5 +358,45 @@ ErrCode BundleResourceHost::WriteVectorToParcel(std::vector &parcelVector, Me } return ERR_OK; } + +template +ErrCode BundleResourceHost::GetVectorParcelInfo(MessageParcel &data, std::vector &parcelInfos) +{ + size_t dataSize = data.ReadUint32(); + if (dataSize == 0) { + APP_LOGW("Parcel no data"); + return ERR_OK; + } + + void *buffer = nullptr; + if (dataSize > MAX_IPC_ALLOWED_CAPACITY) { + APP_LOGE("dataSize is too large"); + return ERR_BUNDLE_MANAGER_PARAM_ERROR; + } else { + if (!GetData(dataSize, data.ReadRawData(dataSize), buffer)) { + APP_LOGE("GetData failed dataSize: %{public}zu", dataSize); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + } + + MessageParcel tempParcel; + if (!tempParcel.ParseFrom(reinterpret_cast(buffer), dataSize)) { + APP_LOGE("Fail to ParseFrom"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + int32_t infoSize = tempParcel.ReadInt32(); + CONTAINER_SECURITY_VERIFY(tempParcel, infoSize, &parcelInfos); + for (int32_t i = 0; i < infoSize; i++) { + std::unique_ptr info(tempParcel.ReadParcelable()); + if (info == nullptr) { + APP_LOGE("Read Parcelable infos failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + parcelInfos.emplace_back(*info); + } + + return ERR_OK; +} } // AppExecFwk } // OHOS diff --git a/interfaces/inner_api/appexecfwk_core/src/bundle_resource/bundle_resource_proxy.cpp b/interfaces/inner_api/appexecfwk_core/src/bundle_resource/bundle_resource_proxy.cpp index 50481462f83a025b4d0d444703801564d6a4201a..231a13f223d30265630e0d05e1b6ffa623195496 100644 --- a/interfaces/inner_api/appexecfwk_core/src/bundle_resource/bundle_resource_proxy.cpp +++ b/interfaces/inner_api/appexecfwk_core/src/bundle_resource/bundle_resource_proxy.cpp @@ -20,6 +20,7 @@ #include "app_log_wrapper.h" #include "appexecfwk_errors.h" +#include "bundle_resource_key.h" #include "hitrace_meter.h" #include "ipc_types.h" #include "parcel.h" @@ -160,6 +161,32 @@ ErrCode BundleResourceProxy::GetAllLauncherAbilityResourceInfo(const uint32_t fl BundleResourceInterfaceCode::GET_ALL_LAUNCHER_ABILITY_RESOURCE_INFO, data, launcherAbilityResourceInfos); } +ErrCode BundleResourceProxy::GetLauncherAbilityResourceInfosByBundleList( + std::vector &resourceKeys, const uint32_t flags, + std::vector &launcherAbilityResourceInfos) +{ + HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr); + APP_LOGD("start, flags:%{public}u", flags); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + APP_LOGE("fail to write InterfaceToken"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + auto ret = WriteVectorToParcel(resourceKeys, data); + if (ret != ERR_OK) { + APP_LOGE("fail to write resourceKeys"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + if (!data.WriteUint32(flags)) { + APP_LOGE("fail to write flags"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + return GetVectorParcelInfo( + BundleResourceInterfaceCode::GET_LAUNCHER_ABILITY_RESOURCE_INFOS_BY_BUNDLE_LIST, data, + launcherAbilityResourceInfos); +} + ErrCode BundleResourceProxy::AddResourceInfoByBundleName(const std::string &bundleName, const int32_t userId) { HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr); @@ -442,5 +469,39 @@ bool BundleResourceProxy::SendRequest(BundleResourceInterfaceCode code, } return true; } + +template +ErrCode BundleResourceProxy::WriteVectorToParcel(std::vector &parcelVector, MessageParcel &reply) +{ + MessageParcel tempParcel; + (void)tempParcel.SetMaxCapacity(Constants::MAX_PARCEL_CAPACITY); + if (!tempParcel.WriteInt32(static_cast(parcelVector.size()))) { + APP_LOGE("write failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + for (auto &parcel : parcelVector) { + if (!tempParcel.WriteParcelable(&parcel)) { + APP_LOGE("write failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + } + + size_t dataSize = tempParcel.GetDataSize(); + if (!reply.WriteUint32(dataSize)) { + APP_LOGE("write failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + if (dataSize > MAX_IPC_ALLOWED_CAPACITY) { + APP_LOGE("datasize is too large"); + return ERR_BUNDLE_MANAGER_PARAM_ERROR; + } + if (!reply.WriteRawData(reinterpret_cast(tempParcel.GetData()), dataSize)) { + APP_LOGE("write parcel failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + return ERR_OK; +} } // AppExecFwk } // OHOS diff --git a/interfaces/inner_api/test/unittest/bundle_resource_host_test/bundle_resource_host_test.cpp b/interfaces/inner_api/test/unittest/bundle_resource_host_test/bundle_resource_host_test.cpp index cfa923453fabbcde7598b05c09c2aa1d81fbdeb9..427c18467988c0a0c4fc0c36ac893d54de9fcd2a 100644 --- a/interfaces/inner_api/test/unittest/bundle_resource_host_test/bundle_resource_host_test.cpp +++ b/interfaces/inner_api/test/unittest/bundle_resource_host_test/bundle_resource_host_test.cpp @@ -23,6 +23,7 @@ #define private public #include "bundle_resource_host.h" #undef private +#include "bundle_resource/bundle_resource_key.h" #include "bundle_resource_interface.h" #include "iremote_stub.h" #include "ability_info.h" @@ -46,6 +47,34 @@ const uint32_t CODE_ADD_RESOURCE_INFO_BY_ABILITY = 5; const uint32_t CODE_DELETE_RESOURCE_INFO = 6; const uint32_t CODE_GET_EXTENSION_ABILITY_RESOURCE_INFO = 7; const uint32_t CODE_ERR = 100; +constexpr int32_t MAX_PARCEL_CAPACITY = 100 * 1024 * 1024; // 100M + +ErrCode WriteVectorToParcel(std::vector &parcelVector, MessageParcel &reply) +{ + Parcel tempParcel; + (void)tempParcel.SetMaxCapacity(MAX_PARCEL_CAPACITY); + if (!tempParcel.WriteInt32(parcelVector.size())) { + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + for (auto &parcel : parcelVector) { + if (!tempParcel.WriteParcelable(&parcel)) { + return ERR_APPEXECFWK_PARCEL_ERROR; + } + } + + size_t dataSize = tempParcel.GetDataSize(); + if (!reply.WriteInt32(static_cast(dataSize))) { + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + if (!reply.WriteRawData( + reinterpret_cast(tempParcel.GetData()), dataSize)) { + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + return ERR_OK; +} } class MockBundleResourceHostImpl : public BundleResourceHost { @@ -430,6 +459,47 @@ HWTEST_F(BundleResourceHostTest, HandleGetAllLauncherAbilityResourceInfo_0100, F EXPECT_EQ(ret, ERR_OK); } +/** + * @tc.number: HandleGetLauncherAbilityResourceInfosByBundleList_0100 + * @tc.name: test the HandleGetLauncherAbilityResourceInfosByBundleList + * @tc.desc: 1. system running normally + * 2. test HandleGetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(BundleResourceHostTest, HandleGetLauncherAbilityResourceInfosByBundleList_0100, Function | SmallTest | Level0) +{ + GTEST_LOG_(INFO) << "HandleGetLauncherAbilityResourceInfosByBundleList_0100 start"; + BundleResourceHost bundleResourceHost; + MessageParcel data; + MessageParcel reply; + auto ret = bundleResourceHost.HandleGetLauncherAbilityResourceInfosByBundleList(data, reply); + GTEST_LOG_(INFO) << "HandleGetLauncherAbilityResourceInfosByBundleList_0100 end" << ret; + EXPECT_EQ(ret, ERR_OK); +} + +/** + * @tc.number: HandleGetLauncherAbilityResourceInfosByBundleList_0200 + * @tc.name: test the HandleGetLauncherAbilityResourceInfosByBundleList + * @tc.desc: 1. system running normally + * 2. test HandleGetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(BundleResourceHostTest, HandleGetLauncherAbilityResourceInfosByBundleList_0200, Function | SmallTest | Level0) +{ + GTEST_LOG_(INFO) << "HandleGetLauncherAbilityResourceInfosByBundleList_0200 start"; + BundleResourceHost bundleResourceHost; + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + std::vector resourceKeys; + resourceKeys.emplace_back(resourceKey); + MessageParcel data; + WriteVectorToParcel(resourceKeys, data); + MessageParcel reply; + auto ret = bundleResourceHost.HandleGetLauncherAbilityResourceInfosByBundleList(data, reply); + GTEST_LOG_(INFO) << "HandleGetLauncherAbilityResourceInfosByBundleList_0200 end" << ret; + EXPECT_EQ(ret, ERR_OK); +} + /** * @tc.number: HandleAddResourceInfoByBundleName_0100 * @tc.name: test the HandleAddResourceInfoByBundleName @@ -1191,5 +1261,47 @@ HWTEST_F(BundleResourceHostTest, MimeTypeMgr_GetUriSuffix_0001, Function | Small auto ret = localMimeTypeMgr->GetMimeTypeByUri(uri, suffix); EXPECT_FALSE(ret); } + +/** + * @tc.number: BundleResourceKey_ReadFromParcel_0001 + * @tc.name: BundleResourceKey_ReadFromParcel_0001 + * @tc.desc: test ReadFromParcel + */ +HWTEST_F(BundleResourceHostTest, BundleResourceKey_ReadFromParcel_0001, Function | SmallTest | Level1) +{ + BundleResourceKey resourceKey; + Parcel parcel {}; + + auto ret = resourceKey.ReadFromParcel(parcel); + EXPECT_FALSE(ret); +} + +/** +* @tc.number: BundleResourceKey_Unmarshalling_0001 +* @tc.name: BundleResourceKey_Unmarshalling_0001 +* @tc.desc: test Unmarshalling +*/ +HWTEST_F(BundleResourceHostTest, BundleResourceKey_Unmarshalling_0001, Function | SmallTest | Level1) +{ + BundleResourceKey resourceKey; + Parcel parcel {}; + + auto ret = resourceKey.Unmarshalling(parcel); + EXPECT_EQ(ret, nullptr); +} + +/** +* @tc.number: BundleResourceKey_Marshalling_0001 +* @tc.name: BundleResourceKey_Marshalling_0001 +* @tc.desc: test Marshalling +*/ +HWTEST_F(BundleResourceHostTest, BundleResourceKey_Marshalling_0001, Function | SmallTest | Level1) +{ + BundleResourceKey resourceKey; + Parcel parcel {}; + + auto ret = resourceKey.Marshalling(parcel); + EXPECT_TRUE(ret); +} } } \ No newline at end of file diff --git a/interfaces/inner_api/test/unittest/bundle_resource_proxy_test/bundle_resource_proxy_test.cpp b/interfaces/inner_api/test/unittest/bundle_resource_proxy_test/bundle_resource_proxy_test.cpp index aaa40c6a7b62113c18e7a6ebd12c6f7762e2d0de..f1637f17d36c7c6e5e208c691e140a8331ec16f3 100644 --- a/interfaces/inner_api/test/unittest/bundle_resource_proxy_test/bundle_resource_proxy_test.cpp +++ b/interfaces/inner_api/test/unittest/bundle_resource_proxy_test/bundle_resource_proxy_test.cpp @@ -17,6 +17,7 @@ #define private public #include "bundle_resource_proxy.h" #undef private +#include "bundle_resource/bundle_resource_key.h" #include "bundle_resource_interface.h" #include "iremote_stub.h" @@ -199,6 +200,27 @@ HWTEST_F(BundleResourceProxyTest, GetAllLauncherAbilityResourceInfo_0100, Functi EXPECT_EQ(ret, ERR_APPEXECFWK_PARCEL_ERROR); } +/** + * @tc.number: GetLauncherAbilityResourceInfosByBundleList_0100 + * @tc.name: test the GetLauncherAbilityResourceInfosByBundleList + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(BundleResourceProxyTest, GetLauncherAbilityResourceInfosByBundleList_0100, Function | SmallTest | Level0) +{ + GTEST_LOG_(INFO) << "GetLauncherAbilityResourceInfosByBundleList_0100 start"; + sptr stub = new MockStub(); + sptr proxy = new BundleResourceProxy(stub->AsObject()); + BundleResourceKey resourceKey; + std::vector resourceKeys; + resourceKeys.emplace_back(resourceKey); + uint32_t flags = 1; + std::vector launcherAbilityResourceInfos; + auto ret = proxy->GetLauncherAbilityResourceInfosByBundleList(resourceKeys, flags, launcherAbilityResourceInfos); + GTEST_LOG_(INFO) << "GetLauncherAbilityResourceInfosByBundleList_0100 end, " << ret; + EXPECT_EQ(ret, ERR_APPEXECFWK_PARCEL_ERROR); +} + /** * @tc.number: AddResourceInfoByBundleName_0100 * @tc.name: test the AddResourceInfoByBundleName diff --git a/interfaces/kits/js/bundle_resource/bundle_resource.cpp b/interfaces/kits/js/bundle_resource/bundle_resource.cpp index 1e050529e2da2e1534a631989f71dd8f3b0a1cb3..b1108ef98f825d6ef07a8b265d1046c3de2593aa 100644 --- a/interfaces/kits/js/bundle_resource/bundle_resource.cpp +++ b/interfaces/kits/js/bundle_resource/bundle_resource.cpp @@ -404,6 +404,106 @@ napi_value GetAllLauncherAbilityResourceInfo(napi_env env, napi_callback_info in return promise; } +static ErrCode InnerGetLauncherAbilityResourceInfosByBundleList(std::vector resourceKeys, + uint32_t flags, std::vector &launcherAbilityResourceInfos) +{ + auto bundleResourceProxy = ResourceHelper::GetBundleResourceMgr(); + if (bundleResourceProxy == nullptr) { + APP_LOGE("bundleResourceProxy is null"); + return ERROR_BUNDLE_SERVICE_EXCEPTION; + } + ErrCode ret = bundleResourceProxy->GetLauncherAbilityResourceInfosByBundleList(resourceKeys, flags, + launcherAbilityResourceInfos); + if (ret != ERR_OK) { + APP_LOGE("failed, errCode: %{public}d", ret); + } + return CommonFunc::ConvertErrCode(ret); +} + +void GetLauncherAbilityResourceInfosByBundleListExec(napi_env env, void *data) +{ + LauncherAbilityResourceInfosByBundleListCallback *asyncCallbackInfo = + reinterpret_cast(data); + if (asyncCallbackInfo == nullptr) { + APP_LOGE("asyncCallbackInfo is null"); + return; + } + asyncCallbackInfo->err = InnerGetLauncherAbilityResourceInfosByBundleList( + asyncCallbackInfo->resourceKeys, asyncCallbackInfo->flags, asyncCallbackInfo->launcherAbilityResourceInfos); +} + +void GetLauncherAbilityResourceInfosByBundleListComplete(napi_env env, napi_status status, void *data) +{ + LauncherAbilityResourceInfosByBundleListCallback *asyncCallbackInfo = + reinterpret_cast(data); + if (asyncCallbackInfo == nullptr) { + APP_LOGE("asyncCallbackInfo is null"); + return; + } + std::unique_ptr callbackPtr {asyncCallbackInfo}; + napi_value result[ARGS_SIZE_TWO] = {0}; + if (asyncCallbackInfo->err == NO_ERROR) { + NAPI_CALL_RETURN_VOID(env, napi_get_null(env, &result[0])); + NAPI_CALL_RETURN_VOID(env, napi_create_array(env, &result[1])); + ConvertLauncherAbilityResourceInfos(env, asyncCallbackInfo->launcherAbilityResourceInfos, result[1]); + } else { + result[0] = BusinessError::CreateCommonError(env, asyncCallbackInfo->err, + GET_ALL_LAUNCHER_ABILITY_RESOURCE_INFO, PERMISSION_GET_ALL_BUNDLE_RESOURCES); + } + CommonFunc::NapiReturnDeferred(env, asyncCallbackInfo, + result, ARGS_SIZE_TWO); +} + +napi_value GetLauncherAbilityResourceInfosByBundleList(napi_env env, napi_callback_info info) +{ + APP_LOGD("NAPI start"); + NapiArg args(env, info); + if (!args.Init(ARGS_SIZE_TWO, ARGS_SIZE_THREE)) { + APP_LOGE("param count invalid"); + BusinessError::ThrowTooFewParametersError(env, ERROR_PARAM_CHECK_ERROR); + return nullptr; + } + LauncherAbilityResourceInfosByBundleListCallback *asyncCallbackInfo = + new (std::nothrow) LauncherAbilityResourceInfosByBundleListCallback(env); + if (asyncCallbackInfo == nullptr) { + APP_LOGE("asyncCallbackInfo is null"); + return nullptr; + } + std::unique_ptr callbackPtr {asyncCallbackInfo}; + + if (!CommonFunc::ParseBundleResourceKeyArray(env, args[ARGS_POS_ZERO], asyncCallbackInfo->resourceKeys) + || asyncCallbackInfo->resourceKeys.empty()) { + APP_LOGE("bundleResourceKeys invalid"); + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, BUNDLE_RESOURCE_KYS, TYPE_ARRAY); + return nullptr; + } + + int32_t flags = 0; + if (!CommonFunc::ParseInt(env, args[ARGS_POS_ONE], flags)) { + APP_LOGE("Flags %{public}d invalid", flags); + BusinessError::ThrowParameterTypeError(env, ERROR_PARAM_CHECK_ERROR, RESOURCE_FLAGS, TYPE_NUMBER); + return nullptr; + } + if (flags <= 0) { + flags = static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL); + } + asyncCallbackInfo->flags = static_cast(flags); + if (args.GetMaxArgc() >= ARGS_SIZE_THREE) { + napi_valuetype valueType = napi_undefined; + napi_typeof(env, args[ARGS_POS_TWO], &valueType); + if (valueType == napi_function) { + NAPI_CALL(env, napi_create_reference(env, args[ARGS_POS_TWO], + NAPI_RETURN_ONE, &asyncCallbackInfo->callback)); + } + } + auto promise = CommonFunc::AsyncCallNativeMethod( + env, asyncCallbackInfo, GET_LAUNCHER_ABILITY_RESOURCE_INFOS_BY_BUNDLE_LIST, + GetLauncherAbilityResourceInfosByBundleListExec, GetLauncherAbilityResourceInfosByBundleListComplete); + callbackPtr.release(); + APP_LOGD("NAPI end"); + return promise; +} + void CreateBundleResourceFlagObject(napi_env env, napi_value value) { napi_value nGetAll; diff --git a/interfaces/kits/js/bundle_resource/bundle_resource.h b/interfaces/kits/js/bundle_resource/bundle_resource.h index 9ced321100d1a3b802637fdb068ea7b3402af700..41c50ccb84c6fbd6f167a59c3c62f50f115ab057 100644 --- a/interfaces/kits/js/bundle_resource/bundle_resource.h +++ b/interfaces/kits/js/bundle_resource/bundle_resource.h @@ -17,6 +17,7 @@ #define FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_INTERFACE_KITS_JS_BUNDLE_RESOURCE_H #include "base_cb_info.h" +#include "bundle_resource_key.h" #include "napi/native_api.h" #include "napi/native_common.h" #include "napi/native_node_api.h" @@ -41,11 +42,20 @@ struct AllLauncherAbilityResourceInfoCallback : public BaseCallbackInfo { uint32_t flags = static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL); std::vector launcherAbilityResourceInfos; }; + +struct LauncherAbilityResourceInfosByBundleListCallback : public BaseCallbackInfo { + explicit LauncherAbilityResourceInfosByBundleListCallback(napi_env napiEnv) : BaseCallbackInfo(napiEnv) {} + + std::vector resourceKeys; + uint32_t flags = static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL); + std::vector launcherAbilityResourceInfos; +}; #endif napi_value GetBundleResourceInfo(napi_env env, napi_callback_info info); napi_value GetLauncherAbilityResourceInfo(napi_env env, napi_callback_info info); napi_value GetAllBundleResourceInfo(napi_env env, napi_callback_info info); napi_value GetAllLauncherAbilityResourceInfo(napi_env env, napi_callback_info info); +napi_value GetLauncherAbilityResourceInfosByBundleList(napi_env env, napi_callback_info info); napi_value GetExtensionAbilityResourceInfo(napi_env env, napi_callback_info info); void CreateBundleResourceFlagObject(napi_env env, napi_value value); } // namespace AppExecFwk diff --git a/interfaces/kits/js/bundle_resource/bundle_resource_unsupported.cpp b/interfaces/kits/js/bundle_resource/bundle_resource_unsupported.cpp index 44c8edc1d9220f012863b334dc4975f3928b5bd1..07eba8a6c7c181f85613d57bd83af848acccf2ec 100644 --- a/interfaces/kits/js/bundle_resource/bundle_resource_unsupported.cpp +++ b/interfaces/kits/js/bundle_resource/bundle_resource_unsupported.cpp @@ -59,6 +59,15 @@ napi_value GetAllLauncherAbilityResourceInfo(napi_env env, napi_callback_info in return nullptr; } +napi_value GetLauncherAbilityResourceInfosByBundleList(napi_env env, napi_callback_info info) +{ + APP_LOGE("SystemCapability.BundleManager.BundleFramework.Resource not supported"); + napi_value error = BusinessError::CreateCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND, + "getLauncherAbilityResourceInfosByBundleList"); + napi_throw(env, error); + return nullptr; +} + void CreateBundleResourceFlagObject(napi_env env, napi_value value) { NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &value)); diff --git a/interfaces/kits/js/bundle_resource/native_module.cpp b/interfaces/kits/js/bundle_resource/native_module.cpp index 081c81b2480304d78b79fd6dd2c306f430b126e1..f4613133b8b2086e891f420049e04fe70b46d6bc 100644 --- a/interfaces/kits/js/bundle_resource/native_module.cpp +++ b/interfaces/kits/js/bundle_resource/native_module.cpp @@ -34,6 +34,8 @@ static napi_value BundleResourceExport(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getLauncherAbilityResourceInfo", GetLauncherAbilityResourceInfo), DECLARE_NAPI_FUNCTION("getAllBundleResourceInfo", GetAllBundleResourceInfo), DECLARE_NAPI_FUNCTION("getAllLauncherAbilityResourceInfo", GetAllLauncherAbilityResourceInfo), + DECLARE_NAPI_FUNCTION("getLauncherAbilityResourceInfosByBundleList", + GetLauncherAbilityResourceInfosByBundleList), DECLARE_NAPI_FUNCTION("getExtensionAbilityResourceInfo", GetExtensionAbilityResourceInfo), DECLARE_NAPI_PROPERTY("ResourceFlag", resourceFlag), }; diff --git a/interfaces/kits/js/common/common_func.cpp b/interfaces/kits/js/common/common_func.cpp index d96992478cc1ac0abc29670f492cca78ca401ca5..7bac3b95cc6fe342c23fe803e8c5e24aa1fd305a 100644 --- a/interfaces/kits/js/common/common_func.cpp +++ b/interfaces/kits/js/common/common_func.cpp @@ -21,6 +21,7 @@ #include "bundle_errors.h" #include "bundle_mgr_interface.h" #include "bundle_mgr_proxy.h" +#include "bundle_resource_key.h" #include "iservice_registry.h" #include "napi/native_api.h" #include "napi/native_common.h" @@ -859,6 +860,64 @@ bool CommonFunc::ParseWantWithoutVerification(napi_env env, napi_value args, Wan return true; } +bool CommonFunc::ParseBundleResourceKey(napi_env env, napi_value args, BundleResourceKey &resourceKey) +{ + napi_valuetype valueType = napi_undefined; + napi_typeof(env, args, &valueType); + if (valueType != napi_object) { + APP_LOGW("args not object type"); + return false; + } + napi_value prop = nullptr; + napi_get_named_property(env, args, BUNDLE_NAME, &prop); + std::string bundleName = GetStringFromNAPI(env, prop); + resourceKey.bundleName = bundleName; + + prop = nullptr; + napi_get_named_property(env, args, MODULE_NAME, &prop); + std::string moduleName = GetStringFromNAPI(env, prop); + resourceKey.moduleName = moduleName; + + prop = nullptr; + napi_get_named_property(env, args, ABILITY_NAME, &prop); + std::string abilityName = GetStringFromNAPI(env, prop); + resourceKey.abilityName = abilityName; + + prop = nullptr; + napi_get_named_property(env, args, APP_INDEX, &prop); + int32_t appIndex; + if (!ParseInt(env, prop, appIndex)) { + appIndex = 0; + } + resourceKey.appIndex = appIndex; + + return true; +} + +bool CommonFunc::ParseBundleResourceKeyArray(napi_env env, napi_value args, + std::vector &resourceKeys) +{ + APP_LOGD("begin to parse bundleResourceKey"); + bool isArray = false; + NAPI_CALL_BASE(env, napi_is_array(env, args, &isArray), false); + if (!isArray) { + return false; + } + uint32_t arrayLength = 0; + NAPI_CALL_BASE(env, napi_get_array_length(env, args, &arrayLength), false); + APP_LOGD("length=%{public}ud", arrayLength); + for (uint32_t j = 0; j < arrayLength; j++) { + BundleResourceKey resourceKey; + napi_value value = nullptr; + NAPI_CALL_BASE(env, napi_get_element(env, args, j, &value), false); + if (!ParseBundleResourceKey(env, value, resourceKey)) { + return false; + } + resourceKeys.push_back(resourceKey); + } + return true; +} + void CommonFunc::ConvertWindowSize(napi_env env, const AbilityInfo &abilityInfo, napi_value value) { napi_value nMaxWindowRatio; diff --git a/interfaces/kits/js/common/common_func.h b/interfaces/kits/js/common/common_func.h index 3ae658ddefef4a5d433baec095e4dba01bfd9c7b..7d550ed7892b1f4cd0e988974035289b1892275c 100644 --- a/interfaces/kits/js/common/common_func.h +++ b/interfaces/kits/js/common/common_func.h @@ -21,6 +21,7 @@ #include "app_log_wrapper.h" #include "bundle_mgr_interface.h" +#include "bundle_resource_key.h" #include "dynamic_icon_info.h" #include "iremote_object.h" #include "launcher_ability_info.h" @@ -88,6 +89,10 @@ static bool ParseWantPerformance(napi_env env, napi_value args, Want &want); static bool ParseWantWithoutVerification(napi_env env, napi_value args, Want &want); +static bool ParseBundleResourceKey(napi_env env, napi_value args, BundleResourceKey &resourceKey); + +static bool ParseBundleResourceKeyArray(napi_env env, napi_value args, std::vector &resourceKeys); + static bool ParseAbilityInfo(napi_env env, napi_value param, AbilityInfo& abilityInfo); static bool ParseShortCutInfo(napi_env env, napi_value param, ShortcutInfo &shortcutInfo); diff --git a/interfaces/kits/js/common/napi_constants.h b/interfaces/kits/js/common/napi_constants.h index 46268f79f54e2c3348d6d9b5bdfc0232066a5cb3..e8b8c2f5e5df0bc5046e99b27ea0e9733aecc794 100644 --- a/interfaces/kits/js/common/napi_constants.h +++ b/interfaces/kits/js/common/napi_constants.h @@ -120,6 +120,9 @@ constexpr const char* PERMISSION_GET_ALL_BUNDLE_RESOURCES = constexpr const char* GET_LAUNCHER_ABILITY_RESOURCE_INFO = "GetLauncherAbilityResourceInfo"; constexpr const char* GET_ALL_BUNDLE_RESOURCE_INFO = "GetAllBundleResourceInfo"; constexpr const char* GET_ALL_LAUNCHER_ABILITY_RESOURCE_INFO = "GetAllLauncherAbilityResourceInfo"; +constexpr const char* GET_LAUNCHER_ABILITY_RESOURCE_INFOS_BY_BUNDLE_LIST = + "GetLauncherAbilityResourceInfosByBundleList"; +constexpr const char* BUNDLE_RESOURCE_KYS = "bundleResourceKeys"; //shortcut_manager constexpr const char* ADD_DESKTOP_SHORTCUT_INFO = "AddDesktopShortcutInfo"; diff --git a/services/bundlemgr/include/bundle_resource/bundle_resource_host_impl.h b/services/bundlemgr/include/bundle_resource/bundle_resource_host_impl.h index 32646c012ebae3167a06fdd9cf7caa7e97c379a5..a928adde11e8b97e716576bf32b7bc1b8939205e 100644 --- a/services/bundlemgr/include/bundle_resource/bundle_resource_host_impl.h +++ b/services/bundlemgr/include/bundle_resource/bundle_resource_host_impl.h @@ -36,6 +36,9 @@ public: virtual ErrCode GetAllLauncherAbilityResourceInfo(const uint32_t flags, std::vector &launcherAbilityResourceInfos) override; + + virtual ErrCode GetLauncherAbilityResourceInfosByBundleList(std::vector &resourceKeys, + const uint32_t flags, std::vector &launcherAbilityResourceInfos) override; virtual ErrCode AddResourceInfoByBundleName(const std::string &bundleName, const int32_t userId) override; diff --git a/services/bundlemgr/include/bundle_resource/bundle_resource_manager.h b/services/bundlemgr/include/bundle_resource/bundle_resource_manager.h index 06462e0ad68987d7117d43726024e14573ed7d43..18c98f6fb0149e0d4c35a2e22a503e45c192c35e 100644 --- a/services/bundlemgr/include/bundle_resource/bundle_resource_manager.h +++ b/services/bundlemgr/include/bundle_resource/bundle_resource_manager.h @@ -24,6 +24,7 @@ #include "ability_info.h" #include "bundle_constants.h" #include "bundle_resource_change_type.h" +#include "bundle_resource_key.h" #include "bundle_resource_rdb.h" #include "bundle_system_state.h" #include "inner_bundle_info.h" @@ -80,6 +81,9 @@ public: bool GetAllLauncherAbilityResourceInfo(const uint32_t flags, std::vector &launcherAbilityResourceInfos); + bool GetLauncherAbilityResourceInfosByBundleList(const std::vector &resourceKeys, + const uint32_t flags, std::vector &launcherAbilityResourceInfos); + bool IsLauncherAbility(const LauncherAbilityResourceInfo &resourceInfo, std::vector &abilityInfos); bool GetLauncherAbilityInfos(const std::string &bundleName, std::vector &abilityInfos); diff --git a/services/bundlemgr/include/bundle_resource/bundle_resource_rdb.h b/services/bundlemgr/include/bundle_resource/bundle_resource_rdb.h index 400f932d3e67e9dee3713fbfce211f312f014281..4343dd82123af7bbdf420e4381f7f9db9bf4fc8f 100644 --- a/services/bundlemgr/include/bundle_resource/bundle_resource_rdb.h +++ b/services/bundlemgr/include/bundle_resource/bundle_resource_rdb.h @@ -18,6 +18,7 @@ #include +#include "bundle_resource_key.h" #include "bundle_system_state.h" #include "bundle_resource_info.h" #include "launcher_ability_resource_info.h" @@ -52,6 +53,9 @@ public: bool GetLauncherAbilityResourceInfo(const std::string &bundleName, const uint32_t flags, std::vector &launcherAbilityResourceInfo, const int32_t appIndex = 0); + bool GetLauncherAbilityResourceInfoByResourceKey(const BundleResourceKey &resourceKey, const uint32_t flags, + std::vector &launcherAbilityResourceInfo); + bool GetAllBundleResourceInfo(const uint32_t flags, std::vector &bundleResourceInfos); bool GetAllLauncherAbilityResourceInfo(const uint32_t flags, diff --git a/services/bundlemgr/src/bundle_resource/bundle_resource_host_impl.cpp b/services/bundlemgr/src/bundle_resource/bundle_resource_host_impl.cpp index 492b7303987ee63cd0f3cebf5a1c3b8525135bdb..2881c5e1f62274b145183aec8fb995b17d4185f6 100644 --- a/services/bundlemgr/src/bundle_resource/bundle_resource_host_impl.cpp +++ b/services/bundlemgr/src/bundle_resource/bundle_resource_host_impl.cpp @@ -17,6 +17,7 @@ #include "bms_extension_client.h" #include "bundle_permission_mgr.h" +#include "bundle_resource_key.h" #include "bundle_resource_manager.h" #include "bundle_mgr_service.h" #include "hitrace_meter.h" @@ -183,6 +184,50 @@ ErrCode BundleResourceHostImpl::GetAllLauncherAbilityResourceInfo(const uint32_t return ERR_OK; } +ErrCode BundleResourceHostImpl::GetLauncherAbilityResourceInfosByBundleList( + std::vector &resourceKeys, const uint32_t flags, + std::vector &launcherAbilityResourceInfos) +{ + APP_LOGD("start"); + if (!BundlePermissionMgr::IsSystemApp()) { + APP_LOGE("non-system app calling system api"); + return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED; + } + if (!BundlePermissionMgr::VerifyCallingPermissionForAll(ServiceConstants::PERMISSION_GET_BUNDLE_RESOURCES)) { + APP_LOGE("verify permission failed"); + return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; + } + if (!BundlePermissionMgr::VerifyCallingPermissionForAll(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST)) { + APP_LOGE("verify permission failed"); + return ERR_BUNDLE_MANAGER_PERMISSION_DENIED; + } + + auto manager = DelayedSingleton::GetInstance(); + if (manager == nullptr) { + APP_LOGE("manager is nullptr"); + BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1); + return ERR_APPEXECFWK_NULL_PTR; + } + if (!manager->GetLauncherAbilityResourceInfosByBundleList(resourceKeys, flags, launcherAbilityResourceInfos)) { + APP_LOGE("get all resource failed, flags:%{public}u", flags); + BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 0, 1); + return ERR_BUNDLE_MANAGER_INTERNAL_ERROR; + } + + if ((flags & static_cast(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL)) == + static_cast(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL)) { + APP_LOGD("need sort by label"); + std::sort(launcherAbilityResourceInfos.begin(), launcherAbilityResourceInfos.end(), + [](const LauncherAbilityResourceInfo &resourceA, const LauncherAbilityResourceInfo &resourceB) { + return resourceA.label < resourceB.label; + }); + } + BundlePermissionMgr::AddPermissionUsedRecord(Constants::PERMISSION_GET_INSTALLED_BUNDLE_LIST, 1, 0); + APP_LOGI_NOFUNC("GetLauncherAbilityResourceInfosByBundleList count:%{public}zu", + launcherAbilityResourceInfos.size()); + return ERR_OK; +} + ErrCode BundleResourceHostImpl::AddResourceInfoByBundleName(const std::string &bundleName, const int32_t userId) { APP_LOGD("start, bundleName:%{public}s userId:%{private}d", bundleName.c_str(), userId); diff --git a/services/bundlemgr/src/bundle_resource/bundle_resource_manager.cpp b/services/bundlemgr/src/bundle_resource/bundle_resource_manager.cpp index ce51576233a5a266ed674ae0dcf4f4094cb94f2f..8841ccc0cab2a70cc2717f7871d3dd3d7f868a2a 100644 --- a/services/bundlemgr/src/bundle_resource/bundle_resource_manager.cpp +++ b/services/bundlemgr/src/bundle_resource/bundle_resource_manager.cpp @@ -517,6 +517,24 @@ bool BundleResourceManager::GetAllLauncherAbilityResourceInfo(const uint32_t fla return bundleResourceRdb_->GetAllLauncherAbilityResourceInfo(resourceFlags, launcherAbilityResourceInfos); } +bool BundleResourceManager::GetLauncherAbilityResourceInfosByBundleList( + const std::vector &resourceKeys, + const uint32_t flags, + std::vector &launcherAbilityResourceInfos) +{ + APP_LOGD("start"); + uint32_t resourceFlags = CheckResourceFlags(flags); + for (const auto &resourceKey : resourceKeys) { + if (bundleResourceRdb_->GetLauncherAbilityResourceInfoByResourceKey(resourceKey, resourceFlags, + launcherAbilityResourceInfos)) { + APP_LOGD("success, bundleName:%{public}s", resourceKey.bundleName.c_str()); + continue; + } + APP_LOGW("%{public}s not exist in resource rdb", resourceKey.bundleName.c_str()); + } + return !launcherAbilityResourceInfos.empty(); +} + bool BundleResourceManager::FilterLauncherAbilityResourceInfoWithFlag(const uint32_t flags, const std::string &bundleName, std::vector &launcherAbilityResourceInfos) { diff --git a/services/bundlemgr/src/bundle_resource/bundle_resource_rdb.cpp b/services/bundlemgr/src/bundle_resource/bundle_resource_rdb.cpp index 552b6466bc0aab9556fcaaa506e09092478b6c46..3b1ff97ce569885c30690974126d4e61223a9b8e 100644 --- a/services/bundlemgr/src/bundle_resource/bundle_resource_rdb.cpp +++ b/services/bundlemgr/src/bundle_resource/bundle_resource_rdb.cpp @@ -355,6 +355,53 @@ bool BundleResourceRdb::GetLauncherAbilityResourceInfo( return !launcherAbilityResourceInfos.empty(); } +bool BundleResourceRdb::GetLauncherAbilityResourceInfoByResourceKey( + const BundleResourceKey &resourceKey, + const uint32_t flags, + std::vector &launcherAbilityResourceInfos) +{ + HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr); + APP_LOGI_NOFUNC("GetLauncherAbilityResourceInfoByResourceKey -n %{public}s -m %{public}s -a %{public}s" + "-i %{public}d", resourceKey.bundleName.c_str(), resourceKey.moduleName.c_str(), + resourceKey.abilityName.c_str(), resourceKey.appIndex); + if (resourceKey.bundleName.empty() || resourceKey.moduleName.empty() || resourceKey.abilityName.empty()) { + APP_LOGE("bundleName is empty"); + return false; + } + ResourceInfo resourceInfo; + resourceInfo.bundleName_ = resourceKey.bundleName; + resourceInfo.moduleName_ = resourceKey.moduleName; + resourceInfo.abilityName_ = resourceKey.abilityName; + resourceInfo.appIndex_ = resourceKey.appIndex; + NativeRdb::AbsRdbPredicates absRdbPredicates(BundleResourceConstants::BUNDLE_RESOURCE_RDB_TABLE_NAME); + absRdbPredicates.BeginsWith(BundleResourceConstants::NAME, resourceInfo.GetKey()); + std::string systemState = BundleSystemState::GetInstance().ToString(); + + auto absSharedResultSet = rdbDataManager_->QueryByStep(absRdbPredicates); + if (absSharedResultSet == nullptr) { + APP_LOGE("QueryByStep failed bundleName %{public}s failed, systemState %{public}s", + resourceKey.bundleName.c_str(), systemState.c_str()); + return false; + } + ScopeGuard stateGuard([absSharedResultSet] { absSharedResultSet->Close(); }); + auto ret = absSharedResultSet->GoToFirstRow(); + if (ret != NativeRdb::E_OK) { + APP_LOGE("bundleName %{public}s GoToFirstRow failed, ret %{public}d, systemState:%{public}s", + resourceKey.bundleName.c_str(), ret, systemState.c_str()); + return false; + } + + do { + LauncherAbilityResourceInfo resourceInfo; + if (ConvertToLauncherAbilityResourceInfo(absSharedResultSet, flags, resourceInfo)) { + launcherAbilityResourceInfos.push_back(resourceInfo); + } + } while (absSharedResultSet->GoToNextRow() == NativeRdb::E_OK); + + APP_LOGI_NOFUNC("end"); + return !launcherAbilityResourceInfos.empty(); +} + bool BundleResourceRdb::GetAllBundleResourceInfo(const uint32_t flags, std::vector &bundleResourceInfos) { diff --git a/services/bundlemgr/test/unittest/bms_bundle_resource_test/bms_bundle_resource_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_resource_test/bms_bundle_resource_test.cpp index 3dd907a05ccc971447064a55a1e16681dcab52f2..ecbe7fd4acd50c2cda386fb950c3571f7e89efc7 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_resource_test/bms_bundle_resource_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_resource_test/bms_bundle_resource_test.cpp @@ -38,6 +38,7 @@ #include "bundle_resource_helper.h" #include "bundle_resource_host_impl.h" #include "bundle_resource_info.h" +#include "bundle_resource_key.h" #include "bundle_resource_manager.h" #include "bundle_resource_observer.h" #include "bundle_resource_param.h" @@ -5949,4 +5950,451 @@ HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0230, Function | SmallTest EXPECT_FALSE(ret); } } + +/** + * @tc.number: BmsBundleResourceTest_0231 + * Function: BundleResourceRdb + * @tc.name: test BundleResourceRdb + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfoByResourceKey + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0231, Function | SmallTest | Level0) +{ + BundleResourceRdb resourceRdb; + ResourceInfo resourceInfo; + resourceInfo.bundleName_ = "bundleName"; + resourceInfo.moduleName_ = "moduleName"; + resourceInfo.abilityName_ = "abilityName"; + resourceInfo.label_ = "label"; + resourceInfo.icon_ = "icon"; + bool ans = resourceRdb.AddResourceInfo(resourceInfo); + EXPECT_TRUE(ans); + + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + std::vector infos; + ans = resourceRdb.GetLauncherAbilityResourceInfoByResourceKey(resourceKey, + static_cast(ResourceFlag::GET_RESOURCE_INFO_WITH_LABEL), infos); + EXPECT_TRUE(ans); + EXPECT_FALSE(infos.empty()); + if (!infos.empty()) { + EXPECT_EQ(infos[0].bundleName, resourceInfo.bundleName_); + EXPECT_EQ(infos[0].moduleName, resourceInfo.moduleName_); + EXPECT_EQ(infos[0].abilityName, resourceInfo.abilityName_); + EXPECT_EQ(infos[0].label, resourceInfo.label_); + EXPECT_TRUE(infos[0].icon.empty()); + } + + ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey()); + EXPECT_TRUE(ans); +} + +/** + * @tc.number: BmsBundleResourceTest_0232 + * Function: BundleResourceRdb + * @tc.name: test BundleResourceRdb + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfoByResourceKey + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0232, Function | SmallTest | Level0) +{ + BundleResourceRdb resourceRdb; + ResourceInfo resourceInfo; + resourceInfo.bundleName_ = "bundleName"; + resourceInfo.moduleName_ = "moduleName"; + resourceInfo.abilityName_ = "abilityName"; + resourceInfo.label_ = "label"; + resourceInfo.icon_ = "icon"; + bool ans = resourceRdb.AddResourceInfo(resourceInfo); + EXPECT_TRUE(ans); + + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + std::vector infos; + ans = resourceRdb.GetLauncherAbilityResourceInfoByResourceKey(resourceKey, + static_cast(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON), infos); + EXPECT_TRUE(ans); + EXPECT_FALSE(infos.empty()); + if (!infos.empty()) { + EXPECT_EQ(infos[0].bundleName, resourceInfo.bundleName_); + EXPECT_EQ(infos[0].moduleName, resourceInfo.moduleName_); + EXPECT_EQ(infos[0].abilityName, resourceInfo.abilityName_); + EXPECT_EQ(infos[0].icon, resourceInfo.icon_); + EXPECT_TRUE(infos[0].label.empty()); + } + + ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey()); + EXPECT_TRUE(ans); +} + +/** + * @tc.number: BmsBundleResourceTest_0233 + * Function: BundleResourceRdb + * @tc.name: test BundleResourceRdb + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfoByResourceKey + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0233, Function | SmallTest | Level0) +{ + BundleResourceRdb resourceRdb; + ResourceInfo resourceInfo; + resourceInfo.bundleName_ = "bundleName"; + resourceInfo.moduleName_ = "moduleName"; + resourceInfo.abilityName_ = "abilityName"; + resourceInfo.label_ = "label"; + resourceInfo.icon_ = "icon"; + bool ans = resourceRdb.AddResourceInfo(resourceInfo); + EXPECT_TRUE(ans); + + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + std::vector infos; + ans = resourceRdb.GetLauncherAbilityResourceInfoByResourceKey(resourceKey, + static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL), infos); + EXPECT_TRUE(ans); + EXPECT_FALSE(infos.empty()); + if (!infos.empty()) { + EXPECT_EQ(infos[0].bundleName, resourceInfo.bundleName_); + EXPECT_EQ(infos[0].moduleName, resourceInfo.moduleName_); + EXPECT_EQ(infos[0].abilityName, resourceInfo.abilityName_); + EXPECT_EQ(infos[0].icon, resourceInfo.icon_); + EXPECT_EQ(infos[0].label, resourceInfo.label_); + } + + ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey()); + EXPECT_TRUE(ans); +} + +/** + * @tc.number: BmsBundleResourceTest_0234 + * Function: BundleResourceRdb + * @tc.name: test BundleResourceRdb + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfoByResourceKey + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0234, Function | SmallTest | Level0) +{ + BundleResourceRdb resourceRdb; + ResourceInfo resourceInfo; + resourceInfo.bundleName_ = "bundleName"; + resourceInfo.moduleName_ = "moduleName"; + resourceInfo.abilityName_ = "abilityName"; + resourceInfo.label_ = "label"; + resourceInfo.icon_ = "icon"; + resourceInfo.appIndex_ = 1; + bool ans = resourceRdb.AddResourceInfo(resourceInfo); + EXPECT_TRUE(ans); + + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + resourceKey.appIndex = 1; + std::vector infos; + ans = resourceRdb.GetLauncherAbilityResourceInfoByResourceKey(resourceKey, + static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL), infos); + EXPECT_TRUE(ans); + EXPECT_FALSE(infos.empty()); + if (!infos.empty()) { + EXPECT_EQ(infos[0].bundleName, resourceInfo.bundleName_); + EXPECT_EQ(infos[0].moduleName, resourceInfo.moduleName_); + EXPECT_EQ(infos[0].abilityName, resourceInfo.abilityName_); + EXPECT_EQ(infos[0].icon, resourceInfo.icon_); + EXPECT_EQ(infos[0].label, resourceInfo.label_); + EXPECT_EQ(infos[0].appIndex, resourceInfo.appIndex_); + } + + ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey()); + EXPECT_TRUE(ans); +} + +/** + * @tc.number: BmsBundleResourceTest_0235 + * Function: BundleResourceRdb + * @tc.name: test BundleResourceRdb + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfoByResourceKey + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0235, Function | SmallTest | Level0) +{ + BundleResourceRdb resourceRdb; + BundleResourceKey resourceKey; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + std::vector infos; + bool ans = resourceRdb.GetLauncherAbilityResourceInfoByResourceKey(resourceKey, + static_cast(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON), infos); + EXPECT_FALSE(ans); + + BundleResourceKey resourceKey1; + resourceKey1.bundleName = "bundleName"; + resourceKey1.abilityName = "abilityName"; + ans = resourceRdb.GetLauncherAbilityResourceInfoByResourceKey(resourceKey1, + static_cast(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON), infos); + EXPECT_FALSE(ans); + + BundleResourceKey resourceKey2; + resourceKey2.bundleName = "bundleName"; + resourceKey2.moduleName = "moduleName"; + ans = resourceRdb.GetLauncherAbilityResourceInfoByResourceKey(resourceKey2, + static_cast(ResourceFlag::GET_RESOURCE_INFO_WITH_ICON), infos); + EXPECT_FALSE(ans); +} + +/** + * @tc.number: BmsBundleResourceTest_0236 + * Function: BundleResourceManager + * @tc.name: test BundleResourceManager + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0236, Function | SmallTest | Level0) +{ + auto manager = DelayedSingleton::GetInstance(); + EXPECT_NE(manager, nullptr); + if (manager != nullptr) { + BundleResourceRdb resourceRdb; + ResourceInfo resourceInfo; + resourceInfo.bundleName_ = "bundleName"; + resourceInfo.moduleName_ = "moduleName"; + resourceInfo.abilityName_ = "abilityName"; + resourceInfo.label_ = "label"; + resourceInfo.icon_ = "icon"; + bool ans = resourceRdb.AddResourceInfo(resourceInfo); + EXPECT_TRUE(ans); + + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + std::vector resourceKeys; + resourceKeys.emplace_back(resourceKey); + std::vector infos; + auto ret = manager->GetLauncherAbilityResourceInfosByBundleList(resourceKeys, + static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL), infos); + EXPECT_TRUE(ret); + EXPECT_FALSE(infos.empty()); + if (!infos.empty()) { + EXPECT_EQ(infos[0].bundleName, resourceInfo.bundleName_); + EXPECT_EQ(infos[0].moduleName, resourceInfo.moduleName_); + EXPECT_EQ(infos[0].abilityName, resourceInfo.abilityName_); + EXPECT_EQ(infos[0].icon, resourceInfo.icon_); + EXPECT_EQ(infos[0].label, resourceInfo.label_); + } + + ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey()); + EXPECT_TRUE(ans); + } +} + +/** + * @tc.number: BmsBundleResourceTest_0237 + * Function: BundleResourceManager + * @tc.name: test BundleResourceManager + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0237, Function | SmallTest | Level0) +{ + auto manager = DelayedSingleton::GetInstance(); + EXPECT_NE(manager, nullptr); + if (manager != nullptr) { + BundleResourceRdb resourceRdb; + ResourceInfo resourceInfo; + resourceInfo.bundleName_ = "bundleName"; + resourceInfo.moduleName_ = "moduleName"; + resourceInfo.abilityName_ = "abilityName"; + resourceInfo.label_ = "label"; + resourceInfo.icon_ = "icon"; + bool ans = resourceRdb.AddResourceInfo(resourceInfo); + EXPECT_TRUE(ans); + + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + BundleResourceKey resourceKey1; + resourceKey1.bundleName = "bundleName1"; + resourceKey1.moduleName = "moduleName1"; + resourceKey1.abilityName = "abilityName1"; + std::vector resourceKeys; + resourceKeys.emplace_back(resourceKey); + resourceKeys.emplace_back(resourceKey1); + std::vector infos; + auto ret = manager->GetLauncherAbilityResourceInfosByBundleList(resourceKeys, + static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL), infos); + EXPECT_TRUE(ret); + EXPECT_TRUE(infos.size() == 1); + if (!infos.empty()) { + EXPECT_EQ(infos[0].bundleName, resourceInfo.bundleName_); + EXPECT_EQ(infos[0].moduleName, resourceInfo.moduleName_); + EXPECT_EQ(infos[0].abilityName, resourceInfo.abilityName_); + EXPECT_EQ(infos[0].icon, resourceInfo.icon_); + EXPECT_EQ(infos[0].label, resourceInfo.label_); + } + + ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey()); + EXPECT_TRUE(ans); + } +} + +/** + * @tc.number: BmsBundleResourceTest_0238 + * Function: BundleResourceManager + * @tc.name: test BundleResourceManager + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0238, Function | SmallTest | Level0) +{ + auto manager = DelayedSingleton::GetInstance(); + EXPECT_NE(manager, nullptr); + if (manager != nullptr) { + BundleResourceRdb resourceRdb; + ResourceInfo resourceInfo; + resourceInfo.bundleName_ = "bundleName"; + resourceInfo.moduleName_ = "moduleName"; + resourceInfo.abilityName_ = "abilityName"; + resourceInfo.label_ = "label"; + resourceInfo.icon_ = "icon"; + bool ans = resourceRdb.AddResourceInfo(resourceInfo); + EXPECT_TRUE(ans); + ResourceInfo resourceInfo1; + resourceInfo1.bundleName_ = "bundleName1"; + resourceInfo1.moduleName_ = "moduleName1"; + resourceInfo1.abilityName_ = "abilityName1"; + resourceInfo1.label_ = "label1"; + resourceInfo1.icon_ = "icon1"; + resourceInfo1.appIndex_ = 1; + ans = resourceRdb.AddResourceInfo(resourceInfo1); + EXPECT_TRUE(ans); + + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + BundleResourceKey resourceKey1; + resourceKey1.bundleName = "bundleName1"; + resourceKey1.moduleName = "moduleName1"; + resourceKey1.abilityName = "abilityName1"; + resourceKey1.appIndex = 1; + std::vector resourceKeys; + resourceKeys.emplace_back(resourceKey); + resourceKeys.emplace_back(resourceKey1); + std::vector infos; + auto ret = manager->GetLauncherAbilityResourceInfosByBundleList(resourceKeys, + static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL), infos); + EXPECT_TRUE(ret); + EXPECT_FALSE(infos.empty()); + + auto iter = std::find_if(infos.begin(), infos.end(), [resourceInfo](LauncherAbilityResourceInfo info) { + return resourceInfo.bundleName_ == info.bundleName; + }); + EXPECT_TRUE(iter != infos.end()); + if (iter != infos.end()) { + EXPECT_EQ(iter->bundleName, resourceInfo.bundleName_); + EXPECT_EQ(iter->moduleName, resourceInfo.moduleName_); + EXPECT_EQ(iter->abilityName, resourceInfo.abilityName_); + EXPECT_EQ(iter->icon, resourceInfo.icon_); + EXPECT_EQ(iter->label, resourceInfo.label_); + } + + auto iter1 = std::find_if(infos.begin(), infos.end(), [resourceInfo1](LauncherAbilityResourceInfo info) { + return resourceInfo1.bundleName_ == info.bundleName; + }); + EXPECT_TRUE(iter1 != infos.end()); + if (iter1 != infos.end()) { + EXPECT_EQ(iter1->bundleName, resourceInfo1.bundleName_); + EXPECT_EQ(iter1->moduleName, resourceInfo1.moduleName_); + EXPECT_EQ(iter1->abilityName, resourceInfo1.abilityName_); + EXPECT_EQ(iter1->icon, resourceInfo1.icon_); + EXPECT_EQ(iter1->label, resourceInfo1.label_); + EXPECT_EQ(iter1->appIndex, resourceInfo1.appIndex_); + } + + ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey()); + EXPECT_TRUE(ans); + ans = resourceRdb.DeleteResourceInfo(resourceInfo1.GetKey()); + EXPECT_TRUE(ans); + } +} + +/** + * @tc.number: BmsBundleResourceTest_0239 + * Function: GetLauncherAbilityResourceInfosByBundleList + * @tc.name: test BundleResourceHostImpl + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0239, Function | SmallTest | Level0) +{ + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + std::vector resourceKeys; + resourceKeys.emplace_back(resourceKey); + std::vector infos; + std::shared_ptr bundleResourceHostImpl = std::make_shared(); + ErrCode ret = bundleResourceHostImpl->GetLauncherAbilityResourceInfosByBundleList(resourceKeys, + static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL), infos); + EXPECT_EQ(ret, ERR_BUNDLE_MANAGER_INTERNAL_ERROR); +} +/** + * @tc.number: BmsBundleResourceTest_0240 + * Function: GetLauncherAbilityResourceInfosByBundleList + * @tc.name: test BundleResourceHostImpl + * @tc.desc: 1. system running normally + * 2. test GetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(BmsBundleResourceTest, BmsBundleResourceTest_0240, Function | SmallTest | Level0) +{ + BundleResourceRdb resourceRdb; + ResourceInfo resourceInfo; + resourceInfo.bundleName_ = "bundleName"; + resourceInfo.moduleName_ = "moduleName"; + resourceInfo.abilityName_ = "abilityName"; + resourceInfo.label_ = "label"; + resourceInfo.icon_ = "icon"; + bool ans = resourceRdb.AddResourceInfo(resourceInfo); + EXPECT_TRUE(ans); + ResourceInfo resourceInfo1; + resourceInfo1.bundleName_ = "bundleName1"; + resourceInfo1.moduleName_ = "moduleName1"; + resourceInfo1.abilityName_ = "abilityName1"; + resourceInfo1.label_ = "label1"; + resourceInfo1.icon_ = "icon1"; + resourceInfo1.appIndex_ = 1; + ans = resourceRdb.AddResourceInfo(resourceInfo1); + EXPECT_TRUE(ans); + + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + BundleResourceKey resourceKey1; + resourceKey1.bundleName = "bundleName1"; + resourceKey1.moduleName = "moduleName1"; + resourceKey1.abilityName = "abilityName1"; + resourceKey1.appIndex = 1; + std::vector resourceKeys; + resourceKeys.emplace_back(resourceKey); + resourceKeys.emplace_back(resourceKey1); + std::vector infos; + std::shared_ptr bundleResourceHostImpl = std::make_shared(); + ErrCode ret = bundleResourceHostImpl->GetLauncherAbilityResourceInfosByBundleList(resourceKeys, + static_cast(ResourceFlag::GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL), infos); + EXPECT_EQ(ret, ERR_OK); + + ans = resourceRdb.DeleteResourceInfo(resourceInfo.GetKey()); + EXPECT_TRUE(ans); + ans = resourceRdb.DeleteResourceInfo(resourceInfo1.GetKey()); + EXPECT_TRUE(ans); +} } // OHOS diff --git a/test/systemtest/common/bms/acts_bms_kit_system_test/acts_bms_kit_system_test.cpp b/test/systemtest/common/bms/acts_bms_kit_system_test/acts_bms_kit_system_test.cpp index 18b2b6d445e1016c7b027cf4445c871c2e46f90d..a3d007f7c651351c245728addc30842ea28bcc1d 100644 --- a/test/systemtest/common/bms/acts_bms_kit_system_test/acts_bms_kit_system_test.cpp +++ b/test/systemtest/common/bms/acts_bms_kit_system_test/acts_bms_kit_system_test.cpp @@ -29,6 +29,7 @@ #include "bundle_installer_proxy.h" #include "bundle_mgr_proxy.h" #include "bundle_resource_info.h" +#include "bundle_resource_key.h" #include "bundle_resource_proxy.h" #include "bundle_status_callback_host.h" #include "bundle_pack_info.h" @@ -9337,6 +9338,33 @@ HWTEST_F(ActsBmsKitSystemTest, GetAllLauncherAbilityResourceInfo_0001, Function } } +/** + * @tc.number: GetLauncherAbilityResourceInfosByBundleList_0001 + * @tc.name: test BundleMgrProxy + * @tc.desc: 1.call GetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(ActsBmsKitSystemTest, GetLauncherAbilityResourceInfosByBundleList_0001, Function | SmallTest | Level1) +{ + sptr bundleMgrProxy = GetBundleMgrProxy(); + EXPECT_NE(bundleMgrProxy, nullptr); + if (bundleMgrProxy != nullptr) { + auto proxy = bundleMgrProxy->GetBundleResourceProxy(); + EXPECT_NE(proxy, nullptr); + if (proxy != nullptr) { + BundleResourceKey resourceKey; + resourceKey.bundleName = "bundleName"; + resourceKey.moduleName = "moduleName"; + resourceKey.abilityName = "abilityName"; + std::vector resourceKeys; + resourceKeys.emplace_back(resourceKey); + std::vector infos; + ErrCode ret = proxy->GetLauncherAbilityResourceInfosByBundleList(resourceKeys, + static_cast(ResourceFlag::GET_RESOURCE_INFO_ALL), infos); + EXPECT_EQ(ret, ERR_BUNDLE_MANAGER_PERMISSION_DENIED); + } + } +} + /** * @tc.number: SendRequest_0001 * @tc.name: test BundleMgrProxy diff --git a/test/systemtest/common/bms/bms_bundle_resource_host_test/bms_bundle_resource_host_test.cpp b/test/systemtest/common/bms/bms_bundle_resource_host_test/bms_bundle_resource_host_test.cpp index 1306aff987e15454f712f3ef6271f204b4d0fb83..400f77cb27ffc9f2f574503cd343f898ae7daeee 100755 --- a/test/systemtest/common/bms/bms_bundle_resource_host_test/bms_bundle_resource_host_test.cpp +++ b/test/systemtest/common/bms/bms_bundle_resource_host_test/bms_bundle_resource_host_test.cpp @@ -138,5 +138,21 @@ HWTEST_F(BmsBundleResourceHostTest, HandleGetAllLauncherAbilityResourceInfo_0100 ErrCode res = bundleResourceHost.HandleGetAllLauncherAbilityResourceInfo(data, reply); EXPECT_EQ(res, ERR_OK); } + +/** + * @tc.number: HandleGetLauncherAbilityResourceInfosByBundleList_0100 + * @tc.name: test the HandleGetLauncherAbilityResourceInfosByBundleList + * @tc.desc: 1. system running normally + * 2. test HandleGetLauncherAbilityResourceInfosByBundleList + */ +HWTEST_F(BmsBundleResourceHostTest, HandleGetLauncherAbilityResourceInfosByBundleList_0100, + Function | MediumTest | Level1) +{ + BundleResourceHost bundleResourceHost; + MessageParcel data; + MessageParcel reply; + ErrCode res = bundleResourceHost.HandleGetLauncherAbilityResourceInfosByBundleList(data, reply); + EXPECT_EQ(res, ERR_OK); +} } // AppExecFwk } // OHOS \ No newline at end of file