From ffce67808dfd273ffabdfb921154940285012577 Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Thu, 26 Jun 2025 15:25:24 +0800 Subject: [PATCH] =?UTF-8?q?abilityManager=20GetTopAbility=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81ets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangzezhong --- frameworks/ets/ani/BUILD.gn | 1 + frameworks/ets/ani/ability_manager/BUILD.gn | 68 ++++++++++ .../include/ets_ability_manager.h | 26 ++++ .../src/ets_ability_manager.cpp | 116 ++++++++++++++++++ .../ani/ani_common/include/ani_common_want.h | 4 + .../ani/ani_common/src/ani_common_want.cpp | 59 +++++++++ .../ets/@ohos.app.ability.abilityManager.ets | 47 +++++++ frameworks/ets/ets/BUILD.gn | 20 +++ 8 files changed, 341 insertions(+) create mode 100644 frameworks/ets/ani/ability_manager/BUILD.gn create mode 100644 frameworks/ets/ani/ability_manager/include/ets_ability_manager.h create mode 100644 frameworks/ets/ani/ability_manager/src/ets_ability_manager.cpp create mode 100644 frameworks/ets/ets/@ohos.app.ability.abilityManager.ets diff --git a/frameworks/ets/ani/BUILD.gn b/frameworks/ets/ani/BUILD.gn index 2c324d61cda..302ba1a3367 100644 --- a/frameworks/ets/ani/BUILD.gn +++ b/frameworks/ets/ani/BUILD.gn @@ -17,6 +17,7 @@ import("//foundation/ability/ability_runtime/ability_runtime.gni") group("ani_packages") { deps = [ "${ability_runtime_path}/frameworks/ets/ani/ability_delegator:ability_delegator_registry_ani_kit", + "${ability_runtime_path}/frameworks/ets/ani/ability_manager:ability_manager_ani_kit", "${ability_runtime_path}/frameworks/ets/ani/ani_common:ani_common", "${ability_runtime_path}/frameworks/ets/ani/native_constructor:context_ani", "${ability_runtime_path}/frameworks/ets/ani/uri_permission_manager:uri_permission_manager_abc_etc", diff --git a/frameworks/ets/ani/ability_manager/BUILD.gn b/frameworks/ets/ani/ability_manager/BUILD.gn new file mode 100644 index 00000000000..53c1823c0b7 --- /dev/null +++ b/frameworks/ets/ani/ability_manager/BUILD.gn @@ -0,0 +1,68 @@ +# 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("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_shared_library("ability_manager_ani_kit") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + cfi_vcall_icall_only = true + debug = false + } + + include_dirs = [ + "./include", + "${ability_runtime_services_path}/common/include", + "${ability_runtime_path}/interfaces/kits/native/ability/native", + "${ability_runtime_path}/interfaces/inner_api/runtime/include", + "${ability_runtime_services_path}/common/include", + ] + + configs = [] + + public_configs = [] + + sources = [ "./src/ets_ability_manager.cpp" ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "${ability_runtime_innerkits_path}/ability_manager:ability_manager", + "${ability_runtime_innerkits_path}/runtime:runtime", + "${ability_runtime_native_path}/ability/native:ability_business_error", + "${ability_runtime_path}/frameworks/ets/ani/ani_common:ani_common", + ] + + external_deps = [ + "ability_base:base", + "ability_base:configuration", + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "ipc:ipc_core", + "napi:ace_napi", + "runtime_core:ani", + "samgr:samgr_proxy", + ] + + innerapi_tags = [ "platformsdk" ] + subsystem_name = "ability" + part_name = "ability_runtime" +} diff --git a/frameworks/ets/ani/ability_manager/include/ets_ability_manager.h b/frameworks/ets/ani/ability_manager/include/ets_ability_manager.h new file mode 100644 index 00000000000..1cdda85e644 --- /dev/null +++ b/frameworks/ets/ani/ability_manager/include/ets_ability_manager.h @@ -0,0 +1,26 @@ +/* + * 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 OHOS_ABILITY_RUNTIME_ETS_ABILITY_MANAGER_H +#define OHOS_ABILITY_RUNTIME_ETS_ABILITY_MANAGER_H + +#include "ets_runtime.h" + +namespace OHOS { +namespace AbilityRuntime { +void EtsAbilityManagerRegistryInit(ani_env *env); +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_ETS_ABILITY_MANAGER_H diff --git a/frameworks/ets/ani/ability_manager/src/ets_ability_manager.cpp b/frameworks/ets/ani/ability_manager/src/ets_ability_manager.cpp new file mode 100644 index 00000000000..a4aa5844323 --- /dev/null +++ b/frameworks/ets/ani/ability_manager/src/ets_ability_manager.cpp @@ -0,0 +1,116 @@ +/* + * 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 "ets_ability_manager.h" + +#include "ability_business_error.h" +#include "ability_manager_client.h" +#include "ability_manager_errors.h" +#include "ability_manager_interface.h" +#include "ani_common_want.h" +#include "ets_error_utils.h" +#include "hilog_tag_wrapper.h" +#include "iservice_registry.h" + +namespace OHOS { +namespace AbilityRuntime { + +namespace { + constexpr const char* ETS_ABILITY_MANAGER_NAMESPACE = "L@ohos/app/ability/abilityManager/abilityManager;"; + constexpr int32_t ERR_FAILURE = -1; +} + +static void GetTopAbility(ani_env *env, ani_object callback) +{ + TAG_LOGD(AAFwkTag::ABILITYMGR, "call GetTopAbility"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "null env"); + return; + } +#ifdef ENABLE_ERRCODE + auto selfToken = IPCSkeleton::GetSelfTokenID(); + if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "not system app"); + AbilityRuntime::ThrowError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP); + return; + } +#endif + AppExecFwk::ElementName elementName = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(); + int resultCode = 0; + ani_object elementNameobj = AppExecFwk::WrapElementName(env, elementName); + if (elementNameobj == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "null elementNameobj"); + resultCode = ERR_FAILURE; + } + + AppExecFwk::AsyncCallback(env, callback, EtsErrorUtil::CreateErrorByNativeErr(env, resultCode), + elementNameobj); + + return; +} + +void EtsAbilityManagerRegistryInit(ani_env *env) +{ + TAG_LOGD(AAFwkTag::ABILITYMGR, "call EtsAbilityManagerRegistryInit"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "null env"); + return; + } + ani_status status = ANI_ERROR; + if (env->ResetError() != ANI_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "ResetError failed"); + } + ani_namespace ns; + status = env->FindNamespace(ETS_ABILITY_MANAGER_NAMESPACE, &ns); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "FindNamespace abilityManager failed status : %{public}d", status); + return; + } + std::array methods = { + ani_native_function {"nativeGetTopAbility", nullptr, reinterpret_cast(GetTopAbility)}, + }; + status = env->Namespace_BindNativeFunctions(ns, methods.data(), methods.size()); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "Namespace_BindNativeFunctions failed status : %{public}d", status); + } + if (env->ResetError() != ANI_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "ResetError failed"); + } +} + +extern "C" { +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + TAG_LOGD(AAFwkTag::ABILITYMGR, "in AbilityManagerEts.ANI_Constructor"); + if (vm == nullptr || result == nullptr) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "null vm or result"); + return ANI_INVALID_ARGS; + } + + ani_env *env = nullptr; + ani_status status = ANI_ERROR; + status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::ABILITYMGR, "GetEnv failed, status=%{public}d", status); + return ANI_NOT_FOUND; + } + EtsAbilityManagerRegistryInit(env); + *result = ANI_VERSION_1; + TAG_LOGD(AAFwkTag::ABILITYMGR, "AbilityManagerEts.ANI_Constructor finished"); + return ANI_OK; +} +} +} // namespace AbilityManagerEts +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/ani/ani_common/include/ani_common_want.h b/frameworks/ets/ani/ani_common/include/ani_common_want.h index a2d1bd125db..4a77835fb82 100644 --- a/frameworks/ets/ani/ani_common/include/ani_common_want.h +++ b/frameworks/ets/ani/ani_common/include/ani_common_want.h @@ -33,6 +33,10 @@ ani_ref WrapWantParams(ani_env *env, const AAFwk::WantParams &wantParams); bool UnwrapWantParams(ani_env *env, ani_ref param, AAFwk::WantParams &wantParams); bool UnWrapAbilityResult(ani_env *env, ani_object param, int &resultCode, AAFwk::Want &want); + +ani_object WrapElementName(ani_env *env, const AppExecFwk::ElementName &elementNameParam); +ani_object WrapElementNameInner(ani_env *env, ani_class elementNameObj, ani_object object, + const AppExecFwk::ElementName &elementNameParam); } // namespace AppExecFwk } // namespace OHOS #endif // OHOS_ABILITY_RUNTIME_ANI_COMMON_WANT_H diff --git a/frameworks/ets/ani/ani_common/src/ani_common_want.cpp b/frameworks/ets/ani/ani_common/src/ani_common_want.cpp index 3b29cf68608..2559c01043c 100644 --- a/frameworks/ets/ani/ani_common/src/ani_common_want.cpp +++ b/frameworks/ets/ani/ani_common/src/ani_common_want.cpp @@ -356,5 +356,64 @@ bool UnWrapAbilityResult(ani_env *env, ani_object param, int &resultCode, AAFwk: } return UnwrapWant(env, reinterpret_cast(wantRef), want); } + +ani_object WrapElementName(ani_env *env, const AppExecFwk::ElementName &elementNameParam) +{ + TAG_LOGD(AAFwkTag::ANI, "WrapElementName"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + ani_class elementNameObj = nullptr; + ani_status status = ANI_ERROR; + ani_method method = nullptr; + ani_object object = nullptr; + static const char *className = "LbundleManager/ElementNameInner/ElementNameInner;"; + if ((status = env->FindClass(className, &elementNameObj)) != ANI_OK || elementNameObj == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "FindClass status : %{public}d or null elementNameObj", status); + return nullptr; + } + if ((status = env->Class_FindMethod(elementNameObj, "", ":V", &method)) != ANI_OK || method == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "Class_FindMethod status : %{public}d or null method", status); + return nullptr; + } + if ((status = env->Object_New(elementNameObj, method, &object)) != ANI_OK || object == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "Object_New status : %{public}d or null object", status); + return nullptr; + } + return WrapElementNameInner(env, elementNameObj, object, elementNameParam); +} + +ani_object WrapElementNameInner(ani_env *env, ani_class elementNameObj, ani_object object, + const AppExecFwk::ElementName &elementNameParam) +{ + TAG_LOGD(AAFwkTag::ANI, "WrapElementNameInner"); + if (env == nullptr || elementNameObj == nullptr || object == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "invalid args"); + return nullptr; + } + if (!SetFieldStringByName(env, elementNameObj, object, "bundleName", elementNameParam.GetBundleName())) { + TAG_LOGE(AAFwkTag::ANI, "set bundleName failed"); + return nullptr; + } + if (!SetFieldStringByName(env, elementNameObj, object, "abilityName", elementNameParam.GetAbilityName())) { + TAG_LOGE(AAFwkTag::ANI, "set abilityName failed"); + return nullptr; + } + if (!SetFieldStringByName(env, elementNameObj, object, "deviceId", elementNameParam.GetDeviceID())) { + TAG_LOGE(AAFwkTag::ANI, "set deviceId failed"); + } + if (!SetFieldStringByName(env, elementNameObj, object, "moduleName", elementNameParam.GetModuleName())) { + TAG_LOGE(AAFwkTag::ANI, "set moduleName failed"); + } + if (!SetFieldStringByName(env, elementNameObj, object, "uri", elementNameParam.GetURI())) { + TAG_LOGE(AAFwkTag::ANI, "set uri failed"); + } + if (!SetFieldStringByName(env, elementNameObj, object, "shortName", elementNameParam.GetURI())) { + TAG_LOGE(AAFwkTag::ANI, "set shortName failed"); + } + return object; +} + } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/ets/ets/@ohos.app.ability.abilityManager.ets b/frameworks/ets/ets/@ohos.app.ability.abilityManager.ets new file mode 100644 index 00000000000..57e8f241353 --- /dev/null +++ b/frameworks/ets/ets/@ohos.app.ability.abilityManager.ets @@ -0,0 +1,47 @@ +/* + * 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 { ElementName } from 'bundleManager.ElementName' +import { BusinessError, AsyncCallback } from '@ohos.base'; +import AsyncCallbackWrapper from './utils/AbilityUtils'; + +export default namespace abilityManager { + loadLibrary("ability_manager_ani_kit.z") + + export native function nativeGetTopAbility(callback:AsyncCallbackWrapper) : void; + + export function getTopAbility():Promise { + let p = new Promise((resolve: (data: ElementName)=>void, reject:(err: BusinessError)=>void):void => { + let myCall = new AsyncCallbackWrapper((err: BusinessError, data: ElementName)=>{ + if (err.code == 0) { + resolve(data); + } else { + reject(err); + } + }); + taskpool.execute((): void => { + abilityManager.nativeGetTopAbility(myCall); + }); + }); + return p; +} + +export function getTopAbility(callback: AsyncCallback): void { + let myCall = new AsyncCallbackWrapper(callback); + taskpool.execute( (): void => { + abilityManager.nativeGetTopAbility(myCall); + }); +} +} \ No newline at end of file diff --git a/frameworks/ets/ets/BUILD.gn b/frameworks/ets/ets/BUILD.gn index 799dbd505d4..a57a17698ad 100644 --- a/frameworks/ets/ets/BUILD.gn +++ b/frameworks/ets/ets/BUILD.gn @@ -80,6 +80,25 @@ ohos_prebuilt_etc("ability_runtime_ability_utils_abc_etc") { deps = [ ":ability_runtime_ability_utils_abc" ] } +generate_static_abc("ability_ability_manager_abc") { + base_url = "./" + files = [ "./@ohos.app.ability.abilityManager.ets" ] + + dst_file = "$target_out_dir/ability_ability_manager.abc" + out_puts = [ "$target_out_dir/ability_ability_manager.abc" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/ability_ability_manager.abc" +} + +ohos_prebuilt_etc("ability_ability_manager_abc_etc") { + source = "$target_out_dir/ability_ability_manager.abc" + module_install_dir = "framework" + subsystem_name = "ability" + part_name = "ability_runtime" + deps = [ ":ability_ability_manager_abc" ] +} + generate_static_abc("ability_runtime_ability_constant_abc") { base_url = "./" files = [ "./@ohos.app.ability.AbilityConstant.ets" ] @@ -456,6 +475,7 @@ ohos_prebuilt_etc("ability_runtime_ability_stage_context_abc_etc") { group("ets_packages") { deps = [ + ":ability_ability_manager_abc_etc", ":ability_delegator_abc_etc", ":ability_delegator_ability_monitor_abc_etc", ":ability_delegator_application_testRunner_abc_etc", -- Gitee