From 45f1d5389721f72872e1e386788d4fbdf1391aab Mon Sep 17 00:00:00 2001 From: "zhangyafei.echo" Date: Sat, 6 Jul 2024 10:24:48 +0800 Subject: [PATCH] Add type check to applicaiton context. Sig:SIG_ApplicationFramework Feature or BugFix: Feature Binary Source: No Signed-off-by: zhangyafei.echo Change-Id: I9172603efd204b82e6af17e87b914b5d682891d3 --- .../app/sendable_context_manager/BUILD.gn | 1 + .../js_sendable_context_manager.cpp | 28 ++++++++++++++++--- .../context/application_context.cpp | 1 + .../context/application_context.h | 10 +++++++ 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/frameworks/js/napi/app/sendable_context_manager/BUILD.gn b/frameworks/js/napi/app/sendable_context_manager/BUILD.gn index d40d0ba834..a71acf76a6 100644 --- a/frameworks/js/napi/app/sendable_context_manager/BUILD.gn +++ b/frameworks/js/napi/app/sendable_context_manager/BUILD.gn @@ -43,6 +43,7 @@ ohos_shared_library("sendablecontextmanager_napi") { "${ability_runtime_napi_path}/inner/napi_common:napi_common", "${ability_runtime_native_path}/ability:ability_context_native", "${ability_runtime_native_path}/ability/native:abilitykit_native", + "${ability_runtime_native_path}/appkit:app_context", "${ability_runtime_native_path}/appkit:app_context_utils", "${ability_runtime_native_path}/appkit:appkit_native", ] diff --git a/frameworks/js/napi/app/sendable_context_manager/js_sendable_context_manager.cpp b/frameworks/js/napi/app/sendable_context_manager/js_sendable_context_manager.cpp index 6500d8bc7f..c0bf5111e1 100644 --- a/frameworks/js/napi/app/sendable_context_manager/js_sendable_context_manager.cpp +++ b/frameworks/js/napi/app/sendable_context_manager/js_sendable_context_manager.cpp @@ -16,6 +16,7 @@ #include "js_sendable_context_manager.h" #include "ability_context.h" +#include "application_context.h" #include "context.h" #include "js_ability_context.h" #include "js_ability_stage_context.h" @@ -88,8 +89,14 @@ napi_value CreateJsBaseContextFromSendable(napi_env env, void* wrapped) return nullptr; } + auto contextPtr = Context::ConvertTo(context); + if (contextPtr == nullptr) { + TAG_LOGE(AAFwkTag::CONTEXT, "Convert to context failed."); + return nullptr; + } + // create normal context - return CreateJsBaseContext(env, context); + return CreateJsBaseContext(env, contextPtr); } napi_value CreateJsApplicationContextFromSendable(napi_env env, void* wrapped) @@ -107,6 +114,12 @@ napi_value CreateJsApplicationContextFromSendable(napi_env env, void* wrapped) return nullptr; } + auto applicationContext = Context::ConvertTo(context); + if (applicationContext == nullptr) { + TAG_LOGE(AAFwkTag::CONTEXT, "Convert to application context failed."); + return nullptr; + } + // create application context return JsApplicationContextUtils::CreateJsApplicationContext(env); } @@ -145,7 +158,7 @@ napi_value CreateJsUIAbilityContextFromSendable(napi_env env, void* wrapped) return nullptr; } - auto uiAbilityContext = AbilityRuntime::Context::ConvertTo(context); + auto uiAbilityContext = Context::ConvertTo(context); if (uiAbilityContext == nullptr) { TAG_LOGE(AAFwkTag::CONTEXT, "Convert to UIAbility context failed."); return nullptr; @@ -216,13 +229,20 @@ private: auto context = GetStageModeContext(env, info.argv[0]); if (context == nullptr) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "get context failed"); + TAG_LOGE(AAFwkTag::CONTEXT, "Get context failed"); ThrowInvalidParamError(env, "Parse param context failed, must not be nullptr."); return CreateJsUndefined(env); } + auto contextPtr = Context::ConvertTo(context); + if (contextPtr == nullptr) { + TAG_LOGE(AAFwkTag::CONTEXT, "Convert to context failed."); + ThrowInvalidParamError(env, "Parse param context failed, must be a context."); + return CreateJsUndefined(env); + } + // create sendable context - return CreateSendableContextObject(env, context); + return CreateSendableContextObject(env, contextPtr); } napi_value OnConvertToContext(napi_env env, NapiCallbackInfo &info) diff --git a/frameworks/native/appkit/ability_runtime/context/application_context.cpp b/frameworks/native/appkit/ability_runtime/context/application_context.cpp index b9052269de..145e6bdfc6 100644 --- a/frameworks/native/appkit/ability_runtime/context/application_context.cpp +++ b/frameworks/native/appkit/ability_runtime/context/application_context.cpp @@ -26,6 +26,7 @@ namespace OHOS { namespace AbilityRuntime { +const size_t ApplicationContext::CONTEXT_TYPE_ID(std::hash {} ("ApplicationContext")); std::vector> ApplicationContext::callbacks_; std::vector> ApplicationContext::envCallbacks_; std::vector> ApplicationContext::applicationStateCallback_; diff --git a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h index cc594b7235..9742bd0c19 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h @@ -137,6 +137,16 @@ public: void SetCurrentAppCloneIndex(int32_t appIndex); int32_t GetCurrentAppMode(); void SetCurrentAppMode(int32_t appIndex); + + using SelfType = ApplicationContext; + static const size_t CONTEXT_TYPE_ID; + +protected: + bool IsContext(size_t contextTypeId) override + { + return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId); + } + private: std::shared_ptr contextImpl_; static std::vector> callbacks_;