From 2ed0098d9a384d3e3cfb5c2b03d2ac5850716f90 Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Mon, 8 Sep 2025 10:27:16 +0800 Subject: [PATCH] bm app Signed-off-by: lanhaoyu --- ...bundle_framework_core_ipc_interface_code.h | 1 + .../include/bundlemgr/bundle_mgr_host.h | 1 + .../include/bundlemgr/bundle_mgr_interface.h | 5 ++++ .../include/bundlemgr/bundle_mgr_proxy.h | 2 ++ .../src/bundlemgr/bundle_mgr_host.cpp | 10 +++++++ .../src/bundlemgr/bundle_mgr_proxy.cpp | 20 +++++++++++++ services/bundlemgr/include/bundle_data_mgr.h | 3 ++ .../bundlemgr/include/bundle_mgr_host_impl.h | 1 + services/bundlemgr/src/bundle_data_mgr.cpp | 25 ++++++++++++++++ .../bundlemgr/src/bundle_mgr_host_impl.cpp | 15 ++++++++++ .../bms_bundle_data_mgr_test.cpp | 12 ++++++++ .../bms_bundle_kit_service_test.cpp | 27 +++++++++++++++++ .../bms_bundle_manager_test_three.cpp | 30 +++++++++++++++++++ 13 files changed, 152 insertions(+) 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 be59b80bd8..1e941692e9 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 @@ -228,6 +228,7 @@ enum class BundleMgrInterfaceCode : uint32_t { IS_DEBUGGABLE_APPLICATION = 202, GET_ALL_BUNDLE_NAMES = 203, GET_ABILITY_RESOURCE_INFO = 204, + CHECK_DEVICE_TYPE = 205, }; /* SAID: 401-85 Interface No.85 subservice also provides the following interfaces */ diff --git a/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_host.h b/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_host.h index d63e75a68e..3163c91a2b 100644 --- a/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_host.h +++ b/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_host.h @@ -949,6 +949,7 @@ private: * @return Returns ERR_OK if called successfully; returns error code otherwise. */ ErrCode HandleGetAbilityResourceInfo(MessageParcel &data, MessageParcel &reply); + ErrCode HandleCheckDeviceType(MessageParcel &data, MessageParcel &reply); private: /** * @brief Write a parcelabe vector objects to the proxy node. diff --git a/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_interface.h b/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_interface.h index b2968a5969..0812f6391b 100644 --- a/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_interface.h +++ b/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_interface.h @@ -1890,6 +1890,11 @@ public: { return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; } + + virtual ErrCode CheckDeviceType(BundleInfo &bundleInfo) + { + return ERR_APPEXECFWK_SERVICE_INTERNAL_ERROR; + } }; #define WRITE_PARCEL(func) \ diff --git a/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_proxy.h b/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_proxy.h index 06878c3b91..795f76e198 100644 --- a/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_proxy.h +++ b/interfaces/inner_api/appexecfwk_core/include/bundlemgr/bundle_mgr_proxy.h @@ -1279,6 +1279,8 @@ public: virtual ErrCode GetTestRunner(const std::string &bundleName, const std::string &moduleName, ModuleTestRunner &testRunner) override; + + virtual ErrCode CheckDeviceType(BundleInfo &bundleInfo) override; private: /** * @brief Send a command message from the proxy object. diff --git a/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_host.cpp b/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_host.cpp index bcbfc8bb7a..287ef9e5bc 100644 --- a/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_host.cpp +++ b/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_host.cpp @@ -708,6 +708,9 @@ int BundleMgrHost::OnRemoteRequest(uint32_t code, MessageParcel &data, MessagePa case static_cast(BundleMgrInterfaceCode::GET_ABILITY_RESOURCE_INFO): errCode = HandleGetAbilityResourceInfo(data, reply); break; + case static_cast(BundleMgrInterfaceCode::CHECK_DEVICE_TYPE): + errCode = HandleCheckDeviceType(data, reply); + break; default : APP_LOGW("bundleMgr host receives unknown code %{public}u", code); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -5030,5 +5033,12 @@ ErrCode BundleMgrHost::HandleGetAbilityResourceInfo(MessageParcel &data, Message } return ERR_OK; } + +ErrCode BundleMgrHost::HandleCheckDeviceType(MessageParcel &data, MessageParcel &reply) +{ + HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr); + std::unique_ptr bundleInfo(data.ReadParcelable()); + return CheckDeviceType(*bundleInfo); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_proxy.cpp b/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_proxy.cpp index f973df1569..36f93d2757 100644 --- a/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_proxy.cpp +++ b/interfaces/inner_api/appexecfwk_core/src/bundlemgr/bundle_mgr_proxy.cpp @@ -6587,5 +6587,25 @@ ErrCode BundleMgrProxy::GetTestRunner( } return ERR_OK; } + +ErrCode BundleMgrProxy::CheckDeviceType(BundleInfo &bundleInfo) +{ + HITRACE_METER_NAME_EX(HITRACE_LEVEL_INFO, HITRACE_TAG_APP, __PRETTY_FUNCTION__, nullptr); + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + APP_LOGE("CheckDeviceType write InterfaceToken fail"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + if (!data.WriteParcelable(&bundleInfo)) { + APP_LOGE("CheckDeviceType write info failed"); + return ERR_APPEXECFWK_PARCEL_ERROR; + } + + MessageParcel reply; + if (!SendTransactCmd(BundleMgrInterfaceCode::CHECK_DEVICE_TYPE, data, reply)) { + return false; + } + return reply.ReadInt32(); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/include/bundle_data_mgr.h b/services/bundlemgr/include/bundle_data_mgr.h index f473cfd14d..2271f4ce35 100644 --- a/services/bundlemgr/include/bundle_data_mgr.h +++ b/services/bundlemgr/include/bundle_data_mgr.h @@ -39,6 +39,7 @@ #endif #include "bundle_data_storage_interface.h" #include "bundle_event_callback_interface.h" +#include "bundle_install_checker.h" #include "bundle_promise.h" #include "bundle_sandbox_app_helper.h" #include "bundle_state_storage.h" @@ -1189,6 +1190,7 @@ public: ErrCode GetTestRunner(const std::string &bundleName, const std::string &moduleName, ModuleTestRunner &testRunner); ErrCode ImplicitQueryAbilityInfosWithDefault(const Want &want, int32_t flags, int32_t userId, std::vector &abilityInfos, AbilityInfo &defaultAbilityInfo, bool &findDefaultApp); + ErrCode CheckDeviceType(BundleInfo &bundleInfo) const; private: /** @@ -1430,6 +1432,7 @@ private: std::shared_ptr uninstallDataMgr_; std::shared_ptr firstInstallDataMgr_; std::shared_ptr shortcutVisibleStorage_; + std::unique_ptr bundleInstallChecker_ = nullptr; // use vector because these functions using for IPC, the bundleName may duplicate std::vector> callbackList_; // common event callback diff --git a/services/bundlemgr/include/bundle_mgr_host_impl.h b/services/bundlemgr/include/bundle_mgr_host_impl.h index 08a625633b..c4c489dc35 100644 --- a/services/bundlemgr/include/bundle_mgr_host_impl.h +++ b/services/bundlemgr/include/bundle_mgr_host_impl.h @@ -1197,6 +1197,7 @@ public: ModuleTestRunner &testRunner) override; virtual ErrCode GetAbilityResourceInfo(const std::string &fileType, std::vector &launcherAbilityResourceInfos) override; + virtual ErrCode CheckDeviceType(BundleInfo &bundleInfo) override; private: bool GetLabelByBundleName(const std::string &bundleName, int32_t userId, std::string &label); diff --git a/services/bundlemgr/src/bundle_data_mgr.cpp b/services/bundlemgr/src/bundle_data_mgr.cpp index 7d0cb071f6..eddefc5614 100644 --- a/services/bundlemgr/src/bundle_data_mgr.cpp +++ b/services/bundlemgr/src/bundle_data_mgr.cpp @@ -156,6 +156,7 @@ BundleDataMgr::BundleDataMgr() routerStorage_ = std::make_shared(); uninstallDataMgr_ = std::make_shared(); firstInstallDataMgr_ = std::make_shared(); + bundleInstallChecker_ = std::make_unique(); baseAppUid_ = system::GetIntParameter("const.product.baseappid", Constants::BASE_APP_UID); if (baseAppUid_ < Constants::BASE_APP_UID || baseAppUid_ >= MAX_APP_UID) { baseAppUid_ = Constants::BASE_APP_UID; @@ -11440,5 +11441,29 @@ ErrCode BundleDataMgr::ImplicitQueryAbilityInfosWithDefault(const Want &want, in #endif return ERR_OK; } + +ErrCode BundleDataMgr::CheckDeviceType(BundleInfo &bundleInfo) const +{ + if (bundleInfo.hapModuleInfos.empty()) { + return ERR_BUNDLE_MANAGER_PARAM_ERROR; + } + std::unordered_map innerBundleInfos; + for (auto info : bundleInfo.hapModuleInfos) { + InnerModuleInfo innerModuleInfo; + innerModuleInfo.deviceTypes = info.deviceTypes; + innerModuleInfo.requiredDeviceFeatures = info.requiredDeviceFeatures; + std::map innerModuleInfos = { { bundleInfo.mainEntry, innerModuleInfo } }; + InnerBundleInfo innerBundleInfo; + innerBundleInfo.AddInnerModuleInfo(innerModuleInfos); + innerBundleInfo.SetCurrentModulePackage(bundleInfo.mainEntry); + innerBundleInfos.emplace(bundleInfo.name, innerBundleInfo); + } + + if (bundleInstallChecker_ == nullptr) { + APP_LOGW("bundleInstallChecker_ is null"); + return ERR_APPEXECFWK_NULL_PTR; + } + return bundleInstallChecker_->CheckDeviceType(innerBundleInfos); +} } // 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 42df40ea52..fb446d094e 100644 --- a/services/bundlemgr/src/bundle_mgr_host_impl.cpp +++ b/services/bundlemgr/src/bundle_mgr_host_impl.cpp @@ -6256,5 +6256,20 @@ ErrCode BundleMgrHostImpl::GetAbilityResourceInfo(const std::string &fileType, APP_LOGI("GetAbilityResourceInfo end, size: %{public}zu", launcherAbilityResourceInfos.size()); return ERR_OK; } + +ErrCode BundleMgrHostImpl::CheckDeviceType(BundleInfo &bundleInfo) +{ + if (!BundlePermissionMgr::IsSystemApp()) { + APP_LOGE("non-system app calling system api"); + return ERR_BUNDLE_MANAGER_SYSTEM_API_DENIED; + } + + auto dataMgr = GetDataMgrFromService(); + if (dataMgr == nullptr) { + APP_LOGE("dataMgr is nullptr"); + return ERR_APPEXECFWK_NULL_PTR; + } + return dataMgr->CheckDeviceType(bundleInfo); +} } // namespace AppExecFwk } // namespace OHOS 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 7c69becbd4..231d7f76eb 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 @@ -4703,4 +4703,16 @@ HWTEST_F(BmsBundleDataMgrTest, GetTestRunner_0300, Function | MediumTest | Level MockUninstallBundle(BUNDLE_NAME_TEST); } + +/** + * @tc.number: CheckDeviceType_0100 + * @tc.name: CheckDeviceType_0100 + * @tc.desc: test CheckDeviceType + */ +HWTEST_F(BmsBundleDataMgrTest, CheckDeviceType_0100, Function | MediumTest | Level1) +{ + BundleInfo bundleInfo; + ErrCode ret = GetBundleDataMgr()->CheckDeviceType(bundleInfo); + EXPECT_EQ(ret, ERR_BUNDLE_MANAGER_PARAM_ERROR); +} } // OHOS diff --git a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp index 6360199182..b4a0eb2f91 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_kit_service_test/bms_bundle_kit_service_test.cpp @@ -9284,4 +9284,31 @@ HWTEST_F(BmsBundleKitServiceTest, GetTestRunner_0100, Function | SmallTest | Lev EXPECT_EQ(ret, ERR_BUNDLE_MANAGER_PERMISSION_DENIED); MockUninstallBundle(BUNDLE_NAME_DEMO); } + +/** + * @tc.number: CheckDeviceType_0100 + * @tc.name: CheckDeviceType + * @tc.desc: test BundleMgrProxy interface CheckDeviceType. + */ +HWTEST_F(BmsBundleKitServiceTest, CheckDeviceType_0100, Function | SmallTest | Level1) +{ + sptr bundleMgrProxy = GetBundleMgrProxy(); + ASSERT_NE(bundleMgrProxy, nullptr); + BundleInfo bundleInfo; + ErrCode ret = bundleMgrProxy->CheckDeviceType(bundleInfo); + EXPECT_EQ(ret, ERR_OK); +} + +/** + * @tc.number: CheckDeviceType_0200 + * @tc.name: Test CheckDeviceType + * @tc.desc: 1.Test the CheckDeviceType by BundleMgrHostImpl + */ +HWTEST_F(BmsBundleKitServiceTest, CheckDeviceType_0200, Function | SmallTest | Level1) +{ + auto hostImpl = std::make_unique(); + BundleInfo bundleInfo; + ErrCode ret = hostImpl->CheckDeviceType(bundleInfo); + EXPECT_EQ(ret, ERR_BUNDLE_MANAGER_PARAM_ERROR); +} } diff --git a/services/bundlemgr/test/unittest/bms_bundle_manager_test/bms_bundle_manager_test_three.cpp b/services/bundlemgr/test/unittest/bms_bundle_manager_test/bms_bundle_manager_test_three.cpp index 6db3cc0235..727f267236 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_manager_test/bms_bundle_manager_test_three.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_manager_test/bms_bundle_manager_test_three.cpp @@ -2207,4 +2207,34 @@ HWTEST_F(BmsBundleManagerTest3, ImplicitQueryAbilityInfosWithDefault_0001, Funct want, 0, USERID, abilityInfos, defaultAbilityInfo, findDefaultApp); EXPECT_EQ(testRet, ERR_BUNDLE_MANAGER_ABILITY_INFO_NOT_FOUND); } + +/** +* @tc.number: CheckDeviceType_0100 +* @tc.name: CheckDeviceType_0100 +* @tc.desc: test CheckDeviceType +*/ +HWTEST_F(BmsBundleManagerTest3, CheckDeviceType_0100, Function | MediumTest | Level1) +{ + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + bundleInfo.hapModuleInfos.emplace_back(hapModuleInfo); + ErrCode ret = GetBundleDataMgr()->CheckDeviceType(bundleInfo); + EXPECT_EQ(ret, ERR_OK); +} + +/** +* @tc.number: CheckDeviceType_0200 +* @tc.name: CheckDeviceType_0200 +* @tc.desc: test CheckDeviceType +*/ +HWTEST_F(BmsBundleManagerTest3, CheckDeviceType_0200, Function | MediumTest | Level1) +{ + BundleInfo bundleInfo; + HapModuleInfo hapModuleInfo; + bundleInfo.hapModuleInfos.emplace_back(hapModuleInfo); + auto bundleDataMgr = GetBundleDataMgr(); + bundleDataMgr->bundleInstallChecker_ = nullptr; + ErrCode ret = bundleDataMgr->CheckDeviceType(bundleInfo); + EXPECT_EQ(ret, ERR_APPEXECFWK_NULL_PTR); +} } // OHOS \ No newline at end of file -- Gitee