mirror of
https://github.com/openharmony/ability_ability_runtime.git
synced 2026-08-24 22:21:36 -04:00
!9290 [fix]增加对sendableContext的类型校验[applicationContext]
Merge pull request !9290 from 张亚菲/zyfContext
This commit is contained in:
@@ -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",
|
||||
]
|
||||
|
||||
@@ -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>(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<ApplicationContext>(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<AbilityContext>(context);
|
||||
auto uiAbilityContext = Context::ConvertTo<AbilityContext>(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>(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)
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
|
||||
namespace OHOS {
|
||||
namespace AbilityRuntime {
|
||||
const size_t ApplicationContext::CONTEXT_TYPE_ID(std::hash<const char*> {} ("ApplicationContext"));
|
||||
std::vector<std::shared_ptr<AbilityLifecycleCallback>> ApplicationContext::callbacks_;
|
||||
std::vector<std::shared_ptr<EnvironmentCallback>> ApplicationContext::envCallbacks_;
|
||||
std::vector<std::weak_ptr<ApplicationStateChangeCallback>> ApplicationContext::applicationStateCallback_;
|
||||
|
||||
@@ -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> contextImpl_;
|
||||
static std::vector<std::shared_ptr<AbilityLifecycleCallback>> callbacks_;
|
||||
|
||||
Reference in New Issue
Block a user