From 2afabce954b0af1ad6ae93458b06ffdc2b5c8c1b Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Tue, 15 Jul 2025 11:28:40 +0800 Subject: [PATCH] Extract methods from ani and js into Helper class Signed-off-by: lanhaoyu --- interfaces/kits/js/distributedBundle/BUILD.gn | 56 +++++++++++++- .../distributedBundle/distributed_bundle.cpp | 58 +------------- .../distributedBundle/distributed_helper.cpp | 77 +++++++++++++++++++ .../js/distributedBundle/distributed_helper.h | 31 ++++++++ 4 files changed, 164 insertions(+), 58 deletions(-) create mode 100644 interfaces/kits/js/distributedBundle/distributed_helper.cpp create mode 100644 interfaces/kits/js/distributedBundle/distributed_helper.h diff --git a/interfaces/kits/js/distributedBundle/BUILD.gn b/interfaces/kits/js/distributedBundle/BUILD.gn index f1a0450..aa80574 100644 --- a/interfaces/kits/js/distributedBundle/BUILD.gn +++ b/interfaces/kits/js/distributedBundle/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022-2023 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -14,6 +14,53 @@ import("//build/ohos.gni") import("../../../../dbms.gni") +ohos_shared_library("distributed_bundle_common") { + branch_protector_ret = "pac_ret" + + sanitize = { + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + integer_overflow = true + ubsan = true + } + + sources = [ "distributed_helper.cpp" ] + + cflags = [ + "-Os", + "-fstack-protector-strong", + ] + + cflags_cc = [ + "-Os", + "-fstack-protector-strong", + ] + + deps = [ "${dbms_inner_api_path}:dbms_fwk" ] + + defines = [ + "APP_LOG_TAG = \"DistributedBundleMgrService\"", + "LOG_DOMAIN = 0xD0011E0", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "bundle_framework:bundle_napi_common", + "bundle_framework:libappexecfwk_common", + "c_utils:utils", + "hilog:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] + + subsystem_name = "bundlemanager" + part_name = "distributed_bundle_framework" +} + ohos_shared_library("distributedbundlemanager") { branch_protector_ret = "pac_ret" @@ -46,7 +93,10 @@ ohos_shared_library("distributedbundlemanager") { configs = [ "${dbms_services_path}:distributed_bms_config" ] - deps = [ "${dbms_inner_api_path}:dbms_fwk" ] + deps = [ + ":distributed_bundle_common", + "${dbms_inner_api_path}:dbms_fwk", + ] external_deps = [ "ability_base:want", @@ -67,7 +117,7 @@ ohos_shared_library("distributedbundlemanager") { } group("jsapi_target") { - deps = [] + deps = [ ":distributed_bundle_common" ] if (support_jsapi && distributed_bundle_framework_graphics) { deps += [ ":distributedbundlemanager" ] diff --git a/interfaces/kits/js/distributedBundle/distributed_bundle.cpp b/interfaces/kits/js/distributedBundle/distributed_bundle.cpp index 920cb18..0308aa7 100644 --- a/interfaces/kits/js/distributedBundle/distributed_bundle.cpp +++ b/interfaces/kits/js/distributedBundle/distributed_bundle.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -22,6 +22,7 @@ #include "common_func.h" #include "distributed_bms_interface.h" #include "distributed_bms_proxy.h" +#include "distributed_helper.h" #include "if_system_ability_manager.h" #include "ipc_skeleton.h" #include "iservice_registry.h" @@ -35,33 +36,6 @@ namespace OHOS { namespace AppExecFwk { -namespace { -constexpr int32_t GET_REMOTE_ABILITY_INFO_MAX_SIZE = 10; -const std::string RESOURCE_NAME_GET_REMOTE_ABILITY_INFO = "GetRemoteAbilityInfo"; -const std::string PARAMETER_ELEMENT_NAME = "elementName"; -const std::string PARAMETER_LOCALE = "locale"; -} - -static OHOS::sptr GetDistributedBundleMgr() -{ - APP_LOGD("GetDistributedBundleMgr start"); - auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (samgr == nullptr) { - APP_LOGE("GetDistributedBundleMgr samgr is nullptr"); - return nullptr; - } - auto remoteObject = samgr->GetSystemAbility(OHOS::DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (remoteObject == nullptr) { - APP_LOGE("GetDistributedBundleMgr remoteObject is nullptr"); - return nullptr; - } - auto distributeBundleMgr = OHOS::iface_cast(remoteObject); - if (distributeBundleMgr == nullptr) { - APP_LOGE("GetDistributedBundleMgr distributeBundleMgr is nullptr"); - return nullptr; - } - return distributeBundleMgr; -} static void ConvertElementName(napi_env env, napi_value objElementName, const ElementName &elementName) { @@ -205,32 +179,6 @@ static bool ParseElementNames(napi_env env, napi_value args, bool &isArray, std: return true; } -int32_t InnerGetRemoteAbilityInfo(const std::vector &elementNames, const std::string &locale, - bool isArray, std::vector &remoteAbilityInfos) -{ - if (elementNames.size() == 0) { - APP_LOGE("InnerGetRemoteAbilityInfos elementNames is empty"); - return ERROR_BUNDLE_SERVICE_EXCEPTION; - } - auto iDistBundleMgr = GetDistributedBundleMgr(); - if (iDistBundleMgr == nullptr) { - APP_LOGE("can not get iDistBundleMgr"); - return ERROR_DISTRIBUTED_SERVICE_NOT_RUNNING; - } - int32_t result; - if (isArray) { - result = iDistBundleMgr->GetRemoteAbilityInfos(elementNames, locale, remoteAbilityInfos); - } else { - RemoteAbilityInfo remoteAbilityInfo; - result = iDistBundleMgr->GetRemoteAbilityInfo(elementNames[0], locale, remoteAbilityInfo); - remoteAbilityInfos.push_back(remoteAbilityInfo); - } - if (result != 0) { - APP_LOGE("InnerGetRemoteAbilityInfo failed"); - } - return CommonFunc::ConvertErrCode(result); -} - void GetRemoteAbilityInfoExec(napi_env env, void *data) { GetRemoteAbilityInfoCallbackInfo *asyncCallbackInfo = reinterpret_cast(data); @@ -238,7 +186,7 @@ void GetRemoteAbilityInfoExec(napi_env env, void *data) APP_LOGE("asyncCallbackInfo is null"); return; } - asyncCallbackInfo->err = InnerGetRemoteAbilityInfo(asyncCallbackInfo->elementNames, + asyncCallbackInfo->err = DistributedHelper::InnerGetRemoteAbilityInfo(asyncCallbackInfo->elementNames, asyncCallbackInfo->locale, asyncCallbackInfo->isArray, asyncCallbackInfo->remoteAbilityInfos); } diff --git a/interfaces/kits/js/distributedBundle/distributed_helper.cpp b/interfaces/kits/js/distributedBundle/distributed_helper.cpp new file mode 100644 index 0000000..09817bb --- /dev/null +++ b/interfaces/kits/js/distributedBundle/distributed_helper.cpp @@ -0,0 +1,77 @@ +/* + * 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 "distributed_helper.h" + +#include "app_log_wrapper.h" +#include "bundle_errors.h" +#include "business_error.h" +#include "common_func.h" +#include "distributed_bms_interface.h" +#include "distributed_bms_proxy.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace AppExecFwk { + +static OHOS::sptr GetDistributedBundleMgr() +{ + APP_LOGD("GetDistributedBundleMgr start"); + auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgr == nullptr) { + APP_LOGE("GetDistributedBundleMgr samgr is nullptr"); + return nullptr; + } + auto remoteObject = samgr->GetSystemAbility(OHOS::DISTRIBUTED_BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (remoteObject == nullptr) { + APP_LOGE("GetDistributedBundleMgr remoteObject is nullptr"); + return nullptr; + } + auto distributeBundleMgr = OHOS::iface_cast(remoteObject); + if (distributeBundleMgr == nullptr) { + APP_LOGE("GetDistributedBundleMgr distributeBundleMgr is nullptr"); + return nullptr; + } + return distributeBundleMgr; +} + +int32_t DistributedHelper::InnerGetRemoteAbilityInfo(const std::vector &elementNames, + const std::string &locale, bool isArray, std::vector &remoteAbilityInfos) +{ + if (elementNames.size() == 0) { + APP_LOGE("InnerGetRemoteAbilityInfos elementNames is empty"); + return ERROR_PARAM_CHECK_ERROR; + } + auto iDistBundleMgr = GetDistributedBundleMgr(); + if (iDistBundleMgr == nullptr) { + APP_LOGE("can not get iDistBundleMgr"); + return ERROR_DISTRIBUTED_SERVICE_NOT_RUNNING; + } + int32_t result; + if (isArray) { + result = iDistBundleMgr->GetRemoteAbilityInfos(elementNames, locale, remoteAbilityInfos); + } else { + RemoteAbilityInfo remoteAbilityInfo; + result = iDistBundleMgr->GetRemoteAbilityInfo(elementNames[0], locale, remoteAbilityInfo); + remoteAbilityInfos.push_back(remoteAbilityInfo); + } + if (result != 0) { + APP_LOGE("InnerGetRemoteAbilityInfo failed"); + } + return CommonFunc::ConvertErrCode(result); +} +} // AppExecFwk +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/js/distributedBundle/distributed_helper.h b/interfaces/kits/js/distributedBundle/distributed_helper.h new file mode 100644 index 0000000..819b2f7 --- /dev/null +++ b/interfaces/kits/js/distributedBundle/distributed_helper.h @@ -0,0 +1,31 @@ +/* + * 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 BUNDLE_MANAGER_FRAMEWORK_DISTRIBUTEBUNDLEMGR_INTERFACES_KITS_JS_DISTRIBUTED_HELPER_H +#define BUNDLE_MANAGER_FRAMEWORK_DISTRIBUTEBUNDLEMGR_INTERFACES_KITS_JS_DISTRIBUTED_HELPER_H + +#include "element_name.h" +#include "remote_ability_info.h" + +namespace OHOS { +namespace AppExecFwk { +class DistributedHelper { +public: + static int32_t InnerGetRemoteAbilityInfo(const std::vector &elementNames, const std::string &locale, + bool isArray, std::vector &remoteAbilityInfos); +}; +} +} +#endif // BUNDLE_MANAGER_FRAMEWORK_DISTRIBUTEBUNDLEMGR_INTERFACES_KITS_JS_DISTRIBUTED_HELPER_H \ No newline at end of file -- Gitee