From fa8eac6909e9339d9d2ff8cf8b4edbaf804074d1 Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Fri, 13 Jun 2025 22:24:07 +0800 Subject: [PATCH] =?UTF-8?q?delegator=20AddAbilityMonitor=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zhangzezhong --- .../src/ets_ability_delegator.cpp | 36 ++++----- .../src/ets_ability_delegator_registry.cpp | 13 +-- .../src/ets_ability_delegator_utils.cpp | 46 ++++++----- .../src/ets_ability_monitor.cpp | 2 +- frameworks/native/ability/native/BUILD.gn | 1 + .../native/ability_runtime/ets_ui_ability.cpp | 63 ++++++--------- .../native/ability_runtime/js_ui_ability.cpp | 80 +++++++++---------- .../native/ability/native/ui_ability.cpp | 24 ++++++ .../native/ability_runtime/ets_ui_ability.h | 1 - .../native/ability_runtime/js_ui_ability.h | 2 +- .../kits/native/ability/native/ui_ability.h | 4 + .../ability_delegator_infos.h | 2 +- .../BUILD.gn | 9 ++- .../ui_ability_test.cpp | 67 ++++++++++++++++ 14 files changed, 214 insertions(+), 136 deletions(-) diff --git a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp index a21efe7f008..be46666377d 100644 --- a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp +++ b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp @@ -37,10 +37,6 @@ std::map, sptr, std::owner_less std::mutex g_mtxMonitorRecord; std::mutex g_mutexAbilityRecord; -enum ERROR_CODE { - INCORRECT_PARAMETERS = 401, -}; - #ifdef ENABLE_ERRCODE constexpr int COMMON_FAILED = 16000100; #else @@ -59,7 +55,7 @@ EtsAbilityDelegator::EtsAbilityDelegator() auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(AbilityRuntime::Runtime::Language::ETS); if (delegator) { auto clearFunc = [](const std::shared_ptr &baseProperty) { - auto property = std::static_pointer_cast(baseProperty); + auto property = std::static_pointer_cast(baseProperty); if (!property) { TAG_LOGE(AAFwkTag::DELEGATOR, "invalid property type"); return; @@ -117,9 +113,9 @@ ani_object EtsAbilityDelegator::CreateEtsBaseContext(ani_env* aniEnv, ani_class return {}; } auto filesDir = context->GetFilesDir(); - ani_string filesDir_string{}; - aniEnv->String_NewUTF8(filesDir.c_str(), filesDir.size(), &filesDir_string); - if (aniEnv->Object_SetField_Ref(contextObj, filesDirField, reinterpret_cast(filesDir_string)) != ANI_OK) { + ani_string filesDirstring = nullptr; + aniEnv->String_NewUTF8(filesDir.c_str(), filesDir.size(), &filesDirstring); + if (aniEnv->Object_SetField_Ref(contextObj, filesDirField, reinterpret_cast(filesDirstring)) != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Object_SetField_Ref failed"); return {}; } @@ -129,7 +125,7 @@ ani_object EtsAbilityDelegator::CreateEtsBaseContext(ani_env* aniEnv, ani_class return {}; } auto tempDir = context->GetTempDir(); - ani_string tempDirString{}; + ani_string tempDirString = nullptr; aniEnv->String_NewUTF8(tempDir.c_str(), tempDir.size(), &tempDirString); if (aniEnv->Object_SetField_Ref(contextObj, tempDirField, reinterpret_cast(tempDirString)) != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Object_SetField_Ref failed"); @@ -171,7 +167,7 @@ ani_object EtsAbilityDelegator::WrapShellCmdResult(ani_env* env, std::unique_ptr if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Class_FindField failed status: %{public}d", status); } - ani_string aniStringVal {}; + ani_string aniStringVal = nullptr; std::string strResult = result->GetStdResult(); status = env->String_NewUTF8(strResult.c_str(), strResult.size(), &aniStringVal); if (status != ANI_OK) { @@ -196,7 +192,7 @@ ani_object EtsAbilityDelegator::GetAppContext(ani_env* env, [[maybe_unused]]ani_ TAG_LOGE(AAFwkTag::DELEGATOR, "env is nullptr"); return {}; } - ani_class cls; + ani_class cls = nullptr; ani_object nullobj = nullptr; if (ANI_OK != env->FindClass(CONTEXT_CLASS_NAME, &cls)) { TAG_LOGE(AAFwkTag::DELEGATOR, "FindClass Context Failed"); @@ -313,7 +309,7 @@ void EtsAbilityDelegator::PrintSync(ani_env *env, [[maybe_unused]]ani_class aniC TAG_LOGE(AAFwkTag::DELEGATOR, "env is nullptr"); return; } - std::string msgStr; + std::string msgStr = ""; ani_size sz {}; env->String_GetUTF8Size(msg, &sz); msgStr.resize(sz + 1); @@ -360,8 +356,7 @@ void EtsAbilityDelegator::AddAbilityMonitor(ani_env *env, [[maybe_unused]]ani_cl std::shared_ptr monitorImpl = nullptr; if (!GetInstance().ParseMonitorPara(env, monitorObj, monitorImpl)) { TAG_LOGE(AAFwkTag::DELEGATOR, "ParseMonitorPara failed"); - AbilityRuntime::ThrowEtsError(env, INCORRECT_PARAMETERS, - "Parse param monitor failed, monitor must be Monitor."); + AbilityRuntime::ThrowEtsError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INVALID_PARAM); return; } auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(AbilityRuntime::Runtime::Language::ETS); @@ -461,41 +456,40 @@ bool EtsAbilityDelegator::ParseMonitorParaInner(ani_env *env, ani_object monitor TAG_LOGE(AAFwkTag::DELEGATOR, "env or monitorObj is nullptr"); return false; } - ani_class monitorCls; + ani_class monitorCls = nullptr; ani_status status = env->FindClass(ABILITY_MONITOR_INNER_CLASS_NAME, &monitorCls); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "FindClass failed status: %{public}d", status); return false; } - ani_ref moduleNameRef; + ani_ref moduleNameRef = nullptr; status = env->Object_GetPropertyByName_Ref(monitorObj, "moduleName", &moduleNameRef); if (ANI_OK != status) { TAG_LOGE(AAFwkTag::DELEGATOR, "Object_GetField_Ref "); AbilityRuntime::ThrowEtsError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INNER); return false; } - std::string strModuleName; + std::string strModuleName = ""; ani_string aniModuleString = static_cast(moduleNameRef); GetInstance().RetrieveStringFromAni(env, aniModuleString, strModuleName); - ani_ref abilityNameRef; + ani_ref abilityNameRef = nullptr; status = env->Object_GetPropertyByName_Ref(monitorObj, "abilityName", &abilityNameRef); if (ANI_OK != status) { TAG_LOGE(AAFwkTag::DELEGATOR, "Object_GetField_Ref "); AbilityRuntime::ThrowEtsError(env, AbilityRuntime::AbilityErrorCode::ERROR_CODE_INNER); return false; } - std::string strAbilityName; + std::string strAbilityName = ""; ani_string aniAbilityName = static_cast(abilityNameRef); GetInstance().RetrieveStringFromAni(env, aniAbilityName, strAbilityName); std::shared_ptr abilityMonitor = nullptr; if (strModuleName.empty()) { abilityMonitor = std::make_shared(strAbilityName); - abilityMonitor->SetEtsAbilityMonitor(env, monitorObj); } else { abilityMonitor = std::make_shared(strAbilityName, strModuleName); - abilityMonitor->SetEtsAbilityMonitor(env, monitorObj); } + abilityMonitor->SetEtsAbilityMonitor(env, monitorObj); monitorImpl = abilityMonitor; std::shared_ptr reference = std::make_shared(); if (reference != nullptr) { diff --git a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_registry.cpp b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_registry.cpp index da840f60a1a..910ad161db1 100644 --- a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_registry.cpp +++ b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_registry.cpp @@ -47,14 +47,14 @@ static ani_object GetAbilityDelegator(ani_env *env, [[maybe_unused]]ani_class an TAG_LOGE(AAFwkTag::DELEGATOR, "value is nullptr"); return {}; } - ani_boolean isValue; + ani_boolean isValue = false; env->Reference_IsNullishValue(value, &isValue); if (isValue) { TAG_LOGE(AAFwkTag::DELEGATOR, "Reference_IsNullishValue"); return {}; } etsReference = std::make_unique(); - ani_ref result; + ani_ref result = nullptr; auto status = env->GlobalReference_Create(value, &(result)); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Create Gloabl ref for delegator failed %{public}d", status); @@ -95,7 +95,7 @@ void EtsAbilityDelegatorRegistryInit(ani_env *env) TAG_LOGE(AAFwkTag::DELEGATOR, "ResetError failed"); } - ani_namespace ns; + ani_namespace ns = nullptr; status = env->FindNamespace("L@ohos/app/ability/abilityDelegatorRegistry/abilityDelegatorRegistry;", &ns); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "FindNamespace abilityDelegatorRegistry failed status: %{public}d", status); @@ -103,8 +103,11 @@ void EtsAbilityDelegatorRegistryInit(ani_env *env) } std::array kitFunctions = { - ani_native_function {"getAbilityDelegator", nullptr, reinterpret_cast(GetAbilityDelegator)}, - ani_native_function {"getArguments", nullptr, reinterpret_cast(GetArguments)}, + ani_native_function {"getAbilityDelegator", + ":Lapplication/AbilityDelegator/AbilityDelegator;", + reinterpret_cast(GetAbilityDelegator)}, + ani_native_function {"getArguments", ":Lapplication/abilityDelegatorArgs/AbilityDelegatorArgs;", + reinterpret_cast(GetArguments)}, }; status = env->Namespace_BindNativeFunctions(ns, kitFunctions.data(), kitFunctions.size()); diff --git a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_utils.cpp b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_utils.cpp index 6d7aed80ad5..dfbb9130384 100644 --- a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_utils.cpp +++ b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_utils.cpp @@ -19,12 +19,17 @@ #include "ets_ability_delegator.h" #include "hilog_tag_wrapper.h" - namespace OHOS { namespace AbilityDelegatorEts { namespace { constexpr const char* ABILITY_DELEGATOR_CLASS_NAME = "Lapplication/AbilityDelegator/AbilityDelegatorInner;"; constexpr const char* RECORD_CLASS_NAME = "Lescompat/Record;"; +constexpr const char* VOID_CONTEXT = ":Lapplication/Context/Context;"; +constexpr const char* STRING_NUMBER_ASYNCCALLBACK = "Lstd/core/String;DLutils/AbilityUtils/AsyncCallbackWrapper;:V"; +constexpr const char* STRING_VOID = "Lstd/core/String;:V"; +constexpr const char* MONITOR_ASYNCCALLBACK_VOID = + "Lapplication/AbilityMonitor/AbilityMonitor;Lutils/AbilityUtils/AsyncCallbackWrapper;:V"; +constexpr const char* WANT_ASYNCCALLBACK = "L@ohos/app/ability/Want/Want;Lutils/AbilityUtils/AsyncCallbackWrapper;:V"; constexpr const char* ARGS_ABILITY_DELEGATOR_CLASS_NAME = "Lapplication/abilityDelegatorArgs/AbilityDelegatorArgsInner;"; } @@ -35,15 +40,16 @@ bool BindFunctions(ani_env *aniEnv, ani_class abilityDelegator) return false; } std::array functions = { - ani_native_function {"getAppContext", nullptr, reinterpret_cast(EtsAbilityDelegator::GetAppContext)}, - ani_native_function {"nativeExecuteShellCommand", nullptr, + ani_native_function {"getAppContext", VOID_CONTEXT, + reinterpret_cast(EtsAbilityDelegator::GetAppContext)}, + ani_native_function {"nativeExecuteShellCommand", STRING_NUMBER_ASYNCCALLBACK, reinterpret_cast(EtsAbilityDelegator::ExecuteShellCommand)}, - ani_native_function {"nativeFinishTest", nullptr, reinterpret_cast(EtsAbilityDelegator::FinishTest)}, - ani_native_function {"printSync", nullptr, reinterpret_cast(EtsAbilityDelegator::PrintSync)}, - ani_native_function {"nativeAddAbilityMonitor", nullptr, + ani_native_function {"nativeFinishTest", STRING_NUMBER_ASYNCCALLBACK, + reinterpret_cast(EtsAbilityDelegator::FinishTest)}, + ani_native_function {"printSync", STRING_VOID, reinterpret_cast(EtsAbilityDelegator::PrintSync)}, + ani_native_function {"nativeAddAbilityMonitor", MONITOR_ASYNCCALLBACK_VOID, reinterpret_cast(EtsAbilityDelegator::AddAbilityMonitor)}, - ani_native_function {"nativeStartAbility", - "L@ohos/app/ability/Want/Want;Lutils/AbilityUtils/AsyncCallbackWrapper;:V", + ani_native_function {"nativeStartAbility", WANT_ASYNCCALLBACK, reinterpret_cast(EtsAbilityDelegator::StartAbility)}, }; ani_status status = aniEnv->Class_BindNativeMethods(abilityDelegator, functions.data(), functions.size()); @@ -100,7 +106,7 @@ void SetBundleName(ani_env *aniEnv, ani_class arguments, ani_object argumentObje return; } ani_status status = ANI_ERROR; - ani_string aniStr; + ani_string aniStr = nullptr; // Get a ani_string from std::string status = aniEnv->String_NewUTF8(bundleName.c_str(), bundleName.length(), &aniStr); if (status != ANI_OK) { @@ -110,7 +116,7 @@ void SetBundleName(ani_env *aniEnv, ani_class arguments, ani_object argumentObje TAG_LOGD(AAFwkTag::DELEGATOR, "String_NewUTF8 success"); // find the setter method - ani_method nameSetter; + ani_method nameSetter = nullptr; status = aniEnv->Class_FindMethod(arguments, "bundleName", nullptr, &nameSetter); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Class_FindMethod failed status: %{public}d", status); @@ -136,25 +142,25 @@ void SetParameters(ani_env *aniEnv, ani_class arguments, ani_object argumentObje return; } ani_status status = ANI_ERROR; - ani_class recordCls; + ani_class recordCls = nullptr; status = aniEnv->FindClass(RECORD_CLASS_NAME, &recordCls); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "FindClass failed status: %{public}d", status); return; } - ani_method recordGetMethod; + ani_method recordGetMethod = nullptr; status = aniEnv->Class_FindMethod(recordCls, "$_get", "Lstd/core/Object;:Lstd/core/Object;", &recordGetMethod); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Class_FindMethod failed status: %{public}d", status); return; } - ani_method recordSetMethod; + ani_method recordSetMethod = nullptr; status = aniEnv->Class_FindMethod(recordCls, "$_set", "Lstd/core/Object;Lstd/core/Object;:V", &recordSetMethod); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Class_FindMethod failed status: %{public}d", status); return; } - ani_ref parameterRef; + ani_ref parameterRef = nullptr; status = aniEnv->Object_CallMethodByName_Ref(argumentObject, "parameters", ":Lescompat/Record;", ¶meterRef); if (status != ANI_OK) { @@ -165,8 +171,8 @@ void SetParameters(ani_env *aniEnv, ani_class arguments, ani_object argumentObje for (auto iter = paras.begin(); iter != paras.end(); ++iter) { std::string key = iter->first; std::string value = iter->second; - ani_string ani_key; - ani_string ani_value; + ani_string ani_key = nullptr; + ani_string ani_value = nullptr; status = aniEnv->String_NewUTF8(key.c_str(), key.length(), &ani_key); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "String_NewUTF8 key failed status: %{public}d", status); @@ -193,7 +199,7 @@ void SetTestCaseNames(ani_env *aniEnv, ani_class arguments, ani_object argumentO return; } ani_status status = ANI_ERROR; - ani_string aniStr; + ani_string aniStr = nullptr; status = aniEnv->String_NewUTF8(testcaseNames.c_str(), testcaseNames.length(), &aniStr); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "String_NewUTF8 failed status: %{public}d", status); @@ -202,7 +208,7 @@ void SetTestCaseNames(ani_env *aniEnv, ani_class arguments, ani_object argumentO TAG_LOGD(AAFwkTag::DELEGATOR, "String_NewUTF8 success"); // find the setter method - ani_method nameSetter; + ani_method nameSetter = nullptr; status = aniEnv->Class_FindMethod(arguments, "testCaseNames", nullptr, &nameSetter); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Class_FindMethod failed status: %{public}d", status); @@ -227,7 +233,7 @@ void SetTestRunnerClassName(ani_env *aniEnv, ani_class arguments, ani_object arg return; } ani_status status = ANI_ERROR; - ani_string aniStr; + ani_string aniStr = nullptr; status = aniEnv->String_NewUTF8(className.c_str(), className.length(), &aniStr); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "String_NewUTF8 failed status: %{public}d", status); @@ -236,7 +242,7 @@ void SetTestRunnerClassName(ani_env *aniEnv, ani_class arguments, ani_object arg TAG_LOGD(AAFwkTag::DELEGATOR, "String_NewUTF8 success"); // find the setter method - ani_method nameSetter; + ani_method nameSetter = nullptr; status = aniEnv->Class_FindMethod(arguments, "testRunnerClassName", nullptr, &nameSetter); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Class_FindMethod failed status: %{public}d", status); diff --git a/frameworks/ets/ani/ability_delegator/src/ets_ability_monitor.cpp b/frameworks/ets/ani/ability_delegator/src/ets_ability_monitor.cpp index 9ce5b78aa6d..c0b5a948afd 100644 --- a/frameworks/ets/ani/ability_delegator/src/ets_ability_monitor.cpp +++ b/frameworks/ets/ani/ability_delegator/src/ets_ability_monitor.cpp @@ -183,7 +183,7 @@ std::shared_ptr EtsAbilityMonitor::GetRuntim TAG_LOGE(AAFwkTag::DELEGATOR, "abilityObj is expired"); return nullptr; } - auto etsbaseProperty = std::static_pointer_cast(baseProperty); + auto etsbaseProperty = std::static_pointer_cast(baseProperty); auto runtimeObj = etsbaseProperty->object_.lock(); if (!runtimeObj) { TAG_LOGE(AAFwkTag::DELEGATOR, "runtimeObj is nullptr"); diff --git a/frameworks/native/ability/native/BUILD.gn b/frameworks/native/ability/native/BUILD.gn index 0b8fcb13732..414982618d5 100644 --- a/frameworks/native/ability/native/BUILD.gn +++ b/frameworks/native/ability/native/BUILD.gn @@ -773,6 +773,7 @@ ohos_shared_library("uiabilitykit_native") { "${ability_runtime_path}/interfaces/kits/native/ability/native", "${ability_runtime_path}/interfaces/kits/native/ability/native/ability_runtime", "${ability_runtime_path}/interfaces/kits/native/ability/native/ui_extension_ability", + "${ability_runtime_path}/interfaces/kits/native/appkit/ability_delegator", "${ability_runtime_path}/utils/global/time/include", ] diff --git a/frameworks/native/ability/native/ability_runtime/ets_ui_ability.cpp b/frameworks/native/ability/native/ability_runtime/ets_ui_ability.cpp index fb20d510af8..fcb3020ca07 100644 --- a/frameworks/native/ability/native/ability_runtime/ets_ui_ability.cpp +++ b/frameworks/native/ability/native/ability_runtime/ets_ui_ability.cpp @@ -17,6 +17,7 @@ #include +#include "ability_delegator_infos.h" #include "ability_delegator_registry.h" #include "ani_common_want.h" #include "app_recovery.h" @@ -261,9 +262,11 @@ void EtsUIAbility::OnStart(const Want &want, sptr sessionInf CallObjectMethod(false, "onCreate", nullptr, wantObj, launchParamObj); auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator( AbilityRuntime::Runtime::Language::ETS); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformStart"); - delegator->PostPerformStart(CreateADelegatorAbilityProperty()); + property->object_ = etsAbilityObj_; + delegator->PostPerformStart(property); } TAG_LOGD(AAFwkTag::UIABILITY, "OnStart end"); } @@ -335,11 +338,12 @@ void EtsUIAbility::OnStopCallback() { auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator( AbilityRuntime::Runtime::Language::ETS); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformStop"); - delegator->PostPerformStop(CreateADelegatorAbilityProperty()); + property->object_ = etsAbilityObj_; + delegator->PostPerformStop(property); } - bool ret = ConnectionManager::GetInstance().DisconnectCaller(AbilityContext::token_); if (ret) { ConnectionManager::GetInstance().ReportConnectionLeakEvent(getpid(), gettid()); @@ -372,9 +376,11 @@ void EtsUIAbility::OnSceneCreated() CallObjectMethod(false, "onWindowStageCreate", nullptr, etsAppWindowStage); auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator( AbilityRuntime::Runtime::Language::ETS); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformScenceCreated"); - delegator->PostPerformScenceCreated(CreateADelegatorAbilityProperty()); + property->object_ = etsAbilityObj_; + delegator->PostPerformScenceCreated(property); } TAG_LOGD(AAFwkTag::UIABILITY, "OnSceneCreated end"); } @@ -394,9 +400,11 @@ void EtsUIAbility::onSceneDestroyed() } auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator( AbilityRuntime::Runtime::Language::ETS); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformScenceDestroyed"); - delegator->PostPerformScenceDestroyed(CreateADelegatorAbilityProperty()); + property->object_ = etsAbilityObj_; + delegator->PostPerformScenceDestroyed(property); } TAG_LOGD(AAFwkTag::UIABILITY, "onSceneDestroyed end"); } @@ -437,9 +445,11 @@ void EtsUIAbility::CallOnForegroundFunc(const Want &want) CallObjectMethod(false, "onForeground", nullptr, wantRef); auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator( AbilityRuntime::Runtime::Language::ETS); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformForeground"); - delegator->PostPerformForeground(CreateADelegatorAbilityProperty()); + property->object_ = etsAbilityObj_; + delegator->PostPerformForeground(property); } TAG_LOGD(AAFwkTag::UIABILITY, "CallOnForegroundFunc end"); } @@ -452,9 +462,11 @@ void EtsUIAbility::OnBackground() UIAbility::OnBackground(); auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator( AbilityRuntime::Runtime::Language::ETS); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformBackground"); - delegator->PostPerformBackground(CreateADelegatorAbilityProperty()); + property->object_ = etsAbilityObj_; + delegator->PostPerformBackground(property); } TAG_LOGD(AAFwkTag::UIABILITY, "OnBackground end"); } @@ -757,30 +769,5 @@ bool EtsUIAbility::CallObjectMethod(bool withResult, const char *name, const cha TAG_LOGI(AAFwkTag::UIABILITY, "CallObjectMethod end, name: %{public}s", name); return false; } - -std::shared_ptr EtsUIAbility::CreateADelegatorAbilityProperty() -{ - if (abilityContext_ == nullptr) { - TAG_LOGE(AAFwkTag::UIABILITY, "null abilityContext_"); - return nullptr; - } - auto property = std::make_shared(); - property->token_ = abilityContext_->GetToken(); - property->name_ = GetAbilityName(); - property->moduleName_ = GetModuleName(); - if (GetApplicationInfo() == nullptr || GetApplicationInfo()->bundleName.empty()) { - property->fullName_ = GetAbilityName(); - } else { - std::string::size_type pos = GetAbilityName().find(GetApplicationInfo()->bundleName); - if (pos == std::string::npos || pos != 0) { - property->fullName_ = GetApplicationInfo()->bundleName + "." + GetAbilityName(); - } else { - property->fullName_ = GetAbilityName(); - } - } - property->lifecycleState_ = GetState(); - property->object_ = etsAbilityObj_; - return property; -} } // namespace AbilityRuntime } // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp b/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp index 10e17da8bb9..ed5aeb34aec 100644 --- a/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp +++ b/frameworks/native/ability/native/ability_runtime/js_ui_ability.cpp @@ -405,13 +405,7 @@ void JsUIAbility::OnStart(const Want &want, sptr sessionInfo AddLifecycleEventBeforeJSCall(FreezeUtil::TimeoutState::FOREGROUND, methodName); CallObjectMethod("onCreate", argv, ArraySize(argv)); AddLifecycleEventAfterJSCall(FreezeUtil::TimeoutState::FOREGROUND, methodName); - - auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(); - if (delegator) { - TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformStart"); - delegator->PostPerformStart(CreateADelegatorAbilityProperty()); - } - + HandleAbilityDelegatorStart(); applicationContext = AbilityRuntime::Context::GetApplicationContext(); if (applicationContext != nullptr) { applicationContext->DispatchOnAbilityCreate(jsAbilityObj_); @@ -419,6 +413,17 @@ void JsUIAbility::OnStart(const Want &want, sptr sessionInfo TAG_LOGD(AAFwkTag::UIABILITY, "end"); } +void JsUIAbility::HandleAbilityDelegatorStart() +{ + auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(); + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { + TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformStart"); + property->object_ = jsAbilityObj_; + delegator->PostPerformStart(property); + } +} + void JsUIAbility::AddLifecycleEventBeforeJSCall(FreezeUtil::TimeoutState state, const std::string &methodName) const { auto entry = std::string("JsUIAbility::") + methodName + " begin"; @@ -525,9 +530,11 @@ void JsUIAbility::OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackI void JsUIAbility::OnStopCallback() { auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformStop"); - delegator->PostPerformStop(CreateADelegatorAbilityProperty()); + property->object_ = jsAbilityObj_; + delegator->PostPerformStop(property); } bool ret = ConnectionManager::GetInstance().DisconnectCaller(AbilityContext::token_); @@ -571,9 +578,11 @@ void JsUIAbility::OnSceneCreated() } auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformScenceCreated"); - delegator->PostPerformScenceCreated(CreateADelegatorAbilityProperty()); + property->object_ = jsAbilityObj_; + delegator->PostPerformScenceCreated(property); } applicationContext = AbilityRuntime::Context::GetApplicationContext(); @@ -608,9 +617,11 @@ void JsUIAbility::OnSceneRestored() } auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformScenceRestored"); - delegator->PostPerformScenceRestored(CreateADelegatorAbilityProperty()); + property->object_ = jsAbilityObj_; + delegator->PostPerformScenceRestored(property); } } @@ -652,9 +663,11 @@ void JsUIAbility::onSceneDestroyed() } auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformScenceDestroyed"); - delegator->PostPerformScenceDestroyed(CreateADelegatorAbilityProperty()); + property->object_ = jsAbilityObj_; + delegator->PostPerformScenceDestroyed(property); } applicationContext = AbilityRuntime::Context::GetApplicationContext(); @@ -714,9 +727,11 @@ void JsUIAbility::CallOnForegroundFunc(const Want &want) AddLifecycleEventAfterJSCall(FreezeUtil::TimeoutState::FOREGROUND, methodName); auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformForeground"); - delegator->PostPerformForeground(CreateADelegatorAbilityProperty()); + property->object_ = jsAbilityObj_; + delegator->PostPerformForeground(property); } applicationContext = AbilityRuntime::Context::GetApplicationContext(); @@ -743,9 +758,11 @@ void JsUIAbility::OnBackground() UIAbility::OnBackground(); auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(); - if (delegator) { + auto property = std::make_shared(); + if (delegator && CreateProperty(abilityContext_, property)) { TAG_LOGD(AAFwkTag::UIABILITY, "call PostPerformBackground"); - delegator->PostPerformBackground(CreateADelegatorAbilityProperty()); + property->object_ = jsAbilityObj_; + delegator->PostPerformBackground(property); } applicationContext = AbilityRuntime::Context::GetApplicationContext(); @@ -2017,31 +2034,6 @@ int32_t JsUIAbility::CallSaveStatePromise(napi_value result, CallOnSaveStateInfo return ERR_OK; } -std::shared_ptr JsUIAbility::CreateADelegatorAbilityProperty() -{ - if (abilityContext_ == nullptr) { - TAG_LOGE(AAFwkTag::UIABILITY, "null abilityContext_"); - return nullptr; - } - auto property = std::make_shared(); - property->token_ = abilityContext_->GetToken(); - property->name_ = GetAbilityName(); - property->moduleName_ = GetModuleName(); - if (GetApplicationInfo() == nullptr || GetApplicationInfo()->bundleName.empty()) { - property->fullName_ = GetAbilityName(); - } else { - std::string::size_type pos = GetAbilityName().find(GetApplicationInfo()->bundleName); - if (pos == std::string::npos || pos != 0) { - property->fullName_ = GetApplicationInfo()->bundleName + "." + GetAbilityName(); - } else { - property->fullName_ = GetAbilityName(); - } - } - property->lifecycleState_ = GetState(); - property->object_ = jsAbilityObj_; - return property; -} - void JsUIAbility::Dump(const std::vector ¶ms, std::vector &info) { UIAbility::Dump(params, info); diff --git a/frameworks/native/ability/native/ui_ability.cpp b/frameworks/native/ability/native/ui_ability.cpp index 71cf562509d..bfeb03df202 100644 --- a/frameworks/native/ability/native/ui_ability.cpp +++ b/frameworks/native/ability/native/ui_ability.cpp @@ -306,6 +306,30 @@ void UIAbility::DestroyInstance() TAG_LOGD(AAFwkTag::UIABILITY, "called"); } +bool UIAbility::CreateProperty(std::shared_ptr abilityContext, + std::shared_ptr delegatorAbilityProperty) +{ + if (abilityContext == nullptr || delegatorAbilityProperty == nullptr) { + TAG_LOGE(AAFwkTag::UIABILITY, "null abilityContext or delegatorAbilityProperty"); + return false; + } + delegatorAbilityProperty->token_ = abilityContext->GetToken(); + delegatorAbilityProperty->name_ = GetAbilityName(); + delegatorAbilityProperty->moduleName_ = GetModuleName(); + if (GetApplicationInfo() == nullptr || GetApplicationInfo()->bundleName.empty()) { + delegatorAbilityProperty->fullName_ = GetAbilityName(); + } else { + std::string::size_type pos = GetAbilityName().find(GetApplicationInfo()->bundleName); + if (pos == std::string::npos || pos != 0) { + delegatorAbilityProperty->fullName_ = GetApplicationInfo()->bundleName + "." + GetAbilityName(); + } else { + delegatorAbilityProperty->fullName_ = GetAbilityName(); + } + } + delegatorAbilityProperty->lifecycleState_ = GetState(); + return true; +} + bool UIAbility::IsRestoredInContinuation() const { if (abilityContext_ == nullptr) { diff --git a/interfaces/kits/native/ability/native/ability_runtime/ets_ui_ability.h b/interfaces/kits/native/ability/native/ability_runtime/ets_ui_ability.h index fc559230dfd..dc39506ebb3 100644 --- a/interfaces/kits/native/ability/native/ability_runtime/ets_ui_ability.h +++ b/interfaces/kits/native/ability/native/ability_runtime/ets_ui_ability.h @@ -152,7 +152,6 @@ private: private: bool CallObjectMethod(bool withResult, const char *name, const char *signature, ...); ani_object CreateAppWindowStage(); - std::shared_ptr CreateADelegatorAbilityProperty(); void SetAbilityContext(std::shared_ptr abilityInfo, std::shared_ptr want, const std::string &moduleName, const std::string &srcPath, const std::shared_ptr &application); void DoOnForegroundForSceneIsNull(const Want &want); diff --git a/interfaces/kits/native/ability/native/ability_runtime/js_ui_ability.h b/interfaces/kits/native/ability/native/ability_runtime/js_ui_ability.h index 26121ff0013..29f5194959e 100644 --- a/interfaces/kits/native/ability/native/ability_runtime/js_ui_ability.h +++ b/interfaces/kits/native/ability/native/ability_runtime/js_ui_ability.h @@ -410,7 +410,6 @@ private: bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo *callbackInfo); int32_t CallSaveStatePromise(napi_value result, CallOnSaveStateInfo info); std::unique_ptr CreateAppWindowStage(); - std::shared_ptr CreateADelegatorAbilityProperty(); sptr SetNewRuleFlagToCallee(napi_env env, napi_value remoteJsObj); void SetAbilityContext(std::shared_ptr abilityInfo, std::shared_ptr want, const std::string &moduleName, const std::string &srcPath); @@ -427,6 +426,7 @@ private: void ReleaseOnContinueAsset(const napi_env env, napi_value &promise, napi_ref &jsWantParamsRef, AppExecFwk::AbilityTransactionCallbackInfo *callbackInfo); void RemoveShareRouterByBundleType(const Want &want); + void HandleAbilityDelegatorStart(); JsRuntime &jsRuntime_; std::shared_ptr shellContextRef_; diff --git a/interfaces/kits/native/ability/native/ui_ability.h b/interfaces/kits/native/ability/native/ui_ability.h index 539f3bdd87d..6af9cf48ba1 100644 --- a/interfaces/kits/native/ability/native/ui_ability.h +++ b/interfaces/kits/native/ability/native/ui_ability.h @@ -46,6 +46,7 @@ class LifeCycle; class ContinuationHandlerStage; class ContinuationManagerStage; class InsightIntentExecuteParam; +struct BaseDelegatorAbilityProperty; struct InsightIntentExecuteResult; using InsightIntentExecutorAsyncCallback = AbilityTransactionCallbackInfo; } // namespace AppExecFwk @@ -335,6 +336,9 @@ public: void SetIsSilentForeground(bool isSilentForeground); + bool CreateProperty(std::shared_ptr abilityContext, + std::shared_ptr delegatorAbilityProperty); + protected: const AAFwk::LaunchParam &GetLaunchParam() const; bool IsRestoredInContinuation() const; diff --git a/interfaces/kits/native/appkit/ability_delegator/ability_delegator_infos.h b/interfaces/kits/native/appkit/ability_delegator/ability_delegator_infos.h index 56baa8b1107..e65a8d28cd7 100644 --- a/interfaces/kits/native/appkit/ability_delegator/ability_delegator_infos.h +++ b/interfaces/kits/native/appkit/ability_delegator/ability_delegator_infos.h @@ -44,7 +44,7 @@ struct ADelegatorAbilityProperty : public BaseDelegatorAbilityProperty { std::weak_ptr object_; }; -struct ETSDelegatorAbilityProperty : public BaseDelegatorAbilityProperty { +struct EtsDelegatorAbilityProperty : public BaseDelegatorAbilityProperty { std::weak_ptr object_; }; diff --git a/test/unittest/frameworks_kits_ability_native_test/BUILD.gn b/test/unittest/frameworks_kits_ability_native_test/BUILD.gn index 475cb31348a..44ea7a191b0 100644 --- a/test/unittest/frameworks_kits_ability_native_test/BUILD.gn +++ b/test/unittest/frameworks_kits_ability_native_test/BUILD.gn @@ -29,19 +29,20 @@ config("module_private_config") { include_dirs = [ "${ability_runtime_innerkits_path}/ability_manager/include", "${ability_runtime_innerkits_path}/app_manager/include/appmgr", + "${ability_runtime_path}/interfaces/kits/native/ability/ability_runtime", "${ability_runtime_path}/interfaces/kits/native/ability/native", "${ability_runtime_path}/interfaces/kits/native/appkit/ability_bundle_manager_helper", - "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include", + "${ability_runtime_path}/interfaces/kits/native/appkit/ability_delegator", "${ability_runtime_path}/interfaces/kits/native/appkit/app", "${ability_runtime_path}/interfaces/kits/native/appkit/app_startup", "${ability_runtime_path}/interfaces/kits/native/appkit/app/task", - "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/aakit/include", - "${ability_runtime_test_path}/mock/frameworks_kits_appkit_native_test/include", "${ability_runtime_services_path}/abilitymgr/include/utils", "${ability_runtime_services_path}/abilitymgr/include", "${ability_runtime_services_path}/common/include", + "${ability_runtime_test_path}/mock/frameworks_kits_ability_native_test/include", + "${ability_runtime_test_path}/mock/frameworks_kits_appkit_native_test/include", + "${ability_runtime_test_path}/mock/services_abilitymgr_test/libs/aakit/include", "${ability_runtime_test_path}/mock/services_appmgr_test/include", - "${ability_runtime_path}/interfaces/kits/native/ability/ability_runtime", ] cflags = [] if (target_cpu == "arm") { diff --git a/test/unittest/frameworks_kits_ability_native_test/ui_ability_test.cpp b/test/unittest/frameworks_kits_ability_native_test/ui_ability_test.cpp index ed83c44ed03..d29f840e5b5 100644 --- a/test/unittest/frameworks_kits_ability_native_test/ui_ability_test.cpp +++ b/test/unittest/frameworks_kits_ability_native_test/ui_ability_test.cpp @@ -21,6 +21,7 @@ #undef protected #undef private #include "ability_context_impl.h" +#include "ability_delegator_infos.h" #include "ability_handler.h" #include "ability_recovery.h" #include "fa_ability_thread.h" @@ -1776,5 +1777,71 @@ HWTEST_F(UIAbilityBaseTest, UIAbility_IsStartByScb_0300, TestSize.Level1) EXPECT_FALSE(result); TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); } + +/** + * @tc.name: UIAbility_CreateProperty_0100 + * @tc.desc: CreateProperty test + * @tc.desc: Verify function CreateProperty. + */ +HWTEST_F(UIAbilityBaseTest, UIAbility_CreateProperty_0100, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + std::shared_ptr uiAbility = std::make_shared(); + ASSERT_NE(uiAbility, nullptr); + bool result = uiAbility->CreateProperty(nullptr, nullptr); + EXPECT_FALSE(result); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: UIAbility_CreateProperty_0200 + * @tc.desc: CreateProperty test + * @tc.desc: Verify function CreateProperty. + */ +HWTEST_F(UIAbilityBaseTest, UIAbility_CreateProperty_0200, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + std::shared_ptr uiAbility = std::make_shared(); + ASSERT_NE(uiAbility, nullptr); + auto abilityContext = uiAbility->GetAbilityContext(); + ASSERT_NE(abilityContext, nullptr); + bool result = uiAbility->CreateProperty(abilityContext, nullptr); + EXPECT_FALSE(result); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: UIAbility_CreateProperty_0300 + * @tc.desc: CreateProperty test + * @tc.desc: Verify function CreateProperty. + */ +HWTEST_F(UIAbilityBaseTest, UIAbility_CreateProperty_0300, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + std::shared_ptr uiAbility = std::make_shared(); + ASSERT_NE(uiAbility, nullptr); + auto property = std::make_shared(); + bool result = uiAbility->CreateProperty(nullptr, property); + EXPECT_FALSE(result); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} + +/** + * @tc.name: UIAbility_CreateProperty_0400 + * @tc.desc: CreateProperty test + * @tc.desc: Verify function CreateProperty. + */ +HWTEST_F(UIAbilityBaseTest, UIAbility_CreateProperty_0400, TestSize.Level1) +{ + TAG_LOGI(AAFwkTag::TEST, "%{public}s start.", __func__); + std::shared_ptr uiAbility = std::make_shared(); + ASSERT_NE(uiAbility, nullptr); + auto abilityContext = uiAbility->GetAbilityContext(); + ASSERT_NE(abilityContext, nullptr); + auto property = std::make_shared(); + bool result = uiAbility->CreateProperty(abilityContext, property); + EXPECT_TRUE(result); + TAG_LOGI(AAFwkTag::TEST, "%{public}s end.", __func__); +} } // namespace AppExecFwk } // namespace OHOS -- Gitee