From 307e52a811c8d76fb23b26848c2b6f6b5a6077a0 Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Mon, 12 May 2025 15:55:04 +0800 Subject: [PATCH] ani distributedBundleManager Signed-off-by: lanhaoyu --- BUILD.gn | 6 +- bundle.json | 2 + dbms.gni | 1 + .../ani/distributed_bundle_manager/BUILD.gn | 121 +++++++++++++ .../ani_distributed_bundle_manager.cpp | 121 +++++++++++++ ...ani_distributed_bundle_manager_convert.cpp | 57 ++++++ .../ani_distributed_bundle_manager_convert.h | 34 ++++ ...distributed_bundle_manager_unsupported.cpp | 80 +++++++++ .../@ohos.bundle.distributedBundleManager.ets | 163 ++++++++++++++++++ .../ets/bundleManager/RemoteAbilityInfo.ets | 27 +++ .../bundleManager/RemoteAbilityInfoInner.ets | 32 ++++ interfaces/kits/js/distributedBundle/BUILD.gn | 56 +++++- .../distributedBundle/distributed_bundle.cpp | 58 +------ .../distributedBundle/distributed_helper.cpp | 77 +++++++++ .../js/distributedBundle/distributed_helper.h | 31 ++++ 15 files changed, 807 insertions(+), 59 deletions(-) create mode 100644 interfaces/kits/ani/distributed_bundle_manager/BUILD.gn create mode 100644 interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager.cpp create mode 100644 interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_convert.cpp create mode 100644 interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_convert.h create mode 100644 interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_unsupported.cpp create mode 100644 interfaces/kits/ani/distributed_bundle_manager/ets/@ohos.bundle.distributedBundleManager.ets create mode 100644 interfaces/kits/ani/distributed_bundle_manager/ets/bundleManager/RemoteAbilityInfo.ets create mode 100644 interfaces/kits/ani/distributed_bundle_manager/ets/bundleManager/RemoteAbilityInfoInner.ets create mode 100644 interfaces/kits/js/distributedBundle/distributed_helper.cpp create mode 100644 interfaces/kits/js/distributedBundle/distributed_helper.h diff --git a/BUILD.gn b/BUILD.gn index bffc9e7..f26bed7 100644 --- a/BUILD.gn +++ b/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 @@ -13,6 +13,10 @@ import("//build/ohos.gni") +group("ani_dbms_packages") { + deps = [ "interfaces/kits/ani/distributed_bundle_manager:ani_dbms_packages" ] +} + group("inner_api_target") { deps = [ "interfaces/inner_api:dbms_fwk" ] } diff --git a/bundle.json b/bundle.json index dc6e473..5dd3aff 100644 --- a/bundle.json +++ b/bundle.json @@ -36,6 +36,7 @@ "napi", "os_account", "resource_management", + "runtime_core", "safwk", "samgr", "selinux_adapter", @@ -50,6 +51,7 @@ }, "build": { "sub_component": [ + "//foundation/bundlemanager/distributed_bundle_framework:ani_dbms_packages", "//foundation/bundlemanager/distributed_bundle_framework:inner_api_target", "//foundation/bundlemanager/distributed_bundle_framework:jsapi_target", "//foundation/bundlemanager/distributed_bundle_framework:dbms_target" diff --git a/dbms.gni b/dbms.gni index 7c577a5..755b4a8 100644 --- a/dbms.gni +++ b/dbms.gni @@ -21,6 +21,7 @@ dbms_services_path = "${bundlemanager_path}/distributed_bundle_framework/services/dbms" distributeddatamgr_path = "//foundation/distributeddatamgr" kits_path = "${bundle_framework_path}/interfaces/kits" +dbms_kits_path = "${bundlemanager_path}/distributed_bundle_framework/interfaces/kits" declare_args() { distributed_bundle_framework_graphics = true diff --git a/interfaces/kits/ani/distributed_bundle_manager/BUILD.gn b/interfaces/kits/ani/distributed_bundle_manager/BUILD.gn new file mode 100644 index 0000000..5f651aa --- /dev/null +++ b/interfaces/kits/ani/distributed_bundle_manager/BUILD.gn @@ -0,0 +1,121 @@ +# 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. + +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("../../../../dbms.gni") + +ohos_shared_library("ani_distributed_bundle_manager") { + branch_protector_ret = "pac_ret" + + sanitize = { + boundary_sanitize = true + cfi = true + cfi_cross_dso = true + debug = false + integer_overflow = true + ubsan = true + } + + cflags = [ "-fstack-protector-strong" ] + cflags_cc = [ + "-Os", + "-fno-unwind-tables", + "-fstack-protector-strong", + ] + + defines = [ + "APP_LOG_TAG = \"DistributedBundleMgrService\"", + "LOG_DOMAIN = 0xD0011E0", + ] + + include_dirs = [ "${dbms_kits_path}/js/distributedBundle" ] + + sources = [ "ani_distributed_bundle_manager_convert.cpp" ] + if (distributed_bundle_framework_enable) { + sources += [ "ani_distributed_bundle_manager.cpp" ] + } else { + sources += [ "ani_distributed_bundle_manager_unsupported.cpp" ] + } + + configs = [ "${dbms_services_path}:distributed_bms_config" ] + + deps = [ + "${dbms_kits_path}/js/distributedBundle:distributed_bundle_common", + "${dbms_inner_api_path}:dbms_fwk", + ] + + external_deps = [ + "ability_base:want", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "bundle_framework:bms_ani_common", + "bundle_framework:bundle_napi_common", + "bundle_framework:libappexecfwk_common", + "c_utils:utils", + "hilog:libhilog", + "napi:ace_napi", + "runtime_core:ani", + "runtime_core:ani_helpers", + "samgr:samgr_proxy", + ] + public_external_deps = [ "bundle_framework:bundle_napi_common" ] + + subsystem_name = "bundlemanager" + part_name = "distributed_bundle_framework" +} + +generate_static_abc("distributed_bundle_manager") { + base_url = "./ets" + files = [ "./ets/@ohos.bundle.distributedBundleManager.ets" ] + is_boot_abc = "True" + device_dst_file = "/system/framework/distributed_bundle_manager.abc" +} + +ohos_prebuilt_etc("distributed_bundle_manager_etc") { + source = "$target_out_dir/distributed_bundle_manager.abc" + module_install_dir = "framework" + subsystem_name = "bundlemanager" + part_name = "distributed_bundle_framework" + deps = [ ":distributed_bundle_manager" ] +} + +generate_static_abc("remote_ability_info") { + base_url = "./ets" + files = [ + "./ets/bundleManager/RemoteAbilityInfo.ets", + "./ets/bundleManager/RemoteAbilityInfoInner.ets", + ] + is_boot_abc = "True" + device_dst_file = "/system/framework/remote_ability_info.abc" +} + +ohos_prebuilt_etc("remote_ability_info_etc") { + source = "$target_out_dir/remote_ability_info.abc" + module_install_dir = "framework" + subsystem_name = "bundlemanager" + part_name = "distributed_bundle_framework" + deps = [ ":remote_ability_info" ] +} + +group("ani_dbms_packages") { + deps = [] + + if (distributed_bundle_framework_graphics) { + deps += [ + ":ani_distributed_bundle_manager", + ":distributed_bundle_manager_etc", + ":remote_ability_info_etc", + ] + } +} diff --git a/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager.cpp b/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager.cpp new file mode 100644 index 0000000..e3ed495 --- /dev/null +++ b/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager.cpp @@ -0,0 +1,121 @@ +/* + * 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 + +#include "ani_distributed_bundle_manager_convert.h" +#include "app_log_wrapper.h" +#include "bundle_errors.h" +#include "business_error_ani.h" +#include "common_fun_ani.h" +#include "distributed_helper.h" +#include "napi_constants.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { +constexpr const char* NS_NAME_DISTRIBUTEDBUNDLEMANAGER = + "@ohos.bundle.distributedBundleManager.distributedBundleManager"; +} // namespace + +static ani_object GetRemoteAbilityInfoInner(ani_env *env, + ani_object aniElementNames, ani_string aniLocale, bool isArray) +{ + std::vector elementNames; + if (!CommonFunAni::ParseAniArray(env, aniElementNames, elementNames, CommonFunAni::ParseElementName)) { + APP_LOGE("ParseAniArray ElementNames failed"); + BusinessErrorAni::ThrowCommonError(env, ERROR_PARAM_CHECK_ERROR, PARAMETER_ELEMENT_NAME, TYPE_OBJECT); + return nullptr; + } + + std::string locale = CommonFunAni::AniStrToString(env, aniLocale); + if (locale.empty()) { + APP_LOGW("Locale undefined"); + } + + if (elementNames.size() > GET_REMOTE_ABILITY_INFO_MAX_SIZE) { + BusinessErrorAni::ThrowError(env, ERROR_PARAM_CHECK_ERROR, + "BusinessError 401: The number of ElementNames is greater than 10"); + return nullptr; + } + + std::vector remoteAbilityInfos; + int32_t ret = DistributedHelper::InnerGetRemoteAbilityInfo(elementNames, locale, isArray, remoteAbilityInfos); + if (ret != ERR_OK) { + APP_LOGE("GetRemoteAbilityInfo failed ret: %{public}d", ret); + BusinessErrorAni::ThrowCommonError(env, ret, + RESOURCE_NAME_GET_REMOTE_ABILITY_INFO, Constants::PERMISSION_GET_BUNDLE_INFO_PRIVILEGED); + return nullptr; + } + + if (isArray) { + ani_object remoteAbilityInfosObject = CommonFunAni::ConvertAniArray(env, remoteAbilityInfos, + AniDistributedbundleManagerConvert::ConvertRemoteAbilityInfo); + if (remoteAbilityInfosObject == nullptr) { + APP_LOGE("nullptr remoteAbilityInfosObject"); + } + return remoteAbilityInfosObject; + } else { + return AniDistributedbundleManagerConvert::ConvertRemoteAbilityInfo(env, remoteAbilityInfos[0]); + } +} + +static ani_object GetRemoteAbilityInfo(ani_env *env, ani_object aniElementNames, ani_string aniLocale) +{ + return GetRemoteAbilityInfoInner(env, aniElementNames, aniLocale, false); +} + +static ani_object GetRemoteAbilityInfos(ani_env *env, ani_object aniElementNames, ani_string aniLocale) +{ + return GetRemoteAbilityInfoInner(env, aniElementNames, aniLocale, true); +} + +extern "C" { +ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result) +{ + APP_LOGI("ANI_Constructor distributedBundleManager called"); + ani_env* env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + RETURN_ANI_STATUS_IF_NOT_OK(status, "Unsupported ANI_VERSION_1"); + + arkts::ani_signature::Namespace freeInstallNS = + arkts::ani_signature::Builder::BuildNamespace(NS_NAME_DISTRIBUTEDBUNDLEMANAGER); + ani_namespace kitNs = nullptr; + status = env->FindNamespace(freeInstallNS.Descriptor().c_str(), &kitNs); + if (status != ANI_OK) { + APP_LOGE("FindNamespace: %{public}s fail with %{public}d", NS_NAME_DISTRIBUTEDBUNDLEMANAGER, status); + return status; + } + + std::array methods = { + ani_native_function { "getRemoteAbilityInfoNative", nullptr, reinterpret_cast(GetRemoteAbilityInfo) }, + ani_native_function { "getRemoteAbilityInfosNative", nullptr, reinterpret_cast(GetRemoteAbilityInfos) } + }; + + status = env->Namespace_BindNativeFunctions(kitNs, methods.data(), methods.size()); + if (status != ANI_OK) { + APP_LOGE("Namespace_BindNativeFunctions: %{public}s fail with %{public}d", + NS_NAME_DISTRIBUTEDBUNDLEMANAGER, status); + return status; + } + + *result = ANI_VERSION_1; + + APP_LOGI("ANI_Constructor finished"); + + return ANI_OK; +} +} +} // AppExecFwk +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_convert.cpp b/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_convert.cpp new file mode 100644 index 0000000..5d99ba1 --- /dev/null +++ b/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_convert.cpp @@ -0,0 +1,57 @@ +/* + * 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 "ani_distributed_bundle_manager_convert.h" +#include "common_fun_ani.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { + constexpr const char* CLASSNAME_REMOTEABILITYINFO = "LbundleManager/RemoteAbilityInfoInner/RemoteAbilityInfoInner;"; + constexpr const char* PROPERTYNAME_ELEMENTNAME = "elementName"; + constexpr const char* PROPERTYNAME_ICON = "icon"; + constexpr const char* PROPERTYNAME_LABEL = "label"; +} + +ani_object AniDistributedbundleManagerConvert::ConvertRemoteAbilityInfo(ani_env* env, + const RemoteAbilityInfo& remoteAbilityInfo) +{ + RETURN_NULL_IF_NULL(env); + + ani_class cls = CommonFunAni::CreateClassByName(env, CLASSNAME_REMOTEABILITYINFO); + RETURN_NULL_IF_NULL(cls); + + ani_object object = CommonFunAni::CreateNewObjectByClass(env, cls); + RETURN_NULL_IF_NULL(object); + + ani_string string = nullptr; + + // elementName: ElementName + ani_object aElementNameObject = CommonFunAni::ConvertElementName(env, remoteAbilityInfo.elementName); + RETURN_NULL_IF_NULL(aElementNameObject); + RETURN_NULL_IF_FALSE(CommonFunAni::CallSetter(env, cls, object, PROPERTYNAME_ELEMENTNAME, aElementNameObject)); + + // label: string + RETURN_NULL_IF_FALSE(CommonFunAni::StringToAniStr(env, remoteAbilityInfo.label, string)); + RETURN_NULL_IF_FALSE(CommonFunAni::CallSetter(env, cls, object, PROPERTYNAME_LABEL, string)); + + // icon: string + RETURN_NULL_IF_FALSE(CommonFunAni::StringToAniStr(env, remoteAbilityInfo.icon, string)); + RETURN_NULL_IF_FALSE(CommonFunAni::CallSetter(env, cls, object, PROPERTYNAME_ICON, string)); + + return object; +} +} // AppExecFwk +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_convert.h b/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_convert.h new file mode 100644 index 0000000..bd1004c --- /dev/null +++ b/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_convert.h @@ -0,0 +1,34 @@ +/* + * 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 DISTRIBUTED_BUNDLE_FRAMEWORK_INTERFACE_KITS_ANI_ANI_DISTRIBUTED_BUNDLE_MANAGER_CONVERT_H +#define DISTRIBUTED_BUNDLE_FRAMEWORK_INTERFACE_KITS_ANI_ANI_DISTRIBUTED_BUNDLE_MANAGER_CONVERT_H + +#include +#include +#include + +#include "element_name.h" +#include "remote_ability_info.h" + +namespace OHOS { +namespace AppExecFwk { +class AniDistributedbundleManagerConvert { +public: + static ani_object ConvertRemoteAbilityInfo(ani_env* env, const RemoteAbilityInfo& remoteAbilityInfo); +}; +} +} +#endif // DISTRIBUTED_BUNDLE_FRAMEWORK_INTERFACE_KITS_ANI_ANI_DISTRIBUTED_BUNDLE_MANAGER_CONVERT_H \ No newline at end of file diff --git a/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_unsupported.cpp b/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_unsupported.cpp new file mode 100644 index 0000000..c06b64f --- /dev/null +++ b/interfaces/kits/ani/distributed_bundle_manager/ani_distributed_bundle_manager_unsupported.cpp @@ -0,0 +1,80 @@ +/* + * 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 + +#include "app_log_wrapper.h" +#include "bundle_errors.h" +#include "business_error_ani.h" +#include "napi_constants.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { +constexpr const char* NS_NAME_DISTRIBUTEDBUNDLEMANAGER = + "@ohos.bundle.distributedBundleManager.distributedBundleManager"; +} // namespace + +ani_object GetRemoteAbilityInfo(ani_env *env, ani_object aniElementNames, ani_string aniLocale) +{ + APP_LOGI("SystemCapability.BundleManager.DistributedBundleFramework not supported."); + BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND, RESOURCE_NAME_GET_REMOTE_ABILITY_INFO, ""); + return nullptr; +} + +ani_object GetRemoteAbilityInfos(ani_env *env, ani_object aniElementNames, ani_string aniLocale) +{ + APP_LOGI("SystemCapability.BundleManager.DistributedBundleFramework not supported."); + BusinessErrorAni::ThrowCommonError(env, ERROR_SYSTEM_ABILITY_NOT_FOUND, RESOURCE_NAME_GET_REMOTE_ABILITY_INFO, ""); + return nullptr; +} + +extern "C" { +ANI_EXPORT ani_status ANI_Constructor(ani_vm* vm, uint32_t* result) +{ + APP_LOGI("ANI_Constructor distributedBundleManager called"); + ani_env* env; + ani_status status = vm->GetEnv(ANI_VERSION_1, &env); + RETURN_ANI_STATUS_IF_NOT_OK(status, "Unsupported ANI_VERSION_1"); + + arkts::ani_signature::Namespace freeInstallNS = + arkts::ani_signature::Builder::BuildNamespace(NS_NAME_DISTRIBUTEDBUNDLEMANAGER); + ani_namespace kitNs = nullptr; + status = env->FindNamespace(freeInstallNS.Descriptor().c_str(), &kitNs); + if (status != ANI_OK) { + APP_LOGE("FindNamespace: %{public}s fail with %{public}d", NS_NAME_DISTRIBUTEDBUNDLEMANAGER, status); + return status; + } + + std::array methods = { + ani_native_function {"getRemoteAbilityInfoNative", nullptr, reinterpret_cast(GetRemoteAbilityInfo) }, + ani_native_function { "getRemoteAbilityInfosNative", nullptr, reinterpret_cast(GetRemoteAbilityInfos) } + }; + + status = env->Namespace_BindNativeFunctions(kitNs, methods.data(), methods.size()); + if (status != ANI_OK) { + APP_LOGE("Namespace_BindNativeFunctions: %{public}s fail with %{public}d", + NS_NAME_DISTRIBUTEDBUNDLEMANAGER, status); + return status; + } + + *result = ANI_VERSION_1; + + APP_LOGI("ANI_Constructor finished"); + + return ANI_OK; +} +} +} // AppExecFwk +} // OHOS \ No newline at end of file diff --git a/interfaces/kits/ani/distributed_bundle_manager/ets/@ohos.bundle.distributedBundleManager.ets b/interfaces/kits/ani/distributed_bundle_manager/ets/@ohos.bundle.distributedBundleManager.ets new file mode 100644 index 0000000..f56c5c3 --- /dev/null +++ b/interfaces/kits/ani/distributed_bundle_manager/ets/@ohos.bundle.distributedBundleManager.ets @@ -0,0 +1,163 @@ +/* + * 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. + */ + +/** + * @file + * @kit AbilityKit + */ + +import { AsyncCallback, BusinessError } from '@ohos.base'; +import { ElementName } from 'bundleManager.ElementName'; +import { RemoteAbilityInfo as _RemoteAbilityInfo } from 'bundleManager.RemoteAbilityInfo'; +import { RemoteAbilityInfoInner } from './bundleManager/RemoteAbilityInfoInner'; + +export default namespace distributedBundleManager { + loadLibrary("ani_distributed_bundle_manager.z"); + + export native function getRemoteAbilityInfoNative(elementNames: Array, locale: string): RemoteAbilityInfo; + export native function getRemoteAbilityInfosNative(elementNames: Array, locale: string): Array; + + function elementName2Array(elementName: ElementName): Array { + let elementNames: Array = new Array(); + elementNames.push(elementName); + return elementNames; + } + + function getRemoteAbilityInfo(elementName: ElementName, callback: AsyncCallback): void { + let cb = (): RemoteAbilityInfo => { + return distributedBundleManager.getRemoteAbilityInfoNative(elementName2Array(elementName), ''); + }; + let p1 = taskpool.execute(cb); + p1.then((e: NullishType) => { + let result: RemoteAbilityInfo = e as RemoteAbilityInfo; + let br: BusinessError = new BusinessError(); + callback(br, result); + }, (err: BusinessError): void => { + let result: RemoteAbilityInfo = new RemoteAbilityInfoInner; + callback(err, result); + }); + } + + function getRemoteAbilityInfo(elementName: ElementName): Promise { + let p = new Promise((resolve: (remoteAbilityInfo: RemoteAbilityInfo) => void, reject: (error: BusinessError) => void) => { + let cb = (): RemoteAbilityInfo => { + return distributedBundleManager.getRemoteAbilityInfoNative(elementName2Array(elementName), ''); + }; + let p1 = taskpool.execute(cb); + p1.then((e: NullishType) => { + let result: RemoteAbilityInfo = e as RemoteAbilityInfo; + resolve(result); + }, (err: BusinessError): void => { + reject(err); + }); + }); + return p; + } + + function getRemoteAbilityInfo(elementNames: Array, callback: AsyncCallback>): void { + let cb = (): (Array) => { + return distributedBundleManager.getRemoteAbilityInfosNative(elementNames, ''); + }; + let p1 = taskpool.execute(cb); + p1.then((e: NullishType) => { + let resultArray: Array = e as Array; + let br: BusinessError = new BusinessError(); + callback(br, resultArray); + }, (err: BusinessError): void => { + let resultArray: Array = new Array; + callback(err, resultArray); + }); + } + + function getRemoteAbilityInfo(elementNames: Array): Promise> { + let p = new Promise>((resolve: (arrRemoteAbilityInfo: Array) => void, reject: (error: BusinessError) => void) => { + let cb = (): (Array) => { + return distributedBundleManager.getRemoteAbilityInfosNative(elementNames, ''); + }; + let p1 = taskpool.execute(cb); + p1.then((e: NullishType) => { + let resultArray: Array = e as Array; + resolve(resultArray); + }, (err: BusinessError): void => { + reject(err); + }); + }); + return p; + } + + function getRemoteAbilityInfo(elementName: ElementName, locale: string, callback: AsyncCallback): void { + let cb = (): RemoteAbilityInfo => { + return distributedBundleManager.getRemoteAbilityInfoNative(elementName2Array(elementName), locale); + }; + let p1 = taskpool.execute(cb); + p1.then((e: NullishType) => { + let result: RemoteAbilityInfo = e as RemoteAbilityInfo; + let br: BusinessError = new BusinessError(); + callback(br, result); + }, (err: BusinessError): void => { + let result: RemoteAbilityInfo = new RemoteAbilityInfoInner; + callback(err, result); + }); + } + + function getRemoteAbilityInfo(elementName: ElementName, locale: string): Promise { + let p = new Promise((resolve: (remoteAbilityInfo: RemoteAbilityInfo) => void, reject: (error: BusinessError) => void) => { + let cb = (): RemoteAbilityInfo => { + return distributedBundleManager.getRemoteAbilityInfoNative(elementName2Array(elementName), locale); + }; + let p1 = taskpool.execute(cb); + p1.then((e: NullishType) => { + let result: RemoteAbilityInfo = e as RemoteAbilityInfo; + resolve(result); + }, (err: BusinessError): void => { + reject(err); + }); + }); + return p; + } + + function getRemoteAbilityInfo(elementNames: Array, locale: string, callback: AsyncCallback>): void { + let cb = (): (Array) => { + return distributedBundleManager.getRemoteAbilityInfosNative(elementNames, locale); + }; + let p1 = taskpool.execute(cb); + p1.then((e: NullishType) => { + let resultArray: Array = e as Array; + let br: BusinessError = new BusinessError(); + callback(br, resultArray); + }, (err: BusinessError): void => { + let resultArray: Array = new Array; + callback(err, resultArray); + }); + } + + function getRemoteAbilityInfo(elementNames: Array, locale: string): Promise> { + let p = new Promise>((resolve: (arrRemoteAbilityInfo: Array) => void, reject: (error: BusinessError) => void) => { + let cb = (): (Array) => { + return distributedBundleManager.getRemoteAbilityInfosNative(elementNames, locale); + }; + let p1 = taskpool.execute(cb); + p1.then((e: NullishType) => { + let resultArray: Array = e as Array; + resolve(resultArray); + }, (err: BusinessError): void => { + reject(err); + }); + }); + return p; + } + + export type RemoteAbilityInfo = _RemoteAbilityInfo; +} diff --git a/interfaces/kits/ani/distributed_bundle_manager/ets/bundleManager/RemoteAbilityInfo.ets b/interfaces/kits/ani/distributed_bundle_manager/ets/bundleManager/RemoteAbilityInfo.ets new file mode 100644 index 0000000..ee71b1a --- /dev/null +++ b/interfaces/kits/ani/distributed_bundle_manager/ets/bundleManager/RemoteAbilityInfo.ets @@ -0,0 +1,27 @@ +/* + * 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. + */ + +/** + * @file + * @kit AbilityKit + */ + +import { ElementName } from 'bundleManager.ElementName'; + +export interface RemoteAbilityInfo { + readonly elementName: ElementName; + readonly label: string; + readonly icon: string; +} diff --git a/interfaces/kits/ani/distributed_bundle_manager/ets/bundleManager/RemoteAbilityInfoInner.ets b/interfaces/kits/ani/distributed_bundle_manager/ets/bundleManager/RemoteAbilityInfoInner.ets new file mode 100644 index 0000000..dbc97a4 --- /dev/null +++ b/interfaces/kits/ani/distributed_bundle_manager/ets/bundleManager/RemoteAbilityInfoInner.ets @@ -0,0 +1,32 @@ +/* + * 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. + */ + +/** + * @file + * @kit AbilityKit + */ + +import { ElementName } from 'bundleManager.ElementName'; +import { RemoteAbilityInfo } from 'bundleManager.RemoteAbilityInfo'; + + +export class RemoteAbilityInfoInner implements RemoteAbilityInfo { + public readonly elementName: ElementName = { + bundleName: '', + abilityName: '', + } + public readonly label: string = ''; + public readonly icon: string = ''; +} \ No newline at end of file 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