From 90744d0e76605e3654204a4a84bf5059ebb91e25 Mon Sep 17 00:00:00 2001 From: yaobaohua Date: Mon, 24 Apr 2023 11:03:25 +0800 Subject: [PATCH] yaobaohua disallow install hap list Signed-off-by: yaobaohua --- .../include/bundle_manager_proxy.h | 14 +- .../src/bundle_manager_proxy.cpp | 75 ++++++++- .../include/enterprise_device_mgr_proxy.h | 2 + .../inner_api/common/include/policy_info.h | 1 + .../include/bundle_manager_addon.h | 15 +- .../src/bundle_manager_addon.cpp | 159 +++++++++++++----- services/edm_plugin/BUILD.gn | 5 +- .../disallowed_install_bundles_plugin.h | 42 +++++ .../src/disallowed_install_bundles_plugin.cpp | 127 ++++++++++++++ .../bundle_manager_proxy_test.cpp | 39 +++++ 10 files changed, 428 insertions(+), 51 deletions(-) create mode 100644 services/edm_plugin/include/disallowed_install_bundles_plugin.h create mode 100644 services/edm_plugin/src/disallowed_install_bundles_plugin.cpp diff --git a/interfaces/inner_api/bundle_manager/include/bundle_manager_proxy.h b/interfaces/inner_api/bundle_manager/include/bundle_manager_proxy.h index a67bf07b2..3cf248851 100644 --- a/interfaces/inner_api/bundle_manager/include/bundle_manager_proxy.h +++ b/interfaces/inner_api/bundle_manager/include/bundle_manager_proxy.h @@ -28,10 +28,22 @@ public: int32_t userId); int32_t GetAllowedInstallBundles(AppExecFwk::ElementName &admin, int32_t userId, std::vector &bundles); + int32_t AddDisallowedInstallBundles(AppExecFwk::ElementName &admin, + std::vector &bundles, int32_t userId); + int32_t RemoveDisallowedInstallBundles(AppExecFwk::ElementName &admin, + std::vector &bundles, int32_t userId); + int32_t GetDisallowedInstallBundles(AppExecFwk::ElementName &admin, int32_t userId, + std::vector &bundles); private: - static std::shared_ptr proxy_; static std::shared_ptr instance_; static std::mutex mutexLock_; + + int32_t AddAllowedOrDisallowedInstallBundles(AppExecFwk::ElementName &admin, + std::vector &bundles, int32_t userId, int32_t allowType); + int32_t RemoveAllowedOrDisallowedInstallBundles(AppExecFwk::ElementName &admin, + std::vector &bundles, int32_t userId, int32_t allowType); + int32_t GetAllowedOrDisallowedInstallBundles(AppExecFwk::ElementName &admin, int32_t userId, + std::vector &bundles, int32_t allowType); }; } // namespace EDM } // namespace OHOS diff --git a/interfaces/inner_api/bundle_manager/src/bundle_manager_proxy.cpp b/interfaces/inner_api/bundle_manager/src/bundle_manager_proxy.cpp index b4950c2e0..1277c3124 100644 --- a/interfaces/inner_api/bundle_manager/src/bundle_manager_proxy.cpp +++ b/interfaces/inner_api/bundle_manager/src/bundle_manager_proxy.cpp @@ -37,17 +37,37 @@ std::shared_ptr BundleManagerProxy::GetBundleManagerProxy() return instance_; } -int32_t BundleManagerProxy::AddAllowedInstallBundles(AppExecFwk::ElementName &admin, std::vector &bundles, - int32_t userId) +int32_t BundleManagerProxy::AddAllowedInstallBundles(AppExecFwk::ElementName &admin, + std::vector &bundles, int32_t userId) +{ + return AddAllowedOrDisallowedInstallBundles(admin, bundles, userId, TYPE_ALLOW); +} + +int32_t BundleManagerProxy::AddDisallowedInstallBundles(AppExecFwk::ElementName &admin, + std::vector &bundles, int32_t userId) +{ + return AddAllowedOrDisallowedInstallBundles(admin, bundles, userId, TYPE_DISALLOW); +} + +int32_t BundleManagerProxy::AddAllowedOrDisallowedInstallBundles(AppExecFwk::ElementName &admin, + std::vector &bundles, int32_t userId, int32_t allowType) { - EDMLOGD("BundleManagerProxy::AddAllowedInstallBundles"); + EDMLOGD("BundleManagerProxy::AddAllowedOrDisallowedInstallBundles"); auto proxy = EnterpriseDeviceMgrProxy::GetInstance(); if (proxy == nullptr) { EDMLOGE("can not get EnterpriseDeviceMgrProxy"); return EdmReturnErrCode::SYSTEM_ABNORMALLY; } MessageParcel data; - std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, ALLOWED_INSTALL_BUNDLES); + std::uint32_t funcCode = 0; + if (allowType == TYPE_ALLOW) { + funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, ALLOWED_INSTALL_BUNDLES); + } else if (allowType == TYPE_DISALLOW) { + funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, DISALLOWED_INSTALL_BUNDLES); + } else { + EDMLOGE("can not get allow type"); + return EdmReturnErrCode::SYSTEM_ABNORMALLY; + } data.WriteInterfaceToken(DESCRIPTOR); data.WriteInt32(HAS_USERID); data.WriteInt32(userId); @@ -59,14 +79,34 @@ int32_t BundleManagerProxy::AddAllowedInstallBundles(AppExecFwk::ElementName &ad int32_t BundleManagerProxy::RemoveAllowedInstallBundles(AppExecFwk::ElementName &admin, std::vector &bundles, int32_t userId) { - EDMLOGD("BundleManagerProxy::RemoveAllowedInstallBundles"); + return RemoveAllowedOrDisallowedInstallBundles(admin, bundles, userId, TYPE_ALLOW); +} + +int32_t BundleManagerProxy::RemoveDisallowedInstallBundles(AppExecFwk::ElementName &admin, + std::vector &bundles, int32_t userId) +{ + return RemoveAllowedOrDisallowedInstallBundles(admin, bundles, userId, TYPE_DISALLOW); +} + +int32_t BundleManagerProxy::RemoveAllowedOrDisallowedInstallBundles(AppExecFwk::ElementName &admin, + std::vector &bundles, int32_t userId, int32_t allowType) +{ + EDMLOGD("BundleManagerProxy::RemoveAllowedOrDisallowedInstallBundles"); auto proxy = EnterpriseDeviceMgrProxy::GetInstance(); if (proxy == nullptr) { EDMLOGE("can not get EnterpriseDeviceMgrProxy"); return EdmReturnErrCode::SYSTEM_ABNORMALLY; } MessageParcel data; - std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, ALLOWED_INSTALL_BUNDLES); + std::uint32_t funcCode = 0; + if (allowType == TYPE_ALLOW) { + funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, ALLOWED_INSTALL_BUNDLES); + } else if (allowType == TYPE_DISALLOW) { + funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, DISALLOWED_INSTALL_BUNDLES); + } else { + EDMLOGE("can not get allow type"); + return EdmReturnErrCode::SYSTEM_ABNORMALLY; + } data.WriteInterfaceToken(DESCRIPTOR); data.WriteInt32(HAS_USERID); data.WriteInt32(userId); @@ -78,7 +118,19 @@ int32_t BundleManagerProxy::RemoveAllowedInstallBundles(AppExecFwk::ElementName int32_t BundleManagerProxy::GetAllowedInstallBundles(AppExecFwk::ElementName &admin, int32_t userId, std::vector &bundles) { - EDMLOGD("BundleManagerProxy::GetAllowedInstallBundles"); + return GetAllowedOrDisallowedInstallBundles(admin, userId, bundles, TYPE_ALLOW); +} + +int32_t BundleManagerProxy::GetDisallowedInstallBundles(AppExecFwk::ElementName &admin, int32_t userId, + std::vector &bundles) +{ + return GetAllowedOrDisallowedInstallBundles(admin, userId, bundles, TYPE_DISALLOW); +} + +int32_t BundleManagerProxy::GetAllowedOrDisallowedInstallBundles(AppExecFwk::ElementName &admin, int32_t userId, + std::vector &bundles, int32_t allowType) +{ + EDMLOGD("BundleManagerProxy::GetAllowedOrDisallowedInstallBundles"); auto proxy = EnterpriseDeviceMgrProxy::GetInstance(); if (proxy == nullptr) { EDMLOGE("can not get EnterpriseDeviceMgrProxy"); @@ -91,7 +143,14 @@ int32_t BundleManagerProxy::GetAllowedInstallBundles(AppExecFwk::ElementName &ad data.WriteInt32(userId); data.WriteInt32(HAS_ADMIN); data.WriteParcelable(&admin); - proxy->GetPolicy(ALLOWED_INSTALL_BUNDLES, data, reply); + if (allowType == TYPE_ALLOW) { + proxy->GetPolicy(ALLOWED_INSTALL_BUNDLES, data, reply); + } else if (allowType == TYPE_DISALLOW) { + proxy->GetPolicy(DISALLOWED_INSTALL_BUNDLES, data, reply); + } else { + EDMLOGE("allow type error"); + return EdmReturnErrCode::SYSTEM_ABNORMALLY; + } int32_t ret = ERR_INVALID_VALUE; bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK); if (!blRes) { diff --git a/interfaces/inner_api/common/include/enterprise_device_mgr_proxy.h b/interfaces/inner_api/common/include/enterprise_device_mgr_proxy.h index c87898ef2..90045dbe3 100644 --- a/interfaces/inner_api/common/include/enterprise_device_mgr_proxy.h +++ b/interfaces/inner_api/common/include/enterprise_device_mgr_proxy.h @@ -30,6 +30,8 @@ constexpr int32_t HAS_ADMIN = 0; constexpr int32_t WITHOUT_ADMIN = 1; constexpr int32_t WITHOUT_USERID = 0; constexpr int32_t HAS_USERID = 1; +constexpr int32_t TYPE_ALLOW = 0; +constexpr int32_t TYPE_DISALLOW = 1; class EnterpriseDeviceMgrProxy { public: diff --git a/interfaces/inner_api/common/include/policy_info.h b/interfaces/inner_api/common/include/policy_info.h index 3d0586daa..7b3781213 100644 --- a/interfaces/inner_api/common/include/policy_info.h +++ b/interfaces/inner_api/common/include/policy_info.h @@ -36,6 +36,7 @@ enum { DISALLOW_MODIFY_DATETIME = 1012, SET_WIFI_PROFILE = 1013, DISALLOW_PRINTING = 1014, + DISALLOWED_INSTALL_BUNDLES = 1015, POLICY_CODE_END = 3000, }; diff --git a/interfaces/kits/bundle_manager/include/bundle_manager_addon.h b/interfaces/kits/bundle_manager/include/bundle_manager_addon.h index 6b1c0ff12..ba8366414 100644 --- a/interfaces/kits/bundle_manager/include/bundle_manager_addon.h +++ b/interfaces/kits/bundle_manager/include/bundle_manager_addon.h @@ -27,7 +27,7 @@ namespace OHOS { namespace EDM { -struct AsyncAllowedInstallBundlesCallbackInfo : AsyncCallbackInfo { +struct AsyncInstallBundlesCallbackInfo : AsyncCallbackInfo { OHOS::AppExecFwk::ElementName elementName; std::vector bundles; int32_t userId = 0; @@ -43,16 +43,25 @@ public: static napi_value AddAllowedInstallBundles(napi_env env, napi_callback_info info); static napi_value RemoveAllowedInstallBundles(napi_env env, napi_callback_info info); static napi_value GetAllowedInstallBundles(napi_env env, napi_callback_info info); + static napi_value AddDisallowedInstallBundles(napi_env env, napi_callback_info info); + static napi_value RemoveDisallowedInstallBundles(napi_env env, napi_callback_info info); + static napi_value GetDisallowedInstallBundles(napi_env env, napi_callback_info info); private: - static napi_value AddOrRemovellowedInstallBundles(napi_env env, napi_callback_info info, + static napi_value AddOrRemoveInstallBundles(napi_env env, napi_callback_info info, + const std::string &workName, napi_async_execute_callback execute); + static napi_value GetAllowedOrDisallowedInstallBundles(napi_env env, napi_callback_info info, const std::string &workName, napi_async_execute_callback execute); static void NativeAddAllowedInstallBundles(napi_env env, void *data); static void NativeRemoveAllowedInstallBundles(napi_env env, void *data); static void NativeGetAllowedInstallBundles(napi_env env, void *data); - static bool CheckAddAllowedInstallBundlesParamType(napi_env env, size_t argc, + static void NativeAddDisallowedInstallBundles(napi_env env, void *data); + static void NativeRemoveDisallowedInstallBundles(napi_env env, void *data); + static void NativeGetDisallowedInstallBundles(napi_env env, void *data); + + static bool CheckAddInstallBundlesParamType(napi_env env, size_t argc, napi_value* argv, bool &hasCallback, bool &hasUserId); static std::shared_ptr bundleManagerProxy_; }; diff --git a/interfaces/kits/bundle_manager/src/bundle_manager_addon.cpp b/interfaces/kits/bundle_manager/src/bundle_manager_addon.cpp index 8ad3fac6f..1ed32e908 100644 --- a/interfaces/kits/bundle_manager/src/bundle_manager_addon.cpp +++ b/interfaces/kits/bundle_manager/src/bundle_manager_addon.cpp @@ -26,6 +26,9 @@ napi_value BundleManagerAddon::Init(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("addAllowedInstallBundles", AddAllowedInstallBundles), DECLARE_NAPI_FUNCTION("removeAllowedInstallBundles", RemoveAllowedInstallBundles), DECLARE_NAPI_FUNCTION("getAllowedInstallBundles", GetAllowedInstallBundles), + DECLARE_NAPI_FUNCTION("addDisallowedInstallBundles", AddDisallowedInstallBundles), + DECLARE_NAPI_FUNCTION("removeDisallowedInstallBundles", RemoveDisallowedInstallBundles), + DECLARE_NAPI_FUNCTION("getDisallowedInstallBundles", GetDisallowedInstallBundles), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property)); return exports; @@ -34,6 +37,20 @@ napi_value BundleManagerAddon::Init(napi_env env, napi_value exports) napi_value BundleManagerAddon::GetAllowedInstallBundles(napi_env env, napi_callback_info info) { EDMLOGI("NAPI_GetAllowedInstallBundles called"); + return GetAllowedOrDisallowedInstallBundles(env, info, "GetAllowedInstallBundles", + NativeGetAllowedInstallBundles); +} + +napi_value BundleManagerAddon::GetDisallowedInstallBundles(napi_env env, napi_callback_info info) +{ + EDMLOGI("NAPI_GetDisallowedInstallBundles called"); + return GetAllowedOrDisallowedInstallBundles(env, info, "GetDisallowedInstallBundles", + NativeGetDisallowedInstallBundles); +} + +napi_value BundleManagerAddon::GetAllowedOrDisallowedInstallBundles(napi_env env, napi_callback_info info, + const std::string &workName, napi_async_execute_callback execute) +{ size_t argc = ARGS_SIZE_THREE; napi_value argv[ARGS_SIZE_THREE] = {nullptr}; napi_value thisArg = nullptr; @@ -41,18 +58,18 @@ napi_value BundleManagerAddon::GetAllowedInstallBundles(napi_env env, napi_callb NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data)); bool hasCallback = false; bool hasUserId = false; - auto asyncCallbackInfo = new (std::nothrow) AsyncAllowedInstallBundlesCallbackInfo(); + auto asyncCallbackInfo = new (std::nothrow) AsyncInstallBundlesCallbackInfo(); if (asyncCallbackInfo == nullptr) { return nullptr; } - std::unique_ptr callbackPtr {asyncCallbackInfo}; + std::unique_ptr callbackPtr {asyncCallbackInfo}; ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_ONE, "Parameter count error"); ASSERT_AND_THROW_PARAM_ERROR(env, CheckAdminWithUserIdParamType(env, argc, argv, hasCallback, hasUserId), "Parameter type error"); ASSERT_AND_THROW_PARAM_ERROR(env, ParseElementName(env, asyncCallbackInfo->elementName, argv[ARR_INDEX_ZERO]), "Parameter want error"); - EDMLOGD("GetAllowedInstallBundles::asyncCallbackInfo->elementName.bundlename %{public}s, " + EDMLOGD("GetAllowedOrDisallowedInstallBundles asyncCallbackInfo->elementName.bundlename %{public}s, " "asyncCallbackInfo->abilityname:%{public}s", asyncCallbackInfo->elementName.GetBundleName().c_str(), asyncCallbackInfo->elementName.GetAbilityName().c_str()); @@ -65,8 +82,8 @@ napi_value BundleManagerAddon::GetAllowedInstallBundles(napi_env env, napi_callb if (hasCallback) { napi_create_reference(env, argv[argc - 1], NAPI_RETURN_ONE, &asyncCallbackInfo->callback); } - napi_value asyncWorkReturn = HandleAsyncWork(env, asyncCallbackInfo, "GetAllowedInstallBundles", - NativeGetAllowedInstallBundles, NativeArrayStringCallbackComplete); + napi_value asyncWorkReturn = HandleAsyncWork(env, asyncCallbackInfo, workName, + execute, NativeArrayStringCallbackComplete); callbackPtr.release(); return asyncWorkReturn; } @@ -78,55 +95,66 @@ void BundleManagerAddon::NativeGetAllowedInstallBundles(napi_env env, void *data EDMLOGE("data is nullptr"); return; } - AsyncAllowedInstallBundlesCallbackInfo *asyncCallbackInfo = - static_cast(data); + AsyncInstallBundlesCallbackInfo *asyncCallbackInfo = + static_cast(data); auto proxy_ = BundleManagerProxy::GetBundleManagerProxy(); if (proxy_ == nullptr) { - EDMLOGE("can not get EnterpriseDeviceMgrProxy"); + EDMLOGE("can not get BundleManagerProxy"); return; } - asyncCallbackInfo->ret = proxy_->GetAllowedInstallBundles(asyncCallbackInfo->elementName, asyncCallbackInfo->userId, - asyncCallbackInfo->arrayStringRet); + asyncCallbackInfo->ret = proxy_->GetAllowedInstallBundles(asyncCallbackInfo->elementName, + asyncCallbackInfo->userId, asyncCallbackInfo->arrayStringRet); +} + +void BundleManagerAddon::NativeGetDisallowedInstallBundles(napi_env env, void *data) +{ + EDMLOGI("NAPI_NativeGetDisallowedInstallBundles called"); + if (data == nullptr) { + EDMLOGE("data is nullptr"); + return; + } + AsyncInstallBundlesCallbackInfo *asyncCallbackInfo = + static_cast(data); + auto proxy_ = BundleManagerProxy::GetBundleManagerProxy(); + if (proxy_ == nullptr) { + EDMLOGE("can not get BundleManagerProxy"); + return; + } + + asyncCallbackInfo->ret = proxy_->GetDisallowedInstallBundles(asyncCallbackInfo->elementName, + asyncCallbackInfo->userId, asyncCallbackInfo->arrayStringRet); } napi_value BundleManagerAddon::AddAllowedInstallBundles(napi_env env, napi_callback_info info) { - return AddOrRemovellowedInstallBundles(env, info, "AddAllowedInstallBundles", NativeAddAllowedInstallBundles); + return AddOrRemoveInstallBundles(env, info, "AddAllowedInstallBundles", NativeAddAllowedInstallBundles); } napi_value BundleManagerAddon::RemoveAllowedInstallBundles(napi_env env, napi_callback_info info) { - return AddOrRemovellowedInstallBundles(env, info, "RemoveAllowedInstallBundles", NativeRemoveAllowedInstallBundles); + return AddOrRemoveInstallBundles(env, info, "RemoveAllowedInstallBundles", NativeRemoveAllowedInstallBundles); } -void BundleManagerAddon::NativeAddAllowedInstallBundles(napi_env env, void *data) +napi_value BundleManagerAddon::AddDisallowedInstallBundles(napi_env env, napi_callback_info info) { - EDMLOGI("NAPI_NativeAddAllowedInstallBundles called"); - if (data == nullptr) { - EDMLOGE("data is nullptr"); - return; - } - AsyncAllowedInstallBundlesCallbackInfo *asyncCallbackInfo = - static_cast(data); - auto bundleManagerProxy_ = BundleManagerProxy::GetBundleManagerProxy(); - if (bundleManagerProxy_ == nullptr) { - EDMLOGE("can not get BundleManagerProxy"); - return; - } - asyncCallbackInfo->ret = bundleManagerProxy_->AddAllowedInstallBundles(asyncCallbackInfo->elementName, - asyncCallbackInfo->bundles, asyncCallbackInfo->userId); + return AddOrRemoveInstallBundles(env, info, "AddDisallowedInstallBundles", NativeAddDisallowedInstallBundles); } -bool BundleManagerAddon::CheckAddAllowedInstallBundlesParamType(napi_env env, size_t argc, +napi_value BundleManagerAddon::RemoveDisallowedInstallBundles(napi_env env, napi_callback_info info) +{ + return AddOrRemoveInstallBundles(env, info, "RemoveDisallowedInstallBundles", NativeRemoveDisallowedInstallBundles); +} + +bool BundleManagerAddon::CheckAddInstallBundlesParamType(napi_env env, size_t argc, napi_value* argv, bool &hasCallback, bool &hasUserId) { if (!MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object) || !MatchValueType(env, argv[ARR_INDEX_ONE], napi_object)) { - EDMLOGE("CheckAddAllowedInstallBundlesParamType admin or array type check failed"); + EDMLOGE("CheckAddInstallBundlesParamType admin or array type check failed"); return false; } - EDMLOGI("CheckAddAllowedInstallBundlesParamType argc = %{public}zu", argc); + EDMLOGI("CheckAddInstallBundlesParamType argc = %{public}zu", argc); if (argc == ARGS_SIZE_TWO) { hasCallback = false; hasUserId = false; @@ -154,7 +182,7 @@ bool BundleManagerAddon::CheckAddAllowedInstallBundlesParamType(napi_env env, si MatchValueType(env, argv[ARR_INDEX_THREE], napi_function); } -napi_value BundleManagerAddon::AddOrRemovellowedInstallBundles(napi_env env, napi_callback_info info, +napi_value BundleManagerAddon::AddOrRemoveInstallBundles(napi_env env, napi_callback_info info, const std::string &workName, napi_async_execute_callback execute) { size_t argc = ARGS_SIZE_FOUR; @@ -164,13 +192,13 @@ napi_value BundleManagerAddon::AddOrRemovellowedInstallBundles(napi_env env, nap bool hasCallback = false; bool hasUserId = false; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data)); - auto asyncCallbackInfo = new (std::nothrow) AsyncAllowedInstallBundlesCallbackInfo(); + auto asyncCallbackInfo = new (std::nothrow) AsyncInstallBundlesCallbackInfo(); if (asyncCallbackInfo == nullptr) { return nullptr; } - std::unique_ptr callbackPtr {asyncCallbackInfo}; + std::unique_ptr callbackPtr {asyncCallbackInfo}; ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_TWO, "Parameter count error"); - ASSERT_AND_THROW_PARAM_ERROR(env, CheckAddAllowedInstallBundlesParamType(env, argc, argv, hasCallback, hasUserId), + ASSERT_AND_THROW_PARAM_ERROR(env, CheckAddInstallBundlesParamType(env, argc, argv, hasCallback, hasUserId), "Parameter type error"); ASSERT_AND_THROW_PARAM_ERROR(env, ParseElementName(env, asyncCallbackInfo->elementName, argv[ARR_INDEX_ZERO]), "Parameter want error"); @@ -190,6 +218,7 @@ napi_value BundleManagerAddon::AddOrRemovellowedInstallBundles(napi_env env, nap if (hasCallback) { napi_create_reference(env, argv[argc - 1], NAPI_RETURN_ONE, &asyncCallbackInfo->callback); } + EDMLOGD("AddOrRemoveInstallBundles::%{public}s", workName.c_str()); napi_value asyncWorkReturn = HandleAsyncWork(env, asyncCallbackInfo, workName, execute, NativeVoidCallbackComplete); callbackPtr.release(); @@ -203,15 +232,69 @@ void BundleManagerAddon::NativeRemoveAllowedInstallBundles(napi_env env, void *d EDMLOGE("data is nullptr"); return; } - AsyncAllowedInstallBundlesCallbackInfo *asyncCallbackInfo = - static_cast(data); + AsyncInstallBundlesCallbackInfo *asyncCallbackInfo = + static_cast(data); + auto bundleManagerProxy_ = BundleManagerProxy::GetBundleManagerProxy(); + if (bundleManagerProxy_ == nullptr) { + EDMLOGE("can not get BundleManagerProxy"); + return; + } + asyncCallbackInfo->ret = bundleManagerProxy_->RemoveAllowedInstallBundles( + asyncCallbackInfo->elementName, asyncCallbackInfo->bundles, asyncCallbackInfo->userId); +} + +void BundleManagerAddon::NativeRemoveDisallowedInstallBundles(napi_env env, void *data) +{ + EDMLOGI("NAPI_NativeRemoveDisallowedInstallBundles called"); + if (data == nullptr) { + EDMLOGE("data is nullptr"); + return; + } + AsyncInstallBundlesCallbackInfo *asyncCallbackInfo = + static_cast(data); + auto bundleManagerProxy_ = BundleManagerProxy::GetBundleManagerProxy(); + if (bundleManagerProxy_ == nullptr) { + EDMLOGE("can not get BundleManagerProxy"); + return; + } + asyncCallbackInfo->ret = bundleManagerProxy_->RemoveDisallowedInstallBundles( + asyncCallbackInfo->elementName, asyncCallbackInfo->bundles, asyncCallbackInfo->userId); +} + +void BundleManagerAddon::NativeAddAllowedInstallBundles(napi_env env, void *data) +{ + EDMLOGI("NAPI_NativeAddAllowedInstallBundles called"); + if (data == nullptr) { + EDMLOGE("data is nullptr"); + return; + } + AsyncInstallBundlesCallbackInfo *asyncCallbackInfo = + static_cast(data); + auto bundleManagerProxy_ = BundleManagerProxy::GetBundleManagerProxy(); + if (bundleManagerProxy_ == nullptr) { + EDMLOGE("can not get BundleManagerProxy"); + return; + } + asyncCallbackInfo->ret = bundleManagerProxy_->AddAllowedInstallBundles( + asyncCallbackInfo->elementName, asyncCallbackInfo->bundles, asyncCallbackInfo->userId); +} + +void BundleManagerAddon::NativeAddDisallowedInstallBundles(napi_env env, void *data) +{ + EDMLOGI("NAPI_NativeAddDisallowedInstallBundles called"); + if (data == nullptr) { + EDMLOGE("data is nullptr"); + return; + } + AsyncInstallBundlesCallbackInfo *asyncCallbackInfo = + static_cast(data); auto bundleManagerProxy_ = BundleManagerProxy::GetBundleManagerProxy(); if (bundleManagerProxy_ == nullptr) { EDMLOGE("can not get BundleManagerProxy"); return; } - asyncCallbackInfo->ret = bundleManagerProxy_->RemoveAllowedInstallBundles(asyncCallbackInfo->elementName, - asyncCallbackInfo->bundles, asyncCallbackInfo->userId); + asyncCallbackInfo->ret = bundleManagerProxy_->AddDisallowedInstallBundles( + asyncCallbackInfo->elementName, asyncCallbackInfo->bundles, asyncCallbackInfo->userId); } static napi_module g_bundleManagerModule = { diff --git a/services/edm_plugin/BUILD.gn b/services/edm_plugin/BUILD.gn index 383ec649b..97cbc0ea7 100644 --- a/services/edm_plugin/BUILD.gn +++ b/services/edm_plugin/BUILD.gn @@ -106,7 +106,10 @@ edm_plugin_shared_library("network_manager_plugin") { } edm_plugin_shared_library("bundle_manager_plugin") { - sources = [ "$PLUGIN_SRC_PATH/allowed_install_bundles_plugin.cpp" ] + sources = [ + "$PLUGIN_SRC_PATH/allowed_install_bundles_plugin.cpp", + "$PLUGIN_SRC_PATH/disallowed_install_bundles_plugin.cpp", + ] deps = [ "$SUBSYSTEM_DIR/common/native:edm_commom" ] diff --git a/services/edm_plugin/include/disallowed_install_bundles_plugin.h b/services/edm_plugin/include/disallowed_install_bundles_plugin.h new file mode 100644 index 000000000..a099f278c --- /dev/null +++ b/services/edm_plugin/include/disallowed_install_bundles_plugin.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 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 SERVICES_EDM_PLUGIN_INCLUDE_ADDDISALLOWEDINSTALLBUNDLES_PLUGIN_H +#define SERVICES_EDM_PLUGIN_INCLUDE_ADDDISALLOWEDINSTALLBUNDLES_PLUGIN_H + +#include +#include +#include "iplugin_manager.h" +#include "iplugin_template.h" + +namespace OHOS { +namespace EDM { +class DisallowedInstallBundlesPlugin : public PluginSingleton> { +public: + void InitPlugin(std::shared_ptr>> ptr) override; + + ErrCode OnSetPolicy(std::vector &data, std::vector ¤tData, int32_t userId); + ErrCode OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply, int32_t userId) override; + ErrCode OnRemovePolicy(std::vector &data, std::vector ¤tData, int32_t userId); + void OnAdminRemoveDone(const std::string &adminName, std::vector &data, int32_t userId); + + sptr GetAppControlProxy(); +}; +} // namespace EDM +} // namespace OHOS + +#endif // SERVICES_EDM_PLUGIN_INCLUDE_ADDDISALLOWEDINSTALLBUNDLES_PLUGIN_H diff --git a/services/edm_plugin/src/disallowed_install_bundles_plugin.cpp b/services/edm_plugin/src/disallowed_install_bundles_plugin.cpp new file mode 100644 index 000000000..78b4e4534 --- /dev/null +++ b/services/edm_plugin/src/disallowed_install_bundles_plugin.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2023 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 "disallowed_install_bundles_plugin.h" +#include +#include + +#include "app_control/app_control_proxy.h" +#include "array_string_serializer.h" +#include "edm_sys_manager.h" +#include "policy_info.h" + +namespace OHOS { +namespace EDM { +const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(DisallowedInstallBundlesPlugin::GetPlugin()); + +constexpr int32_t MAX_SIZE = 200; + +void DisallowedInstallBundlesPlugin::InitPlugin(std::shared_ptr>> ptr) +{ + EDMLOGD("DisallowedInstallBundlesPlugin InitPlugin..."); + std::string policyName; + POLICY_CODE_TO_NAME(DISALLOWED_INSTALL_BUNDLES, policyName); + ptr->InitAttribute(DISALLOWED_INSTALL_BUNDLES, policyName, "ohos.permission.ENTERPRISE_SET_BUNDLE_INSTALL_POLICY", + IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true); + ptr->SetSerializer(ArrayStringSerializer::GetInstance()); + ptr->SetOnHandlePolicyListener(&DisallowedInstallBundlesPlugin::OnSetPolicy, FuncOperateType::SET); + ptr->SetOnHandlePolicyListener(&DisallowedInstallBundlesPlugin::OnRemovePolicy, FuncOperateType::REMOVE); + ptr->SetOnAdminRemoveDoneListener(&DisallowedInstallBundlesPlugin::OnAdminRemoveDone); +} + +ErrCode DisallowedInstallBundlesPlugin::OnSetPolicy(std::vector &data, std::vector ¤tData, + int32_t userId) +{ + EDMLOGI("DisallowedInstallBundlesPlugin OnSetPolicy userId = %{public}d", userId); + if (data.empty()) { + EDMLOGW("DisallowedInstallBundlesPlugin OnSetPolicy data is empty:"); + return ERR_OK; + } + + std::vector mergeData; + std::sort(data.begin(), data.end()); + std::sort(currentData.begin(), currentData.end()); + std::set_union(data.begin(), data.end(), currentData.begin(), currentData.end(), back_inserter(mergeData)); + + if (mergeData.size() > MAX_SIZE) { + EDMLOGE("DisallowedInstallBundlesPlugin OnSetPolicy data is too large:"); + return EdmReturnErrCode::PARAM_ERROR; + } + + ErrCode res = GetAppControlProxy()-> + AddAppInstallControlRule(data, AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL, userId); + if (res != ERR_OK) { + EDMLOGE("DisallowedInstallBundlesPlugin OnSetPolicyDone Faild %{public}d:", res); + return EdmReturnErrCode::SYSTEM_ABNORMALLY; + } + currentData = mergeData; + return ERR_OK; +} + +ErrCode DisallowedInstallBundlesPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply, + int32_t userId) +{ + EDMLOGI("DisallowedInstallBundlesPlugin OnGetPolicy policyData : %{public}s, userId : %{public}d", + policyData.c_str(), userId); + std::vector bundles; + pluginInstance_->serializer_->Deserialize(policyData, bundles); + reply.WriteInt32(ERR_OK); + reply.WriteInt32(bundles.size()); + reply.WriteStringVector(bundles); + return ERR_OK; +} + +ErrCode DisallowedInstallBundlesPlugin::OnRemovePolicy(std::vector &data, + std::vector ¤tData, int32_t userId) +{ + EDMLOGD("DisallowedInstallBundlesPlugin OnRemovePolicy userId : %{public}d:", userId); + if (data.empty()) { + EDMLOGW("DisallowedInstallBundlesPlugin OnRemovePolicy data is empty:"); + return ERR_OK; + } + + std::vector mergeData; + std::sort(data.begin(), data.end()); + std::sort(currentData.begin(), currentData.end()); + std::set_difference(currentData.begin(), currentData.end(), data.begin(), data.end(), back_inserter(mergeData)); + ErrCode res = GetAppControlProxy()-> + DeleteAppInstallControlRule(AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL, data, userId); + if (res != ERR_OK) { + EDMLOGE("DisallowedInstallBundlesPlugin DeleteAppInstallControlRule OnRemovePolicy faild %{public}d:", res); + return EdmReturnErrCode::SYSTEM_ABNORMALLY; + } + currentData = mergeData; + return ERR_OK; +} + +sptr DisallowedInstallBundlesPlugin::GetAppControlProxy() +{ + auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + sptr proxy = iface_cast(remoteObject); + return proxy->GetAppControlProxy(); +} + +void DisallowedInstallBundlesPlugin::OnAdminRemoveDone(const std::string &adminName, std::vector &data, + int32_t userId) +{ + EDMLOGI("DisallowedInstallBundlesPlugin OnAdminRemoveDone adminName : %{public}s userId : %{public}d", + adminName.c_str(), userId); + ErrCode res = GetAppControlProxy()-> + DeleteAppInstallControlRule(AppExecFwk::AppInstallControlRuleType::DISALLOWED_INSTALL, data, userId); + EDMLOGI("DisallowedInstallBundlesPlugin OnAdminRemoveDone result %{public}d:", res); +} +} // namespace EDM +} // namespace OHOS diff --git a/test/unittest/bundle_manager_proxy/bundle_manager_proxy_test.cpp b/test/unittest/bundle_manager_proxy/bundle_manager_proxy_test.cpp index 3d79b8e99..10cf6c741 100644 --- a/test/unittest/bundle_manager_proxy/bundle_manager_proxy_test.cpp +++ b/test/unittest/bundle_manager_proxy/bundle_manager_proxy_test.cpp @@ -84,6 +84,45 @@ HWTEST_F(BundleManagerProxyTest, GetAllowedInstallBundles, TestSize.Level1) ErrCode ret = bundleManagerProxy->GetAllowedInstallBundles(admin, DEFAULT_USER_ID, bundles); ASSERT_TRUE(ret != ERR_OK); } + +/** + * @tc.name: TestAddDisallowedInstallBundles + * @tc.desc: Test AddDisallowedInstallBundles func. + * @tc.type: FUNC + */ +HWTEST_F(BundleManagerProxyTest, AddDisallowedInstallBundles, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + ErrCode ret = bundleManagerProxy->AddDisallowedInstallBundles(admin, bundles, DEFAULT_USER_ID); + ASSERT_TRUE(ret != ERR_OK); +} + +/** + * @tc.name: TestRemoveDisallowedInstallBundles + * @tc.desc: Test RemoveDisallowedInstallBundles func. + * @tc.type: FUNC + */ +HWTEST_F(BundleManagerProxyTest, RemoveDisallowedInstallBundles, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + ErrCode ret = bundleManagerProxy->RemoveDisallowedInstallBundles(admin, bundles, DEFAULT_USER_ID); + ASSERT_TRUE(ret != ERR_OK); +} + +/** + * @tc.name: TestGetDisallowedInstallBundles + * @tc.desc: Test GetDisallowedInstallBundles func. + * @tc.type: FUNC + */ +HWTEST_F(BundleManagerProxyTest, GetDisallowedInstallBundles, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + ErrCode ret = bundleManagerProxy->GetDisallowedInstallBundles(admin, DEFAULT_USER_ID, bundles); + ASSERT_TRUE(ret != ERR_OK); +} } // namespace TEST } // namespace EDM } // namespace OHOS -- Gitee